Skip to content

Commit 85371d5

Browse files
committed
add stack fix
1 parent 2bb3030 commit 85371d5

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed

text.js

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -148,27 +148,42 @@ FastTextDecoder.prototype.decode = function(buffer, options={stream: false}) {
148148
buffer = buffer.buffer;
149149
}
150150

151-
const bytes = new Uint8Array(buffer);
151+
let bytes = new Uint8Array(buffer);
152152
let pos = 0;
153-
const len = bytes.length;
154-
const out = [];
153+
let pending = [];
154+
const chunks = [];
155155

156-
while (pos < len) {
157-
const byte1 = bytes[pos++];
158-
if (byte1 === 0) {
159-
out.push(0);
160-
continue;
156+
for (;;) {
157+
const more = pos < bytes.length;
158+
159+
// If there's no more data or we're >65k bytes, create a chunk.
160+
// This isn't done at the end by simply slicing the data into equal sized
161+
// chunks as we might hit a surrogate pair.
162+
if (!more || (pos & 0x10000)) {
163+
chunks.push(String.fromCharCode.apply(null, pending));
164+
165+
if (!more) {
166+
return chunks.join('');
167+
}
168+
169+
// Move the buffer forward and create another chunk.
170+
pending = [];
171+
bytes = bytes.subarray(pos);
172+
pos = 0;
161173
}
162174

163-
if ((byte1 & 0x80) === 0) { // 1-byte
164-
out.push(byte1);
175+
const byte1 = bytes[pos++];
176+
if (byte1 === 0) {
177+
pending.push(0);
178+
} else if ((byte1 & 0x80) === 0) { // 1-byte
179+
pending.push(byte1);
165180
} else if ((byte1 & 0xe0) === 0xc0) { // 2-byte
166181
const byte2 = bytes[pos++] & 0x3f;
167-
out.push(((byte1 & 0x1f) << 6) | byte2);
182+
pending.push(((byte1 & 0x1f) << 6) | byte2);
168183
} else if ((byte1 & 0xf0) === 0xe0) {
169184
const byte2 = bytes[pos++] & 0x3f;
170185
const byte3 = bytes[pos++] & 0x3f;
171-
out.push(((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3);
186+
pending.push(((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3);
172187
} else if ((byte1 & 0xf8) === 0xf0) {
173188
const byte2 = bytes[pos++] & 0x3f;
174189
const byte3 = bytes[pos++] & 0x3f;
@@ -179,16 +194,14 @@ FastTextDecoder.prototype.decode = function(buffer, options={stream: false}) {
179194
if (codepoint > 0xffff) {
180195
// codepoint &= ~0x10000;
181196
codepoint -= 0x10000;
182-
out.push((codepoint >>> 10) & 0x3ff | 0xd800);
197+
pending.push((codepoint >>> 10) & 0x3ff | 0xd800);
183198
codepoint = 0xdc00 | codepoint & 0x3ff;
184199
}
185-
out.push(codepoint);
200+
pending.push(codepoint);
186201
} else {
187202
// FIXME: we're ignoring this
188203
}
189204
}
190-
191-
return String.fromCharCode.apply(null, out);
192205
}
193206

194207
scope['TextEncoder'] = FastTextEncoder;

0 commit comments

Comments
 (0)