add readVarIntBuf function
...will be useful for new Varint class
This commit is contained in:
parent
3c668c9cf0
commit
d363956ba1
@ -98,6 +98,20 @@ BufferReader.prototype.readVarIntNum = function() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
BufferReader.prototype.readVarIntBuf = function() {
|
||||||
|
var first = this.buf.readUInt8(this.pos);
|
||||||
|
switch (first) {
|
||||||
|
case 0xFD:
|
||||||
|
return this.buffer(1 + 2);;
|
||||||
|
case 0xFE:
|
||||||
|
return this.buffer(1 + 4);
|
||||||
|
case 0xFF:
|
||||||
|
return this.buffer(1 + 8);
|
||||||
|
default:
|
||||||
|
return this.buffer(1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
BufferReader.prototype.readVarIntBN = function() {
|
BufferReader.prototype.readVarIntBN = function() {
|
||||||
var first = this.readUInt8();
|
var first = this.readUInt8();
|
||||||
switch (first) {
|
switch (first) {
|
||||||
|
|||||||
@ -168,6 +168,35 @@ describe('BufferReader', function() {
|
|||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('#readVarIntBuf', function() {
|
||||||
|
|
||||||
|
it('should read a 1 byte varint', function() {
|
||||||
|
var buf = new Buffer([50]);
|
||||||
|
var br = new BufferReader({buf: buf});
|
||||||
|
br.readVarIntBuf().length.should.equal(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should read a 3 byte varint', function() {
|
||||||
|
var buf = new Buffer([253, 253, 0]);
|
||||||
|
var br = new BufferReader({buf: buf});
|
||||||
|
br.readVarIntBuf().length.should.equal(3);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should read a 5 byte varint', function() {
|
||||||
|
var buf = new Buffer([254, 0, 0, 0, 0]);
|
||||||
|
buf.writeUInt32LE(50000, 1);
|
||||||
|
var br = new BufferReader({buf: buf});
|
||||||
|
br.readVarIntBuf().length.should.equal(5);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should read a 9 byte varint', function() {
|
||||||
|
var buf = BufferWriter().writeVarIntBN(BN(Math.pow(2, 54).toString())).concat();
|
||||||
|
var br = new BufferReader({buf: buf});
|
||||||
|
br.readVarIntBuf().length.should.equal(9);
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
describe('#readVarIntNum', function() {
|
describe('#readVarIntNum', function() {
|
||||||
|
|
||||||
it('should read a 1 byte varint', function() {
|
it('should read a 1 byte varint', function() {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user