mirror of
https://github.com/gchq/CyberChef.git
synced 2026-03-26 08:41:25 -07:00
add function safeBigInt to wrap BigInt values and provide useful errors
This commit is contained in:
parent
8a75be1b20
commit
e95782eba7
1 changed files with 12 additions and 4 deletions
|
|
@ -28,6 +28,14 @@ function normaliseInput(input) {
|
|||
return String(input).trim();
|
||||
}
|
||||
|
||||
function safeBigInt(str, context="number") {
|
||||
try {
|
||||
return BigInt(str);
|
||||
} catch {
|
||||
throw new Error(`Invalid ${context}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Compute 10^exp as BigInt without using ** on BigInt, to avoid
|
||||
|
|
@ -216,19 +224,19 @@ export function ToIEEE754Float64(input) {
|
|||
const sciMatch = input.match(/^(.*)e([+-]?\d+)$/i);
|
||||
if (sciMatch) {
|
||||
input = sciMatch[1];
|
||||
sci = BigInt(sciMatch[2]);
|
||||
sci = safeBigInt(sciMatch[2], "exponent");
|
||||
}
|
||||
|
||||
let [intStr, fracStr] = input.split(".");
|
||||
intStr = (intStr || "0").replace(/_/g, "");
|
||||
fracStr = (fracStr || "").replace(/_/g, "");
|
||||
|
||||
let N = BigInt(intStr);
|
||||
let N = safeBigInt(intStr, "integer part");
|
||||
let D = 1n;
|
||||
|
||||
if (fracStr.length > 0) {
|
||||
const pow10 = pow10BigInt(BigInt(fracStr.length));
|
||||
N = N * pow10 + BigInt(fracStr);
|
||||
const pow10 = pow10BigInt(BigInt(fracStr.length));
|
||||
N = N * pow10 + safeBigInt(fracStr, "fractional part");
|
||||
D = pow10;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue