2016-10-08 18:48:03 +00:00
|
|
|
var T = require('../').table;
|
|
|
|
var old = require('crc-32').str;
|
|
|
|
var cur = require('../').str;
|
2014-06-16 21:27:47 +00:00
|
|
|
|
|
|
|
function sheetjs1(utf8) {
|
|
|
|
var buf = new Buffer(utf8);
|
2016-10-08 18:48:03 +00:00
|
|
|
for(var C = -1, i = 0; i != buf.length; ++i) {
|
|
|
|
C = (C >>> 8) ^ T[(C ^ buf[i]) & 0xFF];
|
2014-06-16 21:27:47 +00:00
|
|
|
}
|
2016-10-08 18:48:03 +00:00
|
|
|
return C ^ -1;
|
2014-06-16 21:27:47 +00:00
|
|
|
}
|
|
|
|
|
2016-10-08 18:48:03 +00:00
|
|
|
function sheetjs2(str) {
|
|
|
|
var C = -1;
|
|
|
|
for(var i = 0, L=str.length, c, d; i < L;) {
|
|
|
|
c = str.charCodeAt(i++);
|
2014-06-16 21:27:47 +00:00
|
|
|
if(c < 0x80) {
|
2016-10-08 18:48:03 +00:00
|
|
|
C = (C>>>8) ^ T[(C ^ c)&0xFF];
|
2014-06-16 21:27:47 +00:00
|
|
|
} else if(c < 0x800) {
|
2016-10-08 18:48:03 +00:00
|
|
|
C = (C>>>8) ^ T[(C ^ (192|((c>>6)&31)))&0xFF];
|
|
|
|
C = (C>>>8) ^ T[(C ^ (128|(c&63)))&0xFF];
|
2014-06-16 21:27:47 +00:00
|
|
|
} else if(c >= 0xD800 && c < 0xE000) {
|
2016-10-08 18:48:03 +00:00
|
|
|
c = (c&1023)+64; d = str.charCodeAt(i++)&1023;
|
|
|
|
C = (C>>>8) ^ T[(C ^ (240|((c>>8)&7)))&0xFF];
|
|
|
|
C = (C>>>8) ^ T[(C ^ (128|((c>>2)&63)))&0xFF];
|
|
|
|
C = (C>>>8) ^ T[(C ^ (128|((d>>6)&15)|((c&3)<<4)))&0xFF];
|
|
|
|
C = (C>>>8) ^ T[(C ^ (128|(d&63)))&0xFF];
|
2014-06-16 21:27:47 +00:00
|
|
|
} else {
|
2016-10-08 18:48:03 +00:00
|
|
|
C = (C>>>8) ^ T[(C ^ (224|((c>>12)&15)))&0xFF];
|
|
|
|
C = (C>>>8) ^ T[(C ^ (128|((c>>6)&63)))&0xFF];
|
|
|
|
C = (C>>>8) ^ T[(C ^ (128|(c&63)))&0xFF];
|
2014-06-16 21:27:47 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-08 18:48:03 +00:00
|
|
|
return C ^ -1;
|
2014-06-16 21:27:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var foobar = "foo bar baz٪☃🍣";
|
2014-06-21 02:15:23 +00:00
|
|
|
for(var i = 0; i != 4; ++i) foobar += " " + foobar;
|
2016-10-08 18:48:03 +00:00
|
|
|
|
2014-06-16 21:27:47 +00:00
|
|
|
var assert = require('assert');
|
2016-10-08 18:48:03 +00:00
|
|
|
{
|
|
|
|
assert.equal(sheetjs1(foobar), sheetjs2(foobar));
|
|
|
|
assert.equal(sheetjs1(foobar), old(foobar));
|
|
|
|
assert.equal(sheetjs1(foobar), cur(foobar));
|
|
|
|
}
|
2014-06-16 21:27:47 +00:00
|
|
|
|
2016-10-08 18:48:03 +00:00
|
|
|
var BM = require('./bm');
|
|
|
|
var suite = new BM('unicode string');
|
|
|
|
suite.add('sheetjs 1', function() { for(var j = 0; j != 1000; ++j) sheetjs1(foobar); });
|
|
|
|
suite.add('sheetjs 2', function() { for(var j = 0; j != 1000; ++j) sheetjs2(foobar); });
|
|
|
|
suite.add('last vers', function() { for(var j = 0; j != 1000; ++j) old(foobar); });
|
|
|
|
suite.add('current v', function() { for(var j = 0; j != 1000; ++j) cur(foobar); });
|
|
|
|
suite.run();
|