From cb198d02ec083c05d2e630ee51b1bd0299d99296 Mon Sep 17 00:00:00 2001 From: Kevin Ushey Date: Thu, 16 Apr 2020 15:00:25 -0700 Subject: [PATCH] support Uint8Array sub-array --- suite.js | 6 ++++++ text.js | 8 +++++--- text.min.js | 7 ++----- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/suite.js b/suite.js index 7830e5a..9351051 100644 --- a/suite.js +++ b/suite.js @@ -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]); diff --git a/text.js b/text.js index c0b3e54..0b5fef8 100644 --- a/text.js +++ b/text.js @@ -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 = []; diff --git a/text.min.js b/text.min.js index a17e39a..ad6ce8f 100644 --- a/text.min.js +++ b/text.min.js @@ -1,5 +1,2 @@ -(function(l){function m(){}function k(b,a){b=void 0===b?"utf-8":b;a=void 0===a?{fatal:!1}:a;if(-1==n.indexOf(b.toLowerCase()))throw new RangeError("Failed to construct 'TextDecoder': The encoding label provided ('"+b+"') is invalid.");if(a.fatal)throw Error("Failed to construct 'TextDecoder': the 'fatal' option is unsupported.");}if(l.TextEncoder&&l.TextDecoder)return!1;var n=["utf-8","utf8","unicode-1-1-utf-8"];Object.defineProperty(m.prototype,"encoding",{value:"utf-8"});m.prototype.encode=function(b, -a){a=void 0===a?{stream:!1}:a;if(a.stream)throw Error("Failed to encode: the 'stream' option is unsupported.");a=0;for(var g=b.length,f=0,c=Math.max(32,g+(g>>1)+7),e=new Uint8Array(c>>3<<3);a=d){if(a=d)continue}f+4>e.length&&(c+=8,c*=1+a/b.length*2,c=c>>3<<3,h=new Uint8Array(c),h.set(e),e=h);if(0===(d&4294967168))e[f++]=d;else{if(0===(d&4294965248))e[f++]= -d>>6&31|192;else if(0===(d&4294901760))e[f++]=d>>12&15|224,e[f++]=d>>6&63|128;else if(0===(d&4292870144))e[f++]=d>>18&7|240,e[f++]=d>>12&63|128,e[f++]=d>>6&63|128;else continue;e[f++]=d&63|128}}return e.slice?e.slice(0,f):e.subarray(0,f)};Object.defineProperty(k.prototype,"encoding",{value:"utf-8"});Object.defineProperty(k.prototype,"fatal",{value:!1});Object.defineProperty(k.prototype,"ignoreBOM",{value:!1});k.prototype.decode=function(b,a){a=void 0===a?{stream:!1}:a;if(a.stream)throw Error("Failed to decode: the 'stream' option is unsupported."); -b.buffer instanceof ArrayBuffer&&(b=b.buffer);b=new Uint8Array(b);a=0;for(var g=[],f=[];;){var c=a>>10&1023|55296),c=56320|c&1023);g.push(c)}}};l.TextEncoder=m;l.TextDecoder=k})("undefined"!==typeof window?window:"undefined"!==typeof global?global:this); +(function(b){function e(){}function c(a,d){a=void 0===a?"utf-8":a;d=void 0===d?{fatal:!1}:d;if(-1===f.indexOf(a.toLowerCase()))throw new RangeError("Failed to construct 'TextDecoder': The encoding label provided ('"+a+"') is invalid.");if(d.fatal)throw Error("Failed to construct 'TextDecoder': the 'fatal' option is unsupported.");}if(b.TextEncoder&&b.TextDecoder)return!1;var f=["utf-8","utf8","unicode-1-1-utf-8"];Object.defineProperty(e.prototype,"encoding",{value:"utf-8"});Object.defineProperty(c.prototype, +"encoding",{value:"utf-8"});Object.defineProperty(c.prototype,"fatal",{value:!1});Object.defineProperty(c.prototype,"ignoreBOM",{value:!1});b.TextEncoder=e;b.TextDecoder=c})("undefined"!==typeof window?window:"undefined"!==typeof global?global:this);