tryParse static method
Como PgDecimal.parse, mas devolve null se o texto não for um número.
Implementation
static PgDecimal? tryParse(String source) {
final text = source.trim();
if (text.isEmpty) return null;
final lower = text.toLowerCase();
switch (lower) {
case 'nan':
return nan;
case 'infinity':
case '+infinity':
case 'inf':
case '+inf':
return infinity;
case '-infinity':
case '-inf':
return negativeInfinity;
}
var i = 0;
var negative = false;
final first = text.codeUnitAt(0);
if (first == 0x2B || first == 0x2D) {
negative = first == 0x2D;
i++;
}
final digits = StringBuffer();
var fractionDigits = 0;
var seenPoint = false;
var anyDigit = false;
for (; i < text.length; i++) {
final c = text.codeUnitAt(i);
if (c >= 0x30 && c <= 0x39) {
digits.writeCharCode(c);
anyDigit = true;
if (seenPoint) fractionDigits++;
} else if (c == 0x2E && !seenPoint) {
seenPoint = true;
} else {
break;
}
}
if (!anyDigit) return null;
var exponent = 0;
if (i < text.length) {
final c = text.codeUnitAt(i);
if (c != 0x65 && c != 0x45) return null; // e / E
i++;
var expNegative = false;
if (i < text.length) {
final s = text.codeUnitAt(i);
if (s == 0x2B || s == 0x2D) {
expNegative = s == 0x2D;
i++;
}
}
final start = i;
for (; i < text.length; i++) {
final c = text.codeUnitAt(i);
if (c < 0x30 || c > 0x39) return null;
}
if (i == start || i - start > 9) return null;
exponent = int.parse(text.substring(start, i));
if (exponent > 1000000) return null;
if (expNegative) exponent = -exponent;
}
var unscaled = BigInt.parse(digits.toString());
if (negative) unscaled = -unscaled;
// Escala como no PostgreSQL: dígitos depois do ponto menos o expoente.
final scale = fractionDigits - exponent;
if (scale >= 0) return PgDecimal._finiteValue(unscaled, scale);
return PgDecimal._finiteValue(unscaled * _pow10(-scale), 0);
}