Skip to content

Commit 3a306c0

Browse files
lpatinytargos
authored andcommitted
feat: add int64 support
1 parent 0ddd107 commit 3a306c0

File tree

5 files changed

+70
-0
lines changed

5 files changed

+70
-0
lines changed

src/IOBuffer.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,24 @@ export class IOBuffer {
313313
return value;
314314
}
315315

316+
/**
317+
* Read a 64-bit signed integer number and move pointer forward by 8 bytes.
318+
*/
319+
public readBigInt64(): bigint {
320+
const value = this._data.getBigInt64(this.offset, this.littleEndian);
321+
this.offset += 8;
322+
return value;
323+
}
324+
325+
/**
326+
* Read a 64-bit unsigned integer number and move pointer forward by 8 bytes.
327+
*/
328+
public readBigUint64(): bigint {
329+
const value = this._data.getBigUint64(this.offset, this.littleEndian);
330+
this.offset += 8;
331+
return value;
332+
}
333+
316334
/**
317335
* Read a 1-byte ASCII character and move pointer forward by 1 byte.
318336
*/
@@ -461,6 +479,30 @@ export class IOBuffer {
461479
return this;
462480
}
463481

482+
/**
483+
* Write `value` as a 64-bit signed bigint and move pointer forward by 8
484+
* bytes.
485+
*/
486+
public writeBigInt64(value: bigint): this {
487+
this.ensureAvailable(8);
488+
this._data.setBigInt64(this.offset, value, this.littleEndian);
489+
this.offset += 8;
490+
this._updateLastWrittenByte();
491+
return this;
492+
}
493+
494+
/**
495+
* Write `value` as a 64-bit unsigned bigint and move pointer forward by 8
496+
* bytes.
497+
*/
498+
public writeBigUint64(value: bigint): this {
499+
this.ensureAvailable(8);
500+
this._data.setBigUint64(this.offset, value, this.littleEndian);
501+
this.offset += 8;
502+
this._updateLastWrittenByte();
503+
return this;
504+
}
505+
464506
/**
465507
* Write the charCode of `str`'s first character as an 8-bit unsigned integer
466508
* and move pointer forward by 1 byte.

src/__tests__/__snapshots__/read.ts.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`read data readBigInt64 1`] = `71777218556133120n`;
4+
5+
exports[`read data readBigUint64 1`] = `71777218556133120n`;
6+
37
exports[`read data readFloat32 1`] = `-1.714652191593956e+38`;
48

59
exports[`read data readFloat32 2`] = `2.3418408851396162e-38`;

src/__tests__/__snapshots__/write.ts.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`write data writeBigInt64 1`] = `-1234567890n`;
4+
5+
exports[`write data writeBigUint64 1`] = `1234567890n`;
6+
37
exports[`write data writeFloat32 1`] = `-1.7100000153031665e+38`;
48

59
exports[`write data writeFloat32 2`] = `2.3399999993470327e-38`;

src/__tests__/read.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ describe('read data', () => {
8787
expect(buffer.readFloat64()).toMatchSnapshot();
8888
});
8989

90+
it('readBigInt64', () => {
91+
expect(buffer.readBigInt64()).toMatchSnapshot();
92+
});
93+
94+
it('readBigUint64', () => {
95+
expect(buffer.readBigUint64()).toMatchSnapshot();
96+
});
97+
9098
it('readChar(s)', () => {
9199
const chars = 'hello'.split('').map((char) => char.charCodeAt(0));
92100
const theBuffer = new IOBuffer(new Uint8Array(chars));

src/__tests__/write.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,18 @@ describe('write data', () => {
8585
expect(buffer.readFloat64()).toMatchSnapshot();
8686
});
8787

88+
it('writeBigInt64', () => {
89+
buffer.writeBigInt64(-1234567890n);
90+
buffer.rewind();
91+
expect(buffer.readBigInt64()).toMatchSnapshot();
92+
});
93+
94+
it('writeBigUint64', () => {
95+
buffer.writeBigUint64(1234567890n);
96+
buffer.rewind();
97+
expect(buffer.readBigInt64()).toMatchSnapshot();
98+
});
99+
88100
it('writeChar(s)', () => {
89101
const theBuffer = new IOBuffer(5);
90102
theBuffer.writeChar('h');

0 commit comments

Comments
 (0)