@thi.ng/bitstream 2.1.7 → 2.2.2
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 +8 -1
- package/README.md +32 -1
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/package.json +8 -5
- package/simple.d.ts +26 -0
- package/simple.js +79 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
-
- **Last updated**: 2022-
|
|
3
|
+
- **Last updated**: 2022-08-01T14:53:59Z
|
|
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.
|
|
@@ -9,6 +9,13 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
|
|
|
9
9
|
**Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
|
|
10
10
|
and/or version bumps of transitive dependencies.
|
|
11
11
|
|
|
12
|
+
## [2.2.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/bitstream@2.2.0) (2022-07-06)
|
|
13
|
+
|
|
14
|
+
#### 🚀 Features
|
|
15
|
+
|
|
16
|
+
- add barebones bitReader/Writer() ([e17dff9](https://github.com/thi-ng/umbrella/commit/e17dff9))
|
|
17
|
+
- update pkg & readme
|
|
18
|
+
|
|
12
19
|
## [2.1.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/bitstream@2.1.0) (2021-11-17)
|
|
13
20
|
|
|
14
21
|
#### 🚀 Features
|
package/README.md
CHANGED
|
@@ -17,6 +17,7 @@ This project is part of the
|
|
|
17
17
|
- [API](#api)
|
|
18
18
|
- [BitOutputStream](#bitoutputstream)
|
|
19
19
|
- [BitInputStream](#bitinputstream)
|
|
20
|
+
- [Barebones alternatives](#barebones-alternatives)
|
|
20
21
|
- [Authors](#authors)
|
|
21
22
|
- [License](#license)
|
|
22
23
|
|
|
@@ -58,7 +59,7 @@ node --experimental-repl-await
|
|
|
58
59
|
> const bitstream = await import("@thi.ng/bitstream");
|
|
59
60
|
```
|
|
60
61
|
|
|
61
|
-
Package sizes (gzipped, pre-treeshake): ESM: 1.
|
|
62
|
+
Package sizes (gzipped, pre-treeshake): ESM: 1.31 KB
|
|
62
63
|
|
|
63
64
|
## Dependencies
|
|
64
65
|
|
|
@@ -164,6 +165,36 @@ input.read(7)
|
|
|
164
165
|
In addition to the generic `read()` method, there's also the slightly
|
|
165
166
|
faster `readBit()` for reading single bits.
|
|
166
167
|
|
|
168
|
+
### Barebones alternatives
|
|
169
|
+
|
|
170
|
+
For use cases requiring only word sizes <=8 bits and none of the advanced features provided by the above implementations, the package also provides functional barebones alternatives in the form of [`bitWriter()`](https://docs.thi.ng/umbrella/bitstream/modules.html#bitWriter) and [`bitReader()`](https://docs.thi.ng/umbrella/bitstream/modules.html#bitReader):
|
|
171
|
+
|
|
172
|
+
```ts
|
|
173
|
+
import { bitReader, bitWriter } from "@thi.ng/bistream";
|
|
174
|
+
|
|
175
|
+
const writer = bitWriter();
|
|
176
|
+
// write single bit
|
|
177
|
+
writer.write(1);
|
|
178
|
+
|
|
179
|
+
// write unsigned value (up to 8 bits)
|
|
180
|
+
writer.write(31, 5);
|
|
181
|
+
|
|
182
|
+
// retrieve buffer
|
|
183
|
+
const bytes = writer.bytes();
|
|
184
|
+
// Uint8Array(1) [ 252 ]
|
|
185
|
+
|
|
186
|
+
// create reader from byte buffer
|
|
187
|
+
const reader = bitReader(bytes);
|
|
188
|
+
|
|
189
|
+
// read single bit
|
|
190
|
+
reader();
|
|
191
|
+
// 1
|
|
192
|
+
|
|
193
|
+
// read n-bit unsigned value
|
|
194
|
+
reader(5);
|
|
195
|
+
// 31
|
|
196
|
+
```
|
|
197
|
+
|
|
167
198
|
## Authors
|
|
168
199
|
|
|
169
200
|
Karsten Schmidt
|
package/index.d.ts
CHANGED
package/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/bitstream",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.2.2",
|
|
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",
|
|
@@ -34,15 +34,15 @@
|
|
|
34
34
|
"test": "testament test"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@thi.ng/errors": "^2.1.
|
|
37
|
+
"@thi.ng/errors": "^2.1.9"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"@microsoft/api-extractor": "^7.25.0",
|
|
41
|
-
"@thi.ng/testament": "^0.2.
|
|
41
|
+
"@thi.ng/testament": "^0.2.10",
|
|
42
42
|
"rimraf": "^3.0.2",
|
|
43
43
|
"tools": "^0.0.1",
|
|
44
44
|
"typedoc": "^0.22.17",
|
|
45
|
-
"typescript": "^4.7.
|
|
45
|
+
"typescript": "^4.7.4"
|
|
46
46
|
},
|
|
47
47
|
"keywords": [
|
|
48
48
|
"binary",
|
|
@@ -70,6 +70,9 @@
|
|
|
70
70
|
},
|
|
71
71
|
"./output": {
|
|
72
72
|
"default": "./output.js"
|
|
73
|
+
},
|
|
74
|
+
"./simple": {
|
|
75
|
+
"default": "./simple.js"
|
|
73
76
|
}
|
|
74
77
|
},
|
|
75
78
|
"thi.ng": {
|
|
@@ -78,5 +81,5 @@
|
|
|
78
81
|
"rle-pack"
|
|
79
82
|
]
|
|
80
83
|
},
|
|
81
|
-
"gitHead": "
|
|
84
|
+
"gitHead": "976ccd698cedaa60dcef2e69030a5eb98898cc4a\n"
|
|
82
85
|
}
|
package/simple.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
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 declare const bitWriter: (capacity?: number) => {
|
|
15
|
+
write: (x: number, n?: number) => void;
|
|
16
|
+
bytes: () => Uint8Array;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Barebones alternative to {@link BitInputStream} for word sizes <= 8 and with
|
|
20
|
+
* minimal API surface and WITHOUT bounds checking of any form! The returned
|
|
21
|
+
* function reads `n` bits from the originally provided buffer.
|
|
22
|
+
*
|
|
23
|
+
* @param buf
|
|
24
|
+
*/
|
|
25
|
+
export declare const bitReader: (buf: Uint8Array | number[]) => (n?: number) => number;
|
|
26
|
+
//# sourceMappingURL=simple.d.ts.map
|
package/simple.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
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;
|
|
23
|
+
}
|
|
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
|
+
};
|
|
50
|
+
};
|
|
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
|
+
};
|
|
79
|
+
};
|