From 18aa870744a3d4cc607168a384e46f49dc8e3804 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=80=E1=85=B5=E1=86=B7=E1=84=89=E1=85=A5=E1=86=BC?= =?UTF-8?q?=E1=84=8C=E1=85=AE=E1=86=AB?= Date: Wed, 28 Oct 2015 17:47:59 +0900 Subject: [PATCH] Add CJK support. --- lib/reader2.js | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/lib/reader2.js b/lib/reader2.js index 6c22e07..4fcf0a4 100644 --- a/lib/reader2.js +++ b/lib/reader2.js @@ -236,20 +236,46 @@ Reader.prototype.readString = function(data) { var code = this.reader.nextUInt8(); if (code >= 0 && code < 32) { - return this.reader.nextString(code); + return this._nextString(code); } else if (code >= 0x30 && code <= 0x33) { this.reader.move(-1); var len = this.reader.nextUInt16BE() - 0x3000; - return this.reader.nextString(len); + return this._nextString(len); } else if (code === 0x53) { var len = this.reader.nextUInt16BE(); - return this.reader.nextString(len); + return this._nextString(len); } else if (code === 0x52) { var len = this.reader.nextUInt16BE(); - return this.reader.nextString(len) + this.readString(); + return this._nextString(len) + this.readString(); } }; +Reader.prototype._nextString = function(length, encoding) { + + assert(length >= 0, 'Length must be no negative'); + + var tbuf = []; + while(length > 0) { + + var t = this.reader.nextUInt8(); + tbuf.push(t); + if(t >= 0xC2 && t < 0xE0) { + tbuf.push(this.reader.nextUInt8()); + } + else if(t >= 0xE0 && t < 0xF0) { + tbuf.push(this.reader.nextUInt8()); + tbuf.push(this.reader.nextUInt8()); + } + else if(t >= 0xF0 && t < 0xF5) { + tbuf.push(this.reader.nextUInt8()); + tbuf.push(this.reader.nextUInt8()); + tbuf.push(this.reader.nextUInt8()); + } + length--; + } + return new Buffer(tbuf).toString(encoding); +}; + Reader.prototype.readLong = function(data) { if (data) this.reader = new BufferReader(data);