forked from dotnet/sourcelink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Glob.cs
251 lines (209 loc) · 10.2 KB
/
Glob.cs
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Implementation based on documentation:
// - https://git-scm.com/docs/gitignore
// - http://man7.org/linux/man-pages/man7/glob.7.html
// - https://research.swtch.com/glob
using System;
using System.Diagnostics;
namespace Microsoft.Build.Tasks.Git
{
// https://github.com/dotnet/corefx/issues/18922
// https://github.com/dotnet/corefx/issues/25873
internal static class Glob
{
internal static bool IsMatch(string pattern, string path, bool ignoreCase, bool matchWildCardWithDirectorySeparator)
{
int patternIndex = 0;
int pathIndex = 0;
// true if the next matching character must be the first character of a directory name
bool matchDirectoryNameStart = false;
bool stopAtPathSlash = false;
int nextSinglePatternIndex = -1;
int nextSinglePathIndex = -1;
int nextDoublePatternIndex = -1;
int nextDoublePathIndex = -1;
bool equal(char x, char y)
=> x == y || ignoreCase && char.ToLowerInvariant(x) == char.ToLowerInvariant(y);
while (patternIndex < pattern.Length)
{
var c = pattern[patternIndex++];
if (c == '*')
{
// "a/**/*" does not match "a", although it might appear from the spec that it should.
bool isDoubleAsterisk = patternIndex < pattern.Length && pattern[patternIndex] == '*' &&
(patternIndex == pattern.Length - 1 || pattern[patternIndex + 1] == '/') &&
(patternIndex == 1 || pattern[patternIndex - 2] == '/');
if (isDoubleAsterisk)
{
// trailing "/**"
if (patternIndex == pattern.Length - 1)
{
// remaining path definitely matches
return true;
}
// At this point the initial '/' (if any) is already matched.
Debug.Assert(pattern[patternIndex] == '*' && pattern[patternIndex + 1] == '/');
// Continue matching remainder of the pattern following "**/" with the remainder of the path.
// The next path character only matches if it is preceded by '/'.
// Consider the following cases
// "**/a*" ~ "abc"
// "**/b" ~ "x/yb/b" (do not match the first 'b', only the second one)
// "**/?" ~ "x/yz/u" (do not match 'y', 'z'; match 'u')
// "a/**/b*" ~ "a/bcd"
// "a/**/b" ~ "a/x/yb/b" (do not match the first 'b', only the second one)
patternIndex += 2;
stopAtPathSlash = false;
matchDirectoryNameStart = true;
}
else
{
// trailing "*"
if (patternIndex == pattern.Length)
{
return matchWildCardWithDirectorySeparator || path.IndexOf('/', pathIndex) == -1;
}
stopAtPathSlash = !matchWildCardWithDirectorySeparator;
matchDirectoryNameStart = false;
}
// If the rest of the pattern fails to match the rest of the path, we restart matching at the following indices.
// A sequence of consecutive resets is effectively searching the path for a substring that matches the span of the pattern
// in between the current wildcard and the next one.
//
// For example, consider matching pattern "A/**/B/**/C/*.D" to path "A/z/u/B/q/r/C/z.D".
// Processing the first ** wildcard keeps resetting until the pattern is alligned with "/B/" in the path (wildcard matches "z/u").
// Processing the next ** wildcard keeps resetting until the pattern is alligned with "/C/" in the path (wildcard matches "q/r").
// Finally, processing the * wildcard aligns on ".D" and the wildcard matches "z".
//
// If ** and * differ in matching '/' (matchWildCardWithDirectorySeparator is false) we need to reset them independently.
// Consider pattern "A/**/B*C*D/**/E" matching to "A/u/v/BaaCaaX/u/BoCoD/u/E".
// If we aligned on substring "/B" in between the first ** and the next * we would not match the path correctly.
// Instead, we need to align on the sub-pattern "/B*C*D/" in between the first and the second **.
if (stopAtPathSlash)
{
nextSinglePatternIndex = patternIndex;
nextSinglePathIndex = pathIndex + 1;
}
else
{
nextDoublePatternIndex = patternIndex;
nextDoublePathIndex = pathIndex + 1;
}
continue;
}
bool matching;
if (c == '?')
{
// "?" matches any character except for "/" (when matchWildCardWithDirectorySeparator is false)
matching = pathIndex < path.Length && (matchWildCardWithDirectorySeparator || path[pathIndex] != '/');
}
else if (c == '[')
{
// "[]" matches a single character in the range
matching = pathIndex < path.Length && IsRangeMatch(pattern, ref patternIndex, path[pathIndex], ignoreCase, matchWildCardWithDirectorySeparator);
}
else
{
if (c == '\\' && patternIndex < pattern.Length)
{
c = pattern[patternIndex++];
}
// match specific character:
matching = pathIndex < path.Length && equal(c, path[pathIndex]);
}
if (matching && (!matchDirectoryNameStart || pathIndex == 0 || path[pathIndex - 1] == '/'))
{
matchDirectoryNameStart = false;
pathIndex++;
}
else if (nextDoublePatternIndex >= 0 || nextSinglePatternIndex >= 0)
{
// mismatch while matching pattern following a wildcard ** or *
// "*" matches anything but "/" (when matchWildCardWithDirectorySeparator is false)
if (!stopAtPathSlash || pathIndex < path.Length && path[pathIndex] == '/')
{
// Reset to the last saved ** position, if any.
// Also handles reset of * when matchWildCardWithDirectorySeparator is true.
if (nextDoublePatternIndex < 0)
{
return false;
}
patternIndex = nextDoublePatternIndex;
pathIndex = nextDoublePathIndex;
nextDoublePathIndex++;
}
else
{
// Reset to the last saved * position.
patternIndex = nextSinglePatternIndex;
pathIndex = nextSinglePathIndex;
nextSinglePathIndex++;
}
Debug.Assert(patternIndex >= 0);
Debug.Assert(pathIndex >= 0);
if (pathIndex >= path.Length)
{
return false;
}
}
else
{
// character mismatch
return false;
}
}
return pathIndex == path.Length;
}
private static bool IsRangeMatch(string pattern, ref int patternIndex, char pathChar, bool ignoreCase, bool matchWildCardWithDirectorySeparator)
{
Debug.Assert(pattern[patternIndex - 1] == '[');
if (patternIndex == pattern.Length)
{
return false;
}
if (ignoreCase)
{
pathChar = char.ToLowerInvariant(pathChar);
}
bool negate = false;
bool isEmpty = true;
bool isMatching = false;
var c = pattern[patternIndex];
if (c == '!' || c == '^')
{
negate = true;
patternIndex++;
}
while (patternIndex < pattern.Length)
{
c = pattern[patternIndex++];
if (c == ']' && !isEmpty)
{
// Range does not match '/', but [^a] matches '/' if matchWildCardWithDirectorySeparator=true.
return (pathChar != '/' || matchWildCardWithDirectorySeparator) && (negate ? !isMatching : isMatching);
}
if (ignoreCase)
{
c = char.ToLowerInvariant(c);
}
char d;
if (patternIndex + 1 < pattern.Length && pattern[patternIndex] == '-' && (d = pattern[patternIndex + 1]) != ']')
{
if (ignoreCase)
{
d = char.ToLowerInvariant(d);
}
isMatching |= pathChar == c || pathChar > c && pathChar <= d;
patternIndex += 2;
}
else
{
// continue parsing to validate the range is well-formed
isMatching |= pathChar == c;
}
isEmpty = false;
}
// malformed range
return false;
}
}
}