@ac-kit/core 0.2.0 → 0.4.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.
Files changed (85) hide show
  1. package/CHANGELOG.md +65 -0
  2. package/README.md +1 -1
  3. package/dist/big-int/from-bytes-be.d.ts +14 -0
  4. package/dist/big-int/from-bytes-be.d.ts.map +1 -0
  5. package/dist/big-int/from-bytes-be.js +20 -0
  6. package/dist/big-int/from-bytes-be.js.map +1 -0
  7. package/dist/big-int/from-bytes-le.d.ts +15 -0
  8. package/dist/big-int/from-bytes-le.d.ts.map +1 -0
  9. package/dist/big-int/from-bytes-le.js +21 -0
  10. package/dist/big-int/from-bytes-le.js.map +1 -0
  11. package/dist/big-int/to-bytes-be.d.ts +19 -0
  12. package/dist/big-int/to-bytes-be.d.ts.map +1 -0
  13. package/dist/big-int/to-bytes-be.js +60 -0
  14. package/dist/big-int/to-bytes-be.js.map +1 -0
  15. package/dist/big-int/to-bytes-le.d.ts +19 -0
  16. package/dist/big-int/to-bytes-le.d.ts.map +1 -0
  17. package/dist/big-int/to-bytes-le.js +22 -0
  18. package/dist/big-int/to-bytes-le.js.map +1 -0
  19. package/dist/buffer/bytes-combine.d.ts +18 -0
  20. package/dist/buffer/bytes-combine.d.ts.map +1 -0
  21. package/dist/buffer/bytes-combine.js +25 -0
  22. package/dist/buffer/bytes-combine.js.map +1 -0
  23. package/dist/buffer/bytes-map.d.ts +14 -0
  24. package/dist/buffer/bytes-map.d.ts.map +1 -0
  25. package/dist/buffer/bytes-map.js +21 -0
  26. package/dist/buffer/bytes-map.js.map +1 -0
  27. package/dist/buffer/text/decode-text.d.ts +13 -4
  28. package/dist/buffer/text/decode-text.d.ts.map +1 -1
  29. package/dist/buffer/text/decode-text.js +16 -13
  30. package/dist/buffer/text/decode-text.js.map +1 -1
  31. package/dist/buffer/text/is-valid-text.d.ts +25 -0
  32. package/dist/buffer/text/is-valid-text.d.ts.map +1 -0
  33. package/dist/buffer/text/is-valid-text.js +36 -0
  34. package/dist/buffer/text/is-valid-text.js.map +1 -0
  35. package/dist/buffer/text/next-utf8-boundary.d.ts +17 -0
  36. package/dist/buffer/text/next-utf8-boundary.d.ts.map +1 -0
  37. package/dist/buffer/text/next-utf8-boundary.js +27 -0
  38. package/dist/buffer/text/next-utf8-boundary.js.map +1 -0
  39. package/dist/buffer/text/text-encoding-name.d.ts +11 -0
  40. package/dist/buffer/text/text-encoding-name.d.ts.map +1 -1
  41. package/dist/index.d.ts +15 -3
  42. package/dist/index.d.ts.map +1 -1
  43. package/dist/index.js +15 -3
  44. package/dist/index.js.map +1 -1
  45. package/dist/object/flatten-record.d.ts +67 -0
  46. package/dist/object/flatten-record.d.ts.map +1 -0
  47. package/dist/object/flatten-record.js +81 -0
  48. package/dist/object/flatten-record.js.map +1 -0
  49. package/dist/object/unflatten-record.d.ts +63 -0
  50. package/dist/object/unflatten-record.d.ts.map +1 -0
  51. package/dist/object/unflatten-record.js +71 -0
  52. package/dist/object/unflatten-record.js.map +1 -0
  53. package/dist/string/ascii/is-ascii-blank.d.ts +8 -0
  54. package/dist/string/ascii/is-ascii-blank.d.ts.map +1 -0
  55. package/dist/string/ascii/is-ascii-blank.js +11 -0
  56. package/dist/string/ascii/is-ascii-blank.js.map +1 -0
  57. package/dist/string/ascii/is-ascii-control.d.ts +6 -0
  58. package/dist/string/ascii/is-ascii-control.d.ts.map +1 -0
  59. package/dist/string/ascii/is-ascii-control.js +9 -0
  60. package/dist/string/ascii/is-ascii-control.js.map +1 -0
  61. package/dist/string/ascii/is-ascii-graphic.d.ts +7 -0
  62. package/dist/string/ascii/is-ascii-graphic.d.ts.map +1 -0
  63. package/dist/string/ascii/is-ascii-graphic.js +10 -0
  64. package/dist/string/ascii/is-ascii-graphic.js.map +1 -0
  65. package/dist/string/ascii/is-ascii-printable.d.ts +7 -0
  66. package/dist/string/ascii/is-ascii-printable.d.ts.map +1 -0
  67. package/dist/string/ascii/is-ascii-printable.js +10 -0
  68. package/dist/string/ascii/is-ascii-printable.js.map +1 -0
  69. package/dist/string/ascii/is-ascii-punctuation.d.ts +6 -0
  70. package/dist/string/ascii/is-ascii-punctuation.d.ts.map +1 -0
  71. package/dist/string/ascii/is-ascii-punctuation.js +10 -0
  72. package/dist/string/ascii/is-ascii-punctuation.js.map +1 -0
  73. package/package.json +8 -8
  74. package/dist/system/net/ephemeral-port.d.ts +0 -13
  75. package/dist/system/net/ephemeral-port.d.ts.map +0 -1
  76. package/dist/system/net/ephemeral-port.js +0 -19
  77. package/dist/system/net/ephemeral-port.js.map +0 -1
  78. package/dist/system/net/inet-address.d.ts +0 -20
  79. package/dist/system/net/inet-address.d.ts.map +0 -1
  80. package/dist/system/net/inet-address.js +0 -42
  81. package/dist/system/net/inet-address.js.map +0 -1
  82. package/dist/system/net/inet-endpoint.d.ts +0 -7
  83. package/dist/system/net/inet-endpoint.d.ts.map +0 -1
  84. package/dist/system/net/inet-endpoint.js +0 -2
  85. package/dist/system/net/inet-endpoint.js.map +0 -1
package/CHANGELOG.md CHANGED
@@ -1,5 +1,70 @@
1
1
  # @ac-kit/core
2
2
 
3
+ ## 0.4.0
4
+
5
+ ### Minor Changes
6
+
7
+ - aca1cb1: `decodeText` also reads `"windows-1252"` and `"shift_jis"`, the two legacy
8
+ encodings named by the new `TextDecodingName`; `encodeText` cannot write them.
9
+ `isValidText` tells whether bytes are valid text in an encoding, to test
10
+ candidate encodings without keeping the decoded string.
11
+
12
+ ## 0.3.0
13
+
14
+ ### Minor Changes
15
+
16
+ - a5c4d66: **Breaking:** `bigIntToBytesBe` and `bigIntFromBytesBe` move from
17
+ `@ac-kit/math-integer` to `@ac-kit/core`. Update the import specifier; the
18
+ behaviour is unchanged.
19
+
20
+ They are a wire encoding rather than arithmetic — X.690 §8.3 for an ASN.1
21
+ INTEGER, and what `BigInteger.toByteArray` and `int.to_bytes` produce — and
22
+ their callers need no integer theory at all, while `math-integer` sits too high
23
+ in the dependency graph for some of them to reach it.
24
+
25
+ New in `@ac-kit/core`: `bigIntToBytesLe` and `bigIntFromBytesLe`, the same
26
+ two's-complement, minimal-or-fixed-width encoding with the least significant
27
+ byte first.
28
+
29
+ `@ac-kit/format-asn1` no longer depends on `@ac-kit/math-integer`.
30
+ - a5c4d66: - **Breaking:** `InetAddress`, `InetEndpoint` and `composeInetAddress` are gone,
31
+ and so are `EPHEMERAL_PORT_MIN_VALUE`, `EPHEMERAL_PORT_MAX_VALUE` and
32
+ `getRandomEphemeralPort`. `core` holds language extensions and wrappers over
33
+ simple Web APIs, and `ARCHITECTURE.md` §3 names IP addressing as the example
34
+ of something pure that is nonetheless domain knowledge. The first three are
35
+ now `@ac-kit/node`'s, where `composeInetAddress` is spelled `toInetAddress`;
36
+ the ephemeral port range is `@ac-kit/net-address`'s, beside the rest of the
37
+ RFC 6335 port semantics. No compatibility re-export is left behind.
38
+ - New `flattenRecord(source, options)` and `unflattenRecord(source, options)`,
39
+ collapsing a nested record to delimiter-joined keys and back. A multi-segment
40
+ path can never reach `__proto__`, `constructor` or `prototype`; a
41
+ single-segment key is a leaf and is kept as one, because a record of
42
+ environment variables should still be able to hold one called `constructor`.
43
+ An array is a leaf value by default rather than being numbered, since an index
44
+ is the one segment a flattened key cannot tell apart from an ordinary key
45
+ spelled `"0"`. A key that itself contains the delimiter is not escaped and the
46
+ delimiter wins on the way back, so the round trip holds exactly for keys that
47
+ do not contain one.
48
+ - New `bytesMap(bytes, map)` and `bytesCombine(a, b, combine)`, applying a
49
+ function to every octet of one array or to every octet pair of two. Both mask
50
+ the callback's result to a single octet, so `~byte` and `left & right` read as
51
+ the bitwise operations they are rather than trailing an `& 0xff` that is easy
52
+ to forget and silent when it is. `bytesCombine` throws on mismatched lengths
53
+ instead of pairing up to the shorter array: a bitwise operation over two
54
+ different widths has no meaning, and truncating to the shorter one turns that
55
+ into a wrong answer rather than an error.
56
+ - New `nextUtf8Boundary(bytes, index)`, the first UTF-8 character boundary at or
57
+ after `index`. Slicing a UTF-8 buffer at an arbitrary offset — a tail, a
58
+ window, a chunk handed to `decodeText` — can land inside a multi-byte
59
+ sequence, whose orphaned tail decodes to a leading U+FFFD; aligning the offset
60
+ forward drops the partial character instead.
61
+ - Five more ASCII predicates: `isAsciiControl`, `isAsciiGraphic`,
62
+ `isAsciiPrintable`, `isAsciiPunctuation` and `isAsciiBlank` — C's `iscntrl`,
63
+ `isgraph`, `isprint`, `ispunct` and `isblank`, joining the `isalpha`/`isdigit`
64
+ family already here. Where `punct` stops and `graph` starts is exactly the kind
65
+ of boundary a hand-written range table gets subtly wrong, and every copy of it
66
+ gets it wrong differently.
67
+
3
68
  ## 0.2.0
4
69
 
5
70
  ### Minor Changes
package/README.md CHANGED
@@ -35,5 +35,5 @@ Portable: no Node.js built-ins, so it runs unchanged in browsers, Deno, Bun and
35
35
  edge runtimes. The host-bound counterpart is `@ac-kit/node`.
36
36
 
37
37
  See the
38
- [generated API docs](https://anthochamp.github.io/node-essentials/api/core/)
38
+ [generated API docs](https://anthochamp.pages.grenadine.io/node-essentials/api/core/)
39
39
  for the full reference.
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Decodes two's complement, big-endian bytes back into a `bigint` — the inverse
3
+ * of {@link bigIntToBytesBe}.
4
+ *
5
+ * The high bit of the first byte is the sign, so any width round-trips: `0xff`
6
+ * and `0xff_ff` both decode to `-1`. Reach for {@link bigIntFromBytesLe} when
7
+ * the least significant byte comes first.
8
+ *
9
+ * Time complexity: O(n) in the byte count.
10
+ *
11
+ * @param bytes - The encoding. An empty input decodes to zero.
12
+ */
13
+ export declare function bigIntFromBytesBe(bytes: Uint8Array): bigint;
14
+ //# sourceMappingURL=from-bytes-be.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"from-bytes-be.d.ts","sourceRoot":"","sources":["../../src/big-int/from-bytes-be.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAQ3D"}
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Decodes two's complement, big-endian bytes back into a `bigint` — the inverse
3
+ * of {@link bigIntToBytesBe}.
4
+ *
5
+ * The high bit of the first byte is the sign, so any width round-trips: `0xff`
6
+ * and `0xff_ff` both decode to `-1`. Reach for {@link bigIntFromBytesLe} when
7
+ * the least significant byte comes first.
8
+ *
9
+ * Time complexity: O(n) in the byte count.
10
+ *
11
+ * @param bytes - The encoding. An empty input decodes to zero.
12
+ */
13
+ export function bigIntFromBytesBe(bytes) {
14
+ let value = 0n;
15
+ for (let index = 0; index < bytes.length; index++) {
16
+ value = (value << 8n) | BigInt(bytes[index]);
17
+ }
18
+ return BigInt.asIntN(bytes.length * 8, value);
19
+ }
20
+ //# sourceMappingURL=from-bytes-be.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"from-bytes-be.js","sourceRoot":"","sources":["../../src/big-int/from-bytes-be.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,iBAAiB,CAAC,KAAiB;IAClD,IAAI,KAAK,GAAG,EAAE,CAAC;IAEf,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,KAAK,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC;QACnD,KAAK,GAAG,CAAC,KAAK,IAAI,EAAE,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC,KAAK,CAAE,CAAC,CAAC;IAC/C,CAAC;IAED,OAAO,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,KAAK,CAAC,CAAC;AAC/C,CAAC"}
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Decodes two's complement, little-endian bytes back into a `bigint` — the
3
+ * inverse of {@link bigIntToBytesLe}.
4
+ *
5
+ * The high bit of the _last_ byte is the sign, so any width round-trips: `0xff`
6
+ * and `0xff_ff` both decode to `-1`. Reach for {@link bigIntFromBytesBe} when
7
+ * the most significant byte comes first, which is what wire formats usually
8
+ * specify.
9
+ *
10
+ * Time complexity: O(n) in the byte count.
11
+ *
12
+ * @param bytes - The encoding. An empty input decodes to zero.
13
+ */
14
+ export declare function bigIntFromBytesLe(bytes: Uint8Array): bigint;
15
+ //# sourceMappingURL=from-bytes-le.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"from-bytes-le.d.ts","sourceRoot":"","sources":["../../src/big-int/from-bytes-le.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAQ3D"}
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Decodes two's complement, little-endian bytes back into a `bigint` — the
3
+ * inverse of {@link bigIntToBytesLe}.
4
+ *
5
+ * The high bit of the _last_ byte is the sign, so any width round-trips: `0xff`
6
+ * and `0xff_ff` both decode to `-1`. Reach for {@link bigIntFromBytesBe} when
7
+ * the most significant byte comes first, which is what wire formats usually
8
+ * specify.
9
+ *
10
+ * Time complexity: O(n) in the byte count.
11
+ *
12
+ * @param bytes - The encoding. An empty input decodes to zero.
13
+ */
14
+ export function bigIntFromBytesLe(bytes) {
15
+ let value = 0n;
16
+ for (let index = bytes.length - 1; index >= 0; index--) {
17
+ value = (value << 8n) | BigInt(bytes[index]);
18
+ }
19
+ return BigInt.asIntN(bytes.length * 8, value);
20
+ }
21
+ //# sourceMappingURL=from-bytes-le.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"from-bytes-le.js","sourceRoot":"","sources":["../../src/big-int/from-bytes-le.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,iBAAiB,CAAC,KAAiB;IAClD,IAAI,KAAK,GAAG,EAAE,CAAC;IAEf,KAAK,IAAI,KAAK,GAAG,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,KAAK,IAAI,CAAC,EAAE,KAAK,EAAE,EAAE,CAAC;QACxD,KAAK,GAAG,CAAC,KAAK,IAAI,EAAE,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC,KAAK,CAAE,CAAC,CAAC;IAC/C,CAAC;IAED,OAAO,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,KAAK,CAAC,CAAC;AAC/C,CAAC"}
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Encodes a `bigint` as two's complement, big-endian bytes.
3
+ *
4
+ * What Java's `BigInteger.toByteArray` and Python's `int.to_bytes` produce.
5
+ * Reach for {@link bigIntToBytesLe} when the least significant byte comes
6
+ * first, and for `@ac-kit/math-integer`'s `limb32`/`limb64` families when the
7
+ * digits feed arithmetic rather than a wire: those are little-endian, unsigned,
8
+ * and of fixed width.
9
+ *
10
+ * Time complexity: O(n) in the byte count.
11
+ *
12
+ * @param value - The value to encode. Zero encodes as a single `0x00`.
13
+ * @param byteLength - Fixed output width, sign-extended to fit. Omit for the
14
+ * minimal encoding.
15
+ * @returns A fresh buffer, never empty.
16
+ * @throws {RangeError} When `byteLength` is too small to hold `value`.
17
+ */
18
+ export declare function bigIntToBytesBe(value: bigint, byteLength?: number): Uint8Array<ArrayBuffer>;
19
+ //# sourceMappingURL=to-bytes-be.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"to-bytes-be.d.ts","sourceRoot":"","sources":["../../src/big-int/to-bytes-be.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,eAAe,CAC9B,KAAK,EAAE,MAAM,EACb,UAAU,CAAC,EAAE,MAAM,GACjB,UAAU,CAAC,WAAW,CAAC,CAqBzB"}
@@ -0,0 +1,60 @@
1
+ /**
2
+ * Encodes a `bigint` as two's complement, big-endian bytes.
3
+ *
4
+ * What Java's `BigInteger.toByteArray` and Python's `int.to_bytes` produce.
5
+ * Reach for {@link bigIntToBytesLe} when the least significant byte comes
6
+ * first, and for `@ac-kit/math-integer`'s `limb32`/`limb64` families when the
7
+ * digits feed arithmetic rather than a wire: those are little-endian, unsigned,
8
+ * and of fixed width.
9
+ *
10
+ * Time complexity: O(n) in the byte count.
11
+ *
12
+ * @param value - The value to encode. Zero encodes as a single `0x00`.
13
+ * @param byteLength - Fixed output width, sign-extended to fit. Omit for the
14
+ * minimal encoding.
15
+ * @returns A fresh buffer, never empty.
16
+ * @throws {RangeError} When `byteLength` is too small to hold `value`.
17
+ */
18
+ export function bigIntToBytesBe(value, byteLength) {
19
+ const minimal = toMinimalBytes(value);
20
+ if (byteLength === undefined) {
21
+ return minimal;
22
+ }
23
+ if (minimal.length > byteLength) {
24
+ throw new RangeError(`bigIntToBytesBe: ${value} needs ${minimal.length} bytes, not ${byteLength}`);
25
+ }
26
+ const out = new Uint8Array(byteLength);
27
+ const padding = byteLength - minimal.length;
28
+ // Sign extension, not zero padding: a negative value's high bytes are 0xff.
29
+ out.fill(value < 0n ? 0xff : 0x00, 0, padding);
30
+ out.set(minimal, padding);
31
+ return out;
32
+ }
33
+ function toMinimalBytes(value) {
34
+ if (value === 0n) {
35
+ return new Uint8Array([0x00]);
36
+ }
37
+ const negative = value < 0n;
38
+ // For a negative, encode |value| - 1 and invert: that is two's complement
39
+ // without ever needing to know the final width up front.
40
+ let remaining = negative ? -value - 1n : value;
41
+ const digits = [];
42
+ while (remaining > 0n) {
43
+ digits.unshift(Number(remaining & 0xffn));
44
+ remaining >>= 8n;
45
+ }
46
+ if (negative) {
47
+ for (let index = 0; index < digits.length; index++) {
48
+ digits[index] = ~digits[index] & 0xff;
49
+ }
50
+ // A leading byte without its high bit set would read back as positive.
51
+ if (digits.length === 0 || (digits[0] & 0x80) === 0) {
52
+ digits.unshift(0xff);
53
+ }
54
+ }
55
+ else if ((digits[0] & 0x80) !== 0) {
56
+ digits.unshift(0x00);
57
+ }
58
+ return new Uint8Array(digits);
59
+ }
60
+ //# sourceMappingURL=to-bytes-be.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"to-bytes-be.js","sourceRoot":"","sources":["../../src/big-int/to-bytes-be.ts"],"names":[],"mappings":"A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AC1C,SAAS,KAAK,EAAE,CAAC;IAClB,CAAC;IAED,IAAI,QAAQ,EAAE,CAAC;QACd,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,MAAM,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC;YACpD,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,MAAM,CAAC,KAAK,CAAE,GAAG,IAAI,CAAC;QACxC,CAAC;QAED,uEAAuE;QACvE,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAE,GAAG,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;YACtD,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QACtB,CAAC;IACF,CAAC;SAAM,IAAI,CAAC,MAAM,CAAC,CAAC,CAAE,GAAG,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;QACtC,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IACtB,CAAC;IAED,OAAO,IAAI,UAAU,CAAC,MAAM,CAAC,CAAC;AAC/B,CAAC"}
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Encodes a `bigint` as two's complement, little-endian bytes.
3
+ *
4
+ * The byte order native to x86 and ARM, and what Python's `int.to_bytes(…,
5
+ * "little")` produces. Reach for {@link bigIntToBytesBe} when the most
6
+ * significant byte comes first, which is what wire formats usually specify, and
7
+ * for `@ac-kit/math-integer`'s `limb32`/`limb64` families when the digits feed
8
+ * arithmetic rather than a buffer: those are wider, unsigned, and of fixed
9
+ * width.
10
+ *
11
+ * Time complexity: O(n) in the byte count.
12
+ *
13
+ * @param value - The value to encode. Zero encodes as a single `0x00`. @param
14
+ * byteLength - Fixed output width, sign-extended to fit. Omit for the minimal
15
+ * encoding. @returns A fresh buffer, never empty. @throws {RangeError} When
16
+ * `byteLength` is too small to hold `value`.
17
+ */
18
+ export declare function bigIntToBytesLe(value: bigint, byteLength?: number): Uint8Array<ArrayBuffer>;
19
+ //# sourceMappingURL=to-bytes-le.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"to-bytes-le.d.ts","sourceRoot":"","sources":["../../src/big-int/to-bytes-le.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,eAAe,CAC9B,KAAK,EAAE,MAAM,EACb,UAAU,CAAC,EAAE,MAAM,GACjB,UAAU,CAAC,WAAW,CAAC,CAEzB"}
@@ -0,0 +1,22 @@
1
+ import { bigIntToBytesBe } from "./to-bytes-be.js";
2
+ /**
3
+ * Encodes a `bigint` as two's complement, little-endian bytes.
4
+ *
5
+ * The byte order native to x86 and ARM, and what Python's `int.to_bytes(…,
6
+ * "little")` produces. Reach for {@link bigIntToBytesBe} when the most
7
+ * significant byte comes first, which is what wire formats usually specify, and
8
+ * for `@ac-kit/math-integer`'s `limb32`/`limb64` families when the digits feed
9
+ * arithmetic rather than a buffer: those are wider, unsigned, and of fixed
10
+ * width.
11
+ *
12
+ * Time complexity: O(n) in the byte count.
13
+ *
14
+ * @param value - The value to encode. Zero encodes as a single `0x00`. @param
15
+ * byteLength - Fixed output width, sign-extended to fit. Omit for the minimal
16
+ * encoding. @returns A fresh buffer, never empty. @throws {RangeError} When
17
+ * `byteLength` is too small to hold `value`.
18
+ */
19
+ export function bigIntToBytesLe(value, byteLength) {
20
+ return bigIntToBytesBe(value, byteLength).reverse();
21
+ }
22
+ //# sourceMappingURL=to-bytes-le.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"to-bytes-le.js","sourceRoot":"","sources":["../../src/big-int/to-bytes-le.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AAEnD;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,eAAe,CAC9B,KAAa,EACb,UAAmB;IAEnB,OAAO,eAAe,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC,OAAO,EAAE,CAAC;AACrD,CAAC"}
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Applies `combine` to each octet pair of two equal-length arrays.
3
+ *
4
+ * The result of `combine` is masked to one octet. Pairing stops at the shorter
5
+ * array only when lengths are allowed to differ, which they are not here: a
6
+ * bitwise operation over mismatched widths has no meaning, so it throws rather
7
+ * than silently truncating.
8
+ *
9
+ * Complexity: O(n) time and O(n) memory in the input length.
10
+ *
11
+ * @param a The left octets.
12
+ * @param b The right octets.
13
+ * @param combine Called with each octet pair and its index.
14
+ * @returns A new array of the same length.
15
+ * @throws RangeError When the two arrays differ in length.
16
+ */
17
+ export declare function bytesCombine(a: Uint8Array, b: Uint8Array, combine: (left: number, right: number, index: number) => number): Uint8Array<ArrayBuffer>;
18
+ //# sourceMappingURL=bytes-combine.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bytes-combine.d.ts","sourceRoot":"","sources":["../../src/buffer/bytes-combine.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,YAAY,CAC3B,CAAC,EAAE,UAAU,EACb,CAAC,EAAE,UAAU,EACb,OAAO,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,KAAK,MAAM,GAC7D,UAAU,CAAC,WAAW,CAAC,CASzB"}
@@ -0,0 +1,25 @@
1
+ import { bytesMap } from "./bytes-map.js";
2
+ /**
3
+ * Applies `combine` to each octet pair of two equal-length arrays.
4
+ *
5
+ * The result of `combine` is masked to one octet. Pairing stops at the shorter
6
+ * array only when lengths are allowed to differ, which they are not here: a
7
+ * bitwise operation over mismatched widths has no meaning, so it throws rather
8
+ * than silently truncating.
9
+ *
10
+ * Complexity: O(n) time and O(n) memory in the input length.
11
+ *
12
+ * @param a The left octets.
13
+ * @param b The right octets.
14
+ * @param combine Called with each octet pair and its index.
15
+ * @returns A new array of the same length.
16
+ * @throws RangeError When the two arrays differ in length.
17
+ */
18
+ export function bytesCombine(a, b, combine) {
19
+ if (a.length !== b.length) {
20
+ throw new RangeError(`cannot combine ${a.length} octets with ${b.length} octets`);
21
+ }
22
+ // Non-null: `index` runs over `a`'s length, and `b` has been checked equal.
23
+ return bytesMap(a, (byte, index) => combine(byte, b[index], index));
24
+ }
25
+ //# sourceMappingURL=bytes-combine.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bytes-combine.js","sourceRoot":"","sources":["../../src/buffer/bytes-combine.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAE1C;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,YAAY,CAC3B,CAAa,EACb,CAAa,EACb,OAA+D;IAE/D,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,MAAM,EAAE,CAAC;QAC3B,MAAM,IAAI,UAAU,CACnB,kBAAkB,CAAC,CAAC,MAAM,gBAAgB,CAAC,CAAC,MAAM,SAAS,CAC3D,CAAC;IACH,CAAC;IAED,4EAA4E;IAC5E,OAAO,QAAQ,CAAC,CAAC,EAAE,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC,KAAK,CAAE,EAAE,KAAK,CAAC,CAAC,CAAC;AACtE,CAAC"}
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Applies `map` to every octet of `bytes`, returning a new array.
3
+ *
4
+ * The result of `map` is masked to one octet, so a caller may write `~byte`
5
+ * without restating `& 0xff`.
6
+ *
7
+ * Complexity: O(n) time and O(n) memory in the input length.
8
+ *
9
+ * @param bytes The source octets.
10
+ * @param map Called with each octet and its index.
11
+ * @returns A new array of the same length.
12
+ */
13
+ export declare function bytesMap(bytes: Uint8Array, map: (byte: number, index: number) => number): Uint8Array<ArrayBuffer>;
14
+ //# sourceMappingURL=bytes-map.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bytes-map.d.ts","sourceRoot":"","sources":["../../src/buffer/bytes-map.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,wBAAgB,QAAQ,CACvB,KAAK,EAAE,UAAU,EACjB,GAAG,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,KAAK,MAAM,GAC1C,UAAU,CAAC,WAAW,CAAC,CASzB"}
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Applies `map` to every octet of `bytes`, returning a new array.
3
+ *
4
+ * The result of `map` is masked to one octet, so a caller may write `~byte`
5
+ * without restating `& 0xff`.
6
+ *
7
+ * Complexity: O(n) time and O(n) memory in the input length.
8
+ *
9
+ * @param bytes The source octets.
10
+ * @param map Called with each octet and its index.
11
+ * @returns A new array of the same length.
12
+ */
13
+ export function bytesMap(bytes, map) {
14
+ const result = new Uint8Array(bytes.length);
15
+ for (let index = 0; index < bytes.length; index++) {
16
+ // Non-null: `index` runs over the source array's own length.
17
+ result[index] = map(bytes[index], index) & 0xff;
18
+ }
19
+ return result;
20
+ }
21
+ //# sourceMappingURL=bytes-map.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bytes-map.js","sourceRoot":"","sources":["../../src/buffer/bytes-map.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,QAAQ,CACvB,KAAiB,EACjB,GAA4C;IAE5C,MAAM,MAAM,GAAG,IAAI,UAAU,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;IAE5C,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,KAAK,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC;QACnD,6DAA6D;QAC7D,MAAM,CAAC,KAAK,CAAC,GAAG,GAAG,CAAC,KAAK,CAAC,KAAK,CAAE,EAAE,KAAK,CAAC,GAAG,IAAI,CAAC;IAClD,CAAC;IAED,OAAO,MAAM,CAAC;AACf,CAAC"}
@@ -1,4 +1,4 @@
1
- import type { TextEncodingName } from "./text-encoding-name.js";
1
+ import type { TextDecodingName } from "./text-encoding-name.js";
2
2
  /**
3
3
  * Options mirroring `TextDecoderOptions` (see `TextDecoder`).
4
4
  *
@@ -17,6 +17,11 @@ import type { TextEncodingName } from "./text-encoding-name.js";
17
17
  * `"utf-32be"` ignores `ignoreBOM` (no byte order mark exists for a
18
18
  * 4-byte-per-code-point scheme) but honors `fatal`, since a 4-byte group can
19
19
  * still decode to a surrogate or an out-of-range value.
20
+ *
21
+ * `"windows-1252"` and `"shift_jis"` are decoded by a real `TextDecoder` too:
22
+ * they honor `fatal` and ignore `ignoreBOM`, which the standard defines for
23
+ * Unicode encodings only. Every windows-1252 byte decodes, so `fatal` never
24
+ * throws for it.
20
25
  */
21
26
  export type DecodeTextOptions = {
22
27
  /**
@@ -39,8 +44,12 @@ export type DecodeTextOptions = {
39
44
  * @param options - See {@link DecodeTextOptions}.
40
45
  * @returns The decoded string.
41
46
  * @throws {TypeError} When `options.fatal` is `true` and `bytes` is not valid
42
- * for `encoding`. Never thrown for `"latin1"`/`"ascii"`, for which every byte
43
- * is valid.
47
+ * for `encoding`. Never thrown for `"latin1"`, `"ascii"` and
48
+ * `"windows-1252"`, for which every byte is valid.
49
+ * @throws {RangeError} For `"shift_jis"` where the runtime's `TextDecoder`
50
+ * lacks it.
51
+ * @see {@link isValidText} to test validity without decoding into a string
52
+ * the caller does not need.
44
53
  */
45
- export declare function decodeText(bytes: Uint8Array, encoding: TextEncodingName, options?: DecodeTextOptions): string;
54
+ export declare function decodeText(bytes: Uint8Array, encoding: TextDecodingName, options?: DecodeTextOptions): string;
46
55
  //# sourceMappingURL=decode-text.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"decode-text.d.ts","sourceRoot":"","sources":["../../../src/buffer/text/decode-text.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAEhE;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,MAAM,iBAAiB,GAAG;IAC/B;;;OAGG;IACH,KAAK,CAAC,EAAE,OAAO,CAAC;IAEhB;;;;OAIG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;CACpB,CAAC;AAyBF;;;;;;;;;;GAUG;AACH,wBAAgB,UAAU,CACzB,KAAK,EAAE,UAAU,EACjB,QAAQ,EAAE,gBAAgB,EAC1B,OAAO,CAAC,EAAE,iBAAiB,GACzB,MAAM,CAwDR"}
1
+ {"version":3,"file":"decode-text.d.ts","sourceRoot":"","sources":["../../../src/buffer/text/decode-text.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAEhE;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,MAAM,iBAAiB,GAAG;IAC/B;;;OAGG;IACH,KAAK,CAAC,EAAE,OAAO,CAAC;IAEhB;;;;OAIG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;CACpB,CAAC;AAuBF;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,UAAU,CACzB,KAAK,EAAE,UAAU,EACjB,QAAQ,EAAE,gBAAgB,EAC1B,OAAO,CAAC,EAAE,iBAAiB,GACzB,MAAM,CAkDR"}
@@ -1,14 +1,13 @@
1
- const utf8Decoders = new Map();
2
- const utf16BeDecoders = new Map();
1
+ const decoders = new Map();
3
2
  // `TextDecoder` construction is not free; a decoder only depends on
4
3
  // (label, fatal, ignoreBOM), so one instance per combination is reused across
5
4
  // every call instead of rebuilding it per call.
6
- function getCachedDecoder(cache, label, fatal, ignoreBOM) {
7
- const key = `${fatal}:${ignoreBOM}`;
8
- let decoder = cache.get(key);
5
+ function getCachedDecoder(label, fatal, ignoreBOM) {
6
+ const key = `${label}:${fatal}:${ignoreBOM}`;
7
+ let decoder = decoders.get(key);
9
8
  if (!decoder) {
10
9
  decoder = new TextDecoder(label, { fatal, ignoreBOM });
11
- cache.set(key, decoder);
10
+ decoders.set(key, decoder);
12
11
  }
13
12
  return decoder;
14
13
  }
@@ -20,17 +19,21 @@ function getCachedDecoder(cache, label, fatal, ignoreBOM) {
20
19
  * @param options - See {@link DecodeTextOptions}.
21
20
  * @returns The decoded string.
22
21
  * @throws {TypeError} When `options.fatal` is `true` and `bytes` is not valid
23
- * for `encoding`. Never thrown for `"latin1"`/`"ascii"`, for which every byte
24
- * is valid.
22
+ * for `encoding`. Never thrown for `"latin1"`, `"ascii"` and
23
+ * `"windows-1252"`, for which every byte is valid.
24
+ * @throws {RangeError} For `"shift_jis"` where the runtime's `TextDecoder`
25
+ * lacks it.
26
+ * @see {@link isValidText} to test validity without decoding into a string
27
+ * the caller does not need.
25
28
  */
26
29
  export function decodeText(bytes, encoding, options) {
27
30
  const fatal = options?.fatal ?? false;
28
31
  const ignoreBOM = options?.ignoreBOM ?? false;
29
- if (encoding === "utf-8") {
30
- return getCachedDecoder(utf8Decoders, "utf-8", fatal, ignoreBOM).decode(bytes);
31
- }
32
- if (encoding === "utf-16be") {
33
- return getCachedDecoder(utf16BeDecoders, "utf-16be", fatal, ignoreBOM).decode(bytes);
32
+ if (encoding === "utf-8" ||
33
+ encoding === "utf-16be" ||
34
+ encoding === "windows-1252" ||
35
+ encoding === "shift_jis") {
36
+ return getCachedDecoder(encoding, fatal, ignoreBOM).decode(bytes);
34
37
  }
35
38
  if (encoding === "utf-32be") {
36
39
  let out = "";
@@ -1 +1 @@
1
- {"version":3,"file":"decode-text.js","sourceRoot":"","sources":["../../../src/buffer/text/decode-text.ts"],"names":[],"mappings":"AAoCA,MAAM,YAAY,GAAG,IAAI,GAAG,EAAuB,CAAC;AACpD,MAAM,eAAe,GAAG,IAAI,GAAG,EAAuB,CAAC;AAEvD,oEAAoE;AACpE,8EAA8E;AAC9E,gDAAgD;AAChD,SAAS,gBAAgB,CACxB,KAA+B,EAC/B,KAAa,EACb,KAAc,EACd,SAAkB;IAElB,MAAM,GAAG,GAAG,GAAG,KAAK,IAAI,SAAS,EAAE,CAAC;IACpC,IAAI,OAAO,GAAG,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IAE7B,IA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ACF,IAAI,KAAK,EAAE,CAAC;oBACX,MAAM,IAAI,SAAS,CAClB,6CAA6C,SAAS,CAAC,QAAQ,CAAC,EAAE,CAAC,EAAE,CACrE,CAAC;gBACH,CAAC;gBACD,GAAG,IAAI,QAAQ,CAAC;gBAChB,SAAS;YACV,CAAC;YAED,GAAG,IAAI,MAAM,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC;QACxC,CAAC;QACD,OAAO,GAAG,CAAC;IACZ,CAAC;IAED,6EAA6E;IAC7E,wEAAwE;IACxE,0EAA0E;IAC1E,MAAM,IAAI,GAAG,QAAQ,KAAK,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC;IAChD,IAAI,GAAG,GAAG,EAAE,CAAC;IACb,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,KAAK,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC;QACnD,GAAG,IAAI,MAAM,CAAC,YAAY,CAAC,KAAK,CAAC,KAAK,CAAE,GAAG,IAAI,CAAC,CAAC;IAClD,CAAC;IACD,OAAO,GAAG,CAAC;AACZ,CAAC"}
@@ -0,0 +1,25 @@
1
+ import type { TextDecodingName } from "./text-encoding-name.js";
2
+ /**
3
+ * Whether `bytes` is valid text in `encoding`: whether decoding it would
4
+ * succeed without a single replacement character.
5
+ *
6
+ * Use it to test candidate encodings when guessing which one a byte string
7
+ * uses; use {@link decodeText} with `fatal` when the text itself is wanted.
8
+ * Always `true` for `"latin1"`, `"ascii"` and `"windows-1252"`, where every
9
+ * byte decodes.
10
+ *
11
+ * Time complexity: O(n) in `bytes.length`. Memory: O(n) for the decoded string,
12
+ * discarded.
13
+ *
14
+ * @example
15
+ * isValidText(new Uint8Array([0xc3, 0xa9]), "utf-8"); // true
16
+ * isValidText(new Uint8Array([0xe9]), "utf-8"); // false
17
+ *
18
+ * @param bytes - The bytes to test.
19
+ * @param encoding - The candidate encoding.
20
+ * @returns Whether every byte decodes.
21
+ * @throws {RangeError} For `"shift_jis"` where the runtime's `TextDecoder`
22
+ * lacks it.
23
+ */
24
+ export declare function isValidText(bytes: Uint8Array, encoding: TextDecodingName): boolean;
25
+ //# sourceMappingURL=is-valid-text.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"is-valid-text.d.ts","sourceRoot":"","sources":["../../../src/buffer/text/is-valid-text.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAEhE;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,WAAW,CAC1B,KAAK,EAAE,UAAU,EACjB,QAAQ,EAAE,gBAAgB,GACxB,OAAO,CAWT"}
@@ -0,0 +1,36 @@
1
+ import { decodeText } from "./decode-text.js";
2
+ /**
3
+ * Whether `bytes` is valid text in `encoding`: whether decoding it would
4
+ * succeed without a single replacement character.
5
+ *
6
+ * Use it to test candidate encodings when guessing which one a byte string
7
+ * uses; use {@link decodeText} with `fatal` when the text itself is wanted.
8
+ * Always `true` for `"latin1"`, `"ascii"` and `"windows-1252"`, where every
9
+ * byte decodes.
10
+ *
11
+ * Time complexity: O(n) in `bytes.length`. Memory: O(n) for the decoded string,
12
+ * discarded.
13
+ *
14
+ * @example
15
+ * isValidText(new Uint8Array([0xc3, 0xa9]), "utf-8"); // true
16
+ * isValidText(new Uint8Array([0xe9]), "utf-8"); // false
17
+ *
18
+ * @param bytes - The bytes to test.
19
+ * @param encoding - The candidate encoding.
20
+ * @returns Whether every byte decodes.
21
+ * @throws {RangeError} For `"shift_jis"` where the runtime's `TextDecoder`
22
+ * lacks it.
23
+ */
24
+ export function isValidText(bytes, encoding) {
25
+ try {
26
+ decodeText(bytes, encoding, { fatal: true });
27
+ return true;
28
+ }
29
+ catch (error) {
30
+ if (error instanceof TypeError) {
31
+ return false;
32
+ }
33
+ throw error;
34
+ }
35
+ }
36
+ //# sourceMappingURL=is-valid-text.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"is-valid-text.js","sourceRoot":"","sources":["../../../src/buffer/text/is-valid-text.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAG9C;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,UAAU,WAAW,CAC1B,KAAiB,EACjB,QAA0B;IAE1B,IAAI,CAAC;QACJ,UAAU,CAAC,KAAK,EAAE,QAAQ,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;QAE7C,OAAO,IAAI,CAAC;IACb,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QAChB,IAAI,KAAK,YAAY,SAAS,EAAE,CAAC;YAChC,OAAO,KAAK,CAAC;QACd,CAAC;QACD,MAAM,KAAK,CAAC;IACb,CAAC;AACF,CAAC"}
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Index of the first UTF-8 character boundary at or after `index`.
3
+ *
4
+ * Slicing a UTF-8 buffer at an arbitrary offset can land inside a multi-byte
5
+ * sequence, whose orphaned tail decodes to a leading U+FFFD. Moving the offset
6
+ * forward to a boundary drops the partial character instead.
7
+ *
8
+ * Runs in O(1) on valid input — a sequence is at most four bytes long, so at
9
+ * most three continuation bytes are skipped — and in O(n) on the length of a
10
+ * malformed continuation run. Allocates nothing.
11
+ *
12
+ * @param bytes - UTF-8 encoded bytes.
13
+ * @param index - Offset to align. Negative values align to `0`.
14
+ * @returns The aligned offset, within `[0, bytes.length]`.
15
+ */
16
+ export declare function nextUtf8Boundary(bytes: Uint8Array, index: number): number;
17
+ //# sourceMappingURL=next-utf8-boundary.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"next-utf8-boundary.d.ts","sourceRoot":"","sources":["../../../src/buffer/text/next-utf8-boundary.ts"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAWzE"}