This commit is contained in:
Ryan Dahl
2010-12-01 20:59:06 -08:00
parent e232f6e735
commit dd53ceebe4
7 changed files with 144 additions and 147 deletions

View File

@@ -1,5 +1,5 @@
var StringDecoder = exports.StringDecoder = function (encoding) {
this.encoding = (encoding || 'utf8').toLowerCase().replace(/[-_]/,'');
var StringDecoder = exports.StringDecoder = function(encoding) {
this.encoding = (encoding || 'utf8').toLowerCase().replace(/[-_]/, '');
if (this.encoding === 'utf8') {
this.charBuffer = new Buffer(4);
this.charReceived = 0;
@@ -8,7 +8,7 @@ var StringDecoder = exports.StringDecoder = function (encoding) {
};
StringDecoder.prototype.write = function (buffer) {
StringDecoder.prototype.write = function(buffer) {
// If not utf8...
if (this.encoding !== 'utf8') {
return buffer.toString(this.encoding);
@@ -18,9 +18,9 @@ StringDecoder.prototype.write = function (buffer) {
// if our last write ended with an incomplete multibyte character
if (this.charLength) {
// determine how many remaining bytes this buffer has to offer for this char
var i = (buffer.length >= this.charLength - this.charReceived)
? this.charLength - this.charReceived
: buffer.length;
var i = (buffer.length >= this.charLength - this.charReceived) ?
this.charLength - this.charReceived :
buffer.length;
// add the new bytes to the char buffer
buffer.copy(this.charBuffer, this.charReceived, 0, i);
@@ -46,7 +46,8 @@ StringDecoder.prototype.write = function (buffer) {
// determine how many bytes we have to check at the end of this buffer
var i = (buffer.length >= 3) ? 3 : buffer.length;
// figure out if one of the last i bytes of our buffer announces an incomplete char
// Figure out if one of the last i bytes of our buffer announces an
// incomplete char.
for (; i > 0; i--) {
c = buffer[buffer.length - i];