[Pkg-javascript-commits] [node-iconv-lite] 44/83: Optimized table and internal encoding use "single-byte" way.
matthew pideil
mpideil-guest at moszumanska.debian.org
Tue Apr 1 19:56:49 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 a03ad0e1ad102e57a821c522687f5fb62c109eb7
Author: fengmk2 <fengmk2 at gmail.com>
Date: Sun Aug 26 01:50:45 2012 +0800
Optimized table and internal encoding use "single-byte" way.
---
index.js | 137 +++++++++++++++++++++++++++++------------------------
test/perfomance.js | 16 +++++--
2 files changed, 88 insertions(+), 65 deletions(-)
diff --git a/index.js b/index.js
index 364c3b9..bd4858b 100644
--- a/index.js
+++ b/index.js
@@ -41,12 +41,9 @@ var iconv = module.exports = {
encodings: {
internal: function(options) {
return {
- toEncoding: function(str) {
- return new Buffer(ensureString(str), options.originalEncoding);
- },
- fromEncoding: function(buf) {
- return ensureBuffer(buf).toString(options.originalEncoding);
- }
+ toEncoding: toInternalEncoding,
+ fromEncoding: fromInternalEncoding,
+ options: options
};
},
utf8: "internal",
@@ -83,75 +80,91 @@ var iconv = module.exports = {
toEncoding: toSingleByteEncoding,
fromEncoding: fromSingleByteEncoding,
options: options,
- }
+ };
},
// Codepage double-byte encodings.
table: function(options) {
- var table = options.table, key, revCharsTable = options.revCharsTable;
- if (!table) {
- throw new Error("Encoding '" + options.type +"' has incorect 'table' option");
+ if (!options.table) {
+ throw new Error("Encoding '" + options.type + "' has incorect 'table' option");
}
- if(!revCharsTable) {
- revCharsTable = options.revCharsTable = {};
- for (key in table) {
- revCharsTable[table[key]] = parseInt(key);
+ if (!options.revCharsTable) {
+ options.revCharsTable = {};
+ var table = options.table;
+ for (var key in table) {
+ options.revCharsTable[table[key]] = parseInt(key, 10);
}
}
return {
- toEncoding: function(str) {
- str = ensureString(str);
- 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++) {
- 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;
- }
- }
- return newBuf;
- },
- fromEncoding: function(buf) {
- buf = ensureBuffer(buf);
- 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(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] = unicode & 0xFF; //low byte
- newBuf[j+1] = unicode >> 8; //high byte
- }
- return newBuf.toString('ucs2');
- }
- }
+ toEncoding: toTableEncoding,
+ fromEncoding: fromTableEncoding,
+ options: options,
+ };
}
}
};
+function toInternalEncoding(str) {
+ return new Buffer(ensureString(str), this.options.originalEncoding);
+}
+
+function fromInternalEncoding(buf) {
+ return ensureBuffer(buf).toString(this.options.originalEncoding);
+}
+
+function toTableEncoding(str) {
+ str = ensureString(str);
+ var strLen = str.length;
+ var bufLen = strLen;
+ for (var i = 0; i < strLen; i++) {
+ if (str.charCodeAt(i) >> 7) {
+ bufLen++;
+ }
+ }
+ var revCharsTable = this.options.revCharsTable;
+ var newBuf = new Buffer(bufLen), gbkcode, unicode,
+ defaultChar = revCharsTable[iconv.defaultCharUnicode.charCodeAt(0)];
+
+ for (var i = 0, j = 0; i < strLen; i++) {
+ 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;
+ }
+ }
+ return newBuf;
+}
+
+function fromTableEncoding(buf) {
+ buf = ensureBuffer(buf);
+ 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 table = this.options.table;
+ 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] = unicode & 0xFF; //low byte
+ newBuf[j+1] = unicode >> 8; //high byte
+ }
+ return newBuf.toString('ucs2');
+}
+
function toSingleByteEncoding(str) {
str = ensureString(str);
diff --git a/test/perfomance.js b/test/perfomance.js
index 7d75d1f..f6c6105 100644
--- a/test/perfomance.js
+++ b/test/perfomance.js
@@ -2,14 +2,24 @@
var iconv = require('iconv');
var iconv_lite = require("../index");
-var encoding = "windows-1251";
+var encoding = process.argv[2] || "windows-1251";
var convertTimes = 1000;
+var encodingStrings = {
+ 'windows-1251': 'This is a test string 32 chars..',
+ 'gbk': '这是中文字符测试。。!@¥%',
+ 'utf8': '这是中文字符测试。。!@¥%This is a test string 32 chars..',
+};
// Test encoding.
-var str = "This is a test string 32 chars..";
-for (var i = 0; i < 13; i++)
+var str = encodingStrings[encoding];
+if (!str) {
+ throw new Error('Don\'t support ' + encoding + ' performance test.');
+}
+for (var i = 0; i < 13; i++) {
str = str + str;
+}
+console.log('\n' + encoding + ' charset performance test:');
console.log("\nEncoding "+str.length+" chars "+convertTimes+" times:");
var start = Date.now();
--
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