]> git.codecow.com Git - libnemo.git/commitdiff
Refactor decimal conversions to use loops instead of function calls.
authorChris Duncan <chris@codecow.com>
Thu, 2 Jul 2026 16:11:12 +0000 (09:11 -0700)
committerChris Duncan <chris@codecow.com>
Thu, 2 Jul 2026 16:11:12 +0000 (09:11 -0700)
src/lib/convert/bytes.ts
src/lib/convert/dec.ts

index 3f9b8e207c436c29bfe1616d1d3b045e03244535..9c406c54db5915518925354964ac40d987211b8c 100644 (file)
@@ -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)
                }
        },
 
index b69979b11aca21605db369ecf2f94f3e4bffa3c4..712449832de22b721f44f310c8625bc122d03783 100644 (file)
@@ -1,6 +1,8 @@
 //! SPDX-FileCopyrightText: 2025 Chris Duncan <chris@codecow.com>
 //! 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
        },
 })