Merge pull request #4 from mafintosh/patch-1

Add encode.bytesWritten
master
Chris Dickinson 10 years ago
commit f0e5547f69

@ -27,6 +27,10 @@ decodes `data`, which can be either a buffer or array of integers, from position
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. 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.
### varint.encode.bytesWritten
similar to `bytesRead` when encoding a number it can be useful to know how many bytes where written (especially if you pass an output array). you can access this via `varint.encode.bytesWritten` which holds the number of bytes written in the last encode.
## usage notes ## usage notes
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. 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.

@ -7,11 +7,15 @@ var MSB = 0x80
function encode(num, out, offset) { function encode(num, out, offset) {
out = out || [] out = out || []
offset = offset || 0 offset = offset || 0
var oldOffset = offset
while(num & MSBALL) { while(num & MSBALL) {
out[offset++] = (num & 0xFF) | MSB out[offset++] = (num & 0xFF) | MSB
num >>>= 7 num >>>= 7
} }
out[offset] = num out[offset] = num
encode.bytesWritten = offset - oldOffset + 1
return out return out
} }

@ -60,12 +60,14 @@ test('test decode multiple bytes with zero', function(assert) {
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(encode(expected), [expected]) assert.deepEqual(encode(expected), [expected])
assert.equal(encode.bytesWritten, 1)
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(encode(expected), [0x80, 0x1E]) assert.deepEqual(encode(expected), [0x80, 0x1E])
assert.equal(encode.bytesWritten, 2)
assert.end() assert.end()
}) })

Loading…
Cancel
Save