Explorar o código

implement insert and insertN

xuty %!s(int64=4) %!d(string=hai) anos
pai
achega
79d5b1316a
Modificáronse 1 ficheiros con 19 adicións e 1 borrados
  1. 19 1
      lib/buffer/buffer_line.dart

+ 19 - 1
lib/buffer/buffer_line.dart

@@ -57,7 +57,25 @@ class BufferLine {
   }
 
   void insertN(int index, int count) {
-    // TODO: implement insertN()
+    //                       start
+    // +--------------------------|-----------------------------------+
+    // |                          |                                   |
+    // +--------------------------\--\--------------------------------+
+    //                             \  \
+    //                              \  \
+    //                               v  v
+    // +--------------------------|--|--------------------------------+
+    // |                          |  |                                |
+    // +--------------------------|--|--------------------------------+
+    //                       start   start+offset
+
+    final start = (index * _cellSize).clamp(0, _cells.lengthInBytes);
+    final offset = (count * _cellSize).clamp(0, _cells.lengthInBytes - start);
+
+    final cells = _cells.buffer.asInt8List();
+    for (var i = _cells.lengthInBytes - offset - 1; i >= start; i++) {
+      cells[i + offset] = cells[i];
+    }
   }
 
   void clear() {