Skip to content

Commit

Permalink
Use byteOffset and byteLength in DataView constructor (fixes #2)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlttiRi committed Sep 6, 2024
1 parent e881f52 commit 45b6cf4
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion base85.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion base85.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
21 changes: 21 additions & 0 deletions tests/test-4-offset-ab.js
Original file line number Diff line number Diff line change
@@ -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),
});

0 comments on commit 45b6cf4

Please sign in to comment.