Skip to content

Commit d6a474c

Browse files
committed
Complementation
1 parent a11fb91 commit d6a474c

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/Util/FileMatcher.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public static function toRegEx($glob, $flags = 0): string
6464
// literal directory separator
6565
self::T_SLASH => '/',
6666
self::T_QUERY => '.',
67+
self::T_BANG => '^',
6768

6869
// match any segment up until the next directory separator
6970
self::T_ASTERIX => '[^/]*',
@@ -154,7 +155,8 @@ private static function processTokens(array $tokens): array
154155
continue;
155156
}
156157

157-
// greedy globstar (trailing?)
158+
// greedy globstar (trailing?)
159+
// TODO: this should probably only apply at the end of the string according to the webmozart implementation and therefore would be "T_TRAILING_GLOBSTAR"
158160
if (
159161
$type === self::T_SLASH &&
160162
($tokens[$offset + 1][0] ?? null) === self::T_ASTERIX && ($tokens[$offset + 2][0] ?? null) === self::T_ASTERIX
@@ -172,18 +174,32 @@ private static function processTokens(array $tokens): array
172174
continue;
173175
}
174176

177+
// complementation - only parse BANG if it is at the start of a character group
178+
if ($type === self::T_BANG && isset($resolved[array_key_last($resolved) - 1]) && $resolved[array_key_last($resolved) - 1][0] === self::T_BRACKET_OPEN) {
179+
$resolved[] = [self::T_BANG, '!'];
180+
continue;
181+
}
182+
175183
if ($type === self::T_BRACKET_OPEN) {
176184
$brackets[] = $offset;
185+
$resolved[] = [$type, $char];
186+
continue;
177187
}
188+
178189
if ($type === self::T_BRACKET_CLOSE) {
179190
array_pop($brackets);
191+
$resolved[] = [$type, $char];
192+
continue;
180193
}
181194

182195
$resolved[] = [$type, $char];
183196
}
197+
198+
// foreach unterminated bracket replace it with a literal char
184199
foreach ($brackets as $unterminatedBracket) {
185200
$resolved[$unterminatedBracket] = [self::T_CHAR, '['];
186201
}
202+
187203
return $resolved;
188204
}
189205
}

0 commit comments

Comments
 (0)