Merge pull request #2 from maxogden/buffer-read

switch out the accumulator reader for a faster buffer reader
master
Chris Dickinson 11 years ago
commit 369325f253

@ -1,20 +1,13 @@
# varint # varint
decode [protobuf-style varint bytes](https://developers.google.com/protocol-buffers/docs/encoding#varints) and emit whole integers when enough have been accumulated that a number can be constructed; also encode whole numbers to an array of varint style bytes. encode whole numbers to an array of [protobuf-style varint bytes](https://developers.google.com/protocol-buffers/docs/encoding#varints) and also decode them.
```javascript ```javascript
var varint = require('varint') var varint = require('varint')
var bytes = varint.encode(300) // === [0xAC, 0x02] var bytes = varint.encode(300) // === [0xAC, 0x02]
, vi = varint() varint.decode(bytes) // 300
varint.decode.bytesRead // 2 (the last decode() call required 2 bytes)
vi.once('data', function(num) {
console.log('got', num)
})
vi.write(bytes[0])
vi.write(bytes[1]) // "got 300"
``` ```
## api ## api
@ -24,16 +17,22 @@ vi.write(bytes[1]) // "got 300"
### varint.encode(num[, output=[], offset=0]) -> array ### varint.encode(num[, output=[], offset=0]) -> array
encodes `num` into either the array given by `offset` or a new array at `offset` encodes `num` into either the array given by `offset` or a new array at `offset`
and returns that array. and returns that array filled with integers.
### varint.decode(data[, offset=0]) -> number
decodes `data`, which can be either a buffer or array of integers, from position `offset` or default 0 and returns the decoded original integer.
### varint.decode.bytesRead
### vi = varint() -> EventEmitter if you also require the length (number of bytes) that were required to decode the integer you can access it via `varint.decode.bytesRead`. this is an integer property that will tell you the number of bytes that the last .decode() call had to use to decode.
return a new `varint` instance. ## usage notes
### vi.write(byte) -> undefined if you are using this to decode buffers from a streaming source it's up to you to make sure that you send 'complete' buffers into `varint.decode`. the maximum number of bytes that varint will need to decode is 8, so all you have to do is make sure you are sending buffers that are at least 8 bytes long from the point at which you know a varint range begins.
write a byte to the varint. if the byte is "final" (i.e., does not have the bit at `0x80` set), for example, if you are reading buffers from a `fs.createReadStream`,
it will attempt to compile the number and emit it as a `data` event. imagine the first buffer contains one full varint range and half of a second one, and the second buffer contains the second half of the second varint range. in order to be safe across the buffer boundaries you'd just have to make sure the buffer you give to `varint.decode` contains the full varint range (8 bytes), otherwise you'll get an error.
# License # License

@ -1,33 +1,24 @@
module.exports = Decoder module.exports = read
var MSB = 0x80 var MSB = 0x80
, REST = 0x7F , REST = 0x7F
function read(buf, offset) {
function Decoder() { var res = 0
this.accum = [] , offset = offset || 0
, shift = 0
, counter = offset
, b
do {
b = buf[counter++]
res += shift < 28
? (b & REST) << shift
: (b & REST) * Math.pow(2, shift)
shift += 7
} while (b >= MSB)
read.bytesRead = counter - offset
return res
} }
Decoder.prototype.write = write;
function write(byte) {
var msb = byte & MSB
, accum = this.accum
, len
, out
accum[accum.length] = byte & REST
if(msb) {
return
}
len = accum.length
out = 0
for(var i = 0; i < len; ++i) {
out |= accum[i] << (7 * i)
}
accum.length = 0
this.ondata(out)
return
}

@ -14,4 +14,4 @@ function encode(num, out, offset) {
} }
out[offset] = num out[offset] = num
return out return out
} }

@ -1,22 +1,4 @@
module.exports = varint module.exports = {
encode: require('./encode.js')
varint.encode = require('./encode.js'); , decode: require('./decode.js')
}
var EE = require('events').EventEmitter
, Decoder = require('./decode.js')
function varint() {
var ee = new EE
, dec = new Decoder
dec.ondata = function (item) {
ee.emit("data", item)
}
ee.write = function (item) {
dec.write(item);
}
return ee
}

@ -1,7 +1,7 @@
{ {
"name": "varint", "name": "varint",
"version": "0.0.3", "version": "0.0.3",
"description": "use msb to create integer values of varying sizes", "description": "protobuf-style varint bytes - use msb to create integer values of varying sizes",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"test": "node test.js" "test": "node test.js"

@ -1,85 +1,72 @@
var varint = require('./index') var varint = require('./index')
, test = require('tape') , test = require('tape')
, decode = varint.decode
, encode = varint.encode
test('fuzz test', function(assert) { test('fuzz test', function(assert) {
var vi = varint() var expect
, expect
, encoded , encoded
vi.on('data', function(data) {
assert.equal(expect, data, 'fuzz test: '+expect.toString(16))
})
for(var i = 0, len = 100; i < len; ++i) { for(var i = 0, len = 100; i < len; ++i) {
expect = randint(0x7FFFFFFF) expect = randint(0x7FFFFFFF)
encoded = varint.encode(expect) encoded = encode(expect)
var data = decode(encoded)
for(var x = 0; x < encoded.length; ++x) { assert.equal(expect, data, 'fuzz test: ' + expect.toString())
vi.write(encoded[x]) assert.equal(decode.bytesRead, encoded.length)
}
} }
assert.end() assert.end()
}) })
test('test single byte works as expected', function(assert) { test('test single byte works as expected', function(assert) {
var num = [0xAC, 0x02] var buf = new Uint8Array(2)
var acc = varint() buf[0] = 172
buf[1] = 2
acc.on('data', function(data) { var data = decode(buf)
assert.equal(data, 300, 'should equal 300 every time') assert.equal(data, 300, 'should equal 300')
}) assert.equal(decode.bytesRead, 2)
for(var i = 0, len = 10; i < len; ++i) {
acc.write(0xAC)
acc.write(0x02)
}
assert.end() assert.end()
}) })
test('test encode works as expected', function(assert) { test('test encode works as expected', function(assert) {
var out = [] var out = []
assert.deepEqual(varint.encode(300), [0xAC, 0x02]) assert.deepEqual(encode(300), [0xAC, 0x02])
assert.end() assert.end()
}) })
test('test decode single bytes', function(assert) { test('test decode single bytes', function(assert) {
var vi = varint() var expected = randint(parseInt('1111111', '2'))
, expected = randint(parseInt('1111111', '2')) var buf = new Uint8Array(1)
buf[0] = expected
vi.once('data', function(data) { var data = decode(buf)
assert.equal(data, expected) assert.equal(data, expected)
assert.end() assert.equal(decode.bytesRead, 1)
}) assert.end()
vi.write(expected)
}) })
test('test decode multiple bytes with zero', function(assert) { test('test decode multiple bytes with zero', function(assert) {
var vi = varint() var expected = randint(parseInt('1111111', '2'))
, expected = randint(parseInt('1111111', '2')) var buf = new Uint8Array(2)
buf[0] = 128
vi.once('data', function(data) { buf[1] = expected
assert.equal(data, expected << 7) var data = decode(buf)
assert.end() assert.equal(data, expected << 7)
}) assert.equal(decode.bytesRead, 2)
assert.end()
vi.write(0x80)
vi.write(expected)
}) })
test('encode single byte', function(assert) { test('encode single byte', function(assert) {
var expected = randint(parseInt('1111111', '2')) var expected = randint(parseInt('1111111', '2'))
assert.deepEqual(varint.encode(expected), [expected]) assert.deepEqual(encode(expected), [expected])
assert.end() assert.end()
}) })
test('encode multiple byte with zero first byte', function(assert) { test('encode multiple byte with zero first byte', function(assert) {
var expected = 0x0F00 var expected = 0x0F00
assert.deepEqual(varint.encode(expected), [0x80, 0x1E]) assert.deepEqual(encode(expected), [0x80, 0x1E])
assert.end()
}) })
function randint(range) { function randint(range) {

Loading…
Cancel
Save