From 7c2286fabeb62813b14caff1c3ddb0b9fc6fe3f4 Mon Sep 17 00:00:00 2001 From: "Sakthipriyan Vairamani (thefourtheye)" Date: Tue, 5 Dec 2017 14:07:44 +0530 Subject: [PATCH] incorporate review comments --- lib/string_decoder.js | 49 +++++++++++++++++++------------------------ 1 file changed, 22 insertions(+), 27 deletions(-) diff --git a/lib/string_decoder.js b/lib/string_decoder.js index 6ab7a9fd616f19..e8a29ff59cd493 100644 --- a/lib/string_decoder.js +++ b/lib/string_decoder.js @@ -215,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; } @@ -249,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; } @@ -274,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; } @@ -286,23 +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) : ''); } -function end(buf) { - let result = ''; - - if (this.encoding === 'utf16le') - result = utf16End.call(this, buf); - 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(); +const endMappings = new Map([ + ['utf16le', utf16End], + ['utf8', utf8End], + ['base64', base64End] +]); +function end(buf) { + const result = (endMappings.get(this.encoding) || simpleEnd)(this, buf); + this.reset(this.encoding); return result; }