Reader.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Reader = void 0;
  4. const decodeUtf8_1 = require("./utf8/decodeUtf8");
  5. class Reader {
  6. constructor(uint8 = new Uint8Array([]), view = new DataView(uint8.buffer, uint8.byteOffset, uint8.length), x = 0, end = uint8.length) {
  7. this.uint8 = uint8;
  8. this.view = view;
  9. this.x = x;
  10. this.end = end;
  11. }
  12. reset(uint8) {
  13. this.x = 0;
  14. this.uint8 = uint8;
  15. this.view = new DataView(uint8.buffer, uint8.byteOffset, uint8.length);
  16. }
  17. size() {
  18. return this.end - this.x;
  19. }
  20. /**
  21. * Get current byte value without advancing the cursor.
  22. */
  23. peek() {
  24. return this.view.getUint8(this.x);
  25. }
  26. /**
  27. * @deprecated Use peek() instead.
  28. */
  29. peak() {
  30. return this.peek();
  31. }
  32. skip(length) {
  33. this.x += length;
  34. }
  35. buf(size = this.size()) {
  36. const x = this.x;
  37. const end = x + size;
  38. const bin = this.uint8.subarray(x, end);
  39. this.x = end;
  40. return bin;
  41. }
  42. subarray(start = 0, end) {
  43. const x = this.x;
  44. const actualStart = x + start;
  45. const actualEnd = typeof end === 'number' ? x + end : this.end;
  46. return this.uint8.subarray(actualStart, actualEnd);
  47. }
  48. /**
  49. * Creates a new {@link Reader} that references the same underlying memory
  50. * buffer. But with independent cursor and end.
  51. *
  52. * Preferred over {@link buf} since it also provides a DataView and is much
  53. * faster to allocate a new {@link Slice} than a new {@link Uint8Array}.
  54. *
  55. * @param start Start offset relative to the current cursor position.
  56. * @param end End offset relative to the current cursor position.
  57. * @returns A new {@link Reader} instance.
  58. */
  59. slice(start = 0, end) {
  60. const x = this.x;
  61. const actualStart = x + start;
  62. const actualEnd = typeof end === 'number' ? x + end : this.end;
  63. return new Reader(this.uint8, this.view, actualStart, actualEnd);
  64. }
  65. /**
  66. * Similar to {@link slice} but also advances the cursor. Returns a new
  67. * {@link Reader} that references the same underlying memory buffer, starting
  68. * from the current cursor position.
  69. *
  70. * @param size Number of bytes to cut from the current position.
  71. * @returns A new {@link Reader} instance.
  72. */
  73. cut(size = this.size()) {
  74. const slice = this.slice(0, size);
  75. this.skip(size);
  76. return slice;
  77. }
  78. u8() {
  79. return this.uint8[this.x++];
  80. // return this.view.getUint8(this.x++);
  81. }
  82. i8() {
  83. return this.view.getInt8(this.x++);
  84. }
  85. u16() {
  86. // const num = this.view.getUint16(this.x);
  87. // this.x += 2;
  88. // return num;
  89. let x = this.x;
  90. const num = (this.uint8[x++] << 8) + this.uint8[x++];
  91. this.x = x;
  92. return num;
  93. }
  94. i16() {
  95. const num = this.view.getInt16(this.x);
  96. this.x += 2;
  97. return num;
  98. }
  99. u32() {
  100. const num = this.view.getUint32(this.x);
  101. this.x += 4;
  102. return num;
  103. }
  104. i32() {
  105. const num = this.view.getInt32(this.x);
  106. this.x += 4;
  107. return num;
  108. }
  109. u64() {
  110. const num = this.view.getBigUint64(this.x);
  111. this.x += 8;
  112. return num;
  113. }
  114. i64() {
  115. const num = this.view.getBigInt64(this.x);
  116. this.x += 8;
  117. return num;
  118. }
  119. f32() {
  120. const pos = this.x;
  121. this.x += 4;
  122. return this.view.getFloat32(pos);
  123. }
  124. f64() {
  125. const pos = this.x;
  126. this.x += 8;
  127. return this.view.getFloat64(pos);
  128. }
  129. utf8(size) {
  130. const start = this.x;
  131. this.x += size;
  132. return (0, decodeUtf8_1.decodeUtf8)(this.uint8, start, size);
  133. }
  134. ascii(length) {
  135. const uint8 = this.uint8;
  136. let str = '';
  137. const end = this.x + length;
  138. for (let i = this.x; i < end; i++)
  139. str += String.fromCharCode(uint8[i]);
  140. this.x = end;
  141. return str;
  142. }
  143. }
  144. exports.Reader = Reader;
  145. //# sourceMappingURL=Reader.js.map