瀏覽代碼

implement insert and insertN

xuty 4 年之前
父節點
當前提交
79d5b1316a
共有 1 個文件被更改,包括 19 次插入1 次删除
  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() {