@wrongstack/primitives 0.317.0 → 0.318.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/dist/index.js +52 -19
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -12,28 +12,41 @@ var DANGEROUS_PATTERNS = [
|
|
|
12
12
|
/\(\?<?[!=][^)]*[+*][^)]*\)/
|
|
13
13
|
];
|
|
14
14
|
function hasAmbiguousQuantifiedAlternation(pattern) {
|
|
15
|
+
let outerInClass = false;
|
|
15
16
|
for (let i = 0; i < pattern.length; i++) {
|
|
16
|
-
|
|
17
|
-
if (
|
|
17
|
+
const ch = pattern[i];
|
|
18
|
+
if (ch === "\\") {
|
|
19
|
+
i++;
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
if (outerInClass) {
|
|
23
|
+
if (ch === "]") outerInClass = false;
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
if (ch === "[") {
|
|
27
|
+
outerInClass = true;
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
if (ch !== "(") continue;
|
|
18
31
|
let depth = 0;
|
|
19
32
|
let inClass = false;
|
|
20
33
|
let j = i;
|
|
21
34
|
for (; j < pattern.length; j++) {
|
|
22
|
-
const
|
|
23
|
-
if (
|
|
35
|
+
const ch2 = pattern[j];
|
|
36
|
+
if (ch2 === "\\") {
|
|
24
37
|
j++;
|
|
25
38
|
continue;
|
|
26
39
|
}
|
|
27
40
|
if (inClass) {
|
|
28
|
-
if (
|
|
41
|
+
if (ch2 === "]") inClass = false;
|
|
29
42
|
continue;
|
|
30
43
|
}
|
|
31
|
-
if (
|
|
44
|
+
if (ch2 === "[") {
|
|
32
45
|
inClass = true;
|
|
33
46
|
continue;
|
|
34
47
|
}
|
|
35
|
-
if (
|
|
36
|
-
else if (
|
|
48
|
+
if (ch2 === "(") depth++;
|
|
49
|
+
else if (ch2 === ")") {
|
|
37
50
|
depth--;
|
|
38
51
|
if (depth === 0) break;
|
|
39
52
|
}
|
|
@@ -48,30 +61,30 @@ function hasAmbiguousQuantifiedAlternation(pattern) {
|
|
|
48
61
|
let d = 0;
|
|
49
62
|
let cls = false;
|
|
50
63
|
for (let k = 0; k < inner.length; k++) {
|
|
51
|
-
const
|
|
52
|
-
if (
|
|
53
|
-
current +=
|
|
64
|
+
const ch2 = inner[k];
|
|
65
|
+
if (ch2 === "\\") {
|
|
66
|
+
current += ch2 + (inner[k + 1] ?? "");
|
|
54
67
|
k++;
|
|
55
68
|
continue;
|
|
56
69
|
}
|
|
57
70
|
if (cls) {
|
|
58
|
-
if (
|
|
59
|
-
current +=
|
|
71
|
+
if (ch2 === "]") cls = false;
|
|
72
|
+
current += ch2;
|
|
60
73
|
continue;
|
|
61
74
|
}
|
|
62
|
-
if (
|
|
75
|
+
if (ch2 === "[") {
|
|
63
76
|
cls = true;
|
|
64
|
-
current +=
|
|
77
|
+
current += ch2;
|
|
65
78
|
continue;
|
|
66
79
|
}
|
|
67
|
-
if (
|
|
68
|
-
if (
|
|
69
|
-
if (
|
|
80
|
+
if (ch2 === "(") d++;
|
|
81
|
+
if (ch2 === ")") d--;
|
|
82
|
+
if (ch2 === "|" && d === 0) {
|
|
70
83
|
branches.push(current);
|
|
71
84
|
current = "";
|
|
72
85
|
continue;
|
|
73
86
|
}
|
|
74
|
-
current +=
|
|
87
|
+
current += ch2;
|
|
75
88
|
}
|
|
76
89
|
branches.push(current);
|
|
77
90
|
if (branches.length < 2) continue;
|
|
@@ -86,10 +99,30 @@ function hasAmbiguousQuantifiedAlternation(pattern) {
|
|
|
86
99
|
}
|
|
87
100
|
return false;
|
|
88
101
|
}
|
|
102
|
+
var COMPILED_CACHE = /* @__PURE__ */ new Map();
|
|
103
|
+
var CACHE_MAX_SIZE = 500;
|
|
89
104
|
function compileUserRegex(pattern, flags = "") {
|
|
90
105
|
if (typeof pattern !== "string") {
|
|
91
106
|
return { ok: false, reason: "pattern must be a string" };
|
|
92
107
|
}
|
|
108
|
+
const cacheKey = `${flags}\0${pattern}`;
|
|
109
|
+
const cached = COMPILED_CACHE.get(cacheKey);
|
|
110
|
+
if (cached !== void 0) {
|
|
111
|
+
return cached.ok ? { ok: true, regex: new RegExp(pattern, flags) } : cached;
|
|
112
|
+
}
|
|
113
|
+
if (COMPILED_CACHE.size >= CACHE_MAX_SIZE) {
|
|
114
|
+
let evicted = 0;
|
|
115
|
+
const target = Math.floor(CACHE_MAX_SIZE / 4);
|
|
116
|
+
for (const key of COMPILED_CACHE.keys()) {
|
|
117
|
+
COMPILED_CACHE.delete(key);
|
|
118
|
+
if (++evicted >= target) break;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
const result = compileUncached(pattern, flags);
|
|
122
|
+
COMPILED_CACHE.set(cacheKey, result);
|
|
123
|
+
return result.ok ? { ok: true, regex: new RegExp(pattern, flags) } : result;
|
|
124
|
+
}
|
|
125
|
+
function compileUncached(pattern, flags) {
|
|
93
126
|
if (pattern.length === 0) {
|
|
94
127
|
return { ok: false, reason: "pattern is empty" };
|
|
95
128
|
}
|