| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.Reader = void 0;
- const decodeUtf8_1 = require("./utf8/decodeUtf8");
- class Reader {
- constructor(uint8 = new Uint8Array([]), view = new DataView(uint8.buffer, uint8.byteOffset, uint8.length), x = 0, end = uint8.length) {
- this.uint8 = uint8;
- this.view = view;
- this.x = x;
- this.end = end;
- }
- reset(uint8) {
- this.x = 0;
- this.uint8 = uint8;
- this.view = new DataView(uint8.buffer, uint8.byteOffset, uint8.length);
- }
- size() {
- return this.end - this.x;
- }
- /**
- * Get current byte value without advancing the cursor.
- */
- peek() {
- return this.view.getUint8(this.x);
- }
- /**
- * @deprecated Use peek() instead.
- */
- peak() {
- return this.peek();
- }
- skip(length) {
- this.x += length;
- }
- buf(size = this.size()) {
- const x = this.x;
- const end = x + size;
- const bin = this.uint8.subarray(x, end);
- this.x = end;
- return bin;
- }
- subarray(start = 0, end) {
- const x = this.x;
- const actualStart = x + start;
- const actualEnd = typeof end === 'number' ? x + end : this.end;
- return this.uint8.subarray(actualStart, actualEnd);
- }
- /**
- * Creates a new {@link Reader} that references the same underlying memory
- * buffer. But with independent cursor and end.
- *
- * Preferred over {@link buf} since it also provides a DataView and is much
- * faster to allocate a new {@link Slice} than a new {@link Uint8Array}.
- *
- * @param start Start offset relative to the current cursor position.
- * @param end End offset relative to the current cursor position.
- * @returns A new {@link Reader} instance.
- */
- slice(start = 0, end) {
- const x = this.x;
- const actualStart = x + start;
- const actualEnd = typeof end === 'number' ? x + end : this.end;
- return new Reader(this.uint8, this.view, actualStart, actualEnd);
- }
- /**
- * Similar to {@link slice} but also advances the cursor. Returns a new
- * {@link Reader} that references the same underlying memory buffer, starting
- * from the current cursor position.
- *
- * @param size Number of bytes to cut from the current position.
- * @returns A new {@link Reader} instance.
- */
- cut(size = this.size()) {
- const slice = this.slice(0, size);
- this.skip(size);
- return slice;
- }
- u8() {
- return this.uint8[this.x++];
- // return this.view.getUint8(this.x++);
- }
- i8() {
- return this.view.getInt8(this.x++);
- }
- u16() {
- // const num = this.view.getUint16(this.x);
- // this.x += 2;
- // return num;
- let x = this.x;
- const num = (this.uint8[x++] << 8) + this.uint8[x++];
- this.x = x;
- return num;
- }
- i16() {
- const num = this.view.getInt16(this.x);
- this.x += 2;
- return num;
- }
- u32() {
- const num = this.view.getUint32(this.x);
- this.x += 4;
- return num;
- }
- i32() {
- const num = this.view.getInt32(this.x);
- this.x += 4;
- return num;
- }
- u64() {
- const num = this.view.getBigUint64(this.x);
- this.x += 8;
- return num;
- }
- i64() {
- const num = this.view.getBigInt64(this.x);
- this.x += 8;
- return num;
- }
- f32() {
- const pos = this.x;
- this.x += 4;
- return this.view.getFloat32(pos);
- }
- f64() {
- const pos = this.x;
- this.x += 8;
- return this.view.getFloat64(pos);
- }
- utf8(size) {
- const start = this.x;
- this.x += size;
- return (0, decodeUtf8_1.decodeUtf8)(this.uint8, start, size);
- }
- ascii(length) {
- const uint8 = this.uint8;
- let str = '';
- const end = this.x + length;
- for (let i = this.x; i < end; i++)
- str += String.fromCharCode(uint8[i]);
- this.x = end;
- return str;
- }
- }
- exports.Reader = Reader;
- //# sourceMappingURL=Reader.js.map
|