nextInteger method

int? nextInteger({
  1. RegExp? digitMatcher,
  2. int? zeroDigit,
})

Assuming that the contents are characters, read as many digits as we can see and then return the corresponding integer, advancing the receiver.

For non-ascii digits, the optional arguments are a regular expression digitMatcher to find the next integer, and the codeUnit of the local zero zeroDigit.

Implementation

int? nextInteger({RegExp? digitMatcher, int? zeroDigit}) {
  var string = (digitMatcher ?? regexp.asciiDigitMatcher).stringMatch(rest());
  if (string == null || string.isEmpty) return null;
  read(string.length);
  if (zeroDigit != null && zeroDigit != constants.asciiZeroCodeUnit) {
    // Trying to optimize this, as it might get called a lot.
    var oldDigits = string.codeUnits;
    var newDigits = List<int>.filled(string.length, 0);
    for (var i = 0; i < string.length; i++) {
      newDigits[i] = oldDigits[i] - zeroDigit + constants.asciiZeroCodeUnit;
    }
    string = String.fromCharCodes(newDigits);
  }
  return int.parse(string);
}