startsWith method

bool startsWith(
  1. Iterable<E> other
)

Check if this collection starts with the elements of other.

If other is empty, true is returned. If other has more elements than this collection, false is returned.

[1, 2, 3].startsWith([]); // -> true
[1, 2, 3].startsWith([1]); // -> true
[1, 2, 3].startsWith([1, 2]); // -> true
[1, 2, 3].startsWith([1, 2, 3]); // -> true
[1, 2, 3].startsWith([1, 2, 3, 4]); // -> false
[1, 2, 3].startsWith([2, 3]); // -> false

Implementation

bool startsWith(Iterable<E> other) {
  final it = iterator;
  final otherIt = other.iterator;

  // [other] has no element.
  if (!otherIt.moveNext()) return true;

  do {
    // [it] is done, or the current elements are different.
    if (!it.moveNext() || otherIt.current != it.current) return false;
  } while (otherIt.moveNext());

  return true;
}