Socket
Socket
Sign inDemoInstall

iconv-lite

Package Overview
Dependencies
0
Maintainers
1
Versions
51
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.2.4 to 0.2.5

130

index.js

@@ -44,8 +44,5 @@ // Module exports

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
};

@@ -82,8 +79,6 @@ },

return {
// Seems that V8 is not optimizing functions if they are created again and again.
// TODO: Make same optimization for other encodings.
toEncoding: toSingleByteEncoding,
fromEncoding: fromSingleByteEncoding,
options: options,
}
};
},

@@ -93,66 +88,75 @@

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) {
var revCharsTable = options.revCharsTable = {};
for (var i = 0; i <= 0xFFFF; i++) {
revCharsTable[i] = 0;
}
var table = options.table;
for (var key in table) {
revCharsTable[table[key]] = +key;
}
}
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++;
toEncoding: toTableEncoding,
fromEncoding: fromTableEncoding,
options: options,
};
}
}
};
var newBuf = new Buffer(bufLen), gbkcode, unicode,
defaultChar = revCharsTable[iconv.defaultCharUnicode.charCodeAt(0)];
function toInternalEncoding(str) {
return new Buffer(ensureString(str), this.options.originalEncoding);
}
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');
}
}
function fromInternalEncoding(buf) {
return ensureBuffer(buf).toString(this.options.originalEncoding);
}
function toTableEncoding(str) {
str = ensureString(str);
var strLen = str.length;
var revCharsTable = this.options.revCharsTable;
var newBuf = new Buffer(strLen*2), 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.slice(0, j);
}
function fromTableEncoding(buf) {
buf = ensureBuffer(buf);
var bufLen = buf.length;
var table = this.options.table;
var newBuf = new Buffer(bufLen*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.slice(0, j).toString('ucs2');
}
function toSingleByteEncoding(str) {

@@ -159,0 +163,0 @@ str = ensureString(str);

{
"name": "iconv-lite",
"description": "Convert character encodings in pure javascript.",
"version": "0.2.4",
"version": "0.2.5",

@@ -6,0 +6,0 @@ "keywords": ["iconv", "convert", "charset"],

@@ -5,10 +5,20 @@

var encoding = "windows-1251";
var convertTimes = 1000;
var encoding = process.argv[2] || "windows-1251";
var convertTimes = 10000;
var encodingStrings = {
'windows-1251': 'This is a test string 32 chars..',
'gbk': '这是中文字符测试。。!@¥%12',
'utf8': '这是中文字符测试。。!@¥%12This is a test string 48 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:");

@@ -22,3 +32,3 @@

var duration = Date.now() - start;
var mbs = convertTimes*str.length/duration/1024;
var mbs = convertTimes*b.length/duration/1024;

@@ -32,3 +42,3 @@ console.log("iconv: "+duration+"ms, "+mbs.toFixed(2)+" Mb/s.");

var duration = Date.now() - start;
var mbs = convertTimes*str.length/duration/1024;
var mbs = convertTimes*b.length/duration/1024;

@@ -35,0 +45,0 @@ console.log("iconv-lite: "+duration+"ms, "+mbs.toFixed(2)+" Mb/s.");

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc