-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
string_decoder: make write after end to reset #16594
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -45,15 +45,13 @@ function StringDecoder(encoding) { | |
| this.lastNeed = 0; | ||
| this.lastTotal = 0; | ||
| this.lastChar = Buffer.allocUnsafe(this._nb || 0); | ||
| this._closed = false; | ||
| return; | ||
| } | ||
|
|
||
| this.encoding = normalizeEncoding(encoding); | ||
| switch (this.encoding) { | ||
| case 'utf16le': | ||
| this.text = utf16Text; | ||
| this.end = utf16End; | ||
| this._nb = 4; | ||
| break; | ||
| case 'utf8': | ||
|
|
@@ -62,28 +60,21 @@ function StringDecoder(encoding) { | |
| break; | ||
| case 'base64': | ||
| this.text = base64Text; | ||
| this.end = base64End; | ||
| 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(this._nb); | ||
| this._closed = false; | ||
| } | ||
|
|
||
| StringDecoder.prototype.reset = StringDecoder; | ||
|
|
||
| StringDecoder.prototype.write = function(buf) { | ||
| if (this._closed === true) | ||
| this.reset(); | ||
|
|
||
| if (buf.length === 0) | ||
| return ''; | ||
|
|
||
|
|
@@ -103,7 +94,7 @@ StringDecoder.prototype.write = function(buf) { | |
| return r || ''; | ||
| }; | ||
|
|
||
| StringDecoder.prototype.end = utf8End; | ||
| StringDecoder.prototype.end = end; | ||
|
|
||
| // Returns only complete characters in a Buffer | ||
| StringDecoder.prototype.text = utf8Text; | ||
|
|
@@ -226,7 +217,6 @@ 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; | ||
|
|
@@ -299,3 +289,20 @@ function simpleWrite(buf) { | |
| function simpleEnd(buf) { | ||
| return (buf && buf.length ? this.write(buf) : ''); | ||
| } | ||
|
|
||
| function end(buf) { | ||
| let result = ''; | ||
|
|
||
| if (this.encoding === 'utf16le') | ||
| result = utf16End.call(this, buf); | ||
|
Contributor
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're the only ones calling these methods directly, then we should be able to avoid the overhead of
Contributor
Author
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. Ack. |
||
| else if (this.encoding === 'utf8') | ||
| result = utf8End.call(this, buf); | ||
| else if (this.encoding === 'base64') | ||
| result = base64End.call(this, buf); | ||
| else | ||
| result = simpleEnd.call(this, buf); | ||
|
|
||
| this.reset(); | ||
|
Contributor
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. Would it be better to use
Contributor
Author
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. We still cannot get rid of the
Contributor
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. Correct, but |
||
|
|
||
| return result; | ||
| } | ||
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.
Might consider using a
switchhere.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.
Ack.