UNPKG

@hazae41/cursor

Version:

Rust-like Cursor for TypeScript

334 lines (333 loc) 10.1 kB
var _a, _b, _c, _d, _e; import { Data } from "../../libs/dataviews/mod.js"; export class CursorReadLengthOverflowError extends Error { cursorOffset; cursorLength; bytesLength; #class = _a; name = this.#class.name; constructor(cursorOffset, cursorLength, bytesLength) { super(`Overflow reading ${bytesLength} bytes at offset ${cursorOffset}/${cursorLength}`); this.cursorOffset = cursorOffset; this.cursorLength = cursorLength; this.bytesLength = bytesLength; } static from(cursor, bytesLength) { return new _a(cursor.offset, cursor.length, bytesLength); } } _a = CursorReadLengthOverflowError; export class CursorWriteLengthOverflowError extends Error { cursorOffset; cursorLength; bytesLength; #class = _b; name = this.#class.name; constructor(cursorOffset, cursorLength, bytesLength) { super(`Overflow writing ${bytesLength} bytes at offset ${cursorOffset}/${cursorLength}`); this.cursorOffset = cursorOffset; this.cursorLength = cursorLength; this.bytesLength = bytesLength; } static from(cursor, bytesLength) { return new _b(cursor.offset, cursor.length, bytesLength); } } _b = CursorWriteLengthOverflowError; export class CursorReadNullOverflowError extends Error { cursorOffset; cursorLength; #class = _c; name = this.#class.name; constructor(cursorOffset, cursorLength) { super(`Overflow reading null byte at offset ${cursorOffset}/${cursorLength}`); this.cursorOffset = cursorOffset; this.cursorLength = cursorLength; } static from(cursor) { return new _c(cursor.offset, cursor.length); } } _c = CursorReadNullOverflowError; export class CursorReadUnknownError extends Error { #class = _d; name = this.#class.name; } _d = CursorReadUnknownError; export class CursorWriteUnknownError extends Error { #class = _e; name = this.#class.name; } _e = CursorWriteUnknownError; export class Cursor { bytes; data; offset = 0; /** * A cursor for bytes * @param inner * @param offset */ constructor(bytes) { this.bytes = bytes; this.data = Data.fromView(bytes); } /** * @returns total number of bytes */ get length() { return this.bytes.length; } /** * @returns number of remaining bytes */ get remaining() { return this.length - this.offset; } /** * Get a subarray of the bytes before the current offset * @returns subarray of the bytes before the current offset */ get before() { return this.bytes.subarray(0, this.offset); } /** * Get a subarray of the bytes after the current offset * @returns subarray of the bytes after the current offset */ get after() { return this.bytes.subarray(this.offset); } /** * Get a subarray of the bytes * @param length * @returns subarray of the bytes */ getOrThrow(length) { if (this.remaining < length) throw CursorReadLengthOverflowError.from(this, length); const subarray = this.bytes.subarray(this.offset, this.offset + length); return subarray; } /** * Read a subarray of the bytes * @param length * @returns subarray of the bytes */ readOrThrow(length) { const subarray = this.getOrThrow(length); this.offset += length; return subarray; } /** * Set an array to the bytes * @param array array */ setOrThrow(array) { if (this.remaining < array.length) throw CursorWriteLengthOverflowError.from(this, array.length); this.bytes.set(array, this.offset); } /** * Write an array to the bytes * @param array array */ writeOrThrow(array) { this.setOrThrow(array); this.offset += array.length; } getUint8OrThrow() { return this.data.getUint8(this.offset); } readUint8OrThrow() { const x = this.getUint8OrThrow(); this.offset++; return x; } setUint8OrThrow(x) { this.data.setUint8(this.offset, x); } writeUint8OrThrow(x) { this.setUint8OrThrow(x); this.offset++; } getInt8OrThrow() { return this.data.getInt8(this.offset); } readInt8OrThrow() { const x = this.getInt8OrThrow(); this.offset++; return x; } setInt8OrThrow(x) { this.data.setInt8(this.offset, x); } writeInt8OrThrow(x) { this.setInt8OrThrow(x); this.offset++; } getUint16OrThrow(littleEndian) { return this.data.getUint16(this.offset, littleEndian); } readUint16OrThrow(littleEndian) { const x = this.getUint16OrThrow(littleEndian); this.offset += 2; return x; } setUint16OrThrow(x, littleEndian) { this.data.setUint16(this.offset, x, littleEndian); } writeUint16OrThrow(x, littleEndian) { this.setUint16OrThrow(x, littleEndian); this.offset += 2; } getInt16OrThrow(littleEndian) { return this.data.getInt16(this.offset, littleEndian); } readInt16OrThrow(littleEndian) { const x = this.getInt16OrThrow(littleEndian); this.offset += 2; return x; } setInt16OrThrow(x, littleEndian) { this.data.setInt16(this.offset, x, littleEndian); } writeInt16OrThrow(x, littleEndian) { this.setInt16OrThrow(x, littleEndian); this.offset += 2; } getUint24OrThrow(littleEndian) { if (littleEndian) { return (this.bytes[this.offset]) | (this.bytes[this.offset + 1] << 8) | (this.bytes[this.offset + 2] << 16); } else { return (this.bytes[this.offset] << 16) | (this.bytes[this.offset + 1] << 8) | (this.bytes[this.offset + 2]); } } readUint24OrThrow(littleEndian) { const x = this.getUint24OrThrow(littleEndian); this.offset += 3; return x; } setUint24OrThrow(x, littleEndian) { if (littleEndian) { this.bytes[this.offset] = x & 0xFF; this.bytes[this.offset + 1] = (x >> 8) & 0xFF; this.bytes[this.offset + 2] = (x >> 16) & 0xFF; } else { this.bytes[this.offset] = (x >> 16) & 0xFF; this.bytes[this.offset + 1] = (x >> 8) & 0xFF; this.bytes[this.offset + 2] = x & 0xFF; } } writeUint24OrThrow(x, littleEndian) { this.setUint24OrThrow(x, littleEndian); this.offset += 3; } getUint32OrThrow(littleEndian) { return this.data.getUint32(this.offset, littleEndian); } readUint32OrThrow(littleEndian) { const x = this.getUint32OrThrow(littleEndian); this.offset += 4; return x; } setUint32OrThrow(x, littleEndian) { this.data.setUint32(this.offset, x, littleEndian); } writeUint32OrThrow(x, littleEndian) { this.setUint32OrThrow(x, littleEndian); this.offset += 4; } getInt32OrThrow(littleEndian) { return this.data.getInt32(this.offset, littleEndian); } readInt32OrThrow(littleEndian) { const x = this.getInt32OrThrow(littleEndian); this.offset += 4; return x; } setInt32OrThrow(x, littleEndian) { this.data.setInt32(this.offset, x, littleEndian); } writeInt32OrThrow(x, littleEndian) { this.setInt32OrThrow(x, littleEndian); this.offset += 4; } getBigUint64OrThrow(littleEndian) { return this.data.getBigUint64(this.offset, littleEndian); } readBigUint64OrThrow(littleEndian) { const x = this.getBigUint64OrThrow(littleEndian); this.offset += 8; return x; } setBigUint64OrThrow(x, littleEndian) { this.data.setBigUint64(this.offset, x, littleEndian); } writeBigUint64OrThrow(x, littleEndian) { this.setBigUint64OrThrow(x, littleEndian); this.offset += 8; } getBigInt64OrThrow(littleEndian) { return this.data.getBigInt64(this.offset, littleEndian); } readBigInt64OrThrow(littleEndian) { const x = this.getBigInt64OrThrow(littleEndian); this.offset += 8; return x; } setBigInt64OrThrow(x, littleEndian) { this.data.setBigInt64(this.offset, x, littleEndian); } writeBigInt64OrThrow(x, littleEndian) { this.setBigInt64OrThrow(x, littleEndian); this.offset += 8; } getFloat16OrThrow(littleEndian) { return this.data.getFloat16(this.offset, littleEndian); } readFloat16OrThrow(littleEndian) { const x = this.getFloat16OrThrow(littleEndian); this.offset += 2; return x; } setFloat16OrThrow(x, littleEndian) { this.data.setFloat16(this.offset, x, littleEndian); } writeFloat16OrThrow(x, littleEndian) { this.setFloat16OrThrow(x, littleEndian); this.offset += 2; } getFloat32OrThrow(littleEndian) { return this.data.getFloat32(this.offset, littleEndian); } readFloat32OrThrow(littleEndian) { const x = this.getFloat32OrThrow(littleEndian); this.offset += 4; return x; } setFloat32OrThrow(x, littleEndian) { this.data.setFloat32(this.offset, x, littleEndian); } writeFloat32OrThrow(x, littleEndian) { this.setFloat32OrThrow(x, littleEndian); this.offset += 4; } getFloat64OrThrow(littleEndian) { return this.data.getFloat64(this.offset, littleEndian); } readFloat64OrThrow(littleEndian) { const x = this.getFloat64OrThrow(littleEndian); this.offset += 8; return x; } setFloat64OrThrow(x, littleEndian) { this.data.setFloat64(this.offset, x, littleEndian); } writeFloat64OrThrow(x, littleEndian) { this.setFloat64OrThrow(x, littleEndian); this.offset += 8; } }