Skip to content

Commit

Permalink
string_decoder: reset decoder on end
Browse files Browse the repository at this point in the history
This resets the StringDecoder's state after calling `#end`. Further
writes to the decoder will act as if it were a brand new instance,
allowing simple reuse.

PR-URL: nodejs#18494
Fixes: nodejs#16564
Refs: nodejs#16594
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
  • Loading branch information
jridgewell authored and MayaLekova committed May 8, 2018
1 parent c59484a commit 25c0ebd
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/string_decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,11 @@ function utf8Text(buf, i) {
// character.
function utf8End(buf) {
const r = (buf && buf.length ? this.write(buf) : '');
if (this.lastNeed)
if (this.lastNeed) {
this.lastNeed = 0;
this.lastTotal = 0;
return r + '\ufffd';
}
return r;
}

Expand Down Expand Up @@ -246,6 +249,8 @@ function utf16End(buf) {
const r = (buf && buf.length ? this.write(buf) : '');
if (this.lastNeed) {
const end = this.lastTotal - this.lastNeed;
this.lastNeed = 0;
this.lastTotal = 0;
return r + this.lastChar.toString('utf16le', 0, end);
}
return r;
Expand All @@ -269,8 +274,12 @@ function base64Text(buf, i) {

function base64End(buf) {
const r = (buf && buf.length ? this.write(buf) : '');
if (this.lastNeed)
return r + this.lastChar.toString('base64', 0, 3 - this.lastNeed);
if (this.lastNeed) {
const end = 3 - this.lastNeed;
this.lastNeed = 0;
this.lastTotal = 0;
return r + this.lastChar.toString('base64', 0, end);
}
return r;
}

Expand Down
51 changes: 51 additions & 0 deletions test/parallel/test-string-decoder-end.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,46 @@ for (let i = 1; i <= 16; i++) {

encodings.forEach(testEncoding);

testEnd('utf8', Buffer.of(0xE2), Buffer.of(0x61), '\uFFFDa');
testEnd('utf8', Buffer.of(0xE2), Buffer.of(0x82), '\uFFFD\uFFFD');
testEnd('utf8', Buffer.of(0xE2), Buffer.of(0xE2), '\uFFFD\uFFFD');
testEnd('utf8', Buffer.of(0xE2, 0x82), Buffer.of(0x61), '\uFFFDa');
testEnd('utf8', Buffer.of(0xE2, 0x82), Buffer.of(0xAC), '\uFFFD\uFFFD');
testEnd('utf8', Buffer.of(0xE2, 0x82), Buffer.of(0xE2), '\uFFFD\uFFFD');
testEnd('utf8', Buffer.of(0xE2, 0x82, 0xAC), Buffer.of(0x61), '€a');

testEnd('utf16le', Buffer.of(0x3D), Buffer.of(0x61, 0x00), 'a');
testEnd('utf16le', Buffer.of(0x3D), Buffer.of(0xD8, 0x4D, 0xDC), '\u4DD8');
testEnd('utf16le', Buffer.of(0x3D, 0xD8), Buffer.of(), '\uD83D');
testEnd('utf16le', Buffer.of(0x3D, 0xD8), Buffer.of(0x61, 0x00), '\uD83Da');
testEnd(
'utf16le',
Buffer.of(0x3D, 0xD8),
Buffer.of(0x4D, 0xDC),
'\uD83D\uDC4D'
);
testEnd('utf16le', Buffer.of(0x3D, 0xD8, 0x4D), Buffer.of(), '\uD83D');
testEnd(
'utf16le',
Buffer.of(0x3D, 0xD8, 0x4D),
Buffer.of(0x61, 0x00),
'\uD83Da'
);
testEnd('utf16le', Buffer.of(0x3D, 0xD8, 0x4D), Buffer.of(0xDC), '\uD83D');
testEnd(
'utf16le',
Buffer.of(0x3D, 0xD8, 0x4D, 0xDC),
Buffer.of(0x61, 0x00),
'👍a'
);

testEnd('base64', Buffer.of(0x61), Buffer.of(), 'YQ==');
testEnd('base64', Buffer.of(0x61), Buffer.of(0x61), 'YQ==YQ==');
testEnd('base64', Buffer.of(0x61, 0x61), Buffer.of(), 'YWE=');
testEnd('base64', Buffer.of(0x61, 0x61), Buffer.of(0x61), 'YWE=YQ==');
testEnd('base64', Buffer.of(0x61, 0x61, 0x61), Buffer.of(), 'YWFh');
testEnd('base64', Buffer.of(0x61, 0x61, 0x61), Buffer.of(0x61), 'YWFhYQ==');

function testEncoding(encoding) {
bufs.forEach((buf) => {
testBuf(encoding, buf);
Expand Down Expand Up @@ -66,3 +106,14 @@ function testBuf(encoding, buf) {
assert.strictEqual(res1, res3, 'one byte at a time should match toString');
assert.strictEqual(res2, res3, 'all bytes at once should match toString');
}

function testEnd(encoding, incomplete, next, expected) {
let res = '';
const s = new SD(encoding);
res += s.write(incomplete);
res += s.end();
res += s.write(next);
res += s.end();

assert.strictEqual(res, expected);
}

0 comments on commit 25c0ebd

Please sign in to comment.