copyRange<T> method

void copyRange <T>(List<T> target, int at, List<T> source, [ int start, int end ])

Copy a range of one list into another list.

This is a utility function that can be used to implement methods like setRange.

The range from start to end must be a valid range of source, and there must be room for end - start elements from position at. If start is omitted, it defaults to zero. If end is omitted, it defaults to source.length.

If source and target is the same list, overlapping source and target ranges are respected so that the target range ends up containing the initial content of the source range. Otherwise the order of element copying is not guaranteed.

Implementation

static void copyRange<T>(List<T> target, int at, List<T> source,
    [int start, int end]) {
  start ??= 0;
  end = RangeError.checkValidRange(start, end, source.length);
  int length = end - start;
  if (target.length < at + length) {
    throw ArgumentError.value(target, "target",
        "Not big enough to hold $length elements at position $at");
  }
  if (!identical(source, target) || start >= at) {
    for (int i = 0; i < length; i++) {
      target[at + i] = source[start + i];
    }
  } else {
    for (int i = length; --i >= 0;) {
      target[at + i] = source[start + i];
    }
  }
}