mirror of
https://github.com/nodejs/node.git
synced 2026-11-24 21:50:36 -05:00
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>
This commit is contained in:
1 parent
e5d5137963
commit
f96b0bf494
2 files changed
+63
-3
No files matched your search
+12
-3
@@ -209,8 +209,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;
|
||||
}
|
||||
|
||||
@@ -245,6 +248,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;
|
||||
@@ -268,8 +273,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;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user