@mark1russell7/layout-contract 0.2.21 → 0.3.1

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.
@@ -350,6 +350,15 @@ interface LayoutInput {
350
350
  */
351
351
  readonly invalidated?: readonly string[];
352
352
  }
353
+ /**
354
+ * A link between two items of a packer input. A graph order of the packer reads the links.
355
+ * `a` and `b` are indexes into `PackInput.items`. The weight is 1 when it has no value.
356
+ */
357
+ interface PackLink {
358
+ readonly a: number;
359
+ readonly b: number;
360
+ readonly weight?: number;
361
+ }
353
362
  /** The input of a packer: a `layout-input/1` file without the edges. */
354
363
  interface PackInput {
355
364
  readonly container: LayoutContainer;
@@ -357,9 +366,26 @@ interface PackInput {
357
366
  readonly obstacles?: readonly Obstacle[];
358
367
  readonly gap?: number;
359
368
  readonly pairGaps?: readonly PairGap[];
369
+ /** The links between the items. An input without `links` has no graph, and the links have no effect on an order that does not read them. */
370
+ readonly links?: readonly PackLink[];
371
+ }
372
+ /** The options of `toPackInput`. */
373
+ interface PackInputOptions {
374
+ /** With `true`, the packer input gets the links of `packLinks`. The default is `false`, so the input has no `links`. */
375
+ readonly links?: boolean;
360
376
  }
361
- /** This function gives the packer input of a file. An input without a container gets `{ fixed: "none" }`, so the packer picks the size. */
362
- declare function toPackInput(input: LayoutInput): PackInput;
377
+ /**
378
+ * This function gives the packer input of a file. An input without a container gets `{ fixed: "none" }`, so the packer picks the size.
379
+ * The edges are not in the packer input. With the option `links`, their links are in it.
380
+ */
381
+ declare function toPackInput(input: LayoutInput, options?: PackInputOptions): PackInput;
382
+ /**
383
+ * This function gives the links of the edges of a file, with indexes into `input.boxes`.
384
+ * - Each link has `a` below `b`. The list is sorted by `a` and then by `b`.
385
+ * - The edges of one pair of boxes make one link. Its weight is the count of the edges, and a link of one edge has no weight.
386
+ * - The function does not use an edge to a box that is not in `input.boxes`, or an edge from a box to the same box.
387
+ */
388
+ declare function packLinks(input: LayoutInput): PackLink[];
363
389
  /**
364
390
  * This function gives the canonical form of an input. `documentHash` uses it for each `layout-input/1` document.
365
391
  * Each pair of `pairGaps` gets `a` before `b`, and the list is sorted by `a` and then by `b`.
@@ -556,5 +582,5 @@ declare function minEffectOf(spec: Pick<MeasureSpec, "minEffect">, a?: number, b
556
582
  * Two equal values are a tie, also two infinite values. A value `NaN` is an error.
557
583
  */
558
584
  declare function compareMeasure(candidate: number, baseline: number, spec: Pick<MeasureSpec, "direction" | "minEffect">): Verdict;
559
- export { SPLIT_SCHEMA as $, shapesOf as A, PairGap as B, itemShapeOf as C, translate as Ct, shapeKindsOf as D, shapeKindProblem as E, LayoutEdge as F, ClaimBreak as G, canonicalInput as H, LayoutGenerator as I, DeclaredClaim as J, CorpusEntry as K, LayoutInput as L, LayoutBox as M, LayoutCell as N, shapeLevel as O, LayoutContainer as P, FamilyCell as Q, Obstacle as R, isShapeKind as S, regionArea as St, shapeKindOf as T, portPoint as U, Port as V, toPackInput as W, FAMILY_ANY_KIND as X, Declares as Y, FamilyBy as Z, ShapeKindSpec as _, Sides as _t, MeasureSpec as a, declaredClaimOf as at, boxArea as b, marginSides as bt, minEffectOf as c, leaveOneSeedOut as ct, SHAPE_KINDS as d, Margin as dt, SplitCorpus as et, SHAPE_KIND_IDS as f, Point as ft, ShapeKind as g, Side as gt, ShapeGeometry as h, SIDES as ht, JointMeasureId as i, corpusHashOf as it, Item as j, shapeSupportProblems as k, DEFAULT_SHAPES as l, splitFileName as lt, ShapeCheck as m, Region as mt, JOINT_MEASURES as n, checkDeclared as nt, Verdict as o, declaredCounts as ot, ShapeBound as p, Rect as pt, CorpusSplit as q, JOINT_MOVED_MIN as r, checkSplit as rt, compareMeasure as s, familyOf as st, Direction as t, SplitFold as tt, RuleLevel as u, splitFolds as ut, ShapeLevel as v, boundsOf as vt, ruleLevel as w, checkShapes as x, overlapOf as xt, ShapeSupport as y, grow as yt, PackInput as z };
560
- //# sourceMappingURL=measures-CQAuu3T5.d.ts.map
585
+ export { FAMILY_ANY_KIND as $, shapesOf as A, PackInputOptions as B, itemShapeOf as C, marginSides as Ct, shapeKindsOf as D, shapeKindProblem as E, translate as Et, LayoutEdge as F, packLinks as G, PairGap as H, LayoutGenerator as I, ClaimBreak as J, portPoint as K, LayoutInput as L, LayoutBox as M, LayoutCell as N, shapeLevel as O, LayoutContainer as P, Declares as Q, Obstacle as R, isShapeKind as S, grow as St, shapeKindOf as T, regionArea as Tt, Port as U, PackLink as V, canonicalInput as W, CorpusSplit as X, CorpusEntry as Y, DeclaredClaim as Z, ShapeKindSpec as _, Region as _t, MeasureSpec as a, checkDeclared as at, boxArea as b, Sides as bt, minEffectOf as c, declaredClaimOf as ct, SHAPE_KINDS as d, leaveOneSeedOut as dt, FamilyBy as et, SHAPE_KIND_IDS as f, splitFileName as ft, ShapeKind as g, Rect as gt, ShapeGeometry as h, Point as ht, JointMeasureId as i, SplitFold as it, Item as j, shapeSupportProblems as k, DEFAULT_SHAPES as l, declaredCounts as lt, ShapeCheck as m, Margin as mt, JOINT_MEASURES as n, SPLIT_SCHEMA as nt, Verdict as o, checkSplit as ot, ShapeBound as p, splitFolds as pt, toPackInput as q, JOINT_MOVED_MIN as r, SplitCorpus as rt, compareMeasure as s, corpusHashOf as st, Direction as t, FamilyCell as tt, RuleLevel as u, familyOf as ut, ShapeLevel as v, SIDES as vt, ruleLevel as w, overlapOf as wt, checkShapes as x, boundsOf as xt, ShapeSupport as y, Side as yt, PackInput as z };
586
+ //# sourceMappingURL=measures-BLoTbQwB.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"measures-CQAuu3T5.d.ts","names":[],"sources":["../src/geometry.ts","../src/corpus.ts","../src/input.ts","../src/shapes.ts","../src/measures.ts"],"mappings":";KACY;;cAGC,gBAAgB;UAEZ;WACN;WACA;;;UAIM;WACN;WACA;WACA;WACA;;;UAIM;WACN;WACA;WACA;WACA;;;;;;KAOC;WACG;WAAuB;WAAoB;WAAoB;WAAoB;;WAEnF;WACA;WACA;;WAEA;WAAyB;W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AkB;;KC5NpH;;;;;;UAOK,YAAY;WAClB,IAAI;WACJ;WACA,WAAW;;WAEX;;WAEA;;WAEA;;WAEA;;WAEA;;;;;WAKA;aAAgC;;;WAEhC;;WAEA,kBAAkB;;;;;WAKlB,QAAQ;;;KAIP;;;;;cAoBC,yBAAyB,YAAY;;cAarC;;KAGD;;;;;;iBAOI,YAAY,MAAM,KAAK,2BAA2B,YAAO;;;;;;;iBAezD,eAAe,mBAAmB,kBAAkB,MAAM,KAAK,0CAA0C"}
package/build/text.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { a as MeasureSpec } from "./measures-CQAuu3T5.js";
1
+ import { a as MeasureSpec } from "./measures-BLoTbQwB.js";
2
2
  /**
3
3
  * The advance function of a host: the width of a run of text in a font, in pixels.
4
4
  * A browser host gives `canvasAdvance(context)`. A test or a server host gives a function of a font file.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package",
3
3
  "name": "@mark1russell7/layout-contract",
4
- "version": "0.2.21",
4
+ "version": "0.3.1",
5
5
  "description": "The shared contract of the packing and highways layout engines: the input and record formats, the turn rule, margins, tolerances and hard rules.",
6
6
  "keywords": [
7
7
  "layout",
@@ -55,6 +55,9 @@
55
55
  "vitest": "^5.0.3",
56
56
  "tsdown": "^0.23.1"
57
57
  },
58
+ "dependencies": {
59
+ "@mark1russell7/rigor-generators": "^0.1.0"
60
+ },
58
61
  "scripts": {
59
62
  "build": "tsdown",
60
63
  "test": "vitest run",
package/src/hash.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { canonicalInput, type LayoutInput } from "./input.ts";
2
2
  import { RECORD_EPS } from "./tolerance.ts";
3
- import { utf8 } from "./utf8.ts";
3
+ import { utf8Write } from "./utf8.ts";
4
4
 
5
5
  /** The round constants of SHA-256. */
6
6
  const K: readonly number[] = [
@@ -16,12 +16,13 @@ const K: readonly number[] = [
16
16
  * It uses no API of Node or of the browser.
17
17
  */
18
18
  export function sha256Hex(text: string): string {
19
- const bytes = utf8(text);
20
- const bits = bytes.length * 8;
21
- const total = Math.ceil((bytes.length + 9) / 64) * 64;
19
+ // The bytes go straight into the padded buffer, so the digest makes one array, not a list of the bytes and a copy.
20
+ const length = utf8Write(text);
21
+ const bits = length * 8;
22
+ const total = Math.ceil((length + 9) / 64) * 64;
22
23
  const data = new Uint8Array(total);
23
- data.set(bytes);
24
- data[bytes.length] = 0x80;
24
+ utf8Write(text, data);
25
+ data[length] = 0x80;
25
26
  const view = new DataView(data.buffer);
26
27
  view.setUint32(total - 8, Math.floor(bits / 0x100000000));
27
28
  view.setUint32(total - 4, bits >>> 0);
package/src/input.ts CHANGED
@@ -182,6 +182,16 @@ export interface LayoutInput {
182
182
  readonly invalidated?: readonly string[];
183
183
  }
184
184
 
185
+ /**
186
+ * A link between two items of a packer input. A graph order of the packer reads the links.
187
+ * `a` and `b` are indexes into `PackInput.items`. The weight is 1 when it has no value.
188
+ */
189
+ export interface PackLink {
190
+ readonly a: number;
191
+ readonly b: number;
192
+ readonly weight?: number;
193
+ }
194
+
185
195
  /** The input of a packer: a `layout-input/1` file without the edges. */
186
196
  export interface PackInput {
187
197
  readonly container: LayoutContainer;
@@ -189,19 +199,57 @@ export interface PackInput {
189
199
  readonly obstacles?: readonly Obstacle[];
190
200
  readonly gap?: number;
191
201
  readonly pairGaps?: readonly PairGap[];
202
+ /** The links between the items. An input without `links` has no graph, and the links have no effect on an order that does not read them. */
203
+ readonly links?: readonly PackLink[];
192
204
  }
193
205
 
194
- /** This function gives the packer input of a file. An input without a container gets `{ fixed: "none" }`, so the packer picks the size. */
195
- export function toPackInput(input: LayoutInput): PackInput {
206
+ /** The options of `toPackInput`. */
207
+ export interface PackInputOptions {
208
+ /** With `true`, the packer input gets the links of `packLinks`. The default is `false`, so the input has no `links`. */
209
+ readonly links?: boolean;
210
+ }
211
+
212
+ /**
213
+ * This function gives the packer input of a file. An input without a container gets `{ fixed: "none" }`, so the packer picks the size.
214
+ * The edges are not in the packer input. With the option `links`, their links are in it.
215
+ */
216
+ export function toPackInput(input: LayoutInput, options: PackInputOptions = {}): PackInput {
196
217
  return {
197
218
  container: input.container ?? { fixed: "none" },
198
219
  items: input.boxes,
199
220
  ...(input.obstacles === undefined ? {} : { obstacles: input.obstacles }),
200
221
  ...(input.gap === undefined ? {} : { gap: input.gap }),
201
222
  ...(input.pairGaps === undefined ? {} : { pairGaps: input.pairGaps }),
223
+ ...(options.links === true ? { links: packLinks(input) } : {}),
202
224
  };
203
225
  }
204
226
 
227
+ /**
228
+ * This function gives the links of the edges of a file, with indexes into `input.boxes`.
229
+ * - Each link has `a` below `b`. The list is sorted by `a` and then by `b`.
230
+ * - The edges of one pair of boxes make one link. Its weight is the count of the edges, and a link of one edge has no weight.
231
+ * - The function does not use an edge to a box that is not in `input.boxes`, or an edge from a box to the same box.
232
+ */
233
+ export function packLinks(input: LayoutInput): PackLink[] {
234
+ const index = new Map<string, number>();
235
+ input.boxes.forEach((box, i) => {
236
+ if (!index.has(box.id)) index.set(box.id, i);
237
+ });
238
+ const count = new Map<string, { a: number; b: number; n: number }>();
239
+ for (const edge of input.edges ?? []) {
240
+ const s = index.get(edge.source);
241
+ const t = index.get(edge.target);
242
+ if (s === undefined || t === undefined || s === t) continue;
243
+ const a = Math.min(s, t);
244
+ const b = Math.max(s, t);
245
+ const key = `${a} ${b}`;
246
+ const c = count.get(key);
247
+ if (c === undefined) count.set(key, { a, b, n: 1 });
248
+ else c.n += 1;
249
+ }
250
+ return [...count.values()].sort((x, y) => x.a - y.a || x.b - y.b).map(({ a, b, n }) => (n === 1 ? { a, b } : { a, b, weight: n }));
251
+ }
252
+
205
253
  /** This function compares two strings by their UTF-16 code units, as `Array.prototype.sort` does without a compare function. */
206
254
  const byCode = (x: string, y: string): number => (x < y ? -1 : x > y ? 1 : 0);
207
255
 
package/src/random.ts CHANGED
@@ -1,17 +1,18 @@
1
+ import { mulberry32 as homeMulberry32 } from "@mark1russell7/rigor-generators";
1
2
  import { utf8 } from "./utf8.ts";
2
3
 
3
- /** This function gives a generator of random numbers in [0, 1). It is `mulberry32`, as in `docs/FORMAT.md` of `packing`. */
4
+ /**
5
+ * This function gives a generator of random numbers in [0, 1). It is `mulberry32`, as in `docs/FORMAT.md` of `packing`.
6
+ * Its home is `@mark1russell7/rigor-generators`. This function keeps the name and the signature of the API.
7
+ */
4
8
  export function mulberry32(seed: number): () => number {
5
- let s = seed >>> 0;
6
- return () => {
7
- s = (s + 0x6d2b79f5) | 0;
8
- let t = Math.imul(s ^ (s >>> 15), 1 | s);
9
- t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
10
- return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
11
- };
9
+ return homeMulberry32(seed);
12
10
  }
13
11
 
14
- /** This function gives the FNV-1a hash of 32 bits of the UTF-8 bytes of a string. */
12
+ /**
13
+ * This function gives the FNV-1a hash of 32 bits of the UTF-8 bytes of a string.
14
+ * It stays here because `fnv1a32` of `@mark1russell7/rigor-generators` encodes a lone surrogate as U+FFFD. This function encodes the 3 bytes of its code.
15
+ */
15
16
  export function fnv1a32(text: string): number {
16
17
  let h = 0x811c9dc5;
17
18
  for (const b of utf8(text)) h = Math.imul(h ^ b, 0x01000193) >>> 0;
package/src/record.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { Tolerance } from "./tolerance.ts";
2
2
  import type { LayoutInput } from "./input.ts";
3
- import { shapeKindsOf, type ShapeKind } from "./shapes.ts";
3
+ import { isShapeKind, shapeKindsOf, type ShapeKind } from "./shapes.ts";
4
4
 
5
5
  /** The stage of a record: the packer, the router, or both together. */
6
6
  export type Stage = "pack" | "route" | "joint";
@@ -30,8 +30,11 @@ export interface LayoutRecord {
30
30
  readonly hard: Readonly<Record<string, number>>;
31
31
  /** The tolerance of each hard rule of the run. */
32
32
  readonly tolerance?: Readonly<Partial<Tolerance>>;
33
- /** `true` when two runs with the same seed give the same positions. */
34
- readonly deterministic: boolean;
33
+ /**
34
+ * The value is `true` when two runs with the same seed give the same positions.
35
+ * A skipped record has `null`, because no run made a claim. Each other record has a boolean.
36
+ */
37
+ readonly deterministic: boolean | null;
35
38
  /** The id of the prediction that was written before the run. */
36
39
  readonly prediction?: string;
37
40
  /** The median of `msRuns`, or the time of the one run. */
@@ -67,6 +70,10 @@ export interface RunEnv {
67
70
  readonly cpu: string;
68
71
  /** The count of the logical processors. */
69
72
  readonly cpus: number;
73
+ /** The mode of the timing, for example `process` or `fresh` of `paired-time/1`. Each timing record names its mode. */
74
+ readonly mode?: string;
75
+ /** The context of the run, for example `ci` or `local`, with the Node flags. */
76
+ readonly context?: string;
70
77
  }
71
78
 
72
79
  /** The facts of a run that the input does not give. */
@@ -90,3 +97,75 @@ export function toLayoutRecord(input: LayoutInput, run: RunFacts): LayoutRecord
90
97
  ...(onlyRect ? {} : { shapes }),
91
98
  };
92
99
  }
100
+
101
+ type Fields = Readonly<Record<string, unknown>>;
102
+
103
+ const isFields = (v: unknown): v is Fields => typeof v === "object" && v !== null && !Array.isArray(v);
104
+
105
+ const STAGES: readonly string[] = ["pack", "route", "joint"];
106
+ const RECORD_STRINGS = ["step", "strategy", "corpus", "input", "inputHash", "set"] as const;
107
+ const RECORD_NUMBERS = ["seed", "nodes", "edges", "ms"] as const;
108
+ const RECORD_OPTIONAL_STRINGS = ["prediction", "outputHash", "skipped", "commit"] as const;
109
+ const ENV_STRINGS = ["node", "os", "arch", "runner", "cpu"] as const;
110
+ const ENV_OPTIONAL_STRINGS = ["mode", "context"] as const;
111
+
112
+ /** This function adds a problem for each value of a map that is not a number. */
113
+ function numberMap(out: string[], v: unknown, name: string): void {
114
+ if (!isFields(v)) {
115
+ out.push(`${name} is not an object`);
116
+ return;
117
+ }
118
+ for (const [key, n] of Object.entries(v)) if (typeof n !== "number") out.push(`${name}.${key} is not a number`);
119
+ }
120
+
121
+ /** This function gives the problems of the field `env` of a record. */
122
+ function envProblems(env: unknown): string[] {
123
+ if (!isFields(env)) return ["env is not an object"];
124
+ const out: string[] = [];
125
+ for (const key of ENV_STRINGS) if (typeof env[key] !== "string") out.push(`env.${key} is not a string`);
126
+ if (typeof env["cpus"] !== "number") out.push("env.cpus is not a number");
127
+ for (const key of ENV_OPTIONAL_STRINGS) if (env[key] !== undefined && typeof env[key] !== "string") out.push(`env.${key} is not a string`);
128
+ return out;
129
+ }
130
+
131
+ /**
132
+ * This function gives the problems of one `layout-record/1` value. An empty list means a good record.
133
+ * A record that has `skipped` has `deterministic: null`. An old skipped record with a boolean is also correct.
134
+ * A record without `skipped` has a boolean. The check accepts fields that the format does not know.
135
+ */
136
+ export function checkLayoutRecord(value: unknown): string[] {
137
+ if (!isFields(value)) return ["the record is not an object"];
138
+ const out: string[] = [];
139
+ const v = value;
140
+ if (v["schema"] !== "layout-record/1") out.push("schema is not layout-record/1");
141
+ if (typeof v["stage"] !== "string" || !STAGES.includes(v["stage"])) out.push("stage is not pack, route or joint");
142
+ for (const key of RECORD_STRINGS) if (typeof v[key] !== "string") out.push(`${key} is not a string`);
143
+ for (const key of RECORD_NUMBERS) if (typeof v[key] !== "number") out.push(`${key} is not a number`);
144
+ for (const key of RECORD_OPTIONAL_STRINGS) if (v[key] !== undefined && typeof v[key] !== "string") out.push(`${key} is not a string`);
145
+ if (!isFields(v["options"])) out.push("options is not an object");
146
+ numberMap(out, v["measures"], "measures");
147
+ numberMap(out, v["hard"], "hard");
148
+ if (v["tolerance"] !== undefined) numberMap(out, v["tolerance"], "tolerance");
149
+ const shapes = v["shapes"];
150
+ if (shapes !== undefined && (!Array.isArray(shapes) || !shapes.every(isShapeKind))) out.push("shapes is not a list of shape kinds");
151
+ const msRuns = v["msRuns"];
152
+ if (msRuns !== undefined && (!Array.isArray(msRuns) || !msRuns.every((n) => typeof n === "number"))) out.push("msRuns is not a list of numbers");
153
+ if (v["env"] !== undefined) out.push(...envProblems(v["env"]));
154
+ const deterministic = v["deterministic"];
155
+ if (v["skipped"] === undefined) {
156
+ if (typeof deterministic !== "boolean") out.push("deterministic is not a boolean, and the record has no skipped");
157
+ } else if (deterministic !== null && typeof deterministic !== "boolean") out.push("deterministic of a skipped record is not null or a boolean");
158
+ return out;
159
+ }
160
+
161
+ /**
162
+ * This function reads one `layout-record/1` value, for example one parsed line of a JSON Lines file.
163
+ * It throws a `TypeError` with the problems of `checkLayoutRecord`.
164
+ * An old skipped record has a boolean `deterministic`. The result has `null` in its place, because no run made that claim.
165
+ */
166
+ export function parseLayoutRecord(value: unknown): LayoutRecord {
167
+ const problems = checkLayoutRecord(value);
168
+ if (problems.length > 0) throw new TypeError(`The record is not a layout-record/1: ${problems.join(", ")}.`);
169
+ const record = value as LayoutRecord;
170
+ return record.skipped !== undefined && record.deterministic !== null ? { ...record, deterministic: null } : record;
171
+ }
package/src/utf8.ts CHANGED
@@ -1,12 +1,51 @@
1
- /** This function gives the UTF-8 bytes of a string. The package has no runtime dependencies, thus it does not use `TextEncoder`. */
2
- export function utf8(text: string): number[] {
3
- const bytes: number[] = [];
4
- for (const ch of text) {
5
- const c = ch.codePointAt(0)!;
6
- if (c < 0x80) bytes.push(c);
7
- else if (c < 0x800) bytes.push(0xc0 | (c >> 6), 0x80 | (c & 63));
8
- else if (c < 0x10000) bytes.push(0xe0 | (c >> 12), 0x80 | ((c >> 6) & 63), 0x80 | (c & 63));
9
- else bytes.push(0xf0 | (c >> 18), 0x80 | ((c >> 12) & 63), 0x80 | ((c >> 6) & 63), 0x80 | (c & 63));
1
+ /**
2
+ * This function writes the UTF-8 bytes of a string into `out`, from the index `at`, and gives the index after the last byte.
3
+ * With no `out`, it writes nothing and gives the count of the bytes. A lone surrogate gives the 3 bytes of its code.
4
+ * It reads the UTF-16 code units, so it makes no string for each character (L81: `sha256Hex` allocated 2 GB in one pair).
5
+ */
6
+ export function utf8Write(text: string, out?: Uint8Array, at = 0): number {
7
+ let n = at;
8
+ for (let i = 0; i < text.length; i++) {
9
+ let c = text.charCodeAt(i);
10
+ if (c >= 0xd800 && c < 0xdc00 && i + 1 < text.length) {
11
+ const low = text.charCodeAt(i + 1);
12
+ if (low >= 0xdc00 && low < 0xe000) {
13
+ c = 0x10000 + ((c - 0xd800) << 10) + (low - 0xdc00);
14
+ i++;
15
+ }
16
+ }
17
+ if (c < 0x80) {
18
+ if (out !== undefined) out[n] = c;
19
+ n += 1;
20
+ } else if (c < 0x800) {
21
+ if (out !== undefined) {
22
+ out[n] = 0xc0 | (c >> 6);
23
+ out[n + 1] = 0x80 | (c & 63);
24
+ }
25
+ n += 2;
26
+ } else if (c < 0x10000) {
27
+ if (out !== undefined) {
28
+ out[n] = 0xe0 | (c >> 12);
29
+ out[n + 1] = 0x80 | ((c >> 6) & 63);
30
+ out[n + 2] = 0x80 | (c & 63);
31
+ }
32
+ n += 3;
33
+ } else {
34
+ if (out !== undefined) {
35
+ out[n] = 0xf0 | (c >> 18);
36
+ out[n + 1] = 0x80 | ((c >> 12) & 63);
37
+ out[n + 2] = 0x80 | ((c >> 6) & 63);
38
+ out[n + 3] = 0x80 | (c & 63);
39
+ }
40
+ n += 4;
41
+ }
10
42
  }
43
+ return n;
44
+ }
45
+
46
+ /** This function gives the UTF-8 bytes of a string. It does not use `TextEncoder`, because `TextEncoder` encodes a lone surrogate as U+FFFD. */
47
+ export function utf8(text: string): Uint8Array {
48
+ const bytes = new Uint8Array(utf8Write(text));
49
+ utf8Write(text, bytes);
11
50
  return bytes;
12
51
  }