From 45b6cf40a85c475855f739cecd98845b4ede2b1e Mon Sep 17 00:00:00 2001 From: AlttiRi <16310547+AlttiRi@users.noreply.github.com> Date: Sat, 7 Sep 2024 00:02:29 +0300 Subject: [PATCH] Use `byteOffset` and `byteLength` in `DataView` constructor (fixes #2) --- base85.js | 2 +- base85.ts | 2 +- package.json | 2 +- tests/test-4-offset-ab.js | 21 +++++++++++++++++++++ 4 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 tests/test-4-offset-ab.js diff --git a/base85.js b/base85.js index 3aa6bae..01b7c21 100644 --- a/base85.js +++ b/base85.js @@ -43,7 +43,7 @@ export function encode(ui8a, charset) { const last5Length = remain ? remain + 1 : 0; const length = Math.ceil(ui8a.length * 5 / 4); const target = new Uint8Array(length); - const dw = new DataView(ui8a.buffer); + const dw = new DataView(ui8a.buffer, ui8a.byteOffset, ui8a.byteLength); const to = Math.trunc(ui8a.length / 4); for (let i = 0; i < to; i++) { let num = dw.getUint32(4 * i); diff --git a/base85.ts b/base85.ts index 6dc8507..50b4d50 100644 --- a/base85.ts +++ b/base85.ts @@ -52,7 +52,7 @@ export function encode(ui8a: Uint8Array, charset?: "ascii85" | "z85" | CharSet): const length = Math.ceil(ui8a.length * 5 / 4); const target = new Uint8Array(length); - const dw = new DataView(ui8a.buffer); + const dw = new DataView(ui8a.buffer, ui8a.byteOffset, ui8a.byteLength); const to = Math.trunc(ui8a.length / 4); for (let i = 0; i < to; i++) { let num = dw.getUint32(4 * i); diff --git a/package.json b/package.json index 866c709..330aa0f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@alttiri/base85", - "version": "1.7.6", + "version": "1.8.0", "description": "Pretty fast base85 JavaScript library", "homepage": "https://github.com/alttiri/base85", "keywords": [ diff --git a/tests/test-4-offset-ab.js b/tests/test-4-offset-ab.js new file mode 100644 index 0000000..c05305a --- /dev/null +++ b/tests/test-4-offset-ab.js @@ -0,0 +1,21 @@ +import {Tester} from "@alttiri/util-node-js"; +import {encode} from "../base85.js"; + +const {t} = new Tester().destructible(); + +const a = [0x00, 0x86, 0x4F, 0xD2, 0x6F, 0xB5, 0x59, 0xF7, 0x5B]; +const newA1 = new Uint8Array(a).slice(1); +const newA2 = new Uint8Array(a).subarray(1); + +// console.log(newA1); +// console.log(newA2); +// console.log(encode(newA1)); +// console.log(encode(newA2)); + +// Uint8Array(8) [134, 79, 210, 111, 181, 89, 247, 91] +// HelloWorld + +t({ + expect: true, + result: encode(newA1) === encode(newA2), +});