Skip to content

Commit 9db66bd

Browse files
committed
[Implement] ReadUInt8
1 parent fe46e13 commit 9db66bd

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

assembly/buffer/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,9 @@ export class Buffer extends Uint8Array {
2727
if(<u32>offset >= this.dataLength) throw new RangeError(E_INDEXOUTOFRANGE);
2828
return load<i8>(this.dataStart + usize(offset));
2929
}
30+
31+
readUInt8(offset: i32 = 0): u8 {
32+
if(<u32>offset > this.dataLength) throw new RangeError(E_INDEXOUTOFRANGE);
33+
return load<u8>(this.dataStart + usize(offset));
34+
}
3035
}

assembly/node.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ declare class Buffer extends Uint8Array {
55
static allocUnsafe(size: i32): Buffer;
66
/** Reads a signed integer at the designated offset. */
77
readInt8(offset?: i32): i8;
8+
readUInt8(offset?: i32): u8;
89
}

tests/buffer.spec.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,21 @@ describe("buffer", () => {
5656
// let newBuff = new Buffer(1);
5757
// newBuff.readInt8(5);
5858
// }).toThrow();
59-
})
59+
});
60+
61+
test("#readUInt8", () => {
62+
let buff = new Buffer(10);
63+
buff[0] = -2;
64+
buff[9] = 47;
65+
// Testing casting between u8 and i8.
66+
expect<u8>(buff.readUInt8(0)).toBe(254);
67+
expect<u8>(buff.readUInt8()).toBe(254);
68+
// Testing offset
69+
expect<u8>(buff.readUInt8(9)).toBe(47);
70+
// TODO:
71+
// expectFn(() => {
72+
// let newBuff = new Buffer(1);
73+
// newBuff.readUInt8(5);
74+
// }).toThrow();
75+
});
6076
});

0 commit comments

Comments
 (0)