Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
./bench.js
./test.js
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# varint

encode whole numbers to an array of [protobuf-style varint bytes](https://developers.google.com/protocol-buffers/docs/encoding#varints) and also decode them.
> 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
var varint = require('varint')
```js
import varint from 'varint'

var bytes = varint.encode(300) // === [0xAC, 0x02]
varint.decode(bytes) // 300
Expand All @@ -12,33 +12,43 @@ varint.decode.bytes // 2 (the last decode() call required 2 bytes)

## api

### varint = require('varint')
### varint = await import('varint')
<br>

### varint.encode(num[, buffer=[], offset=0]) -> buffer

Encodes `num` into `buffer` starting at `offset`. returns `buffer`, with the encoded varint written into it. If `buffer` is not provided, it will default to a new array.

`varint.encode.bytes` will now be set to the number of bytes
modified.
<br>
<br>

### 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.

Throws a `RangeError` when `data` does not represent a valid encoding.
<br>
<br>

### varint.decode.bytes

if you also require the length (number of bytes) that were required to decode the integer you can access it via `varint.decode.bytes`. this is an integer property that will tell you the number of bytes that the last .decode() call had to use to decode.
<br>
<br>

### varint.encode.bytes

similar to `decode.bytes` 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.bytes` which holds the number of bytes written in the last encode.

<br>
<br>

### varint.encodingLength(num)

returns the number of bytes this number will be encoded as, up to a maximum of 8.
<br>
<br>

## usage notes

Expand Down
10 changes: 6 additions & 4 deletions bench.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@

import varint from './exports/index.js'

var N = 1e7
var M = 10
/*
Expand Down Expand Up @@ -30,13 +33,12 @@ var M = 10
recomendation: return undefined
*/

var buffer = new Buffer(8)
var _buffer = buffer.slice(0, 4)
var varint = require('./')
var buffer = new Uint8Array(8)
var _buffer = buffer.subarray(0, 4)
var l = N
var invalid = 0

includeInvalid = !!process.env.INVALID
let includeInvalid = !!process.env.INVALID

var start = Date.now()
while (l--) {
Expand Down
29 changes: 0 additions & 29 deletions decode.js

This file was deleted.

30 changes: 0 additions & 30 deletions encode.js

This file was deleted.

25 changes: 25 additions & 0 deletions exports/decode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const MSB = 0x80;
const REST = 0x7F;
const decode = (buf, offset) => {
offset = offset || 0;
const l = buf.length;
let counter = offset;
let result = 0;
let shift = 0;
let b;
do {
if (counter >= l || shift > 49) {
decode.bytes = 0;
throw new RangeError('Could not decode varint');
}
b = buf[counter++];
result += shift < 28
? (b & REST) << shift
: (b & REST) * Math.pow(2, shift);
shift += 7;
} while (b >= MSB);
decode.bytes = counter - offset;
return result;
};

export { decode as default };
26 changes: 26 additions & 0 deletions exports/encode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const MSB = 0x80;
const REST = 0x7F;
const MSBALL = ~REST;
const INT = Math.pow(2, 31);
const encode = (num, out, offset) => {
if (Number.MAX_SAFE_INTEGER && num > Number.MAX_SAFE_INTEGER) {
encode.bytes = 0;
throw new RangeError('Could not encode varint');
}
out = out || [];
offset = offset || 0;
const oldOffset = offset;
while (num >= INT) {
out[offset++] = (num & 0xFF) | MSB;
num /= 128;
}
while (num & MSBALL) {
out[offset++] = (num & 0xFF) | MSB;
num >>>= 7;
}
out[offset] = num | 0;
encode.bytes = offset - oldOffset + 1;
return out;
};

export { encode as default };
11 changes: 11 additions & 0 deletions exports/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import encode from './encode.js';
import decode from './decode.js';
import encodingLength from './length.js';

var index = {
encode,
decode,
encodingLength
};

export { index as default };
21 changes: 21 additions & 0 deletions exports/length.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const N1 = Math.pow(2, 7);
const N2 = Math.pow(2, 14);
const N3 = Math.pow(2, 21);
const N4 = Math.pow(2, 28);
const N5 = Math.pow(2, 35);
const N6 = Math.pow(2, 42);
const N7 = Math.pow(2, 49);
const N8 = Math.pow(2, 56);
const N9 = Math.pow(2, 63);
var encodingLength = (value) => (value < N1 ? 1
: value < N2 ? 2
: value < N3 ? 3
: value < N4 ? 4
: value < N5 ? 5
: value < N6 ? 6
: value < N7 ? 7
: value < N8 ? 8
: value < N9 ? 9
: 10);

export { encodingLength as default };
5 changes: 0 additions & 5 deletions index.js

This file was deleted.

25 changes: 0 additions & 25 deletions length.js

This file was deleted.

Loading