@duoquote/geotini 0.0.0-stage → 0.1.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/geotini.js ADDED
@@ -0,0 +1,3169 @@
1
+ // Geotini format, version 1: fast, compact, URL-safe text codec for GeoJSON geometries.
2
+ // Specified in SPEC.md; src/reference.js is the bit-by-bit reference.
3
+ //
4
+ // encode(geometry, options = 5) -> string GeoJSON geometry, [lng, lat(, z(, m))]
5
+ // decode(string) -> geometry
6
+ // encodeLine(points, options = 5) -> string [[lat, lng], ...] as LineString
7
+ // decodeLine(string) -> [[lat, lng], ...]
8
+ /* @ts-self-types="../types/index.d.ts" */
9
+ import { GeotiniError } from './error.js';
10
+
11
+ const ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
12
+ const TYPES = ['Point', 'LineString', 'Polygon', 'MultiPoint', 'MultiLineString', 'MultiPolygon', 'GeometryCollection'];
13
+ const POW = /* @__PURE__ */ Array.from({ length: 16 }, (_, p) => 10 ** p);
14
+ const P2 = /* @__PURE__ */ Float64Array.from({ length: 64 }, (_, i) => 2 ** i);
15
+ const KI = [0, 0, 0, 0, 0, 3, 6, 9, 13, 16, 19, 23, 26, 29, 33, 36];
16
+ const COS2 = [4096, 4057, 3940, 3751, 3495, 3183, 2826, 2438, 2048, 1658, 1270, 913, 601, 345, 156, 39, 16];
17
+ const KMAX = 24, KWIDE = 52, BLOCK = 16, MAX_DEPTH = 32, MAXN = 134217719, LIMIT = 2 ** 50, H = 0.49999999999999994, KEEP = 1 << 20;
18
+ // The parameter a 5-bit long-form code stands for: 25 and 26 are -1 and -2,
19
+ // and 99 is outside every range, so the range check after it refuses 27 to
20
+ // 31. A table and not a branch on purpose: the decode loops seldom read a
21
+ // long form, but with the branch written out in them routes decoded 3 to 4%
22
+ // slower on Node and Deno. The loops came out compiled differently.
23
+ const LK = /* @__PURE__ */ Int8Array.from({ length: 32 }, (_, i) => (i <= KMAX ? i : i <= KMAX + 2 ? KMAX - i : 99));
24
+ // The longest string this decoder takes: bit positions are 32-bit integers.
25
+ // A longer one is refused with LIMIT_EXCEEDED (SPEC.md section 14), whatever
26
+ // the caller allows.
27
+ const MAX_LEN = 2 ** 28;
28
+ // What a caller allows when it says nothing: maxLength and maxPositions
29
+ // (SPEC.md section 14). A string can hold 24 positions a character, so
30
+ // without a cap a megabyte of characters is 1.8 GB of arrays, and a megabyte
31
+ // and a half with Z aborted Node. With 2^20 positions and parts, a call
32
+ // builds at most 93 MB on Node and Deno and 114 MB on Bun, whatever the
33
+ // string (76 MB for a line of [x, y], 93 with Z and M, 88 to 114 for empty
34
+ // geometries). The default length is 16 times that many characters, which no
35
+ // real geometry of 2^20 positions needs: it bounds what a string costs to
36
+ // read before it's refused. A string of at most DEF_FREE characters can't
37
+ // hold more than DEF_POS (nothing holds more than 4 to a bit), so it's
38
+ // decoded without being counted.
39
+ const DEF_LEN = 2 ** 24, DEF_POS = 2 ** 20, DEF_FREE = 43690;
40
+ // This call's caps and what's left of maxPositions (more where they're used
41
+ // below). Declared here because CUT, when the module loads, reaches
42
+ // badChar, which reads CAPL.
43
+ let CAPL = DEF_LEN, CAPP = DEF_POS, LEFT = 0;
44
+ // How much a count sizes up front. The bounds rule lets a refused string pass
45
+ // a count at every level at once: 32 nested collections of 1.5 million
46
+ // members fit in a megabyte, and Array(n) for each took 380 MB on Node before
47
+ // the first member was read. So a collection or a polygon reserves at most RP
48
+ // parts, a MultiLineString or MultiPolygon at most RP or one per 32 bits
49
+ // left, and a path at most RPT points or one value per bit left (a point is 2
50
+ // values, 3 with Z, 4 with Z and M: SPEC.md section 15); the arrays grow if
51
+ // more are written. Real strings take more bits than that per part and per
52
+ // point and keep their exact Array(n): growing cost 12% on a MultiPolygon of
53
+ // 3,000 buildings and 20 to 35% on lines of 200,000 points on Node.
54
+ const RP = 1024, RPT = 65536;
55
+
56
+ export { GeotiniError };
57
+ // The error codes of SPEC.md section 14. A refusal found at bit p, after a
58
+ // read past the end of the string (SEND), is TRUNCATED: a decoder that reads
59
+ // one bit at a time runs out of input before it can see the other problem.
60
+ // SEND is only read when there's a position: the self-test below runs before
61
+ // SEND is declared, and a ReferenceError there would quietly turn off every
62
+ // fast string-to-bytes route (long strings decoded 15 to 23% slower on V8).
63
+ // decodeLine passes its own end (e) instead of storing SEND on every call:
64
+ // the store cost about 3% on 3-point lines on Node. The body* loops pass
65
+ // their end argument too. They used SEND, which decodeLine doesn't set, so
66
+ // a refusal inside them took its code from whatever string decode had read
67
+ // last (TRUNCATED in a fresh process, OUT_OF_RANGE after a long string).
68
+ // A Rice-T6 pair reads both classes before it checks the first, so a refusal
69
+ // for y's class (a run of 36 zeros) or for x's quotient takes the position
70
+ // of x's or y's too-large class when that one comes first in the string.
71
+ const T = 'TRUNCATED', R = 'OUT_OF_RANGE', NC = 'NON_CANONICAL', CT = 'COUNT_TOO_LARGE', BH = 'BAD_HEADER', TD = 'TRAILING_DATA', CS = 'CHECKSUM';
72
+ const err = (c, p, e) => new GeotiniError(p === undefined ? c : p > (e === undefined ? SEND : e) ? T : c);
73
+ const verErr = () => err('UNSUPPORTED_VERSION');
74
+ // maxLength counts the UTF-8 bytes of a string (SPEC.md section 14), and
75
+ // str.length counts UTF-16 units, which are never more. They differ only
76
+ // for a string with a character outside ASCII, which is refused anyway, so
77
+ // its bytes are counted here, where it's refused, and a string over the
78
+ // limit in bytes is LIMIT_EXCEEDED as in every other implementation. A unit
79
+ // is at most 3 bytes, so most refused strings aren't counted at all. A lone
80
+ // surrogate counts the 3 bytes of the U+FFFD TextEncoder writes for it.
81
+ const badChar = (str) => (str.length * 3 > CAPL && new TextEncoder().encode(str).length > CAPL ? over(0) : err('BAD_CHARACTER'));
82
+
83
+ // ---------------------------------------------------------------- lookup tables
84
+
85
+ // The short codes of a first position start at SHORT_A degrees of latitude
86
+ // and SHORT_B of longitude, for precisions 0 to 7 (SPEC.md section 7).
87
+ const SHORT_A = [-10, 29, -65, -15, 23, -65, -15, 19], SHORT_B = [-10, -5, -125, -10, -10, -140, -30, -10];
88
+ const AN_N = new Float64Array(16), AN_B = new Int32Array(16), AN_T = new Float64Array(16), AN_O = new Float64Array(16), AN_M = new Float64Array(16);
89
+ for (let p = 0; p < 8; p++) {
90
+ const f = POW[p], nA = 180 * f + 1, bA = 31 - Math.clz32(nA), nB = 360 * f + 1, bB = nB < 4294967296 ? 31 - Math.clz32(nB) : 32;
91
+ AN_N[p] = nA; AN_B[p] = bA; AN_T[p] = P2[bA + 1] - nA; AN_O[p] = -SHORT_A[p] * f; AN_M[p] = 90 * f;
92
+ AN_N[8 + p] = nB; AN_B[8 + p] = bB; AN_T[8 + p] = P2[bB + 1] - nB; AN_O[8 + p] = -SHORT_B[p] * f; AN_M[8 + p] = 180 * f;
93
+ }
94
+ const INV = /* @__PURE__ */ Int32Array.from({ length: 512 }, (_, d) => (d ? ((16777216 + (d >> 1)) / d) | 0 : 0));
95
+ const TJ = /* @__PURE__ */ (() => {
96
+ const t = new Uint16Array(2048);
97
+ for (let a = 0; a < 6; a++)
98
+ for (let b = 0; b < 6 && a + b < 10; b++) {
99
+ const n = a + b + 2, code = (1 << (b + 1)) | 1;
100
+ for (let r = 0; r < 1 << (11 - n); r++) t[(code << (11 - n)) | r] = a | (b << 4) | (n << 8);
101
+ }
102
+ return t;
103
+ })();
104
+ const TR = /* @__PURE__ */ (() => {
105
+ const t = new Int32Array(2048), un = (c) => ((c >>> 1) ^ -(c & 1)) & 255;
106
+ for (let i = 0; i < 2048; i++) if (TJ[i]) t[i] = un(TJ[i] & 15) | (un((TJ[i] >> 4) & 15) << 8) | ((TJ[i] >> 8) << 16);
107
+ return t;
108
+ })();
109
+ // Character code to its 6 bits, shifted into place for each character of a
110
+ // 4-character group, and -1 for anything outside base64url. Built by pure
111
+ // calls, so a bundle that only encodes leaves them out.
112
+ const b64 = (shift) => { const t = new Int32Array(256).fill(-1); for (let i = 0; i < 64; i++) t[ALPHABET.charCodeAt(i)] = i << shift; return t; };
113
+ const E0 = /* @__PURE__ */ b64(18), E1 = /* @__PURE__ */ b64(12), E2 = /* @__PURE__ */ b64(6), E3 = /* @__PURE__ */ b64(0);
114
+
115
+ let CRC;
116
+ function crc32c(str, len) {
117
+ if (!CRC) {
118
+ CRC = new Int32Array(256);
119
+ for (let i = 0; i < 256; i++) { let c = i; for (let b = 0; b < 8; b++) c = c & 1 ? (c >>> 1) ^ 0x82f63b78 : c >>> 1; CRC[i] = c; }
120
+ }
121
+ let crc = -1;
122
+ for (let i = 0; i < len; i++) crc = CRC[(crc ^ str.charCodeAt(i)) & 255] ^ (crc >>> 8);
123
+ return ~crc >>> 0;
124
+ }
125
+
126
+ // ---------------------------------------------------------------- base64url -> bytes
127
+
128
+ const SLACK = 512, BufferCtor = globalThis.Buffer, TC39 = { alphabet: 'base64url', lastChunkHandling: 'stop-before-partial' };
129
+ let BUF = /* @__PURE__ */ new Uint8Array(0), DV = /* @__PURE__ */ new DataView(BUF.buffer), RAW = BUF;
130
+ let COPY_FROM = Infinity, NATIVE = 0, NATIVE_FROM = Infinity;
131
+
132
+ function allocBuf(size) { const b = BufferCtor ? BufferCtor.allocUnsafeSlow(size) : new Uint8Array(size); return [b, new DataView(b.buffer, b.byteOffset, size)]; }
133
+ function ensureDecBuf(bytes) {
134
+ const need = bytes + SLACK;
135
+ if (need <= BUF.length) return DV;
136
+ if (need <= KEEP) {
137
+ const size = Math.min(KEEP, Math.max(need, BUF.length * 2));
138
+ [BUF, DV] = allocBuf(size);
139
+ if (BufferCtor) RAW = BufferCtor.allocUnsafeSlow(Math.min(KEEP * 2, size * 2));
140
+ return DV;
141
+ }
142
+ return allocBuf(need)[1];
143
+ }
144
+ function shortBytes(str, len, dv) {
145
+ const whole = len & ~3;
146
+ let i = 0, w = 0, bad = 0, hi = 0, v = 0, c = 0, c1 = 0, c2 = 0, c3 = 0;
147
+ for (; i < whole; i += 4, w += 3) {
148
+ c = str.charCodeAt(i); c1 = str.charCodeAt(i + 1); c2 = str.charCodeAt(i + 2); c3 = str.charCodeAt(i + 3);
149
+ hi |= c | c1 | c2 | c3; v = E0[c & 255] | E1[c1 & 255] | E2[c2 & 255] | E3[c3 & 255]; bad |= v; dv.setInt32(w, v << 8);
150
+ }
151
+ for (v = 0, c1 = 18; i < len; i++, c1 -= 6) { c = str.charCodeAt(i); hi |= c; c2 = E3[c & 255]; bad |= c2; v |= c2 << c1; }
152
+ if (bad < 0 || hi > 127) throw badChar(str);
153
+ dv.setInt32(w, v << 8); dv.setInt32(w + 4, 0); dv.setInt32(w + 8, 0);
154
+ return dv;
155
+ }
156
+ function toBytes(str, len) {
157
+ const bytes = (len * 3 + 3) >> 2, dv = ensureDecBuf(bytes);
158
+ const b = dv === DV ? BUF : new Uint8Array(dv.buffer, dv.byteOffset, dv.byteLength), whole = len & ~3;
159
+ let w = (whole >> 2) * 3, i = whole, bad = 0, v = 0;
160
+ if (len >= NATIVE_FROM) {
161
+ // Cut only for the characters before a check, which were judged whole
162
+ // first, so badChar never sees a cut string.
163
+ if (len !== str.length) str = str.slice(0, len);
164
+ if (NATIVE === 1) {
165
+ const s = len - whole === 1 ? str.slice(0, whole) : str;
166
+ const nb = dv === DV ? b : BufferCtor.from(b.buffer, b.byteOffset, b.byteLength);
167
+ if (nb.base64urlWrite(s, 0) !== (s.length * 3) >> 2 || str.includes('+') || str.includes('/')) throw badChar(str);
168
+ } else {
169
+ let r; try { r = b.setFromBase64(str, TC39); } catch { throw badChar(str); }
170
+ if (r.read !== whole || r.written !== w) throw badChar(str);
171
+ }
172
+ } else if (len >= COPY_FROM && len === str.length && len <= RAW.length) {
173
+ const r = RAW; if (r.utf8Write(str, 0) !== len) throw badChar(str);
174
+ for (i = 0, w = 0; i < whole; i += 4, w += 3) { v = E0[r[i]] | E1[r[i + 1]] | E2[r[i + 2]] | E3[r[i + 3]]; bad |= v; dv.setInt32(w, v << 8); }
175
+ if (bad < 0) throw badChar(str);
176
+ } else return shortBytes(str, len, dv);
177
+ for (v = 0, bad = 18; i < len; i++, bad -= 6) { const c = str.charCodeAt(i), x = c < 128 ? E3[c] : -1; if (x < 0) throw badChar(str); v |= x << bad; }
178
+ dv.setInt32(w, v << 8); dv.setInt32(w + 4, 0); dv.setInt32(w + 8, 0);
179
+ return dv;
180
+ }
181
+ // Picks the fastest way this runtime has to turn a string into bytes, after
182
+ // checking that each one gives the same bytes and refuses the same strings.
183
+ // CUT is the length from which toBytes is worth calling. A pure call that
184
+ // runs when the module loads, as before, but a bundle without the decoder
185
+ // can drop it.
186
+ const CUT = /* @__PURE__ */ (() => {
187
+ const good = 'QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVotXzAxMjM0NTY3ODk';
188
+ const bad = ['QUJD*EVGR0hJSktM', 'QUJD EVGR0hJSktM', 'QUJD=EVGR0hJSktM', 'QUJD+EVGR0hJSktM', 'QUJD/EVGR0hJSktM', 'QUJDREVGR0hJSłtM', 'QUJDREVGR0hJSétM', 'QUJDR\nVGR0hJSktMT', 'QUJDREVGR0hJSktMT='];
189
+ const bytesOf = (s) => { const dv = toBytes(s, s.length); return new Uint8Array(dv.buffer, dv.byteOffset, ((s.length * 3 + 3) >> 2) + 8).join(); };
190
+ const works = (want) => {
191
+ try {
192
+ for (let cut = 0; cut < 4; cut++) if (bytesOf(good.slice(0, good.length - cut)) !== want[cut]) return false;
193
+ for (const s of bad) { try { bytesOf(s); return false; } catch (e) { if (!(e instanceof GeotiniError)) return false; } }
194
+ return true;
195
+ } catch { return false; }
196
+ };
197
+ ensureDecBuf(300);
198
+ const want = [0, 1, 2, 3].map((cut) => bytesOf(good.slice(0, good.length - cut)));
199
+ const deno = !!globalThis.Deno, bun = !!globalThis.Bun, P = BufferCtor && BufferCtor.prototype;
200
+ const tryRoute = (set) => { set(0); const ok = works(want); set(Infinity); return ok; };
201
+ if (P && typeof P.utf8Write === 'function' && tryRoute((v) => (COPY_FROM = v))) COPY_FROM = deno ? 80 : 96;
202
+ if (!deno && P && typeof P.base64urlWrite === 'function') { NATIVE = 1; if (tryRoute((v) => (NATIVE_FROM = v))) NATIVE_FROM = bun ? 200 : 232; }
203
+ if (NATIVE_FROM === Infinity && typeof BUF.setFromBase64 === 'function') { NATIVE = 2; if (tryRoute((v) => (NATIVE_FROM = v))) NATIVE_FROM = 300; }
204
+ if (NATIVE_FROM === Infinity) NATIVE = 0;
205
+ return Math.min(COPY_FROM, NATIVE_FROM, 300);
206
+ })();
207
+
208
+ // ---------------------------------------------------------------- shared decode helpers
209
+
210
+ let DD = DV, SP = 0, SEND = 0, SLAT = 0, SLNG = 0, SK1 = 0, SK2 = 0, WIDE = 0, OPEN = 0, STARTED = 0, PREC = 5, NX = 0;
211
+ // What the canonical-form checks need. It's kept off the ordinary path on
212
+ // purpose: counting steps per path and resetting four more variables per
213
+ // string made small geometries decode 2 to 5% slower on all three runtimes.
214
+ // OPTS is the six option bits of an extended header, 0 for a short one, so
215
+ // the end checks cost one test when there's nothing to check. STEPS, the
216
+ // block-coded points so far, BIG, that a wide string needed to be wide, and
217
+ // OPENED, that a ring was open, are only kept for strings with options.
218
+ // LOW counts paths without a step and polygons without a ring, and is never
219
+ // reset: a geometry is without a step when LOW grew by all it holds.
220
+ let STEPS = 0, BIG = 0, OPENED = 0, OPTS = 0, LOW = 0;
221
+ // maxLength and maxPositions. CAPL and CAPP are this call's: the defaults,
222
+ // except inside a call that was given others. LEFT is what's left of
223
+ // maxPositions in a string that's counted.
224
+ //
225
+ // A string that's too short to hold more than maxPositions isn't counted,
226
+ // and is decoded by decodeBody, decodePolygon and decodePath, which know
227
+ // nothing of the limit. A string that is counted goes through bodyCounted,
228
+ // polygonCounted and pathCounted below them. They're separate on purpose.
229
+ // With the counting written into the three hot functions, behind branches
230
+ // that a short string never takes, short geometries still decoded 1 to 2%
231
+ // slower on Node, and a call to a helper in those branches cost the same.
232
+ // LIMIT_EXCEEDED isn't about the string, which may be valid, so the message
233
+ // names the option that decides.
234
+ const over = (positions) => new GeotiniError('LIMIT_EXCEEDED', 'The string ' + (positions ? 'holds more than maxPositions' : 'is longer than maxLength') + ' allows');
235
+ const XF = [1, 1], XP = [0, 0], XV = [0, 0], XK = [0, 0];
236
+ let RA = 0, RB = 0, RK1 = 0, RK2 = 0;
237
+
238
+ const bits32 = (d, p, n) =>
239
+ n > 16
240
+ ? (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (47 - n)) * 65536 + ((d.getInt32((p + n - 16) >> 3) << ((p + n - 16) & 7)) >>> 16)
241
+ : ((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - n);
242
+
243
+ function bitsBig(d, p, n) {
244
+ let v = 0;
245
+ for (; n > 24; n -= 24, p += 24) v = v * 16777216 + ((d.getInt32(p >> 3) << (p & 7)) >>> 8);
246
+ return v * P2[n] + (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - n));
247
+ }
248
+ function readCount(d) {
249
+ const p = SP, w = d.getInt32(p >> 3) << (p & 7), z = Math.clz32(w);
250
+ SP = p + 2 * z + 4;
251
+ if (z < 11) return ((w << z) >>> (28 - z)) - 8;
252
+ if (z > 23) throw err(CT, p + 24);
253
+ return bitsBig(d, p + z, z + 4) - 8;
254
+ }
255
+ function readMode(d) {
256
+ const p = SP, w = d.getInt32(p >> 3) << (p & 7);
257
+ if (w >= 0) { SP = p + 2; return (w >>> 30) & 1; }
258
+ if ((w & 0x40000000) === 0) { SP = p + 2; return 2; }
259
+ if ((w & 0x20000000) === 0) { SP = p + 3; return 3; }
260
+ SP = p + 4; return 4 + ((w >>> 28) & 1);
261
+ }
262
+ function readParam(d, p, prev, width, kMax, minT) {
263
+ const w = d.getInt32(p >> 3) << (p & 7), z = Math.clz32(w);
264
+ let k;
265
+ if (z < 8) { SP = p + z + 1; k = prev + ((z >>> 1) ^ -(z & 1)); if (k > kMax) throw err(R, SP); }
266
+ else {
267
+ k = (w << 8) >>> (32 - width); SP = p + 8 + width;
268
+ if (k > kMax) { if (k > kMax + 2) throw err(R, SP); k = kMax - k; }
269
+ const df = k - prev; if ((df < 0 ? -2 * df - 1 : 2 * df) < 8) throw err(NC, SP);
270
+ }
271
+ if (k < minT) throw err(R, SP);
272
+ return k;
273
+ }
274
+ function divSlope(a, b) {
275
+ if (b === 0) return 0;
276
+ const ad = b < 0 ? -b : b;
277
+ if (ad < 512) { const q = Math.imul(a, INV[ad]) >> 12; return b < 0 ? -q | 0 : q; }
278
+ return ((a << 12) / b) | 0;
279
+ }
280
+ // SPEC.md section 9.4: 4096 when lat0 isn't a latitude, and otherwise from
281
+ // u = floor(|lat0| * 256 / (90 * 10^p)). a / (d / 256) is that floor exactly:
282
+ // d / 256 is exact in a double, and the quotient is never within an ulp of
283
+ // the next integer (256 a < 2^53 below precision 7; d / 256 is an integer
284
+ // from 7 on, and a < 2^53).
285
+ function cos2Int(lat0, prec) {
286
+ const a = lat0 < 0 ? -lat0 : lat0, d = 90 * POW[prec];
287
+ if (a > d) return 4096;
288
+ const u = Math.floor(a / (d / 256)), i = u >> 4;
289
+ return i >= 16 ? 16 : (COS2[i] * (16 - (u & 15)) + COS2[i + 1] * (u & 15) + 8) >> 4;
290
+ }
291
+ function readTruncAnchor(d, p, prec) {
292
+ if (prec === 5) {
293
+ let v = (d.getInt32(p >> 3) << (p & 7)) >>> 8; p += 24;
294
+ if (v >= 15554431) { v = (v << 1) + ((d.getInt32(p >> 3) << (p & 7)) >>> 31) - 15554431; p++; }
295
+ SLAT = v > 15500000 ? v - 24500001 : v - 6500000;
296
+ v = (d.getInt32(p >> 3) << (p & 7)) >>> 7; p += 25;
297
+ if (v >= 31108863) { v = (v << 1) + ((d.getInt32(p >> 3) << (p & 7)) >>> 31) - 31108863; p++; }
298
+ SLNG = v > 32000000 ? v - 50000001 : v - 14000000;
299
+ return p;
300
+ }
301
+ if (prec === 6) {
302
+ let v = (((d.getInt32(p >> 3) << (p & 7)) >>> 16) << 11) | ((d.getInt32((p + 16) >> 3) << ((p + 16) & 7)) >>> 21); p += 27;
303
+ if (v >= 88435455) { v = (v << 1) + ((d.getInt32(p >> 3) << (p & 7)) >>> 31) - 88435455; p++; }
304
+ SLAT = v > 105000000 ? v - 195000001 : v - 15000000;
305
+ v = (((d.getInt32(p >> 3) << (p & 7)) >>> 16) << 12) | ((d.getInt32((p + 16) >> 3) << ((p + 16) & 7)) >>> 20); p += 28;
306
+ if (v >= 176870911) { v = (v << 1) + ((d.getInt32(p >> 3) << (p & 7)) >>> 31) - 176870911; p++; }
307
+ SLNG = v > 210000000 ? v - 390000001 : v - 30000000;
308
+ return p;
309
+ }
310
+ for (let s = 0; s < 2; s++) {
311
+ const idx = (s << 3) + prec, b = AN_B[idx], t = AN_T[idx], N = AN_N[idx];
312
+ let v = bits32(d, p, b); p += b;
313
+ if (v >= t) { v = v * 2 + ((d.getInt32(p >> 3) << (p & 7)) >>> 31) - t; p++; if (v >= N) throw err(R, p); }
314
+ let c = v - AN_O[idx]; if (c > AN_M[idx]) c -= N; if (c < -AN_M[idx]) c += N;
315
+ if (s) SLNG = c | 0; else SLAT = c | 0;
316
+ }
317
+ return p;
318
+ }
319
+ function readPositionFast(d, prec) {
320
+ const p = SP;
321
+ if (STARTED) {
322
+ const w = d.getInt32(p >> 3) << (p & 7);
323
+ if (w >= 0) { SP = p + 1; return; }
324
+ const L = ((w << 1) >>> 27) + 1, a = bits32(d, p + 6, L), b = bits32(d, p + 6 + L, L);
325
+ if (!(((a >>> (L - 1)) | (b >>> (L - 1))) & 1)) throw err(NC, p + 6 + (L << 1));
326
+ SLAT = (SLAT + ((a >>> 1) ^ -(a & 1))) | 0; SLNG = (SLNG + ((b >>> 1) ^ -(b & 1))) | 0;
327
+ SP = p + 6 + (L << 1);
328
+ } else { STARTED = 1; SP = readTruncAnchor(d, p, prec); }
329
+ }
330
+
331
+ // ---------------------------------------------------------------- hot decode loops (100% pure Int32, ZERO calls inside loops!)
332
+
333
+ // Modes 0 (delta) and 1 (d2)
334
+ function bodyCoord(d, p, end, out, o, m, lin, geo, a, b, f, k) {
335
+ p |= 0; end |= 0; o |= 0; m |= 0; lin |= 0; geo |= 0; a |= 0; b |= 0; f = +f; k |= 0;
336
+ const tj = TJ, tr = TR;
337
+ const sa = geo ? 16 : 24, sb = 40 - sa, ca = geo ? 4 : 0, cb = 4 - ca, ba = geo ? 0 : 1, bb = 1 - ba;
338
+ let va = 0, vb = 0, w = 0, e = 0, x = 0, y = 0, n = 0, z1 = 0, z2 = 0;
339
+
340
+ while (o < m) {
341
+ if (p > end) throw err(T);
342
+ const stop = o + BLOCK < m ? o + BLOCK : m;
343
+ w = d.getInt32(p >> 3) << (p & 7); x = Math.clz32(w);
344
+ if (x < 8) { p += x + 1; k = (k + ((x >>> 1) ^ -(x & 1))) | 0; }
345
+ else { x = LK[(w << 8) >>> 27]; n = x - k; if ((n < 0 ? -2 * n - 1 : 2 * n) < 8) throw err(NC, p + 13, end); k = x; p += 13; }
346
+ if (k < -2 || k > KMAX) throw err(R, p, end);
347
+
348
+ if (k === -2) {
349
+ k = 0;
350
+ if (lin === 0) { const fa = a / f, fb = b / f; for (; o < stop; o++) out[o] = [fa, fb]; }
351
+ else for (; o < stop; o++) { a = (a + va) | 0; b = (b + vb) | 0; out[o] = [a / f, b / f]; }
352
+ continue;
353
+ }
354
+ if (k === -1) {
355
+ k = 0; const cnt = stop - o, mask = ((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - cnt); p += cnt;
356
+ if (mask === 0) throw err(NC, p, end);
357
+ for (let bit = 1 << (cnt - 1); o < stop; o++, bit >>>= 1) {
358
+ if (mask & bit) {
359
+ w = d.getInt32(p >> 3) << (p & 7); e = tj[w >>> 21];
360
+ if (e !== 0) { x = e & 15; y = (e >> 4) & 15; p += e >> 8; }
361
+ else {
362
+ x = Math.clz32(w); if (x > 23) { p += 24; x = Math.clz32(d.getInt32(p >> 3) << (p & 7)); if (x > 11) throw err(R, p + 12, end); p += x + 1; x += 24; } else p += x + 1;
363
+ y = Math.clz32(d.getInt32(p >> 3) << (p & 7)); if (y > 23) { p += 24; y = Math.clz32(d.getInt32(p >> 3) << (p & 7)); if (y > 11) throw err(R, p + 12, end); p += y + 1; y += 24; } else p += y + 1;
364
+ if (x >= 6) { n = x - 4; if (n > 31) throw err(R, p - x - y + 34, end); x = 1; if (n > 16) { x = (1 << (n - 16)) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (47 - n)); p += n - 16; n = 16; } x = (((x << n) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - n))) + 2) | 0; p += n; if ((x >>> 0) < 2) throw err(R, p, end); }
365
+ if (y >= 6) { n = y - 4; if (n > 31) throw err(R, p - y + 35 - ((x >>> 0) >= 6 ? 31 - Math.clz32(x - 2) : 0), end); y = 1; if (n > 16) { y = (1 << (n - 16)) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (47 - n)); p += n - 16; n = 16; } y = (((y << n) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - n))) + 2) | 0; p += n; if ((y >>> 0) < 2) throw err(R, p, end); }
366
+ }
367
+ if (x === 0) { if (y === -1) throw err(R, p, end); y = (y + 1) | 0; }
368
+ n = (x ^ y) & geo; x ^= n; y ^= n;
369
+ va = ((va & lin) + ((x >>> 1) ^ -(x & 1))) | 0; vb = ((vb & lin) + ((y >>> 1) ^ -(y & 1))) | 0;
370
+ } else { va &= lin; vb &= lin; }
371
+ a = (a + va) | 0; b = (b + vb) | 0; out[o] = [a / f, b / f];
372
+ }
373
+ continue;
374
+ }
375
+
376
+ const kk = k + k, mk = (1 << k) - 1, one = kk < 15;
377
+ if (k === 0) {
378
+ for (; o < stop; o++) {
379
+ e = tr[(d.getInt32(p >> 3) << (p & 7)) >>> 21]; if (e === 0) break; p += e >> 16;
380
+ va = ((va & lin) + ((e << sa) >> 24)) | 0; vb = ((vb & lin) + ((e << sb) >> 24)) | 0;
381
+ a = (a + va) | 0; b = (b + vb) | 0; out[o] = [a / f, b / f];
382
+ }
383
+ } else if (k === 1) {
384
+ for (; o < stop; o++) {
385
+ w = d.getInt32(p >> 3) << (p & 7); e = tj[w >>> 21]; if (e === 0) break;
386
+ n = e >> 8; w = (w << n) >>> 30; p += n + 2;
387
+ va = ((va & lin) + (((e >> ca) & 15) ^ -((w >> ba) & 1))) | 0;
388
+ vb = ((vb & lin) + (((e >> cb) & 15) ^ -((w >> bb) & 1))) | 0;
389
+ a = (a + va) | 0; b = (b + vb) | 0; out[o] = [a / f, b / f];
390
+ }
391
+ }
392
+ for (; o < stop; o++) {
393
+ w = d.getInt32(p >> 3) << (p & 7); e = tj[w >>> 21];
394
+ if (e !== 0) {
395
+ if (one) {
396
+ n = e >> 8; w = ((w << n) >>> 1) >>> (31 - kk);
397
+ z1 = ((e & 15) << k) | (w >>> k); z2 = (((e >> 4) & 15) << k) | (w & mk); p += n + kk;
398
+ } else {
399
+ p += e >> 8;
400
+ z1 = ((e & 15) << k) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - k)); p += k;
401
+ z2 = (((e >> 4) & 15) << k) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - k)); p += k;
402
+ }
403
+ } else {
404
+ x = Math.clz32(w); if (x > 23) { p += 24; x = Math.clz32(d.getInt32(p >> 3) << (p & 7)); if (x > 11) throw err(R, p + 12 - k, end); p += x + 1; x += 24; } else p += x + 1;
405
+ y = Math.clz32(d.getInt32(p >> 3) << (p & 7)); if (y > 23) { p += 24; y = Math.clz32(d.getInt32(p >> 3) << (p & 7)); if (y > 11) throw err(R, x + k > 35 ? p - x + 11 - k : p + 12 - k, end); p += y + 1; y += 24; } else p += y + 1;
406
+ z1 = ((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - k); p += k;
407
+ z2 = ((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - k); p += k;
408
+ if (x >= 6) { n = x - 4; if (n + k > 31) throw err(R, p - x - y - k - 2 * k + 34, end); x = 1; if (n > 16) { x = (1 << (n - 16)) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (47 - n)); p += n - 16; n = 16; } x = (((x << n) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - n))) + 2) | 0; p += n; if ((x >>> 0) < 2 || (k && (x >>> (32 - k)))) throw err(R, y + k > 35 ? p - y - k - 2 * k + 35 - (31 - Math.clz32(x - 2)) : p, end); }
409
+ if (y >= 6) { n = y - 4; if (n + k > 31) throw err(R, p - y - k - 2 * k + 35 - ((x >>> 0) >= 6 ? 31 - Math.clz32(x - 2) : 0), end); y = 1; if (n > 16) { y = (1 << (n - 16)) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (47 - n)); p += n - 16; n = 16; } y = (((y << n) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - n))) + 2) | 0; p += n; if ((y >>> 0) < 2 || (k && (y >>> (32 - k)))) throw err(R, p, end); }
410
+ z1 |= x << k; z2 |= y << k;
411
+ }
412
+ n = (z1 ^ z2) & geo; z1 ^= n; z2 ^= n;
413
+ va = ((va & lin) + ((z1 >>> 1) ^ -(z1 & 1))) | 0; vb = ((vb & lin) + ((z2 >>> 1) ^ -(z2 & 1))) | 0;
414
+ a = (a + va) | 0; b = (b + vb) | 0; out[o] = [a / f, b / f];
415
+ }
416
+ }
417
+ RA = a; RB = b; RK1 = k;
418
+ return p;
419
+ }
420
+
421
+ // Mode 4 (ema) — hoisted o === 1, ZERO calls inside loop!
422
+ function bodyEma(d, p, end, out, o, m, geo, a, b, f, k) {
423
+ p |= 0; end |= 0; o |= 0; m |= 0; geo |= 0; a |= 0; b |= 0; f = +f; k |= 0;
424
+ const tj = TJ, tr = TR, sa = geo ? 16 : 24, sb = 40 - sa, ca = geo ? 4 : 0, cb = 4 - ca, ba = geo ? 0 : 1, bb = 1 - ba;
425
+ let va = 0, vb = 0, w = 0, e = 0, x = 0, y = 0, n = 0, z1 = 0, z2 = 0;
426
+
427
+ while (o < m) {
428
+ if (p > end) throw err(T);
429
+ const stop = o + BLOCK < m ? o + BLOCK : m;
430
+ w = d.getInt32(p >> 3) << (p & 7); x = Math.clz32(w);
431
+ if (x < 8) { p += x + 1; k = (k + ((x >>> 1) ^ -(x & 1))) | 0; }
432
+ else { x = LK[(w << 8) >>> 27]; n = x - k; if ((n < 0 ? -2 * n - 1 : 2 * n) < 8) throw err(NC, p + 13, end); k = x; p += 13; }
433
+ if (k < -2 || k > KMAX) throw err(R, p, end);
434
+
435
+ if (k < 0) {
436
+ let mask = 0; const cnt = stop - o;
437
+ if (k === -1) { mask = ((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - cnt); p += cnt; if (mask === 0) throw err(NC, p, end); }
438
+ k = 0;
439
+ for (let bit = 1 << (cnt - 1); o < stop; o++, bit >>>= 1) {
440
+ if (mask & bit) {
441
+ w = d.getInt32(p >> 3) << (p & 7); e = tj[w >>> 21];
442
+ if (e !== 0) { x = e & 15; y = (e >> 4) & 15; p += e >> 8; }
443
+ else {
444
+ x = Math.clz32(w); if (x > 23) { p += 24; x = Math.clz32(d.getInt32(p >> 3) << (p & 7)); if (x > 11) throw err(R, p + 12, end); p += x + 1; x += 24; } else p += x + 1;
445
+ y = Math.clz32(d.getInt32(p >> 3) << (p & 7)); if (y > 23) { p += 24; y = Math.clz32(d.getInt32(p >> 3) << (p & 7)); if (y > 11) throw err(R, p + 12, end); p += y + 1; y += 24; } else p += y + 1;
446
+ if (x >= 6) { n = x - 4; if (n > 31) throw err(R, p - x - y + 34, end); x = 1; if (n > 16) { x = (1 << (n - 16)) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (47 - n)); p += n - 16; n = 16; } x = (((x << n) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - n))) + 2) | 0; p += n; if ((x >>> 0) < 2) throw err(R, p, end); }
447
+ if (y >= 6) { n = y - 4; if (n > 31) throw err(R, p - y + 35 - ((x >>> 0) >= 6 ? 31 - Math.clz32(x - 2) : 0), end); y = 1; if (n > 16) { y = (1 << (n - 16)) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (47 - n)); p += n - 16; n = 16; } y = (((y << n) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - n))) + 2) | 0; p += n; if ((y >>> 0) < 2) throw err(R, p, end); }
448
+ }
449
+ if (x === 0) { if (y === -1) throw err(R, p, end); y = (y + 1) | 0; }
450
+ n = (x ^ y) & geo; x ^= n; y ^= n; z1 = (x >>> 1) ^ -(x & 1); z2 = (y >>> 1) ^ -(y & 1);
451
+ } else { z1 = 0; z2 = 0; }
452
+ x = (va + z1) | 0; y = (vb + z2) | 0; va = o === 1 ? x : (va + x) >> 1; vb = o === 1 ? y : (vb + y) >> 1;
453
+ a = (a + x) | 0; b = (b + y) | 0; out[o] = [a / f, b / f];
454
+ }
455
+ continue;
456
+ }
457
+
458
+ const kk = k + k, mk = (1 << k) - 1, one = kk < 15;
459
+ if (o === 1) {
460
+ w = d.getInt32(p >> 3) << (p & 7); e = tj[w >>> 21];
461
+ if (e !== 0) {
462
+ if (one) { n = e >> 8; w = ((w << n) >>> 1) >>> (31 - kk); z1 = ((e & 15) << k) | (w >>> k); z2 = (((e >> 4) & 15) << k) | (w & mk); p += n + kk; }
463
+ else { p += e >> 8; z1 = ((e & 15) << k) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - k)); p += k; z2 = (((e >> 4) & 15) << k) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - k)); p += k; }
464
+ } else {
465
+ x = Math.clz32(w); if (x > 23) { p += 24; x = Math.clz32(d.getInt32(p >> 3) << (p & 7)); if (x > 11) throw err(R, p + 12 - k, end); p += x + 1; x += 24; } else p += x + 1;
466
+ y = Math.clz32(d.getInt32(p >> 3) << (p & 7)); if (y > 23) { p += 24; y = Math.clz32(d.getInt32(p >> 3) << (p & 7)); if (y > 11) throw err(R, x + k > 35 ? p - x + 11 - k : p + 12 - k, end); p += y + 1; y += 24; } else p += y + 1;
467
+ z1 = ((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - k); p += k;
468
+ z2 = ((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - k); p += k;
469
+ if (x >= 6) { n = x - 4; if (n + k > 31) throw err(R, p - x - y - k - 2 * k + 34, end); x = 1; if (n > 16) { x = (1 << (n - 16)) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (47 - n)); p += n - 16; n = 16; } x = (((x << n) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - n))) + 2) | 0; p += n; if ((x >>> 0) < 2 || (k && (x >>> (32 - k)))) throw err(R, y + k > 35 ? p - y - k - 2 * k + 35 - (31 - Math.clz32(x - 2)) : p, end); }
470
+ if (y >= 6) { n = y - 4; if (n + k > 31) throw err(R, p - y - k - 2 * k + 35 - ((x >>> 0) >= 6 ? 31 - Math.clz32(x - 2) : 0), end); y = 1; if (n > 16) { y = (1 << (n - 16)) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (47 - n)); p += n - 16; n = 16; } y = (((y << n) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - n))) + 2) | 0; p += n; if ((y >>> 0) < 2 || (k && (y >>> (32 - k)))) throw err(R, p, end); }
471
+ z1 |= x << k; z2 |= y << k;
472
+ }
473
+ n = (z1 ^ z2) & geo; z1 ^= n; z2 ^= n;
474
+ va = (z1 >>> 1) ^ -(z1 & 1); vb = (z2 >>> 1) ^ -(z2 & 1);
475
+ a = (a + va) | 0; b = (b + vb) | 0; out[1] = [a / f, b / f]; o = 2;
476
+ if (o >= stop) continue;
477
+ }
478
+ if (k === 0) {
479
+ for (; o < stop; o++) {
480
+ e = tr[(d.getInt32(p >> 3) << (p & 7)) >>> 21]; if (e === 0) break; p += e >> 16;
481
+ x = (va + ((e << sa) >> 24)) | 0; y = (vb + ((e << sb) >> 24)) | 0;
482
+ va = (va + x) >> 1; vb = (vb + y) >> 1;
483
+ a = (a + x) | 0; b = (b + y) | 0; out[o] = [a / f, b / f];
484
+ }
485
+ } else if (k === 1) {
486
+ for (; o < stop; o++) {
487
+ w = d.getInt32(p >> 3) << (p & 7); e = tj[w >>> 21]; if (e === 0) break;
488
+ n = e >> 8; w = (w << n) >>> 30; p += n + 2;
489
+ x = (va + (((e >> ca) & 15) ^ -((w >> ba) & 1))) | 0; y = (vb + (((e >> cb) & 15) ^ -((w >> bb) & 1))) | 0;
490
+ va = (va + x) >> 1; vb = (vb + y) >> 1;
491
+ a = (a + x) | 0; b = (b + y) | 0; out[o] = [a / f, b / f];
492
+ }
493
+ }
494
+ for (; o < stop; o++) {
495
+ w = d.getInt32(p >> 3) << (p & 7); e = tj[w >>> 21];
496
+ if (e !== 0) {
497
+ if (one) { n = e >> 8; w = ((w << n) >>> 1) >>> (31 - kk); z1 = ((e & 15) << k) | (w >>> k); z2 = (((e >> 4) & 15) << k) | (w & mk); p += n + kk; }
498
+ else { p += e >> 8; z1 = ((e & 15) << k) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - k)); p += k; z2 = (((e >> 4) & 15) << k) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - k)); p += k; }
499
+ } else {
500
+ x = Math.clz32(w); if (x > 23) { p += 24; x = Math.clz32(d.getInt32(p >> 3) << (p & 7)); if (x > 11) throw err(R, p + 12 - k, end); p += x + 1; x += 24; } else p += x + 1;
501
+ y = Math.clz32(d.getInt32(p >> 3) << (p & 7)); if (y > 23) { p += 24; y = Math.clz32(d.getInt32(p >> 3) << (p & 7)); if (y > 11) throw err(R, x + k > 35 ? p - x + 11 - k : p + 12 - k, end); p += y + 1; y += 24; } else p += y + 1;
502
+ z1 = ((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - k); p += k;
503
+ z2 = ((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - k); p += k;
504
+ if (x >= 6) { n = x - 4; if (n + k > 31) throw err(R, p - x - y - k - 2 * k + 34, end); x = 1; if (n > 16) { x = (1 << (n - 16)) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (47 - n)); p += n - 16; n = 16; } x = (((x << n) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - n))) + 2) | 0; p += n; if ((x >>> 0) < 2 || (k && (x >>> (32 - k)))) throw err(R, y + k > 35 ? p - y - k - 2 * k + 35 - (31 - Math.clz32(x - 2)) : p, end); }
505
+ if (y >= 6) { n = y - 4; if (n + k > 31) throw err(R, p - y - k - 2 * k + 35 - ((x >>> 0) >= 6 ? 31 - Math.clz32(x - 2) : 0), end); y = 1; if (n > 16) { y = (1 << (n - 16)) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (47 - n)); p += n - 16; n = 16; } y = (((y << n) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - n))) + 2) | 0; p += n; if ((y >>> 0) < 2 || (k && (y >>> (32 - k)))) throw err(R, p, end); }
506
+ z1 |= x << k; z2 |= y << k;
507
+ }
508
+ n = (z1 ^ z2) & geo; z1 ^= n; z2 ^= n;
509
+ x = (va + ((z1 >>> 1) ^ -(z1 & 1))) | 0; y = (vb + ((z2 >>> 1) ^ -(z2 & 1))) | 0;
510
+ va = (va + x) >> 1; vb = (vb + y) >> 1;
511
+ a = (a + x) | 0; b = (b + y) | 0; out[o] = [a / f, b / f];
512
+ }
513
+ }
514
+ RA = a; RB = b; RK1 = k;
515
+ return p;
516
+ }
517
+
518
+ // Modes 2 (slope-fold) and 5 (slope-lin): ZERO calls inside loop, fast !one (p=6) path!
519
+ function bodySlope(d, p, end, out, o, m, lin, geo, a, b, f, k1, k2) {
520
+ p |= 0; end |= 0; o |= 0; m |= 0; lin |= 0; geo |= 0; a |= 0; b |= 0; f = +f; k1 |= 0; k2 |= 0;
521
+ const tj = TJ, inv = INV;
522
+ let sw = ~geo, s = 0, g = 0, pM = 0, w = 0, e = 0, x = 0, y = 0, n = 0, z1 = 0, z2 = 0, dM = 0, dm = 0;
523
+
524
+ while (o < m) {
525
+ if (p > end) throw err(T);
526
+ const stop = o + BLOCK < m ? o + BLOCK : m;
527
+ w = d.getInt32(p >> 3) << (p & 7); x = Math.clz32(w);
528
+ if (x < 8) { p += x + 1; k1 = (k1 + ((x >>> 1) ^ -(x & 1))) | 0; }
529
+ else { x = LK[(w << 8) >>> 27]; n = x - k1; if ((n < 0 ? -2 * n - 1 : 2 * n) < 8) throw err(NC, p + 13, end); k1 = x; p += 13; }
530
+ if (k1 >>> 0 > KMAX) throw err(R, p, end);
531
+
532
+ let bk2 = k2;
533
+ if (!(o === 1 && m === 2)) {
534
+ w = d.getInt32(p >> 3) << (p & 7); x = Math.clz32(w);
535
+ if (x < 8) { p += x + 1; bk2 = (k2 + ((x >>> 1) ^ -(x & 1))) | 0; }
536
+ else { x = LK[(w << 8) >>> 27]; n = x - k2; if ((n < 0 ? -2 * n - 1 : 2 * n) < 8) throw err(NC, p + 13, end); bk2 = x; p += 13; }
537
+ if (bk2 < -1 || bk2 > KMAX) throw err(R, p, end);
538
+ k2 = bk2 < 0 ? 0 : bk2;
539
+ }
540
+
541
+ if (o === 1) {
542
+ w = d.getInt32(p >> 3) << (p & 7); e = tj[w >>> 21];
543
+ if (e !== 0) {
544
+ p += e >> 8;
545
+ z1 = ((e & 15) << k1) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - k1)); p += k1;
546
+ z2 = (((e >> 4) & 15) << k1) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - k1)); p += k1;
547
+ } else {
548
+ x = Math.clz32(w); if (x > 23) { p += 24; x = Math.clz32(d.getInt32(p >> 3) << (p & 7)); if (x > 11) throw err(R, p + 12 - k1, end); p += x + 1; x += 24; } else p += x + 1;
549
+ y = Math.clz32(d.getInt32(p >> 3) << (p & 7)); if (y > 23) { p += 24; y = Math.clz32(d.getInt32(p >> 3) << (p & 7)); if (y > 11) throw err(R, x + k1 > 35 ? p - x + 11 - k1 : p + 12 - k1, end); p += y + 1; y += 24; } else p += y + 1;
550
+ z1 = ((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - k1); p += k1;
551
+ z2 = ((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - k1); p += k1;
552
+ if (x >= 6) { n = x - 4; if (n + k1 > 31) throw err(R, p - x - y - k1 - 2 * k1 + 34, end); x = 1; if (n > 16) { x = (1 << (n - 16)) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (47 - n)); p += n - 16; n = 16; } x = (((x << n) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - n))) + 2) | 0; p += n; if ((x >>> 0) < 2 || (k1 && (x >>> (32 - k1)))) throw err(R, y + k1 > 35 ? p - y - k1 - 2 * k1 + 35 - (31 - Math.clz32(x - 2)) : p, end); }
553
+ if (y >= 6) { n = y - 4; if (n + k1 > 31) throw err(R, p - y - k1 - 2 * k1 + 35 - ((x >>> 0) >= 6 ? 31 - Math.clz32(x - 2) : 0), end); y = 1; if (n > 16) { y = (1 << (n - 16)) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (47 - n)); p += n - 16; n = 16; } y = (((y << n) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - n))) + 2) | 0; p += n; if ((y >>> 0) < 2 || (k1 && (y >>> (32 - k1)))) throw err(R, p, end); }
554
+ z1 |= x << k1; z2 |= y << k1;
555
+ }
556
+ const da = (z1 >>> 1) ^ -(z1 & 1), db = (z2 >>> 1) ^ -(z2 & 1);
557
+ const lm = (db < 0 ? -db : db) >= (da < 0 ? -da : da);
558
+ sw = lm ? ~geo : geo; dM = lm ? db : da; dm = lm ? da : db;
559
+ x = dM < 0 ? -dM : dM;
560
+ s = x === 0 ? 0 : x < 512 ? ((y = Math.imul(dm, inv[x]) >> 12), dM < 0 ? -y | 0 : y) : ((dm << 12) / dM) | 0;
561
+ pM = dM; g = dM !== 0 ? (dM >> 31) : 0;
562
+ if (geo) { a = (a + db) | 0; b = (b + da) | 0; } else { a = (a + da) | 0; b = (b + db) | 0; }
563
+ out[1] = [a / f, b / f]; o = 2;
564
+ if (o >= stop) continue;
565
+ }
566
+
567
+ if (bk2 < 0) {
568
+ for (; o < stop; o++) {
569
+ w = d.getInt32(p >> 3) << (p & 7); x = Math.clz32(w);
570
+ if (x > 23) { p += 24; x = Math.clz32(d.getInt32(p >> 3) << (p & 7)); if (x > 11) throw err(R, p + 12 - k1, end); p += x + 1; x += 24; } else p += x + 1;
571
+ z1 = ((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - k1); p += k1;
572
+ if (x >= 6) { n = x - 4; if (n + k1 > 31) throw err(R, p - x - 2 * k1 + 35, end); x = 1; if (n > 16) { x = (1 << (n - 16)) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (47 - n)); p += n - 16; n = 16; } x = (((x << n) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - n))) + 2) | 0; p += n; if ((x >>> 0) < 2 || (k1 && (x >>> (32 - k1)))) throw err(R, p, end); }
573
+ z1 |= x << k1;
574
+ if (lin) dM = (pM + ((z1 >>> 1) ^ -(z1 & 1))) | 0;
575
+ else { x = z1 >>> 3; dM = (z1 & 7) === 7 ? ~x : (z1 - x) | 0; dM = ((dM ^ g) - g) | 0; }
576
+ dm = (Math.imul(dM, s) + 2048) >> 12;
577
+ if (sw) { b = (b + dM) | 0; a = (a + dm) | 0; } else { a = (a + dM) | 0; b = (b + dm) | 0; }
578
+ out[o] = [a / f, b / f]; pM = dM;
579
+ const adm = dm < 0 ? -dm : dm, adM = dM < 0 ? -dM : dM;
580
+ if (adm > adM) { sw = ~sw; s = adm < 512 ? ((x = Math.imul(dM, inv[adm]) >> 12), dm < 0 ? -x | 0 : x) : ((dM << 12) / dm) | 0; pM = dm; }
581
+ if (pM !== 0) g = pM >> 31;
582
+ }
583
+ continue;
584
+ }
585
+
586
+ const kk = k1 + bk2, m2 = (1 << bk2) - 1, one = kk < 15;
587
+ for (; o < stop; o++) {
588
+ w = d.getInt32(p >> 3) << (p & 7); e = tj[w >>> 21];
589
+ if (e !== 0) {
590
+ if (one) {
591
+ n = e >> 8; w = ((w << n) >>> 1) >>> (31 - kk);
592
+ z1 = ((e & 15) << k1) | (w >>> bk2); z2 = (((e >> 4) & 15) << bk2) | (w & m2); p += n + kk;
593
+ } else {
594
+ p += e >> 8;
595
+ z1 = ((e & 15) << k1) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - k1)); p += k1;
596
+ z2 = (((e >> 4) & 15) << bk2) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - bk2)); p += bk2;
597
+ }
598
+ } else {
599
+ x = Math.clz32(w); if (x > 23) { p += 24; x = Math.clz32(d.getInt32(p >> 3) << (p & 7)); if (x > 11) throw err(R, p + 12 - k1, end); p += x + 1; x += 24; } else p += x + 1;
600
+ y = Math.clz32(d.getInt32(p >> 3) << (p & 7)); if (y > 23) { p += 24; y = Math.clz32(d.getInt32(p >> 3) << (p & 7)); if (y > 11) throw err(R, x + k1 > 35 ? p - x + 11 - k1 : p + 12 - bk2, end); p += y + 1; y += 24; } else p += y + 1;
601
+ z1 = ((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - k1); p += k1;
602
+ z2 = ((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - bk2); p += bk2;
603
+ if (x >= 6) { n = x - 4; if (n + k1 > 31) throw err(R, p - x - y - bk2 - 2 * k1 + 34, end); x = 1; if (n > 16) { x = (1 << (n - 16)) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (47 - n)); p += n - 16; n = 16; } x = (((x << n) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - n))) + 2) | 0; p += n; if ((x >>> 0) < 2 || (k1 && (x >>> (32 - k1)))) throw err(R, y + bk2 > 35 ? p - y - k1 - 2 * bk2 + 35 - (31 - Math.clz32(x - 2)) : p, end); }
604
+ if (y >= 6) { n = y - 4; if (n + bk2 > 31) throw err(R, p - y - k1 - 2 * bk2 + 35 - ((x >>> 0) >= 6 ? 31 - Math.clz32(x - 2) : 0), end); y = 1; if (n > 16) { y = (1 << (n - 16)) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (47 - n)); p += n - 16; n = 16; } y = (((y << n) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - n))) + 2) | 0; p += n; if ((y >>> 0) < 2 || (bk2 && (y >>> (32 - bk2)))) throw err(R, p, end); }
605
+ z1 |= x << k1; z2 |= y << bk2;
606
+ }
607
+ if (lin) dM = (pM + ((z1 >>> 1) ^ -(z1 & 1))) | 0;
608
+ else { x = z1 >>> 3; dM = (z1 & 7) === 7 ? ~x : (z1 - x) | 0; dM = ((dM ^ g) - g) | 0; }
609
+ y = (z2 >>> 1) ^ -(z2 & 1);
610
+ dm = (((Math.imul(dM, s) + 2048) >> 12) + y) | 0;
611
+ if (sw) { b = (b + dM) | 0; a = (a + dm) | 0; } else { a = (a + dM) | 0; b = (b + dm) | 0; }
612
+ out[o] = [a / f, b / f]; pM = dM;
613
+ const adm = dm < 0 ? -dm : dm, adM = dM < 0 ? -dM : dM;
614
+ if (adm > adM) {
615
+ sw = ~sw;
616
+ if (adm < 512) { x = Math.imul(dM, inv[adm]) >> 12; n = dm >> 31; s = ((x ^ n) - n) | 0; }
617
+ else s = ((dM << 12) / dm) | 0;
618
+ pM = dm;
619
+ } else {
620
+ if (adM < 512) { x = Math.imul(y, inv[adM]) >> 12; n = dM >> 31; x = ((x ^ n) - n) | 0; if (adM < 4) x >>= 1; }
621
+ else x = ((y << 12) / dM) | 0;
622
+ s = (s + x) | 0;
623
+ }
624
+ if (pM !== 0) g = pM >> 31;
625
+ }
626
+ }
627
+ RA = a; RB = b; RK1 = k1; RK2 = k2;
628
+ return p;
629
+ }
630
+
631
+ // Mode 3 (ortho): dedicated sleek function for 5-point building rectangles (m === 4, closed === 1)
632
+ function bodyOrtho4(d, p, end, out, geo, a, b, f, prec, k1, k2) {
633
+ p |= 0; end |= 0; geo |= 0; a |= 0; b |= 0; f = +f; k1 |= 0; k2 |= 0;
634
+ if (p > end) throw err(T);
635
+ const tj = TJ, inv = INV, c2 = cos2Int(geo ? b : a, prec);
636
+ let w = d.getInt32(p >> 3) << (p & 7), x = Math.clz32(w), y = 0, n = 0, e = 0, z1 = 0, z2 = 0;
637
+ if (x < 8) { p += x + 1; k1 = (k1 + ((x >>> 1) ^ -(x & 1))) | 0; }
638
+ else { x = LK[(w << 8) >>> 27]; n = x - k1; if ((n < 0 ? -2 * n - 1 : 2 * n) < 8) throw err(NC, p + 13, end); k1 = x; p += 13; }
639
+ if (k1 >>> 0 > KMAX) throw err(R, p, end);
640
+ w = d.getInt32(p >> 3) << (p & 7); x = Math.clz32(w);
641
+ let bk2 = k2;
642
+ if (x < 8) { p += x + 1; bk2 = (k2 + ((x >>> 1) ^ -(x & 1))) | 0; }
643
+ else { x = LK[(w << 8) >>> 27]; n = x - k2; if ((n < 0 ? -2 * n - 1 : 2 * n) < 8) throw err(NC, p + 13, end); bk2 = x; p += 13; }
644
+ if (bk2 < -1 || bk2 > KMAX) throw err(R, p, end);
645
+ k2 = bk2 < 0 ? 0 : bk2;
646
+
647
+ // Step 1
648
+ w = d.getInt32(p >> 3) << (p & 7); e = tj[w >>> 21];
649
+ if (e !== 0) {
650
+ if (k1 < 8) { n = e >> 8; w = ((w << n) >>> 1) >>> (31 - (k1 << 1)); z1 = ((e & 15) << k1) | (w >>> k1); z2 = (((e >> 4) & 15) << k1) | (w & ((1 << k1) - 1)); p += n + (k1 << 1); }
651
+ else { p += e >> 8; z1 = ((e & 15) << k1) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - k1)); p += k1; z2 = (((e >> 4) & 15) << k1) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - k1)); p += k1; }
652
+ } else {
653
+ x = Math.clz32(w); if (x > 23) { p += 24; x = Math.clz32(d.getInt32(p >> 3) << (p & 7)); if (x > 11) throw err(R, p + 12 - k1, end); p += x + 1; x += 24; } else p += x + 1;
654
+ y = Math.clz32(d.getInt32(p >> 3) << (p & 7)); if (y > 23) { p += 24; y = Math.clz32(d.getInt32(p >> 3) << (p & 7)); if (y > 11) throw err(R, x + k1 > 35 ? p - x + 11 - k1 : p + 12 - k1, end); p += y + 1; y += 24; } else p += y + 1;
655
+ z1 = ((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - k1); p += k1;
656
+ z2 = ((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - k1); p += k1;
657
+ if (x >= 6) { n = x - 4; if (n + k1 > 31) throw err(R, p - x - y - k1 - 2 * k1 + 34, end); x = 1; if (n > 16) { x = (1 << (n - 16)) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (47 - n)); p += n - 16; n = 16; } x = (((x << n) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - n))) + 2) | 0; p += n; if ((x >>> 0) < 2 || (k1 && (x >>> (32 - k1)))) throw err(R, y + k1 > 35 ? p - y - k1 - 2 * k1 + 35 - (31 - Math.clz32(x - 2)) : p, end); }
658
+ if (y >= 6) { n = y - 4; if (n + k1 > 31) throw err(R, p - y - k1 - 2 * k1 + 35 - ((x >>> 0) >= 6 ? 31 - Math.clz32(x - 2) : 0), end); y = 1; if (n > 16) { y = (1 << (n - 16)) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (47 - n)); p += n - 16; n = 16; } y = (((y << n) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - n))) + 2) | 0; p += n; if ((y >>> 0) < 2 || (k1 && (y >>> (32 - k1)))) throw err(R, p, end); }
659
+ z1 |= x << k1; z2 |= y << k1;
660
+ }
661
+ const da1 = (z1 >>> 1) ^ -(z1 & 1), db1 = (z2 >>> 1) ^ -(z2 & 1);
662
+ let ua = da1, ub = db1;
663
+ while ((ua < 0 ? -ua : ua) >= 262144 || (ub < 0 ? -ub : ub) >= 262144) { ua >>= 1; ub >>= 1; }
664
+ const fva = -((Math.imul(c2, ub) + 2048) >> 12) | 0, fvb = ua;
665
+ const ava = fva < 0 ? -fva : fva, avb = fvb < 0 ? -fvb : fvb;
666
+ const swV = avb >= ava ? 1 : 0;
667
+ const sV = swV ? (avb === 0 ? 0 : avb < 512 ? ((x = Math.imul(fva, inv[avb]) >> 12), fvb < 0 ? -x | 0 : x) : ((fva << 12) / fvb) | 0)
668
+ : (ava < 512 ? ((x = Math.imul(fvb, inv[ava]) >> 12), fva < 0 ? -x | 0 : x) : ((fvb << 12) / fva) | 0);
669
+ if (geo) { a = (a + db1) | 0; b = (b + da1) | 0; } else { a = (a + da1) | 0; b = (b + db1) | 0; }
670
+ out[1] = [a / f, b / f];
671
+
672
+ // Steps 2 and 3
673
+ let ev = 0, ea = 0, eb = 0;
674
+ if (bk2 < 0) {
675
+ w = d.getInt32(p >> 3) << (p & 7); x = Math.clz32(w);
676
+ if (x > 23) { p += 24; x = Math.clz32(d.getInt32(p >> 3) << (p & 7)); if (x > 11) throw err(R, p + 12 - k1, end); p += x + 1; x += 24; } else p += x + 1;
677
+ z1 = ((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - k1); p += k1;
678
+ if (x >= 6) { n = x - 4; if (n + k1 > 31) throw err(R, p - x - 2 * k1 + 35, end); x = 1; if (n > 16) { x = (1 << (n - 16)) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (47 - n)); p += n - 16; n = 16; } x = (((x << n) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - n))) + 2) | 0; p += n; if ((x >>> 0) < 2 || (k1 && (x >>> (32 - k1)))) throw err(R, p, end); }
679
+ z1 |= x << k1;
680
+ } else {
681
+ const kk = k1 + bk2, m2 = (1 << bk2) - 1;
682
+ w = d.getInt32(p >> 3) << (p & 7); e = tj[w >>> 21];
683
+ if (e !== 0) {
684
+ if (kk < 15) { n = e >> 8; w = ((w << n) >>> 1) >>> (31 - kk); z1 = ((e & 15) << k1) | (w >>> bk2); z2 = (((e >> 4) & 15) << bk2) | (w & m2); p += n + kk; }
685
+ else { p += e >> 8; z1 = ((e & 15) << k1) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - k1)); p += k1; z2 = (((e >> 4) & 15) << bk2) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - bk2)); p += bk2; }
686
+ } else {
687
+ x = Math.clz32(w); if (x > 23) { p += 24; x = Math.clz32(d.getInt32(p >> 3) << (p & 7)); if (x > 11) throw err(R, p + 12 - k1, end); p += x + 1; x += 24; } else p += x + 1;
688
+ y = Math.clz32(d.getInt32(p >> 3) << (p & 7)); if (y > 23) { p += 24; y = Math.clz32(d.getInt32(p >> 3) << (p & 7)); if (y > 11) throw err(R, x + k1 > 35 ? p - x + 11 - k1 : p + 12 - bk2, end); p += y + 1; y += 24; } else p += y + 1;
689
+ z1 = ((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - k1); p += k1;
690
+ z2 = ((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - bk2); p += bk2;
691
+ if (x >= 6) { n = x - 4; if (n + k1 > 31) throw err(R, p - x - y - bk2 - 2 * k1 + 34, end); x = 1; if (n > 16) { x = (1 << (n - 16)) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (47 - n)); p += n - 16; n = 16; } x = (((x << n) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - n))) + 2) | 0; p += n; if ((x >>> 0) < 2 || (k1 && (x >>> (32 - k1)))) throw err(R, y + bk2 > 35 ? p - y - k1 - 2 * bk2 + 35 - (31 - Math.clz32(x - 2)) : p, end); }
692
+ if (y >= 6) { n = y - 4; if (n + bk2 > 31) throw err(R, p - y - k1 - 2 * bk2 + 35 - ((x >>> 0) >= 6 ? 31 - Math.clz32(x - 2) : 0), end); y = 1; if (n > 16) { y = (1 << (n - 16)) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (47 - n)); p += n - 16; n = 16; } y = (((y << n) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - n))) + 2) | 0; p += n; if ((y >>> 0) < 2 || (bk2 && (y >>> (32 - bk2)))) throw err(R, p, end); }
693
+ z1 |= x << k1; z2 |= y << bk2;
694
+ }
695
+ ev = (z2 >>> 1) ^ -(z2 & 1);
696
+ w = d.getInt32(p >> 3) << (p & 7); e = tj[w >>> 21];
697
+ if (e !== 0) {
698
+ if (bk2 < 8) { n = e >> 8; w = ((w << n) >>> 1) >>> (31 - (bk2 << 1)); z2 = ((e & 15) << bk2) | (w >>> bk2); x = (((e >> 4) & 15) << bk2) | (w & m2); p += n + (bk2 << 1); }
699
+ else { p += e >> 8; z2 = ((e & 15) << bk2) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - bk2)); p += bk2; x = (((e >> 4) & 15) << bk2) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - bk2)); p += bk2; }
700
+ } else {
701
+ x = Math.clz32(w); if (x > 23) { p += 24; x = Math.clz32(d.getInt32(p >> 3) << (p & 7)); if (x > 11) throw err(R, p + 12 - bk2, end); p += x + 1; x += 24; } else p += x + 1;
702
+ y = Math.clz32(d.getInt32(p >> 3) << (p & 7)); if (y > 23) { p += 24; y = Math.clz32(d.getInt32(p >> 3) << (p & 7)); if (y > 11) throw err(R, x + bk2 > 35 ? p - x + 11 - bk2 : p + 12 - bk2, end); p += y + 1; y += 24; } else p += y + 1;
703
+ z2 = ((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - bk2); p += bk2;
704
+ const zb = ((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - bk2); p += bk2;
705
+ if (x >= 6) { n = x - 4; if (n + bk2 > 31) throw err(R, p - x - y - bk2 - 2 * bk2 + 34, end); x = 1; if (n > 16) { x = (1 << (n - 16)) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (47 - n)); p += n - 16; n = 16; } x = (((x << n) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - n))) + 2) | 0; p += n; if ((x >>> 0) < 2 || (bk2 && (x >>> (32 - bk2)))) throw err(R, y + bk2 > 35 ? p - y - bk2 - 2 * bk2 + 35 - (31 - Math.clz32(x - 2)) : p, end); }
706
+ if (y >= 6) { n = y - 4; if (n + bk2 > 31) throw err(R, p - y - bk2 - 2 * bk2 + 35 - ((x >>> 0) >= 6 ? 31 - Math.clz32(x - 2) : 0), end); y = 1; if (n > 16) { y = (1 << (n - 16)) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (47 - n)); p += n - 16; n = 16; } y = (((y << n) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - n))) + 2) | 0; p += n; if ((y >>> 0) < 2 || (bk2 && (y >>> (32 - bk2)))) throw err(R, p, end); }
707
+ z2 |= x << bk2; x = zb | (y << bk2);
708
+ }
709
+ ea = (z2 >>> 1) ^ -(z2 & 1); eb = (x >>> 1) ^ -(x & 1);
710
+ }
711
+ const dM = (z1 >>> 1) ^ -(z1 & 1), dm = (((Math.imul(dM, sV) + 2048) >> 12) + ev) | 0;
712
+ const da = swV ? dm : dM, db = swV ? dM : dm;
713
+ if (geo) { a = (a + db) | 0; b = (b + da) | 0; out[2] = [a / f, b / f]; a = (a + eb - db1) | 0; b = (b + ea - da1) | 0; out[3] = [a / f, b / f]; }
714
+ else { a = (a + da) | 0; b = (b + db) | 0; out[2] = [a / f, b / f]; a = (a + ea - da1) | 0; b = (b + eb - db1) | 0; out[3] = [a / f, b / f]; }
715
+ RA = a; RB = b; RK1 = k1; RK2 = k2;
716
+ return p;
717
+ }
718
+
719
+ // Mode 3 (ortho): general m != 4 (or open) path, ZERO calls inside loop!
720
+ let OF_SWU = 0, OF_SU = 0, OF_SWV = 0, OF_SV = 0;
721
+ function setOrthoFrame(ua, ub, c2) {
722
+ ua |= 0; ub |= 0;
723
+ while ((ua < 0 ? -ua : ua) >= 262144 || (ub < 0 ? -ub : ub) >= 262144) { ua >>= 1; ub >>= 1; }
724
+ const va = -((Math.imul(c2, ub) + 2048) >> 12) | 0, vb = ua;
725
+ if ((ub < 0 ? -ub : ub) >= (ua < 0 ? -ua : ua)) { OF_SWU = 1; OF_SU = divSlope(ua, ub); } else { OF_SWU = 0; OF_SU = divSlope(ub, ua); }
726
+ if ((vb < 0 ? -vb : vb) >= (va < 0 ? -va : va)) { OF_SWV = 1; OF_SV = divSlope(va, vb); } else { OF_SWV = 0; OF_SV = divSlope(vb, va); }
727
+ }
728
+
729
+ function bodyOrtho(d, p, end, out, o, m, closed, geo, a, b, f, prec, k1, k2) {
730
+ p |= 0; end |= 0; o |= 0; m |= 0; closed |= 0; geo |= 0; a |= 0; b |= 0; f = +f; k1 |= 0; k2 |= 0;
731
+ const tj = TJ, inv = INV, lat0 = geo ? b : a, lng0 = geo ? a : b;
732
+ const c2 = cos2Int(lat0, prec), closingK2 = closed === 1 && m >= 4;
733
+ let swU = 0, sU = 0, swV = 0, sV = 0, maxL1 = 0, nextV = 1;
734
+ let w = 0, e = 0, x = 0, y = 0, n = 0, z1 = 0, z2 = 0, da = 0, db = 0;
735
+
736
+ while (o < m) {
737
+ if (p > end) throw err(T);
738
+ const stop = o + BLOCK < m ? o + BLOCK : m;
739
+ if (!(closingK2 && o === m - 1)) {
740
+ w = d.getInt32(p >> 3) << (p & 7); x = Math.clz32(w);
741
+ if (x < 8) { p += x + 1; k1 = (k1 + ((x >>> 1) ^ -(x & 1))) | 0; }
742
+ else { x = LK[(w << 8) >>> 27]; n = x - k1; if ((n < 0 ? -2 * n - 1 : 2 * n) < 8) throw err(NC, p + 13, end); k1 = x; p += 13; }
743
+ if (k1 >>> 0 > KMAX) throw err(R, p, end);
744
+ }
745
+ let bk2 = k2;
746
+ if (!(o === 1 && m === 2)) {
747
+ w = d.getInt32(p >> 3) << (p & 7); x = Math.clz32(w);
748
+ if (x < 8) { p += x + 1; bk2 = (k2 + ((x >>> 1) ^ -(x & 1))) | 0; }
749
+ else { x = LK[(w << 8) >>> 27]; n = x - k2; if ((n < 0 ? -2 * n - 1 : 2 * n) < 8) throw err(NC, p + 13, end); bk2 = x; p += 13; }
750
+ if (bk2 < -1 || bk2 > KMAX) throw err(R, p, end);
751
+ k2 = bk2 < 0 ? 0 : bk2;
752
+ }
753
+
754
+ if (o === 1) {
755
+ w = d.getInt32(p >> 3) << (p & 7); e = tj[w >>> 21];
756
+ if (e !== 0) {
757
+ p += e >> 8;
758
+ z1 = ((e & 15) << k1) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - k1)); p += k1;
759
+ z2 = (((e >> 4) & 15) << k1) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - k1)); p += k1;
760
+ } else {
761
+ x = Math.clz32(w); if (x > 23) { p += 24; x = Math.clz32(d.getInt32(p >> 3) << (p & 7)); if (x > 11) throw err(R, p + 12 - k1, end); p += x + 1; x += 24; } else p += x + 1;
762
+ y = Math.clz32(d.getInt32(p >> 3) << (p & 7)); if (y > 23) { p += 24; y = Math.clz32(d.getInt32(p >> 3) << (p & 7)); if (y > 11) throw err(R, x + k1 > 35 ? p - x + 11 - k1 : p + 12 - k1, end); p += y + 1; y += 24; } else p += y + 1;
763
+ z1 = ((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - k1); p += k1;
764
+ z2 = ((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - k1); p += k1;
765
+ if (x >= 6) { n = x - 4; if (n + k1 > 31) throw err(R, p - x - y - k1 - 2 * k1 + 34, end); x = 1; if (n > 16) { x = (1 << (n - 16)) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (47 - n)); p += n - 16; n = 16; } x = (((x << n) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - n))) + 2) | 0; p += n; if ((x >>> 0) < 2 || (k1 && (x >>> (32 - k1)))) throw err(R, y + k1 > 35 ? p - y - k1 - 2 * k1 + 35 - (31 - Math.clz32(x - 2)) : p, end); }
766
+ if (y >= 6) { n = y - 4; if (n + k1 > 31) throw err(R, p - y - k1 - 2 * k1 + 35 - ((x >>> 0) >= 6 ? 31 - Math.clz32(x - 2) : 0), end); y = 1; if (n > 16) { y = (1 << (n - 16)) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (47 - n)); p += n - 16; n = 16; } y = (((y << n) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - n))) + 2) | 0; p += n; if ((y >>> 0) < 2 || (k1 && (y >>> (32 - k1)))) throw err(R, p, end); }
767
+ z1 |= x << k1; z2 |= y << k1;
768
+ }
769
+ const da1 = (z1 >>> 1) ^ -(z1 & 1), db1 = (z2 >>> 1) ^ -(z2 & 1);
770
+ let ua = da1, ub = db1;
771
+ while ((ua < 0 ? -ua : ua) >= 262144 || (ub < 0 ? -ub : ub) >= 262144) { ua >>= 1; ub >>= 1; }
772
+ const fva = -((Math.imul(c2, ub) + 2048) >> 12) | 0, fvb = ua;
773
+ const aua = ua < 0 ? -ua : ua, aub = ub < 0 ? -ub : ub;
774
+ if (aub >= aua) { swU = 1; sU = aub === 0 ? 0 : aub < 512 ? ((x = Math.imul(ua, inv[aub]) >> 12), ub < 0 ? -x | 0 : x) : ((ua << 12) / ub) | 0; }
775
+ else { swU = 0; sU = aua < 512 ? ((x = Math.imul(ub, inv[aua]) >> 12), ua < 0 ? -x | 0 : x) : ((ub << 12) / ua) | 0; }
776
+ const ava = fva < 0 ? -fva : fva, avb = fvb < 0 ? -fvb : fvb;
777
+ if (avb >= ava) { swV = 1; sV = avb === 0 ? 0 : avb < 512 ? ((x = Math.imul(fva, inv[avb]) >> 12), fvb < 0 ? -x | 0 : x) : ((fva << 12) / fvb) | 0; }
778
+ else { swV = 0; sV = ava < 512 ? ((x = Math.imul(fvb, inv[ava]) >> 12), fva < 0 ? -x | 0 : x) : ((fvb << 12) / fva) | 0; }
779
+ if (geo) { a = (a + db1) | 0; b = (b + da1) | 0; } else { a = (a + da1) | 0; b = (b + db1) | 0; }
780
+ out[1] = [a / f, b / f]; o = 2;
781
+ maxL1 = (da1 < 0 ? -da1 : da1) + (db1 < 0 ? -db1 : db1); nextV = 1;
782
+ if (o >= stop) continue;
783
+ }
784
+
785
+ const endInt = closingK2 && stop === m ? m - 1 : stop;
786
+ if (bk2 < 0) {
787
+ for (; o < endInt; o++) {
788
+ w = d.getInt32(p >> 3) << (p & 7); x = Math.clz32(w);
789
+ if (x > 23) { p += 24; x = Math.clz32(d.getInt32(p >> 3) << (p & 7)); if (x > 11) throw err(R, p + 12 - k1, end); p += x + 1; x += 24; } else p += x + 1;
790
+ z1 = ((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - k1); p += k1;
791
+ if (x >= 6) { n = x - 4; if (n + k1 > 31) throw err(R, p - x - 2 * k1 + 35, end); x = 1; if (n > 16) { x = (1 << (n - 16)) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (47 - n)); p += n - 16; n = 16; } x = (((x << n) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - n))) + 2) | 0; p += n; if ((x >>> 0) < 2 || (k1 && (x >>> (32 - k1)))) throw err(R, p, end); }
792
+ z1 |= x << k1;
793
+ const sw = nextV ? swV : swU, cs = nextV ? sV : sU, dM = (z1 >>> 1) ^ -(z1 & 1), dm = (Math.imul(dM, cs) + 2048) >> 12;
794
+ da = sw ? dm : dM; db = sw ? dM : dm;
795
+ const wasU = nextV === 0; nextV ^= 1;
796
+ const ada = da < 0 ? -da : da, adb = db < 0 ? -db : db;
797
+ if (ada < 262144 && adb < 262144 && ada + adb > maxL1) {
798
+ maxL1 = ada + adb;
799
+ let ua = wasU ? da : db, ub = wasU ? db : -(((da << 12) / c2) | 0) | 0;
800
+ while ((ua < 0 ? -ua : ua) >= 262144 || (ub < 0 ? -ub : ub) >= 262144) { ua >>= 1; ub >>= 1; }
801
+ const fva = -((Math.imul(c2, ub) + 2048) >> 12) | 0, fvb = ua;
802
+ const aua = ua < 0 ? -ua : ua, aub = ub < 0 ? -ub : ub;
803
+ if (aub >= aua) { swU = 1; sU = aub === 0 ? 0 : aub < 512 ? ((x = Math.imul(ua, inv[aub]) >> 12), ub < 0 ? -x | 0 : x) : ((ua << 12) / ub) | 0; }
804
+ else { swU = 0; sU = aua < 512 ? ((x = Math.imul(ub, inv[aua]) >> 12), ua < 0 ? -x | 0 : x) : ((ub << 12) / ua) | 0; }
805
+ const ava = fva < 0 ? -fva : fva, avb = fvb < 0 ? -fvb : fvb;
806
+ if (avb >= ava) { swV = 1; sV = avb === 0 ? 0 : avb < 512 ? ((x = Math.imul(fva, inv[avb]) >> 12), fvb < 0 ? -x | 0 : x) : ((fva << 12) / fvb) | 0; }
807
+ else { swV = 0; sV = ava < 512 ? ((x = Math.imul(fvb, inv[ava]) >> 12), fva < 0 ? -x | 0 : x) : ((fvb << 12) / fva) | 0; }
808
+ }
809
+ if (geo) { a = (a + db) | 0; b = (b + da) | 0; } else { a = (a + da) | 0; b = (b + db) | 0; }
810
+ out[o] = [a / f, b / f];
811
+ }
812
+ } else {
813
+ const kk = k1 + bk2, m2 = (1 << bk2) - 1, one = kk < 15;
814
+ for (; o < endInt; o++) {
815
+ w = d.getInt32(p >> 3) << (p & 7); e = tj[w >>> 21];
816
+ if (e !== 0) {
817
+ if (one) { n = e >> 8; w = ((w << n) >>> 1) >>> (31 - kk); z1 = ((e & 15) << k1) | (w >>> bk2); z2 = (((e >> 4) & 15) << bk2) | (w & m2); p += n + kk; }
818
+ else { p += e >> 8; z1 = ((e & 15) << k1) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - k1)); p += k1; z2 = (((e >> 4) & 15) << bk2) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - bk2)); p += bk2; }
819
+ } else {
820
+ x = Math.clz32(w); if (x > 23) { p += 24; x = Math.clz32(d.getInt32(p >> 3) << (p & 7)); if (x > 11) throw err(R, p + 12 - k1, end); p += x + 1; x += 24; } else p += x + 1;
821
+ y = Math.clz32(d.getInt32(p >> 3) << (p & 7)); if (y > 23) { p += 24; y = Math.clz32(d.getInt32(p >> 3) << (p & 7)); if (y > 11) throw err(R, x + k1 > 35 ? p - x + 11 - k1 : p + 12 - bk2, end); p += y + 1; y += 24; } else p += y + 1;
822
+ z1 = ((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - k1); p += k1;
823
+ z2 = ((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - bk2); p += bk2;
824
+ if (x >= 6) { n = x - 4; if (n + k1 > 31) throw err(R, p - x - y - bk2 - 2 * k1 + 34, end); x = 1; if (n > 16) { x = (1 << (n - 16)) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (47 - n)); p += n - 16; n = 16; } x = (((x << n) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - n))) + 2) | 0; p += n; if ((x >>> 0) < 2 || (k1 && (x >>> (32 - k1)))) throw err(R, y + bk2 > 35 ? p - y - k1 - 2 * bk2 + 35 - (31 - Math.clz32(x - 2)) : p, end); }
825
+ if (y >= 6) { n = y - 4; if (n + bk2 > 31) throw err(R, p - y - k1 - 2 * bk2 + 35 - ((x >>> 0) >= 6 ? 31 - Math.clz32(x - 2) : 0), end); y = 1; if (n > 16) { y = (1 << (n - 16)) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (47 - n)); p += n - 16; n = 16; } y = (((y << n) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - n))) + 2) | 0; p += n; if ((y >>> 0) < 2 || (bk2 && (y >>> (32 - bk2)))) throw err(R, p, end); }
826
+ z1 |= x << k1; z2 |= y << bk2;
827
+ }
828
+ const sw = nextV ? swV : swU, cs = nextV ? sV : sU, dM = (z1 >>> 1) ^ -(z1 & 1), ev = (z2 >>> 1) ^ -(z2 & 1);
829
+ const dm = (((Math.imul(dM, cs) + 2048) >> 12) + ev) | 0;
830
+ da = sw ? dm : dM; db = sw ? dM : dm;
831
+ let wasU = nextV === 0;
832
+ if (ev !== 0) {
833
+ const osw = nextV ? swU : swV, os = nextV ? sU : sV, oerr = ((osw ? da : db) - ((Math.imul(osw ? db : da, os) + 2048) >> 12)) | 0;
834
+ const st = (ev < 0 ? -ev : ev) <= (oerr < 0 ? -oerr : oerr); wasU = nextV ? !st : st;
835
+ }
836
+ nextV = wasU ? 1 : 0;
837
+ const ada = da < 0 ? -da : da, adb = db < 0 ? -db : db;
838
+ if (ada < 262144 && adb < 262144 && ada + adb > maxL1) {
839
+ maxL1 = ada + adb;
840
+ let ua = wasU ? da : db, ub = wasU ? db : -(((da << 12) / c2) | 0) | 0;
841
+ while ((ua < 0 ? -ua : ua) >= 262144 || (ub < 0 ? -ub : ub) >= 262144) { ua >>= 1; ub >>= 1; }
842
+ const fva = -((Math.imul(c2, ub) + 2048) >> 12) | 0, fvb = ua;
843
+ const aua = ua < 0 ? -ua : ua, aub = ub < 0 ? -ub : ub;
844
+ if (aub >= aua) { swU = 1; sU = aub === 0 ? 0 : aub < 512 ? ((x = Math.imul(ua, inv[aub]) >> 12), ub < 0 ? -x | 0 : x) : ((ua << 12) / ub) | 0; }
845
+ else { swU = 0; sU = aua < 512 ? ((x = Math.imul(ub, inv[aua]) >> 12), ua < 0 ? -x | 0 : x) : ((ub << 12) / ua) | 0; }
846
+ const ava = fva < 0 ? -fva : fva, avb = fvb < 0 ? -fvb : fvb;
847
+ if (avb >= ava) { swV = 1; sV = avb === 0 ? 0 : avb < 512 ? ((x = Math.imul(fva, inv[avb]) >> 12), fvb < 0 ? -x | 0 : x) : ((fva << 12) / fvb) | 0; }
848
+ else { swV = 0; sV = ava < 512 ? ((x = Math.imul(fvb, inv[ava]) >> 12), fva < 0 ? -x | 0 : x) : ((fvb << 12) / fva) | 0; }
849
+ }
850
+ if (geo) { a = (a + db) | 0; b = (b + da) | 0; } else { a = (a + da) | 0; b = (b + db) | 0; }
851
+ out[o] = [a / f, b / f];
852
+ }
853
+ }
854
+
855
+ if (o < stop) {
856
+ let ea = 0, eb = 0;
857
+ if (bk2 >= 0) {
858
+ w = d.getInt32(p >> 3) << (p & 7); e = tj[w >>> 21];
859
+ if (e !== 0) {
860
+ p += e >> 8;
861
+ z1 = ((e & 15) << bk2) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - bk2)); p += bk2;
862
+ z2 = (((e >> 4) & 15) << bk2) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - bk2)); p += bk2;
863
+ } else {
864
+ x = Math.clz32(w); if (x > 23) { p += 24; x = Math.clz32(d.getInt32(p >> 3) << (p & 7)); if (x > 11) throw err(R, p + 12 - bk2, end); p += x + 1; x += 24; } else p += x + 1;
865
+ y = Math.clz32(d.getInt32(p >> 3) << (p & 7)); if (y > 23) { p += 24; y = Math.clz32(d.getInt32(p >> 3) << (p & 7)); if (y > 11) throw err(R, x + bk2 > 35 ? p - x + 11 - bk2 : p + 12 - bk2, end); p += y + 1; y += 24; } else p += y + 1;
866
+ z1 = ((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - bk2); p += bk2;
867
+ z2 = ((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - bk2); p += bk2;
868
+ if (x >= 6) { n = x - 4; if (n + bk2 > 31) throw err(R, p - x - y - bk2 - 2 * bk2 + 34, end); x = 1; if (n > 16) { x = (1 << (n - 16)) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (47 - n)); p += n - 16; n = 16; } x = (((x << n) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - n))) + 2) | 0; p += n; if ((x >>> 0) < 2 || (bk2 && (x >>> (32 - bk2)))) throw err(R, y + bk2 > 35 ? p - y - bk2 - 2 * bk2 + 35 - (31 - Math.clz32(x - 2)) : p, end); }
869
+ if (y >= 6) { n = y - 4; if (n + bk2 > 31) throw err(R, p - y - bk2 - 2 * bk2 + 35 - ((x >>> 0) >= 6 ? 31 - Math.clz32(x - 2) : 0), end); y = 1; if (n > 16) { y = (1 << (n - 16)) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (47 - n)); p += n - 16; n = 16; } y = (((y << n) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - n))) + 2) | 0; p += n; if ((y >>> 0) < 2 || (bk2 && (y >>> (32 - bk2)))) throw err(R, p, end); }
870
+ z1 |= x << bk2; z2 |= y << bk2;
871
+ }
872
+ ea = (z1 >>> 1) ^ -(z1 & 1); eb = (z2 >>> 1) ^ -(z2 & 1);
873
+ }
874
+ const sw = nextV ? swV : swU, cs = nextV ? sV : sU, pA = sw ? cs >> 4 : 256, pB = sw ? 256 : cs >> 4;
875
+ const wB = ((c2 >> 4) * pB + 128) >> 8, denom = pA * pA + wB * pB, num = ((lat0 - (geo ? b : a)) | 0) * pA + ((lng0 - (geo ? a : b)) | 0) * wB;
876
+ da = ((Math.trunc((num * pA) / denom) | 0) + ea) | 0; db = ((Math.trunc((num * pB) / denom) | 0) + eb) | 0;
877
+ if (geo) { a = (a + db) | 0; b = (b + da) | 0; } else { a = (a + da) | 0; b = (b + db) | 0; }
878
+ out[o++] = [a / f, b / f];
879
+ }
880
+ }
881
+ RA = a; RB = b; RK1 = k1; RK2 = k2;
882
+ return p;
883
+ }
884
+
885
+ // ---------------------------------------------------------------- slow decode (wide + Z/M, compact)
886
+
887
+ const take = (d, n) => { const v = bitsBig(d, SP, n); SP += n; return v; };
888
+ const unzigBig = (z) => (z % 2 ? -(z + 1) / 2 : z / 2);
889
+ function readSized(d) { const n = take(d, 6); if (n > 53) throw err(R, SP); const z = take(d, n); if (n && z < P2[n - 1]) throw err(NC, SP); return z; }
890
+ // The class is too large at its (max + 1)th zero, so that is where the
891
+ // refusal is found: checking for the end of the string first, a chunk at a
892
+ // time, made a run that reached it inside the string TRUNCATED. Only the
893
+ // first 25 bits of the shifted window are the string's, so z counts to 24.
894
+ function readUnaryBig(d, max) {
895
+ const p = SP;
896
+ for (let c = 0; ; c += 24, SP += 24) {
897
+ if (SP > SEND) throw err(T);
898
+ const z = Math.min(24, Math.clz32(d.getInt32(SP >> 3) << (SP & 7)));
899
+ if (c + z > max) throw err(R, p + max + 1);
900
+ if (z < 24) { SP += z + 1; return c + z; }
901
+ }
902
+ }
903
+ function readQuotBig(d, c, k, maxBits) {
904
+ let q = c;
905
+ if (c >= 6) { const n = c - 4; if (n + k >= maxBits) throw err(R, SP); q = P2[n] + take(d, n) + 2; }
906
+ if (q >= P2[maxBits - k]) throw err(R, SP);
907
+ return q;
908
+ }
909
+ function readPairBig(d, k1, k2, maxBits) {
910
+ const c1 = readUnaryBig(d, maxBits + 3 - k1), c2 = readUnaryBig(d, maxBits + 3 - k2), f1 = take(d, k1), f2 = take(d, k2);
911
+ return [readQuotBig(d, c1, k1, maxBits) * P2[k1] + f1, readQuotBig(d, c2, k2, maxBits) * P2[k2] + f2];
912
+ }
913
+ function readSingleBig(d, k, maxBits) { const c = readUnaryBig(d, maxBits + 3 - k), f = take(d, k); return readQuotBig(d, c, k, maxBits) * P2[k] + f; }
914
+ function extraStep(x, v) { const nv = (XV[x] += v); if (!(nv >= -LIMIT && nv <= LIMIT)) throw err(R, SP); }
915
+ function slowPosition(d, prec) {
916
+ if (!WIDE) readPositionFast(d, prec);
917
+ else if (STARTED) {
918
+ if (take(d, 1)) {
919
+ const L = take(d, 6) + 1; if (L > 53) throw err(R, SP);
920
+ const a = take(d, L), b = take(d, L); if (a < P2[L - 1] && b < P2[L - 1]) throw err(NC, SP);
921
+ SLAT += unzigBig(a); SLNG += unzigBig(b);
922
+ }
923
+ } else {
924
+ STARTED = 1; SLAT = unzigBig(readSized(d)); SLNG = unzigBig(readSized(d));
925
+ if (Math.abs(SLAT) > 90 * POW[prec] || Math.abs(SLNG) > 180 * POW[prec]) BIG = 1; // no anchor code: wide was needed
926
+ }
927
+ if (WIDE && BIG === 0 && (SLAT < -2147483648 || SLAT > 2147483647 || SLNG < -2147483648 || SLNG > 2147483647)) BIG = 1;
928
+ for (let x = 0; x < NX; x++) extraStep(x, unzigBig(readSized(d)));
929
+ }
930
+ // Keep this function small. The test for "wide was needed" was in here, and
931
+ // at 291 bytes of bytecode against 222 V8 stopped inlining it into slowPath:
932
+ // strings with Z decoded 2 to 3% slower on Node and Deno. The test is now
933
+ // where the position changes, in slowPosition and slowPath, and only made
934
+ // until it's known, which at precision 8 or more is from the header on.
935
+ //
936
+ // A position with Z or M is written at its final length, in a function of
937
+ // its own to keep this one small. It used to be [a, b] with Z and M pushed
938
+ // on, and an array that grows by push keeps room for 17 more numbers: 216
939
+ // bytes a position on V8, where 3 values written at once take 80 and 4
940
+ // take 88.
941
+ function slowPoint(geo, f) {
942
+ if (WIDE && !(SLAT >= -LIMIT && SLAT <= LIMIT && SLNG >= -LIMIT && SLNG <= LIMIT)) throw err(R, SP);
943
+ if (NX) return geo ? pointX(SLNG / f, SLAT / f) : pointX(SLAT / f, SLNG / f);
944
+ return geo ? [SLNG / f, SLAT / f] : [SLAT / f, SLNG / f];
945
+ }
946
+ function pointX(a, b) { return NX === 1 ? [a, b, XV[0] / XF[0]] : [a, b, XV[0] / XF[0], XV[1] / XF[1]]; }
947
+ function slowPath(d, mode, m, closed, open, geo, f, prec) {
948
+ const out = Array(m + closed <= RPT || (2 + NX) * m <= SEND - SP ? m + closed : RPT), wide = WIDE, maxBits = wide ? 53 : 32, maxVal = P2[maxBits] - 1;
949
+ const isSplit = mode === 2 || mode === 3 || mode === 5, n = m - 1, ve = [0, 0];
950
+ slowPosition(d, prec); out[0] = slowPoint(geo, f);
951
+ const unsure = wide && BIG === 0; // wide, and nothing so far needed it
952
+ let va = 0, vb = 0, sw = -1, s = 0, g = 0, pM = 0, maxL1 = 0, nextV = 1, da1 = 0, db1 = 0, swU = 0, sU = 0, swV = 0, sV = 0;
953
+ const lat0 = SLAT, lng0 = SLNG, x0 = XV[0], x1 = XV[1], c2 = mode === 3 ? cos2Int(lat0, prec) : 0, c2s = c2 >> 4, closingK2 = mode === 3 && closed === 1 && m >= 4;
954
+
955
+ for (let i = 0; i < n; i += BLOCK) {
956
+ if (SP > SEND) throw err(T);
957
+ const cnt = Math.min(BLOCK, n - i);
958
+ let k1 = SK1, bk2 = SK2, mask = 0;
959
+ if (!isSplit) {
960
+ k1 = readParam(d, SP, SK1, wide ? 6 : 5, wide ? KWIDE : KMAX, -2); SK1 = k1 < 0 ? 0 : k1;
961
+ if (k1 === -1) { mask = take(d, cnt); if (mask === 0) throw err(NC, SP); }
962
+ } else {
963
+ if (!(closingK2 && i === n - 1)) { k1 = readParam(d, SP, SK1, 5, KMAX, 0); SK1 = k1; }
964
+ if (!(i === 0 && n === 1)) { bk2 = readParam(d, SP, SK2, 5, KMAX, -1); SK2 = bk2 < 0 ? 0 : bk2; }
965
+ }
966
+ for (let j = 0; j < cnt; j++) {
967
+ const idx = i + j; let z1 = 0, z2 = 0, da = 0, db = 0;
968
+ if (!isSplit) {
969
+ if (k1 === -1) { if ((mask >>> (cnt - 1 - j)) & 1) { [z1, z2] = readPairBig(d, 0, 0, maxBits); if (z1 === 0) { if (z2 >= maxVal) throw err(R, SP); z2 += 1; } } }
970
+ else if (k1 >= 0) [z1, z2] = readPairBig(d, k1, k1, maxBits);
971
+ const ra = wide ? unzigBig(z1) : (z1 >>> 1) ^ -(z1 & 1), rb = wide ? unzigBig(z2) : (z2 >>> 1) ^ -(z2 & 1);
972
+ da = mode ? (wide ? va + ra : (va + ra) | 0) : ra; db = mode ? (wide ? vb + rb : (vb + rb) | 0) : rb;
973
+ if (mode === 1) { va = da; vb = db; }
974
+ else if (mode === 4) { va = idx ? (wide ? Math.floor((va + da) / 2) : (va + da) >> 1) : da; vb = idx ? (wide ? Math.floor((vb + db) / 2) : (vb + db) >> 1) : db; }
975
+ SLAT = wide ? SLAT + da : (SLAT + da) | 0; SLNG = wide ? SLNG + db : (SLNG + db) | 0;
976
+ } else {
977
+ const isCl = closingK2 && idx === n - 1;
978
+ if (idx === 0) [z1, z2] = readPairBig(d, k1, k1, 32);
979
+ else if (isCl) { if (bk2 >= 0) [z1, z2] = readPairBig(d, bk2, bk2, 32); }
980
+ else if (bk2 < 0) z1 = readSingleBig(d, k1, 32);
981
+ else [z1, z2] = readPairBig(d, k1, bk2, 32);
982
+ if (idx === 0) {
983
+ da = (z1 >>> 1) ^ -(z1 & 1); db = (z2 >>> 1) ^ -(z2 & 1);
984
+ if (mode === 3) { da1 = da; db1 = db; setOrthoFrame(da, db, c2); swU = OF_SWU; sU = OF_SU; swV = OF_SWV; sV = OF_SV; maxL1 = Math.abs(da) + Math.abs(db); nextV = 1; }
985
+ else { sw = Math.abs(db) >= Math.abs(da) ? -1 : 0; const dM = sw ? db : da, dm = sw ? da : db; s = dM ? divSlope(dm, dM) : 0; pM = dM; g = dM ? dM >> 31 : 0; }
986
+ } else if (isCl) {
987
+ const ea = (z1 >>> 1) ^ -(z1 & 1), eb = (z2 >>> 1) ^ -(z2 & 1);
988
+ if (m === 4) { da = (ea - da1) | 0; db = (eb - db1) | 0; }
989
+ else {
990
+ const cw = nextV ? swV : swU, cs = nextV ? sV : sU, pA = cw ? cs >> 4 : 256, pB = cw ? 256 : cs >> 4;
991
+ const wB = (c2s * pB + 128) >> 8, denom = pA * pA + wB * pB, num = ((lat0 - SLAT) | 0) * pA + ((lng0 - SLNG) | 0) * wB;
992
+ da = ((Math.trunc((num * pA) / denom) | 0) + ea) | 0; db = ((Math.trunc((num * pB) / denom) | 0) + eb) | 0;
993
+ }
994
+ } else if (mode === 3) {
995
+ const cw = nextV ? swV : swU, cs = nextV ? sV : sU, dM = (z1 >>> 1) ^ -(z1 & 1), ev = (z2 >>> 1) ^ -(z2 & 1);
996
+ const dm = (((Math.imul(dM, cs) + 2048) >> 12) + ev) | 0; da = cw ? dm : dM; db = cw ? dM : dm;
997
+ const ow = nextV ? swU : swV, os = nextV ? sU : sV, oerr = ((ow ? da : db) - ((Math.imul(ow ? db : da, os) + 2048) >> 12)) | 0;
998
+ const wasU = nextV ? !(Math.abs(ev) <= Math.abs(oerr)) : (Math.abs(ev) <= Math.abs(oerr)); nextV = wasU ? 1 : 0;
999
+ if (Math.abs(da) < 262144 && Math.abs(db) < 262144 && Math.abs(da) + Math.abs(db) > maxL1) {
1000
+ maxL1 = Math.abs(da) + Math.abs(db);
1001
+ if (wasU) setOrthoFrame(da, db, c2); else setOrthoFrame(db, -(((da << 12) / c2) | 0) | 0, c2);
1002
+ swU = OF_SWU; sU = OF_SU; swV = OF_SWV; sV = OF_SV;
1003
+ }
1004
+ } else {
1005
+ const dM = mode === 5 ? (pM + ((z1 >>> 1) ^ -(z1 & 1))) | 0 : ((((z1 & 7) === 7 ? ~(z1 >>> 3) : (z1 - (z1 >>> 3)) | 0) ^ g) - g) | 0;
1006
+ const ev = (z2 >>> 1) ^ -(z2 & 1), dm = (((Math.imul(dM, s) + 2048) >> 12) + ev) | 0;
1007
+ da = sw ? dm : dM; db = sw ? dM : dm; pM = dM;
1008
+ if (Math.abs(dm) > Math.abs(dM)) { sw = ~sw; s = divSlope(dM, dm); pM = dm; }
1009
+ else if (ev && dM) { let ds = divSlope(ev, dM); if (Math.abs(dM) < 4) ds >>= 1; s = (s + ds) | 0; }
1010
+ if (pM) g = pM >> 31;
1011
+ }
1012
+ // The steps of a split mode are 32-bit in a wide string too; only the sum is exact.
1013
+ if (wide) { SLAT += da; SLNG += db; } else { SLAT = (SLAT + da) | 0; SLNG = (SLNG + db) | 0; }
1014
+ }
1015
+ if (unsure && (SLAT < -2147483648 || SLAT > 2147483647 || SLNG < -2147483648 || SLNG > 2147483647)) BIG = 1;
1016
+ out[1 + idx] = slowPoint(geo, f);
1017
+ }
1018
+ for (let x = 0; x < NX; x++) {
1019
+ const ke = readParam(d, SP, XK[x], 6, KWIDE, -1); XK[x] = ke < 0 ? 0 : ke;
1020
+ for (let j = 0; j < cnt; j++) {
1021
+ const rv = unzigBig(ke < 0 ? 0 : readSingleBig(d, ke, 53)), dv = XP[x] ? ve[x] + rv : rv;
1022
+ ve[x] = dv; extraStep(x, dv); out[1 + i + j][2 + x] = XV[x] / XF[x];
1023
+ }
1024
+ }
1025
+ }
1026
+ if (open && m > 1 && SLAT === lat0 && SLNG === lng0 && (NX < 1 || XV[0] === x0) && (NX < 2 || XV[1] === x1)) throw err(NC, SP);
1027
+ if (closed) out[m] = out[0].slice();
1028
+ return out;
1029
+ }
1030
+
1031
+ // ---------------------------------------------------------------- structure decode
1032
+
1033
+ // open: the path has a closed bit and it's 0. Such a path can't end on its
1034
+ // first point: closed is how that's written (SPEC.md section 6).
1035
+ function decodePath(d, mode, kind, geo, f, prec) {
1036
+ let p = SP, closed = kind === 2 ? 0 : 1, open = 0, lat0 = 0, lng0 = 0;
1037
+ if (p > SEND) throw err(T);
1038
+ if (kind === 0 || (kind === 1 && OPEN)) { closed = (d.getInt32(p >> 3) << (p & 7)) >>> 31; SP = p + 1; open = closed ^ 1; if (kind === 1 && open) OPENED = 1; }
1039
+ const m = readCount(d);
1040
+ if (m === 0) { if (SP > SEND) throw err(T); if (closed) throw err('EMPTY_CLOSED_PATH', SP); LOW++; return []; }
1041
+ const isSplit = mode === 2 || mode === 3 || mode === 5;
1042
+ if (m - 1 > (isSplit ? SEND - SP : 4 * (SEND - SP))) throw err(T);
1043
+ if (WIDE | NX) { STEPS += m - 1; if (m === 1) LOW++; return slowPath(d, mode, m, closed, open, geo, f, prec); }
1044
+ readPositionFast(d, prec);
1045
+ if (open) { lat0 = SLAT; lng0 = SLNG; }
1046
+ const out = Array(m + closed <= RPT || 2 * m <= SEND - SP ? m + closed : RPT), first = (out[0] = geo ? [SLNG / f, SLAT / f] : [SLAT / f, SLNG / f]);
1047
+ if (m > 1) {
1048
+ const a = geo ? SLNG : SLAT, b = geo ? SLAT : SLNG;
1049
+ if (mode < 2) { SP = bodyCoord(d, SP, SEND, out, 1, m, -mode, -geo, a, b, f, SK1); SK1 = RK1; }
1050
+ else if (mode === 4) { SP = bodyEma(d, SP, SEND, out, 1, m, -geo, a, b, f, SK1); SK1 = RK1; }
1051
+ else if (mode === 3) { SP = m === 4 && closed === 1 ? bodyOrtho4(d, SP, SEND, out, -geo, a, b, f, prec, SK1, SK2) : bodyOrtho(d, SP, SEND, out, 1, m, closed, -geo, a, b, f, prec, SK1, SK2); SK1 = RK1; SK2 = RK2; }
1052
+ else { SP = bodySlope(d, SP, SEND, out, 1, m, mode === 5 ? -1 : 0, -geo, a, b, f, SK1, SK2); SK1 = RK1; SK2 = RK2; }
1053
+ if (geo) { SLNG = RA; SLAT = RB; } else { SLAT = RA; SLNG = RB; }
1054
+ if (open && SLAT === lat0 && SLNG === lng0) throw err(NC, SP);
1055
+ } else LOW++;
1056
+ if (closed) out[m] = [first[0], first[1]];
1057
+ return out;
1058
+ }
1059
+ function decodePolygon(d, mode, f, prec) {
1060
+ const p = SP; let n = 1;
1061
+ if (p > SEND) throw err(T);
1062
+ SP = p + 1;
1063
+ if (d.getInt32(p >> 3) << (p & 7) >= 0) { n = readCount(d); if (n * 4 > SEND - SP) throw err(T); if (n === 1) throw err(NC, SP); if (n === 0) LOW++; }
1064
+ const rings = Array(n < RP ? n : RP);
1065
+ for (let i = 0; i < n; i++) rings[i] = decodePath(d, mode, 1, 1, f, prec);
1066
+ return rings;
1067
+ }
1068
+ function decodeBody(d, type, depth, f, prec) {
1069
+ let p = SP;
1070
+ if (p > SEND) throw err(T);
1071
+ if (type === 0) {
1072
+ SP = p + 1;
1073
+ if (d.getInt32(p >> 3) << (p & 7) >= 0) return { type: 'Point', coordinates: [] };
1074
+ if (WIDE | NX) { slowPosition(d, prec); return { type: 'Point', coordinates: slowPoint(1, f) }; }
1075
+ readPositionFast(d, prec);
1076
+ return { type: 'Point', coordinates: [SLNG / f, SLAT / f] };
1077
+ }
1078
+ if (type === 6) {
1079
+ if (depth >= MAX_DEPTH) throw err('TOO_DEEP');
1080
+ const n = readCount(d); if (n * 4 > SEND - SP) throw err(T);
1081
+ const geometries = Array(n < RP ? n : RP);
1082
+ for (let i = 0; i < n; i++) {
1083
+ p = SP; if (p > SEND) throw err(T);
1084
+ const t = (d.getInt32(p >> 3) << (p & 7)) >>> 29; if (t === 7) throw verErr();
1085
+ SP = p + 3; geometries[i] = decodeBody(d, t, depth + 1, f, prec);
1086
+ }
1087
+ return { type: 'GeometryCollection', geometries };
1088
+ }
1089
+ const mode = readMode(d), before = LOW;
1090
+ let coordinates, n = 1;
1091
+ if (type === 1) coordinates = decodePath(d, mode, 0, 1, f, prec);
1092
+ else if (type === 2) coordinates = decodePolygon(d, mode, f, prec);
1093
+ else if (type === 3) coordinates = decodePath(d, mode, 2, 1, f, prec);
1094
+ else {
1095
+ n = readCount(d); if (n * 4 > SEND - SP) throw err(T);
1096
+ coordinates = Array(n <= RP || 32 * n <= SEND - SP ? n : RP);
1097
+ if (type === 4) for (let i = 0; i < n; i++) coordinates[i] = decodePath(d, mode, 0, 1, f, prec);
1098
+ else for (let i = 0; i < n; i++) coordinates[i] = decodePolygon(d, mode, f, prec);
1099
+ }
1100
+ // A mode that predicted nothing must be 0 (SPEC.md section 4): the
1101
+ // geometry holds nothing, or everything it holds is without a step.
1102
+ if (mode !== 0 && (LOW !== before || n === 0) && LOW - before === held(type, coordinates, n)) throw err(NC, SP);
1103
+ return { type: TYPES[type], coordinates };
1104
+ }
1105
+ // How many paths a geometry holds, a polygon without a ring counting as one.
1106
+ function held(type, c, n) {
1107
+ if (type === 2) return c.length || 1;
1108
+ if (type !== 5) return type === 4 ? n : 1;
1109
+ let u = 0;
1110
+ for (let i = 0; i < c.length; i++) u += c[i].length || 1;
1111
+ return u;
1112
+ }
1113
+
1114
+ // The same three for a string that's counted against maxPositions (SPEC.md
1115
+ // section 14). Every geometry and every polygon counts one where it begins,
1116
+ // a Point's position one more, and a path counts when its count has been
1117
+ // read and judged: one for itself and one for each of its positions, the
1118
+ // closing one included. So a string that holds too much is refused at the
1119
+ // count that says so, before anything is built for it.
1120
+ //
1121
+ // Each is its original with those counts and nothing else, line for line. A
1122
+ // change to one belongs in the other: test/limits.test.js compares their
1123
+ // text, and the fuzz test decodes every string both ways. pathCounted was a
1124
+ // wrapper once, which read a path's count ahead and let decodePath read it
1125
+ // again. A MultiPolygon of many small rings then decoded 2.5 to 4% slower on
1126
+ // Node, so it's a copy too.
1127
+ function pathCounted(d, mode, kind, geo, f, prec) {
1128
+ let p = SP, closed = kind === 2 ? 0 : 1, open = 0, lat0 = 0, lng0 = 0;
1129
+ if (p > SEND) throw err(T);
1130
+ if (kind === 0 || (kind === 1 && OPEN)) { closed = (d.getInt32(p >> 3) << (p & 7)) >>> 31; SP = p + 1; open = closed ^ 1; if (kind === 1 && open) OPENED = 1; }
1131
+ const m = readCount(d);
1132
+ if (m === 0) { if (SP > SEND) throw err(T); if (closed) throw err('EMPTY_CLOSED_PATH', SP); if (--LEFT < 0) throw over(1); LOW++; return []; }
1133
+ const isSplit = mode === 2 || mode === 3 || mode === 5;
1134
+ if (m - 1 > (isSplit ? SEND - SP : 4 * (SEND - SP))) throw err(T);
1135
+ if ((LEFT -= m + closed + 1) < 0) throw over(1);
1136
+ if (WIDE | NX) { STEPS += m - 1; if (m === 1) LOW++; return slowPath(d, mode, m, closed, open, geo, f, prec); }
1137
+ readPositionFast(d, prec);
1138
+ if (open) { lat0 = SLAT; lng0 = SLNG; }
1139
+ const out = Array(m + closed <= RPT || 2 * m <= SEND - SP ? m + closed : RPT), first = (out[0] = geo ? [SLNG / f, SLAT / f] : [SLAT / f, SLNG / f]);
1140
+ if (m > 1) {
1141
+ const a = geo ? SLNG : SLAT, b = geo ? SLAT : SLNG;
1142
+ if (mode < 2) { SP = bodyCoord(d, SP, SEND, out, 1, m, -mode, -geo, a, b, f, SK1); SK1 = RK1; }
1143
+ else if (mode === 4) { SP = bodyEma(d, SP, SEND, out, 1, m, -geo, a, b, f, SK1); SK1 = RK1; }
1144
+ else if (mode === 3) { SP = m === 4 && closed === 1 ? bodyOrtho4(d, SP, SEND, out, -geo, a, b, f, prec, SK1, SK2) : bodyOrtho(d, SP, SEND, out, 1, m, closed, -geo, a, b, f, prec, SK1, SK2); SK1 = RK1; SK2 = RK2; }
1145
+ else { SP = bodySlope(d, SP, SEND, out, 1, m, mode === 5 ? -1 : 0, -geo, a, b, f, SK1, SK2); SK1 = RK1; SK2 = RK2; }
1146
+ if (geo) { SLNG = RA; SLAT = RB; } else { SLAT = RA; SLNG = RB; }
1147
+ if (open && SLAT === lat0 && SLNG === lng0) throw err(NC, SP);
1148
+ } else LOW++;
1149
+ if (closed) out[m] = [first[0], first[1]];
1150
+ return out;
1151
+ }
1152
+ function polygonCounted(d, mode, f, prec) {
1153
+ const p = SP; let n = 1;
1154
+ if (p > SEND) throw err(T);
1155
+ if (--LEFT < 0) throw over(1);
1156
+ SP = p + 1;
1157
+ if (d.getInt32(p >> 3) << (p & 7) >= 0) { n = readCount(d); if (n * 4 > SEND - SP) throw err(T); if (n === 1) throw err(NC, SP); if (n === 0) LOW++; }
1158
+ const rings = Array(n < RP ? n : RP);
1159
+ for (let i = 0; i < n; i++) rings[i] = pathCounted(d, mode, 1, 1, f, prec);
1160
+ return rings;
1161
+ }
1162
+ function bodyCounted(d, type, depth, f, prec) {
1163
+ let p = SP;
1164
+ if (p > SEND) throw err(T);
1165
+ if (--LEFT < 0) throw over(1);
1166
+ if (type === 0) {
1167
+ SP = p + 1;
1168
+ if (d.getInt32(p >> 3) << (p & 7) >= 0) return { type: 'Point', coordinates: [] };
1169
+ if (--LEFT < 0) throw over(1); // its position
1170
+ if (WIDE | NX) { slowPosition(d, prec); return { type: 'Point', coordinates: slowPoint(1, f) }; }
1171
+ readPositionFast(d, prec);
1172
+ return { type: 'Point', coordinates: [SLNG / f, SLAT / f] };
1173
+ }
1174
+ if (type === 6) {
1175
+ if (depth >= MAX_DEPTH) throw err('TOO_DEEP');
1176
+ const n = readCount(d); if (n * 4 > SEND - SP) throw err(T);
1177
+ const geometries = Array(n < RP ? n : RP);
1178
+ for (let i = 0; i < n; i++) {
1179
+ p = SP; if (p > SEND) throw err(T);
1180
+ const t = (d.getInt32(p >> 3) << (p & 7)) >>> 29; if (t === 7) throw verErr();
1181
+ SP = p + 3; geometries[i] = bodyCounted(d, t, depth + 1, f, prec);
1182
+ }
1183
+ return { type: 'GeometryCollection', geometries };
1184
+ }
1185
+ const mode = readMode(d), before = LOW;
1186
+ let coordinates, n = 1;
1187
+ if (type === 1) coordinates = pathCounted(d, mode, 0, 1, f, prec);
1188
+ else if (type === 2) coordinates = polygonCounted(d, mode, f, prec);
1189
+ else if (type === 3) coordinates = pathCounted(d, mode, 2, 1, f, prec);
1190
+ else {
1191
+ n = readCount(d); if (n * 4 > SEND - SP) throw err(T);
1192
+ coordinates = Array(n <= RP || 32 * n <= SEND - SP ? n : RP);
1193
+ if (type === 4) for (let i = 0; i < n; i++) coordinates[i] = pathCounted(d, mode, 0, 1, f, prec);
1194
+ else for (let i = 0; i < n; i++) coordinates[i] = polygonCounted(d, mode, f, prec);
1195
+ }
1196
+ // A mode that predicted nothing must be 0 (SPEC.md section 4): the
1197
+ // geometry holds nothing, or everything it holds is without a step.
1198
+ if (mode !== 0 && (LOW !== before || n === 0) && LOW - before === held(type, coordinates, n)) throw err(NC, SP);
1199
+ return { type: TYPES[type], coordinates };
1200
+ }
1201
+
1202
+ const T2_MAP = [0, 1, 2, 5], S2_MAP = [3, 4, 6], S3_MAP = [0, 3, 4, 5, 6];
1203
+ // need: the caller requires a check, and a string without one is refused
1204
+ // like one whose check fails.
1205
+ // maxLength isn't judged here: the callers that can have one a string
1206
+ // reaches judge it first, before anything of the string is read.
1207
+ function decodeHeader(str, len, line, need) {
1208
+ if (len === 0) throw err(T);
1209
+ let d = len < CUT ? shortBytes(str, len, DV) : toBytes(str, len);
1210
+ const h = d.getInt32(0); WIDE = OPEN = NX = STARTED = OPTS = 0;
1211
+ let type = 0, prec = 5, p = 4;
1212
+ if (h >= 0) { type = T2_MAP[(h >>> 29) & 3]; prec = 5 + ((h >>> 28) & 1); }
1213
+ else if ((h & 0x40000000) === 0) {
1214
+ const s2 = (h >>> 28) & 3, p6 = (h >>> 27) & 1; p = 5;
1215
+ if (s2 < 3) { type = S2_MAP[s2]; prec = 5 + p6; } else { type = p6 ? 2 : 1; prec = 7; }
1216
+ } else if ((h & 0x20000000) === 0) {
1217
+ const s3 = (h >>> 26) & 7; if (s3 >= 5) throw verErr();
1218
+ type = S3_MAP[s3]; prec = 7; p = 6;
1219
+ } else {
1220
+ if (h & 0x10000000) throw verErr();
1221
+ // The extended header: all 17 bits before any is judged, and the check
1222
+ // before the rest, so a damaged header is CHECKSUM whatever it says.
1223
+ if (len < 3) throw err(T);
1224
+ type = (h >>> 25) & 7; prec = (h >>> 21) & 15; const o = (h >>> 15) & 63, check = o & 3; p = 17;
1225
+ OPTS = o; STEPS = BIG = OPENED = 0;
1226
+ if (check === 3) throw verErr();
1227
+ if (check) {
1228
+ const tail = check === 2 ? 6 : 2;
1229
+ if (len < 3 + tail) throw err(T);
1230
+ const at = (len - tail) * 6, crc = crc32c(str, len - tail);
1231
+ if (check === 2 ? bitsBig(d, at, 32) !== crc || bits32(d, at + 32, 4) : bits32(d, at, 12) !== crc >>> 20) throw err(CS);
1232
+ len -= tail; d = len < CUT ? shortBytes(str, len, DV) : toBytes(str, len);
1233
+ } else if (need) throw err(CS);
1234
+ if (type === 7 || (o & 12) === 4) throw verErr();
1235
+ WIDE = o >> 5; OPEN = (o >> 4) & 1;
1236
+ if (prec > 7) { if (!WIDE) throw err(BH); BIG = 1; }
1237
+ if (o === 0 && prec >= 5 && prec <= 7) throw err(NC);
1238
+ if (OPEN && (type < 2 || type === 3 || type === 4)) throw err(NC);
1239
+ if (o & 8) { XF[0] = POW[(d.getInt32(p >> 3) << (p & 7)) >>> 28]; XP[0] = (d.getInt32((p + 4) >> 3) << ((p + 4) & 7)) >>> 31; XV[0] = XK[0] = 0; NX = 1; p += 5; }
1240
+ if (o & 4) { XF[1] = POW[(d.getInt32(p >> 3) << (p & 7)) >>> 28]; XP[1] = (d.getInt32((p + 4) >> 3) << ((p + 4) & 7)) >>> 31; XV[1] = XK[1] = 0; NX = 2; p += 5; }
1241
+ }
1242
+ if (need && (OPTS & 3) === 0) throw err(CS);
1243
+ if (line && type !== 1) throw err('WRONG_TYPE');
1244
+ DD = d; PREC = prec; SP = p; SEND = len * 6; SK1 = KI[prec]; SK2 = 0;
1245
+ return type;
1246
+ }
1247
+ // The end of the body: an option the string didn't need is another spelling
1248
+ // of it (SPEC.md section 13), then at most 5 zero bits.
1249
+ function checkEnded(d) {
1250
+ const p = SP;
1251
+ if (p > SEND) throw err(T);
1252
+ if (OPTS !== 0 && ((WIDE && !BIG) || (OPEN && !OPENED) || (NX && (!STARTED || (STEPS === 0 && (XP[0] || (NX > 1 && XP[1]))))))) throw err(NC);
1253
+ if (SEND - p > 5 || d.getInt32(p >> 3) << (p & 7) !== 0) throw err(TD);
1254
+ }
1255
+
1256
+ // What decode, decodeLine and canonical take as options: nothing, or an
1257
+ // object. Sets this call's caps and returns whether a check is required.
1258
+ // 'short' is accepted for checksum so that one options object can go to
1259
+ // encode and to decode; either kind of check satisfies the requirement.
1260
+ const isCap = (v) => typeof v === 'number' && v >= 0 && (v === Infinity || Number.isInteger(v));
1261
+ function decodeOptions(options) {
1262
+ if (options === null) return false;
1263
+ if (typeof options !== 'object') throw new TypeError('options must be an object');
1264
+ const l = options.maxLength, n = options.maxPositions, c = options.checksum;
1265
+ if ((l != null && !isCap(l)) || (n != null && !isCap(n))) throw new RangeError('maxLength and maxPositions must be integers from 0 up, or Infinity');
1266
+ if (c != null && c !== false && c !== true && c !== 'short') throw new RangeError("checksum must be true, false or 'short'");
1267
+ CAPL = l == null ? DEF_LEN : l < MAX_LEN ? l : MAX_LEN; CAPP = n == null ? DEF_POS : n;
1268
+ return c === true || c === 'short';
1269
+ }
1270
+ // decode and decodeLine with options, or without them for a string long
1271
+ // enough to hold more than the default maxPositions. The caps hold for this
1272
+ // call only. A string of at most maxPositions / 24 characters can't hold
1273
+ // more than maxPositions (nothing holds more than 4 to a bit), so it's
1274
+ // decoded without being counted. They are two functions so that a bundle
1275
+ // with one of decode and decodeLine doesn't carry the other's decoder.
1276
+ function decodeWith(str, len, options) {
1277
+ try {
1278
+ const need = options !== undefined && decodeOptions(options);
1279
+ if (len > CAPL) throw over(0);
1280
+ const type = decodeHeader(str, len, 0, need), d = DD;
1281
+ let g;
1282
+ if (len > CAPP / 24) { LEFT = CAPP; g = bodyCounted(d, type, 0, POW[PREC], PREC); } else g = decodeBody(d, type, 0, POW[PREC], PREC);
1283
+ checkEnded(d);
1284
+ return g;
1285
+ } finally { CAPL = DEF_LEN; CAPP = DEF_POS; }
1286
+ }
1287
+ function lineWith(str, len, options) {
1288
+ try {
1289
+ const need = options !== undefined && decodeOptions(options);
1290
+ if (len > CAPL) throw over(0);
1291
+ decodeHeader(str, len, 1, need);
1292
+ const d = DD, counted = len > CAPP / 24;
1293
+ if (counted) { if (SP > SEND) throw err(T); LEFT = CAPP - 1; if (LEFT < 0) throw over(1); } // the line is a geometry, and counts one like any other
1294
+ const mode = readMode(d), before = LOW, line = counted ? pathCounted(d, mode, 0, 0, POW[PREC], PREC) : decodePath(d, mode, 0, 0, POW[PREC], PREC);
1295
+ if (mode !== 0 && LOW !== before) throw err(NC, SP);
1296
+ checkEnded(d);
1297
+ return line;
1298
+ } finally { CAPL = DEF_LEN; CAPP = DEF_POS; }
1299
+ }
1300
+
1301
+ export function decode(str, options) {
1302
+ if (typeof str !== 'string') throw new TypeError('Expected a string');
1303
+ const len = str.length;
1304
+ // Three kinds of call leave here for decodeWith: one with options, a string
1305
+ // of more than DEF_FREE characters, which could pass a default limit, and
1306
+ // the empty string, which decodeHeader refuses there. One test covers the
1307
+ // last two: (len - 1) >>> 0 turns a length of 0 into 2^32 - 1.
1308
+ //
1309
+ // How these lines are written matters to the engines more than it should.
1310
+ // Both inline this function into a caller's loop, and Points decode a
1311
+ // quarter to a third slower where they don't. V8 inlines up to 460 bytes
1312
+ // of bytecode, and this is 444. With the test below the Points and one of
1313
+ // its own for the empty string, Points were 23% slower on Node.
1314
+ // JavaScriptCore ranks the whole call tree before it inlines. With
1315
+ // `options !== undefined` in this test it left the function out, and
1316
+ // Points were 33% slower on Bun 1.4. With `options != null` it's in. null
1317
+ // means no options anyway (decodeOptions).
1318
+ if (options != null || (len - 1) >>> 0 >= DEF_FREE) return decodeWith(str, len, options);
1319
+ if (len <= 11) {
1320
+ const c0 = str.charCodeAt(0);
1321
+ if (c0 >= 65 && c0 <= 72) {
1322
+ const d = shortBytes(str, len, DV), h = d.getInt32(0), prec = 5 + ((h >>> 28) & 1), end = len * 6;
1323
+ if ((h & 0x08000000) === 0) { if (end > 10 || (h << 5) !== 0) throw err(TD); return { type: 'Point', coordinates: [] }; }
1324
+ const p = readTruncAnchor(d, 5, prec), f = POW[prec];
1325
+ if (p > end) throw err(T);
1326
+ if (end - p > 5 || d.getInt32(p >> 3) << (p & 7) !== 0) throw err(TD);
1327
+ return { type: 'Point', coordinates: [SLNG / f, SLAT / f] };
1328
+ }
1329
+ }
1330
+ const type = decodeHeader(str, len, 0, false), d = DD, g = decodeBody(d, type, 0, POW[PREC], PREC);
1331
+ checkEnded(d);
1332
+ return g;
1333
+ }
1334
+
1335
+ export function decodeLine(str, options) {
1336
+ if (typeof str !== 'string') throw new TypeError('Expected a string');
1337
+ const len = str.length;
1338
+ // Up to DEF_FREE characters nothing can pass a default cap. A longer
1339
+ // string's length is judged before it's turned into bytes, so that one
1340
+ // over the limit costs nothing and is LIMIT_EXCEEDED whatever its
1341
+ // characters are.
1342
+ if (options !== undefined || len > DEF_FREE) return lineWith(str, len, options);
1343
+ const d = len < CUT ? shortBytes(str, len, DV) : toBytes(str, len);
1344
+ const h = d.getInt32(0);
1345
+ // Any other header. This block is written out here and not a call to
1346
+ // lineWith, which does the same: as a call, short lines and rings decoded
1347
+ // 1 to 1.7% slower on Node, though the call is never made for them.
1348
+ if (len < 2 || (h >>> 29) !== 1) {
1349
+ decodeHeader(str, len, 1, false);
1350
+ const dd = DD, mode = readMode(dd), before = LOW;
1351
+ const line = decodePath(dd, mode, 0, 0, POW[PREC], PREC);
1352
+ if (mode !== 0 && LOW !== before) throw err(NC, SP);
1353
+ checkEnded(dd);
1354
+ return line;
1355
+ }
1356
+ const prec = 5 + ((h >>> 28) & 1), f = POW[prec], end = len * 6;
1357
+ let p = 6, mode = 0;
1358
+ const mb = (h >>> 24) & 15;
1359
+ if (mb < 8) mode = mb >> 2; else if (mb < 12) mode = 2; else if (mb < 14) { mode = 3; p = 7; } else { mode = 4 + (mb & 1); p = 8; }
1360
+ const closed = (h << p) >>> 31; p++;
1361
+ let w = h << p, z = Math.clz32(w), m = 0;
1362
+ if (z < 10) { m = ((w << z) >>> (28 - z)) - 8; p += 4 + (z << 1); } else { SP = p; SEND = end; m = readCount(d); p = SP; }
1363
+ // The bounds rule before the allocation: on JavaScriptCore, Array(n) for a
1364
+ // forged count of 134 million took 140 ms and the memory with it.
1365
+ if (m === 0) { if (closed) throw err('EMPTY_CLOSED_PATH', p, end); }
1366
+ else if (m - 1 > (mode === 2 || mode === 3 || mode === 5 ? end - p : 4 * (end - p))) throw err(T);
1367
+ const out = Array(m + closed <= RPT || 2 * m <= end - p ? m + closed : RPT);
1368
+ if (m > 0) {
1369
+ let lat = 0, lng = 0;
1370
+ if (prec === 5) {
1371
+ let v = (d.getInt32(p >> 3) << (p & 7)) >>> 8; p += 24;
1372
+ if (v >= 15554431) { v = (v << 1) + ((d.getInt32(p >> 3) << (p & 7)) >>> 31) - 15554431; p++; }
1373
+ lat = v > 15500000 ? v - 24500001 : v - 6500000;
1374
+ v = (d.getInt32(p >> 3) << (p & 7)) >>> 7; p += 25;
1375
+ if (v >= 31108863) { v = (v << 1) + ((d.getInt32(p >> 3) << (p & 7)) >>> 31) - 31108863; p++; }
1376
+ lng = v > 32000000 ? v - 50000001 : v - 14000000;
1377
+ } else if (prec === 6) {
1378
+ let v = (((d.getInt32(p >> 3) << (p & 7)) >>> 16) << 11) | ((d.getInt32((p + 16) >> 3) << ((p + 16) & 7)) >>> 21); p += 27;
1379
+ if (v >= 88435455) { v = (v << 1) + ((d.getInt32(p >> 3) << (p & 7)) >>> 31) - 88435455; p++; }
1380
+ lat = v > 105000000 ? v - 195000001 : v - 15000000;
1381
+ v = (((d.getInt32(p >> 3) << (p & 7)) >>> 16) << 12) | ((d.getInt32((p + 16) >> 3) << ((p + 16) & 7)) >>> 20); p += 28;
1382
+ if (v >= 176870911) { v = (v << 1) + ((d.getInt32(p >> 3) << (p & 7)) >>> 31) - 176870911; p++; }
1383
+ lng = v > 210000000 ? v - 390000001 : v - 30000000;
1384
+ } else { p = readTruncAnchor(d, p, prec); lat = SLAT; lng = SLNG; }
1385
+ out[0] = [lat / f, lng / f];
1386
+ if (m > 1) {
1387
+ let o = 1;
1388
+ if (m < 18 && mode < 2) {
1389
+ const tj = TJ, lin = -mode, p0 = p;
1390
+ let kw = d.getInt32(p >> 3) << (p & 7), k = KI[prec], e = Math.clz32(kw), n = 0, z1 = 0, z2 = 0, va = 0, vb = 0, a = lat, b = lng;
1391
+ if (e < 8) { p += e + 1; k = (k + ((e >>> 1) ^ -(e & 1))) | 0; }
1392
+ else { e = (kw << 8) >>> 27; n = e - k; k = e > KMAX || (n < 0 ? -2 * n - 1 : 2 * n) < 8 ? -9 : e; p += 13; }
1393
+ if (k >>> 0 <= KMAX) {
1394
+ const kk = k + k, mk = (1 << k) - 1, one = kk < 15;
1395
+ let x = 0, y = 0;
1396
+ for (; o < m; o++) {
1397
+ kw = d.getInt32(p >> 3) << (p & 7); e = tj[kw >>> 21];
1398
+ if (e !== 0) {
1399
+ if (one) {
1400
+ n = e >> 8; kw = ((kw << n) >>> 1) >>> (31 - kk);
1401
+ z1 = ((e & 15) << k) | (kw >>> k); z2 = (((e >> 4) & 15) << k) | (kw & mk); p += n + kk;
1402
+ } else {
1403
+ p += e >> 8;
1404
+ z1 = ((e & 15) << k) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - k)); p += k;
1405
+ z2 = (((e >> 4) & 15) << k) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - k)); p += k;
1406
+ }
1407
+ } else {
1408
+ x = Math.clz32(kw); if (x > 23) { p += 24; x = Math.clz32(d.getInt32(p >> 3) << (p & 7)); if (x > 11) throw err(R, p + 12 - k, end); p += x + 1; x += 24; } else p += x + 1;
1409
+ y = Math.clz32(d.getInt32(p >> 3) << (p & 7)); if (y > 23) { p += 24; y = Math.clz32(d.getInt32(p >> 3) << (p & 7)); if (y > 11) throw err(R, x + k > 35 ? p - x + 11 - k : p + 12 - k, end); p += y + 1; y += 24; } else p += y + 1;
1410
+ z1 = ((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - k); p += k;
1411
+ z2 = ((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - k); p += k;
1412
+ if (x >= 6) { n = x - 4; if (n + k > 31) throw err(R, p - x - y - k - 2 * k + 34, end); x = 1; if (n > 16) { x = (1 << (n - 16)) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (47 - n)); p += n - 16; n = 16; } x = (((x << n) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - n))) + 2) | 0; p += n; if ((x >>> 0) < 2 || (k && (x >>> (32 - k)))) throw err(R, y + k > 35 ? p - y - k - 2 * k + 35 - (31 - Math.clz32(x - 2)) : p, end); }
1413
+ if (y >= 6) { n = y - 4; if (n + k > 31) throw err(R, p - y - k - 2 * k + 35 - ((x >>> 0) >= 6 ? 31 - Math.clz32(x - 2) : 0), end); y = 1; if (n > 16) { y = (1 << (n - 16)) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (47 - n)); p += n - 16; n = 16; } y = (((y << n) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - n))) + 2) | 0; p += n; if ((y >>> 0) < 2 || (k && (y >>> (32 - k)))) throw err(R, p, end); }
1414
+ z1 |= x << k; z2 |= y << k;
1415
+ }
1416
+ va = ((va & lin) + ((z1 >>> 1) ^ -(z1 & 1))) | 0; vb = ((vb & lin) + ((z2 >>> 1) ^ -(z2 & 1))) | 0;
1417
+ a = (a + va) | 0; b = (b + vb) | 0; out[o] = [a / f, b / f];
1418
+ }
1419
+ RA = a; RB = b;
1420
+ }
1421
+ if (o < m) { o = 1; p = p0; }
1422
+ }
1423
+ if (o < m) {
1424
+ if (mode < 2) p = bodyCoord(d, p, end, out, 1, m, -mode, 0, lat, lng, f, KI[prec]);
1425
+ else if (mode === 4) p = bodyEma(d, p, end, out, 1, m, 0, lat, lng, f, KI[prec]);
1426
+ else if (mode === 3) p = m === 4 && closed === 1 ? bodyOrtho4(d, p, end, out, 0, lat, lng, f, prec, KI[prec], 0) : bodyOrtho(d, p, end, out, 1, m, closed, 0, lat, lng, f, prec, KI[prec], 0);
1427
+ else p = bodySlope(d, p, end, out, 1, m, mode === 5 ? -1 : 0, 0, lat, lng, f, KI[prec], 0);
1428
+ }
1429
+ // An open line can't end on its first point: closed is how that's written.
1430
+ if (closed === 0 && RA === lat && RB === lng) throw err(NC, p, end);
1431
+ } else if (mode !== 0) throw err(NC, p, end); // a mode that predicted nothing must be 0
1432
+ if (closed) out[m] = [lat / f, lng / f];
1433
+ } else if (mode !== 0) throw err(NC, p, end);
1434
+ if (p > end) throw err(T);
1435
+ if (end - p > 5 || d.getInt32(p >> 3) << (p & 7) !== 0) throw err(TD);
1436
+ return out;
1437
+ }
1438
+
1439
+ // ---------------------------------------------------------------- encoder: branchless bit writer + hybrid toText
1440
+
1441
+ let OUT = null, ODV = null, WO = 0, WACC = 0, WN = 0;
1442
+ // Set while an encoder writes a binary value (SPEC.md section 23): toText and
1443
+ // addSum then give the bits as bytes, through it. The encoders are the same
1444
+ // ones. It's a function and not a flag so that the code for values is named
1445
+ // only by the exports that write them: with a flag, toText named it, and
1446
+ // import { encode } alone grew by 282 bytes gzipped.
1447
+ let BIN = null;
1448
+ function outBuffer(size) { const b = BufferCtor ? BufferCtor.allocUnsafeSlow(size) : new Uint8Array(size); if (OUT) b.set(OUT.subarray(0, WO)); OUT = b; ODV = new DataView(b.buffer, b.byteOffset, size); }
1449
+ function room(bytes) { if (WO + bytes + 8 > OUT.length) outBuffer(OUT.length * 2 + bytes); }
1450
+ function put(n, v) { const nb = WN + n, acc = (WACC << n) | v; ODV.setInt32(WO, acc << (32 - nb)); WACC = acc; WO += nb >> 3; WN = nb & 7; }
1451
+ function putBig(n, v) {
1452
+ if (n > 32) { const lo = v % 4294967296; putBig(n - 32, (v - lo) / 4294967296); v = lo; n = 32; }
1453
+ if (n > 16) { put(n - 16, v >>> 16); put(16, v & 65535); } else if (n > 0) put(n, v);
1454
+ }
1455
+ function putUnary(c) { for (; c > 23; c -= 24) put(24, 0); put(c + 1, 1); }
1456
+ function putCount(n) { if (n > MAXN) throw new RangeError('Too many points'); const bits = 66 - 2 * Math.clz32((n >>> 3) + 1); if (bits < 25) put(bits, n + 8); else putBig(bits, n + 8); }
1457
+ function putParam(k, pk, width) { const dz = k < pk ? 2 * (pk - k) - 1 : 2 * (k - pk); if (dz < 8) put(dz + 1, 1); else put(8 + width, k < 0 ? (width === 6 ? KWIDE : KMAX) - k : k); }
1458
+ const MODE_LEN = [2, 2, 2, 3, 4, 4], MODE_VAL = [0, 1, 2, 6, 14, 15];
1459
+
1460
+ const C12 = /* @__PURE__ */ (() => {
1461
+ const t = new Uint16Array(4096), little = new Uint8Array(new Uint16Array([1]).buffer)[0];
1462
+ for (let v = 0; v < 4096; v++) { const a = ALPHABET.charCodeAt(v >> 6), b = ALPHABET.charCodeAt(v & 63); t[v] = little ? a | (b << 8) : (a << 8) | b; }
1463
+ return t;
1464
+ })();
1465
+ let T12 = null, TEXT_CUT = Infinity, CAT_BELOW = Infinity, LONG = 0, TXT = null, T16 = null;
1466
+ const B64 = { alphabet: 'base64url', omitPadding: true };
1467
+
1468
+ function concatText(chars) {
1469
+ const t = T12 || (T12 = Array.from({ length: 4096 }, (_, v) => ALPHABET[v >> 6] + ALPHABET[v & 63])), dv = ODV, whole = chars >> 2;
1470
+ let s = '', i = 0, v = 0;
1471
+ for (; i < whole; i++) { v = dv.getInt32(i * 3) >>> 8; s += t[v >>> 12]; s += t[v & 4095]; }
1472
+ v = dv.getInt32(i * 3) >>> 8; i = chars & 3;
1473
+ if (i > 1) s += t[v >>> 12]; if (i & 1) s += ALPHABET[(v >>> (i === 1 ? 18 : 6)) & 63];
1474
+ return s;
1475
+ }
1476
+ function setupEncoder() {
1477
+ outBuffer(4096);
1478
+ const deno = !!globalThis.Deno, bun = !!globalThis.Bun, node = BufferCtor && !deno;
1479
+ for (let i = 0; i < 48; i++) OUT[i] = (i * 151 + 7) & 255;
1480
+ OUT[48] = OUT[49] = OUT[50] = OUT[51] = 0;
1481
+ const want = concatText(64), same = (mk) => { try { return mk() === want; } catch { return false; } };
1482
+ if (node && typeof OUT.base64urlSlice === 'function' && same(() => OUT.base64urlSlice(0, 48))) LONG = 1;
1483
+ else if (typeof OUT.toBase64 === 'function' && same(() => OUT.subarray(0, 48).toBase64(B64))) LONG = 2;
1484
+ if (LONG) TEXT_CUT = 96;
1485
+ if (LONG === 1 && typeof OUT.latin1Slice === 'function') {
1486
+ TXT = BufferCtor.allocUnsafeSlow(256); T16 = new Uint16Array(TXT.buffer, TXT.byteOffset, 128);
1487
+ WO = 48; CAT_BELOW = 0;
1488
+ if (same(() => toText())) { TEXT_CUT = bun ? 64 : 120; CAT_BELOW = bun ? 0 : 13; } else CAT_BELOW = Infinity;
1489
+ WO = 0;
1490
+ }
1491
+ }
1492
+ function toText() {
1493
+ if (BIN !== null) return BIN(null, 0);
1494
+ const b = OUT, total = WO * 8 + WN, chars = ((total + 5) / 6) | 0;
1495
+ let n = WO; if (WN) b[n++] = WACC << (8 - WN);
1496
+ b[n] = b[n + 1] = b[n + 2] = b[n + 3] = 0;
1497
+ let s;
1498
+ if (chars < TEXT_CUT) {
1499
+ if (chars < CAT_BELOW) s = concatText(chars);
1500
+ else {
1501
+ const t = T16, c = C12, dv = ODV;
1502
+ for (let i = 0, o = 0, v = 0; i < n; i += 3, o += 2) { v = dv.getInt32(i) >>> 8; t[o] = c[v >>> 12]; t[o + 1] = c[v & 4095]; }
1503
+ s = TXT.latin1Slice(0, chars);
1504
+ }
1505
+ } else { s = LONG === 1 ? b.base64urlSlice(0, n) : b.subarray(0, n).toBase64(B64); if (s.length > chars) s = s.slice(0, chars); }
1506
+ // A buffer that grew past KEEP goes back to a small one. Nothing is kept
1507
+ // from it: outBuffer copies the WO bytes written so far, and with WO left
1508
+ // at the length of a long string that copy didn't fit the small buffer.
1509
+ // Every encode whose output passed 4,096 bytes then threw a RangeError,
1510
+ // from the first one past KEEP (about 0.7 million characters) on.
1511
+ if (OUT.length > KEEP) { WO = 0; outBuffer(4096); }
1512
+ return s;
1513
+ }
1514
+ // kind 1: the 12 high bits of the CRC in 2 characters. 2: all 32 in 6.
1515
+ function addSum(s, kind) {
1516
+ if (BIN !== null) return BIN(s, kind);
1517
+ const c = crc32c(s, s.length);
1518
+ s += ALPHABET[c >>> 26] + ALPHABET[(c >>> 20) & 63];
1519
+ return kind === 1 ? s : s + ALPHABET[(c >>> 14) & 63] + ALPHABET[(c >>> 8) & 63] + ALPHABET[(c >>> 2) & 63] + ALPHABET[(c << 4) & 63];
1520
+ }
1521
+ function putHeader(type, prec, o, zp, zpred, mp, mpred) {
1522
+ WO = WACC = WN = 0;
1523
+ if (!o && (prec === 5 || prec === 6)) {
1524
+ const p6 = prec - 5;
1525
+ if (type < 3) put(4, (type << 1) | p6); else if (type === 5) put(4, 6 | p6);
1526
+ else put(5, (type === 3 ? 16 : type === 4 ? 18 : 20) | p6);
1527
+ } else if (!o && prec === 7) {
1528
+ if (type === 1 || type === 2) put(5, 21 + type);
1529
+ else put(6, 48 | (type === 0 ? 0 : type === 3 ? 1 : type === 4 ? 2 : type === 5 ? 3 : 4));
1530
+ } else {
1531
+ put(17, (14 << 13) | (type << 10) | (prec << 6) | o);
1532
+ if (o & 8) put(5, (zp << 1) | zpred); if (o & 4) put(5, (mp << 1) | mpred);
1533
+ }
1534
+ }
1535
+ function putTruncAnchor(lat, lng, prec) {
1536
+ if (prec === 5) {
1537
+ let u = lat + 6500000; if (u < 0) u += 18000001;
1538
+ if (u < 15554431) put(24, u); else { u += 15554431; put(9, u >>> 16); put(16, u & 65535); }
1539
+ u = lng + 14000000; if (u < 0) u += 36000001;
1540
+ if (u < 31108863) { put(9, u >>> 16); put(16, u & 65535); } else { u += 31108863; put(10, u >>> 16); put(16, u & 65535); }
1541
+ return;
1542
+ }
1543
+ if (prec === 6) {
1544
+ let u = lat + 15000000; if (u < 0) u += 180000001;
1545
+ if (u < 88435455) { put(11, u >>> 16); put(16, u & 65535); } else { u += 88435455; put(12, u >>> 16); put(16, u & 65535); }
1546
+ u = lng + 30000000; if (u < 0) u += 360000001;
1547
+ if (u < 176870911) { put(12, u >>> 16); put(16, u & 65535); } else { u += 176870911; put(13, u >>> 16); put(16, u & 65535); }
1548
+ return;
1549
+ }
1550
+ for (let s = 0; s < 2; s++) {
1551
+ const idx = (s << 3) + prec, N = AN_N[idx], t = AN_T[idx], b = AN_B[idx];
1552
+ let u = (s ? lng : lat) + AN_O[idx]; if (u < 0) u += N; if (u >= N) u -= N;
1553
+ if (u < t) putBig(b, u); else putBig(b + 1, u + t);
1554
+ }
1555
+ }
1556
+
1557
+ // ---------------------------------------------------------------- encoder: values, k selection & emit
1558
+
1559
+ const Z = /* @__PURE__ */ new Int32Array(192), ST = /* @__PURE__ */ new Int32Array(8), LS = /* @__PURE__ */ new Int32Array(6);
1560
+ const QC6 = /* @__PURE__ */ Uint8Array.from({ length: 256 }, (_, q) => (q < 6 ? q : 2 * (31 - Math.clz32(q - 2)) + 4));
1561
+ const INV_N = /* @__PURE__ */ Int32Array.from({ length: 33 }, (_, n) => (n ? Math.ceil(65536 / n) : 0));
1562
+ const SEVENTH = 1 / 7;
1563
+ // The fast encoders take a coordinate whose integer is within 32 bits:
1564
+ // |x * 10^p| below this rounds to at most 2^31 - 1.
1565
+ const I32H = 2147483647.5;
1566
+ let QA = /* @__PURE__ */ new Int32Array(1024), QB = /* @__PURE__ */ new Int32Array(1024), QN = 0;
1567
+ let PS = /* @__PURE__ */ new Int32Array(64), PM = /* @__PURE__ */ new Int32Array(64), PC = /* @__PURE__ */ new Int32Array(64), PN = 0;
1568
+ let EOPEN = 0, ESTART = 0, ELAT = 0, ELNG = 0, EK1 = 0, EK2 = 0, PI = 0, KBITS = 0, READY = 0, OR_N1 = 0, OR_N2 = 0, OR_K1 = 0, OR_K2 = 0, OR_STEPS = 0;
1569
+
1570
+ function pairedValues(i, cnt, isFirst, withEma) {
1571
+ const qa = QA, qb = QB, z = Z, st = ST;
1572
+ let va = st[0], vb = st[1], ea = st[2], eb = st[3], pa = qa[i - 1], pb = qb[i - 1], l0 = 0, l1 = 0, l4 = 0;
1573
+ for (let j = 0; j < cnt; j++, i++) {
1574
+ const a = qa[i], b = qb[i], da = (a - pa) | 0, db = (b - pb) | 0; pa = a; pb = b;
1575
+ let x = (da << 1) ^ (da >> 31), y = (db << 1) ^ (db >> 31);
1576
+ z[j] = x; z[cnt + j] = y; l0 += 64 - Math.clz32(x) - Math.clz32(y);
1577
+ x = (da - va) | 0; y = (db - vb) | 0; va = da; vb = db;
1578
+ x = (x << 1) ^ (x >> 31); y = (y << 1) ^ (y >> 31);
1579
+ z[32 + j] = x; z[32 + cnt + j] = y; l1 += 64 - Math.clz32(x) - Math.clz32(y);
1580
+ if (withEma) {
1581
+ x = (da - ea) | 0; y = (db - eb) | 0;
1582
+ if (isFirst && j === 0) { ea = da; eb = db; } else { ea = (ea + da) >> 1; eb = (eb + db) >> 1; }
1583
+ x = (x << 1) ^ (x >> 31); y = (y << 1) ^ (y >> 31);
1584
+ z[64 + j] = x; z[64 + cnt + j] = y; l4 += 64 - Math.clz32(x) - Math.clz32(y);
1585
+ }
1586
+ }
1587
+ st[0] = va; st[1] = vb; st[2] = ea; st[3] = eb; LS[0] = l0; LS[1] = l1; LS[4] = l4;
1588
+ }
1589
+ function pairedOne(o, mode, i, cnt, isFirst) {
1590
+ const qa = QA, qb = QB, z = Z, st = ST, o2 = o + cnt;
1591
+ let va = st[0], vb = st[1], pa = qa[i - 1], pb = qb[i - 1], l = 0;
1592
+ if (mode === 4) {
1593
+ for (let j = 0; j < cnt; j++, i++) {
1594
+ const a = qa[i], b = qb[i], da = (a - pa) | 0, db = (b - pb) | 0; pa = a; pb = b;
1595
+ let x = (da - va) | 0, y = (db - vb) | 0;
1596
+ if (isFirst && j === 0) { va = da; vb = db; } else { va = (va + da) >> 1; vb = (vb + db) >> 1; }
1597
+ x = (x << 1) ^ (x >> 31); y = (y << 1) ^ (y >> 31);
1598
+ z[o + j] = x; z[o2 + j] = y; l += 64 - Math.clz32(x) - Math.clz32(y);
1599
+ }
1600
+ } else {
1601
+ const lin = -mode;
1602
+ for (let j = 0; j < cnt; j++, i++) {
1603
+ const a = qa[i], b = qb[i], da = (a - pa) | 0, db = (b - pb) | 0; pa = a; pb = b;
1604
+ let x = (da - (va & lin)) | 0, y = (db - (vb & lin)) | 0; va = da; vb = db;
1605
+ x = (x << 1) ^ (x >> 31); y = (y << 1) ^ (y >> 31);
1606
+ z[o + j] = x; z[o2 + j] = y; l += 64 - Math.clz32(x) - Math.clz32(y);
1607
+ }
1608
+ }
1609
+ st[0] = va; st[1] = vb; LS[mode] = l;
1610
+ }
1611
+ function slopeValues(i, cnt, isFirst) {
1612
+ const qa = QA, qb = QB, z = Z, st = ST, inv = INV;
1613
+ let sw = st[4], s = st[5], g = st[6], pM = st[7], pa = qa[i - 1], pb = qb[i - 1], l2 = 0, l5 = 0, lm = 0;
1614
+ for (let j = 0; j < cnt; j++, i++) {
1615
+ const a = qa[i], b = qb[i], da = (a - pa) | 0, db = (b - pb) | 0; pa = a; pb = b;
1616
+ if (isFirst && j === 0) {
1617
+ const x = (da << 1) ^ (da >> 31), y = (db << 1) ^ (db >> 31);
1618
+ z[96] = z[112] = x; z[128] = y;
1619
+ sw = Math.abs(db) >= Math.abs(da) ? -1 : 0; const dM = sw ? db : da, dm = sw ? da : db;
1620
+ s = divSlope(dm, dM); pM = dM; g = dM !== 0 ? (dM >> 31) : 0; continue;
1621
+ }
1622
+ const dM = sw ? db : da, dm = sw ? da : db, ev = (dm - ((Math.imul(dM, s) + 2048) >> 12)) | 0, ez = (ev << 1) ^ (ev >> 31);
1623
+ z[128 + j] = ez; lm += 32 - Math.clz32(ez);
1624
+ // Below -2^29 the fold is that of u + 2^32, which the decoder wraps back.
1625
+ const u = ((dM ^ g) - g) | 0, w = u >>> 0;
1626
+ const fz = u < -536870912 ? w + Math.floor(w / 7) : u < 0 ? (~u) * 8 + 7 : u + ((u * SEVENTH) | 0);
1627
+ z[96 + j] = fz; l2 += 32 - Math.clz32(fz);
1628
+ const lz = (((dM - pM) | 0) << 1) ^ (((dM - pM) | 0) >> 31);
1629
+ z[112 + j] = lz; l5 += 32 - Math.clz32(lz);
1630
+ pM = dM; const adm = dm < 0 ? -dm : dm, adM = dM < 0 ? -dM : dM;
1631
+ if (adm > adM) { sw = ~sw; s = adm < 512 ? ((adm ? Math.imul(dM, inv[adm]) >> 12 : 0) * (dm < 0 ? -1 : 1)) | 0 : ((dM << 12) / dm) | 0; pM = dm; }
1632
+ else if (ev !== 0 && dM !== 0) { let ds = adM < 512 ? ((Math.imul(ev, inv[adM]) >> 12) * (dM < 0 ? -1 : 1)) | 0 : ((ev << 12) / dM) | 0; if (adM < 4) ds >>= 1; s = (s + ds) | 0; }
1633
+ if (pM !== 0) g = pM >> 31;
1634
+ }
1635
+ st[4] = sw; st[5] = s; st[6] = g; st[7] = pM; LS[2] = l2; LS[5] = l5; LS[3] = lm;
1636
+ }
1637
+ let QB_K = 0;
1638
+ function quickBits(off, n, len, isPaired, allowZero) {
1639
+ if (len === 0) { QB_K = isPaired ? -2 : allowZero ? -1 : 0; return isPaired ? 4 : allowZero ? 2 : n + 1; }
1640
+ const z = Z, qc = QC6, end = off + n;
1641
+ let k = (((len + (n >> 1)) * INV_N[n]) >> 16) - 1, ca = 0, cb = 0, q = 0;
1642
+ k = k < 0 ? 0 : k > KMAX - 1 ? KMAX - 1 : k;
1643
+ for (let i = off; i < end; i++) {
1644
+ q = z[i] >>> k; ca = (ca + (q < 256 ? qc[q] : 2 * (31 - Math.clz32(q - 2)) + 4)) | 0;
1645
+ q >>>= 1; cb = (cb + (q < 256 ? qc[q] : 2 * (31 - Math.clz32(q - 2)) + 4)) | 0;
1646
+ }
1647
+ if (cb + n < ca) { QB_K = k + 1; return n * (k + 2) + cb; }
1648
+ QB_K = k; return n * (k + 1) + ca;
1649
+ }
1650
+ function chooseK(off, n, pk, len, isPaired, allowZeroMinor, mf) {
1651
+ const z = Z, qc = QC6, end = off + n;
1652
+ if (len === 0) {
1653
+ const cK0 = (pk === 0 ? 1 : pk <= 4 ? 2 * pk : 13) + n, c2 = pk <= 2 ? 2 * (pk + 2) : 13, c1 = pk <= 3 ? 2 * (pk + 1) : 13;
1654
+ if (isPaired && c2 < cK0) { KBITS = c2; return -2; }
1655
+ if (allowZeroMinor && c1 < cK0) { KBITS = c1; return -1; }
1656
+ KBITS = cK0; return 0;
1657
+ }
1658
+ let best = 1e9, bk = 0, q = 0, ca = 0, cb = 0, all = 0, total = 0, v = 0;
1659
+ let k0 = (((len + (n >> 1)) * INV_N[n]) >> 16) - 1;
1660
+ k0 = k0 < 0 ? 0 : k0 > KMAX - 1 ? KMAX - 1 : k0;
1661
+ if (mf === 1) {
1662
+ for (let i = off; i < end; i++) {
1663
+ v = z[i]; q = v >>> k0; ca = (ca + (q < 256 ? qc[q] : 2 * (31 - Math.clz32(q - 2)) + 4)) | 0;
1664
+ q >>>= 1; cb = (cb + (q < 256 ? qc[q] : 2 * (31 - Math.clz32(q - 2)) + 4)) | 0;
1665
+ }
1666
+ q = k0 < pk ? 2 * (pk - k0) - 1 : 2 * (k0 - pk); ca += n * (k0 + 1) + (q < 8 ? q + 1 : 13);
1667
+ q = k0 + 1 < pk ? 2 * (pk - k0) - 3 : 2 * (k0 + 1 - pk); cb += n * (k0 + 2) + (q < 8 ? q + 1 : 13);
1668
+ if (cb < ca) { KBITS = cb; return k0 + 1; }
1669
+ KBITS = ca; return k0;
1670
+ } else {
1671
+ for (let i = off; i < end; i++) {
1672
+ v = z[i]; all |= v; total = (total + v) | 0;
1673
+ q = v >>> k0; ca = (ca + (q < 256 ? qc[q] : 2 * (31 - Math.clz32(q - 2)) + 4)) | 0;
1674
+ q >>>= 1; cb = (cb + (q < 256 ? qc[q] : 2 * (31 - Math.clz32(q - 2)) + 4)) | 0;
1675
+ }
1676
+ q = k0 < pk ? 2 * (pk - k0) - 1 : 2 * (k0 - pk); ca += n * (k0 + 1) + (q < 8 ? q + 1 : 13);
1677
+ q = k0 + 1 < pk ? 2 * (pk - k0) - 3 : 2 * (k0 + 1 - pk); cb += n * (k0 + 2) + (q < 8 ? q + 1 : 13);
1678
+ if (cb < ca) { best = cb; bk = k0 + 1; } else { best = ca; bk = k0; }
1679
+ if ((all >>> (k0 + 4)) !== 0) {
1680
+ let mean = total / n;
1681
+ if (all >>> 26) { for (mean = 0, v = off; v < end; v++) mean += z[v] >>> 0; mean /= n; }
1682
+ let kv = mean < 1 ? 0 : 31 - Math.clz32(mean); if (kv > KMAX) kv = KMAX;
1683
+ if (kv > k0 + 1) {
1684
+ q = kv < pk ? 2 * (pk - kv) - 1 : 2 * (kv - pk); cb = n * (kv + 1) + (q < 8 ? q + 1 : 13);
1685
+ for (let i = off; i < end && cb < best; i++) { q = z[i] >>> kv; cb = (cb + (q < 256 ? qc[q] : 2 * (31 - Math.clz32(q - 2)) + 4)) | 0; }
1686
+ if (cb < best) { best = cb; bk = kv; }
1687
+ }
1688
+ }
1689
+ }
1690
+ if (isPaired && pk <= 2 && len <= (n >> 2)) {
1691
+ const cnt = n >> 1; let cSp = 2 * (pk + 1) + cnt;
1692
+ for (let i = 0; i < cnt && cSp < best; i++) {
1693
+ const a = z[off + i] >>> 0, b = z[off + cnt + i] >>> 0;
1694
+ if ((a | b) === 0) continue;
1695
+ const b2 = a === 0 ? b - 1 : b;
1696
+ cSp += 2 + (a < 256 ? qc[a] : 2 * (31 - Math.clz32(a - 2)) + 4) + (b2 < 256 ? qc[b2] : 2 * (31 - Math.clz32(b2 - 2)) + 4);
1697
+ }
1698
+ if (cSp + 3 < best) { best = cSp; bk = -1; }
1699
+ }
1700
+ KBITS = best; return bk;
1701
+ }
1702
+ function putPairSlow(z1, z2, k1, k2) {
1703
+ const q1 = z1 >>> k1, q2 = z2 >>> k2;
1704
+ let c1 = q1, n1 = 0, e1 = 0, c2 = q2, n2 = 0, e2 = 0;
1705
+ if (q1 >= 6) { const v = q1 - 2; n1 = 31 - Math.clz32(v); c1 = n1 + 4; e1 = v - (1 << n1); }
1706
+ if (q2 >= 6) { const v = q2 - 2; n2 = 31 - Math.clz32(v); c2 = n2 + 4; e2 = v - (1 << n2); }
1707
+ putUnary(c1); putUnary(c2);
1708
+ if (k1) put(k1, z1 & ((1 << k1) - 1)); if (k2) put(k2, z2 & ((1 << k2) - 1));
1709
+ if (n1) putBig(n1, e1); if (n2) putBig(n2, e2);
1710
+ }
1711
+ function putSingleSlow(z, k) {
1712
+ const q = z >>> k; let c = q, n = 0, e = 0;
1713
+ if (q >= 6) { const v = q - 2; n = 31 - Math.clz32(v); c = n + 4; e = v - (1 << n); }
1714
+ putUnary(c); if (k) put(k, z & ((1 << k) - 1)); if (n) putBig(n, e);
1715
+ }
1716
+ function putPairFast(z1, z2, k1, k2) {
1717
+ const q1 = (z1 >>> k1) | 0, q2 = (z2 >>> k2) | 0;
1718
+ if ((q1 | q2) >>> 0 < 6) {
1719
+ const n = (q1 + q2 + 2) | 0, kk = k1 + k2;
1720
+ if (n + kk < 25) { put(n + kk, (((1 << (q2 + 1)) | 1) << kk) | ((z1 & ((1 << k1) - 1)) << k2) | (z2 & ((1 << k2) - 1))); return; }
1721
+ }
1722
+ putPairSlow(z1, z2, k1, k2);
1723
+ }
1724
+ function putBlockFast(o1, o2, cnt, k1, k2) {
1725
+ if (k1 === -2) return;
1726
+ const z = Z, dv = ODV; let acc = WACC, nb = WN, o = WO;
1727
+ if (k1 === -1) {
1728
+ let mask = 0; for (let j = 0; j < cnt; j++) mask = (mask << 1) | ((z[o1 + j] | z[o2 + j]) ? 1 : 0);
1729
+ nb += cnt; acc = (acc << cnt) | mask; dv.setInt32(o, acc << (32 - nb)); o += nb >> 3; nb &= 7;
1730
+ WACC = acc; WN = nb; WO = o;
1731
+ for (let j = 0; j < cnt; j++) { let a = z[o1 + j] >>> 0, b = z[o2 + j] >>> 0; if (a | b) putPairFast(a, a === 0 ? (b - 1) >>> 0 : b, 0, 0); }
1732
+ return;
1733
+ }
1734
+ if (k2 < 0) { for (let j = 0; j < cnt; j++) putSingleSlow(z[o1 + j], k1); return; }
1735
+ const kk = k1 + k2, m1 = (1 << k1) - 1, m2 = (1 << k2) - 1;
1736
+ for (let j = 0; j < cnt; j++) {
1737
+ const z1 = z[o1 + j], z2 = z[o2 + j], q1 = (z1 >>> k1) | 0, q2 = (z2 >>> k2) | 0;
1738
+ if ((q1 | q2) >>> 0 < 6) {
1739
+ let n = (q1 + q2 + 2) | 0;
1740
+ if (n + kk < 25) { n += kk; acc = (acc << n) | (((1 << (q2 + 1)) | 1) << kk) | ((z1 & m1) << k2) | (z2 & m2); }
1741
+ else {
1742
+ acc = (acc << n) | (1 << (q2 + 1)) | 1; nb += n; dv.setInt32(o, acc << (32 - nb)); o += nb >> 3; nb &= 7;
1743
+ acc = (acc << k1) | (z1 & m1); nb += k1; dv.setInt32(o, acc << (32 - nb)); o += nb >> 3; nb &= 7;
1744
+ acc = (acc << k2) | (z2 & m2); n = k2;
1745
+ }
1746
+ nb += n; dv.setInt32(o, acc << (32 - nb)); o += nb >> 3; nb &= 7;
1747
+ } else { WACC = acc; WN = nb; WO = o; putPairSlow(z1, z2, k1, k2); acc = WACC; nb = WN; o = WO; }
1748
+ }
1749
+ WACC = acc; WN = nb; WO = o;
1750
+ }
1751
+
1752
+ // Mode 3 (ortho) residual builder + pricer/emitter (inlined ODV bit writer + m === 4 fast path!)
1753
+ function emitOrthoBlock(cnt, i, n, closingK2, rk1, rk2) {
1754
+ const z = Z, dv = ODV;
1755
+ let acc = WACC, nb = WN, o = WO, p1 = 0, p2 = 0;
1756
+ const m1 = (1 << rk1) - 1, m2 = rk2 >= 0 ? (1 << rk2) - 1 : 0, kk12 = rk1 + rk2;
1757
+ for (let j = 0; j < cnt; j++) {
1758
+ const idx = i + j;
1759
+ let z1 = 0, z2 = 0, k1 = rk1, k2 = rk2, mk1 = m1, mk2 = m2, kk = kk12;
1760
+ if (idx === 0) { z1 = z[144 + p1++]; z2 = z[144 + p1++]; k2 = rk1; mk2 = m1; kk = rk1 + rk1; }
1761
+ else if (closingK2 && idx === n - 1) {
1762
+ if (rk2 < 0) { p2 += 2; continue; }
1763
+ z1 = z[162 + p2++]; z2 = z[162 + p2++]; k1 = rk2; mk1 = m2; kk = rk2 + rk2;
1764
+ } else if (rk2 < 0) {
1765
+ WACC = acc; WN = nb; WO = o; putSingleSlow(z[144 + p1++], rk1); p2++; acc = WACC; nb = WN; o = WO; continue;
1766
+ } else { z1 = z[144 + p1++]; z2 = z[162 + p2++]; }
1767
+ const q1 = (z1 >>> k1) | 0, q2 = (z2 >>> k2) | 0;
1768
+ if ((q1 | q2) >>> 0 < 6) {
1769
+ let b = (q1 + q2 + 2) | 0;
1770
+ if (b + kk < 25) { b += kk; acc = (acc << b) | (((1 << (q2 + 1)) | 1) << kk) | ((z1 & mk1) << k2) | (z2 & mk2); }
1771
+ else {
1772
+ acc = (acc << b) | (1 << (q2 + 1)) | 1; nb += b; dv.setInt32(o, acc << (32 - nb)); o += nb >> 3; nb &= 7;
1773
+ acc = (acc << k1) | (z1 & mk1); nb += k1; dv.setInt32(o, acc << (32 - nb)); o += nb >> 3; nb &= 7;
1774
+ acc = (acc << k2) | (z2 & mk2); b = k2;
1775
+ }
1776
+ nb += b; dv.setInt32(o, acc << (32 - nb)); o += nb >> 3; nb &= 7;
1777
+ } else { WACC = acc; WN = nb; WO = o; putPairSlow(z1, z2, k1, k2); acc = WACC; nb = WN; o = WO; }
1778
+ }
1779
+ WACC = acc; WN = nb; WO = o;
1780
+ }
1781
+ function orthoHope(at, m, prec) {
1782
+ const qa = QA, qb = QB, c2 = cos2Int(qa[at], prec) * (1 / 4096);
1783
+ const da1 = qa[at + 1] - qa[at], db1 = qb[at + 1] - qb[at];
1784
+ const da2 = qa[at + 2] - qa[at + 1], db2 = qb[at + 2] - qb[at + 1];
1785
+ if ((da1 | db1) === 0 || Math.abs(da1 * da2 + db1 * db2 * c2) <= Math.abs(da1 * db2 - db1 * da2) * 0.7) return true;
1786
+ if (m >= 5) {
1787
+ const da3 = qa[at + 3] - qa[at + 2], db3 = qb[at + 3] - qb[at + 2];
1788
+ if (Math.abs(da2 * da3 + db2 * db3 * c2) <= Math.abs(da2 * db3 - db2 * da3) * 0.7) return true;
1789
+ }
1790
+ return false;
1791
+ }
1792
+ function orthoPath(at, m, closed, prec, ck1, ck2, mf, emit, quickOnly, maxSteps) {
1793
+ const qa = QA, qb = QB, z = Z, n = m - 1, lat0 = qa[at], lng0 = qb[at], c2 = cos2Int(lat0, prec), c2s = c2 >> 4;
1794
+ // Ultra-fast path for 5-point building rectangles (m === 4, closed === 1): ZERO swU, oerr, maxL1, Math.trunc!
1795
+ if (m === 4 && closed === 1) {
1796
+ OR_STEPS = 3;
1797
+ const da1 = (qa[at + 1] - lat0) | 0, db1 = (qb[at + 1] - lng0) | 0;
1798
+ const da2 = (qa[at + 2] - qa[at + 1]) | 0, db2 = (qb[at + 2] - qb[at + 1]) | 0;
1799
+ const da3 = (qa[at + 3] - qa[at + 2]) | 0, db3 = (qb[at + 3] - qb[at + 2]) | 0;
1800
+ let ua = da1, ub = db1;
1801
+ while ((ua < 0 ? -ua : ua) >= 262144 || (ub < 0 ? -ub : ub) >= 262144) { ua >>= 1; ub >>= 1; }
1802
+ const fva = -((Math.imul(c2, ub) + 2048) >> 12) | 0, fvb = ua;
1803
+ const swV = (fvb < 0 ? -fvb : fvb) >= (fva < 0 ? -fva : fva) ? 1 : 0;
1804
+ const sV = swV ? divSlope(fva, fvb) : divSlope(fvb, fva);
1805
+ const dM = swV ? db2 : da2, dm = swV ? da2 : db2, ev = (dm - ((Math.imul(dM, sV) + 2048) >> 12)) | 0;
1806
+ const ea = (da3 + da1) | 0, eb = (db3 + db1) | 0;
1807
+ const z0a = (da1 << 1) ^ (da1 >> 31), z0b = (db1 << 1) ^ (db1 >> 31), z1a = (dM << 1) ^ (dM >> 31);
1808
+ const z1b = (ev << 1) ^ (ev >> 31), z2a = (ea << 1) ^ (ea >> 31), z2b = (eb << 1) ^ (eb >> 31);
1809
+ z[144] = z0a; z[145] = z0b; z[146] = z1a; z[162] = z1b; z[163] = z2a; z[164] = z2b;
1810
+ const l1 = 96 - Math.clz32(z0a) - Math.clz32(z0b) - Math.clz32(z1a), l2 = 96 - Math.clz32(z1b) - Math.clz32(z2a) - Math.clz32(z2b);
1811
+ if (quickOnly) return quickBits(144, 3, l1, false, false) + quickBits(162, 3, l2, false, true) + 2;
1812
+ const rk1 = chooseK(144, 3, ck1, l1, false, false, mf); let bits = KBITS;
1813
+ const rk2 = chooseK(162, 3, ck2, l2, false, true, mf); bits += KBITS;
1814
+ OR_N1 = 3; OR_N2 = 3; OR_K1 = rk1; OR_K2 = rk2; RK1 = rk1; RK2 = rk2 < 0 ? 0 : rk2;
1815
+ if (emit) { if (WO + 128 > OUT.length) room(128); putParam(rk1, ck1, 5); putParam(rk2, ck2, 5); emitOrthoBlock(3, 0, 3, true, rk1, rk2); }
1816
+ return bits;
1817
+ }
1818
+ const closingK2 = closed === 1 && m >= 4, lim = maxSteps < n ? maxSteps : n;
1819
+ let swU = 0, sU = 0, swV = 0, sV = 0, maxL1 = 0, nextV = 1, da1 = 0, db1 = 0, bits = 0, k1 = ck1, k2 = ck2;
1820
+ OR_STEPS = 0;
1821
+ for (let i = 0; i < lim; i += BLOCK) {
1822
+ const cnt = Math.min(BLOCK, n - i); OR_STEPS += cnt;
1823
+ let n1 = 0, n2 = 0, l1 = 0, l2 = 0;
1824
+ for (let j = 0; j < cnt; j++) {
1825
+ const idx = i + j, pi = at + 1 + idx, da = (qa[pi] - qa[pi - 1]) | 0, db = (qb[pi] - qb[pi - 1]) | 0;
1826
+ if (idx === 0) {
1827
+ const za = (da << 1) ^ (da >> 31), zb = (db << 1) ^ (db >> 31);
1828
+ z[144 + n1++] = za; z[144 + n1++] = zb; l1 += 64 - Math.clz32(za) - Math.clz32(zb);
1829
+ da1 = da; db1 = db; setOrthoFrame(da, db, c2); swU = OF_SWU; sU = OF_SU; swV = OF_SWV; sV = OF_SV;
1830
+ maxL1 = (da < 0 ? -da : da) + (db < 0 ? -db : db); nextV = 1;
1831
+ } else if (closingK2 && idx === n - 1) {
1832
+ const cw = nextV ? swV : swU, cs = nextV ? sV : sU, pA = cw ? cs >> 4 : 256, pB = cw ? 256 : cs >> 4;
1833
+ const wB = (c2s * pB + 128) >> 8, denom = pA * pA + wB * pB, num = ((lat0 - qa[pi - 1]) | 0) * pA + ((lng0 - qb[pi - 1]) | 0) * wB;
1834
+ const ea = (da - (Math.trunc((num * pA) / denom) | 0)) | 0, eb = (db - (Math.trunc((num * pB) / denom) | 0)) | 0;
1835
+ const za = (ea << 1) ^ (ea >> 31), zb = (eb << 1) ^ (eb >> 31);
1836
+ z[162 + n2++] = za; z[162 + n2++] = zb; l2 += 64 - Math.clz32(za) - Math.clz32(zb);
1837
+ } else {
1838
+ const cw = nextV ? swV : swU, cs = nextV ? sV : sU, dM = cw ? db : da, dm = cw ? da : db;
1839
+ const ev = (dm - ((Math.imul(dM, cs) + 2048) >> 12)) | 0, za = (dM << 1) ^ (dM >> 31), zb = (ev << 1) ^ (ev >> 31);
1840
+ z[144 + n1++] = za; z[162 + n2++] = zb; l1 += 32 - Math.clz32(za); l2 += 32 - Math.clz32(zb);
1841
+ let wasU = nextV === 0;
1842
+ if (ev !== 0) {
1843
+ const ow = nextV ? swU : swV, os = nextV ? sU : sV, oerr = ((ow ? da : db) - ((Math.imul(ow ? db : da, os) + 2048) >> 12)) | 0;
1844
+ const st = (ev < 0 ? -ev : ev) <= (oerr < 0 ? -oerr : oerr); wasU = nextV ? !st : st;
1845
+ }
1846
+ nextV = wasU ? 1 : 0;
1847
+ const ada = da < 0 ? -da : da, adb = db < 0 ? -db : db;
1848
+ if (ada < 262144 && adb < 262144 && ada + adb > maxL1) {
1849
+ maxL1 = ada + adb;
1850
+ if (wasU) setOrthoFrame(da, db, c2); else setOrthoFrame(db, -(((da << 12) / c2) | 0) | 0, c2);
1851
+ swU = OF_SWU; sU = OF_SU; swV = OF_SWV; sV = OF_SV;
1852
+ }
1853
+ }
1854
+ }
1855
+ if (quickOnly) {
1856
+ if (n1 > 0) bits += quickBits(144, n1, l1, false, false) + 1;
1857
+ if (n2 > 0) bits += quickBits(162, n2, l2, false, true) + 1;
1858
+ } else {
1859
+ let rk1 = k1, rk2 = k2;
1860
+ if (n1 > 0) { rk1 = chooseK(144, n1, k1, l1, false, false, mf); bits += KBITS; }
1861
+ if (n2 > 0) { rk2 = chooseK(162, n2, k2, l2, false, true, mf); bits += KBITS; }
1862
+ OR_N1 = n1; OR_N2 = n2; OR_K1 = rk1; OR_K2 = rk2;
1863
+ if (emit) {
1864
+ if (WO + 400 > OUT.length) room(400);
1865
+ if (n1 > 0) putParam(rk1, k1, 5); if (n2 > 0) putParam(rk2, k2, 5);
1866
+ emitOrthoBlock(cnt, i, n, closingK2, rk1, rk2);
1867
+ }
1868
+ if (n1 > 0) k1 = rk1; if (n2 > 0) k2 = rk2 < 0 ? 0 : rk2;
1869
+ }
1870
+ }
1871
+ RK1 = k1; RK2 = k2;
1872
+ return bits;
1873
+ }
1874
+
1875
+ // ---------------------------------------------------------------- mode picker + slopeHope gate
1876
+
1877
+ let HM = 0, HL = 0, HR = 0;
1878
+ function slopeHope(i, end, warm) {
1879
+ const qa = QA, qb = QB;
1880
+ let pa = qa[i - 1], pb = qb[i - 1], sa = 0, sb = 0, hm = 0, hl = 0, hr = 0;
1881
+ if (warm) { sa = pa - qa[i - 2]; sb = pb - qb[i - 2]; }
1882
+ for (; i < end; i++) {
1883
+ const da = qa[i] - pa, db = qb[i] - pb; pa += da; pb += db;
1884
+ let x = sa < 0 ? -sa : sa, y = sb < 0 ? -sb : sb, r = x > y ? x : y;
1885
+ x = da < 0 ? -da : da; y = db < 0 ? -db : db; if (y > x) { const c = x; x = y; y = c; }
1886
+ if (x > 0) hm += 33 - Math.clz32(x);
1887
+ if (x !== r) hl += 33 - Math.clz32(x > r ? x - r : r - x);
1888
+ if (r > 0) { const c = Math.abs(da * sb - db * sa); if (c > 0) { y = (c < 4294967296 ? 33 - Math.clz32(c) : 65 - Math.clz32(c / 4294967296)) - (32 - Math.clz32(r)); hr += y < 1 ? 1 : y; } }
1889
+ else if (y > 0) hr += 33 - Math.clz32(y);
1890
+ sa = da; sb = db;
1891
+ }
1892
+ HM += hm; HL += hl; HR += hr;
1893
+ }
1894
+
1895
+ const SLOPE_MIN = 0.08, ORTHO_MIN = 0.05, EMA_MIN = 0.015, SAMPLE_MIN = 64, SAMPLE_MAX = 160, SLOPE_FROM = 4, HOPE = 0.06, HOPE1 = 0.08, CLEAR = 0.02;
1896
+ let WIN = /* @__PURE__ */ new Int32Array(3 * 64);
1897
+
1898
+ function pickModeGeo(first, last, prec, favor) {
1899
+ const speed = favor === 'speed', mf = speed ? 1 : 0;
1900
+ let total = 0, hasClosed4 = false, nw = 0, pts = 0;
1901
+ READY = 0;
1902
+ for (let t = first; t < last; t++) {
1903
+ if (PM[t] > 1) total += PM[t] - 1;
1904
+ if (PC[t] && PM[t] >= 4 && PM[t] <= 64 && orthoHope(PS[t], PM[t], prec)) hasClosed4 = true;
1905
+ }
1906
+ if (total === 0) return 0;
1907
+
1908
+ const sample = Math.max(SAMPLE_MIN, Math.min(SAMPLE_MAX, total >> 2));
1909
+ const need = 3 * (sample < total ? (sample >> 4) : last - first);
1910
+ if (need > WIN.length) WIN = new Int32Array(need + (need >> 1));
1911
+ const win = WIN;
1912
+ if (sample >= total) {
1913
+ for (let t = first; t < last; t++) if (PM[t] > 1) { win[nw++] = PS[t] + 1; win[nw++] = PS[t] + PM[t]; win[nw++] = 0; }
1914
+ pts = total;
1915
+ } else {
1916
+ const windows = sample >> 4;
1917
+ for (let w = 0, t = first, before = 0; w < windows; w++) {
1918
+ const g = Math.floor(((total - BLOCK) * w) / (windows - 1));
1919
+ for (; PM[t] < 2 || g >= before + PM[t] - 1; t++) if (PM[t] > 1) before += PM[t] - 1;
1920
+ const from = PS[t] + 1, at = from + g - before, end = PS[t] + PM[t], stop = at + BLOCK < end ? at + BLOCK : end;
1921
+ win[nw++] = at; win[nw++] = stop; win[nw++] = at > from ? 1 : 0; pts += stop - at;
1922
+ }
1923
+ }
1924
+
1925
+ const one = last - first === 1 && PM[first] - 1 <= BLOCK, withEma = !speed && total >= 48;
1926
+ let c0 = 2, c1 = 2, c4 = 4, l0 = 0, l1 = 0;
1927
+ for (let w = 0; w < nw; w += 3) {
1928
+ const start = win[w], end = win[w + 1], inside = win[w + 2];
1929
+ ST[0] = ST[1] = ST[2] = ST[3] = 0;
1930
+ if (inside) pairedValues(start - 1, 1, false, withEma);
1931
+ for (let i = start; i < end; i += BLOCK) {
1932
+ const cnt = end - i < BLOCK ? end - i : BLOCK;
1933
+ pairedValues(i, cnt, !inside && i === start, withEma);
1934
+ if (one) { l0 = LS[0]; l1 = LS[1]; }
1935
+ else {
1936
+ c0 += quickBits(0, cnt << 1, LS[0], true, false) + 1;
1937
+ c1 += quickBits(32, cnt << 1, LS[1], true, false) + 1;
1938
+ if (withEma) c4 += quickBits(64, cnt << 1, LS[4], true, false) + 1;
1939
+ }
1940
+ }
1941
+ }
1942
+ let coord = 0, best = 0;
1943
+ if (one) {
1944
+ READY = 1; coord = l1 < l0 ? 1 : 0;
1945
+ if ((coord ? l0 - l1 : l1 - l0) > CLEAR * ((coord ? l1 : l0) + 3 * pts)) {
1946
+ if (speed || (!hasClosed4 && pts + 1 < SLOPE_FROM)) return coord;
1947
+ best = 2 + quickBits(coord << 5, pts << 1, coord ? l1 : l0, true, false) + 1;
1948
+ } else {
1949
+ c0 = 2 + quickBits(0, pts << 1, l0, true, false) + 1; c1 = 2 + quickBits(32, pts << 1, l1, true, false) + 1;
1950
+ coord = c1 < c0 ? 1 : 0; best = coord ? c1 : c0;
1951
+ }
1952
+ } else { best = c0; coord = 0; if (c1 < best) { best = c1; coord = 1; } if (withEma && c4 < best * (1 - EMA_MIN)) { best = c4; coord = 4; } }
1953
+ if (speed) return coord;
1954
+
1955
+ let bestSplit = 1e9, splitMode = -1;
1956
+ if (hasClosed4) {
1957
+ const budget = 32;
1958
+ let co = 0, ok1 = EK1, ok2 = EK2, checked = 0;
1959
+ for (let t = first; t < last && checked < budget; t++) {
1960
+ if (PM[t] <= 1) continue;
1961
+ co += orthoPath(PS[t], PM[t], PC[t], prec, ok1, ok2, mf, false, true, budget - checked);
1962
+ ok1 = RK1; ok2 = RK2; checked += OR_STEPS;
1963
+ if (checked >= 16 && co * pts > best * checked) break;
1964
+ }
1965
+ if (checked > 0) { const sc = 3 + co * (pts / checked); if (sc < bestSplit) { bestSplit = sc; splitMode = 3; } }
1966
+ }
1967
+
1968
+ if (pts + 1 >= SLOPE_FROM) {
1969
+ HM = HL = HR = 0;
1970
+ for (let w = 0; w < nw; w += 3) slopeHope(win[w], win[w + 1], win[w + 2]);
1971
+ if ((HL < HM ? HL : HM) + HR + 3 * pts < best * (1 - (one ? HOPE1 : HOPE))) {
1972
+ let c2 = 2, c5 = 4;
1973
+ for (let w = 0; w < nw; w += 3) {
1974
+ const start = win[w], end = win[w + 1], inside = win[w + 2];
1975
+ ST[4] = -1; ST[5] = ST[6] = ST[7] = 0;
1976
+ if (inside) slopeValues(start - 1, 1, false);
1977
+ for (let i = start; i < end; i += BLOCK) {
1978
+ const cnt = end - i < BLOCK ? end - i : BLOCK, firstBlk = !inside && i === start;
1979
+ slopeValues(i, cnt, firstBlk);
1980
+ if (firstBlk) {
1981
+ const z0a = Z[96], z0b = Z[128], l0ab = 64 - Math.clz32(z0a) - Math.clz32(z0b);
1982
+ Z[144] = z0a; Z[145] = z0b; for (let j = 1; j < cnt; j++) Z[145 + j] = Z[96 + j];
1983
+ c2 += quickBits(144, cnt + 1, LS[2] + l0ab, false, false) + 1;
1984
+ for (let j = 1; j < cnt; j++) Z[145 + j] = Z[112 + j];
1985
+ c5 += quickBits(144, cnt + 1, LS[5] + l0ab, false, false) + 1;
1986
+ if (cnt > 1) { const cm = quickBits(129, cnt - 1, LS[3], false, true) + 1; c2 += cm; c5 += cm; }
1987
+ } else {
1988
+ const cm = quickBits(128, cnt, LS[3], false, true) + 2;
1989
+ c2 += quickBits(96, cnt, LS[2], false, false) + cm; c5 += quickBits(112, cnt, LS[5], false, false) + cm;
1990
+ }
1991
+ }
1992
+ }
1993
+ if (c2 < bestSplit) { bestSplit = c2; splitMode = 2; }
1994
+ if (c5 < bestSplit) { bestSplit = c5; splitMode = 5; }
1995
+ }
1996
+ }
1997
+ const thresh = splitMode === 3 ? ORTHO_MIN : SLOPE_MIN;
1998
+ return splitMode >= 0 && bestSplit < best * (1 - thresh) ? splitMode : coord;
1999
+ }
2000
+
2001
+ // ---------------------------------------------------------------- encoder: path/geometry emission
2002
+
2003
+ function growQ(n) { const a = new Int32Array(n + (n >> 1) + 64), b = new Int32Array(a.length); a.set(QA.subarray(0, QN)); b.set(QB.subarray(0, QN)); QA = a; QB = b; }
2004
+ function addEntry(at, n, kind) {
2005
+ if (PN === PS.length) { const s = new Int32Array(PN * 2), m = new Int32Array(PN * 2), c = new Int32Array(PN * 2); s.set(PS); m.set(PM); c.set(PC); PS = s; PM = m; PC = c; }
2006
+ const closed = kind !== 2 && n > 1 && QA[at] === QA[at + n - 1] && QB[at] === QB[at + n - 1] ? 1 : 0;
2007
+ if (kind === 1 && !closed) EOPEN = 1;
2008
+ PS[PN] = at; PM[PN] = n - closed; PC[PN++] = closed;
2009
+ }
2010
+ function addPath(points, kind, la, lo, two, f) {
2011
+ if (!Array.isArray(points)) return false;
2012
+ const n = points.length, at = QN; if (at + n > QA.length) growQ(at + n);
2013
+ const qa = QA, qb = QB; let pt, x = 0, y = 0;
2014
+ try {
2015
+ for (let i = 0; i < n; i++) {
2016
+ pt = points[i]; x = pt[la]; y = pt[lo];
2017
+ if (typeof x !== 'number' || typeof y !== 'number' || (two && pt.length !== 2) || !(Math.abs(x *= f) < I32H && Math.abs(y *= f) < I32H)) return false;
2018
+ qa[at + i] = (x < 0 ? x - H : x + H) | 0; qb[at + i] = (y < 0 ? y - H : y + H) | 0;
2019
+ }
2020
+ } catch { return false; }
2021
+ QN = at + n; addEntry(at, n, kind); return true;
2022
+ }
2023
+ function collectGeo(g, depth, f) {
2024
+ if (g == null) return false;
2025
+ const c = g.coordinates;
2026
+ switch (g.type) {
2027
+ case 'Point': return Array.isArray(c) && (c.length === 0 || addPath([c], 2, 1, 0, true, f));
2028
+ case 'LineString': return addPath(c, 0, 1, 0, true, f);
2029
+ case 'MultiPoint': return addPath(c, 2, 1, 0, true, f);
2030
+ case 'MultiLineString': if (!Array.isArray(c)) return false; for (let i = 0; i < c.length; i++) if (!addPath(c[i], 0, 1, 0, true, f)) return false; return true;
2031
+ case 'Polygon': if (!Array.isArray(c)) return false; for (let i = 0; i < c.length; i++) if (!addPath(c[i], 1, 1, 0, true, f)) return false; return true;
2032
+ case 'MultiPolygon': if (!Array.isArray(c)) return false; for (let i = 0; i < c.length; i++) { const r = c[i]; if (!Array.isArray(r)) return false; for (let j = 0; j < r.length; j++) if (!addPath(r[j], 1, 1, 0, true, f)) return false; } return true;
2033
+ case 'GeometryCollection': { const l = g.geometries; if (depth >= MAX_DEPTH || !Array.isArray(l)) return false; for (let i = 0; i < l.length; i++) if (!collectGeo(l[i], depth + 1, f)) return false; return true; }
2034
+ }
2035
+ return false;
2036
+ }
2037
+
2038
+ function putPathBlocks(at, m, closed, mode, prec, mf) {
2039
+ const end = at + m, z = Z;
2040
+ if (mode === 3) { orthoPath(at, m, closed, prec, EK1, EK2, mf, true, false, 1e9); EK1 = RK1; EK2 = RK2; READY = 0; return; }
2041
+ if (mode === 0 || mode === 1 || mode === 4) {
2042
+ const o1 = mode === 1 ? 32 : mode === 4 ? 64 : 0; let k1 = EK1;
2043
+ if (!READY) ST[0] = ST[1] = 0;
2044
+ for (let i = at + 1; i < end; i += BLOCK) {
2045
+ const cnt = end - i < BLOCK ? end - i : BLOCK;
2046
+ if (WO + 400 > OUT.length) room(400);
2047
+ if (!READY) pairedOne(o1, mode, i, cnt, i === at + 1);
2048
+ READY = 0;
2049
+ const k = chooseK(o1, cnt << 1, k1, LS[mode], true, false, mf);
2050
+ putParam(k, k1, 5); k1 = k < 0 ? 0 : k; putBlockFast(o1, o1 + cnt, cnt, k, k);
2051
+ }
2052
+ EK1 = k1; return;
2053
+ }
2054
+ const isLin = mode === 5, z1Off = isLin ? 112 : 96, stIdx = isLin ? 5 : 2;
2055
+ ST[4] = -1; ST[5] = ST[6] = ST[7] = 0; READY = 0;
2056
+ let k1 = EK1, k2 = EK2;
2057
+ for (let i = at + 1; i < end; i += BLOCK) {
2058
+ const cnt = end - i < BLOCK ? end - i : BLOCK, firstBlk = i === at + 1;
2059
+ if (WO + 400 > OUT.length) room(400);
2060
+ slopeValues(i, cnt, firstBlk);
2061
+ if (firstBlk) {
2062
+ const z0a = z[96], z0b = z[128], l0ab = 64 - Math.clz32(z0a) - Math.clz32(z0b);
2063
+ z[144] = z0a; z[145] = z0b; for (let j = 1; j < cnt; j++) z[145 + j] = z[z1Off + j];
2064
+ const rk1 = chooseK(144, cnt + 1, k1, LS[stIdx] + l0ab, false, false, mf);
2065
+ putParam(rk1, k1, 5); k1 = rk1;
2066
+ let rk2 = k2;
2067
+ if (cnt > 1) { rk2 = chooseK(129, cnt - 1, k2, LS[3], false, true, mf); putParam(rk2, k2, 5); k2 = rk2 < 0 ? 0 : rk2; }
2068
+ putPairFast(z0a, z0b, rk1, rk1);
2069
+ if (cnt > 1) putBlockFast(z1Off + 1, 129, cnt - 1, rk1, rk2);
2070
+ } else {
2071
+ const rk1 = chooseK(z1Off, cnt, k1, LS[stIdx], false, false, mf), rk2 = chooseK(128, cnt, k2, LS[3], false, true, mf);
2072
+ putParam(rk1, k1, 5); putParam(rk2, k2, 5); k1 = rk1; k2 = rk2 < 0 ? 0 : rk2;
2073
+ putBlockFast(z1Off, 128, cnt, rk1, rk2);
2074
+ }
2075
+ }
2076
+ EK1 = k1; EK2 = k2;
2077
+ }
2078
+ function putPositionFast(lat, lng, prec) {
2079
+ room(32);
2080
+ if (ESTART) {
2081
+ const a = (((lat - ELAT) << 1) ^ ((lat - ELAT) >> 31)) >>> 0, b = (((lng - ELNG) << 1) ^ ((lng - ELNG) >> 31)) >>> 0, L = 32 - Math.clz32(a | b);
2082
+ if (L === 0) return put(1, 0);
2083
+ put(6, 31 + L); putBig(L, a); putBig(L, b);
2084
+ } else { ESTART = 1; putTruncAnchor(lat, lng, prec); }
2085
+ }
2086
+ function putPathGeo(t, kind, mode, prec, mf) {
2087
+ const m = PM[t], at = PS[t]; room(64);
2088
+ if (kind === 0 || (kind === 1 && EOPEN)) put(1, PC[t]);
2089
+ putCount(m); if (m === 0) return;
2090
+ putPositionFast(QA[at], QB[at], prec);
2091
+ if (m > 1) putPathBlocks(at, m, PC[t], mode, prec, mf);
2092
+ ELAT = QA[at + m - 1]; ELNG = QB[at + m - 1];
2093
+ }
2094
+ function putRingsGeo(n, mode, prec, mf) {
2095
+ room(16); if (n === 1) put(1, 1); else { put(1, 0); putCount(n); }
2096
+ for (let i = 0; i < n; i++) putPathGeo(PI++, 1, mode, prec, mf);
2097
+ }
2098
+ function emitGeo(g, prec, favor) {
2099
+ const c = g.coordinates, mf = favor === 'speed' ? 1 : 0; room(32);
2100
+ switch (g.type) {
2101
+ case 'Point': if (c.length === 0) put(1, 0); else { const at = PS[PI++]; put(1, 1); putPositionFast(QA[at], QB[at], prec); ELAT = QA[at]; ELNG = QB[at]; } return;
2102
+ case 'GeometryCollection': { const l = g.geometries; putCount(l.length); for (let i = 0; i < l.length; i++) { room(8); put(3, TYPES.indexOf(l[i].type)); emitGeo(l[i], prec, favor); } return; }
2103
+ case 'LineString': case 'MultiPoint': { const md = PM[PI] > 1 ? pickModeGeo(PI, PI + 1, prec, favor) : 0; put(MODE_LEN[md], MODE_VAL[md]); putPathGeo(PI++, g.type === 'LineString' ? 0 : 2, md, prec, mf); return; }
2104
+ case 'MultiLineString': { const md = pickModeGeo(PI, PI + c.length, prec, favor); put(MODE_LEN[md], MODE_VAL[md]); putCount(c.length); for (let i = 0; i < c.length; i++) putPathGeo(PI++, 0, md, prec, mf); return; }
2105
+ case 'Polygon': { const md = pickModeGeo(PI, PI + c.length, prec, favor); put(MODE_LEN[md], MODE_VAL[md]); putRingsGeo(c.length, md, prec, mf); return; }
2106
+ default: { let rings = 0; for (let i = 0; i < c.length; i++) rings += c[i].length; const md = pickModeGeo(PI, PI + rings, prec, favor); put(MODE_LEN[md], MODE_VAL[md]); putCount(c.length); for (let i = 0; i < c.length; i++) putRingsGeo(c[i].length, md, prec, mf); }
2107
+ }
2108
+ }
2109
+
2110
+ function shortEncode(points, ring, la, lo, two, prec, favor) {
2111
+ const n = points.length, f = POW[prec], z = Z, qa = QA, qb = QB, speed = favor === 'speed', mf = speed ? 1 : 0;
2112
+ let pt, x = 0, y = 0, lat = 0, lng = 0, m = n, cnt = 0, mode = 0, l0 = 0, l1 = 0, hm = 0, hl = 0, hr = 0, slope = false;
2113
+ try {
2114
+ pt = points[0]; x = pt[la]; y = pt[lo];
2115
+ if (typeof x !== 'number' || typeof y !== 'number' || (two && pt.length !== 2) || !(Math.abs(x *= f) < I32H && Math.abs(y *= f) < I32H)) return null;
2116
+ lat = (x < 0 ? x - H : x + H) | 0; lng = (y < 0 ? y - H : y + H) | 0;
2117
+ if (!(lat >= -90 * f && lat <= 90 * f && lng >= -180 * f && lng <= 180 * f)) return null;
2118
+ if (n > 1) {
2119
+ pt = points[n - 1]; x = pt[la]; y = pt[lo];
2120
+ if (typeof x !== 'number' || typeof y !== 'number' || (two && pt.length !== 2) || !(Math.abs(x *= f) < I32H && Math.abs(y *= f) < I32H)) return null;
2121
+ if (((x < 0 ? x - H : x + H) | 0) === lat && ((y < 0 ? y - H : y + H) | 0) === lng) m = n - 1;
2122
+ }
2123
+ if (ring && m === n) return null;
2124
+ if (m > 17) return null;
2125
+ cnt = m - 1; slope = !speed && !ring && m >= SLOPE_FROM && !(m <= 5 && m < n);
2126
+ let pa = lat, pb = lng, va = 0, vb = 0, a = 0, b = 0, da = 0, db = 0, r = 0, t = 0;
2127
+ qa[0] = lat; qb[0] = lng;
2128
+ for (let i = 1, j = 0; i < m; i++, j++) {
2129
+ pt = points[i]; x = pt[la]; y = pt[lo];
2130
+ if (typeof x !== 'number' || typeof y !== 'number' || (two && pt.length !== 2) || !(Math.abs(x *= f) < I32H && Math.abs(y *= f) < I32H)) return null;
2131
+ a = (x < 0 ? x - H : x + H) | 0; b = (y < 0 ? y - H : y + H) | 0;
2132
+ qa[i] = a; qb[i] = b; da = (a - pa) | 0; db = (b - pb) | 0; pa = a; pb = b;
2133
+ a = (da << 1) ^ (da >> 31); b = (db << 1) ^ (db >> 31);
2134
+ z[j] = a; z[cnt + j] = b; l0 += 64 - Math.clz32(a) - Math.clz32(b);
2135
+ a = ((da - va) << 1) ^ ((da - va) >> 31); b = ((db - vb) << 1) ^ ((db - vb) >> 31);
2136
+ z[32 + j] = a; z[32 + cnt + j] = b; l1 += 64 - Math.clz32(a) - Math.clz32(b);
2137
+ if (slope) {
2138
+ a = da < 0 ? -da : da; b = db < 0 ? -db : db; if (b > a) { t = a; a = b; b = t; }
2139
+ if (a > 0) hm += 33 - Math.clz32(a);
2140
+ r = va < 0 ? -va : va; t = vb < 0 ? -vb : vb; if (t > r) r = t;
2141
+ if (a !== r) hl += 33 - Math.clz32(a > r ? a - r : r - a);
2142
+ if (r > 0) { x = Math.abs(da * vb - db * va); if (x > 0) { t = (x < 4294967296 ? 33 - Math.clz32(x) : 65 - Math.clz32(x / 4294967296)) - (32 - Math.clz32(r)); hr += t < 1 ? 1 : t; } }
2143
+ else if (b > 0) hr += 33 - Math.clz32(b);
2144
+ }
2145
+ va = da; vb = db;
2146
+ }
2147
+ } catch { return null; }
2148
+
2149
+ const closed = n - m;
2150
+ let k1 = KI[prec], k2 = 0, best = 0, orthoReady = false, knownK = -9, sk2 = 0, sk5 = 0, skm = 0;
2151
+ if (cnt > 1) {
2152
+ mode = l1 < l0 ? 1 : 0;
2153
+ const needOrtho = !speed && closed === 1 && m >= 4 && (m === 4 || orthoHope(0, m, prec));
2154
+ if ((mode ? l0 - l1 : l1 - l0) > CLEAR * ((mode ? l1 : l0) + 3 * cnt)) {
2155
+ if (slope || needOrtho) { best = 2 + quickBits(mode << 5, cnt << 1, mode ? l1 : l0, true, false) + 1; knownK = QB_K; }
2156
+ } else {
2157
+ const c0 = 2 + quickBits(0, cnt << 1, l0, true, false) + 1, k0q = QB_K;
2158
+ const c1 = 2 + quickBits(32, cnt << 1, l1, true, false) + 1, k1q = QB_K;
2159
+ if (c1 < c0) { mode = 1; best = c1; knownK = k1q; } else { mode = 0; best = c0; knownK = k0q; }
2160
+ }
2161
+ let bestSplit = 1e9, sm = -1;
2162
+ if (needOrtho) {
2163
+ const co = 3 + orthoPath(0, m, 1, prec, k1, 0, 1, false, false, 1e9);
2164
+ if (co < bestSplit) { bestSplit = co; sm = 3; orthoReady = true; }
2165
+ }
2166
+ if (slope && !(sm === 3 && bestSplit < best * 0.94) && (hl < hm ? hl : hm) + hr + 3 * cnt < best * (1 - HOPE1)) {
2167
+ ST[4] = -1; ST[5] = ST[6] = ST[7] = 0;
2168
+ slopeValues(1, cnt, true);
2169
+ const z0a = z[96], z0b = z[128], l0ab = 64 - Math.clz32(z0a) - Math.clz32(z0b);
2170
+ z[144] = z0a; z[145] = z0b; for (let j = 1; j < cnt; j++) z[145 + j] = z[96 + j];
2171
+ const cm = quickBits(129, cnt - 1, LS[3], false, true) + 1; skm = QB_K;
2172
+ const c2 = 2 + quickBits(144, cnt + 1, LS[2] + l0ab, false, false) + 1 + cm; sk2 = QB_K;
2173
+ for (let j = 1; j < cnt; j++) z[145 + j] = z[112 + j];
2174
+ const c5 = 4 + quickBits(144, cnt + 1, LS[5] + l0ab, false, false) + 1 + cm; sk5 = QB_K;
2175
+ orthoReady = false;
2176
+ if (c2 < bestSplit) { bestSplit = c2; sm = 2; }
2177
+ if (c5 < bestSplit) { bestSplit = c5; sm = 5; }
2178
+ }
2179
+ if (sm >= 0 && bestSplit < best * (1 - (sm === 3 ? ORTHO_MIN : SLOPE_MIN))) mode = sm;
2180
+ }
2181
+
2182
+ if (prec < 7) {
2183
+ WO = WACC = WN = 0;
2184
+ const ml = MODE_LEN[mode], cl = m < 8 ? 4 : 6;
2185
+ put(5 + ml + cl, ((((((((ring ? 2 : 1) << 1) | (prec - 5)) << ml) | MODE_VAL[mode]) << 1) | (ring ? 1 : closed)) << cl) | (m + 8));
2186
+ } else {
2187
+ putHeader(ring ? 2 : 1, prec, 0, 0, 0, 0, 0);
2188
+ put(MODE_LEN[mode], MODE_VAL[mode]);
2189
+ put(1, ring ? 1 : closed);
2190
+ putCount(m);
2191
+ }
2192
+ putTruncAnchor(lat, lng, prec);
2193
+ if (cnt > 0) {
2194
+ if (mode < 2) {
2195
+ const o1 = mode << 5, ml = mode ? l1 : l0;
2196
+ const k = knownK >= 0 && ml > 0 ? knownK : chooseK(o1, cnt << 1, k1, ml, true, false, mf);
2197
+ putParam(k, k1, 5); putBlockFast(o1, o1 + cnt, cnt, k, k);
2198
+ } else if (mode === 3) {
2199
+ if (orthoReady) {
2200
+ if (OR_N1 > 0) putParam(OR_K1, k1, 5); if (OR_N2 > 0) putParam(OR_K2, 0, 5);
2201
+ emitOrthoBlock(cnt, 0, cnt, true, OR_K1, OR_K2);
2202
+ } else orthoPath(0, m, 1, prec, k1, 0, 1, true, false, 1e9);
2203
+ } else {
2204
+ const isLin = mode === 5, z1Off = isLin ? 112 : 96;
2205
+ const rk1 = isLin ? sk5 : sk2, rk2 = LS[3] === 0 ? -1 : skm;
2206
+ putParam(rk1, k1, 5); putParam(rk2, k2, 5);
2207
+ putPairFast(z[96], z[128], rk1, rk1); putBlockFast(z1Off + 1, 129, cnt - 1, rk1, rk2);
2208
+ }
2209
+ }
2210
+ return toText();
2211
+ }
2212
+
2213
+ // Fused 1-pass streaming line encoder for lines > 161 points (ZERO QA/QB allocation/pass!)
2214
+ function streamEncodeLine(points, la, lo, prec, favor) {
2215
+ const n = points.length, f = POW[prec], speed = favor === 'speed', mf = speed ? 1 : 0;
2216
+ let pt, x = 0, y = 0, lat0 = 0, lng0 = 0, m = n;
2217
+ try {
2218
+ pt = points[0]; x = pt[la]; y = pt[lo];
2219
+ if (typeof x !== 'number' || typeof y !== 'number' || pt.length !== 2 || !(Math.abs(x *= f) < I32H && Math.abs(y *= f) < I32H)) return null;
2220
+ lat0 = (x < 0 ? x - H : x + H) | 0; lng0 = (y < 0 ? y - H : y + H) | 0;
2221
+ if (!(lat0 >= -90 * f && lat0 <= 90 * f && lng0 >= -180 * f && lng0 <= 180 * f)) return null;
2222
+ pt = points[n - 1]; x = pt[la]; y = pt[lo];
2223
+ if (typeof x !== 'number' || typeof y !== 'number' || pt.length !== 2 || !(Math.abs(x *= f) < I32H && Math.abs(y *= f) < I32H)) return null;
2224
+ if (((x < 0 ? x - H : x + H) | 0) === lat0 && ((y < 0 ? y - H : y + H) | 0) === lng0) m = n - 1;
2225
+ } catch { return null; }
2226
+ if (m <= 64) return null;
2227
+
2228
+ // Sample 4..10 windows of 16 steps directly from points[] into QA[0..199], QB[0..199]
2229
+ const total = m - 1, windows = Math.max(4, Math.min(10, total >> 6)), qa = QA, qb = QB, z = Z, inv = INV;
2230
+ let c0 = 2, c1 = 2, c4 = 4, nw = 0, pts = 0, q = 0;
2231
+ const win = WIN;
2232
+ try {
2233
+ for (let w = 0; w < windows; w++) {
2234
+ const g = Math.floor(((total - BLOCK) * w) / (windows - 1));
2235
+ const at = 1 + g, stop = at + BLOCK, inside = at > 1 ? 1 : 0;
2236
+ const from = inside ? at - 2 : 0;
2237
+ const qStart = q;
2238
+ for (let i = from; i < stop; i++) {
2239
+ pt = points[i]; x = pt[la]; y = pt[lo];
2240
+ if (typeof x !== 'number' || typeof y !== 'number' || pt.length !== 2 || !(Math.abs(x *= f) < I32H && Math.abs(y *= f) < I32H)) return null;
2241
+ qa[q] = (x < 0 ? x - H : x + H) | 0; qb[q++] = (y < 0 ? y - H : y + H) | 0;
2242
+ }
2243
+ const wStart = qStart + (inside ? 2 : 1);
2244
+ win[nw++] = wStart; win[nw++] = q; win[nw++] = inside; pts += BLOCK;
2245
+ ST[0] = ST[1] = ST[2] = ST[3] = 0;
2246
+ if (inside) pairedValues(wStart - 1, 1, false, !speed);
2247
+ pairedValues(wStart, BLOCK, !inside, !speed);
2248
+ c0 += quickBits(0, 32, LS[0], true, false) + 1;
2249
+ c1 += quickBits(32, 32, LS[1], true, false) + 1;
2250
+ if (!speed) c4 += quickBits(64, 32, LS[4], true, false) + 1;
2251
+ }
2252
+ } catch { return null; }
2253
+
2254
+ let mode = 0, best = c0;
2255
+ if (c1 < best) { best = c1; mode = 1; }
2256
+ if (!speed && c4 < best * (1 - EMA_MIN)) { best = c4; mode = 4; }
2257
+ if (!speed) {
2258
+ HM = HL = HR = 0;
2259
+ for (let w = 0; w < nw; w += 3) slopeHope(win[w], win[w + 1], win[w + 2]);
2260
+ if ((HL < HM ? HL : HM) + HR + 3 * pts < best * (1 - HOPE)) {
2261
+ let c2 = 2, c5 = 4;
2262
+ for (let w = 0; w < nw; w += 3) {
2263
+ const start = win[w], inside = win[w + 2];
2264
+ ST[4] = -1; ST[5] = ST[6] = ST[7] = 0;
2265
+ if (inside) slopeValues(start - 1, 1, false);
2266
+ slopeValues(start, BLOCK, !inside);
2267
+ if (!inside) {
2268
+ const z0a = z[96], z0b = z[128], l0ab = 64 - Math.clz32(z0a) - Math.clz32(z0b);
2269
+ z[144] = z0a; z[145] = z0b; for (let j = 1; j < BLOCK; j++) z[145 + j] = z[96 + j];
2270
+ const cm = quickBits(129, 15, LS[3], false, true) + 1;
2271
+ c2 += quickBits(144, 17, LS[2] + l0ab, false, false) + 1 + cm;
2272
+ for (let j = 1; j < BLOCK; j++) z[145 + j] = z[112 + j];
2273
+ c5 += quickBits(144, 17, LS[5] + l0ab, false, false) + 1 + cm;
2274
+ } else {
2275
+ const cm = quickBits(128, BLOCK, LS[3], false, true) + 2;
2276
+ c2 += quickBits(96, BLOCK, LS[2], false, false) + cm;
2277
+ c5 += quickBits(112, BLOCK, LS[5], false, false) + cm;
2278
+ }
2279
+ }
2280
+ const sm = c5 < c2 ? 5 : 2, sb = c5 < c2 ? c5 : c2;
2281
+ if (sb < best * (1 - SLOPE_MIN)) mode = sm;
2282
+ }
2283
+ }
2284
+
2285
+ // Emit header, mode, closed, count, anchor, then stream ALL points in 1 fused pass!
2286
+ putHeader(1, prec, 0, 0, 0, 0, 0);
2287
+ put(MODE_LEN[mode], MODE_VAL[mode]);
2288
+ put(1, n - m);
2289
+ putCount(m);
2290
+ putTruncAnchor(lat0, lng0, prec);
2291
+
2292
+ let pa = lat0, pb = lng0, va = 0, vb = 0, k1 = KI[prec], k2 = 0;
2293
+ try {
2294
+ if (mode === 0 || mode === 1 || mode === 4) {
2295
+ const lin = mode === 1 ? -1 : 0, isEma = mode === 4;
2296
+ for (let i = 1; i < m; i += BLOCK) {
2297
+ const cnt = m - i < BLOCK ? m - i : BLOCK;
2298
+ if (WO + 400 > OUT.length) room(400);
2299
+ let len = 0;
2300
+ if (isEma) {
2301
+ for (let j = 0, idx = i; j < cnt; j++, idx++) {
2302
+ pt = points[idx]; x = pt[la]; y = pt[lo];
2303
+ if (typeof x !== 'number' || typeof y !== 'number' || pt.length !== 2 || !(Math.abs(x *= f) < I32H && Math.abs(y *= f) < I32H)) return null;
2304
+ const a = (x < 0 ? x - H : x + H) | 0, b = (y < 0 ? y - H : y + H) | 0;
2305
+ const da = (a - pa) | 0, db = (b - pb) | 0; pa = a; pb = b;
2306
+ let ra = (da - va) | 0, rb = (db - vb) | 0;
2307
+ if (idx === 1) { va = da; vb = db; } else { va = (va + da) >> 1; vb = (vb + db) >> 1; }
2308
+ ra = (ra << 1) ^ (ra >> 31); rb = (rb << 1) ^ (rb >> 31);
2309
+ z[j] = ra; z[cnt + j] = rb; len += 64 - Math.clz32(ra) - Math.clz32(rb);
2310
+ }
2311
+ } else {
2312
+ for (let j = 0, idx = i; j < cnt; j++, idx++) {
2313
+ pt = points[idx]; x = pt[la]; y = pt[lo];
2314
+ if (typeof x !== 'number' || typeof y !== 'number' || pt.length !== 2 || !(Math.abs(x *= f) < I32H && Math.abs(y *= f) < I32H)) return null;
2315
+ const a = (x < 0 ? x - H : x + H) | 0, b = (y < 0 ? y - H : y + H) | 0;
2316
+ const da = (a - pa) | 0, db = (b - pb) | 0; pa = a; pb = b;
2317
+ let ra = (da - (va & lin)) | 0, rb = (db - (vb & lin)) | 0; va = da; vb = db;
2318
+ ra = (ra << 1) ^ (ra >> 31); rb = (rb << 1) ^ (rb >> 31);
2319
+ z[j] = ra; z[cnt + j] = rb; len += 64 - Math.clz32(ra) - Math.clz32(rb);
2320
+ }
2321
+ }
2322
+ const rk = chooseK(0, cnt << 1, k1, len, true, false, mf);
2323
+ putParam(rk, k1, 5); k1 = rk < 0 ? 0 : rk; putBlockFast(0, cnt, cnt, rk, rk);
2324
+ }
2325
+ } else {
2326
+ const isLin = mode === 5;
2327
+ let sw = -1, s = 0, g = 0, pM = 0;
2328
+ for (let i = 1; i < m; i += BLOCK) {
2329
+ const cnt = m - i < BLOCK ? m - i : BLOCK, firstBlk = i === 1;
2330
+ if (WO + 400 > OUT.length) room(400);
2331
+ let l1 = 0, l2 = 0, z0a = 0, z0b = 0;
2332
+ for (let j = 0, idx = i; j < cnt; j++, idx++) {
2333
+ pt = points[idx]; x = pt[la]; y = pt[lo];
2334
+ if (typeof x !== 'number' || typeof y !== 'number' || pt.length !== 2 || !(Math.abs(x *= f) < I32H && Math.abs(y *= f) < I32H)) return null;
2335
+ const a = (x < 0 ? x - H : x + H) | 0, b = (y < 0 ? y - H : y + H) | 0;
2336
+ const da = (a - pa) | 0, db = (b - pb) | 0; pa = a; pb = b;
2337
+ if (idx === 1) {
2338
+ z0a = (da << 1) ^ (da >> 31); z0b = (db << 1) ^ (db >> 31);
2339
+ z[144] = z0a; z[145] = z0b; l1 += 64 - Math.clz32(z0a) - Math.clz32(z0b);
2340
+ sw = (db < 0 ? -db : db) >= (da < 0 ? -da : da) ? -1 : 0;
2341
+ const dM = sw ? db : da, dm = sw ? da : db;
2342
+ s = divSlope(dm, dM); pM = dM; g = dM !== 0 ? (dM >> 31) : 0;
2343
+ continue;
2344
+ }
2345
+ const dM = sw ? db : da, dm = sw ? da : db, ev = (dm - ((Math.imul(dM, s) + 2048) >> 12)) | 0, ez = (ev << 1) ^ (ev >> 31);
2346
+ z[128 + j] = ez; l2 += 32 - Math.clz32(ez);
2347
+ let mz;
2348
+ if (isLin) { const d = (dM - pM) | 0; mz = (d << 1) ^ (d >> 31); }
2349
+ else { const u = ((dM ^ g) - g) | 0; if (u < -536870912) return null; mz = u < 0 ? (~u) * 8 + 7 : u + ((u * SEVENTH) | 0); }
2350
+ if (firstBlk) z[145 + j] = mz; else z[96 + j] = mz;
2351
+ l1 += 32 - Math.clz32(mz);
2352
+ pM = dM; const adm = dm < 0 ? -dm : dm, adM = dM < 0 ? -dM : dM;
2353
+ if (adm > adM) {
2354
+ sw = ~sw;
2355
+ if (adm < 512) { const qv = Math.imul(dM, inv[adm]) >> 12, sg = dm >> 31; s = ((qv ^ sg) - sg) | 0; }
2356
+ else s = ((dM << 12) / dm) | 0;
2357
+ pM = dm;
2358
+ } else {
2359
+ let ds;
2360
+ if (adM < 512) { const qv = Math.imul(ev, inv[adM]) >> 12, sg = dM >> 31; ds = ((qv ^ sg) - sg) | 0; if (adM < 4) ds >>= 1; }
2361
+ else ds = ((ev << 12) / dM) | 0;
2362
+ s = (s + ds) | 0;
2363
+ }
2364
+ if (pM !== 0) g = pM >> 31;
2365
+ }
2366
+ if (firstBlk) {
2367
+ const rk1 = chooseK(144, cnt + 1, k1, l1, false, false, mf);
2368
+ const rk2 = chooseK(129, cnt - 1, k2, l2, false, true, mf);
2369
+ putParam(rk1, k1, 5); putParam(rk2, k2, 5); k1 = rk1; k2 = rk2 < 0 ? 0 : rk2;
2370
+ putPairFast(z0a, z0b, rk1, rk1);
2371
+ putBlockFast(146, 129, cnt - 1, rk1, rk2);
2372
+ } else {
2373
+ const rk1 = chooseK(96, cnt, k1, l1, false, false, mf);
2374
+ const rk2 = chooseK(128, cnt, k2, l2, false, true, mf);
2375
+ putParam(rk1, k1, 5); putParam(rk2, k2, 5); k1 = rk1; k2 = rk2 < 0 ? 0 : rk2;
2376
+ putBlockFast(96, 128, cnt, rk1, rk2);
2377
+ }
2378
+ }
2379
+ }
2380
+ } catch { return null; }
2381
+ return toText();
2382
+ }
2383
+
2384
+ // The default and favor: 'speed' encoders for a string that's ordinary and
2385
+ // 2D. Null when it isn't: the canonical encoder below writes it. check is 0,
2386
+ // 1 (2 characters) or 2 (6).
2387
+ function fastEncodeGeo(g, prec, check, favor, isLine, la, lo) {
2388
+ const f = POW[prec], mf = favor === 'speed' ? 1 : 0;
2389
+ QN = PN = PI = EOPEN = ESTART = READY = 0;
2390
+ if (!(isLine ? addPath(g, 0, la, lo, true, f) : collectGeo(g, 0, f))) return null;
2391
+ if (QN > 0 && !(QA[0] >= -90 * f && QA[0] <= 90 * f && QB[0] >= -180 * f && QB[0] <= 180 * f)) return null;
2392
+ putHeader(isLine ? 1 : TYPES.indexOf(g.type), prec, (EOPEN << 4) | check, 0, 0, 0, 0);
2393
+ EK1 = KI[prec]; EK2 = 0;
2394
+ if (isLine) { const md = PM[0] > 1 ? pickModeGeo(0, 1, prec, favor) : 0; put(MODE_LEN[md], MODE_VAL[md]); putPathGeo(0, 0, md, prec, mf); }
2395
+ else emitGeo(g, prec, favor);
2396
+ const s = toText(); if (QA.length > (KEEP >> 2)) { QA = new Int32Array(1024); QB = new Int32Array(1024); }
2397
+ return check ? addSum(s, check) : s;
2398
+ }
2399
+
2400
+ // ---------------------------------------------------------------- canonical encoder (SPEC.md section 16.4)
2401
+ //
2402
+ // The encoder the format defines, and what favor: 'size' means. It also
2403
+ // writes every string the fast paths above don't: wide ones and ones with Z
2404
+ // or M. For each geometry it costs every mode exactly, block by block with
2405
+ // the carried parameters, and for each block every parameter; a tie goes to
2406
+ // the lowest. Integers only, so every implementation writes the same string.
2407
+
2408
+ const bitLenBig = (x) => (x <= 0 ? 0 : x < 4294967296 ? 32 - Math.clz32(x) : 64 - Math.clz32(x / 4294967296));
2409
+ const zigBig = (v) => (v < 0 ? -2 * v - 1 : 2 * v);
2410
+ const costBig = (z, k) => { const q = k < 32 && z < 4294967296 ? z >>> k : Math.floor(z / P2[k]); return q < 6 ? q + 1 + k : 2 * (bitLenBig(q - 2) - 1) + 5 + k; };
2411
+ // The parameter of a block of n values after pk, and its bits in KBITS.
2412
+ // kind 0: a paired block (every z1, then every z2), which can be -2 (all
2413
+ // zero) or -1 (sparse). kind 1: k1. kind 2: k2 or an extra coordinate, which
2414
+ // can be -1 (all zero).
2415
+ //
2416
+ // The choice is the cheapest of every parameter, and the lowest of those that
2417
+ // cost the same. Not every one is costed, though. A value of bit length L
2418
+ // takes at least k + 1 bits at k, and at least 2L - k - 1, so with s the sum
2419
+ // of the bit lengths a block of n values takes more than n(k + 1) and more
2420
+ // than 2s - n(k + 1). Once one parameter near s / n is costed, those two
2421
+ // bounds rule out most of the others. This made favor: 'size' 2.3 times
2422
+ // faster on building polygons and 3.6 times on country outlines, for the
2423
+ // same strings.
2424
+ function bestKBig(v, n, pk, kMax, far, kind) {
2425
+ let best = Infinity, bk = 0, c = 0, dz = 0, s = 0;
2426
+ for (let i = 0; i < n; i++) s += bitLenBig(v[i]);
2427
+ if (s === 0) { if (kind !== 1) { bk = kind ? -1 : -2; dz = 2 * (pk - bk) - 1; best = dz < 8 ? dz + 1 : far; } }
2428
+ else if (kind === 0) {
2429
+ const cnt = n >> 1; dz = 2 * pk + 1; c = (dz < 8 ? dz + 1 : far) + cnt;
2430
+ for (let i = 0; i < cnt; i++) { const a = v[i], b = v[cnt + i]; if (a !== 0 || b !== 0) c += costBig(a, 0) + costBig(a === 0 ? b - 1 : b, 0); }
2431
+ best = c; bk = -1;
2432
+ }
2433
+ let kg = (s / n) | 0; if (kg > kMax) kg = kMax;
2434
+ dz = kg < pk ? 2 * (pk - kg) - 1 : 2 * (kg - pk); let cg = dz < 8 ? dz + 1 : far;
2435
+ for (let i = 0; i < n; i++) cg += costBig(v[i], kg);
2436
+ // kg or a parameter below it takes this over: nothing from here on is
2437
+ // accepted unless it costs less than best.
2438
+ if (cg < best) best = cg + 1;
2439
+ for (let k = 0; k <= kMax; k++) {
2440
+ const low = n * (k + 1);
2441
+ if (low + 1 >= best) break;
2442
+ if (2 * s - low + 1 >= best) continue;
2443
+ if (k === kg) c = cg;
2444
+ else {
2445
+ dz = k < pk ? 2 * (pk - k) - 1 : 2 * (k - pk); c = dz < 8 ? dz + 1 : far;
2446
+ for (let i = 0; i < n && c < best; i++) c += costBig(v[i], k);
2447
+ }
2448
+ if (c < best) { best = c; bk = k; }
2449
+ }
2450
+ KBITS = best; return bk;
2451
+ }
2452
+ function putSized(z) { const n = bitLenBig(z); put(6, n); if (n) putBig(n, z); }
2453
+ // Rice-T6 (SPEC.md section 8): one value, or a pair with its two codes, two
2454
+ // remainders and two tails in that order.
2455
+ function putRice(z1, z2, k1, k2, pair) {
2456
+ room(48);
2457
+ const q1 = k1 < 32 && z1 < 4294967296 ? z1 >>> k1 : Math.floor(z1 / P2[k1]), n1 = q1 >= 6 ? bitLenBig(q1 - 2) - 1 : 0;
2458
+ let q2 = 0, n2 = 0;
2459
+ putUnary(q1 < 6 ? q1 : n1 + 4);
2460
+ if (pair) { q2 = k2 < 32 && z2 < 4294967296 ? z2 >>> k2 : Math.floor(z2 / P2[k2]); n2 = q2 >= 6 ? bitLenBig(q2 - 2) - 1 : 0; putUnary(q2 < 6 ? q2 : n2 + 4); }
2461
+ if (k1) putBig(k1, z1 - q1 * P2[k1]);
2462
+ if (pair && k2) putBig(k2, z2 - q2 * P2[k2]);
2463
+ if (n1) putBig(n1, q1 - 2 - P2[n1]);
2464
+ if (n2) putBig(n2, q2 - 2 - P2[n2]);
2465
+ }
2466
+
2467
+ // Every position of the geometry as integers, in the order they're written,
2468
+ // and the values of one path under one mode.
2469
+ let CA = /* @__PURE__ */ new Float64Array(256), CB = /* @__PURE__ */ new Float64Array(256), CE0 = /* @__PURE__ */ new Float64Array(256), CE1 = /* @__PURE__ */ new Float64Array(256);
2470
+ let R1 = /* @__PURE__ */ new Float64Array(256), R2 = /* @__PURE__ */ new Float64Array(256);
2471
+ const BV = /* @__PURE__ */ new Float64Array(34), BW = /* @__PURE__ */ new Float64Array(34);
2472
+ function growC() { const g = (a) => { const b = new Float64Array(a.length * 2); b.set(a); return b; }; CA = g(CA); CB = g(CB); CE0 = g(CE0); CE1 = g(CE1); }
2473
+
2474
+ function canonEncode(g, prec, zp, mp, check, isLine) {
2475
+ const f = POW[prec], f0 = POW[zp], f1 = POW[mp], paths = [], la = isLine ? 0 : 1, lo = 1 - la;
2476
+ let dims = 0, big = 0, open = 0, cn = 0, longest = 0;
2477
+ const quant = (x, sc) => {
2478
+ const v = x * sc; if (typeof x !== 'number' || !Number.isFinite(v)) throw new RangeError('Invalid coordinate');
2479
+ const r = v < 0 ? -Math.round(-v) : Math.round(v); if (!(r >= -LIMIT && r <= LIMIT)) throw new RangeError('Coordinate too large');
2480
+ return r + 0;
2481
+ };
2482
+ const pos = (c) => {
2483
+ if (c == null || !(c.length >= 2 && c.length <= 4)) throw new TypeError('Invalid position');
2484
+ if (dims && dims !== c.length) throw new TypeError('Dimension mismatch');
2485
+ dims = c.length; if (cn === CA.length) growC();
2486
+ const a = quant(c[la], f), b = quant(c[lo], f); CA[cn] = a; CB[cn] = b;
2487
+ if (a < -2147483648 || a > 2147483647 || b < -2147483648 || b > 2147483647) big = 1;
2488
+ if (dims > 2) { CE0[cn] = quant(c[2], f0); if (dims > 3) CE1[cn] = quant(c[3], f1); }
2489
+ return cn++;
2490
+ };
2491
+ const list = (v) => { if (!Array.isArray(v)) throw new TypeError('Expected array'); return v; };
2492
+ // kind 0: a line, 1: a ring, 2: the points of a MultiPoint. A line or ring
2493
+ // whose last position repeats its first is closed, and the repeat isn't written.
2494
+ const path = (coords, kind) => {
2495
+ const at = cn, n = coords.length; for (let i = 0; i < n; i++) pos(coords[i]);
2496
+ const e = cn - 1, closed = kind !== 2 && n > 1 && CA[at] === CA[e] && CB[at] === CB[e] && (dims < 3 || CE0[at] === CE0[e]) && (dims < 4 || CE1[at] === CE1[e]) ? 1 : 0;
2497
+ if (kind === 1 && !closed) open = 1;
2498
+ if (n > longest) longest = n;
2499
+ paths.push({ kind, at, closed, m: n - closed });
2500
+ };
2501
+ const prep = (node, depth) => {
2502
+ const type = TYPES.indexOf(node == null ? node : node.type);
2503
+ if (type < 0) throw new TypeError('Invalid geometry');
2504
+ if (type === 6) { if (depth >= MAX_DEPTH) throw new RangeError('Depth exceeded'); return { type, members: list(node.geometries).map((m) => prep(m, depth + 1)) }; }
2505
+ const c = list(node.coordinates);
2506
+ if (type === 0) return { type, at: c.length ? pos(c) : -1 };
2507
+ const from = paths.length; let polys = null;
2508
+ if (type === 1 || type === 3) path(c, type === 1 ? 0 : 2);
2509
+ else if (type === 4) for (let i = 0; i < c.length; i++) path(list(c[i]), 0);
2510
+ else {
2511
+ polys = [];
2512
+ for (const rings of type === 2 ? [c] : c) { list(rings); for (let i = 0; i < rings.length; i++) path(list(rings[i]), 1); polys.push(rings.length); }
2513
+ }
2514
+ return { type, from, to: paths.length, polys };
2515
+ };
2516
+ let tree;
2517
+ if (isLine) { if (g == null || !(g.length >= 0)) throw new TypeError('Expected array of points'); path(g, 0); tree = { type: 1, from: 0, to: 1, polys: null }; }
2518
+ else tree = prep(g, 0);
2519
+
2520
+ // Wide when the string can't be ordinary: the precision, an integer
2521
+ // outside 32 bits, or a first position with no anchor code.
2522
+ const nx = dims > 2 ? dims - 2 : 0, wide = prec > 7 || big || (cn > 0 && (Math.abs(CA[0]) > 90 * f || Math.abs(CB[0]) > 180 * f)) ? 1 : 0;
2523
+ const kMax = wide ? KWIDE : KMAX, width = wide ? 6 : 5, far = 8 + width, xpred = [0, 0];
2524
+ if (R1.length < longest) { R1 = new Float64Array(longest + (longest >> 1)); R2 = new Float64Array(R1.length); }
2525
+ // An extra coordinate takes second differences when their bit lengths add up to less.
2526
+ for (let d = 0; d < nx; d++) {
2527
+ const E = d ? CE1 : CE0; let c0 = 0, c1 = 0, fits = 1;
2528
+ for (const p of paths) {
2529
+ let pv = 0;
2530
+ for (let i = p.at + 1, e = p.at + p.m; i < e; i++) { const dv = E[i] - E[i - 1], z = zigBig(dv - pv); c0 += bitLenBig(zigBig(dv)); c1 += bitLenBig(z); if (z >= P2[53]) fits = 0; pv = dv; }
2531
+ }
2532
+ xpred[d] = fits && c1 < c0 ? 1 : 0;
2533
+ }
2534
+
2535
+ // The values `mode` writes for the steps of path p, into R1 and R2. False
2536
+ // when it can't: a value of 2^53 or more in a wide string, or, for a split
2537
+ // mode in a wide string, a step outside 32 bits.
2538
+ const resid = (p, mode) => {
2539
+ const at = p.at, m = p.m, r1 = R1, r2 = R2, a = CA, b = CB;
2540
+ if (mode < 2 || mode === 4) {
2541
+ let va = 0, vb = 0;
2542
+ for (let i = 1; i < m; i++) {
2543
+ let da = a[at + i] - a[at + i - 1], db = b[at + i] - b[at + i - 1], ra = 0, rb = 0;
2544
+ if (wide) {
2545
+ ra = mode ? da - va : da; rb = mode ? db - vb : db;
2546
+ if (mode === 1) { va = da; vb = db; } else if (mode === 4) { va = i === 1 ? da : Math.floor((va + da) / 2); vb = i === 1 ? db : Math.floor((vb + db) / 2); }
2547
+ ra = zigBig(ra); rb = zigBig(rb); if (ra >= P2[53] || rb >= P2[53]) return false;
2548
+ } else {
2549
+ da |= 0; db |= 0; ra = mode ? (da - va) | 0 : da; rb = mode ? (db - vb) | 0 : db;
2550
+ if (mode === 1) { va = da; vb = db; } else if (mode === 4) { va = i === 1 ? da : (va + da) >> 1; vb = i === 1 ? db : (vb + db) >> 1; }
2551
+ ra = ((ra << 1) ^ (ra >> 31)) >>> 0; rb = ((rb << 1) ^ (rb >> 31)) >>> 0;
2552
+ }
2553
+ r1[i - 1] = ra; r2[i - 1] = rb;
2554
+ }
2555
+ return true;
2556
+ }
2557
+ const lat0 = a[at], lng0 = b[at], c2 = mode === 3 ? cos2Int(lat0, prec) : 0, c2s = c2 >> 4, closing = mode === 3 && p.closed === 1 && m >= 4;
2558
+ let sw = -1, s = 0, sg = 0, pM = 0, swU = 0, sU = 0, swV = 0, sV = 0, maxL1 = 0, nextV = 1, da1 = 0, db1 = 0;
2559
+ for (let i = 1; i < m; i++) {
2560
+ let da = a[at + i] - a[at + i - 1], db = b[at + i] - b[at + i - 1];
2561
+ if (wide && (da < -2147483648 || da > 2147483647 || db < -2147483648 || db > 2147483647)) return false;
2562
+ da |= 0; db |= 0;
2563
+ if (i === 1) {
2564
+ r1[0] = ((da << 1) ^ (da >> 31)) >>> 0; r2[0] = ((db << 1) ^ (db >> 31)) >>> 0;
2565
+ if (mode === 3) { da1 = da; db1 = db; setOrthoFrame(da, db, c2); swU = OF_SWU; sU = OF_SU; swV = OF_SWV; sV = OF_SV; maxL1 = Math.abs(da) + Math.abs(db); }
2566
+ else { sw = Math.abs(db) >= Math.abs(da) ? -1 : 0; const dM = sw ? db : da; s = divSlope(sw ? da : db, dM); pM = dM; sg = dM ? dM >> 31 : 0; }
2567
+ } else if (mode !== 3) {
2568
+ const dM = sw ? db : da, dm = sw ? da : db, ev = (dm - ((Math.imul(dM, s) + 2048) >> 12)) | 0;
2569
+ if (mode === 5) { const u = (dM - pM) | 0; r1[i - 1] = ((u << 1) ^ (u >> 31)) >>> 0; }
2570
+ else { const u = ((dM ^ sg) - sg) | 0, w = u >>> 0; r1[i - 1] = u < -536870912 ? w + Math.floor(w / 7) : u < 0 ? (-u - 1) * 8 + 7 : u + Math.floor(u / 7); }
2571
+ r2[i - 1] = ((ev << 1) ^ (ev >> 31)) >>> 0; pM = dM;
2572
+ if (Math.abs(dm) > Math.abs(dM)) { sw = ~sw; s = divSlope(dM, dm); pM = dm; }
2573
+ else if (ev && dM) { let ds = divSlope(ev, dM); if (Math.abs(dM) < 4) ds >>= 1; s = (s + ds) | 0; }
2574
+ if (pM) sg = pM >> 31;
2575
+ } else if (closing && i === m - 1) {
2576
+ // The closing corner: what's left to the first point along the next edge direction.
2577
+ let pa = -da1 | 0, pb = -db1 | 0;
2578
+ if (m !== 4) {
2579
+ const cw = nextV ? swV : swU, cs = nextV ? sV : sU, pA = cw ? cs >> 4 : 256, pB = cw ? 256 : cs >> 4;
2580
+ const wB = (c2s * pB + 128) >> 8, denom = pA * pA + wB * pB, num = ((lat0 - a[at + i - 1]) | 0) * pA + ((lng0 - b[at + i - 1]) | 0) * wB;
2581
+ pa = Math.trunc((num * pA) / denom) | 0; pb = Math.trunc((num * pB) / denom) | 0;
2582
+ }
2583
+ const ea = (da - pa) | 0, eb = (db - pb) | 0;
2584
+ r1[i - 1] = ((ea << 1) ^ (ea >> 31)) >>> 0; r2[i - 1] = ((eb << 1) ^ (eb >> 31)) >>> 0;
2585
+ } else {
2586
+ const cw = nextV ? swV : swU, cs = nextV ? sV : sU, dM = cw ? db : da, ev = ((cw ? da : db) - ((Math.imul(dM, cs) + 2048) >> 12)) | 0;
2587
+ r1[i - 1] = ((dM << 1) ^ (dM >> 31)) >>> 0; r2[i - 1] = ((ev << 1) ^ (ev >> 31)) >>> 0;
2588
+ const ow = nextV ? swU : swV, os = nextV ? sU : sV, oerr = ((ow ? da : db) - ((Math.imul(ow ? db : da, os) + 2048) >> 12)) | 0;
2589
+ const stayed = Math.abs(ev) <= Math.abs(oerr), wasU = nextV ? !stayed : stayed; nextV = wasU ? 1 : 0;
2590
+ if (Math.abs(da) < 262144 && Math.abs(db) < 262144 && Math.abs(da) + Math.abs(db) > maxL1) {
2591
+ maxL1 = Math.abs(da) + Math.abs(db);
2592
+ if (wasU) setOrthoFrame(da, db, c2); else setOrthoFrame(db, -(((da << 12) / c2) | 0) | 0, c2);
2593
+ swU = OF_SWU; sU = OF_SU; swV = OF_SWV; sV = OF_SV;
2594
+ }
2595
+ }
2596
+ }
2597
+ return true;
2598
+ };
2599
+
2600
+ // The parameters of the block of cnt steps from step i of a path of n
2601
+ // steps, after ck1 and ck2: bk1 and bk2 (-9 when the block doesn't read
2602
+ // one), and their bits with the values they code.
2603
+ let bk1 = 0, bk2 = 0;
2604
+ const block = (split, closing, n, i, cnt, ck1, ck2) => {
2605
+ if (!split) {
2606
+ for (let j = 0; j < cnt; j++) { BV[j] = R1[i + j]; BV[cnt + j] = R2[i + j]; }
2607
+ bk1 = bk2 = bestKBig(BV, 2 * cnt, ck1, kMax, far, 0); return KBITS;
2608
+ }
2609
+ let n1 = 0, n2 = 0, bits = 0;
2610
+ for (let j = i; j < i + cnt; j++) {
2611
+ if (j === 0) { BV[n1++] = R1[0]; BV[n1++] = R2[0]; }
2612
+ else if (closing && j === n - 1) { BW[n2++] = R1[j]; BW[n2++] = R2[j]; }
2613
+ else { BV[n1++] = R1[j]; BW[n2++] = R2[j]; }
2614
+ }
2615
+ bk1 = bk2 = -9;
2616
+ if (n1) { bk1 = bestKBig(BV, n1, ck1, KMAX, 13, 1); bits = KBITS; }
2617
+ if (n2) { bk2 = bestKBig(BW, n2, ck2, KMAX, 13, 2); bits += KBITS; }
2618
+ return bits;
2619
+ };
2620
+
2621
+ let started = 0, lat = 0, lng = 0, k1 = KI[prec], k2 = 0;
2622
+ const xv = [0, 0], xk = [0, 0];
2623
+ // The mode of the geometry whose paths are from..to: the cheapest, and
2624
+ // the lowest of those. 0 when it has no step to predict.
2625
+ const pickMode = (from, to) => {
2626
+ let best = Infinity, bm = 0, steps = 0;
2627
+ for (let t = from; t < to; t++) if (paths[t].m > 1) steps = 1;
2628
+ for (let md = 0; steps && md < 6; md++) {
2629
+ const split = md === 2 || md === 3 || md === 5; let c = MODE_LEN[md], ck1 = k1, ck2 = k2;
2630
+ for (let t = from; t < to && c < Infinity; t++) {
2631
+ const p = paths[t], n = p.m - 1; if (n < 1) continue;
2632
+ if (!resid(p, md)) { c = Infinity; break; }
2633
+ const closing = md === 3 && p.closed === 1 && p.m >= 4;
2634
+ for (let i = 0; i < n; i += BLOCK) {
2635
+ c += block(split, closing, n, i, n - i < BLOCK ? n - i : BLOCK, ck1, ck2);
2636
+ if (bk1 !== -9) ck1 = bk1 < 0 ? 0 : bk1;
2637
+ if (split && bk2 !== -9) ck2 = bk2 < 0 ? 0 : bk2;
2638
+ }
2639
+ }
2640
+ if (c < best) { best = c; bm = md; }
2641
+ }
2642
+ return bm;
2643
+ };
2644
+ const putPos = (i) => {
2645
+ room(64);
2646
+ const a = CA[i], b = CB[i];
2647
+ if (!started) { started = 1; if (wide) { putSized(zigBig(a)); putSized(zigBig(b)); } else putTruncAnchor(a, b, prec); }
2648
+ else {
2649
+ const za = wide ? zigBig(a - lat) : (((a - lat) << 1) ^ ((a - lat) >> 31)) >>> 0, zb = wide ? zigBig(b - lng) : (((b - lng) << 1) ^ ((b - lng) >> 31)) >>> 0;
2650
+ const L = Math.max(bitLenBig(za), bitLenBig(zb));
2651
+ if (L === 0) put(1, 0); else { put(1, 1); put(width, L - 1); putBig(L, za); putBig(L, zb); }
2652
+ }
2653
+ lat = a; lng = b;
2654
+ if (nx) { putSized(zigBig(CE0[i] - xv[0])); xv[0] = CE0[i]; if (nx > 1) { putSized(zigBig(CE1[i] - xv[1])); xv[1] = CE1[i]; } }
2655
+ };
2656
+ const putPath = (p, mode) => {
2657
+ room(32); if (p.kind === 0 || (p.kind === 1 && open)) put(1, p.closed);
2658
+ putCount(p.m); if (p.m === 0) return;
2659
+ putPos(p.at);
2660
+ const n = p.m - 1, split = mode === 2 || mode === 3 || mode === 5, closing = mode === 3 && p.closed === 1 && p.m >= 4, ve = [0, 0];
2661
+ if (n > 0) resid(p, mode);
2662
+ for (let i = 0; i < n; i += BLOCK) {
2663
+ const cnt = n - i < BLOCK ? n - i : BLOCK; block(split, closing, n, i, cnt, k1, k2);
2664
+ const a = bk1, b = bk2; room(16);
2665
+ if (!split) {
2666
+ putParam(a, k1, width); k1 = a < 0 ? 0 : a;
2667
+ if (a === -1) {
2668
+ let mask = 0; for (let j = i; j < i + cnt; j++) mask = (mask << 1) | (R1[j] !== 0 || R2[j] !== 0 ? 1 : 0);
2669
+ put(cnt, mask);
2670
+ for (let j = i; j < i + cnt; j++) { const u = R1[j], v = R2[j]; if (u !== 0 || v !== 0) putRice(u, u === 0 ? v - 1 : v, 0, 0, 1); }
2671
+ } else if (a >= 0) for (let j = i; j < i + cnt; j++) putRice(R1[j], R2[j], a, a, 1);
2672
+ } else {
2673
+ if (a !== -9) { putParam(a, k1, 5); k1 = a; }
2674
+ if (b !== -9) { putParam(b, k2, 5); k2 = b < 0 ? 0 : b; }
2675
+ for (let j = i; j < i + cnt; j++) {
2676
+ if (j === 0) putRice(R1[0], R2[0], a, a, 1);
2677
+ else if (closing && j === n - 1) { if (b >= 0) putRice(R1[j], R2[j], b, b, 1); }
2678
+ else if (b < 0) putRice(R1[j], 0, a, 0, 0);
2679
+ else putRice(R1[j], R2[j], a, b, 1);
2680
+ }
2681
+ }
2682
+ for (let d = 0; d < nx; d++) {
2683
+ const E = d ? CE1 : CE0;
2684
+ for (let j = 0, x = p.at + 1 + i; j < cnt; j++, x++) { const dv = E[x] - E[x - 1]; BV[j] = zigBig(xpred[d] ? dv - ve[d] : dv); ve[d] = dv; }
2685
+ const ek = bestKBig(BV, cnt, xk[d], KWIDE, 14, 2); room(16); putParam(ek, xk[d], 6); xk[d] = ek < 0 ? 0 : ek;
2686
+ if (ek >= 0) for (let j = 0; j < cnt; j++) putRice(BV[j], 0, ek, 0, 0);
2687
+ }
2688
+ }
2689
+ const e = p.at + n; lat = CA[e]; lng = CB[e]; xv[0] = CE0[e]; xv[1] = CE1[e];
2690
+ };
2691
+ const putNode = (node) => {
2692
+ room(32);
2693
+ if (node.type === 0) { put(1, node.at < 0 ? 0 : 1); if (node.at >= 0) putPos(node.at); return; }
2694
+ if (node.type === 6) { putCount(node.members.length); for (const mb of node.members) { room(16); put(3, mb.type); putNode(mb); } return; }
2695
+ const md = pickMode(node.from, node.to); let t = node.from;
2696
+ put(MODE_LEN[md], MODE_VAL[md]);
2697
+ if (node.polys === null) { if (node.type === 4) putCount(node.to - t); for (; t < node.to; t++) putPath(paths[t], md); return; }
2698
+ if (node.type === 5) putCount(node.polys.length);
2699
+ for (const rings of node.polys) {
2700
+ room(16); if (rings === 1) put(1, 1); else { put(1, 0); putCount(rings); }
2701
+ for (let r = 0; r < rings; r++) putPath(paths[t++], md);
2702
+ }
2703
+ };
2704
+ putHeader(tree.type, prec, (wide << 5) | (open << 4) | (nx > 0 ? 8 : 0) | (nx > 1 ? 4 : 0) | check, zp, xpred[0], mp, xpred[1]);
2705
+ putNode(tree);
2706
+ const s = toText();
2707
+ if (CA.length > (KEEP >> 2)) { CA = new Float64Array(256); CB = new Float64Array(256); CE0 = new Float64Array(256); CE1 = new Float64Array(256); R1 = new Float64Array(256); R2 = new Float64Array(256); }
2708
+ return check ? addSum(s, check) : s;
2709
+ }
2710
+
2711
+ // ---------------------------------------------------------------- public encoders (zero-alloc option parsing!)
2712
+
2713
+ // OP_ZP and OP_MP are -1 when the option was left out, and precision then
2714
+ // stands in. The -1 is never what a caller passed: every value that's given
2715
+ // is checked where it's read. It once was the test for "left out" as well, so
2716
+ // zPrecision: -1 quietly meant the default (SPEC.md section 16.2 refuses it).
2717
+ // An option that's given must be one of its values: checksum: 'none' used to
2718
+ // turn the full check on, and favor: 'fast' was the default encoder.
2719
+ let OP_P = 5, OP_ZP = -1, OP_MP = -1, OP_CS = 0, OP_FV;
2720
+ const badPrecision = () => new RangeError('precision, zPrecision and mPrecision must be integers from 0 to 15');
2721
+ function parseOpts(options) {
2722
+ OP_P = 5; OP_ZP = -1; OP_MP = -1; OP_CS = 0; OP_FV = undefined;
2723
+ if (options != null) {
2724
+ if (typeof options !== 'object') OP_P = options;
2725
+ else {
2726
+ let v = options.precision; if (v != null) OP_P = v;
2727
+ v = options.zPrecision; if (v != null) { if (typeof v !== 'number' || POW[v] === undefined) throw badPrecision(); OP_ZP = v; }
2728
+ v = options.mPrecision; if (v != null) { if (typeof v !== 'number' || POW[v] === undefined) throw badPrecision(); OP_MP = v; }
2729
+ v = options.checksum; if (v != null && v !== false) { if (v === true) OP_CS = 2; else if (v === 'short') OP_CS = 1; else throw new RangeError("checksum must be true, false or 'short'"); }
2730
+ v = options.favor; if (v != null) { if (v !== 'size' && v !== 'speed') throw new RangeError("favor must be 'size' or 'speed'"); OP_FV = v; }
2731
+ }
2732
+ }
2733
+ if (typeof OP_P !== 'number' || POW[OP_P] === undefined) throw badPrecision();
2734
+ if (OUT === null) setupEncoder();
2735
+ }
2736
+
2737
+ // The fast encoders for a line, whichever function it came through:
2738
+ // encodeLine(points) and encode({ type: 'LineString' }) write the same string
2739
+ // at every setting. la and lo are where a point keeps its latitude and
2740
+ // longitude. Null when the line needs the canonical encoder: wide, Z or M.
2741
+ function lineFast(points, la, lo, prec, check, favor) {
2742
+ if (points.length === 0) { putHeader(1, prec, check, 0, 0, 0, 0); put(7, 8); const s = toText(); return check ? addSum(s, check) : s; }
2743
+ if (prec >= 5 && !check) {
2744
+ if (points.length <= 18) { const s = shortEncode(points, false, la, lo, true, prec, favor); if (s !== null) return s; }
2745
+ else if (points.length > 64) { const s = streamEncodeLine(points, la, lo, prec, favor); if (s !== null) return s; }
2746
+ }
2747
+ return fastEncodeGeo(points, prec, check, favor, true, la, lo);
2748
+ }
2749
+
2750
+ export function encode(geometry, options) {
2751
+ parseOpts(options);
2752
+ const prec = OP_P, check = OP_CS, favor = OP_FV;
2753
+ if (geometry != null && geometry.type === 'Feature') geometry = geometry.geometry;
2754
+ if (prec < 8 && favor !== 'size' && geometry != null) {
2755
+ const t = geometry.type, c = geometry.coordinates;
2756
+ if (t === 'LineString') {
2757
+ if (Array.isArray(c)) { const s = lineFast(c, 1, 0, prec, check, favor); if (s !== null) return s; }
2758
+ } else {
2759
+ if (prec >= 5 && !check) {
2760
+ if (t === 'Point' && Array.isArray(c)) {
2761
+ if (c.length === 0) { putHeader(0, prec, 0, 0, 0, 0, 0); put(1, 0); return toText(); }
2762
+ if (c.length === 2) {
2763
+ const f = POW[prec]; let x = c[1], y = c[0];
2764
+ if (typeof x === 'number' && typeof y === 'number' && Math.abs(x *= f) < I32H && Math.abs(y *= f) < I32H) {
2765
+ const lat = (x < 0 ? x - H : x + H) | 0, lng = (y < 0 ? y - H : y + H) | 0;
2766
+ if (lat >= -90 * f && lat <= 90 * f && lng >= -180 * f && lng <= 180 * f) { putHeader(0, prec, 0, 0, 0, 0, 0); put(1, 1); putTruncAnchor(lat, lng, prec); return toText(); }
2767
+ }
2768
+ }
2769
+ } else if (t === 'Polygon' && Array.isArray(c) && c.length === 1 && Array.isArray(c[0]) && c[0].length > 1 && c[0].length <= 18) {
2770
+ const s = shortEncode(c[0], true, 1, 0, true, prec, favor); if (s !== null) return s;
2771
+ }
2772
+ }
2773
+ const s = fastEncodeGeo(geometry, prec, check, favor, false, 0, 1); if (s !== null) return s;
2774
+ }
2775
+ }
2776
+ return canonEncode(geometry, prec, OP_ZP < 0 ? prec : OP_ZP, OP_MP < 0 ? prec : OP_MP, check, false);
2777
+ }
2778
+
2779
+ export function encodeLine(points, options) {
2780
+ parseOpts(options);
2781
+ const prec = OP_P, check = OP_CS;
2782
+ if (prec < 8 && OP_FV !== 'size' && Array.isArray(points)) { const s = lineFast(points, 0, 1, prec, check, OP_FV); if (s !== null) return s; }
2783
+ return canonEncode(points, prec, OP_ZP < 0 ? prec : OP_ZP, OP_MP < 0 ? prec : OP_MP, check, true);
2784
+ }
2785
+
2786
+ // The canonical string for what str decodes to: what favor: 'size' writes
2787
+ // for it (SPEC.md section 16.5).
2788
+ //
2789
+ // Without a target that's at str's own precisions and with its kind of
2790
+ // check. A string is canonical when this returns the string itself, and two
2791
+ // canonical strings with the same precisions and the same kind of check are
2792
+ // equal exactly when their geometries are. The precisions and the check are
2793
+ // the sender's, though: one Point has a canonical string at each of the 16
2794
+ // precisions and with each kind of check.
2795
+ //
2796
+ // With options.to, a precision or the options of encode, the string is
2797
+ // written at those and nothing is taken from str but its geometry: exactly
2798
+ // encode(decode(str, options), { ...to, favor: 'size' }). At a target the
2799
+ // application fixes, one geometry has one string.
2800
+ //
2801
+ // The target is judged before the string is read, so that a wrong one is
2802
+ // the caller's RangeError whatever the string is.
2803
+ export function canonical(str, options) {
2804
+ if (typeof str !== 'string') throw new TypeError('Expected a string');
2805
+ try {
2806
+ let need = false, to;
2807
+ if (options !== undefined) { need = decodeOptions(options); if (options !== null) to = options.to; }
2808
+ if (to != null) { parseOpts(to); if (OP_FV === 'speed') throw new RangeError("canonical writes with favor 'size'"); }
2809
+ if (str.length > CAPL) throw over(0);
2810
+ const type = decodeHeader(str, str.length, 0, need), d = DD, prec = PREC, check = OPTS & 3, zp = NX > 0 ? POW.indexOf(XF[0]) : prec, mp = NX > 1 ? POW.indexOf(XF[1]) : prec;
2811
+ let g;
2812
+ if (str.length > CAPP / 24) { LEFT = CAPP; g = bodyCounted(d, type, 0, POW[prec], prec); } else g = decodeBody(d, type, 0, POW[prec], prec);
2813
+ checkEnded(d);
2814
+ if (OUT === null) setupEncoder();
2815
+ if (to != null) return canonEncode(g, OP_P, OP_ZP < 0 ? OP_P : OP_ZP, OP_MP < 0 ? OP_P : OP_MP, OP_CS, false);
2816
+ return canonEncode(g, prec, zp, mp, check, false);
2817
+ } finally { CAPL = DEF_LEN; CAPP = DEF_POS; }
2818
+ }
2819
+
2820
+ // ---------------------------------------------------------------- the binary form (SPEC.md section 23)
2821
+ //
2822
+ // The same bits, 8 to a byte. The decoder above works on bytes already: a
2823
+ // string is turned into them first. So a value is copied into the same
2824
+ // buffer and read by the same functions, without the base64url step, and
2825
+ // the encoders give their bytes as they are. What differs is here: the
2826
+ // check, which is over the bytes and takes whole bytes, the padding, fewer
2827
+ // than 8 zero bits, and what the limits count.
2828
+
2829
+ // The longest value this decoder takes: as many bits as MAX_LEN characters.
2830
+ // A value of at most FREE_BYTES bytes can't hold more than DEF_POS (nothing
2831
+ // holds more than 4 to a bit), so it's decoded without being counted.
2832
+ const MAX_BYTES = 3 * 2 ** 26, FREE_BYTES = 32768;
2833
+ const notBytes = () => new TypeError('Expected a Uint8Array');
2834
+ // Whether b isn't a Uint8Array. instanceof is false for one made in another
2835
+ // realm (a vm context, an iframe), so that one is asked for the name of its
2836
+ // typed array, through this realm's getter: the internal name, which a
2837
+ // property can't fake, and undefined for anything else.
2838
+ const TA_NAME = /* @__PURE__ */ Object.getOwnPropertyDescriptor(Object.getPrototypeOf(Uint8Array.prototype), Symbol.toStringTag).get;
2839
+ const notU8 = (b) => !(b instanceof Uint8Array) && TA_NAME.call(b) !== 'Uint8Array';
2840
+ // The message of a refusal says "string". For a value it says "value".
2841
+ const asValue = (e) => { if (e instanceof GeotiniError) e.message = e.message.replace('string', 'value'); return e; };
2842
+
2843
+ function crcBytes(b, len) {
2844
+ if (!CRC) crc32c('', 0);
2845
+ let crc = -1;
2846
+ for (let i = 0; i < len; i++) crc = CRC[(crc ^ b[i]) & 255] ^ (crc >>> 8);
2847
+ return ~crc >>> 0;
2848
+ }
2849
+ // kind 1: the 16 high bits of the CRC of the n bytes before it, in 2 bytes. 2: all 32 in 4.
2850
+ function putCrc(out, n, kind) {
2851
+ const c = crcBytes(out, n);
2852
+ out[n] = c >>> 24; out[n + 1] = c >>> 16;
2853
+ if (kind === 2) { out[n + 2] = c >>> 8; out[n + 3] = c; }
2854
+ }
2855
+ // The first n bytes of a buffer into an array: a loop for a few bytes, and
2856
+ // set from a plain view of the buffer for more. Under 64 bytes, making the
2857
+ // array and filling it took 47 to 74 ns with the loop on Node 26 and 90 to
2858
+ // 102 with set. From 65 bytes on the array is what costs, 0.6 to 1.9
2859
+ // microseconds for 65 to 1,000 bytes there, whichever way it's filled.
2860
+ function copyTo(out, b, n) {
2861
+ if (n < 64) for (let i = 0; i < n; i++) out[i] = b[i]; else out.set(new Uint8Array(b.buffer, b.byteOffset, n));
2862
+ }
2863
+ // The decode buffer as bytes: the one that's kept, or the one made for a long input.
2864
+ const bufOf = (dv) => (dv === DV ? BUF : new Uint8Array(dv.buffer, dv.byteOffset, dv.byteLength));
2865
+ // A value in the decode buffer, with zero bytes after it as there are after a string's.
2866
+ function loadBytes(bytes, len) {
2867
+ const dv = ensureDecBuf(len), b = bufOf(dv);
2868
+ if (len < 64) for (let i = 0; i < len; i++) b[i] = bytes[i]; else b.set(bytes);
2869
+ dv.setInt32(len, 0); dv.setInt32(len + 4, 0); dv.setInt32(len + 8, 0);
2870
+ return dv;
2871
+ }
2872
+
2873
+ // decodeHeader for a value: the same lines, but for the bytes and the check.
2874
+ // test/binary.test.js compares their text. The check is the CRC of the bytes
2875
+ // before it, and once it's verified it's cleared from the buffer, so that
2876
+ // the bytes after the payload are zero as they are after a string's.
2877
+ function headerValue(bytes, len, line, need) {
2878
+ if (len === 0) throw err(T);
2879
+ const d = loadBytes(bytes, len);
2880
+ const h = d.getInt32(0); WIDE = OPEN = NX = STARTED = OPTS = 0;
2881
+ let type = 0, prec = 5, p = 4;
2882
+ if (h >= 0) { type = T2_MAP[(h >>> 29) & 3]; prec = 5 + ((h >>> 28) & 1); }
2883
+ else if ((h & 0x40000000) === 0) {
2884
+ const s2 = (h >>> 28) & 3, p6 = (h >>> 27) & 1; p = 5;
2885
+ if (s2 < 3) { type = S2_MAP[s2]; prec = 5 + p6; } else { type = p6 ? 2 : 1; prec = 7; }
2886
+ } else if ((h & 0x20000000) === 0) {
2887
+ const s3 = (h >>> 26) & 7; if (s3 >= 5) throw verErr();
2888
+ type = S3_MAP[s3]; prec = 7; p = 6;
2889
+ } else {
2890
+ if (h & 0x10000000) throw verErr();
2891
+ // The extended header: all 17 bits before any is judged, and the check
2892
+ // before the rest, so a damaged header is CHECKSUM whatever it says.
2893
+ if (len < 3) throw err(T);
2894
+ type = (h >>> 25) & 7; prec = (h >>> 21) & 15; const o = (h >>> 15) & 63, check = o & 3; p = 17;
2895
+ OPTS = o; STEPS = BIG = OPENED = 0;
2896
+ if (check === 3) throw verErr();
2897
+ if (check) {
2898
+ const tail = check << 1;
2899
+ if (len < 3 + tail) throw err(T);
2900
+ len -= tail; const crc = crcBytes(bytes, len);
2901
+ if (check === 2 ? d.getInt32(len) !== (crc | 0) : d.getUint16(len) !== crc >>> 16) throw err(CS);
2902
+ d.setInt32(len, 0);
2903
+ } else if (need) throw err(CS);
2904
+ if (type === 7 || (o & 12) === 4) throw verErr();
2905
+ WIDE = o >> 5; OPEN = (o >> 4) & 1;
2906
+ if (prec > 7) { if (!WIDE) throw err(BH); BIG = 1; }
2907
+ if (o === 0 && prec >= 5 && prec <= 7) throw err(NC);
2908
+ if (OPEN && (type < 2 || type === 3 || type === 4)) throw err(NC);
2909
+ if (o & 8) { XF[0] = POW[(d.getInt32(p >> 3) << (p & 7)) >>> 28]; XP[0] = (d.getInt32((p + 4) >> 3) << ((p + 4) & 7)) >>> 31; XV[0] = XK[0] = 0; NX = 1; p += 5; }
2910
+ if (o & 4) { XF[1] = POW[(d.getInt32(p >> 3) << (p & 7)) >>> 28]; XP[1] = (d.getInt32((p + 4) >> 3) << ((p + 4) & 7)) >>> 31; XV[1] = XK[1] = 0; NX = 2; p += 5; }
2911
+ }
2912
+ if (need && (OPTS & 3) === 0) throw err(CS);
2913
+ if (line && type !== 1) throw err('WRONG_TYPE');
2914
+ DD = d; PREC = prec; SP = p; SEND = len * 8; SK1 = KI[prec]; SK2 = 0;
2915
+ return type;
2916
+ }
2917
+ // checkEnded for a value: at most 7 zero bits.
2918
+ function endedValue(d) {
2919
+ const p = SP;
2920
+ if (p > SEND) throw err(T);
2921
+ if (OPTS !== 0 && ((WIDE && !BIG) || (OPEN && !OPENED) || (NX && (!STARTED || (STEPS === 0 && (XP[0] || (NX > 1 && XP[1]))))))) throw err(NC);
2922
+ if (SEND - p > 7 || d.getInt32(p >> 3) << (p & 7) !== 0) throw err(TD);
2923
+ }
2924
+ // A value's geometry, built or not: decodeWith's lines after its options.
2925
+ // A value of at most maxPositions / 32 bytes can't hold more than
2926
+ // maxPositions, so it isn't counted.
2927
+ function bodyValue(bytes, len, need) {
2928
+ if (len > (CAPL < MAX_BYTES ? CAPL : MAX_BYTES)) throw over(0);
2929
+ const type = headerValue(bytes, len, 0, need), d = DD;
2930
+ let g;
2931
+ if (len > CAPP / 32) { LEFT = CAPP; g = bodyCounted(d, type, 0, POW[PREC], PREC); } else g = decodeBody(d, type, 0, POW[PREC], PREC);
2932
+ endedValue(d);
2933
+ return g;
2934
+ }
2935
+
2936
+ // decode and decodeLine with options, or for a value long enough to hold more
2937
+ // than the default maxPositions, as decodeWith and lineWith are for a string.
2938
+ function valueWith(bytes, len, options) {
2939
+ try { return bodyValue(bytes, len, options != null && decodeOptions(options)); } finally { CAPL = DEF_LEN; CAPP = DEF_POS; }
2940
+ }
2941
+ function lineValueWith(bytes, len, options) {
2942
+ try {
2943
+ const need = options != null && decodeOptions(options);
2944
+ if (len > (CAPL < MAX_BYTES ? CAPL : MAX_BYTES)) throw over(0);
2945
+ headerValue(bytes, len, 1, need);
2946
+ const d = DD, counted = len > CAPP / 32;
2947
+ if (counted) { if (SP > SEND) throw err(T); LEFT = CAPP - 1; if (LEFT < 0) throw over(1); } // the line is a geometry, and counts one like any other
2948
+ const mode = readMode(d), before = LOW, line = counted ? pathCounted(d, mode, 0, 0, POW[PREC], PREC) : decodePath(d, mode, 0, 0, POW[PREC], PREC);
2949
+ if (mode !== 0 && LOW !== before) throw err(NC, SP);
2950
+ endedValue(d);
2951
+ return line;
2952
+ } finally { CAPL = DEF_LEN; CAPP = DEF_POS; }
2953
+ }
2954
+
2955
+ export function decodeValue(bytes, options) { try { return valueGeometry(bytes, options); } catch (e) { throw asValue(e); } }
2956
+ function valueGeometry(bytes, options) {
2957
+ if (notU8(bytes)) throw notBytes();
2958
+ const len = bytes.length;
2959
+ // As in decode: a call with options, a value long enough to pass a default
2960
+ // limit and the empty value leave here. Not with decode's (len - 1) >>> 0,
2961
+ // because a Uint8Array can have 2^32 bytes or more, and a length wrapped
2962
+ // to 32 bits took a value of 2^32 + 1 bytes for one of 1, past every limit.
2963
+ if (options != null || len > FREE_BYTES || len === 0) return valueWith(bytes, len, options);
2964
+ // A Point at precision 5 or 6 with a position: 7 or 8 bytes that start
2965
+ // with 0000 1 or 0001 1. Its bytes go to the buffer as two integers, with
2966
+ // zero bytes after them.
2967
+ if (len < 9) {
2968
+ const b0 = bytes[0];
2969
+ if (len > 6 && (b0 & 0xe8) === 8) {
2970
+ const d = BUF.length < 32 ? ensureDecBuf(32) : DV;
2971
+ d.setInt32(0, (b0 << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3]); d.setInt32(4, (bytes[4] << 24) | (bytes[5] << 16) | (bytes[6] << 8) | (len > 7 ? bytes[7] : 0)); d.setInt32(8, 0); d.setInt32(12, 0);
2972
+ const prec = 5 + (b0 >> 4), end = len * 8, p = readTruncAnchor(d, 5, prec), f = POW[prec];
2973
+ if (p > end) throw err(T);
2974
+ if (end - p > 7 || d.getInt32(p >> 3) << (p & 7) !== 0) throw err(TD);
2975
+ return { type: 'Point', coordinates: [SLNG / f, SLAT / f] };
2976
+ }
2977
+ }
2978
+ const type = headerValue(bytes, len, 0, false), d = DD, g = decodeBody(d, type, 0, POW[PREC], PREC);
2979
+ endedValue(d);
2980
+ return g;
2981
+ }
2982
+
2983
+ export function decodeLineValue(bytes, options) { try { return valueLine(bytes, options); } catch (e) { throw asValue(e); } }
2984
+ // decodeLine for a value. From the line that sets prec on, this is
2985
+ // decodeLine's text with 8 bits a byte and at most 7 bits of padding:
2986
+ // test/binary.test.js compares the two.
2987
+ function valueLine(bytes, options) {
2988
+ if (notU8(bytes)) throw notBytes();
2989
+ const len = bytes.length;
2990
+ if (options !== undefined || len > FREE_BYTES) return lineValueWith(bytes, len, options);
2991
+ const d = loadBytes(bytes, len);
2992
+ const h = d.getInt32(0);
2993
+ // Any other header.
2994
+ if (len < 2 || (h >>> 29) !== 1) {
2995
+ headerValue(bytes, len, 1, false);
2996
+ const dd = DD, mode = readMode(dd), before = LOW;
2997
+ const line = decodePath(dd, mode, 0, 0, POW[PREC], PREC);
2998
+ if (mode !== 0 && LOW !== before) throw err(NC, SP);
2999
+ endedValue(dd);
3000
+ return line;
3001
+ }
3002
+ const prec = 5 + ((h >>> 28) & 1), f = POW[prec], end = len * 8;
3003
+ let p = 6, mode = 0;
3004
+ const mb = (h >>> 24) & 15;
3005
+ if (mb < 8) mode = mb >> 2; else if (mb < 12) mode = 2; else if (mb < 14) { mode = 3; p = 7; } else { mode = 4 + (mb & 1); p = 8; }
3006
+ const closed = (h << p) >>> 31; p++;
3007
+ let w = h << p, z = Math.clz32(w), m = 0;
3008
+ if (z < 10) { m = ((w << z) >>> (28 - z)) - 8; p += 4 + (z << 1); } else { SP = p; SEND = end; m = readCount(d); p = SP; }
3009
+ // The bounds rule before the allocation: on JavaScriptCore, Array(n) for a
3010
+ // forged count of 134 million took 140 ms and the memory with it.
3011
+ if (m === 0) { if (closed) throw err('EMPTY_CLOSED_PATH', p, end); }
3012
+ else if (m - 1 > (mode === 2 || mode === 3 || mode === 5 ? end - p : 4 * (end - p))) throw err(T);
3013
+ const out = Array(m + closed <= RPT || 2 * m <= end - p ? m + closed : RPT);
3014
+ if (m > 0) {
3015
+ let lat = 0, lng = 0;
3016
+ if (prec === 5) {
3017
+ let v = (d.getInt32(p >> 3) << (p & 7)) >>> 8; p += 24;
3018
+ if (v >= 15554431) { v = (v << 1) + ((d.getInt32(p >> 3) << (p & 7)) >>> 31) - 15554431; p++; }
3019
+ lat = v > 15500000 ? v - 24500001 : v - 6500000;
3020
+ v = (d.getInt32(p >> 3) << (p & 7)) >>> 7; p += 25;
3021
+ if (v >= 31108863) { v = (v << 1) + ((d.getInt32(p >> 3) << (p & 7)) >>> 31) - 31108863; p++; }
3022
+ lng = v > 32000000 ? v - 50000001 : v - 14000000;
3023
+ } else if (prec === 6) {
3024
+ let v = (((d.getInt32(p >> 3) << (p & 7)) >>> 16) << 11) | ((d.getInt32((p + 16) >> 3) << ((p + 16) & 7)) >>> 21); p += 27;
3025
+ if (v >= 88435455) { v = (v << 1) + ((d.getInt32(p >> 3) << (p & 7)) >>> 31) - 88435455; p++; }
3026
+ lat = v > 105000000 ? v - 195000001 : v - 15000000;
3027
+ v = (((d.getInt32(p >> 3) << (p & 7)) >>> 16) << 12) | ((d.getInt32((p + 16) >> 3) << ((p + 16) & 7)) >>> 20); p += 28;
3028
+ if (v >= 176870911) { v = (v << 1) + ((d.getInt32(p >> 3) << (p & 7)) >>> 31) - 176870911; p++; }
3029
+ lng = v > 210000000 ? v - 390000001 : v - 30000000;
3030
+ } else { p = readTruncAnchor(d, p, prec); lat = SLAT; lng = SLNG; }
3031
+ out[0] = [lat / f, lng / f];
3032
+ if (m > 1) {
3033
+ let o = 1;
3034
+ if (m < 18 && mode < 2) {
3035
+ const tj = TJ, lin = -mode, p0 = p;
3036
+ let kw = d.getInt32(p >> 3) << (p & 7), k = KI[prec], e = Math.clz32(kw), n = 0, z1 = 0, z2 = 0, va = 0, vb = 0, a = lat, b = lng;
3037
+ if (e < 8) { p += e + 1; k = (k + ((e >>> 1) ^ -(e & 1))) | 0; }
3038
+ else { e = (kw << 8) >>> 27; n = e - k; k = e > KMAX || (n < 0 ? -2 * n - 1 : 2 * n) < 8 ? -9 : e; p += 13; }
3039
+ if (k >>> 0 <= KMAX) {
3040
+ const kk = k + k, mk = (1 << k) - 1, one = kk < 15;
3041
+ let x = 0, y = 0;
3042
+ for (; o < m; o++) {
3043
+ kw = d.getInt32(p >> 3) << (p & 7); e = tj[kw >>> 21];
3044
+ if (e !== 0) {
3045
+ if (one) {
3046
+ n = e >> 8; kw = ((kw << n) >>> 1) >>> (31 - kk);
3047
+ z1 = ((e & 15) << k) | (kw >>> k); z2 = (((e >> 4) & 15) << k) | (kw & mk); p += n + kk;
3048
+ } else {
3049
+ p += e >> 8;
3050
+ z1 = ((e & 15) << k) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - k)); p += k;
3051
+ z2 = (((e >> 4) & 15) << k) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - k)); p += k;
3052
+ }
3053
+ } else {
3054
+ x = Math.clz32(kw); if (x > 23) { p += 24; x = Math.clz32(d.getInt32(p >> 3) << (p & 7)); if (x > 11) throw err(R, p + 12 - k, end); p += x + 1; x += 24; } else p += x + 1;
3055
+ y = Math.clz32(d.getInt32(p >> 3) << (p & 7)); if (y > 23) { p += 24; y = Math.clz32(d.getInt32(p >> 3) << (p & 7)); if (y > 11) throw err(R, x + k > 35 ? p - x + 11 - k : p + 12 - k, end); p += y + 1; y += 24; } else p += y + 1;
3056
+ z1 = ((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - k); p += k;
3057
+ z2 = ((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - k); p += k;
3058
+ if (x >= 6) { n = x - 4; if (n + k > 31) throw err(R, p - x - y - k - 2 * k + 34, end); x = 1; if (n > 16) { x = (1 << (n - 16)) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (47 - n)); p += n - 16; n = 16; } x = (((x << n) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - n))) + 2) | 0; p += n; if ((x >>> 0) < 2 || (k && (x >>> (32 - k)))) throw err(R, y + k > 35 ? p - y - k - 2 * k + 35 - (31 - Math.clz32(x - 2)) : p, end); }
3059
+ if (y >= 6) { n = y - 4; if (n + k > 31) throw err(R, p - y - k - 2 * k + 35 - ((x >>> 0) >= 6 ? 31 - Math.clz32(x - 2) : 0), end); y = 1; if (n > 16) { y = (1 << (n - 16)) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (47 - n)); p += n - 16; n = 16; } y = (((y << n) | (((d.getInt32(p >> 3) << (p & 7)) >>> 1) >>> (31 - n))) + 2) | 0; p += n; if ((y >>> 0) < 2 || (k && (y >>> (32 - k)))) throw err(R, p, end); }
3060
+ z1 |= x << k; z2 |= y << k;
3061
+ }
3062
+ va = ((va & lin) + ((z1 >>> 1) ^ -(z1 & 1))) | 0; vb = ((vb & lin) + ((z2 >>> 1) ^ -(z2 & 1))) | 0;
3063
+ a = (a + va) | 0; b = (b + vb) | 0; out[o] = [a / f, b / f];
3064
+ }
3065
+ RA = a; RB = b;
3066
+ }
3067
+ if (o < m) { o = 1; p = p0; }
3068
+ }
3069
+ if (o < m) {
3070
+ if (mode < 2) p = bodyCoord(d, p, end, out, 1, m, -mode, 0, lat, lng, f, KI[prec]);
3071
+ else if (mode === 4) p = bodyEma(d, p, end, out, 1, m, 0, lat, lng, f, KI[prec]);
3072
+ else if (mode === 3) p = m === 4 && closed === 1 ? bodyOrtho4(d, p, end, out, 0, lat, lng, f, prec, KI[prec], 0) : bodyOrtho(d, p, end, out, 1, m, closed, 0, lat, lng, f, prec, KI[prec], 0);
3073
+ else p = bodySlope(d, p, end, out, 1, m, mode === 5 ? -1 : 0, 0, lat, lng, f, KI[prec], 0);
3074
+ }
3075
+ // An open line can't end on its first point: closed is how that's written.
3076
+ if (closed === 0 && RA === lat && RB === lng) throw err(NC, p, end);
3077
+ } else if (mode !== 0) throw err(NC, p, end); // a mode that predicted nothing must be 0
3078
+ if (closed) out[m] = [lat / f, lng / f];
3079
+ } else if (mode !== 0) throw err(NC, p, end);
3080
+ if (p > end) throw err(T);
3081
+ if (end - p > 7 || d.getInt32(p >> 3) << (p & 7) !== 0) throw err(TD);
3082
+ return out;
3083
+ }
3084
+
3085
+ // The encoders' bytes as a value of its own: toText in binary.
3086
+ function toBin() {
3087
+ const b = OUT; let n = WO; if (WN) b[n++] = WACC << (8 - WN);
3088
+ const out = new Uint8Array(n);
3089
+ copyTo(out, b, n);
3090
+ if (OUT.length > KEEP) { WO = 0; outBuffer(4096); }
3091
+ return out;
3092
+ }
3093
+ // addSum in binary: the value with its check after it.
3094
+ function sumBin(b, kind) {
3095
+ const n = b.length, out = new Uint8Array(n + (kind << 1));
3096
+ out.set(b); putCrc(out, n, kind);
3097
+ return out;
3098
+ }
3099
+
3100
+ // What BIN is while a value is written: toText (no bytes yet) or addSum.
3101
+ function binOut(b, kind) { return b === null ? toBin() : sumBin(b, kind); }
3102
+
3103
+ // The same encoders, every one of them: what they'd write as a string comes
3104
+ // out as its value (SPEC.md section 23.5). The encoder is set up first,
3105
+ // because setting up tries toText and must get text.
3106
+ export function encodeValue(geometry, options) {
3107
+ if (OUT === null) setupEncoder();
3108
+ BIN = binOut;
3109
+ try { return encode(geometry, options); } finally { BIN = null; }
3110
+ }
3111
+ export function encodeLineValue(points, options) {
3112
+ if (OUT === null) setupEncoder();
3113
+ BIN = binOut;
3114
+ try { return encodeLine(points, options); } finally { BIN = null; }
3115
+ }
3116
+
3117
+ // canonical() for a value: the canonical value for what it decodes to, at
3118
+ // its own settings or at options.to.
3119
+ export function canonicalValue(bytes, options) {
3120
+ if (notU8(bytes)) throw notBytes();
3121
+ try {
3122
+ let need = false, to;
3123
+ if (options !== undefined) { need = decodeOptions(options); if (options !== null) to = options.to; }
3124
+ if (to != null) { parseOpts(to); if (OP_FV === 'speed') throw new RangeError("canonical writes with favor 'size'"); }
3125
+ const g = bodyValue(bytes, bytes.length, need), prec = PREC, check = OPTS & 3, zp = NX > 0 ? POW.indexOf(XF[0]) : prec, mp = NX > 1 ? POW.indexOf(XF[1]) : prec;
3126
+ if (OUT === null) setupEncoder();
3127
+ BIN = binOut;
3128
+ if (to != null) return canonEncode(g, OP_P, OP_ZP < 0 ? OP_P : OP_ZP, OP_MP < 0 ? OP_P : OP_MP, OP_CS, false);
3129
+ return canonEncode(g, prec, zp, mp, check, false);
3130
+ } catch (e) { throw asValue(e); } finally { BIN = null; CAPL = DEF_LEN; CAPP = DEF_POS; }
3131
+ }
3132
+
3133
+ // The value for a string (SPEC.md section 23.6). How many bytes it has
3134
+ // depends on where the string's geometry ends, and only decoding says where,
3135
+ // so the string is decoded, with decode's options, and what decode refuses
3136
+ // this refuses with the same code. Its bits are in the decode buffer by
3137
+ // then, 8 to a byte with zero bits after them: the value, but for its check.
3138
+ function valueFor(str, options) {
3139
+ if (typeof str !== 'string') throw new TypeError('Expected a string');
3140
+ try {
3141
+ const need = options != null && decodeOptions(options), len = str.length;
3142
+ if (len > CAPL) throw over(0);
3143
+ const type = decodeHeader(str, len, 0, need), d = DD, check = OPTS & 3;
3144
+ if (len > CAPP / 24) { LEFT = CAPP; bodyCounted(d, type, 0, POW[PREC], PREC); } else decodeBody(d, type, 0, POW[PREC], PREC);
3145
+ checkEnded(d);
3146
+ const n = (SP + 7) >> 3, src = bufOf(d), out = new Uint8Array(n + (check << 1));
3147
+ copyTo(out, src, n);
3148
+ if (check) putCrc(out, n, check);
3149
+ return out;
3150
+ } finally { CAPL = DEF_LEN; CAPP = DEF_POS; }
3151
+ }
3152
+ export { valueFor as toValue };
3153
+
3154
+ // The string for a value: the same the other way. The value's bits go to
3155
+ // the encoder's buffer, and toText writes them as it writes any string.
3156
+ export function fromValue(bytes, options) {
3157
+ if (notU8(bytes)) throw notBytes();
3158
+ try {
3159
+ bodyValue(bytes, bytes.length, options != null && decodeOptions(options));
3160
+ const n = SP >> 3, bits = SP & 7, check = OPTS & 3, src = bufOf(DD);
3161
+ if (OUT === null) setupEncoder();
3162
+ WO = 0; room(n);
3163
+ const o = OUT;
3164
+ copyTo(o, src, n);
3165
+ WO = n; WN = bits; WACC = bits ? src[n] >>> (8 - bits) : 0;
3166
+ const s = toText();
3167
+ return check ? addSum(s, check) : s;
3168
+ } catch (e) { throw asValue(e); } finally { CAPL = DEF_LEN; CAPP = DEF_POS; }
3169
+ }