Comparing version
26
index.js
@@ -254,2 +254,4 @@ /** | ||
var buf = str; | ||
var enableZero = true; | ||
var enableGroupSpace = true; | ||
var output, offset, digits, cur, i, c, t, len, padding; | ||
@@ -268,5 +270,9 @@ | ||
}); | ||
table = t; | ||
} | ||
} | ||
enableZero = !table[ASCII85_ZERO_VALUE]; | ||
enableGroupSpace = !table[ASCII85_GROUP_SPACE_VALUE]; | ||
if (!(buf instanceof Buffer)) { | ||
@@ -277,7 +283,15 @@ buf = _BufferFrom(buf); | ||
// estimate output length and alloc buffer for it. | ||
for (i = 0, t = 0, len = buf.length; i < len; i++) { | ||
c = buf.readUInt8(i); | ||
t = 0; | ||
if (c === ASCII85_ZERO_VALUE || c === ASCII85_GROUP_SPACE_VALUE) { | ||
t++; | ||
if (enableZero || enableGroupSpace) { | ||
for (i = 0, len = buf.length; i < len; i++) { | ||
c = buf.readUInt8(i); | ||
if (enableZero && c === ASCII85_ZERO_VALUE) { | ||
t++; | ||
} | ||
if (enableGroupSpace && c === ASCII85_GROUP_SPACE_VALUE) { | ||
t++; | ||
} | ||
} | ||
@@ -310,3 +324,3 @@ } | ||
if (c === ASCII85_ZERO_VALUE) { | ||
if (enableZero && c === ASCII85_ZERO_VALUE) { | ||
offset += output.write(ASCII85_NULL_STRING, offset); | ||
@@ -316,3 +330,3 @@ continue; | ||
if (c === ASCII85_GROUP_SPACE_VALUE) { | ||
if (enableGroupSpace && c === ASCII85_GROUP_SPACE_VALUE) { | ||
offset += output.write(ASCII85_GROUP_SPACE_STRING, offset); | ||
@@ -319,0 +333,0 @@ continue; |
{ | ||
"name": "ascii85", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"engines" : { | ||
@@ -5,0 +5,0 @@ "node" : ">=0.12" |
@@ -6,9 +6,18 @@ 'use strict'; | ||
var _BufferAllocUnsafe = Buffer.allocUnsafe || function(size) { | ||
return new Buffer(size); | ||
}; | ||
describe('Random codec', function() { | ||
var i; | ||
var codecs = [ascii85, ascii85.ZeroMQ, ascii85.PostScript]; | ||
var i, result; | ||
for (i = 10; i < 100; i += 1) { | ||
it('tests random binarys with length ' + i, function() { | ||
random(10, i).forEach(function(s) { | ||
expect(ascii85.decode(ascii85.encode(s)).toString('binary')).to.equal(s.toString('binary')); | ||
random(10, i).forEach(function(buf) { | ||
codecs.forEach(function(codec) { | ||
result = codec.decode(codec.encode(buf)); | ||
expect(result.length).to.equal(buf.length); | ||
expect(result.toString('binary')).to.equal(buf.toString('binary')); | ||
}); | ||
}); | ||
@@ -20,16 +29,16 @@ }); | ||
function random(num, len) { | ||
var strs = []; | ||
var i, j, s; | ||
var bufs = []; | ||
var i, j, buf; | ||
for (i = 0; i < num; i++) { | ||
s = ''; | ||
buf = _BufferAllocUnsafe(len); | ||
for (j = 0; j < len; j++) { | ||
s += String.fromCharCode(~~(Math.random() * 256)); | ||
buf.writeUInt8(~~(Math.random() * 256), j); | ||
} | ||
strs.push(Buffer.from? Buffer.from(s, 'binary'): new Buffer(s, 'binary')); | ||
bufs.push(buf); | ||
} | ||
return strs; | ||
return bufs; | ||
} |
22774
2.68%445
4.46%