Skip to content

Commit

Permalink
Remove Buffer usage from (un)packLongBytes
Browse files Browse the repository at this point in the history
  • Loading branch information
valadaptive committed Mar 30, 2024
1 parent 004d076 commit 714d3a2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
2 changes: 1 addition & 1 deletion doc
Submodule doc updated from bebd04 to b36b2f
14 changes: 7 additions & 7 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function capitalize(s) { return s.charAt(0).toUpperCase() + s.slice(1); }
function compare(n1, n2) { return n1 === n2 ? 0 : (n1 < n2 ? -1 : 1); }

let bufCompare;
if (typeof Buffer.prototype.compare == 'function') {
if (typeof Buffer == 'function') {
bufCompare = Buffer.compare;
} else {
bufCompare = function(buf1, buf2) {
Expand Down Expand Up @@ -928,7 +928,7 @@ class Tap {
// worry about Avro's zigzag encoding, we directly expose longs as unpacked.

unpackLongBytes () {
let res = Buffer.alloc(8);
let res = new Uint8Array(8);
let n = 0;
let i = 0; // Byte index in target buffer.
let j = 6; // Bit offset in current target buffer byte.
Expand Down Expand Up @@ -975,9 +975,9 @@ class Tap {
}

let parts = [
buf.readUIntLE(0, 3),
buf.readUIntLE(3, 3),
buf.readUIntLE(6, 2)
(buf[0] | (buf[1] << 8) | (buf[2] << 16)),
(buf[3] | (buf[4] << 8) | (buf[5] << 16)),
(buf[6] | (buf[7] << 8))
];
// Not reading more than 24 bits because we need to be able to combine the
// "carry" bits from the previous part and JavaScript only supports bitwise
Expand Down Expand Up @@ -1016,8 +1016,8 @@ class Tap {
/**
* Invert all bits in a buffer.
*
* @param buf {Buffer} Non-empty buffer to invert.
* @param len {Number} Buffer length (must be positive).
* @param {Uint8Array} buf Non-empty buffer to invert.
* @param {number} len Buffer length (must be positive).
*/
function invert(buf, len) {
while (len--) {
Expand Down
10 changes: 6 additions & 4 deletions test/test_types.js
Original file line number Diff line number Diff line change
Expand Up @@ -2474,27 +2474,29 @@ suite('types', () => {

let slowLongType = builtins.LongType.__with({
fromBuffer: function (buf) {
let dv = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
let neg = buf[7] >> 7;
if (neg) { // Negative number.
invert(buf);
}
let n = buf.readInt32LE(0) + Math.pow(2, 32) * buf.readInt32LE(4);
let n = dv.getInt32(0, true) + Math.pow(2, 32) * dv.getInt32(4, true);
if (neg) {
invert(buf);
n = -n - 1;
}
return n;
},
toBuffer: function (n) {
let buf = Buffer.alloc(8);
let buf = new Uint8Array(8);
let dv = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
let neg = n < 0;
if (neg) {
invert(buf);
n = -n - 1;
}
buf.writeInt32LE(n | 0);
dv.setInt32(0, n | 0, true);
let h = n / Math.pow(2, 32) | 0;
buf.writeInt32LE(h ? h : (n >= 0 ? 0 : -1), 4);
dv.setInt32(4, h ? h : (n >= 0 ? 0 : -1), true);
if (neg) {
invert(buf);
}
Expand Down
16 changes: 13 additions & 3 deletions test/test_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,16 +263,26 @@ suite('utils', () => {

test('unpack multiple bytes', () => {
let t = Tap.withCapacity(10);
let l;
let l, unpacked, dv;
l = 18932;
t.writeLong(l);
t.pos = 0;
assert.deepEqual(t.unpackLongBytes().readInt32LE(0), l);
unpacked = t.unpackLongBytes();
dv = new DataView(
unpacked.buffer,
unpacked.byteOffset,
unpacked.byteLength);
assert.deepEqual(dv.getInt32(0, true), l);
t.pos = 0;
l = -3210984;
t.writeLong(l);
t.pos = 0;
assert.deepEqual(t.unpackLongBytes().readInt32LE(0), l);
unpacked = t.unpackLongBytes();
dv = new DataView(
unpacked.buffer,
unpacked.byteOffset,
unpacked.byteLength);
assert.deepEqual(dv.getInt32(0, true), l);
});

test('pack single byte', () => {
Expand Down

0 comments on commit 714d3a2

Please sign in to comment.