[Pkg-javascript-commits] [node-iconv-lite] 37/83: Fixed bug in GBK encoding. Closes #13.

matthew pideil mpideil-guest at moszumanska.debian.org
Tue Apr 1 19:56:48 UTC 2014


This is an automated email from the git hooks/post-receive script.

mpideil-guest pushed a commit to branch master
in repository node-iconv-lite.

commit 7dec02981ea2866f67d88c67d98ec81998111cb5
Author: Alexander Shtuchkin <ashtuchkin at gmail.com>
Date:   Fri Jun 29 18:47:55 2012 +0400

    Fixed bug in GBK encoding. Closes #13.
---
 index.js         | 59 ++++++++++++++++++++++++++++----------------------------
 package.json     |  2 +-
 test/gbk-test.js |  8 ++++++++
 3 files changed, 39 insertions(+), 30 deletions(-)

diff --git a/index.js b/index.js
index a235be1..e330e5f 100644
--- a/index.js
+++ b/index.js
@@ -121,21 +121,21 @@ var iconv = module.exports = {
             return {
                 toEncoding: function(str) {
                     str = ensureString(str);
-                    var len = 0, strLen = str.length;
-                    for (var i = 0; i < strLen; i++) {
-                        if (!!(str.charCodeAt(i) >> 8)) {
-                            len += 2;
-                        } else {
-                            len ++;
-                        }
-                    }
-                    var newBuf = new Buffer(len);
+                    var strLen = str.length;
+                    var bufLen = strLen;
+                    for (var i = 0; i < strLen; i++)
+                        if (str.charCodeAt(i) >> 7)
+                            bufLen++;
+
+                    var newBuf = new Buffer(bufLen), gbkcode, unicode, 
+                        defaultChar = revCharsTable[iconv.defaultCharUnicode.charCodeAt(0)];
+
                     for (var i = 0, j = 0; i < strLen; i++) {
-                        var unicode = str.charCodeAt(i);
-                        if (!!(unicode >> 7)) {
-                            var gbkcode = revCharsTable[unicode] || revCharsTable[iconv.defaultCharUnicode.charCodeAt(0)];//not found in table ,replace it
-                            newBuf[j++] = gbkcode >> 8;//high byte;
-                            newBuf[j++] = gbkcode & 0xFF;//low byte
+                        unicode = str.charCodeAt(i);
+                        if (unicode >> 7) {
+                            gbkcode = revCharsTable[unicode] || defaultChar;
+                            newBuf[j++] = gbkcode >> 8; //high byte;
+                            newBuf[j++] = gbkcode & 0xFF; //low byte
                         } else {//ascii
                             newBuf[j++] = unicode;
                         }
@@ -144,24 +144,25 @@ var iconv = module.exports = {
                 },
                 fromEncoding: function(buf) {
                     buf = ensureBuffer(buf);
-                    var idx = 0, len = 0,
-                        newBuf = new Buffer(len*2),unicode,gbkcode;
-                    for (var i = 0, _len = buf.length; i < _len; i++, len++) {
-                        if (!!(buf[i] & 0x80)) {//the high bit is 1, so this byte is gbkcode's high byte.skip next byte
+                    var bufLen = buf.length, strLen = 0;
+                    for (var i = 0; i < bufLen; i++) {
+                        strLen++;
+                        if (buf[i] & 0x80) //the high bit is 1, so this byte is gbkcode's high byte.skip next byte
                             i++;
-                        }
                     }
-                    var newBuf = new Buffer(len*2);
-                    for (var i = 0, j = 0, _len = buf.length; i < _len; i++, j++) {
-                        var temp = buf[i], gbkcode, unicode;
-                        if (temp & 0x80) {
-                            gbkcode = (temp << 8) + buf[++i];
-                            unicode = table[gbkcode] || iconv.defaultCharUnicode.charCodeAt(0);//not found in table, replace with defaultCharUnicode
-                        }else {
-                            unicode = temp;
+                    var newBuf = new Buffer(strLen*2), unicode, gbkcode,
+                        defaultChar = iconv.defaultCharUnicode.charCodeAt(0);
+                    
+                    for (var i = 0, j = 0; i < bufLen; i++, j+=2) {
+                        gbkcode = buf[i];
+                        if (gbkcode & 0x80) {
+                            gbkcode = (gbkcode << 8) + buf[++i];
+                            unicode = table[gbkcode] || defaultChar;
+                        } else {
+                            unicode = gbkcode;
                         }
-                        newBuf[j*2] = unicode & 0xFF;//low byte
-                        newBuf[j*2+1] = unicode >> 8;//high byte
+                        newBuf[j] = unicode & 0xFF; //low byte
+                        newBuf[j+1] = unicode >> 8; //high byte
                     }
                     return newBuf.toString('ucs2');
                 }
diff --git a/package.json b/package.json
index 184eefc..13bc91f 100644
--- a/package.json
+++ b/package.json
@@ -1,7 +1,7 @@
 {
     "name": "iconv-lite",
     "description": "Convert character encodings in pure javascript.",
-    "version": "0.2.0",
+    "version": "0.2.1",
     
     "keywords": ["iconv", "convert", "charset"],
     "author": "Alexander Shtuchkin <ashtuchkin at gmail.com>",
diff --git a/test/gbk-test.js b/test/gbk-test.js
index 6a4933c..7b2e47b 100644
--- a/test/gbk-test.js
+++ b/test/gbk-test.js
@@ -27,4 +27,12 @@ vows.describe("GBK tests").addBatch({
         var iconvc = new (require('iconv').Iconv)('GBK','utf8');
         assert.strictEqual(iconvc.convert(contentBuffer).toString(), str);
     },
+    "GBK correctly decodes and encodes characters · and ×": function() {
+        // https://github.com/ashtuchkin/iconv-lite/issues/13
+        // Reference: http://www.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP936.TXT
+        var chars = "·×";
+        var gbkChars = new Buffer([0xA1, 0xA4, 0xA1, 0xC1]);
+        assert.strictEqual(iconv.toEncoding(chars, "GBK").toString('binary'), gbkChars.toString('binary'));
+        assert.strictEqual(iconv.fromEncoding(gbkChars, "GBK"), chars)
+    },
 }).export(module)

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-iconv-lite.git



More information about the Pkg-javascript-commits mailing list