Skip to content

Commit

Permalink
incorporate review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
thefourtheye committed Dec 5, 2017
1 parent f34b251 commit 7c2286f
Showing 1 changed file with 22 additions and 27 deletions.
49 changes: 22 additions & 27 deletions lib/string_decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}

Expand All @@ -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;
}

0 comments on commit 7c2286f

Please sign in to comment.