forked from fdzcxy212206413/gsl_grs
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
595 B
31 lines
595 B
1 month ago
|
module.exports = encode
|
||
|
|
||
|
var MSB = 0x80
|
||
|
, REST = 0x7F
|
||
|
, MSBALL = ~REST
|
||
|
, INT = Math.pow(2, 31)
|
||
|
|
||
|
function 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
|
||
|
var 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
|
||
|
}
|