Skip to content

Commit

Permalink
Merge pull request #11 from Nesopie/feat/int-read-write
Browse files Browse the repository at this point in the history
Feat/int read write
  • Loading branch information
junderw authored Aug 29, 2024
2 parents 8218fc0 + 4d2d33f commit c09ddd1
Show file tree
Hide file tree
Showing 8 changed files with 1,166 additions and 14 deletions.
170 changes: 169 additions & 1 deletion src/cjs/browser.cjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.readUInt64 = exports.readUInt32 = exports.readUInt16 = exports.readUInt8 = exports.writeUInt64 = exports.writeUInt32 = exports.writeUInt16 = exports.writeUInt8 = exports.compare = exports.fromBase64 = exports.toBase64 = exports.fromHex = exports.toHex = exports.concat = exports.fromUtf8 = exports.toUtf8 = void 0;
exports.readInt64 = exports.readInt32 = exports.readInt16 = exports.readInt8 = exports.writeInt64 = exports.writeInt32 = exports.writeInt16 = exports.writeInt8 = exports.readUInt64 = exports.readUInt32 = exports.readUInt16 = exports.readUInt8 = exports.writeUInt64 = exports.writeUInt32 = exports.writeUInt16 = exports.writeUInt8 = exports.compare = exports.fromBase64 = exports.toBase64 = exports.fromHex = exports.toHex = exports.concat = exports.fromUtf8 = exports.toUtf8 = void 0;
const HEX_STRINGS = "0123456789abcdefABCDEF";
const HEX_CODES = HEX_STRINGS.split("").map((c) => c.codePointAt(0));
const HEX_CODEPOINTS = Array(256)
Expand Down Expand Up @@ -107,6 +107,7 @@ function writeUInt8(buffer, offset, value) {
throw new Error(`The value of "value" is out of range. It must be >= 0 and <= ${0xff}. Received ${value}`);
}
buffer[offset] = value;
return offset + 1;
}
exports.writeUInt8 = writeUInt8;
function writeUInt16(buffer, offset, value, littleEndian) {
Expand All @@ -125,6 +126,7 @@ function writeUInt16(buffer, offset, value, littleEndian) {
buffer[offset] = (value >> 8) & 0xff;
buffer[offset + 1] = value & 0xff;
}
return offset + 2;
}
exports.writeUInt16 = writeUInt16;
function writeUInt32(buffer, offset, value, littleEndian) {
Expand All @@ -147,6 +149,7 @@ function writeUInt32(buffer, offset, value, littleEndian) {
buffer[offset + 2] = (value >> 8) & 0xff;
buffer[offset + 3] = value & 0xff;
}
return offset + 4;
}
exports.writeUInt32 = writeUInt32;
function writeUInt64(buffer, offset, value, littleEndian) {
Expand Down Expand Up @@ -177,6 +180,7 @@ function writeUInt64(buffer, offset, value, littleEndian) {
buffer[offset + 6] = Number((value >> 8n) & 0xffn);
buffer[offset + 7] = Number(value & 0xffn);
}
return offset + 8;
}
exports.writeUInt64 = writeUInt64;
function readUInt8(buffer, offset) {
Expand Down Expand Up @@ -259,3 +263,167 @@ function readUInt64(buffer, offset, littleEndian) {
}
}
exports.readUInt64 = readUInt64;
function writeInt8(buffer, offset, value) {
if (offset + 1 > buffer.length) {
throw new Error("Offset is outside the bounds of Uint8Array");
}
if (value > 0x7f || value < -0x80) {
throw new Error(`The value of "value" is out of range. It must be >= ${-0x80} and <= ${0x7f}. Received ${value}`);
}
buffer[offset] = value;
return offset + 1;
}
exports.writeInt8 = writeInt8;
function writeInt16(buffer, offset, value, littleEndian) {
if (offset + 2 > buffer.length) {
throw new Error("Offset is outside the bounds of Uint8Array");
}
if (value > 0x7fff || value < -0x8000) {
throw new Error(`The value of "value" is out of range. It must be >= ${-0x8000} and <= ${0x7fff}. Received ${value}`);
}
littleEndian = littleEndian.toUpperCase();
if (littleEndian === "LE") {
buffer[offset] = value & 0xff;
buffer[offset + 1] = (value >> 8) & 0xff;
}
else {
buffer[offset] = (value >> 8) & 0xff;
buffer[offset + 1] = value & 0xff;
}
return offset + 2;
}
exports.writeInt16 = writeInt16;
function writeInt32(buffer, offset, value, littleEndian) {
if (offset + 4 > buffer.length) {
throw new Error("Offset is outside the bounds of Uint8Array");
}
if (value > 0x7fffffff || value < -0x80000000) {
throw new Error(`The value of "value" is out of range. It must be >= ${-0x80000000} and <= ${0x7fffffff}. Received ${value}`);
}
littleEndian = littleEndian.toUpperCase();
if (littleEndian === "LE") {
buffer[offset] = value & 0xff;
buffer[offset + 1] = (value >> 8) & 0xff;
buffer[offset + 2] = (value >> 16) & 0xff;
buffer[offset + 3] = (value >> 24) & 0xff;
}
else {
buffer[offset] = (value >> 24) & 0xff;
buffer[offset + 1] = (value >> 16) & 0xff;
buffer[offset + 2] = (value >> 8) & 0xff;
buffer[offset + 3] = value & 0xff;
}
return offset + 4;
}
exports.writeInt32 = writeInt32;
function writeInt64(buffer, offset, value, littleEndian) {
if (offset + 8 > buffer.length) {
throw new Error("Offset is outside the bounds of Uint8Array");
}
if (value > 0x7fffffffffffffffn || value < -0x8000000000000000n) {
throw new Error(`The value of "value" is out of range. It must be >= ${-0x8000000000000000n} and <= ${0x7fffffffffffffffn}. Received ${value}`);
}
littleEndian = littleEndian.toUpperCase();
if (littleEndian === "LE") {
buffer[offset] = Number(value & 0xffn);
buffer[offset + 1] = Number((value >> 8n) & 0xffn);
buffer[offset + 2] = Number((value >> 16n) & 0xffn);
buffer[offset + 3] = Number((value >> 24n) & 0xffn);
buffer[offset + 4] = Number((value >> 32n) & 0xffn);
buffer[offset + 5] = Number((value >> 40n) & 0xffn);
buffer[offset + 6] = Number((value >> 48n) & 0xffn);
buffer[offset + 7] = Number((value >> 56n) & 0xffn);
}
else {
buffer[offset] = Number((value >> 56n) & 0xffn);
buffer[offset + 1] = Number((value >> 48n) & 0xffn);
buffer[offset + 2] = Number((value >> 40n) & 0xffn);
buffer[offset + 3] = Number((value >> 32n) & 0xffn);
buffer[offset + 4] = Number((value >> 24n) & 0xffn);
buffer[offset + 5] = Number((value >> 16n) & 0xffn);
buffer[offset + 6] = Number((value >> 8n) & 0xffn);
buffer[offset + 7] = Number(value & 0xffn);
}
return offset + 8;
}
exports.writeInt64 = writeInt64;
function readInt8(buffer, offset) {
if (offset + 1 > buffer.length) {
throw new Error("Offset is outside the bounds of Uint8Array");
}
const val = buffer[offset];
if (val <= 0x7f) {
return val;
}
else {
return val - 0x100;
}
}
exports.readInt8 = readInt8;
function readInt16(buffer, offset, littleEndian) {
if (offset + 2 > buffer.length) {
throw new Error("Offset is outside the bounds of Uint8Array");
}
littleEndian = littleEndian.toUpperCase();
if (littleEndian === "LE") {
const val = buffer[offset] + (buffer[offset + 1] << 8);
return buffer[offset + 1] <= 0x7f ? val : val - 0x10000;
}
else {
const val = (buffer[offset] << 8) + buffer[offset + 1];
return buffer[offset] <= 0x7f ? val : val - 0x10000;
}
}
exports.readInt16 = readInt16;
function readInt32(buffer, offset, littleEndian) {
if (offset + 4 > buffer.length) {
throw new Error("Offset is outside the bounds of Uint8Array");
}
littleEndian = littleEndian.toUpperCase();
if (littleEndian === "LE") {
const val = buffer[offset] +
(buffer[offset + 1] << 8) +
(buffer[offset + 2] << 16) +
((buffer[offset + 3] << 24) >>> 0);
return buffer[offset + 3] <= 0x7f ? val : val - 0x100000000;
}
else {
const val = ((buffer[offset] << 24) >>> 0) +
(buffer[offset + 1] << 16) +
(buffer[offset + 2] << 8) +
buffer[offset + 3];
return buffer[offset] <= 0x7f ? val : val - 0x100000000;
}
}
exports.readInt32 = readInt32;
function readInt64(buffer, offset, littleEndian) {
if (offset + 8 > buffer.length) {
throw new Error("Offset is outside the bounds of Uint8Array");
}
littleEndian = littleEndian.toUpperCase();
let num = 0n;
if (littleEndian === "LE") {
num = (num << 8n) + BigInt(buffer[offset + 7]);
num = (num << 8n) + BigInt(buffer[offset + 6]);
num = (num << 8n) + BigInt(buffer[offset + 5]);
num = (num << 8n) + BigInt(buffer[offset + 4]);
num = (num << 8n) + BigInt(buffer[offset + 3]);
num = (num << 8n) + BigInt(buffer[offset + 2]);
num = (num << 8n) + BigInt(buffer[offset + 1]);
num = (num << 8n) + BigInt(buffer[offset]);
return buffer[offset + 7] <= 0x7f ? num : num - 0x10000000000000000n;
}
else {
let num = 0n;
num = (num << 8n) + BigInt(buffer[offset]);
num = (num << 8n) + BigInt(buffer[offset + 1]);
num = (num << 8n) + BigInt(buffer[offset + 2]);
num = (num << 8n) + BigInt(buffer[offset + 3]);
num = (num << 8n) + BigInt(buffer[offset + 4]);
num = (num << 8n) + BigInt(buffer[offset + 5]);
num = (num << 8n) + BigInt(buffer[offset + 6]);
num = (num << 8n) + BigInt(buffer[offset + 7]);
return buffer[offset] <= 0x7f ? num : num - 0x10000000000000000n;
}
}
exports.readInt64 = readInt64;
114 changes: 113 additions & 1 deletion src/cjs/index.cjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.readUInt64 = exports.readUInt32 = exports.readUInt16 = exports.readUInt8 = exports.writeUInt64 = exports.writeUInt32 = exports.writeUInt16 = exports.writeUInt8 = exports.compare = exports.fromBase64 = exports.toBase64 = exports.fromHex = exports.toHex = exports.concat = exports.fromUtf8 = exports.toUtf8 = void 0;
exports.readInt64 = exports.readInt32 = exports.readInt16 = exports.readInt8 = exports.writeInt64 = exports.writeInt32 = exports.writeInt16 = exports.writeInt8 = exports.readUInt64 = exports.readUInt32 = exports.readUInt16 = exports.readUInt8 = exports.writeUInt64 = exports.writeUInt32 = exports.writeUInt16 = exports.writeUInt8 = exports.compare = exports.fromBase64 = exports.toBase64 = exports.fromHex = exports.toHex = exports.concat = exports.fromUtf8 = exports.toUtf8 = void 0;
function toUtf8(bytes) {
return Buffer.from(bytes || []).toString();
}
Expand Down Expand Up @@ -40,6 +40,7 @@ function writeUInt8(buffer, offset, value) {
const buf = Buffer.alloc(1);
buf.writeUInt8(value, 0);
buffer.set(Uint8Array.from(buf), offset);
return offset + 1;
}
exports.writeUInt8 = writeUInt8;
function writeUInt16(buffer, offset, value, littleEndian) {
Expand All @@ -55,6 +56,7 @@ function writeUInt16(buffer, offset, value, littleEndian) {
buf.writeUInt16BE(value, 0);
}
buffer.set(Uint8Array.from(buf), offset);
return offset + 2;
}
exports.writeUInt16 = writeUInt16;
function writeUInt32(buffer, offset, value, littleEndian) {
Expand All @@ -70,6 +72,7 @@ function writeUInt32(buffer, offset, value, littleEndian) {
buf.writeUInt32BE(value, 0);
}
buffer.set(Uint8Array.from(buf), offset);
return offset + 4;
}
exports.writeUInt32 = writeUInt32;
function writeUInt64(buffer, offset, value, littleEndian) {
Expand All @@ -88,6 +91,7 @@ function writeUInt64(buffer, offset, value, littleEndian) {
buf.writeBigUInt64BE(value, 0);
}
buffer.set(Uint8Array.from(buf), offset);
return offset + 8;
}
exports.writeUInt64 = writeUInt64;
function readUInt8(buffer, offset) {
Expand Down Expand Up @@ -140,3 +144,111 @@ function readUInt64(buffer, offset, littleEndian) {
}
}
exports.readUInt64 = readUInt64;
function writeInt8(buffer, offset, value) {
if (offset + 1 > buffer.length) {
throw new Error("Offset is outside the bounds of Uint8Array");
}
const buf = Buffer.alloc(1);
buf.writeInt8(value, 0);
buffer.set(Uint8Array.from(buf), offset);
return offset + 1;
}
exports.writeInt8 = writeInt8;
function writeInt16(buffer, offset, value, littleEndian) {
if (offset + 2 > buffer.length) {
throw new Error("Offset is outside the bounds of Uint8Array");
}
littleEndian = littleEndian.toUpperCase();
const buf = Buffer.alloc(2);
if (littleEndian === "LE") {
buf.writeInt16LE(value, 0);
}
else {
buf.writeInt16BE(value, 0);
}
buffer.set(Uint8Array.from(buf), offset);
return offset + 2;
}
exports.writeInt16 = writeInt16;
function writeInt32(buffer, offset, value, littleEndian) {
if (offset + 4 > buffer.length) {
throw new Error("Offset is outside the bounds of Uint8Array");
}
littleEndian = littleEndian.toUpperCase();
const buf = Buffer.alloc(4);
if (littleEndian === "LE") {
buf.writeInt32LE(value, 0);
}
else {
buf.writeInt32BE(value, 0);
}
buffer.set(Uint8Array.from(buf), offset);
return offset + 4;
}
exports.writeInt32 = writeInt32;
function writeInt64(buffer, offset, value, littleEndian) {
if (offset + 8 > buffer.length) {
throw new Error("Offset is outside the bounds of Uint8Array");
}
if (value > 0x7fffffffffffffffn || value < -0x8000000000000000n) {
throw new Error(`The value of "value" is out of range. It must be >= ${-0x8000000000000000n} and <= ${0x7fffffffffffffffn}. Received ${value}`);
}
littleEndian = littleEndian.toUpperCase();
const buf = Buffer.alloc(8);
if (littleEndian === "LE") {
buf.writeBigInt64LE(value, 0);
}
else {
buf.writeBigInt64BE(value, 0);
}
buffer.set(Uint8Array.from(buf), offset);
return offset + 8;
}
exports.writeInt64 = writeInt64;
function readInt8(buffer, offset) {
if (offset + 1 > buffer.length) {
throw new Error("Offset is outside the bounds of Uint8Array");
}
const buf = Buffer.from(buffer);
return buf.readInt8(offset);
}
exports.readInt8 = readInt8;
function readInt16(buffer, offset, littleEndian) {
if (offset + 2 > buffer.length) {
throw new Error("Offset is outside the bounds of Uint8Array");
}
littleEndian = littleEndian.toUpperCase();
if (littleEndian === "LE") {
return Buffer.from(buffer).readInt16LE(offset);
}
else {
return Buffer.from(buffer).readInt16BE(offset);
}
}
exports.readInt16 = readInt16;
function readInt32(buffer, offset, littleEndian) {
if (offset + 4 > buffer.length) {
throw new Error("Offset is outside the bounds of Uint8Array");
}
littleEndian = littleEndian.toUpperCase();
if (littleEndian === "LE") {
return Buffer.from(buffer).readInt32LE(offset);
}
else {
return Buffer.from(buffer).readInt32BE(offset);
}
}
exports.readInt32 = readInt32;
function readInt64(buffer, offset, littleEndian) {
if (offset + 8 > buffer.length) {
throw new Error("Offset is outside the bounds of Uint8Array");
}
littleEndian = littleEndian.toUpperCase();
if (littleEndian === "LE") {
return Buffer.from(buffer).readBigInt64LE(offset);
}
else {
return Buffer.from(buffer).readBigInt64BE(offset);
}
}
exports.readInt64 = readInt64;
16 changes: 12 additions & 4 deletions src/cjs/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,19 @@ export declare function fromBase64(base64: string): Uint8Array;
export declare type CompareResult = -1 | 0 | 1;
export declare function compare(v1: Uint8Array, v2: Uint8Array): CompareResult;
export declare type endian = "LE" | "BE" | "le" | "be";
export declare function writeUInt8(buffer: Uint8Array, offset: number, value: number): void;
export declare function writeUInt16(buffer: Uint8Array, offset: number, value: number, littleEndian: endian): void;
export declare function writeUInt32(buffer: Uint8Array, offset: number, value: number, littleEndian: endian): void;
export declare function writeUInt64(buffer: Uint8Array, offset: number, value: bigint, littleEndian: endian): void;
export declare function writeUInt8(buffer: Uint8Array, offset: number, value: number): number;
export declare function writeUInt16(buffer: Uint8Array, offset: number, value: number, littleEndian: endian): number;
export declare function writeUInt32(buffer: Uint8Array, offset: number, value: number, littleEndian: endian): number;
export declare function writeUInt64(buffer: Uint8Array, offset: number, value: bigint, littleEndian: endian): number;
export declare function readUInt8(buffer: Uint8Array, offset: number): number;
export declare function readUInt16(buffer: Uint8Array, offset: number, littleEndian: endian): number;
export declare function readUInt32(buffer: Uint8Array, offset: number, littleEndian: endian): number;
export declare function readUInt64(buffer: Uint8Array, offset: number, littleEndian: endian): bigint;
export declare function writeInt8(buffer: Uint8Array, offset: number, value: number): number;
export declare function writeInt16(buffer: Uint8Array, offset: number, value: number, littleEndian: endian): number;
export declare function writeInt32(buffer: Uint8Array, offset: number, value: number, littleEndian: endian): number;
export declare function writeInt64(buffer: Uint8Array, offset: number, value: bigint, littleEndian: endian): number;
export declare function readInt8(buffer: Uint8Array, offset: number): number;
export declare function readInt16(buffer: Uint8Array, offset: number, littleEndian: endian): number;
export declare function readInt32(buffer: Uint8Array, offset: number, littleEndian: endian): number;
export declare function readInt64(buffer: Uint8Array, offset: number, littleEndian: endian): bigint;
Loading

0 comments on commit c09ddd1

Please sign in to comment.