// SPDX-License-Identifier: EUPL-1.2 // Exact decimal arithmetic on BigInt, matching xs:decimal semantics used by the Schematron // rules. Binary floating point is never used for money. export interface Dec { /** Unscaled value. The number is `n / 10^s`. */ readonly n: bigint; readonly s: number; } const DECIMAL_PATTERN = /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)$/; const MAX_LEXICAL_LENGTH = 40; export const ZERO: Dec = { n: 0n, s: 0 }; export const ONE: Dec = { n: 1n, s: 0 }; /** Parses the xs:decimal lexical form (surrounding whitespace allowed). Returns null if invalid. */ export function parseDec(text: string | undefined): Dec | null { if (text === undefined) return null; const trimmed = text.trim(); if (trimmed.length === 0 || trimmed.length > MAX_LEXICAL_LENGTH || !DECIMAL_PATTERN.test(trimmed)) return null; const negative = trimmed.charCodeAt(0) === 0x2d; const unsigned = trimmed.replace(/^[+-]/, ""); const dot = unsigned.indexOf("."); const integerPart = dot === -1 ? unsigned : unsigned.slice(0, dot); const fractionPart = dot === -1 ? "" : unsigned.slice(dot + 1); const magnitude = BigInt(`${integerPart}${fractionPart}` || "0"); return { n: negative ? -magnitude : magnitude, s: fractionPart.length }; } export function fromInt(value: number): Dec { return { n: BigInt(value), s: 0 }; } function pow10(exponent: number): bigint { return 10n ** BigInt(exponent); } function align(a: Dec, b: Dec): [bigint, bigint, number] { if (a.s === b.s) return [a.n, b.n, a.s]; return a.s > b.s ? [a.n, b.n * pow10(a.s - b.s), a.s] : [a.n * pow10(b.s - a.s), b.n, b.s]; } export function add(a: Dec, b: Dec): Dec { const [x, y, s] = align(a, b); return { n: x + y, s }; } export function sub(a: Dec, b: Dec): Dec { const [x, y, s] = align(a, b); return { n: x - y, s }; } export function mul(a: Dec, b: Dec): Dec { return { n: a.n * b.n, s: a.s + b.s }; } /** Exact division by 100. */ export function div100(a: Dec): Dec { return { n: a.n, s: a.s + 2 }; } const DIVISION_SCALE = 20; /** Division truncated to 20 fractional digits. Returns null when the divisor is zero. */ export function div(a: Dec, b: Dec): Dec | null { if (b.n === 0n) return null; const numerator = a.n * pow10(DIVISION_SCALE + b.s); const denominator = b.n * pow10(a.s); return { n: numerator / denominator, s: DIVISION_SCALE }; } export function cmp(a: Dec, b: Dec): -1 | 0 | 1 { const [x, y] = align(a, b); return x < y ? -1 : x > y ? 1 : 0; } export const eq = (a: Dec, b: Dec): boolean => cmp(a, b) === 0; export const lt = (a: Dec, b: Dec): boolean => cmp(a, b) < 0; export const gt = (a: Dec, b: Dec): boolean => cmp(a, b) > 0; export const isZero = (a: Dec): boolean => a.n === 0n; export function abs(a: Dec): Dec { return a.n < 0n ? { n: -a.n, s: a.s } : a; } function floorDiv(numerator: bigint, denominator: bigint): bigint { const quotient = numerator / denominator; return numerator % denominator !== 0n && numerator < 0n !== denominator < 0n ? quotient - 1n : quotient; } /** XPath `round()`: nearest integer, ties toward positive infinity (round(-2.5) = -2). */ export function round0(a: Dec): Dec { if (a.s === 0) return a; const factor = pow10(a.s); return { n: floorDiv(2n * a.n + factor, 2n * factor), s: 0 }; } /** The Schematron idiom `round(x * 10 * 10) div 100`. */ export function round2(a: Dec): Dec { if (a.s <= 2) return a; return { n: round0({ n: a.n, s: a.s - 2 }).n, s: 2 }; } export function sum(values: readonly Dec[]): Dec { let total = ZERO; for (const value of values) total = add(total, value); return total; } /** Number of digits after the decimal point in the lexical form (`substring-after(., '.')`). */ export function fractionDigits(text: string): number { const dot = text.indexOf("."); return dot === -1 ? 0 : text.length - dot - 1; } export function decToString(a: Dec): string { const negative = a.n < 0n; const digits = (negative ? -a.n : a.n).toString().padStart(a.s + 1, "0"); const integerPart = digits.slice(0, digits.length - a.s); const fractionPart = a.s === 0 ? "" : `.${digits.slice(digits.length - a.s)}`; return `${negative ? "-" : ""}${integerPart}${fractionPart}`; } /** Parses `YYYY-MM-DD` with an optional time-zone suffix into a day number. Null if invalid. */ export function parseIsoDate(text: string | undefined): number | null { if (text === undefined) return null; const match = /^(-?\d{4,})-(\d{2})-(\d{2})(?:Z|[+-]\d{2}:\d{2})?$/.exec(text.trim()); if (!match) return null; const year = Number(match[1]); const month = Number(match[2]); const day = Number(match[3]); if (month < 1 || month > 12 || day < 1) return null; const leap = (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0; const daysInMonth = [31, leap ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month - 1] ?? 0; if (day > daysInMonth) return null; return Math.floor(Date.UTC(year, month - 1, day) / 86400000); }