Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 36.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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -41,36 +41,43 @@ 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); | ||
| 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; | ||
| return; | ||
| } | ||
| this.lastNeed = 0; | ||
| this.lastTotal = 0; | ||
| this.lastChar = Buffer.allocUnsafe(nb); | ||
| this.lastChar = Buffer.allocUnsafe(this._nb); | ||
| } | ||
| StringDecoder.prototype.reset = StringDecoder; | ||
| StringDecoder.prototype.write = function(buf) { | ||
| if (buf.length === 0) | ||
| return ''; | ||
| var r; | ||
| var i; | ||
| if (this.lastNeed) { | ||
| @@ -87,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; | ||
| @@ -208,9 +215,9 @@ function utf8Text(buf, i) { | ||
| // For UTF-8, a replacement character is added when ending on a partial | ||
| // character. | ||
| function utf8End(buf) { | ||
| const r = (buf && buf.length ? this.write(buf) : ''); | ||
| if (this.lastNeed) | ||
| function utf8End(ctx, buf) { | ||
| const r = (buf && buf.length ? ctx.write(buf) : ''); | ||
| if (ctx.lastNeed) | ||
| return r + '\ufffd'; | ||
| return r; | ||
| } | ||
| @@ -242,11 +249,11 @@ function utf16Text(buf, i) { | ||
| // For UTF-16LE we do not explicitly append special replacement characters if we | ||
| // end on a partial character, we simply let v8 handle that. | ||
| function utf16End(buf) { | ||
| const r = (buf && buf.length ? this.write(buf) : ''); | ||
| if (this.lastNeed) { | ||
| const end = this.lastTotal - this.lastNeed; | ||
| return r + this.lastChar.toString('utf16le', 0, end); | ||
| function utf16End(ctx, buf) { | ||
| const r = (buf && buf.length ? ctx.write(buf) : ''); | ||
| if (ctx.lastNeed) { | ||
| const end = ctx.lastTotal - ctx.lastNeed; | ||
| return r + ctx.lastChar.toString('utf16le', 0, end); | ||
| } | ||
| return r; | ||
| } | ||
| @@ -267,10 +274,10 @@ 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); | ||
| function base64End(ctx, buf) { | ||
| const r = (buf && buf.length ? ctx.write(buf) : ''); | ||
| if (ctx.lastNeed) | ||
| return r + ctx.lastChar.toString('base64', 0, 3 - ctx.lastNeed); | ||
| return r; | ||
| } | ||
| @@ -279,6 +286,18 @@ function simpleWrite(buf) { | ||
| return buf.toString(this.encoding); | ||
| } | ||
| function simpleEnd(buf) { | ||
| return (buf && buf.length ? this.write(buf) : ''); | ||
| function simpleEnd(ctx, buf) { | ||
| return (buf && buf.length ? ctx.write(buf) : ''); | ||
| } | ||
| const endMappings = new Map([ | ||
| ['utf16le', utf16End], | ||
| ['utf8', utf8End], | ||
| ['base64', base64End] | ||
| ]); | ||
| function end(buf) { | ||
| const result = (endMappings.get(this.encoding) || simpleEnd)(this, buf); | ||
Member 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. This looks "elegant" but using a switch case for the encoding and calling the function directly is probably faster. | ||
| this.reset(this.encoding); | ||
| 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.
I believe this is an implementation detail users don't have to know about.
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.
As it is, there is no way for the users to reset the string decoder, right? They have to create a new instance if needed. This will enable reusability.
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 new way to reset the state each time was not requested as far as I read the issue. So I also prefer not to expose the reset function. If I am correct the following should work.