Skip to content

Commit

Permalink
Merge pull request #391 from uProxy/trevj-arraybuffers-use-buffer-whe…
Browse files Browse the repository at this point in the history
…re-possible

delegate a bunch of arraybuffers functions to Buffer
  • Loading branch information
trevj committed Apr 15, 2016
2 parents 9748923 + 9a9a8e3 commit afce285
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 74 deletions.
38 changes: 1 addition & 37 deletions src/arraybuffers/arraybuffers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,49 +63,13 @@ describe('ArrayBuffers <-> Hex Strings', function() {
});
});

describe('ArrayBuffers concat & chunk', function() {
describe('ArrayBuffers chunk', function() {
it('chunk(array12, 6).length == 3', function() {
expect(arraybuffers.chunk(array12,6).length).toBe(3);
});
it('chunk(array12, 1).length == 15', function() {
expect(arraybuffers.chunk(array12,1).length).toBe(15);
});
it('concat(array1,array2) == array12', function() {
expect(arraybuffers.byteEquality(
arraybuffers.concat([array1, array2]),
array12))
.toBe(true);
});
it('concat(chunk(array12, 1)) == array12', function() {
expect(arraybuffers.byteEquality(
arraybuffers.concat(arraybuffers.chunk(array12,1)),
array12))
.toBe(true);
});
it('concat(chunk(array12, 4)) == array12', function() {
expect(arraybuffers.byteEquality(
arraybuffers.concat(arraybuffers.chunk(array12,4)),
array12))
.toBe(true);
});
it('concat(chunk(array12, 5)) == array12', function() {
expect(arraybuffers.byteEquality(
arraybuffers.concat(arraybuffers.chunk(array12,5)),
array12))
.toBe(true);
});
it('concat(chunk(array12, array12.byteLength)) == array12', function() {
expect(arraybuffers.byteEquality(
arraybuffers.concat(arraybuffers.chunk(array12,array12.byteLength)),
array12))
.toBe(true);
});
it('concat(chunk(array12, 20)) == array12', function() {
expect(arraybuffers.byteEquality(
arraybuffers.concat(arraybuffers.chunk(array12,20)),
array12))
.toBe(true);
});
});

describe('ArrayBuffers <-> strings', function() {
Expand Down
51 changes: 14 additions & 37 deletions src/arraybuffers/arraybuffers.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,17 @@
/// <reference path='../../../third_party/typings/browser.d.ts' />

// Byte-wise equality check of array buffers by comparison of each byte's
// value.
export function byteEquality(b1 :ArrayBuffer, b2 :ArrayBuffer)
:boolean {
var a1 = new Uint8Array(b1);
var a2 = new Uint8Array(b2);
if(a1.byteLength !== a2.byteLength) return false;
for(var i:number = 0; i < a1.byteLength; ++i) {
if(a1[i] !== a2[i]) return false;
}
return true;
// Returns true if b1 and b2 have exactly the same bytes.
export function byteEquality(b1: ArrayBuffer, b2: ArrayBuffer): boolean {
// The Buffer instances share memory with their source ArrayBuffers.
return new Buffer(b1).equals(new Buffer(b2));
}

// Concat |ArrayBuffer|s into a single ArrayBuffer. If size is given, then
// the destination array buffer is of the given size. If size is not given or
// zero, the size of all buffers is summed to make the new array buffer.
export function concat(buffers:ArrayBuffer[], size?:number)
:ArrayBuffer {
if(!size) {
size = 0;
buffers.forEach(a => { size += a.byteLength });
}
var accumulatorBuffer = new Uint8Array(size);
var location = 0;
buffers.forEach(a => {
accumulatorBuffer.set(new Uint8Array(a), location);
location += a.byteLength;
});
return accumulatorBuffer.buffer;
// Returns a new ArrayBuffer which is the result of concatenating all the
// supplied ArrayBuffers together. If size is supplied, the resulting
// ArrayBuffer will be of the given size.
export function concat(arrayBuffers: ArrayBuffer[], size?: number): ArrayBuffer {
// The Buffer instances share memory with their source ArrayBuffers.
return Buffer.concat(arrayBuffers.map(ab => new Buffer(ab)), size).buffer;
}

// Break an array buffer into multiple array buffers that are at most |size|
Expand Down Expand Up @@ -168,14 +151,8 @@ export function parse(buffer:ArrayBuffer, lengths:number[]) :ArrayBuffer[] {
return parts;
}

// Finds the index of a character in an ArrayBuffer
export function indexOf(ab :ArrayBuffer, char :number) :number {
let bytes = new Uint8Array(ab);
for(let i = 0; i < bytes.length; ++i) {
if (bytes[i]==char) {
return i;
}
}

return -1;
// Returns the index of the first appearance of i in ab, or -1 if not found.
export function indexOf(ab: ArrayBuffer, i: number): number {
// The Buffer instance shares memory with the source ArrayBuffer.
return new Buffer(ab).indexOf(i);
}

0 comments on commit afce285

Please sign in to comment.