File tree Expand file tree Collapse file tree 3 files changed +17
-0
lines changed Expand file tree Collapse file tree 3 files changed +17
-0
lines changed Original file line number Diff line number Diff line change @@ -23,6 +23,12 @@ export class Buffer extends Uint8Array {
2323 return result ;
2424 }
2525
26+ writeInt8 ( value : i8 , offset : i32 = 0 ) : i32 {
27+ if ( < u32 > offset > this . dataLength ) throw new RangeError ( E_INDEXOUTOFRANGE ) ;
28+ store < i8 > ( this . dataStart + offset , value ) ;
29+ return offset + 1 ;
30+ }
31+
2632 readInt8 ( offset : i32 = 0 ) : i8 {
2733 if ( < u32 > offset >= this . dataLength ) throw new RangeError ( E_INDEXOUTOFRANGE ) ;
2834 return load < i8 > ( this . dataStart + usize ( offset ) ) ;
Original file line number Diff line number Diff line change @@ -3,6 +3,8 @@ declare class Buffer extends Uint8Array {
33 static alloc ( size : i32 ) : Buffer ;
44 /** This method allocates a new Buffer of indicated size. This is unsafe because the data is not zeroed. */
55 static allocUnsafe ( size : i32 ) : Buffer ;
6+ /** Writes an inputted value to the buffer, at the desired offset. */
7+ writeInt8 ( value :i8 , offset ?:i32 ) : i32 ;
68 /** Reads a signed integer at the designated offset. */
79 readInt8 ( offset ?: i32 ) : i8 ;
810}
Original file line number Diff line number Diff line change @@ -43,6 +43,14 @@ describe("buffer", () => {
4343 // TODO: expectFn(() => { Buffer.allocUnsafe(BLOCK_MAXSIZE + 1); }).toThrow();
4444 } ) ;
4545
46+ test ( "#writeInt8" , ( ) => {
47+ let buff = new Buffer ( 5 ) ;
48+ expect < i32 > ( buff . writeInt8 ( 9 ) ) . toBe ( 1 ) ;
49+ expect < i32 > ( buff . writeInt8 ( - 3 , 4 ) ) . toBe ( 5 ) ;
50+ expect < i8 > ( buff [ 0 ] ) . toBe ( 9 ) ;
51+ expect < i8 > ( buff [ 4 ] ) . toBe ( - 3 ) ;
52+ } ) ;
53+
4654 test ( "#readInt8" , ( ) => {
4755 let buff = new Buffer ( 10 ) ;
4856 buff [ 0 ] = 5 ;
@@ -57,4 +65,5 @@ describe("buffer", () => {
5765 // newBuff.readInt8(5);
5866 // }).toThrow();
5967 } )
68+
6069} ) ;
You can’t perform that action at this time.
0 commit comments