libshv-js 3.16.1 → 3.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/cpcontext.ts DELETED
@@ -1,87 +0,0 @@
1
- class UnpackContext {
2
- index: number;
3
- private readonly data: Uint8Array;
4
-
5
- constructor(buf: ArrayBufferLike) {
6
- this.data = new Uint8Array(buf);
7
- this.index = 0;
8
- }
9
-
10
- getByte() {
11
- if (this.index >= this.data.length) {
12
- throw new RangeError('unexpected end of data');
13
- }
14
-
15
- return this.data[this.index++];
16
- }
17
-
18
- peekByte() {
19
- if (this.index >= this.data.length) {
20
- return -1;
21
- }
22
-
23
- return this.data[this.index];
24
- }
25
-
26
- getBytes(str: string) {
27
- for (let i = 0; i < str.length; i++) {
28
- if (str.codePointAt(i) !== this.getByte()) {
29
- throw new TypeError(`'${str}' expected`);
30
- }
31
- }
32
- }
33
- }
34
-
35
- const transfer = (source: ArrayBuffer, length: number) => {
36
- if (length <= source.byteLength) {
37
- return source.slice(0, length);
38
- }
39
-
40
- const sourceView = new Uint8Array(source);
41
- const destView = new Uint8Array(new ArrayBuffer(length));
42
- destView.set(sourceView);
43
- return destView.buffer;
44
- };
45
-
46
- class PackContext {
47
- static CHUNK_LEN = 1024;
48
- data = new Uint8Array(0);
49
- length = 0;
50
-
51
- putByte(b: number) {
52
- if (this.length >= this.data.length) {
53
- const buffer = transfer(this.data.buffer, this.data.length + PackContext.CHUNK_LEN);
54
- this.data = new Uint8Array(buffer);
55
- }
56
-
57
- this.data[this.length++] = b;
58
- }
59
-
60
- writeStringUtf8(str: string) {
61
- for (let i = 0; i < str.length; i++) {
62
- const charcode = str.codePointAt(i)!;
63
- this.writeCharCodeUtf8(charcode);
64
- }
65
- }
66
-
67
- writeCharCodeUtf8(charcode: number) {
68
- /* eslint-disable no-bitwise */
69
- if (charcode < 0x80) {
70
- this.putByte(charcode);
71
- } else if (charcode < 0x8_00) {
72
- this.putByte(0xC0 | (charcode >> 6));
73
- this.putByte(0x80 | (charcode & 0x3F));
74
- } else if (charcode < 0xD8_00 || charcode >= 0xE0_00) {
75
- this.putByte(0xE0 | (charcode >> 12));
76
- this.putByte(0x80 | ((charcode >> 6) & 0x3F));
77
- this.putByte(0x80 | (charcode & 0x3F));
78
- }
79
- /* eslint-enable no-bitwise */
80
- }
81
-
82
- buffer() {
83
- return this.data.buffer.slice(0, this.length);
84
- }
85
- }
86
-
87
- export {PackContext, UnpackContext};