@duoquote/polytini 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +120 -120
- package/dist/types/cli.d.cts +2 -2
- package/dist/types/deckgl.d.cts +72 -60
- package/dist/types/flexible.d.cts +92 -80
- package/dist/types/geojson.jsr.d.cts +111 -0
- package/dist/types/google.d.cts +48 -48
- package/dist/types/google.jsr.d.cts +72 -60
- package/dist/types/index.d.cts +109 -87
- package/dist/types/leaflet-auto.d.cts +4 -4
- package/dist/types/leaflet-auto.jsr.d.cts +2 -2
- package/dist/types/leaflet.d.cts +39 -39
- package/dist/types/leaflet.jsr.d.cts +15 -13
- package/dist/types/mapbox.d.cts +50 -50
- package/dist/types/mapbox.jsr.d.cts +5 -0
- package/dist/types/maplibre.d.cts +54 -54
- package/dist/types/maplibre.jsr.d.cts +92 -0
- package/dist/types/openlayers.d.cts +23 -23
- package/dist/types/openlayers.jsr.d.cts +42 -0
- package/dist/types/presets.d.cts +70 -66
- package/dist/types/turf.d.cts +33 -33
- package/dist/types/turf.jsr.d.cts +46 -0
- package/dist/types/wasm.d.cts +42 -33
- package/package.json +200 -200
- package/src/browser.js +15 -15
- package/src/bun.js +4 -4
- package/src/cli-lib.js +160 -160
- package/src/cli.js +7 -7
- package/src/core.js +268 -268
- package/src/deckgl.js +176 -176
- package/src/deno.js +12 -12
- package/src/error.js +15 -15
- package/src/fast.js +179 -179
- package/src/flexible.js +283 -283
- package/src/google.js +70 -70
- package/src/leaflet-auto.js +11 -11
- package/src/leaflet.js +44 -44
- package/src/mapbox.js +10 -10
- package/src/maplibre.js +57 -57
- package/src/node.js +17 -17
- package/src/openlayers.js +172 -172
- package/src/presets.js +242 -242
- package/src/text.js +111 -111
- package/src/turf.js +80 -80
- package/src/wasm-bytes.js +2 -2
- package/src/wasm.js +235 -235
- package/src/web.js +9 -9
- package/types/cli.d.ts +2 -2
- package/types/deckgl.d.ts +72 -60
- package/types/flexible.d.ts +92 -80
- package/types/geojson.jsr.d.ts +111 -0
- package/types/google.d.ts +48 -48
- package/types/google.jsr.d.ts +72 -60
- package/types/index.d.ts +109 -87
- package/types/leaflet-auto.d.ts +4 -4
- package/types/leaflet-auto.jsr.d.ts +2 -2
- package/types/leaflet.d.ts +39 -39
- package/types/leaflet.jsr.d.ts +15 -13
- package/types/mapbox.d.ts +50 -50
- package/types/mapbox.jsr.d.ts +5 -0
- package/types/maplibre.d.ts +54 -54
- package/types/maplibre.jsr.d.ts +92 -0
- package/types/openlayers.d.ts +23 -23
- package/types/openlayers.jsr.d.ts +42 -0
- package/types/presets.d.ts +70 -66
- package/types/turf.d.ts +33 -33
- package/types/turf.jsr.d.ts +46 -0
- package/types/wasm.d.ts +42 -33
package/src/fast.js
CHANGED
|
@@ -1,179 +1,179 @@
|
|
|
1
|
-
// Fast paths for runtimes with Node's Buffer: Node and Bun. Each choice here
|
|
2
|
-
// was measured with the benchmark fixtures (see "Platform notes" in the
|
|
3
|
-
// README):
|
|
4
|
-
//
|
|
5
|
-
// - Decode copies the string into a scratch buffer with `ucs2Write` and
|
|
6
|
-
// parses the Uint16Array. On V8, charCodeAt in a hot loop re-checks the
|
|
7
|
-
// string representation on every call, and the copy is close to a memcpy.
|
|
8
|
-
// `latin1Write` is a bit faster but keeps only the low byte, so U+0151
|
|
9
|
-
// would turn into a valid "Q". UTF-16 keeps strict mode exact.
|
|
10
|
-
// - The decoder counts the points first (two characters per step, through
|
|
11
|
-
// a Uint32Array view), so the output array is allocated at its exact size.
|
|
12
|
-
// Growing it with push, or trimming an oversized one, both measured slower.
|
|
13
|
-
// - Encode writes bytes into a scratch buffer and makes the string with one
|
|
14
|
-
// `latin1Slice` call. That was 2 to 4 times faster than building the string
|
|
15
|
-
// with += on both V8 and JavaScriptCore, and the result is a flat string.
|
|
16
|
-
// - The character loops branch on "is there another chunk". A branchless
|
|
17
|
-
// version was faster on the real-world lines, where 1 and 2 character
|
|
18
|
-
// values alternate at random, but slower on the GPS tracks, where they come
|
|
19
|
-
// in runs the branch predictor learns. Branchy won 5 of the 6 columns.
|
|
20
|
-
// - Strings under 64 characters skip the copy and use the core's loop.
|
|
21
|
-
import { opts, st, LIMIT, slowDecode, slowEncode, decodeLoop, flip } from './core.js';
|
|
22
|
-
|
|
23
|
-
// Scratch memory is reused between calls up to this size. Bigger inputs get
|
|
24
|
-
// a temporary buffer, so one huge line doesn't pin megabytes for good.
|
|
25
|
-
const KEEP = 1 << 20;
|
|
26
|
-
|
|
27
|
-
// `grow`: above this many points, build the result by appending instead of
|
|
28
|
-
// allocating it at its final size. See node.js for why.
|
|
29
|
-
export function fast(Buffer, grow = Infinity) {
|
|
30
|
-
const ucs2Write = Buffer.prototype.ucs2Write, latin1Slice = Buffer.prototype.latin1Slice;
|
|
31
|
-
// The kept scratch buffer and its views. allocUnsafeSlow gives each buffer
|
|
32
|
-
// its own ArrayBuffer at offset 0, so the wider views are aligned.
|
|
33
|
-
let kept, k16, k32, u16;
|
|
34
|
-
const views = (buf) => [buf, new Uint16Array(buf.buffer, 0, buf.length >> 1), new Uint32Array(buf.buffer, 0, buf.length >> 2)];
|
|
35
|
-
[kept, k16, k32] = views(Buffer.allocUnsafeSlow(1 << 14));
|
|
36
|
-
const scratch = (bytes) => (bytes <= kept.length ? kept : bytes <= KEEP ? ([kept, k16, k32] = views(Buffer.allocUnsafeSlow(bytes)))[0] : Buffer.allocUnsafeSlow(bytes));
|
|
37
|
-
|
|
38
|
-
// Copies str into scratch as UTF-16, points u16 at it, and returns the
|
|
39
|
-
// number of values.
|
|
40
|
-
function load(str, len) {
|
|
41
|
-
const buf = scratch((len + 4) * 2 + 8);
|
|
42
|
-
ucs2Write.call(buf, str, 0);
|
|
43
|
-
let u32 = k32;
|
|
44
|
-
if (buf == kept) u16 = k16;
|
|
45
|
-
else [, u16, u32] = views(buf);
|
|
46
|
-
// Padding: '?' is a complete zero value, so a truncated string stops
|
|
47
|
-
// inside the buffer and leaves the index past len.
|
|
48
|
-
u16[len] = u16[len + 1] = u16[len + 2] = 63;
|
|
49
|
-
// Every value ends with one character below code 95. Count the others,
|
|
50
|
-
// which have bit 5 set after adding 1, two characters per word.
|
|
51
|
-
let more = 0;
|
|
52
|
-
for (let k = 0, w = (len + 1) >> 1; k < w; k++) {
|
|
53
|
-
const y = ((u32[k] + 0x10001) >>> 5) & 0x10001;
|
|
54
|
-
more += (y & 0xffff) + (y >>> 16);
|
|
55
|
-
}
|
|
56
|
-
return len - more;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
// decodeLoop over the UTF-16 copy, with an exactly sized output. Returns
|
|
60
|
-
// null for slowDecode. Tuples and flat output are separate loops here: one
|
|
61
|
-
// shared loop with a per-point branch measured about 20% slower on
|
|
62
|
-
// JavaScriptCore at 10,000 points.
|
|
63
|
-
function big(str, f) {
|
|
64
|
-
const len = str.length, want = load(str, len) >> 1, b = u16, out = want > grow ? [] : Array(want);
|
|
65
|
-
let i = 0, n = 0, lat = 0, lng = 0, bad = 0, c, v, s;
|
|
66
|
-
while (i < len) {
|
|
67
|
-
c = b[i++] - 63;
|
|
68
|
-
bad |= c;
|
|
69
|
-
v = c & 31;
|
|
70
|
-
for (s = 5; c >= 32; s += 5) {
|
|
71
|
-
c = b[i++] - 63;
|
|
72
|
-
bad |= c | (25 - s);
|
|
73
|
-
v |= (c & 31) << s;
|
|
74
|
-
}
|
|
75
|
-
lat += (v >>> 1) ^ -(v & 1);
|
|
76
|
-
c = b[i++] - 63;
|
|
77
|
-
bad |= c;
|
|
78
|
-
v = c & 31;
|
|
79
|
-
for (s = 5; c >= 32; s += 5) {
|
|
80
|
-
c = b[i++] - 63;
|
|
81
|
-
bad |= c | (25 - s);
|
|
82
|
-
v |= (c & 31) << s;
|
|
83
|
-
}
|
|
84
|
-
lng += (v >>> 1) ^ -(v & 1);
|
|
85
|
-
out[n++] = [lat / f, lng / f];
|
|
86
|
-
}
|
|
87
|
-
// The count is exact for valid input, and invalid input stops here.
|
|
88
|
-
return bad & ~63 || i != len ? null : out;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
function bigFlat(str, f, sw) {
|
|
92
|
-
const len = str.length, out = new Float64Array(load(str, len) & ~1), b = u16, a = +sw;
|
|
93
|
-
let i = 0, n = 0, lat = 0, lng = 0, bad = 0, c, v, s;
|
|
94
|
-
while (i < len) {
|
|
95
|
-
c = b[i++] - 63;
|
|
96
|
-
bad |= c;
|
|
97
|
-
v = c & 31;
|
|
98
|
-
for (s = 5; c >= 32; s += 5) {
|
|
99
|
-
c = b[i++] - 63;
|
|
100
|
-
bad |= c | (25 - s);
|
|
101
|
-
v |= (c & 31) << s;
|
|
102
|
-
}
|
|
103
|
-
lat += (v >>> 1) ^ -(v & 1);
|
|
104
|
-
c = b[i++] - 63;
|
|
105
|
-
bad |= c;
|
|
106
|
-
v = c & 31;
|
|
107
|
-
for (s = 5; c >= 32; s += 5) {
|
|
108
|
-
c = b[i++] - 63;
|
|
109
|
-
bad |= c | (25 - s);
|
|
110
|
-
v |= (c & 31) << s;
|
|
111
|
-
}
|
|
112
|
-
lng += (v >>> 1) ^ -(v & 1);
|
|
113
|
-
out[n + a] = lat / f;
|
|
114
|
-
out[n + 1 - a] = lng / f;
|
|
115
|
-
n += 2;
|
|
116
|
-
}
|
|
117
|
-
return bad & ~63 || i != len ? null : out;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
const decodeWith = (str, o, flat) => {
|
|
121
|
-
const f = opts(o);
|
|
122
|
-
if (st.dims != 2 || typeof str != 'string') return slowDecode(str, f, flat);
|
|
123
|
-
const out = str.length < 64 ? decodeLoop(str, f, st.swap, flat) : flat ? bigFlat(str, f, st.swap) : big(str, f);
|
|
124
|
-
return out ? (st.swap && !flat ? flip(out) : out) : slowDecode(str, f, flat);
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
// Writes bytes into scratch, 7 per value at most below LIMIT. Returns null
|
|
128
|
-
// for slowEncode.
|
|
129
|
-
function encodeLoop(src, f, a, flat) {
|
|
130
|
-
const n = flat ? src.length >> 1 : src.length, buf = scratch(n * 14 + 8);
|
|
131
|
-
let i = 0, k = 0, la = 0, lb = 0, x, y, p, q, z, pt;
|
|
132
|
-
try {
|
|
133
|
-
for (; i < n; i++) {
|
|
134
|
-
if (flat) {
|
|
135
|
-
x = src[i * 2 + a];
|
|
136
|
-
y = src[i * 2 + 1 - a];
|
|
137
|
-
} else {
|
|
138
|
-
pt = src[i];
|
|
139
|
-
x = pt[a];
|
|
140
|
-
y = pt[1 - a];
|
|
141
|
-
}
|
|
142
|
-
if (typeof x != 'number' || typeof y != 'number' || !((x *= f) < LIMIT && x > -LIMIT && (y *= f) < LIMIT && y > -LIMIT)) return null;
|
|
143
|
-
// Round half away from zero. floor(|x| + 0.5) is exact here except for
|
|
144
|
-
// |x| = 0.49999999999999994, where the addition rounds up to 1, so any
|
|
145
|
-
// |x| < 0.5 is set to 0 afterwards. Math.round would be exact on its own,
|
|
146
|
-
// but it measured 40% slower on JavaScriptCore.
|
|
147
|
-
p = x < 0 ? -((0.5 - x) | 0) : (x + 0.5) | 0;
|
|
148
|
-
q = y < 0 ? -((0.5 - y) | 0) : (y + 0.5) | 0;
|
|
149
|
-
if (x < 0.5 && x > -0.5) p = 0;
|
|
150
|
-
if (y < 0.5 && y > -0.5) q = 0;
|
|
151
|
-
z = p - la;
|
|
152
|
-
z = (z << 1) ^ (z >> 31);
|
|
153
|
-
for (; z >= 32; z >>>= 5) buf[k++] = (z & 31) + 95;
|
|
154
|
-
buf[k++] = z + 63;
|
|
155
|
-
z = q - lb;
|
|
156
|
-
z = (z << 1) ^ (z >> 31);
|
|
157
|
-
for (; z >= 32; z >>>= 5) buf[k++] = (z & 31) + 95;
|
|
158
|
-
buf[k++] = z + 63;
|
|
159
|
-
la = p;
|
|
160
|
-
lb = q;
|
|
161
|
-
}
|
|
162
|
-
} catch {
|
|
163
|
-
return null;
|
|
164
|
-
}
|
|
165
|
-
return latin1Slice.call(buf, 0, k);
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
const encodeWith = (src, o, flat) => {
|
|
169
|
-
const f = opts(o);
|
|
170
|
-
return (st.dims == 2 && f <= 1e6 && (flat ? src != null && !(src.length % 2) : Array.isArray(src)) ? encodeLoop(src, f, +st.swap, flat) : null) ?? slowEncode(src, f, flat);
|
|
171
|
-
};
|
|
172
|
-
|
|
173
|
-
return {
|
|
174
|
-
decode: (str, o) => decodeWith(str, o, 0),
|
|
175
|
-
decodeFlat: (str, o) => decodeWith(str, o, 1),
|
|
176
|
-
encode: (src, o) => encodeWith(src, o, 0),
|
|
177
|
-
encodeFlat: (src, o) => encodeWith(src, o, 1),
|
|
178
|
-
};
|
|
179
|
-
}
|
|
1
|
+
// Fast paths for runtimes with Node's Buffer: Node and Bun. Each choice here
|
|
2
|
+
// was measured with the benchmark fixtures (see "Platform notes" in the
|
|
3
|
+
// README):
|
|
4
|
+
//
|
|
5
|
+
// - Decode copies the string into a scratch buffer with `ucs2Write` and
|
|
6
|
+
// parses the Uint16Array. On V8, charCodeAt in a hot loop re-checks the
|
|
7
|
+
// string representation on every call, and the copy is close to a memcpy.
|
|
8
|
+
// `latin1Write` is a bit faster but keeps only the low byte, so U+0151
|
|
9
|
+
// would turn into a valid "Q". UTF-16 keeps strict mode exact.
|
|
10
|
+
// - The decoder counts the points first (two characters per step, through
|
|
11
|
+
// a Uint32Array view), so the output array is allocated at its exact size.
|
|
12
|
+
// Growing it with push, or trimming an oversized one, both measured slower.
|
|
13
|
+
// - Encode writes bytes into a scratch buffer and makes the string with one
|
|
14
|
+
// `latin1Slice` call. That was 2 to 4 times faster than building the string
|
|
15
|
+
// with += on both V8 and JavaScriptCore, and the result is a flat string.
|
|
16
|
+
// - The character loops branch on "is there another chunk". A branchless
|
|
17
|
+
// version was faster on the real-world lines, where 1 and 2 character
|
|
18
|
+
// values alternate at random, but slower on the GPS tracks, where they come
|
|
19
|
+
// in runs the branch predictor learns. Branchy won 5 of the 6 columns.
|
|
20
|
+
// - Strings under 64 characters skip the copy and use the core's loop.
|
|
21
|
+
import { opts, st, LIMIT, slowDecode, slowEncode, decodeLoop, flip } from './core.js';
|
|
22
|
+
|
|
23
|
+
// Scratch memory is reused between calls up to this size. Bigger inputs get
|
|
24
|
+
// a temporary buffer, so one huge line doesn't pin megabytes for good.
|
|
25
|
+
const KEEP = 1 << 20;
|
|
26
|
+
|
|
27
|
+
// `grow`: above this many points, build the result by appending instead of
|
|
28
|
+
// allocating it at its final size. See node.js for why.
|
|
29
|
+
export function fast(Buffer, grow = Infinity) {
|
|
30
|
+
const ucs2Write = Buffer.prototype.ucs2Write, latin1Slice = Buffer.prototype.latin1Slice;
|
|
31
|
+
// The kept scratch buffer and its views. allocUnsafeSlow gives each buffer
|
|
32
|
+
// its own ArrayBuffer at offset 0, so the wider views are aligned.
|
|
33
|
+
let kept, k16, k32, u16;
|
|
34
|
+
const views = (buf) => [buf, new Uint16Array(buf.buffer, 0, buf.length >> 1), new Uint32Array(buf.buffer, 0, buf.length >> 2)];
|
|
35
|
+
[kept, k16, k32] = views(Buffer.allocUnsafeSlow(1 << 14));
|
|
36
|
+
const scratch = (bytes) => (bytes <= kept.length ? kept : bytes <= KEEP ? ([kept, k16, k32] = views(Buffer.allocUnsafeSlow(bytes)))[0] : Buffer.allocUnsafeSlow(bytes));
|
|
37
|
+
|
|
38
|
+
// Copies str into scratch as UTF-16, points u16 at it, and returns the
|
|
39
|
+
// number of values.
|
|
40
|
+
function load(str, len) {
|
|
41
|
+
const buf = scratch((len + 4) * 2 + 8);
|
|
42
|
+
ucs2Write.call(buf, str, 0);
|
|
43
|
+
let u32 = k32;
|
|
44
|
+
if (buf == kept) u16 = k16;
|
|
45
|
+
else [, u16, u32] = views(buf);
|
|
46
|
+
// Padding: '?' is a complete zero value, so a truncated string stops
|
|
47
|
+
// inside the buffer and leaves the index past len.
|
|
48
|
+
u16[len] = u16[len + 1] = u16[len + 2] = 63;
|
|
49
|
+
// Every value ends with one character below code 95. Count the others,
|
|
50
|
+
// which have bit 5 set after adding 1, two characters per word.
|
|
51
|
+
let more = 0;
|
|
52
|
+
for (let k = 0, w = (len + 1) >> 1; k < w; k++) {
|
|
53
|
+
const y = ((u32[k] + 0x10001) >>> 5) & 0x10001;
|
|
54
|
+
more += (y & 0xffff) + (y >>> 16);
|
|
55
|
+
}
|
|
56
|
+
return len - more;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// decodeLoop over the UTF-16 copy, with an exactly sized output. Returns
|
|
60
|
+
// null for slowDecode. Tuples and flat output are separate loops here: one
|
|
61
|
+
// shared loop with a per-point branch measured about 20% slower on
|
|
62
|
+
// JavaScriptCore at 10,000 points.
|
|
63
|
+
function big(str, f) {
|
|
64
|
+
const len = str.length, want = load(str, len) >> 1, b = u16, out = want > grow ? [] : Array(want);
|
|
65
|
+
let i = 0, n = 0, lat = 0, lng = 0, bad = 0, c, v, s;
|
|
66
|
+
while (i < len) {
|
|
67
|
+
c = b[i++] - 63;
|
|
68
|
+
bad |= c;
|
|
69
|
+
v = c & 31;
|
|
70
|
+
for (s = 5; c >= 32; s += 5) {
|
|
71
|
+
c = b[i++] - 63;
|
|
72
|
+
bad |= c | (25 - s);
|
|
73
|
+
v |= (c & 31) << s;
|
|
74
|
+
}
|
|
75
|
+
lat += (v >>> 1) ^ -(v & 1);
|
|
76
|
+
c = b[i++] - 63;
|
|
77
|
+
bad |= c;
|
|
78
|
+
v = c & 31;
|
|
79
|
+
for (s = 5; c >= 32; s += 5) {
|
|
80
|
+
c = b[i++] - 63;
|
|
81
|
+
bad |= c | (25 - s);
|
|
82
|
+
v |= (c & 31) << s;
|
|
83
|
+
}
|
|
84
|
+
lng += (v >>> 1) ^ -(v & 1);
|
|
85
|
+
out[n++] = [lat / f, lng / f];
|
|
86
|
+
}
|
|
87
|
+
// The count is exact for valid input, and invalid input stops here.
|
|
88
|
+
return bad & ~63 || i != len ? null : out;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function bigFlat(str, f, sw) {
|
|
92
|
+
const len = str.length, out = new Float64Array(load(str, len) & ~1), b = u16, a = +sw;
|
|
93
|
+
let i = 0, n = 0, lat = 0, lng = 0, bad = 0, c, v, s;
|
|
94
|
+
while (i < len) {
|
|
95
|
+
c = b[i++] - 63;
|
|
96
|
+
bad |= c;
|
|
97
|
+
v = c & 31;
|
|
98
|
+
for (s = 5; c >= 32; s += 5) {
|
|
99
|
+
c = b[i++] - 63;
|
|
100
|
+
bad |= c | (25 - s);
|
|
101
|
+
v |= (c & 31) << s;
|
|
102
|
+
}
|
|
103
|
+
lat += (v >>> 1) ^ -(v & 1);
|
|
104
|
+
c = b[i++] - 63;
|
|
105
|
+
bad |= c;
|
|
106
|
+
v = c & 31;
|
|
107
|
+
for (s = 5; c >= 32; s += 5) {
|
|
108
|
+
c = b[i++] - 63;
|
|
109
|
+
bad |= c | (25 - s);
|
|
110
|
+
v |= (c & 31) << s;
|
|
111
|
+
}
|
|
112
|
+
lng += (v >>> 1) ^ -(v & 1);
|
|
113
|
+
out[n + a] = lat / f;
|
|
114
|
+
out[n + 1 - a] = lng / f;
|
|
115
|
+
n += 2;
|
|
116
|
+
}
|
|
117
|
+
return bad & ~63 || i != len ? null : out;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const decodeWith = (str, o, flat) => {
|
|
121
|
+
const f = opts(o);
|
|
122
|
+
if (st.dims != 2 || typeof str != 'string') return slowDecode(str, f, flat);
|
|
123
|
+
const out = str.length < 64 ? decodeLoop(str, f, st.swap, flat) : flat ? bigFlat(str, f, st.swap) : big(str, f);
|
|
124
|
+
return out ? (st.swap && !flat ? flip(out) : out) : slowDecode(str, f, flat);
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
// Writes bytes into scratch, 7 per value at most below LIMIT. Returns null
|
|
128
|
+
// for slowEncode.
|
|
129
|
+
function encodeLoop(src, f, a, flat) {
|
|
130
|
+
const n = flat ? src.length >> 1 : src.length, buf = scratch(n * 14 + 8);
|
|
131
|
+
let i = 0, k = 0, la = 0, lb = 0, x, y, p, q, z, pt;
|
|
132
|
+
try {
|
|
133
|
+
for (; i < n; i++) {
|
|
134
|
+
if (flat) {
|
|
135
|
+
x = src[i * 2 + a];
|
|
136
|
+
y = src[i * 2 + 1 - a];
|
|
137
|
+
} else {
|
|
138
|
+
pt = src[i];
|
|
139
|
+
x = pt[a];
|
|
140
|
+
y = pt[1 - a];
|
|
141
|
+
}
|
|
142
|
+
if (typeof x != 'number' || typeof y != 'number' || !((x *= f) < LIMIT && x > -LIMIT && (y *= f) < LIMIT && y > -LIMIT)) return null;
|
|
143
|
+
// Round half away from zero. floor(|x| + 0.5) is exact here except for
|
|
144
|
+
// |x| = 0.49999999999999994, where the addition rounds up to 1, so any
|
|
145
|
+
// |x| < 0.5 is set to 0 afterwards. Math.round would be exact on its own,
|
|
146
|
+
// but it measured 40% slower on JavaScriptCore.
|
|
147
|
+
p = x < 0 ? -((0.5 - x) | 0) : (x + 0.5) | 0;
|
|
148
|
+
q = y < 0 ? -((0.5 - y) | 0) : (y + 0.5) | 0;
|
|
149
|
+
if (x < 0.5 && x > -0.5) p = 0;
|
|
150
|
+
if (y < 0.5 && y > -0.5) q = 0;
|
|
151
|
+
z = p - la;
|
|
152
|
+
z = (z << 1) ^ (z >> 31);
|
|
153
|
+
for (; z >= 32; z >>>= 5) buf[k++] = (z & 31) + 95;
|
|
154
|
+
buf[k++] = z + 63;
|
|
155
|
+
z = q - lb;
|
|
156
|
+
z = (z << 1) ^ (z >> 31);
|
|
157
|
+
for (; z >= 32; z >>>= 5) buf[k++] = (z & 31) + 95;
|
|
158
|
+
buf[k++] = z + 63;
|
|
159
|
+
la = p;
|
|
160
|
+
lb = q;
|
|
161
|
+
}
|
|
162
|
+
} catch {
|
|
163
|
+
return null;
|
|
164
|
+
}
|
|
165
|
+
return latin1Slice.call(buf, 0, k);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const encodeWith = (src, o, flat) => {
|
|
169
|
+
const f = opts(o);
|
|
170
|
+
return (st.dims == 2 && f <= 1e6 && (flat ? src != null && !(src.length % 2) : Array.isArray(src)) ? encodeLoop(src, f, +st.swap, flat) : null) ?? slowEncode(src, f, flat);
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
return {
|
|
174
|
+
decode: (str, o) => decodeWith(str, o, 0),
|
|
175
|
+
decodeFlat: (str, o) => decodeWith(str, o, 1),
|
|
176
|
+
encode: (src, o) => encodeWith(src, o, 0),
|
|
177
|
+
encodeFlat: (src, o) => encodeWith(src, o, 1),
|
|
178
|
+
};
|
|
179
|
+
}
|