File size: 1,082 Bytes
b0bfea8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
 * indexed_array.js
 * purpose: exteds Uint8Array with a currentIndex property
 */

export class IndexedByteArray extends Uint8Array
{
    /**
     * The current index of the array
     * @type {number}
     */
    currentIndex = 0;
    
    /**
     * Creates a new instance of an Uint8Array with a currentIndex property
     * @param args {any} same as for Uint8Array
     */
    constructor(args)
    {
        super(args);
    }
    
    /**
     * @param start {number?}
     * @param end {number?}
     * @returns {IndexedByteArray}
     */
    slice(start, end)
    {
        const a = super.slice(start, end);
        a.currentIndex = 0;
        return a;
    }
}


/**
 * @param arrs {(IndexedByteArray|Uint8Array)[]}
 * @returns {IndexedByteArray|Uint8Array}
 */
export function combineArrays(arrs)
{
    const length = arrs.reduce((sum, current) => sum + current.length, 0);
    const newArr = new IndexedByteArray(length);
    let offset = 0;
    for (const arr of arrs)
    {
        newArr.set(arr, offset);
        offset += arr.length;
    }
    return newArr;
}