/** * Byte manipulation utilities for IAVL tree. * Provides helpers for comparing, concatenating, and working with Uint8Arrays. */ /** * Compares two byte arrays lexicographically. * Returns negative if a < b, positive if a > b, zero if equal. * * @param a - First byte array * @param b - Second byte array * @returns Comparison result */ export function compareBytes(a: Uint8Array, b: Uint8Array): number { const minLen = Math.min(a.length, b.length); for (let i = 0; i < minLen; i++) { if (a[i] !== b[i]) { return a[i] - b[i]; } } // If all bytes are equal up to minLen, shorter array is less return a.length - b.length; } /** * Checks if two byte arrays are equal. * * @param a - First byte array * @param b - Second byte array * @returns True if arrays are equal */ export function equalBytes(a: Uint8Array, b: Uint8Array): boolean { if (a.length !== b.length) return false; for (let i = 0; i < a.length; i++) { if (a[i] !== b[i]) return false; } return true; } /** * Converts a byte array to hexadecimal string. * * @param bytes - Byte array to convert * @returns Hex string representation */ export function toHex(bytes: Uint8Array): string { return Array.from(bytes) .map(b => b.toString(16).padStart(2, "0")) .join(""); } /** * Converts a hexadecimal string to byte array. * * @param hex - Hex string to convert * @returns Byte array */ export function fromHex(hex: string): Uint8Array { const bytes = new Uint8Array(hex.length / 2); for (let i = 0; i < bytes.length; i++) { bytes[i] = parseInt(hex.substr(i * 2, 2), 16); } return bytes; } /** * Concatenates multiple byte arrays into a single array. * * @param arrays - Arrays to concatenate * @returns Concatenated array */ export function concatBytes(...arrays: Uint8Array[]): Uint8Array { const totalLength = arrays.reduce((sum, arr) => sum + arr.length, 0); const result = new Uint8Array(totalLength); let offset = 0; for (const arr of arrays) { result.set(arr, offset); offset += arr.length; } return result; } /** * Creates a copy of a byte array. * * @param bytes - Array to copy * @returns New array with same contents */ export function copyBytes(bytes: Uint8Array): Uint8Array { return new Uint8Array(bytes); } /** * Converts a UTF-8 string to a byte array. * * @param str - String to convert * @returns Byte array */ export function stringToBytes(str: string): Uint8Array { return new TextEncoder().encode(str); } /** * Converts a byte array to a UTF-8 string. * * @param bytes - Byte array to convert * @returns Decoded string */ export function bytesToString(bytes: Uint8Array): string { return new TextDecoder().decode(bytes); } /** * Converts a byte array to a hexadecimal string. * * @param bytes - Byte array to convert * @returns Hex string (lowercase) */ export function bytesToHex(bytes: Uint8Array): string { return Array.from(bytes) .map(b => b.toString(16).padStart(2, "0")) .join(""); } /** * Converts a hexadecimal string to a byte array. * * @param hex - Hex string (with or without 0x prefix) * @returns Byte array * @throws Error if hex string is invalid */ export function hexToBytes(hex: string): Uint8Array { // Remove 0x prefix if present const cleanHex = hex.startsWith("0x") ? hex.slice(2) : hex; if (cleanHex.length % 2 !== 0) { throw new Error("hexToBytes: hex string must have even length"); } if (!/^[\da-f]*$/i.test(cleanHex)) { throw new Error("hexToBytes: invalid hex string"); } const bytes = new Uint8Array(cleanHex.length / 2); for (let i = 0; i < bytes.length; i++) { bytes[i] = Number.parseInt(cleanHex.slice(i * 2, i * 2 + 2), 16); } return bytes; } /** * Checks if a byte array is empty (length 0). * * @param bytes - Byte array to check * @returns True if empty */ export function isEmpty(bytes: Uint8Array): boolean { return bytes.length === 0; } /** * Returns a slice of a byte array (creates a copy, not a view). * * @param bytes - Source array * @param start - Start index (inclusive) * @param end - End index (exclusive), defaults to end of array * @returns New array with sliced contents */ export function sliceBytes(bytes: Uint8Array, start: number, end?: number): Uint8Array { return new Uint8Array(bytes.slice(start, end)); }