2010-12-02 05:59:06 +01:00
|
|
|
var StringDecoder = exports.StringDecoder = function(encoding) {
|
|
|
|
this.encoding = (encoding || 'utf8').toLowerCase().replace(/[-_]/, '');
|
2010-06-16 03:19:25 +02:00
|
|
|
if (this.encoding === 'utf8') {
|
|
|
|
this.charBuffer = new Buffer(4);
|
|
|
|
this.charReceived = 0;
|
|
|
|
this.charLength = 0;
|
|
|
|
}
|
2010-05-18 04:25:51 +02:00
|
|
|
};
|
|
|
|
|
2010-06-16 03:19:25 +02:00
|
|
|
|
2010-12-02 05:59:06 +01:00
|
|
|
StringDecoder.prototype.write = function(buffer) {
|
2010-06-30 08:12:46 +02:00
|
|
|
// If not utf8...
|
2010-06-16 03:19:25 +02:00
|
|
|
if (this.encoding !== 'utf8') {
|
|
|
|
return buffer.toString(this.encoding);
|
|
|
|
}
|
|
|
|
|
2010-05-18 04:25:51 +02:00
|
|
|
var charStr = '';
|
|
|
|
// 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
|
2010-12-02 05:59:06 +01:00
|
|
|
var i = (buffer.length >= this.charLength - this.charReceived) ?
|
|
|
|
this.charLength - this.charReceived :
|
|
|
|
buffer.length;
|
2010-05-18 04:25:51 +02:00
|
|
|
|
|
|
|
// add the new bytes to the char buffer
|
2010-06-29 18:36:50 +02:00
|
|
|
buffer.copy(this.charBuffer, this.charReceived, 0, i);
|
2010-05-18 04:25:51 +02:00
|
|
|
this.charReceived += i;
|
|
|
|
|
|
|
|
if (this.charReceived < this.charLength) {
|
|
|
|
// still not enough chars in this buffer? wait for more ...
|
2010-06-16 03:19:25 +02:00
|
|
|
return '';
|
2010-05-18 04:25:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// get the character that was split
|
|
|
|
charStr = this.charBuffer.slice(0, this.charLength).toString();
|
|
|
|
this.charReceived = this.charLength = 0;
|
|
|
|
|
2010-06-16 03:19:25 +02:00
|
|
|
// if there are no more bytes in this buffer, just emit our char
|
|
|
|
if (i == buffer.length) return charStr;
|
2010-05-18 04:25:51 +02:00
|
|
|
|
2010-06-16 03:19:25 +02:00
|
|
|
// otherwise cut off the characters end from the beginning of this buffer
|
2010-05-18 04:25:51 +02:00
|
|
|
buffer = buffer.slice(i, buffer.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// determine how many bytes we have to check at the end of this buffer
|
2010-06-16 03:19:25 +02:00
|
|
|
var i = (buffer.length >= 3) ? 3 : buffer.length;
|
2010-05-18 04:25:51 +02:00
|
|
|
|
2010-12-02 05:59:06 +01:00
|
|
|
// Figure out if one of the last i bytes of our buffer announces an
|
|
|
|
// incomplete char.
|
2010-05-18 04:25:51 +02:00
|
|
|
for (; i > 0; i--) {
|
|
|
|
c = buffer[buffer.length - i];
|
|
|
|
|
|
|
|
// See http://en.wikipedia.org/wiki/UTF-8#Description
|
|
|
|
|
|
|
|
// 110XXXXX
|
|
|
|
if (i == 1 && c >> 5 == 0x06) {
|
|
|
|
this.charLength = 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 1110XXXX
|
|
|
|
if (i <= 2 && c >> 4 == 0x0E) {
|
|
|
|
this.charLength = 3;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 11110XXX
|
|
|
|
if (i <= 3 && c >> 3 == 0x1E) {
|
|
|
|
this.charLength = 4;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.charLength) {
|
|
|
|
// no incomplete char at the end of this buffer, emit the whole thing
|
2010-06-16 03:19:25 +02:00
|
|
|
return charStr + buffer.toString();
|
2010-05-18 04:25:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// buffer the incomplete character bytes we got
|
2010-06-29 18:36:50 +02:00
|
|
|
buffer.copy(this.charBuffer, 0, buffer.length - i, buffer.length);
|
2010-05-18 04:25:51 +02:00
|
|
|
this.charReceived = i;
|
|
|
|
|
|
|
|
if (buffer.length - i > 0) {
|
|
|
|
// buffer had more bytes before the incomplete char, emit them
|
2010-06-16 03:19:25 +02:00
|
|
|
return charStr + buffer.toString('utf8', 0, buffer.length - i);
|
2010-05-18 04:25:51 +02:00
|
|
|
}
|
2010-06-16 03:19:25 +02:00
|
|
|
|
|
|
|
// or just emit the charStr
|
|
|
|
return charStr;
|
2010-05-18 04:25:51 +02:00
|
|
|
};
|