Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions assembly/buffer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ export class Buffer extends Uint8Array {
return value instanceof Buffer;
}

// Adapted from https://github.com/AssemblyScript/assemblyscript/blob/master/std/assembly/typedarray.ts
public subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Buffer {
var len = <i32>this.dataLength;
begin = begin < 0 ? max(len + begin, 0) : min(begin, len);
end = end < 0 ? max(len + end, 0) : min(end, len);
end = max(end, begin);
var out = changetype<Buffer>(__alloc(offsetof<Buffer>(), idof<Buffer>())); // retains
out.data = this.data; // retains
out.dataStart = this.dataStart + <usize>begin;
out.dataLength = end - begin;
return out;
}

readInt8(offset: i32 = 0): i8 {
if(i32(offset < 0) | i32(<u32>offset >= this.dataLength)) throw new RangeError(E_INDEXOUTOFRANGE);
return load<i8>(this.dataStart + <usize>offset);
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 33 additions & 3 deletions tests/buffer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ describe("buffer", () => {
// expectFn(() => {
// let newBuff = new Buffer(1);
// newBuff.writeInt8(5,10);
// }).toThrow();
// }).toThrow();
});

test("#writeUInt8", () => {
Expand All @@ -115,8 +115,8 @@ describe("buffer", () => {
// expectFn(() => {
// let newBuff = new Buffer(1);
// newBuff.writeUInt8(5,10);
// }).toThrow();
});
// }).toThrow();
});

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

test("#subarray", () => {
let example = create<Buffer>([1, 2, 3, 4, 5, 6, 7, 8]);

// no parameters means copy the Buffer
let actual = example.subarray();
expect<Buffer>(actual).toStrictEqual(example);
expect<ArrayBuffer>(actual.buffer).toBe(example.buffer); // should use the same buffer

// start at offset 5
actual = example.subarray(5);
let expected = create<Buffer>([6, 7, 8]);
// trace("length", 1, expected.length);
expect<Buffer>(actual).toStrictEqual(expected);

// negative start indicies, start at (8 - 5)
actual = example.subarray(-5);
expected = create<Buffer>([4, 5, 6, 7, 8]);
expect<Buffer>(actual).toStrictEqual(expected);

// two parameters
actual = example.subarray(2, 6);
expected = create<Buffer>([3, 4, 5, 6]);
expect<Buffer>(actual).toStrictEqual(expected);

// negative end index
actual = example.subarray(4, -1);
expected = create<Buffer>([5, 6, 7]);
expect<Buffer>(actual).toStrictEqual(expected);
});

test("#Hex.encode", () => {
let actual = "000102030405060708090a0b0c0d0e0f102030405060708090a0b0c0d0e0f0";
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]);
Expand Down