import { decodeBase64, decodeHex, encodeBase64, encodeHex, } from "jsr:@std/encoding@^1.0.6"; const encoder = new TextEncoder(); const decoder = new TextDecoder(); export type BinaryLike = string | Uint8Array; export function from_utf8(s: BinaryLike): string { return typeof s === "string" ? s : decoder.decode(s); } export function to_utf8(s: BinaryLike): Uint8Array { return typeof s === "string" ? encoder.encode(s) : s; } export function encode_utf8(s: BinaryLike, buf: Uint8Array): number { if (typeof s === "string") return encoder.encodeInto(s, buf).written; else return buf.set(s), s.length; } export function from_hex(s: BinaryLike): Uint8Array { return decodeHex(from_utf8(s)); } export function to_hex(b: BinaryLike): string { return encodeHex(to_utf8(b)); } export function from_base64(s: BinaryLike): Uint8Array { return decodeBase64(from_utf8(s)); } export function to_base64(b: BinaryLike): string { return encodeBase64(to_utf8(b)); } export { equals as buf_eq, concat as buf_concat } from "jsr:@std/bytes@^1.0.4"; export function buf_concat_fast(a: Uint8Array, b: Uint8Array) { const m = a.length; const c = new Uint8Array(m + b.length); return c.set(a, 0), c.set(b, m), c; } export function buf_resize(buf: Uint8Array, n: number) { const res = new Uint8Array(n); return res.set(buf.subarray(0, n)), res; } export function buf_xor(a: Uint8Array, b: Uint8Array) { const n = Math.max(a.length, b.length); const c = new Uint8Array(n); for (let i = 0; i < n; i++) c[i] = a[i] ^ b[i]; return c; }