Debian Codegraph @

node/varint/encode.js

raw
1 module.exports = encode
2
3 var MSB = 0x80
4 , REST = 0x7F
5 , MSBALL = ~REST
6 , INT = Math.pow(2, 31)
7
8 function encode(num, out, offset) {
9 if (Number.MAX_SAFE_INTEGER && num > Number.MAX_SAFE_INTEGER) {
10 encode.bytes = 0
11 throw new RangeError('Could not encode varint')
12 }
13 out = out || []
14 offset = offset || 0
15 var oldOffset = offset
16
17 while(num >= INT) {
18 out[offset++] = (num & 0xFF) | MSB
19 num /= 128
20 }
21 while(num & MSBALL) {
22 out[offset++] = (num & 0xFF) | MSB
23 num >>>= 7
24 }
25 out[offset] = num | 0
26
27 encode.bytes = offset - oldOffset + 1
28
29 return out
30 }