Skip to content

Commit

Permalink
support Uint8Array sub-array
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinushey committed Apr 16, 2020
1 parent 0423145 commit cb198d0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
6 changes: 6 additions & 0 deletions suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ function tests(isNative, TextEncoder, TextDecoder) {
}
});

test('subarray', () => {
const buffer = new Uint8Array([104, 101, 108, 108, 111]);
const array = buffer.subarray(0, 4);
assert.equal(dec.decode(array), 'hell');
});

test('null in middle', () => {
const s = 'pad\x00pad';
const buffer = new Uint8Array([112, 97, 100, 0, 112, 97, 100]);
Expand Down
8 changes: 5 additions & 3 deletions text.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,16 @@ FastTextDecoder.prototype.decode = function(buffer, options={stream: false}) {
throw new Error(`Failed to decode: the 'stream' option is unsupported.`);
}

// Accept Uint8Array's as-is.
let bytes = buffer;

// Look for ArrayBufferView, which isn't a real type, but basically represents
// all the valid TypedArray types plus DataView. They all have ".buffer" as
// an instance of ArrayBuffer.
if (buffer.buffer instanceof ArrayBuffer) {
buffer = buffer.buffer;
if (!(bytes instanceof Uint8Array) && bytes.buffer instanceof ArrayBuffer) {
bytes = new Uint8Array(buffer.buffer);
}

let bytes = new Uint8Array(buffer);
let pos = 0;
let pending = [];
const chunks = [];
Expand Down
7 changes: 2 additions & 5 deletions text.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cb198d0

Please sign in to comment.