UNPKG

ox

Version:

Ethereum Standard Library

67 lines 2.14 kB
import * as Hex from '../Hex.js'; /** @internal */ export function assertSize(hex, size_) { const len = hex.length - 2; // Ceil to match `Hex.size` semantics for odd-nibble inputs (e.g. `0x0`). const size = (len + 1) >> 1; if (size > size_) throw new Hex.SizeOverflowError({ givenSize: size, maxSize: size_ }); } /** @internal */ export function assertStartOffset(value, start) { if (typeof start !== 'number' || start <= 0) return; const size = (value.length - 2 + 1) >> 1; if (start > size - 1) throw new Hex.SliceOffsetOutOfBoundsError({ offset: start, position: 'start', size, }); } /** @internal */ export function assertEndOffset(value, start, end) { if (typeof start !== 'number' || typeof end !== 'number') return; const size = (value.length - 2 + 1) >> 1; if (size !== end - start) throw new Hex.SliceOffsetOutOfBoundsError({ offset: end, position: 'end', size, }); } /** @internal */ export function pad(hex_, options = {}) { const { dir, size = 32 } = options; if (size === 0) return hex_; const hex = hex_.slice(2); const target = size * 2; if (hex.length > target) throw new Hex.SizeExceedsPaddingSizeError({ size: (hex.length + 1) >> 1, targetSize: size, type: 'Hex', }); return `0x${dir === 'right' ? hex.padEnd(target, '0') : hex.padStart(target, '0')}`; } /** @internal */ export function trim(value, options = {}) { const { dir = 'left' } = options; const data = value.slice(2); let start = 0; let end = data.length; if (dir === 'left') while (start < end && data.charCodeAt(start) === 48 /* '0' */) start++; else while (end > start && data.charCodeAt(end - 1) === 48 /* '0' */) end--; if (start >= end) return '0x'; if (dir === 'right' && (end - start) % 2 === 1) return `0x${data.slice(start, end)}0`; return `0x${data.slice(start, end)}`; } //# sourceMappingURL=hex.js.map