Skip to content

Commit e24e06d

Browse files
authored
add support for ArrayBuffer and check non-Uint8Array types (#14)
* code change * compiled version * put set in branch
1 parent 2489b7e commit e24e06d

File tree

4 files changed

+50
-16
lines changed

4 files changed

+50
-16
lines changed

suite.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,41 @@ function tests(isNative, TextEncoder, TextDecoder) {
1616
const out = dec.decode(buffer);
1717

1818
assert.equal(out, s);
19-
2019
});
2120

2221
suite('decoder', () => {
2322

2423
test('basic', () => {
2524
const buffer = new Uint8Array([104, 101, 108, 108, 111]);
26-
assert.equal(dec.decode(buffer), 'hello');
25+
assert.equal(dec.decode(buffer), 'hello', 'directly Uint8Array');
26+
assert.equal(dec.decode(buffer.buffer), 'hello', 'pass underlying ArrayBuffer');
27+
});
28+
29+
test('non-8 backing', () => {
30+
// If passed a Uint32Array, TextDecoder will still decode the real
31+
// underlying bytes. Source data must be aligned.
32+
const padded = new Uint8Array([104, 101, 108, 108, 111, 33, 46, 46]);
33+
const u32 = new Uint32Array(padded.buffer);
34+
assert.equal(padded.length >> 2, u32.length, 'u32 must be 1/4 of real data');
35+
assert.equal(dec.decode(u32), 'hello!..', 'pass Uint32Array');
36+
assert.equal(dec.decode(u32.buffer), 'hello!..', 'pass Uint32Array\'s buffer');
37+
38+
// Ensure that we don't parse larger typed arrays as uint8's. We expect
39+
// nulls here to pad out the remaining three bytes in every word.
40+
const u32raw = new Uint32Array([104, 105, 33, 46]);
41+
assert.equal(dec.decode(u32raw), 'h\0\0\0i\0\0\0!\0\0\0.\0\0\0', 'u32 with nulls');
42+
});
43+
44+
test('arraylike', () => {
45+
const arr = [104, 101, 108, 108, 111];
46+
47+
if (isNative) {
48+
// Native can't take Array.
49+
assert.throws(() => dec.decode(arr));
50+
} else {
51+
// Polyfill can accept Array or array-like.
52+
assert.equal(dec.decode(arr), 'hello', 'decode arraylike');
53+
}
2754
});
2855

2956
test('constructor', () => {

text.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -254,14 +254,21 @@ FastTextDecoder.prototype['decode'] = function(buffer, options={stream: false})
254254
throw new Error(`Failed to decode: the 'stream' option is unsupported.`);
255255
}
256256

257-
// Accept Uint8Array instances as-is.
258-
let bytes = buffer;
259-
260-
// Look for ArrayBufferView, which isn't a real type, but basically represents
261-
// all the valid TypedArray types plus DataView. They all have ".buffer" as
262-
// an instance of ArrayBuffer.
263-
if (!(bytes instanceof Uint8Array) && bytes.buffer instanceof ArrayBuffer) {
257+
let bytes;
258+
259+
if (buffer instanceof Uint8Array) {
260+
// Accept Uint8Array instances as-is.
261+
bytes = buffer;
262+
} else if (buffer.buffer instanceof ArrayBuffer) {
263+
// Look for ArrayBufferView, which isn't a real type, but basically
264+
// represents all the valid TypedArray types plus DataView. They all have
265+
// ".buffer" as an instance of ArrayBuffer.
264266
bytes = new Uint8Array(buffer.buffer);
267+
} else {
268+
// The only other valid argument here is that "buffer" is an ArrayBuffer.
269+
// We also try to convert anything else passed to a Uint8Array, as this
270+
// catches anything that's array-like. Native code would throw here.
271+
bytes = new Uint8Array(buffer);
265272
}
266273

267274
return decodeImpl(/** @type {!Uint8Array} */ (bytes));

text.min.js

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)