Range constructor

Range(
  1. int a, [
  2. int? b,
  3. int? c
])

safe constructor - returns empty range if arguments are invalid

Implementation

factory Range(int a, [int? b, int? c]) {
  // step is never zero:
  final step = c ?? 1;
  if (step.isZero || step.isNaN) {
    return Range.empty();
  }
  // sanity check
  assert(
    step != 0,
    'Something wrong with this logic; '
    'step should non-zero int, but got $step',
  );

  var start = 0;
  var stop = 0;
  // start and stop values:
  if (b != null) {
    start = a;
    stop = b;
  } else {
    start = 0;
    stop = a;
  }

  // confirm direction
  if (_isInRange(start, stop, step)) {
    return Range._(start, stop, step);
  }
  return Range.empty();
}