add function to enforce base 10 decimal sytnax before parsing

This commit is contained in:
atsiv sivat 2026-03-08 00:59:21 +00:00
parent e95782eba7
commit fd1be923b2

View file

@ -36,6 +36,12 @@ function safeBigInt(str, context="number") {
}
}
function validateDecimal(input) {
// validates optional-sign base-10 decimal input: integers, decimals with optional digits before or after the decimal point, and scientific notation
if (!/^[+-]?(?:\d+\.?\d*|\.\d+)(?:e[+-]?\d+)?$/i.test(input))
throw new Error("Invalid decimal number");
}
/**
* Compute 10^exp as BigInt without using ** on BigInt, to avoid
@ -212,6 +218,8 @@ export function ToIEEE754Float64(input) {
return `${s} ${expOnes} ${mantZeros}`;
}
validateDecimal(input);
let sign = 0n;
if (input.startsWith("-")) {
sign = 1n;