ox
Version:
Ethereum Standard Library
78 lines • 2.34 kB
JavaScript
import * as Bytes from '../Bytes.js';
/** @internal */
export function assertSize(bytes, size_) {
if (Bytes.size(bytes) > size_)
throw new Bytes.SizeOverflowError({
givenSize: Bytes.size(bytes),
maxSize: size_,
});
}
/** @internal */
export function assertStartOffset(value, start) {
if (typeof start === 'number' && start > 0 && start > Bytes.size(value) - 1)
throw new Bytes.SliceOffsetOutOfBoundsError({
offset: start,
position: 'start',
size: Bytes.size(value),
});
}
/** @internal */
export function assertEndOffset(value, start, end) {
if (typeof start === 'number' &&
typeof end === 'number' &&
Bytes.size(value) !== end - start) {
throw new Bytes.SliceOffsetOutOfBoundsError({
offset: end,
position: 'end',
size: Bytes.size(value),
});
}
}
/** @internal */
export const charCodeMap = {
zero: 48,
nine: 57,
A: 65,
F: 70,
a: 97,
f: 102,
};
/** @internal */
export function charCodeToBase16(char) {
if (char >= charCodeMap.zero && char <= charCodeMap.nine)
return char - charCodeMap.zero;
if (char >= charCodeMap.A && char <= charCodeMap.F)
return char - (charCodeMap.A - 10);
if (char >= charCodeMap.a && char <= charCodeMap.f)
return char - (charCodeMap.a - 10);
return undefined;
}
/** @internal */
export function pad(bytes, options = {}) {
const { dir, size = 32 } = options;
if (size === 0)
return bytes;
if (bytes.length > size)
throw new Bytes.SizeExceedsPaddingSizeError({
size: bytes.length,
targetSize: size,
type: 'Bytes',
});
const paddedBytes = new Uint8Array(size);
paddedBytes.set(bytes, dir === 'right' ? 0 : size - bytes.length);
return paddedBytes;
}
/** @internal */
export function trim(value, options = {}) {
const { dir = 'left' } = options;
let start = 0;
let end = value.length;
if (dir === 'left')
while (start < end && value[start] === 0)
start++;
else
while (end > start && value[end - 1] === 0)
end--;
return (start === 0 && end === value.length ? value : value.slice(start, end));
}
//# sourceMappingURL=bytes.js.map