diff --git a/bytes.ts b/bytes.ts index 1b3971d..1f28af8 100644 --- a/bytes.ts +++ b/bytes.ts @@ -39,45 +39,65 @@ export function to_base64(b: BinaryLike): string { return encodeBase64(to_utf8(b)); } +export function read_u8(b: Uint8Array, i = 0) { + return b[i]; +} + +export function write_u8(b: Uint8Array, n: number, i = 0) { + if (n < 0 || 0xff < n) throw new TypeError(`input out of range, expected u8`); + b[i] = n; +} + +export function read_i8(b: Uint8Array, i = 0) { + const n = b[i]; + return n | ((n & 0x80) * 0x1fffffe); +} + +export function write_i8(b: Uint8Array, n: number, i = 0) { + if (n < -0x80 || 0x7f < n) + throw new TypeError(`input out of range, expected i8`); + b[i] = n; +} + export function read_u16_be(b: Uint8Array, i = 0) { - return (b[i++] << 8) + b[i++]; -} - -export function read_i16_be(b: Uint8Array, i = 0) { - const n = (b[i++] << 8) + b[i++]; - return n | ((n & 0x8000) * 0x1fffe); -} - -export function read_u32_be(b: Uint8Array, i = 0) { - return ((b[i++] << 24) + (b[i++] << 16) + (b[i++] << 8) + b[i++]) >>> 0; -} - -export function read_i32_be(b: Uint8Array, i = 0) { - return (b[i++] << 24) + (b[i++] << 16) + (b[i++] << 8) + b[i++]; + return (b[i++] << 8) + b[i]; } export function write_u16_be(b: Uint8Array, n: number, i = 0) { if (n < 0 || 0xffff < n) throw new TypeError(`input out of range, expected u16`); - (b[i++] = n >>> 8), (b[i++] = n); + (b[i++] = n >>> 8), (b[i] = n); +} + +export function read_i16_be(b: Uint8Array, i = 0) { + const n = (b[i++] << 8) + b[i]; + return n | ((n & 0x8000) * 0x1fffe); } export function write_i16_be(b: Uint8Array, n: number, i = 0) { if (n < -0x8000 || 0x7fff < n) throw new TypeError(`input out of range, expected i16`); - (b[i++] = n >>> 8), (b[i++] = n); + (b[i++] = n >>> 8), (b[i] = n); +} + +export function read_u32_be(b: Uint8Array, i = 0) { + return ((b[i++] << 24) + (b[i++] << 16) + (b[i++] << 8) + b[i]) >>> 0; } export function write_u32_be(b: Uint8Array, n: number, i = 0) { if (n < 0 || 0xffffffff < n) throw new TypeError(`input out of range, expected u32`); - (b[i++] = n >>> 24), (b[i++] = n >>> 16), (b[i++] = n >>> 8), (b[i++] = n); + (b[i++] = n >>> 24), (b[i++] = n >>> 16), (b[i++] = n >>> 8), (b[i] = n); +} + +export function read_i32_be(b: Uint8Array, i = 0) { + return (b[i++] << 24) + (b[i++] << 16) + (b[i++] << 8) + b[i]; } export function write_i32_be(b: Uint8Array, n: number, i = 0) { if (n < -0x80000000 || 0x7fffffff < n) throw new TypeError(`input out of range, expected i32`); - (b[i++] = n >>> 24), (b[i++] = n >>> 16), (b[i++] = n >>> 8), (b[i++] = n); + (b[i++] = n >>> 24), (b[i++] = n >>> 16), (b[i++] = n >>> 8), (b[i] = n); } export { equals as buf_eq, concat as buf_concat } from "jsr:@std/bytes@^1.0.4"; diff --git a/deno.json b/deno.json index b04e117..82dd816 100644 --- a/deno.json +++ b/deno.json @@ -1,5 +1,5 @@ { "name": "@luaneko/lstd", - "version": "0.1.4", + "version": "0.1.5", "exports": "./mod.ts" } diff --git a/mod.ts b/mod.ts index 8f58689..a44ce4a 100644 --- a/mod.ts +++ b/mod.ts @@ -7,13 +7,17 @@ export { to_hex, from_base64, to_base64, + read_u8, + write_u8, + read_i8, + write_i8, read_u16_be, - read_i16_be, - read_u32_be, - read_i32_be, write_u16_be, + read_i16_be, write_i16_be, + read_u32_be, write_u32_be, + read_i32_be, write_i32_be, buf_eq, buf_concat,