Skip to content

Commit 04668b8

Browse files
authored
[Implement] subarray (#20)
1 parent 9027d73 commit 04668b8

3 files changed

Lines changed: 47 additions & 4 deletions

File tree

assembly/buffer/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ export class Buffer extends Uint8Array {
2727
return value instanceof Buffer;
2828
}
2929

30+
// Adapted from https://github.com/AssemblyScript/assemblyscript/blob/master/std/assembly/typedarray.ts
31+
public subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Buffer {
32+
var len = <i32>this.dataLength;
33+
begin = begin < 0 ? max(len + begin, 0) : min(begin, len);
34+
end = end < 0 ? max(len + end, 0) : min(end, len);
35+
end = max(end, begin);
36+
var out = changetype<Buffer>(__alloc(offsetof<Buffer>(), idof<Buffer>())); // retains
37+
out.data = this.data; // retains
38+
out.dataStart = this.dataStart + <usize>begin;
39+
out.dataLength = end - begin;
40+
return out;
41+
}
42+
3043
readInt8(offset: i32 = 0): i8 {
3144
if(i32(offset < 0) | i32(<u32>offset >= this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
3245
return load<i8>(this.dataStart + <usize>offset);

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/buffer.spec.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ describe("buffer", () => {
102102
// expectFn(() => {
103103
// let newBuff = new Buffer(1);
104104
// newBuff.writeInt8(5,10);
105-
// }).toThrow();
105+
// }).toThrow();
106106
});
107107

108108
test("#writeUInt8", () => {
@@ -115,8 +115,8 @@ describe("buffer", () => {
115115
// expectFn(() => {
116116
// let newBuff = new Buffer(1);
117117
// newBuff.writeUInt8(5,10);
118-
// }).toThrow();
119-
});
118+
// }).toThrow();
119+
});
120120

121121
test("#readInt16LE", () => {
122122
let buff = create<Buffer>([0x0,0x05,0x0]);
@@ -214,6 +214,36 @@ describe("buffer", () => {
214214
// }).toThrow();
215215
});
216216

217+
test("#subarray", () => {
218+
let example = create<Buffer>([1, 2, 3, 4, 5, 6, 7, 8]);
219+
220+
// no parameters means copy the Buffer
221+
let actual = example.subarray();
222+
expect<Buffer>(actual).toStrictEqual(example);
223+
expect<ArrayBuffer>(actual.buffer).toBe(example.buffer); // should use the same buffer
224+
225+
// start at offset 5
226+
actual = example.subarray(5);
227+
let expected = create<Buffer>([6, 7, 8]);
228+
// trace("length", 1, expected.length);
229+
expect<Buffer>(actual).toStrictEqual(expected);
230+
231+
// negative start indicies, start at (8 - 5)
232+
actual = example.subarray(-5);
233+
expected = create<Buffer>([4, 5, 6, 7, 8]);
234+
expect<Buffer>(actual).toStrictEqual(expected);
235+
236+
// two parameters
237+
actual = example.subarray(2, 6);
238+
expected = create<Buffer>([3, 4, 5, 6]);
239+
expect<Buffer>(actual).toStrictEqual(expected);
240+
241+
// negative end index
242+
actual = example.subarray(4, -1);
243+
expected = create<Buffer>([5, 6, 7]);
244+
expect<Buffer>(actual).toStrictEqual(expected);
245+
});
246+
217247
test("#Hex.encode", () => {
218248
let actual = "000102030405060708090a0b0c0d0e0f102030405060708090a0b0c0d0e0f0";
219249
let exampleBuffer = create<Buffer>([0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80, 0x90, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0]);

0 commit comments

Comments
 (0)