// Copyright Joyent, Inc. and other Node contributors. // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the // "Software"), to deal in the Software without restriction, including // without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit // persons to whom the Software is furnished to do so, subject to the // following conditions: // // The above copyright notice and this permission notice shall be included // in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE // USE OR OTHER DEALINGS IN THE SOFTWARE. var StringDecoder = exports.StringDecoder = function(encoding) { this.encoding = (encoding || 'utf8').toLowerCase().replace(/[-_]/, ''); if (this.encoding === 'utf8') { this.charBuffer = new Buffer(6); this.charReceived = 0; this.charLength = 0; } }; StringDecoder.prototype.write = function(buffer) { // If not utf8... if (this.encoding !== 'utf8') { return buffer.toString(this.encoding); } var charStr = ''; var offset = 0; // if our last write ended with an incomplete multibyte character while (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; // add the new bytes to the char buffer buffer.copy(this.charBuffer, this.charReceived, offset, i); this.charReceived += (i - offset); offset = i; if (this.charReceived < this.charLength) { // still not enough chars in this buffer? wait for more ... return ''; } // get the character that was split charStr = this.charBuffer.slice(0, this.charLength).toString(); // lead surrogate (D800-DBFF) is also the incomplete character if (this.charLength === 3) { var charCode = charStr.charCodeAt(0); if (charCode >= 0xD800 && charCode <= 0xDBFF) { charStr = ''; this.charLength += 3; // size of trail surrogate (DC00-DFFF) continue; } } this.charReceived = this.charLength = 0; // if there are no more bytes in this buffer, just emit our char if (i == buffer.length) return charStr; // otherwise cut off the characters end from the beginning of this buffer buffer = buffer.slice(i, buffer.length); break; } // 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. for (; i > 0; i--) { var 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; } } var end = buffer.length; if (this.charLength) { // buffer the incomplete character bytes we got buffer.copy(this.charBuffer, 0, buffer.length - i, buffer.length); this.charReceived = i; end -= i; } charStr += buffer.toString('utf8', 0, end); // lead surrogate (D800-DBFF) is also the incomplete character end = charStr.length - 1; var charCode = charStr.charCodeAt(end); if (charCode >= 0xD800 && charCode <= 0xDBFF) { // CESU-8 represents each of Surrogate Pair by 3-bytes this.charLength += 3 this.charReceived += 3 this.charBuffer.copy(this.charBuffer, 3, 0, 3); this.charBuffer.write(charStr.charAt(end)); return charStr.substring(0, end); } // or just emit the charStr return charStr; };