Skip to content

Commit

Permalink
feat: rm bigint from functions less than 64 bits
Browse files Browse the repository at this point in the history
  • Loading branch information
Nesopie committed Jul 7, 2024
1 parent c92d94f commit 93e91ea
Show file tree
Hide file tree
Showing 8 changed files with 403 additions and 103 deletions.
132 changes: 110 additions & 22 deletions 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.writeUInt64 = exports.writeUInt32 = exports.writeUInt16 = exports.writeUInt8 = exports.compare = exports.fromHex = exports.toHex = exports.toUtf8 = void 0;
exports.readUInt64 = exports.readUInt32 = exports.readUInt16 = exports.readUInt8 = exports.writeUInt64 = exports.writeUInt32 = exports.writeUInt16 = exports.writeUInt8 = exports.compare = exports.fromHex = exports.toHex = 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 @@ -73,54 +73,62 @@ function compare(v1, v2) {
exports.compare = compare;
function writeUInt8(buffer, offset, value) {
if (offset + 1 > buffer.length) {
throw new Error(" Offset is outside the bounds of Uint8Array");
throw new Error("Offset is outside the bounds of Uint8Array");
}
value = value & 0xffn;
buffer[offset] = Number(value);
if (value > 0xffn) {
throw new Error(`The value of "value" is out of range. It must be >= 0 and <= ${0xffn}. Received ${value}`);
}
buffer[offset] = value;
}
exports.writeUInt8 = writeUInt8;
function writeUInt16(buffer, offset, value, littleEndian) {
if (offset + 2 > buffer.length) {
throw new Error(" Offset is outside the bounds of Uint8Array");
throw new Error("Offset is outside the bounds of Uint8Array");
}
littleEndian = littleEndian.toUpperCase();
value = value & 0xffffn;
if (value > 0xffffn) {
throw new Error(`The value of "value" is out of range. It must be >= 0 and <= ${0xffffn}. Received ${value}`);
}
if (littleEndian === "LE") {
buffer[offset] = Number(value & 0xffn);
buffer[offset + 1] = Number((value >> 8n) & 0xffn);
buffer[offset] = value & 0xff;
buffer[offset + 1] = (value >> 8) & 0xff;
}
else {
buffer[offset] = Number((value >> 8n) & 0xffn);
buffer[offset + 1] = Number(value & 0xffn);
buffer[offset] = (value >> 8) & 0xff;
buffer[offset + 1] = value & 0xff;
}
}
exports.writeUInt16 = writeUInt16;
function writeUInt32(buffer, offset, value, littleEndian) {
if (offset + 4 > buffer.length) {
throw new Error(" Offset is outside the bounds of Uint8Array");
throw new Error("Offset is outside the bounds of Uint8Array");
}
littleEndian = littleEndian.toUpperCase();
value = value & 0xffffffffn;
if (value > 0xffffffffn) {
throw new Error(`The value of "value" is out of range. It must be >= 0 and <= ${0xffffffffn}. Received ${value}`);
}
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] = value & 0xff;
buffer[offset + 1] = (value >> 8) & 0xff;
buffer[offset + 2] = (value >> 16) & 0xff;
buffer[offset + 3] = (value >> 24) & 0xff;
}
else {
buffer[offset] = Number((value >> 24n) & 0xffn);
buffer[offset + 1] = Number((value >> 16n) & 0xffn);
buffer[offset + 2] = Number((value >> 8n) & 0xffn);
buffer[offset + 3] = Number(value & 0xffn);
buffer[offset] = (value >> 24) & 0xff;
buffer[offset + 1] = (value >> 16) & 0xff;
buffer[offset + 2] = (value >> 8) & 0xff;
buffer[offset + 3] = value & 0xff;
}
}
exports.writeUInt32 = writeUInt32;
function writeUInt64(buffer, offset, value, littleEndian) {
if (offset + 8 > buffer.length) {
throw new Error(" Offset is outside the bounds of Uint8Array");
throw new Error("Offset is outside the bounds of Uint8Array");
}
littleEndian = littleEndian.toUpperCase();
value = value & 0xffffffffffffffffn;
if (value > 0xffffffffffffffffn) {
throw new Error(`The value of "value" is out of range. It must be >= 0 and <= ${0xffffffffffffffffn}. Received ${value}`);
}
if (littleEndian === "LE") {
buffer[offset] = Number(value & 0xffn);
buffer[offset + 1] = Number((value >> 8n) & 0xffn);
Expand All @@ -143,3 +151,83 @@ function writeUInt64(buffer, offset, value, littleEndian) {
}
}
exports.writeUInt64 = writeUInt64;
function readUInt8(buffer, offset) {
if (offset + 1 > buffer.length) {
throw new Error("Offset is outside the bounds of Uint8Array");
}
return BigInt(buffer[offset]);
}
exports.readUInt8 = readUInt8;
function readUInt16(buffer, offset, littleEndian) {
if (offset + 2 > buffer.length) {
throw new Error("Offset is outside the bounds of Uint8Array");
}
littleEndian = littleEndian.toUpperCase();
if (littleEndian === "LE") {
let num = 0n;
num = (num << 8n) + BigInt(buffer[offset + 1]);
num = (num << 8n) + BigInt(buffer[offset]);
return num;
}
else {
let num = 0n;
num = (num << 8n) + BigInt(buffer[offset]);
num = (num << 8n) + BigInt(buffer[offset + 1]);
return num;
}
}
exports.readUInt16 = readUInt16;
function readUInt32(buffer, offset, littleEndian) {
if (offset + 4 > buffer.length) {
throw new Error("Offset is outside the bounds of Uint8Array");
}
littleEndian = littleEndian.toUpperCase();
if (littleEndian === "LE") {
let num = 0n;
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 num;
}
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]);
return num;
}
}
exports.readUInt32 = readUInt32;
function readUInt64(buffer, offset, littleEndian) {
if (offset + 8 > buffer.length) {
throw new Error("Offset is outside the bounds of Uint8Array");
}
littleEndian = littleEndian.toUpperCase();
if (littleEndian === "LE") {
let num = 0n;
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 num;
}
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 num;
}
}
exports.readUInt64 = readUInt64;
77 changes: 71 additions & 6 deletions 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.writeUInt64 = exports.writeUInt32 = exports.writeUInt16 = exports.writeUInt8 = exports.compare = exports.fromHex = exports.toHex = exports.toUtf8 = void 0;
exports.readUInt64 = exports.readUInt32 = exports.readUInt16 = exports.readUInt8 = exports.writeUInt64 = exports.writeUInt32 = exports.writeUInt16 = exports.writeUInt8 = exports.compare = exports.fromHex = exports.toHex = exports.toUtf8 = void 0;
function toUtf8(bytes) {
return Buffer.from(bytes || []).toString();
}
Expand All @@ -18,38 +18,53 @@ function compare(v1, v2) {
}
exports.compare = compare;
function writeUInt8(buffer, offset, value) {
if (offset + 1 > buffer.length) {
throw new Error("Offset is outside the bounds of Uint8Array");
}
const buf = Buffer.alloc(1);
buf.writeUInt8(Number(value), 0);
buf.writeUInt8(value, 0);
buffer.set(Uint8Array.from(buf), offset);
}
exports.writeUInt8 = writeUInt8;
function writeUInt16(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.writeUInt16LE(Number(value), 0);
buf.writeUInt16LE(value, 0);
}
else {
buf.writeUInt16BE(Number(value), 0);
buf.writeUInt16BE(value, 0);
}
buffer.set(Uint8Array.from(buf), offset);
}
exports.writeUInt16 = writeUInt16;
function writeUInt32(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.writeUInt32LE(Number(value), 0);
buf.writeUInt32LE(value, 0);
}
else {
buf.writeUInt32BE(Number(value), 0);
buf.writeUInt32BE(value, 0);
}
buffer.set(Uint8Array.from(buf), offset);
}
exports.writeUInt32 = writeUInt32;
function writeUInt64(buffer, offset, value, littleEndian) {
if (offset + 8 > buffer.length) {
throw new Error("Offset is outside the bounds of Uint8Array");
}
littleEndian = littleEndian.toUpperCase();
const buf = Buffer.alloc(8);
if (value > 0xffffffffffffffffn) {
throw new Error(`The value of "value" is out of range. It must be >= 0 and <= ${0xffffffffffffffffn}. Received ${value}`);
}
if (littleEndian === "LE") {
buf.writeBigUInt64LE(value, 0);
}
Expand All @@ -59,3 +74,53 @@ function writeUInt64(buffer, offset, value, littleEndian) {
buffer.set(Uint8Array.from(buf), offset);
}
exports.writeUInt64 = writeUInt64;
function readUInt8(buffer, offset) {
if (offset + 1 > buffer.length) {
throw new Error("Offset is outside the bounds of Uint8Array");
}
const buf = Buffer.from(buffer);
return BigInt(buf.readUInt8(offset));
}
exports.readUInt8 = readUInt8;
function readUInt16(buffer, offset, littleEndian) {
if (offset + 2 > buffer.length) {
throw new Error("Offset is outside the bounds of Uint8Array");
}
littleEndian = littleEndian.toUpperCase();
const buf = Buffer.from(buffer);
if (littleEndian === "LE") {
return BigInt(buf.readUInt16LE(offset));
}
else {
return BigInt(buf.readUInt16BE(offset));
}
}
exports.readUInt16 = readUInt16;
function readUInt32(buffer, offset, littleEndian) {
if (offset + 4 > buffer.length) {
throw new Error("Offset is outside the bounds of Uint8Array");
}
littleEndian = littleEndian.toUpperCase();
const buf = Buffer.from(buffer);
if (littleEndian === "LE") {
return BigInt(buf.readUInt32LE(offset));
}
else {
return BigInt(buf.readUInt32BE(offset));
}
}
exports.readUInt32 = readUInt32;
function readUInt64(buffer, offset, littleEndian) {
if (offset + 8 > buffer.length) {
throw new Error("Offset is outside the bounds of Uint8Array");
}
littleEndian = littleEndian.toUpperCase();
const buf = Buffer.from(buffer);
if (littleEndian === "LE") {
return buf.readBigUInt64LE(offset);
}
else {
return buf.readBigUInt64BE(offset);
}
}
exports.readUInt64 = readUInt64;
10 changes: 7 additions & 3 deletions src/cjs/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ export declare function fromHex(hexString: 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: bigint): void;
export declare function writeUInt16(buffer: Uint8Array, offset: number, value: bigint, littleEndian: endian): void;
export declare function writeUInt32(buffer: Uint8Array, offset: number, value: bigint, littleEndian: endian): void;
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 readUInt8(buffer: Uint8Array, offset: number): bigint;
export declare function readUInt16(buffer: Uint8Array, offset: number, littleEndian: endian): bigint;
export declare function readUInt32(buffer: Uint8Array, offset: number, littleEndian: endian): bigint;
export declare function readUInt64(buffer: Uint8Array, offset: number, littleEndian: endian): bigint;
Loading

0 comments on commit 93e91ea

Please sign in to comment.