indices method

Range<int> indices({
  1. int step = 1,
})

Returns a Range of the indices of this iterable that can be accessed with the [] operator.

An optional non-zero step counter can be provided to skip over indices. A negative step counter returns the indices in reverse order.

For example, the expression

['a', 'b', 'c'].indices(step: 2)

returns

[0, 2]

Implementation

Range<int> indices({int step = 1}) {
  final count = length;
  if (step == 1) {
    return IntegerRange._(0, count, 1, count);
  } else if (step == -1) {
    return IntegerRange._(count - 1, -1, -1, count);
  } else if (step > 1) {
    return IntegerRange._(0, count, step, (count + 1) ~/ step);
  } else if (step < -1) {
    return IntegerRange._(count - 1, -1, step, (count + 1) ~/ -step);
  } else {
    throw ArgumentError.value(step, 'step', 'Non-zero step-size expected');
  }
}