-
Notifications
You must be signed in to change notification settings - Fork 4
/
stringMatcher.ts
159 lines (145 loc) · 3.84 KB
/
stringMatcher.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// forked from https://github.com/joonhocho/superwild
class Matcher {
private wchar;
private pattern;
private segments = [];
private starCount = 0;
private minLength = 0;
private maxLength = 0;
private segStartIndex = 0;
constructor(pattern: string, wchar: string = "*") {
this.wchar = wchar;
this.pattern = pattern;
for (let i = 0, len = pattern.length; i < len; i += 1) {
const char = pattern[i];
if (char === wchar) {
this.starCount += 1;
if (i > this.segStartIndex) {
this.segments.push(pattern.substring(this.segStartIndex, i));
}
this.segments.push(char);
this.segStartIndex = i + 1;
}
}
if (this.segStartIndex < pattern.length) {
this.segments.push(pattern.substring(this.segStartIndex));
}
if (this.starCount) {
this.minLength = pattern.length - this.starCount;
this.maxLength = Infinity;
} else {
this.maxLength = this.minLength = pattern.length;
}
}
match(match: string): boolean {
if (this.pattern === this.wchar) {
return true;
}
if (this.segments.length === 0) {
return this.pattern === match;
}
const { length } = match;
if (length < this.minLength || length > this.maxLength) {
return false;
}
let segLeftIndex = 0;
let segRightIndex = this.segments.length - 1;
let rightPos = match.length - 1;
let rightIsStar = false;
while (true) {
const segment = this.segments[segRightIndex];
segRightIndex -= 1;
if (segment === this.wchar) {
rightIsStar = true;
} else {
const lastIndex = rightPos + 1 - segment.length;
const index = match.lastIndexOf(segment, lastIndex);
if (index === -1 || index > lastIndex) {
return false;
}
if (rightIsStar) {
rightPos = index - 1;
rightIsStar = false;
} else {
if (index !== lastIndex) {
return false;
}
rightPos -= segment.length;
}
}
if (segLeftIndex > segRightIndex) {
break;
}
}
return true;
}
}
export default Matcher;
const segments = [];
export function Match(pattern: string, match: string, wchar: string = "*") {
if (pattern === wchar) {
return true;
}
segments.length = 0;
let starCount = 0;
let minLength = 0;
let maxLength = 0;
let segStartIndex = 0;
for (let i = 0, len = pattern.length; i < len; i += 1) {
const char = pattern[i];
if (char === wchar) {
starCount += 1;
if (i > segStartIndex) {
segments.push(pattern.substring(segStartIndex, i));
}
segments.push(char);
segStartIndex = i + 1;
}
}
if (segStartIndex < pattern.length) {
segments.push(pattern.substring(segStartIndex));
}
if (starCount) {
minLength = pattern.length - starCount;
maxLength = Infinity;
} else {
maxLength = minLength = pattern.length;
}
if (segments.length === 0) {
return pattern === match;
}
const length = match.length;
if (length < minLength || length > maxLength) {
return false;
}
let segLeftIndex = 0;
let segRightIndex = segments.length - 1;
let rightPos = match.length - 1;
let rightIsStar = false;
while (true) {
const segment = segments[segRightIndex];
segRightIndex -= 1;
if (segment === wchar) {
rightIsStar = true;
} else {
const lastIndex = rightPos + 1 - segment.length;
const index = match.lastIndexOf(segment, lastIndex);
if (index === -1 || index > lastIndex) {
return false;
}
if (rightIsStar) {
rightPos = index - 1;
rightIsStar = false;
} else {
if (index !== lastIndex) {
return false;
}
rightPos -= segment.length;
}
}
if (segLeftIndex > segRightIndex) {
break;
}
}
return true;
}