UNPKG

@substrate-system/uint8-util

Version:

Fastest possible buffer-like utilities for uint8.

64 lines (63 loc) 1.78 kB
"use strict"; var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); /* Common package for dealing with hex/string/uint8 conversions (and sha1 hashing) * * @author Jimmy Wärting <jimmy@warting.se> (https://jimmy.warting.se/opensource) * @license MIT */ export const alphabet = "0123456789abcdef"; const encodeLookup = []; const decodeLookup = []; for (let i = 0; i < 256; i++) { encodeLookup[i] = alphabet[i >> 4 & 15] + alphabet[i & 15]; if (i < 16) { if (i < 10) { decodeLookup[48 + i] = i; } else { decodeLookup[97 - 10 + i] = i; } } } export const arr2hex = /* @__PURE__ */ __name((data) => { const length = data.length; let string = ""; let i = 0; while (i < length) { string += encodeLookup[data[i++]]; } return string; }, "arr2hex"); export const hex2arr = /* @__PURE__ */ __name((str) => { const sizeof = str.length >> 1; const length = sizeof << 1; const array = new Uint8Array(sizeof); let n = 0; let i = 0; while (i < length) { array[n++] = decodeLookup[str.charCodeAt(i++)] << 4 | decodeLookup[str.charCodeAt(i++)]; } return array; }, "hex2arr"); export const concat = /* @__PURE__ */ __name((chunks, size = 0) => { const length = chunks.length || 0; if (!size) { let i2 = length; while (i2--) size += chunks[i2].length; } const b = new Uint8Array(size); let offset = size; let i = length; while (i--) { offset -= chunks[i].length; b.set(chunks[i], offset); } return b; }, "concat"); export const equal = /* @__PURE__ */ __name((a, b) => { if (a.length !== b.length) return false; for (let i = a.length; i > -1; i -= 1) { if (a[i] !== b[i]) return false; } return true; }, "equal");