From: Chris Duncan Date: Thu, 2 Jul 2026 16:11:12 +0000 (-0700) Subject: Refactor decimal conversions to use loops instead of function calls. X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=ad849947a6412ff2f2684a0dc80b1759c155e699;p=libnemo.git Refactor decimal conversions to use loops instead of function calls. --- diff --git a/src/lib/convert/bytes.ts b/src/lib/convert/bytes.ts index 3f9b8e2..9c406c5 100644 --- a/src/lib/convert/bytes.ts +++ b/src/lib/convert/bytes.ts @@ -79,14 +79,14 @@ export const bytes = Object.freeze({ * @returns {bigint|number} Decimal sum of the literal byte values */ toDec (bytes: Bytes): bigint | number { - let decimal = 0n + let int = 0n for (let i = bytes.byteLength; i > 0; i--) { - decimal += BigInt(bytes[i - 1]) << (BigInt(bytes.byteLength - i) * 8n) + int += BigInt(bytes[i - 1]) << (BigInt(bytes.byteLength - i) << 3n) } - if (decimal > 9007199254740991n) { - return decimal + if (int > 9007199254740991n) { + return int } else { - return Number(decimal) + return Number(int) } }, diff --git a/src/lib/convert/dec.ts b/src/lib/convert/dec.ts index b69979b..7124498 100644 --- a/src/lib/convert/dec.ts +++ b/src/lib/convert/dec.ts @@ -1,6 +1,8 @@ //! SPDX-FileCopyrightText: 2025 Chris Duncan //! SPDX-License-Identifier: GPL-3.0-or-later +import { HEXCHAR } from "../constants" + export const dec = Object.freeze({ /** * Convert a decimal integer to a Uint8Array of bytes. Fractional part is @@ -17,20 +19,22 @@ export const dec = Object.freeze({ if (typeof padding !== 'number' || padding < 1 || padding > 0xffffffff) { throw new TypeError('Invalid padding') } - let integer = BigInt(decimal) - const bytes: number[] = (integer === 0n) ? [0] : [] - while (integer > 0) { - const lsb = BigInt.asUintN(8, integer) - bytes.push(Number(lsb)) - integer >>= 8n + let int = BigInt(decimal) + if (int < 0n) { + throw new TypeError('Decimal must be non-negative') + } + const bytes: number[] = (int === 0n) ? [0] : [] + while (int > 0n) { + bytes.push(Number(int & 255n)) + int >>= 8n } - const result = new Uint8Array(Math.max(padding, bytes.length)) + const result = new Uint8Array(padding > bytes.length ? padding : bytes.length) result.set(bytes) return (result.reverse()) }, /** - * Convert a decimal integer to a hexadecimal string. + * Convert a non-negative decimal integer to a hexadecimal string. * * @param {(bigint|number|string)} decimal - Integer to convert * @param {number} [padding=1] - Minimum length of the resulting string padded as necessary with starting zeroes @@ -43,13 +47,18 @@ export const dec = Object.freeze({ if (typeof padding !== 'number' || padding < 1 || padding > 0x1fffffffe) { throw new TypeError('Invalid padding') } - try { - return BigInt(decimal) - .toString(16) - .padStart(padding, '0') - .toUpperCase() - } catch (err) { - throw new RangeError('Invalid decimal integer') + let int = BigInt(decimal) + if (int < 0n) { + throw new TypeError('Decimal must be non-negative') + } + let hex: string = '' + while (int > 0n) { + hex = HEXCHAR[Number(int & 15n)] + hex + int >>= 4n + } + while (hex.length < padding) { + hex = '0' + hex } + return hex }, })