|
1 | 1 | /// <reference path='../../../third_party/typings/browser.d.ts' /> |
2 | 2 |
|
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)); |
14 | 7 | } |
15 | 8 |
|
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; |
32 | 15 | } |
33 | 16 |
|
34 | 17 | // 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[] { |
168 | 151 | return parts; |
169 | 152 | } |
170 | 153 |
|
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); |
181 | 158 | } |
0 commit comments