Browse Source

add readVarIntBuf function

...will be useful for new Varint class
patch-2
Ryan X. Charles 10 years ago
parent
commit
d363956ba1
  1. 14
      lib/bufferreader.js
  2. 29
      test/bufferreader.js

14
lib/bufferreader.js

@ -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) {

29
test/bufferreader.js

@ -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() {

Loading…
Cancel
Save