diff options
author | Justin Ridgewell <justin@ridgewell.name> | 2018-02-01 01:12:05 -0500 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2018-02-02 20:31:39 +0100 |
commit | d2a6110d3fa78ef6680a9ef5faa279a4f35f486c (patch) | |
tree | 60f96b0c62aac6b9deb6feca0b7e50e9e157c3d0 /lib | |
parent | 68783ae0b823c584f625c045a134bfff63f4d1b3 (diff) | |
download | android-node-v8-d2a6110d3fa78ef6680a9ef5faa279a4f35f486c.tar.gz android-node-v8-d2a6110d3fa78ef6680a9ef5faa279a4f35f486c.tar.bz2 android-node-v8-d2a6110d3fa78ef6680a9ef5faa279a4f35f486c.zip |
string_decoder: reset decoder on end
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: https://github.com/nodejs/node/pull/18494
Fixes: https://github.com/nodejs/node/issues/16564
Refs: https://github.com/nodejs/node/pull/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>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/string_decoder.js | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/lib/string_decoder.js b/lib/string_decoder.js index 891ab5bdba..1e569ba6b2 100644 --- a/lib/string_decoder.js +++ b/lib/string_decoder.js @@ -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; } @@ -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; @@ -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; } |