Skip to content

Commit afce285

Browse files
committed
Merge pull request #391 from uProxy/trevj-arraybuffers-use-buffer-where-possible
delegate a bunch of arraybuffers functions to Buffer
2 parents 9748923 + 9a9a8e3 commit afce285

File tree

2 files changed

+15
-74
lines changed

2 files changed

+15
-74
lines changed

src/arraybuffers/arraybuffers.spec.ts

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -63,49 +63,13 @@ describe('ArrayBuffers <-> Hex Strings', function() {
6363
});
6464
});
6565

66-
describe('ArrayBuffers concat & chunk', function() {
66+
describe('ArrayBuffers chunk', function() {
6767
it('chunk(array12, 6).length == 3', function() {
6868
expect(arraybuffers.chunk(array12,6).length).toBe(3);
6969
});
7070
it('chunk(array12, 1).length == 15', function() {
7171
expect(arraybuffers.chunk(array12,1).length).toBe(15);
7272
});
73-
it('concat(array1,array2) == array12', function() {
74-
expect(arraybuffers.byteEquality(
75-
arraybuffers.concat([array1, array2]),
76-
array12))
77-
.toBe(true);
78-
});
79-
it('concat(chunk(array12, 1)) == array12', function() {
80-
expect(arraybuffers.byteEquality(
81-
arraybuffers.concat(arraybuffers.chunk(array12,1)),
82-
array12))
83-
.toBe(true);
84-
});
85-
it('concat(chunk(array12, 4)) == array12', function() {
86-
expect(arraybuffers.byteEquality(
87-
arraybuffers.concat(arraybuffers.chunk(array12,4)),
88-
array12))
89-
.toBe(true);
90-
});
91-
it('concat(chunk(array12, 5)) == array12', function() {
92-
expect(arraybuffers.byteEquality(
93-
arraybuffers.concat(arraybuffers.chunk(array12,5)),
94-
array12))
95-
.toBe(true);
96-
});
97-
it('concat(chunk(array12, array12.byteLength)) == array12', function() {
98-
expect(arraybuffers.byteEquality(
99-
arraybuffers.concat(arraybuffers.chunk(array12,array12.byteLength)),
100-
array12))
101-
.toBe(true);
102-
});
103-
it('concat(chunk(array12, 20)) == array12', function() {
104-
expect(arraybuffers.byteEquality(
105-
arraybuffers.concat(arraybuffers.chunk(array12,20)),
106-
array12))
107-
.toBe(true);
108-
});
10973
});
11074

11175
describe('ArrayBuffers <-> strings', function() {

src/arraybuffers/arraybuffers.ts

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,17 @@
11
/// <reference path='../../../third_party/typings/browser.d.ts' />
22

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

16-
// Concat |ArrayBuffer|s into a single ArrayBuffer. If size is given, then
17-
// the destination array buffer is of the given size. If size is not given or
18-
// zero, the size of all buffers is summed to make the new array buffer.
19-
export function concat(buffers:ArrayBuffer[], size?:number)
20-
:ArrayBuffer {
21-
if(!size) {
22-
size = 0;
23-
buffers.forEach(a => { size += a.byteLength });
24-
}
25-
var accumulatorBuffer = new Uint8Array(size);
26-
var location = 0;
27-
buffers.forEach(a => {
28-
accumulatorBuffer.set(new Uint8Array(a), location);
29-
location += a.byteLength;
30-
});
31-
return accumulatorBuffer.buffer;
9+
// Returns a new ArrayBuffer which is the result of concatenating all the
10+
// supplied ArrayBuffers together. If size is supplied, the resulting
11+
// ArrayBuffer will be of the given size.
12+
export function concat(arrayBuffers: ArrayBuffer[], size?: number): ArrayBuffer {
13+
// The Buffer instances share memory with their source ArrayBuffers.
14+
return Buffer.concat(arrayBuffers.map(ab => new Buffer(ab)), size).buffer;
3215
}
3316

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

171-
// Finds the index of a character in an ArrayBuffer
172-
export function indexOf(ab :ArrayBuffer, char :number) :number {
173-
let bytes = new Uint8Array(ab);
174-
for(let i = 0; i < bytes.length; ++i) {
175-
if (bytes[i]==char) {
176-
return i;
177-
}
178-
}
179-
180-
return -1;
154+
// Returns the index of the first appearance of i in ab, or -1 if not found.
155+
export function indexOf(ab: ArrayBuffer, i: number): number {
156+
// The Buffer instance shares memory with the source ArrayBuffer.
157+
return new Buffer(ab).indexOf(i);
181158
}

0 commit comments

Comments
 (0)