Skip to content

Commit

Permalink
Merge pull request samthor#11 from kevinushey/feature/uint8array-suba…
Browse files Browse the repository at this point in the history
…rray

support Uint8Array sub-array; tweak utilities
  • Loading branch information
samthor authored Apr 17, 2020
2 parents 0423145 + 8d34015 commit e4ec606
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 12 deletions.
9 changes: 9 additions & 0 deletions compile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env sh

google-closure-compiler \
--compilation_level ADVANCED \
--js_output_file text.min.js \
--language_out ECMASCRIPT5 \
--warning_level VERBOSE \
--create_source_map %outname%.map \
text.js
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
"license": "Apache-2.0",
"devDependencies": {
"chai": "^4.2.0",
"google-closure-compiler": "^20200406.0.0",
"mocha": "^7.1.0"
},
"scripts": {
"compile": "./compile.sh",
"test": "mocha"
}
}
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
12 changes: 7 additions & 5 deletions text.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Object.defineProperty(FastTextEncoder.prototype, 'encoding', {value: 'utf-8'});
* @param {{stream: boolean}=} options
* @return {!Uint8Array}
*/
FastTextEncoder.prototype.encode = function(string, options={stream: false}) {
FastTextEncoder.prototype['encode'] = function(string, options={stream: false}) {
if (options.stream) {
throw new Error(`Failed to encode: the 'stream' option is unsupported.`);
}
Expand Down Expand Up @@ -135,19 +135,21 @@ Object.defineProperty(FastTextDecoder.prototype, 'ignoreBOM', {value: false});
* @param {{stream: boolean}=} options
* @return {string}
*/
FastTextDecoder.prototype.decode = function(buffer, options={stream: false}) {
FastTextDecoder.prototype['decode'] = function(buffer, options={stream: false}) {
if (options['stream']) {
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
10 changes: 5 additions & 5 deletions text.min.js

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

8 changes: 8 additions & 0 deletions text.min.js.map

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

Loading

0 comments on commit e4ec606

Please sign in to comment.