parseDecimal static method
Strict decimal parse: throws IntegerError on overflow/underflow, unlike the wrapping constructor.
Implementation
static Int32 parseDecimal(String s) {
if (s.isEmpty) {
throw ArgumentException.invalidOperationArguments(
"parseDecimal",
reason: 'Invalid decimal literal.',
details: {"value": s},
);
}
final negative = s.startsWith('-');
final digits = negative ? s.substring(1) : s;
// A valid i32 magnitude has at most 10 digits ("2147483648"); rejecting
// longer strings up front means int.parse below never sees more digits
// than that, so it stays exact on web.
if (digits.isEmpty ||
digits.length > 10 ||
!RegExp(r'^[0-9]+$').hasMatch(digits)) {
throw ArgumentException.invalidOperationArguments(
"parseDecimal",
reason: 'Invalid decimal literal.',
details: {"value": s},
);
}
final mag = int.parse(digits);
if (negative) {
if (mag > 0x80000000) throw IntegerError.overflow;
return Int32._((0x100000000 - mag) & BinaryOps.mask32);
} else {
if (mag > 0x7FFFFFFF) throw IntegerError.overflow;
return Int32._(mag);
}
}