Fix TypedDataBuffer.insertAll() with an Iterable.
Closes #1.
R=lrn@google.com
Review URL: https://codereview.chromium.org//1728943003 .
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6be0b2d..edfc03d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,12 @@
+## 1.1.2
+
+* Fix a bug where `TypedDataBuffer.insertAll` could fail to insert some elements
+ of an `Iterable`.
+
## 1.1.1
+
* Optimize `insertAll` with an `Iterable` argument and no end-point.
+
## 1.1.0
* Add `start` and `end` parameters to the `addAll()` and `insertAll()` methods
diff --git a/lib/typed_buffers.dart b/lib/typed_buffers.dart
index c3c0f38..2d1ca13 100644
--- a/lib/typed_buffers.dart
+++ b/lib/typed_buffers.dart
@@ -59,7 +59,7 @@
}
void _add(E value) {
- if (_length == _buffer.length) _grow();
+ if (_length == _buffer.length) _grow(_length);
_buffer[_length++] = value;
}
@@ -125,25 +125,26 @@
// Add elements at end, growing as appropriate, then put them back at
// position [index] using flip-by-double-reverse.
- if (end != null) values = values.take(end);
- int writeIndex = _length;
- int skipCount = start;
+ var writeIndex = _length;
+ var skipCount = start;
for (var value in values) {
if (skipCount > 0) {
skipCount--;
continue;
}
if (writeIndex == _buffer.length) {
- _grow();
+ _grow(writeIndex);
}
_buffer[writeIndex++] = value;
}
+
if (skipCount > 0) {
throw new StateError("Too few elements");
}
if (end != null && writeIndex < end) {
throw new RangeError.range(end, start, writeIndex, "end");
}
+
// Swap [index.._length) and [_length..writeIndex) by double-reversing.
_reverse(_buffer, index, _length);
_reverse(_buffer, _length, writeIndex);
@@ -255,8 +256,11 @@
return _createBuffer(newLength);
}
- void _grow() {
- _buffer = _createBiggerBuffer(null)..setRange(0, _length, _buffer);
+ /// Grows the buffer.
+ ///
+ /// This copies the first [length] elements into the new buffer.
+ void _grow(int length) {
+ _buffer = _createBiggerBuffer(null)..setRange(0, length, _buffer);
}
void setRange(int start, int end, Iterable<E> source, [int skipCount = 0]) {
diff --git a/pubspec.yaml b/pubspec.yaml
index 8927a76..a58a7e7 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,5 +1,5 @@
name: typed_data
-version: 1.1.2-dev
+version: 1.1.2
author: Dart Team <misc@dartlang.org>
description: Utility functions and classes related to the 'dart:typed_data' library.
homepage: https://github.com/dart-lang/typed_data
diff --git a/test/typed_buffers_test.dart b/test/typed_buffers_test.dart
index 0c99a40..9e97592 100644
--- a/test/typed_buffers_test.dart
+++ b/test/typed_buffers_test.dart
@@ -109,6 +109,13 @@
expect(buffer, equals([2, 3, 4, 5, 6, 1, 7, 8, 9, 10]));
});
+ // Regression test for #1.
+ test("inserts values into the buffer after removeRange()", () {
+ buffer.removeRange(1, 4);
+ buffer.insertAll(1, source);
+ expect(buffer, equals([6, 1, 2, 3, 4, 5, 10]));
+ });
+
test("does nothing for empty slices", () {
buffer.insertAll(1, source, 0, 0);
expect(buffer, equals([6, 7, 8, 9, 10]));