Add _TypedDataBuffer.addRange.

This is useful for concatenating ranges of existing lists, as comes up
occasionally when implementing converters.

Also make _TypedDataBuffer.addAll more efficient for lists.

R=lrn@google.com

Review URL: https://codereview.chromium.org//1404443005 .
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a123d27..36fa6b1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,12 @@
-# Changelog
+## 1.1.0
+
+* Add `start` and `end` parameters to the `addAll()` and `insertAll()` methods
+  for the typed data buffer classes. These allow efficient concatenation of
+  slices of existing typed data.
+
+* Make `addAll()` for typed data buffer classes more efficient for lists,
+  especially typed data lists.
 
 ## 1.0.0
 
-- ChangeLog starts here
+* ChangeLog starts here
diff --git a/lib/typed_buffers.dart b/lib/typed_buffers.dart
index 50ed241..ebf5766 100644
--- a/lib/typed_buffers.dart
+++ b/lib/typed_buffers.dart
@@ -65,13 +65,108 @@
     _buffer[_length++] = value;
   }
 
-  // We override the default implementation of `add` and `addAll` because
-  // they grow by setting the length in increments of one. We want to grow
-  // by doubling capacity in most cases.
+  // We override the default implementation of `add` because it grows the list
+  // by setting the length in increments of one. We want to grow by doubling
+  // capacity in most cases.
   void add(E value) { _add(value); }
 
-  void addAll(Iterable<E> values) {
-    for (E value in values) _add(value);
+  /// Appends all objects of [values] to the end of this buffer.
+  ///
+  /// This adds values from [start] (inclusive) to [end] (exclusive) in
+  /// [values]. If [end] is omitted, it defaults to adding all elements of
+  /// [values] after [start].
+  ///
+  /// The [start] value must be non-negative. The [values] iterable must have at
+  /// least [start] elements, and if [end] is specified, it must be greater than
+  /// or equal to [start] and [values] must have at least [end] elements.
+  void addAll(Iterable<E> values, [int start = 0, int end]) {
+    RangeError.checkNotNegative(start, "start");
+    if (end != null && start > end) {
+      throw new RangeError.range(end, start, null, "end");
+    }
+
+    _addAll(values, start, end);
+  }
+
+  /// Inserts all objects of [values] at position [index] in this list.
+  ///
+  /// This adds values from [start] (inclusive) to [end] (exclusive) in
+  /// [values]. If [end] is omitted, it defaults to adding all elements of
+  /// [values] after [start].
+  ///
+  /// The [start] value must be non-negative. The [values] iterable must have at
+  /// least [start] elements, and if [end] is specified, it must be greater than
+  /// or equal to [start] and [values] must have at least [end] elements.
+  void insertAll(int index, Iterable<E> values, [int start = 0, int end]) {
+    RangeError.checkValidIndex(index, this, "index", _length + 1);
+    RangeError.checkNotNegative(start, "start");
+    if (end != null && start > end) {
+      throw new RangeError.range(end, start, null, "end");
+    }
+
+    // If we're adding to the end of the list anyway, use [_addAll]. This lets
+    // us avoid converting [values] into a list even if [end] is null, since we
+    // can add values iteratively to the end of the list. We can't do so in the
+    // center because copying the trailing elements every time is non-linear.
+    if (index == _length) {
+      _addAll(values, start, end);
+      return;
+    }
+
+    // If we don't know how much room to make for [values], convert it to a list
+    // so we can tell.
+    if (end == null) {
+      if (values is! List) values = values.toList(growable: false);
+      end = values.length;
+    }
+
+    _insertKnownLength(index, values, start, end);
+  }
+
+  /// Does the same thing as [addAll].
+  ///
+  /// This allows [addAll] and [insertAll] to share implementation without a
+  /// subclass unexpectedly overriding both when it intended to only override
+  /// [addAll].
+  void _addAll(Iterable<E> values, [int start = 0, int end]) {
+    if (values is List) end ??= values.length;
+
+    // If we know the length of the segment to add, do so with [addRange]. This
+    // way we know how much to grow the buffer in advance, and it may be even
+    // more efficient for typed data input.
+    if (end != null) {
+      _insertKnownLength(_length, values, start, end);
+      return;
+    }
+
+    // Otherwise, just add values one at a time.
+    var i = 0;
+    for (var value in values) {
+      if (i >= start) add(value);
+      i++;
+    }
+    if (i < start) throw new StateError("Too few elements");
+  }
+
+  /// Like [insertAll], but with a guaranteed non-`null` [start] and [end].
+  void _insertKnownLength(int index, Iterable<E> values, int start, int end) {
+    if (values is List) {
+      end ??= values.length;
+      if (start > values.length || end > values.length) {
+        throw new StateError("Too few elements");
+      }
+    } else {
+      assert(end != null);
+    }
+
+    var valuesLength = end - start;
+    var newLength = _length + valuesLength;
+    _ensureCapacity(newLength);
+
+    _buffer.setRange(
+        index + valuesLength, _length + valuesLength, _buffer, index);
+    _buffer.setRange(index, index + valuesLength, values, start);
+    _length = newLength;
   }
 
   void insert(int index, E element) {
@@ -92,18 +187,28 @@
     _buffer = newBuffer;
   }
 
+  /// Ensures that [_buffer] is at least [requiredCapacity] long,
+  ///
+  /// Grows the buffer if necessary, preserving existing data.
+  void _ensureCapacity(int requiredCapacity) {
+    if (requiredCapacity <= _buffer.length) return;
+    var newBuffer = _createBiggerBuffer(requiredCapacity);
+    newBuffer.setRange(0, _length, _buffer);
+    _buffer = newBuffer;
+  }
+
   /**
    * Create a bigger buffer.
    *
    * This method determines how much bigger a bigger buffer should
-   * be. If [requiredLength] is not null, it will be at least that
+   * be. If [requiredCapacity] is not null, it will be at least that
    * size. It will always have at least have double the capacity of
    * the current buffer.
    */
-  List<E> _createBiggerBuffer(int requiredLength) {
+  List<E> _createBiggerBuffer(int requiredCapacity) {
     int newLength = _buffer.length * 2;
-    if (requiredLength != null && newLength < requiredLength) {
-      newLength = requiredLength;
+    if (requiredCapacity != null && newLength < requiredCapacity) {
+      newLength = requiredCapacity;
     } else if (newLength < INITIAL_LENGTH) {
       newLength = INITIAL_LENGTH;
     }
@@ -116,6 +221,11 @@
 
   void setRange(int start, int end, Iterable<E> source, [int skipCount = 0]) {
     if (end > _length) throw new RangeError.range(end, 0, _length);
+    _setRange(start, end, source, skipCount);
+  }
+
+  /// Like [setRange], but with no bounds checking.
+  void _setRange(int start, int end, Iterable<E> source, int skipCount) {
     if (source is _TypedDataBuffer<E>) {
       _buffer.setRange(start, end, source._buffer, skipCount);
     } else {
diff --git a/pubspec.yaml b/pubspec.yaml
index c54a860..4ca6cb2 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,5 +1,5 @@
 name: typed_data
-version: 1.0.1-dev
+version: 1.1.0
 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 b5f907c..0c99a40 100644
--- a/test/typed_buffers_test.dart
+++ b/test/typed_buffers_test.dart
@@ -35,6 +35,108 @@
   testFloatBuffer(64, doubleSamples, () => new Float64Buffer(), (x) => x);
 
   testFloat32x4Buffer(roundedFloatSamples);
+
+  group("addAll", () {
+    for (var type in ['a list', 'an iterable']) {
+      group("with $type", () {
+        var source;
+        var buffer;
+        setUp(() {
+          source = [1, 2, 3, 4, 5];
+          if (type == 'an iterable') source = source.reversed.toList().reversed;
+          buffer = new Uint8Buffer();
+        });
+
+        test("adds values to the buffer", () {
+          buffer.addAll(source, 1, 4);
+          expect(buffer, equals([2, 3, 4]));
+
+          buffer.addAll(source, 4);
+          expect(buffer, equals([2, 3, 4, 5]));
+
+          buffer.addAll(source, 0, 1);
+          expect(buffer, equals([2, 3, 4, 5, 1]));
+        });
+
+        test("does nothing for empty slices", () {
+          buffer.addAll([6, 7, 8, 9, 10]);
+
+          buffer.addAll(source, 0, 0);
+          expect(buffer, equals([6, 7, 8, 9, 10]));
+
+          buffer.addAll(source, 3, 3);
+          expect(buffer, equals([6, 7, 8, 9, 10]));
+
+          buffer.addAll(source, 5);
+          expect(buffer, equals([6, 7, 8, 9, 10]));
+
+          buffer.addAll(source, 5, 5);
+          expect(buffer, equals([6, 7, 8, 9, 10]));
+        });
+
+        test("throws errors for invalid start and end", () {
+          expect(() => buffer.addAll(source, -1), throwsRangeError);
+          expect(() => buffer.addAll(source, -1, 2), throwsRangeError);
+          expect(() => buffer.addAll(source, 10), throwsStateError);
+          expect(() => buffer.addAll(source, 10, 11), throwsStateError);
+          expect(() => buffer.addAll(source, 3, 2), throwsRangeError);
+          expect(() => buffer.addAll(source, 3, 10), throwsStateError);
+          expect(() => buffer.addAll(source, 3, -1), throwsRangeError);
+        });
+      });
+    }
+  });
+
+  group("insertAll", () {
+    for (var type in ['a list', 'an iterable']) {
+      group("with $type", () {
+        var source;
+        var buffer;
+        setUp(() {
+          source = [1, 2, 3, 4, 5];
+          if (type == 'an iterable') source = source.reversed.toList().reversed;
+          buffer = new Uint8Buffer()..addAll([6, 7, 8, 9, 10]);
+        });
+
+        test("inserts values into the buffer", () {
+          buffer.insertAll(0, source, 1, 4);
+          expect(buffer, equals([2, 3, 4, 6, 7, 8, 9, 10]));
+
+          buffer.insertAll(3, source, 4);
+          expect(buffer, equals([2, 3, 4, 5, 6, 7, 8, 9, 10]));
+
+          buffer.insertAll(5, source, 0, 1);
+          expect(buffer, equals([2, 3, 4, 5, 6, 1, 7, 8, 9, 10]));
+        });
+
+        test("does nothing for empty slices", () {
+          buffer.insertAll(1, source, 0, 0);
+          expect(buffer, equals([6, 7, 8, 9, 10]));
+
+          buffer.insertAll(2, source, 3, 3);
+          expect(buffer, equals([6, 7, 8, 9, 10]));
+
+          buffer.insertAll(3, source, 5);
+          expect(buffer, equals([6, 7, 8, 9, 10]));
+
+          buffer.insertAll(4, source, 5, 5);
+          expect(buffer, equals([6, 7, 8, 9, 10]));
+        });
+
+        test("throws errors for invalid start and end", () {
+          expect(() => buffer.insertAll(-1, source), throwsRangeError);
+          expect(() => buffer.insertAll(6, source), throwsRangeError);
+          expect(() => buffer.insertAll(1, source, -1), throwsRangeError);
+          expect(() => buffer.insertAll(2, source, -1, 2), throwsRangeError);
+          expect(() => buffer.insertAll(3, source, 10), throwsStateError);
+          expect(() => buffer.insertAll(4, source, 10, 11), throwsStateError);
+          expect(() => buffer.insertAll(5, source, 3, 2), throwsRangeError);
+          expect(() => buffer.insertAll(1, source, 3, 10), throwsStateError);
+          expect(() => buffer.insertAll(2, source, 3, -1), throwsRangeError);
+        });
+      });
+    }
+  });
 }
 
 double roundToFloat(double value) {