add readVarIntBuf function

...will be useful for new Varint class
This commit is contained in:
Ryan X. Charles 2014-09-15 14:27:42 -07:00
parent 3c668c9cf0
commit d363956ba1
2 changed files with 43 additions and 0 deletions

View File

@ -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() {
var first = this.readUInt8();
switch (first) {

View File

@ -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() {
it('should read a 1 byte varint', function() {