Skip to content

Commit

Permalink
protobufjs#66 - BitSet support
Browse files Browse the repository at this point in the history
  • Loading branch information
mhseiden committed Jan 24, 2016
1 parent a7a250d commit cd6011c
Show file tree
Hide file tree
Showing 11 changed files with 1,344 additions and 936 deletions.
709 changes: 402 additions & 307 deletions dist/bytebuffer-dataview.js

Large diffs are not rendered by default.

72 changes: 37 additions & 35 deletions dist/bytebuffer-dataview.min.js

Large diffs are not rendered by default.

Binary file modified dist/bytebuffer-dataview.min.js.gz
Binary file not shown.
6 changes: 3 additions & 3 deletions dist/bytebuffer-dataview.min.map

Large diffs are not rendered by default.

487 changes: 291 additions & 196 deletions dist/bytebuffer-node.js

Large diffs are not rendered by default.

709 changes: 402 additions & 307 deletions dist/bytebuffer.js

Large diffs are not rendered by default.

171 changes: 87 additions & 84 deletions dist/bytebuffer.min.js

Large diffs are not rendered by default.

Binary file modified dist/bytebuffer.min.js.gz
Binary file not shown.
6 changes: 3 additions & 3 deletions dist/bytebuffer.min.map

Large diffs are not rendered by default.

89 changes: 89 additions & 0 deletions src/types/bytes/bitset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* Writes the array as a bitset.
* @param {Array<boolean>} value Array of booleans to write
* @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `length` if omitted.
* @returns {!ByteBuffer}
* @expose
*/
ByteBufferPrototype.writeBitSet = function(value, offset) {
//? RELATIVE()
if (!this.noAssert) {
if (!(value instanceof Array))
throw TypeError("Illegal BitSet: Not an array");
//? ASSERT_OFFSET();
}

var start = offset,
bits = value.length,
bytes = (bits >> 3),
bit = 0,
k;

offset += this.writeVarint32(bits,offset);

while(bytes--) {
k = (!!value[bit++] & 1) |
((!!value[bit++] & 1) << 1) |
((!!value[bit++] & 1) << 2) |
((!!value[bit++] & 1) << 3) |
((!!value[bit++] & 1) << 4) |
((!!value[bit++] & 1) << 5) |
((!!value[bit++] & 1) << 6) |
((!!value[bit++] & 1) << 7);
this.writeByte(k,offset++);
}

if(bit < bits) {
var m = 0; k = 0;
while(bit < bits) k = k | ((!!value[bit++] & 1) << (m++));
this.writeByte(k,offset++);
}

if (relative) {
this.offset = offset;
return this;
}
return offset - start;
}

/**
* Reads a BitSet as an array of booleans.
* @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `length` if omitted.
* @returns {Array<boolean>
* @expose
*/
ByteBufferPrototype.readBitSet = function(offset) {
//? RELATIVE()

var ret = this.readVarint32(offset),
bits = ret.value,
bytes = (bits >> 3),
bit = 0,
value = [],
k;

offset += ret.length;

while(bytes--) {
k = this.readByte(offset++);
value[bit++] = !!(k & 0x01);
value[bit++] = !!(k & 0x02);
value[bit++] = !!(k & 0x04);
value[bit++] = !!(k & 0x08);
value[bit++] = !!(k & 0x10);
value[bit++] = !!(k & 0x20);
value[bit++] = !!(k & 0x40);
value[bit++] = !!(k & 0x80);
}

if(bit < bits) {
var m = 0;
k = this.readByte(offset++);
while(bit < bits) value[bit++] = !!((k >> (m++)) & 1);
}

if (relative) {
this.offset = offset;
}
return value;
}
31 changes: 30 additions & 1 deletion tests/suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,35 @@ function makeSuite(ByteBuffer) {
};
});

suite.types.bitset = function(test) {
var bb = new ByteBuffer(2),
arr;

function run(data) {
bb.reset();
bb.writeBitSet(data);
bb.reset();
test.deepEqual(bb.readBitSet(),data);
};

run([]);
run([true]);
run([false]);
run([false,true]);
run([false,false,false,false,false,false,false,false]);
run([true,false,true,false,true,false,true,false]);
run([true,true,true,true,true,true,true,true]);
run([true,false,true,false,true,false,true,false]);
run([true,false,true,false,true,false,true,false,true]);

bb.reset();
bb.writeBitSet([,null,"",0,42,"hello world",new Date(0),{},[]]);
bb.reset();
test.deepEqual(bb.readBitSet(),[false,false,false,false,true,true,true,true,true]);

test.done();
};

suite.types.calculateVarint = function(test) {
test.equal(ByteBuffer.MAX_VARINT32_BYTES, 5);
test.equal(ByteBuffer.MAX_VARINT64_BYTES, 10);
Expand Down Expand Up @@ -973,4 +1002,4 @@ module.exports = {
"node": makeSuite(ByteBufferNode),
"browser": makeSuite(ByteBufferBrowser),
"dataview": makeSuite(ByteBufferBrowser_DataView)
};
};

0 comments on commit cd6011c

Please sign in to comment.