Skip to content

Commit b97d1e9

Browse files
committed
improve wildcmp
1 parent f5fecbf commit b97d1e9

File tree

1 file changed

+12
-32
lines changed

1 file changed

+12
-32
lines changed

webextensions/edge/background.js

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,41 +20,21 @@ const ALARM_MINUTES = 0.5;
2020
* true
2121
*/
2222
function wildcmp(wild, string) {
23-
let i = 0;
24-
let j = 0;
25-
let mp, cp;
26-
27-
while ((j < string.length) && (wild[i] != '*')) {
28-
if ((wild[i] != string[j]) && (wild[i] != '?')) {
29-
return 0;
30-
}
31-
i += 1;
32-
j += 1;
33-
}
34-
while (j < string.length) {
35-
if (wild[i] == '*') {
36-
i += 1;
37-
38-
if (i == wild.length) {
39-
return 1;
40-
}
41-
mp = i;
42-
cp = j + 1
43-
} else if ((wild[i] == string[j]) || (wild[i] == '?')) {
44-
i += 1;
45-
j += 1;
46-
} else {
47-
i = mp;
48-
j = cp;
49-
cp += 1;
50-
}
51-
}
52-
while (wild[i] == '*' && i < wild.length) {
53-
i += 1;
23+
if(!wild || !string) {
24+
return false;
5425
}
55-
return i >= wild.length;
26+
const pattern = wildcardToRegexp(wild);
27+
const regex = new RegExp(`^${pattern}$`, "i");
28+
return regex.test(string);
5629
};
5730

31+
function wildcardToRegexp(source) {
32+
// https://stackoverflow.com/questions/6300183/sanitize-string-of-regex-characters-before-regexp-build
33+
const sanitized = source.replace(/[#-.]|[[-^]|[?|{}]/g, "\\$&");
34+
const wildcardAccepted = sanitized.replace(/\\\*/g, ".*").replace(/\\\?/g, ".");
35+
return wildcardAccepted;
36+
}
37+
5838
/*
5939
* Observe WebRequests with config fetched from RepostConfirmationCanceler.
6040
*

0 commit comments

Comments
 (0)