@thi.ng/bitstream 2.2.35 → 2.2.37

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/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2023-12-09T19:12:03Z
3
+ - **Last updated**: 2023-12-18T13:41:19Z
4
4
  - **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
5
5
 
6
6
  All notable changes to this project will be documented in this file.
package/input.js CHANGED
@@ -1,116 +1,116 @@
1
1
  import { illegalArgs } from "@thi.ng/errors/illegal-arguments";
2
2
  import { illegalState } from "@thi.ng/errors/illegal-state";
3
3
  const U32 = Math.pow(2, 32);
4
- export class BitInputStream {
5
- buffer;
6
- start;
7
- limit;
8
- pos;
9
- bitPos;
10
- bit;
11
- constructor(buffer, offset = 0, limit = buffer.length << 3) {
12
- this.buffer = buffer;
13
- this.start = offset;
14
- this.limit = limit;
15
- this.seek(offset);
4
+ class BitInputStream {
5
+ buffer;
6
+ start;
7
+ limit;
8
+ pos;
9
+ bitPos;
10
+ bit;
11
+ constructor(buffer, offset = 0, limit = buffer.length << 3) {
12
+ this.buffer = buffer;
13
+ this.start = offset;
14
+ this.limit = limit;
15
+ this.seek(offset);
16
+ }
17
+ *[Symbol.iterator]() {
18
+ let j = this.start;
19
+ let i = j >>> 3;
20
+ let b = 7 - (j & 7);
21
+ while (j < this.limit) {
22
+ yield this.buffer[i] >>> b & 1;
23
+ if (--b < 0) {
24
+ i++;
25
+ b = 7;
26
+ }
27
+ j++;
16
28
  }
17
- *[Symbol.iterator]() {
18
- let j = this.start;
19
- let i = j >>> 3;
20
- let b = 7 - (j & 0x7);
21
- while (j < this.limit) {
22
- yield (this.buffer[i] >>> b) & 1;
23
- if (--b < 0) {
24
- i++;
25
- b = 7;
26
- }
27
- j++;
28
- }
29
+ }
30
+ get length() {
31
+ return this.limit;
32
+ }
33
+ get position() {
34
+ return this.bitPos;
35
+ }
36
+ seek(pos) {
37
+ if (pos < this.start || pos >= this.limit) {
38
+ illegalArgs(`seek pos out of bounds: ${pos}`);
29
39
  }
30
- get length() {
31
- return this.limit;
40
+ this.pos = pos >>> 3;
41
+ this.bit = 8 - (pos & 7);
42
+ this.bitPos = pos;
43
+ return this;
44
+ }
45
+ read(wordSize = 1) {
46
+ if (wordSize > 32) {
47
+ return this.read(wordSize - 32) * U32 + this.read(32);
48
+ } else if (wordSize > 8) {
49
+ let out = 0;
50
+ let n = wordSize & -8;
51
+ let msb = wordSize - n;
52
+ if (msb > 0) {
53
+ out = this._read(msb);
54
+ }
55
+ while (n > 0) {
56
+ out = (out << 8 | this._read(8)) >>> 0;
57
+ n -= 8;
58
+ }
59
+ return out;
60
+ } else {
61
+ return this._read(wordSize);
32
62
  }
33
- get position() {
34
- return this.bitPos;
63
+ }
64
+ readFields(fields) {
65
+ return fields.map((word) => this.read(word));
66
+ }
67
+ readWords(n, wordSize = 8) {
68
+ let out = [];
69
+ while (n-- > 0) {
70
+ out.push(this.read(wordSize));
35
71
  }
36
- seek(pos) {
37
- if (pos < this.start || pos >= this.limit) {
38
- illegalArgs(`seek pos out of bounds: ${pos}`);
39
- }
40
- this.pos = pos >>> 3;
41
- this.bit = 8 - (pos & 0x7);
42
- this.bitPos = pos;
43
- return this;
72
+ return out;
73
+ }
74
+ readStruct(fields) {
75
+ return fields.reduce((acc, [id, word]) => {
76
+ return acc[id] = this.read(word), acc;
77
+ }, {});
78
+ }
79
+ readBit() {
80
+ this.checkLimit(1);
81
+ this.bit--;
82
+ this.bitPos++;
83
+ let out = this.buffer[this.pos] >>> this.bit & 1;
84
+ if (this.bit === 0) {
85
+ this.pos++;
86
+ this.bit = 8;
44
87
  }
45
- read(wordSize = 1) {
46
- if (wordSize > 32) {
47
- return this.read(wordSize - 32) * U32 + this.read(32);
48
- }
49
- else if (wordSize > 8) {
50
- let out = 0;
51
- let n = wordSize & -8;
52
- let msb = wordSize - n;
53
- if (msb > 0) {
54
- out = this._read(msb);
55
- }
56
- while (n > 0) {
57
- out = ((out << 8) | this._read(8)) >>> 0;
58
- n -= 8;
59
- }
60
- return out;
61
- }
62
- else {
63
- return this._read(wordSize);
64
- }
88
+ return out;
89
+ }
90
+ _read(wordSize) {
91
+ this.checkLimit(wordSize);
92
+ let l = this.bit - wordSize, out;
93
+ if (l >= 0) {
94
+ this.bit = l;
95
+ out = this.buffer[this.pos] >>> l & (1 << wordSize) - 1;
96
+ if (l === 0) {
97
+ this.pos++;
98
+ this.bit = 8;
99
+ }
100
+ } else {
101
+ out = (this.buffer[this.pos++] & (1 << this.bit) - 1) << -l;
102
+ this.bit = 8 + l;
103
+ out = out | this.buffer[this.pos] >>> this.bit;
65
104
  }
66
- readFields(fields) {
67
- return fields.map((word) => this.read(word));
68
- }
69
- readWords(n, wordSize = 8) {
70
- let out = [];
71
- while (n-- > 0) {
72
- out.push(this.read(wordSize));
73
- }
74
- return out;
75
- }
76
- readStruct(fields) {
77
- return fields.reduce((acc, [id, word]) => {
78
- return (acc[id] = this.read(word)), acc;
79
- }, {});
80
- }
81
- readBit() {
82
- this.checkLimit(1);
83
- this.bit--;
84
- this.bitPos++;
85
- let out = (this.buffer[this.pos] >>> this.bit) & 1;
86
- if (this.bit === 0) {
87
- this.pos++;
88
- this.bit = 8;
89
- }
90
- return out;
91
- }
92
- _read(wordSize) {
93
- this.checkLimit(wordSize);
94
- let l = this.bit - wordSize, out;
95
- if (l >= 0) {
96
- this.bit = l;
97
- out = (this.buffer[this.pos] >>> l) & ((1 << wordSize) - 1);
98
- if (l === 0) {
99
- this.pos++;
100
- this.bit = 8;
101
- }
102
- }
103
- else {
104
- out = (this.buffer[this.pos++] & ((1 << this.bit) - 1)) << -l;
105
- this.bit = 8 + l;
106
- out = out | (this.buffer[this.pos] >>> this.bit);
107
- }
108
- this.bitPos += wordSize;
109
- return out;
110
- }
111
- checkLimit(requested) {
112
- if (this.bitPos + requested > this.limit) {
113
- illegalState(`can't read past EOF`);
114
- }
105
+ this.bitPos += wordSize;
106
+ return out;
107
+ }
108
+ checkLimit(requested) {
109
+ if (this.bitPos + requested > this.limit) {
110
+ illegalState(`can't read past EOF`);
115
111
  }
112
+ }
116
113
  }
114
+ export {
115
+ BitInputStream
116
+ };
package/output.js CHANGED
@@ -1,118 +1,108 @@
1
1
  import { illegalArgs } from "@thi.ng/errors/illegal-arguments";
2
2
  import { BitInputStream } from "./input.js";
3
- const DEFAULT_BUF_SIZE = 0x10;
3
+ const DEFAULT_BUF_SIZE = 16;
4
4
  const U32 = Math.pow(2, 32);
5
- export class BitOutputStream {
6
- buffer;
7
- start;
8
- pos;
9
- bit;
10
- bitPos;
11
- constructor(buffer, offset = 0) {
12
- this.buffer =
13
- typeof buffer === "undefined"
14
- ? new Uint8Array(DEFAULT_BUF_SIZE)
15
- : typeof buffer === "number"
16
- ? new Uint8Array(buffer)
17
- : buffer;
18
- this.start = offset;
19
- this.seek(offset);
20
- this.buffer[this.pos] &= ~((1 << this.bit) - 1);
5
+ class BitOutputStream {
6
+ buffer;
7
+ start;
8
+ pos;
9
+ bit;
10
+ bitPos;
11
+ constructor(buffer, offset = 0) {
12
+ this.buffer = typeof buffer === "undefined" ? new Uint8Array(DEFAULT_BUF_SIZE) : typeof buffer === "number" ? new Uint8Array(buffer) : buffer;
13
+ this.start = offset;
14
+ this.seek(offset);
15
+ this.buffer[this.pos] &= ~((1 << this.bit) - 1);
16
+ }
17
+ get position() {
18
+ return this.bitPos;
19
+ }
20
+ seek(pos) {
21
+ if (pos < this.start || pos >= this.buffer.length << 3) {
22
+ illegalArgs(`seek pos out of bounds: ${pos}`);
21
23
  }
22
- get position() {
23
- return this.bitPos;
24
+ this.pos = pos >>> 3;
25
+ this.bit = 8 - (pos & 7);
26
+ this.bitPos = pos;
27
+ return this;
28
+ }
29
+ bytes() {
30
+ return this.buffer.slice(0, this.pos + (this.bit & 7 ? 1 : 0));
31
+ }
32
+ reader(from = 0) {
33
+ return new BitInputStream(this.buffer, from, this.position);
34
+ }
35
+ write(x, wordSize = 1) {
36
+ if (wordSize > 32) {
37
+ let hi = Math.floor(x / U32);
38
+ this.write(hi, wordSize - 32);
39
+ this.write(x - hi * U32, 32);
40
+ } else if (wordSize > 8) {
41
+ let n = wordSize & -8;
42
+ let msb = wordSize - n;
43
+ if (msb > 0) {
44
+ this._write(x >>> n, msb);
45
+ }
46
+ n -= 8;
47
+ while (n >= 0) {
48
+ this._write(x >>> n, 8);
49
+ n -= 8;
50
+ }
51
+ } else {
52
+ this._write(x, wordSize);
24
53
  }
25
- seek(pos) {
26
- if (pos < this.start || pos >= this.buffer.length << 3) {
27
- illegalArgs(`seek pos out of bounds: ${pos}`);
28
- }
29
- this.pos = pos >>> 3;
30
- this.bit = 8 - (pos & 0x7);
31
- this.bitPos = pos;
32
- return this;
54
+ return this;
55
+ }
56
+ writeWords(input, wordSize = 8) {
57
+ let iter = input[Symbol.iterator]();
58
+ let v;
59
+ while (v = iter.next(), !v.done) {
60
+ this.write(v.value, wordSize);
33
61
  }
34
- bytes() {
35
- return this.buffer.slice(0, this.pos + (this.bit & 7 ? 1 : 0));
62
+ }
63
+ writeBit(x) {
64
+ this.bit--;
65
+ this.buffer[this.pos] = this.buffer[this.pos] & ~(1 << this.bit) | x << this.bit;
66
+ if (this.bit === 0) {
67
+ this.ensureSize();
68
+ this.bit = 8;
36
69
  }
37
- reader(from = 0) {
38
- return new BitInputStream(this.buffer, from, this.position);
70
+ this.bitPos++;
71
+ return this;
72
+ }
73
+ _write(x, wordSize) {
74
+ x &= (1 << wordSize) - 1;
75
+ let buf = this.buffer;
76
+ let pos = this.pos;
77
+ let bit = this.bit;
78
+ let b = bit - wordSize;
79
+ let m = bit < 8 ? ~((1 << bit) - 1) : 0;
80
+ if (b >= 0) {
81
+ m |= (1 << b) - 1;
82
+ buf[pos] = buf[pos] & m | x << b & ~m;
83
+ if (b === 0) {
84
+ this.ensureSize();
85
+ this.bit = 8;
86
+ } else {
87
+ this.bit = b;
88
+ }
89
+ } else {
90
+ this.bit = bit = 8 + b;
91
+ buf[pos] = buf[pos] & m | x >>> -b & ~m;
92
+ this.ensureSize();
93
+ this.buffer[this.pos] = this.buffer[this.pos] & (1 << bit) - 1 | x << bit & 255;
39
94
  }
40
- write(x, wordSize = 1) {
41
- if (wordSize > 32) {
42
- let hi = Math.floor(x / U32);
43
- this.write(hi, wordSize - 32);
44
- this.write(x - hi * U32, 32);
45
- }
46
- else if (wordSize > 8) {
47
- let n = wordSize & -8;
48
- let msb = wordSize - n;
49
- if (msb > 0) {
50
- this._write(x >>> n, msb);
51
- }
52
- n -= 8;
53
- while (n >= 0) {
54
- this._write(x >>> n, 8);
55
- n -= 8;
56
- }
57
- }
58
- else {
59
- this._write(x, wordSize);
60
- }
61
- return this;
62
- }
63
- writeWords(input, wordSize = 8) {
64
- let iter = input[Symbol.iterator]();
65
- let v;
66
- while (((v = iter.next()), !v.done)) {
67
- this.write(v.value, wordSize);
68
- }
69
- }
70
- writeBit(x) {
71
- this.bit--;
72
- this.buffer[this.pos] =
73
- (this.buffer[this.pos] & ~(1 << this.bit)) | (x << this.bit);
74
- if (this.bit === 0) {
75
- this.ensureSize();
76
- //this.buffer[this.pos] = 0;
77
- this.bit = 8;
78
- }
79
- this.bitPos++;
80
- return this;
81
- }
82
- _write(x, wordSize) {
83
- x &= (1 << wordSize) - 1;
84
- let buf = this.buffer;
85
- let pos = this.pos;
86
- let bit = this.bit;
87
- let b = bit - wordSize;
88
- let m = bit < 8 ? ~((1 << bit) - 1) : 0;
89
- if (b >= 0) {
90
- m |= (1 << b) - 1;
91
- buf[pos] = (buf[pos] & m) | ((x << b) & ~m);
92
- if (b === 0) {
93
- this.ensureSize();
94
- this.bit = 8;
95
- }
96
- else {
97
- this.bit = b;
98
- }
99
- }
100
- else {
101
- this.bit = bit = 8 + b;
102
- buf[pos] = (buf[pos] & m) | ((x >>> -b) & ~m);
103
- this.ensureSize();
104
- this.buffer[this.pos] =
105
- (this.buffer[this.pos] & ((1 << bit) - 1)) |
106
- ((x << bit) & 0xff);
107
- }
108
- this.bitPos += wordSize;
109
- return this;
110
- }
111
- ensureSize() {
112
- if (++this.pos === this.buffer.length) {
113
- let b = new Uint8Array(this.buffer.length << 1);
114
- b.set(this.buffer);
115
- this.buffer = b;
116
- }
95
+ this.bitPos += wordSize;
96
+ return this;
97
+ }
98
+ ensureSize() {
99
+ if (++this.pos === this.buffer.length) {
100
+ let b = new Uint8Array(this.buffer.length << 1);
101
+ b.set(this.buffer);
102
+ this.buffer = b;
117
103
  }
104
+ }
118
105
  }
106
+ export {
107
+ BitOutputStream
108
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/bitstream",
3
- "version": "2.2.35",
3
+ "version": "2.2.37",
4
4
  "description": "ES6 iterator based read/write bit streams with support for variable word widths",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -24,7 +24,9 @@
24
24
  "author": "Karsten Schmidt (https://thi.ng)",
25
25
  "license": "Apache-2.0",
26
26
  "scripts": {
27
- "build": "yarn clean && tsc --declaration",
27
+ "build": "yarn build:esbuild && yarn build:decl",
28
+ "build:decl": "tsc --declaration --emitDeclarationOnly",
29
+ "build:esbuild": "esbuild --format=esm --platform=neutral --target=es2022 --tsconfig=tsconfig.json --outdir=. src/**/*.ts",
28
30
  "clean": "rimraf --glob '*.js' '*.d.ts' '*.map' doc",
29
31
  "doc": "typedoc --excludePrivate --excludeInternal --out doc src/index.ts",
30
32
  "doc:ae": "mkdir -p .ae/doc .ae/temp && api-extractor run --local --verbose",
@@ -33,10 +35,11 @@
33
35
  "test": "bun test"
34
36
  },
35
37
  "dependencies": {
36
- "@thi.ng/errors": "^2.4.5"
38
+ "@thi.ng/errors": "^2.4.7"
37
39
  },
38
40
  "devDependencies": {
39
41
  "@microsoft/api-extractor": "^7.38.3",
42
+ "esbuild": "^0.19.8",
40
43
  "rimraf": "^5.0.5",
41
44
  "tools": "^0.0.1",
42
45
  "typedoc": "^0.25.4",
@@ -53,7 +56,7 @@
53
56
  "access": "public"
54
57
  },
55
58
  "engines": {
56
- "node": ">=12.7"
59
+ "node": ">=18"
57
60
  },
58
61
  "files": [
59
62
  "./*.js",
@@ -79,5 +82,5 @@
79
82
  "rle-pack"
80
83
  ]
81
84
  },
82
- "gitHead": "25f2ac8ff795a432a930119661b364d4d93b59a0\n"
85
+ "gitHead": "25a42a81fac8603a1e440a7aa8bc343276211ff4\n"
83
86
  }
package/simple.js CHANGED
@@ -1,79 +1,60 @@
1
- /**
2
- * Barebones alternative to {@link BitOutputStream} for word sizes <= 8 and with
3
- * minimal API surface. The returned object only exposes 2 functions:
4
- *
5
- * - `write(x, size)` - writes a single value of given bit size (default: 1 bit)
6
- * - `bytes()` - retrieve all bytes written so far
7
- *
8
- * @remarks
9
- * The internal backing buffer automatically resizes on demand. The optionally
10
- * provided `capacity` is only the initial buffer size.
11
- *
12
- * @param capacity - initial capacity
13
- */
14
- export const bitWriter = (capacity = 16) => {
15
- let buf = new Uint8Array(capacity);
16
- let pos = 0;
17
- let bit = 8;
18
- const ensure = () => {
19
- if (++pos === buf.length) {
20
- let b = new Uint8Array(buf.length << 1);
21
- b.set(buf);
22
- buf = b;
1
+ const bitWriter = (capacity = 16) => {
2
+ let buf = new Uint8Array(capacity);
3
+ let pos = 0;
4
+ let bit = 8;
5
+ const ensure = () => {
6
+ if (++pos === buf.length) {
7
+ let b = new Uint8Array(buf.length << 1);
8
+ b.set(buf);
9
+ buf = b;
10
+ }
11
+ };
12
+ return {
13
+ write: (x, n = 1) => {
14
+ x &= (1 << n) - 1;
15
+ let b = bit - n;
16
+ let m = bit < 8 ? ~((1 << bit) - 1) : 0;
17
+ if (b >= 0) {
18
+ m |= (1 << b) - 1;
19
+ buf[pos] = buf[pos] & m | x << b & ~m;
20
+ if (b === 0) {
21
+ ensure();
22
+ bit = 8;
23
+ } else {
24
+ bit = b;
23
25
  }
24
- };
25
- return {
26
- write: (x, n = 1) => {
27
- x &= (1 << n) - 1;
28
- let b = bit - n;
29
- let m = bit < 8 ? ~((1 << bit) - 1) : 0;
30
- if (b >= 0) {
31
- m |= (1 << b) - 1;
32
- buf[pos] = (buf[pos] & m) | ((x << b) & ~m);
33
- if (b === 0) {
34
- ensure();
35
- bit = 8;
36
- }
37
- else {
38
- bit = b;
39
- }
40
- }
41
- else {
42
- bit = 8 + b;
43
- buf[pos] = (buf[pos] & m) | ((x >>> -b) & ~m);
44
- ensure();
45
- buf[pos] = (buf[pos] & ((1 << bit) - 1)) | ((x << bit) & 0xff);
46
- }
47
- },
48
- bytes: () => buf.slice(0, pos + (bit & 7 ? 1 : 0)),
49
- };
26
+ } else {
27
+ bit = 8 + b;
28
+ buf[pos] = buf[pos] & m | x >>> -b & ~m;
29
+ ensure();
30
+ buf[pos] = buf[pos] & (1 << bit) - 1 | x << bit & 255;
31
+ }
32
+ },
33
+ bytes: () => buf.slice(0, pos + (bit & 7 ? 1 : 0))
34
+ };
50
35
  };
51
- /**
52
- * Barebones alternative to {@link BitInputStream} for word sizes <= 8 and with
53
- * minimal API surface and WITHOUT bounds checking of any form! The returned
54
- * function reads `n` bits from the originally provided buffer.
55
- *
56
- * @param buf
57
- */
58
- export const bitReader = (buf) => {
59
- let p = 0;
60
- let b = 8;
61
- return (n = 1) => {
62
- let l = b - n;
63
- let out;
64
- if (l >= 0) {
65
- b = l;
66
- out = (buf[p] >>> l) & ((1 << n) - 1);
67
- if (!l) {
68
- p++;
69
- b = 8;
70
- }
71
- }
72
- else {
73
- out = (buf[p++] & ((1 << b) - 1)) << -l;
74
- b = 8 + l;
75
- out = out | (buf[p] >>> b);
76
- }
77
- return out;
78
- };
36
+ const bitReader = (buf) => {
37
+ let p = 0;
38
+ let b = 8;
39
+ return (n = 1) => {
40
+ let l = b - n;
41
+ let out;
42
+ if (l >= 0) {
43
+ b = l;
44
+ out = buf[p] >>> l & (1 << n) - 1;
45
+ if (!l) {
46
+ p++;
47
+ b = 8;
48
+ }
49
+ } else {
50
+ out = (buf[p++] & (1 << b) - 1) << -l;
51
+ b = 8 + l;
52
+ out = out | buf[p] >>> b;
53
+ }
54
+ return out;
55
+ };
56
+ };
57
+ export {
58
+ bitReader,
59
+ bitWriter
79
60
  };