parseDecimal static method

Uint64 parseDecimal(
  1. String s
)

Strict decimal parse: throws IntegerError on overflow, unlike the wrapping */+ operators.

Implementation

static Uint64 parseDecimal(String s) {
  if (s.isEmpty || !RegExp(r'^[0-9]+$').hasMatch(s)) {
    throw ArgumentException.invalidOperationArguments(
      "parseDecimal",
      reason: 'Invalid decimal literal.',
      details: {"value": s},
    );
  }
  var acc = Uint64.zero;
  final ten = Uint64(10);
  for (final rune in s.codeUnits) {
    final digit = Uint64(rune - 0x30);
    final scaled = acc.mulChecked(ten); // throws on overflow
    final next = scaled + digit;
    if (next < scaled) {
      throw IntegerError.overflow;
    }
    acc = next;
  }
  return acc;
}