-
Notifications
You must be signed in to change notification settings - Fork 29.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
string_decoder: make write after end to reset #16594
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,36 +41,52 @@ function normalizeEncoding(enc) { | |
// characters. | ||
exports.StringDecoder = StringDecoder; | ||
function StringDecoder(encoding) { | ||
if (encoding === this.encoding && encoding !== undefined) { | ||
this.lastNeed = 0; | ||
this.lastTotal = 0; | ||
this.lastChar = Buffer.allocUnsafe(this._nb || 0); | ||
this._closed = false; | ||
return; | ||
} | ||
|
||
this.encoding = normalizeEncoding(encoding); | ||
var nb; | ||
switch (this.encoding) { | ||
case 'utf16le': | ||
this.text = utf16Text; | ||
this.end = utf16End; | ||
nb = 4; | ||
this._nb = 4; | ||
break; | ||
case 'utf8': | ||
this.fillLast = utf8FillLast; | ||
nb = 4; | ||
this._nb = 4; | ||
break; | ||
case 'base64': | ||
this.text = base64Text; | ||
this.end = base64End; | ||
nb = 3; | ||
this._nb = 3; | ||
break; | ||
default: | ||
this.write = simpleWrite; | ||
this.end = simpleEnd; | ||
this._nb = 0; | ||
this._closed = false; | ||
return; | ||
} | ||
this.lastNeed = 0; | ||
this.lastTotal = 0; | ||
this.lastChar = Buffer.allocUnsafe(nb); | ||
this.lastChar = Buffer.allocUnsafe(this._nb); | ||
this._closed = false; | ||
} | ||
|
||
StringDecoder.prototype.reset = StringDecoder; | ||
|
||
StringDecoder.prototype.write = function(buf) { | ||
if (this._closed === true) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we explicitly compare strictly against the value, the performance would be better right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not with TurboFan, no. I asked about this in another PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For reference, I think this is the previous discussion in question: #16397 (comment) |
||
this.reset(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this logic be in |
||
|
||
if (buf.length === 0) | ||
return ''; | ||
|
||
var r; | ||
var i; | ||
if (this.lastNeed) { | ||
|
@@ -210,6 +226,7 @@ function utf8Text(buf, i) { | |
// character. | ||
function utf8End(buf) { | ||
const r = (buf && buf.length ? this.write(buf) : ''); | ||
this._closed = true; | ||
if (this.lastNeed) | ||
return r + '\ufffd'; | ||
return r; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A nit: it seems this should go before the
stringDecoder.write()
, ABC-wise.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!