uint8arraylist 1.0.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -0
- package/dist/src/index.d.ts +31 -4
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +91 -19
- package/dist/src/index.js.map +1 -1
- package/package.json +3 -2
- package/src/index.ts +111 -23
package/README.md
CHANGED
|
@@ -23,6 +23,14 @@ list.toUint8Array()
|
|
|
23
23
|
list.consume(3)
|
|
24
24
|
list.toUint8Array()
|
|
25
25
|
// -> Uint8Array([3, 4, 5])
|
|
26
|
+
|
|
27
|
+
// you can also iterate over the list
|
|
28
|
+
for (const buf of list) {
|
|
29
|
+
// ..do something with `buf`
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
list.slice(0, 1)
|
|
33
|
+
// -> Uint8ArrayList([0])
|
|
26
34
|
```
|
|
27
35
|
|
|
28
36
|
## Inspiration
|
package/dist/src/index.d.ts
CHANGED
|
@@ -1,13 +1,40 @@
|
|
|
1
|
-
|
|
1
|
+
declare type Appendable = Uint8ArrayList | Uint8Array;
|
|
2
|
+
export declare class Uint8ArrayList implements Iterable<Uint8Array> {
|
|
2
3
|
private bufs;
|
|
3
4
|
length: number;
|
|
4
|
-
constructor(data
|
|
5
|
-
|
|
5
|
+
constructor(...data: Appendable[]);
|
|
6
|
+
[Symbol.iterator](): Generator<Uint8Array, void, undefined>;
|
|
7
|
+
get byteLength(): number;
|
|
8
|
+
/**
|
|
9
|
+
* Add one or more `bufs` to this Uint8ArrayList
|
|
10
|
+
*/
|
|
11
|
+
append(...bufs: Appendable[]): void;
|
|
12
|
+
/**
|
|
13
|
+
* Add all `bufs` to this Uint8ArrayList
|
|
14
|
+
*/
|
|
15
|
+
appendAll(bufs: Appendable[]): void;
|
|
16
|
+
/**
|
|
17
|
+
* Read the value at `index`
|
|
18
|
+
*/
|
|
6
19
|
get(index: number): number;
|
|
20
|
+
/**
|
|
21
|
+
* Set the value at `index` to `value`
|
|
22
|
+
*/
|
|
23
|
+
set(index: number, value: number): void;
|
|
24
|
+
/**
|
|
25
|
+
* Copy bytes from `buf` to the index specified by `offset`
|
|
26
|
+
*/
|
|
27
|
+
write(buf: Appendable, offset?: number): void;
|
|
7
28
|
/**
|
|
8
29
|
* Remove bytes from the front of the pool
|
|
9
30
|
*/
|
|
10
31
|
consume(bytes: number): void;
|
|
11
|
-
|
|
32
|
+
slice(beginInclusive?: number, endExclusive?: number): Uint8Array;
|
|
33
|
+
subarray(beginInclusive?: number, endExclusive?: number): Uint8ArrayList;
|
|
34
|
+
_subList(beginInclusive?: number, endExclusive?: number): {
|
|
35
|
+
bufs: Uint8Array[];
|
|
36
|
+
length: number;
|
|
37
|
+
};
|
|
12
38
|
}
|
|
39
|
+
export {};
|
|
13
40
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/src/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAEA,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAEA,aAAK,UAAU,GAAG,cAAc,GAAG,UAAU,CAAA;AAyB7C,qBAAa,cAAe,YAAW,QAAQ,CAAC,UAAU,CAAC;IACzD,OAAO,CAAC,IAAI,CAAc;IACnB,MAAM,EAAE,MAAM,CAAA;gBAER,GAAG,IAAI,EAAE,UAAU,EAAE;IAOhC,CAAC,MAAM,CAAC,QAAQ,CAAC;IAInB,IAAI,UAAU,WAEb;IAED;;OAEG;IACH,MAAM,CAAE,GAAG,IAAI,EAAE,UAAU,EAAE;IAI7B;;OAEG;IACH,SAAS,CAAE,IAAI,EAAE,UAAU,EAAE;IAgB7B;;OAEG;IACH,GAAG,CAAE,KAAK,EAAE,MAAM;IAMlB;;OAEG;IACH,GAAG,CAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM;IAMjC;;OAEG;IACH,KAAK,CAAE,GAAG,EAAE,UAAU,EAAE,MAAM,GAAE,MAAU;IAY1C;;OAEG;IACH,OAAO,CAAE,KAAK,EAAE,MAAM;IAsBtB,KAAK,CAAE,cAAc,CAAC,EAAE,MAAM,EAAE,YAAY,CAAC,EAAE,MAAM;IAMrD,QAAQ,CAAE,cAAc,CAAC,EAAE,MAAM,EAAE,YAAY,CAAC,EAAE,MAAM;IASxD,QAAQ,CAAE,cAAc,CAAC,EAAE,MAAM,EAAE,YAAY,CAAC,EAAE,MAAM;;;;CA2DzD"}
|
package/dist/src/index.js
CHANGED
|
@@ -1,29 +1,84 @@
|
|
|
1
1
|
import { concat } from 'uint8arrays';
|
|
2
|
+
function findBufAndOffset(bufs, index, totalLength) {
|
|
3
|
+
if (index == null || index < 0 || index >= totalLength) {
|
|
4
|
+
throw new RangeError('index is out of bounds');
|
|
5
|
+
}
|
|
6
|
+
let offset = 0;
|
|
7
|
+
for (const buf of bufs) {
|
|
8
|
+
const bufEnd = offset + buf.byteLength;
|
|
9
|
+
if (index < bufEnd) {
|
|
10
|
+
return {
|
|
11
|
+
buf,
|
|
12
|
+
index: index - offset
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
offset = bufEnd;
|
|
16
|
+
}
|
|
17
|
+
throw new RangeError('index is out of bounds');
|
|
18
|
+
}
|
|
2
19
|
export class Uint8ArrayList {
|
|
3
|
-
constructor(data) {
|
|
20
|
+
constructor(...data) {
|
|
4
21
|
this.bufs = [];
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
22
|
+
this.length = 0;
|
|
23
|
+
this.appendAll(data);
|
|
24
|
+
}
|
|
25
|
+
*[Symbol.iterator]() {
|
|
26
|
+
yield* this.bufs;
|
|
9
27
|
}
|
|
10
|
-
|
|
11
|
-
this.
|
|
12
|
-
|
|
28
|
+
get byteLength() {
|
|
29
|
+
return this.length;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Add one or more `bufs` to this Uint8ArrayList
|
|
33
|
+
*/
|
|
34
|
+
append(...bufs) {
|
|
35
|
+
this.appendAll(bufs);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Add all `bufs` to this Uint8ArrayList
|
|
39
|
+
*/
|
|
40
|
+
appendAll(bufs) {
|
|
41
|
+
let length = 0;
|
|
42
|
+
for (const buf of bufs) {
|
|
43
|
+
if (buf instanceof Uint8Array) {
|
|
44
|
+
length += buf.byteLength;
|
|
45
|
+
this.bufs.push(buf);
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
length += buf.length;
|
|
49
|
+
this.bufs = this.bufs.concat(buf.bufs);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
this.length += length;
|
|
13
53
|
}
|
|
54
|
+
/**
|
|
55
|
+
* Read the value at `index`
|
|
56
|
+
*/
|
|
14
57
|
get(index) {
|
|
15
|
-
|
|
16
|
-
|
|
58
|
+
const res = findBufAndOffset(this.bufs, index, this.length);
|
|
59
|
+
return res.buf[res.index];
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Set the value at `index` to `value`
|
|
63
|
+
*/
|
|
64
|
+
set(index, value) {
|
|
65
|
+
const res = findBufAndOffset(this.bufs, index, this.length);
|
|
66
|
+
res.buf[res.index] = value;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Copy bytes from `buf` to the index specified by `offset`
|
|
70
|
+
*/
|
|
71
|
+
write(buf, offset = 0) {
|
|
72
|
+
if (buf instanceof Uint8Array) {
|
|
73
|
+
for (let i = 0; i < buf.length; i++) {
|
|
74
|
+
this.set(offset + i, buf[i]);
|
|
75
|
+
}
|
|
17
76
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
if (index < bufEnd) {
|
|
22
|
-
return buf[index - offset];
|
|
77
|
+
else {
|
|
78
|
+
for (let i = 0; i < buf.length; i++) {
|
|
79
|
+
this.set(offset + i, buf.get(i));
|
|
23
80
|
}
|
|
24
|
-
offset = bufEnd;
|
|
25
81
|
}
|
|
26
|
-
throw new RangeError('index is out of bounds');
|
|
27
82
|
}
|
|
28
83
|
/**
|
|
29
84
|
* Remove bytes from the front of the pool
|
|
@@ -48,11 +103,28 @@ export class Uint8ArrayList {
|
|
|
48
103
|
}
|
|
49
104
|
}
|
|
50
105
|
}
|
|
51
|
-
|
|
106
|
+
slice(beginInclusive, endExclusive) {
|
|
107
|
+
const { bufs, length } = this._subList(beginInclusive, endExclusive);
|
|
108
|
+
return concat(bufs, length);
|
|
109
|
+
}
|
|
110
|
+
subarray(beginInclusive, endExclusive) {
|
|
111
|
+
const { bufs } = this._subList(beginInclusive, endExclusive);
|
|
112
|
+
const list = new Uint8ArrayList();
|
|
113
|
+
list.appendAll(bufs);
|
|
114
|
+
return list;
|
|
115
|
+
}
|
|
116
|
+
_subList(beginInclusive, endExclusive) {
|
|
117
|
+
if (beginInclusive == null && endExclusive == null) {
|
|
118
|
+
return { bufs: this.bufs, length: this.length };
|
|
119
|
+
}
|
|
120
|
+
beginInclusive = beginInclusive ?? 0;
|
|
52
121
|
endExclusive = endExclusive ?? (this.length > 0 ? this.length : 0);
|
|
53
122
|
if (beginInclusive < 0 || endExclusive > this.length) {
|
|
54
123
|
throw new RangeError('index out of bounds');
|
|
55
124
|
}
|
|
125
|
+
if (beginInclusive === endExclusive) {
|
|
126
|
+
return { bufs: [], length: 0 };
|
|
127
|
+
}
|
|
56
128
|
const bufs = [];
|
|
57
129
|
let offset = 0;
|
|
58
130
|
for (const buf of this.bufs) {
|
|
@@ -85,7 +157,7 @@ export class Uint8ArrayList {
|
|
|
85
157
|
break;
|
|
86
158
|
}
|
|
87
159
|
}
|
|
88
|
-
return
|
|
160
|
+
return { bufs, length: endExclusive - beginInclusive };
|
|
89
161
|
}
|
|
90
162
|
}
|
|
91
163
|
//# sourceMappingURL=index.js.map
|
package/dist/src/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AAIpC,SAAS,gBAAgB,CAAE,IAAkB,EAAE,KAAa,EAAE,WAAmB;IAC/E,IAAI,KAAK,IAAI,IAAI,IAAI,KAAK,GAAG,CAAC,IAAI,KAAK,IAAI,WAAW,EAAE;QACtD,MAAM,IAAI,UAAU,CAAC,wBAAwB,CAAC,CAAA;KAC/C;IAED,IAAI,MAAM,GAAG,CAAC,CAAA;IAEd,KAAK,MAAM,GAAG,IAAI,IAAI,EAAE;QACtB,MAAM,MAAM,GAAG,MAAM,GAAG,GAAG,CAAC,UAAU,CAAA;QAEtC,IAAI,KAAK,GAAG,MAAM,EAAE;YAClB,OAAO;gBACL,GAAG;gBACH,KAAK,EAAE,KAAK,GAAG,MAAM;aACtB,CAAA;SACF;QAED,MAAM,GAAG,MAAM,CAAA;KAChB;IAED,MAAM,IAAI,UAAU,CAAC,wBAAwB,CAAC,CAAA;AAChD,CAAC;AAED,MAAM,OAAO,cAAc;IAIzB,YAAa,GAAG,IAAkB;QAChC,IAAI,CAAC,IAAI,GAAG,EAAE,CAAA;QACd,IAAI,CAAC,MAAM,GAAG,CAAC,CAAA;QAEf,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAA;IACtB,CAAC;IAED,CAAE,CAAC,MAAM,CAAC,QAAQ,CAAC;QACjB,KAAM,CAAC,CAAC,IAAI,CAAC,IAAI,CAAA;IACnB,CAAC;IAED,IAAI,UAAU;QACZ,OAAO,IAAI,CAAC,MAAM,CAAA;IACpB,CAAC;IAED;;OAEG;IACH,MAAM,CAAE,GAAG,IAAkB;QAC3B,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAA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AY,IAAI,MAAM,CAAA;YACxE,MAAM,UAAU,GAAG,cAAc,GAAG,QAAQ,IAAI,YAAY,IAAI,MAAM,CAAA;YACtE,MAAM,GAAG,MAAM,CAAA;YAEf,IAAI,UAA8B,CAAA;YAClC,IAAI,QAA4B,CAAA;YAEhC,IAAI,eAAe,EAAE;gBACnB,UAAU,GAAG,cAAc,GAAG,QAAQ,CAAA;gBACtC,QAAQ,GAAG,GAAG,CAAC,UAAU,CAAA;aAC1B;YAED,IAAI,cAAc,EAAE;gBAClB,QAAQ,GAAG,YAAY,GAAG,QAAQ,CAAA;gBAElC,IAAI,UAAU,IAAI,IAAI,EAAE;oBACtB,UAAU,GAAG,CAAC,CAAA;iBACf;aACF;YAED,IAAI,UAAU,EAAE;gBACd,UAAU,GAAG,CAAC,CAAA;gBACd,QAAQ,GAAG,GAAG,CAAC,UAAU,CAAA;aAC1B;YAED,IAAI,UAAU,IAAI,IAAI,IAAI,QAAQ,IAAI,IAAI,EAAE;gBAC1C,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,UAAU,EAAE,QAAQ,CAAC,CAAC,CAAA;aAC9C;YAED,IAAI,cAAc,EAAE;gBAClB,MAAK;aACN;SACF;QAED,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,YAAY,GAAG,cAAc,EAAE,CAAA;IACxD,CAAC;CACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "uint8arraylist",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "Append and consume bytes using only no-copy operations",
|
|
5
5
|
"author": "Alex Potsides <alex@achingbrain.net>",
|
|
6
6
|
"license": "Apache-2.0 OR MIT",
|
|
@@ -134,6 +134,7 @@
|
|
|
134
134
|
"uint8arrays": "^3.0.0"
|
|
135
135
|
},
|
|
136
136
|
"devDependencies": {
|
|
137
|
-
"aegir": "^36.1.3"
|
|
137
|
+
"aegir": "^36.1.3",
|
|
138
|
+
"it-all": "^1.0.6"
|
|
138
139
|
}
|
|
139
140
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,42 +1,106 @@
|
|
|
1
1
|
import { concat } from 'uint8arrays'
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
type Appendable = Uint8ArrayList | Uint8Array
|
|
4
|
+
|
|
5
|
+
function findBufAndOffset (bufs: Uint8Array[], index: number, totalLength: number) {
|
|
6
|
+
if (index == null || index < 0 || index >= totalLength) {
|
|
7
|
+
throw new RangeError('index is out of bounds')
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
let offset = 0
|
|
11
|
+
|
|
12
|
+
for (const buf of bufs) {
|
|
13
|
+
const bufEnd = offset + buf.byteLength
|
|
14
|
+
|
|
15
|
+
if (index < bufEnd) {
|
|
16
|
+
return {
|
|
17
|
+
buf,
|
|
18
|
+
index: index - offset
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
offset = bufEnd
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
throw new RangeError('index is out of bounds')
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export class Uint8ArrayList implements Iterable<Uint8Array> {
|
|
4
29
|
private bufs: Uint8Array[]
|
|
5
30
|
public length: number
|
|
6
31
|
|
|
7
|
-
constructor (data
|
|
32
|
+
constructor (...data: Appendable[]) {
|
|
8
33
|
this.bufs = []
|
|
34
|
+
this.length = 0
|
|
9
35
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
36
|
+
this.appendAll(data)
|
|
37
|
+
}
|
|
13
38
|
|
|
14
|
-
|
|
39
|
+
* [Symbol.iterator] () {
|
|
40
|
+
yield * this.bufs
|
|
15
41
|
}
|
|
16
42
|
|
|
17
|
-
|
|
18
|
-
this.
|
|
19
|
-
this.length += data.byteLength
|
|
43
|
+
get byteLength () {
|
|
44
|
+
return this.length
|
|
20
45
|
}
|
|
21
46
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
47
|
+
/**
|
|
48
|
+
* Add one or more `bufs` to this Uint8ArrayList
|
|
49
|
+
*/
|
|
50
|
+
append (...bufs: Appendable[]) {
|
|
51
|
+
this.appendAll(bufs)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Add all `bufs` to this Uint8ArrayList
|
|
56
|
+
*/
|
|
57
|
+
appendAll (bufs: Appendable[]) {
|
|
58
|
+
let length = 0
|
|
59
|
+
|
|
60
|
+
for (const buf of bufs) {
|
|
61
|
+
if (buf instanceof Uint8Array) {
|
|
62
|
+
length += buf.byteLength
|
|
63
|
+
this.bufs.push(buf)
|
|
64
|
+
} else {
|
|
65
|
+
length += buf.length
|
|
66
|
+
this.bufs = this.bufs.concat(buf.bufs)
|
|
67
|
+
}
|
|
25
68
|
}
|
|
26
69
|
|
|
27
|
-
|
|
70
|
+
this.length += length
|
|
71
|
+
}
|
|
28
72
|
|
|
29
|
-
|
|
30
|
-
|
|
73
|
+
/**
|
|
74
|
+
* Read the value at `index`
|
|
75
|
+
*/
|
|
76
|
+
get (index: number) {
|
|
77
|
+
const res = findBufAndOffset(this.bufs, index, this.length)
|
|
31
78
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
}
|
|
79
|
+
return res.buf[res.index]
|
|
80
|
+
}
|
|
35
81
|
|
|
36
|
-
|
|
37
|
-
|
|
82
|
+
/**
|
|
83
|
+
* Set the value at `index` to `value`
|
|
84
|
+
*/
|
|
85
|
+
set (index: number, value: number) {
|
|
86
|
+
const res = findBufAndOffset(this.bufs, index, this.length)
|
|
38
87
|
|
|
39
|
-
|
|
88
|
+
res.buf[res.index] = value
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Copy bytes from `buf` to the index specified by `offset`
|
|
93
|
+
*/
|
|
94
|
+
write (buf: Appendable, offset: number = 0) {
|
|
95
|
+
if (buf instanceof Uint8Array) {
|
|
96
|
+
for (let i = 0; i < buf.length; i++) {
|
|
97
|
+
this.set(offset + i, buf[i])
|
|
98
|
+
}
|
|
99
|
+
} else {
|
|
100
|
+
for (let i = 0; i < buf.length; i++) {
|
|
101
|
+
this.set(offset + i, buf.get(i))
|
|
102
|
+
}
|
|
103
|
+
}
|
|
40
104
|
}
|
|
41
105
|
|
|
42
106
|
/**
|
|
@@ -64,13 +128,37 @@ export class Uint8ArrayList {
|
|
|
64
128
|
}
|
|
65
129
|
}
|
|
66
130
|
|
|
67
|
-
|
|
131
|
+
slice (beginInclusive?: number, endExclusive?: number) {
|
|
132
|
+
const { bufs, length } = this._subList(beginInclusive, endExclusive)
|
|
133
|
+
|
|
134
|
+
return concat(bufs, length)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
subarray (beginInclusive?: number, endExclusive?: number) {
|
|
138
|
+
const { bufs } = this._subList(beginInclusive, endExclusive)
|
|
139
|
+
|
|
140
|
+
const list = new Uint8ArrayList()
|
|
141
|
+
list.appendAll(bufs)
|
|
142
|
+
|
|
143
|
+
return list
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
_subList (beginInclusive?: number, endExclusive?: number) {
|
|
147
|
+
if (beginInclusive == null && endExclusive == null) {
|
|
148
|
+
return { bufs: this.bufs, length: this.length }
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
beginInclusive = beginInclusive ?? 0
|
|
68
152
|
endExclusive = endExclusive ?? (this.length > 0 ? this.length : 0)
|
|
69
153
|
|
|
70
154
|
if (beginInclusive < 0 || endExclusive > this.length) {
|
|
71
155
|
throw new RangeError('index out of bounds')
|
|
72
156
|
}
|
|
73
157
|
|
|
158
|
+
if (beginInclusive === endExclusive) {
|
|
159
|
+
return { bufs: [], length: 0 }
|
|
160
|
+
}
|
|
161
|
+
|
|
74
162
|
const bufs: Uint8Array[] = []
|
|
75
163
|
let offset = 0
|
|
76
164
|
|
|
@@ -112,6 +200,6 @@ export class Uint8ArrayList {
|
|
|
112
200
|
}
|
|
113
201
|
}
|
|
114
202
|
|
|
115
|
-
return
|
|
203
|
+
return { bufs, length: endExclusive - beginInclusive }
|
|
116
204
|
}
|
|
117
205
|
}
|