function Gitignore::lineToRegex
1 call to Gitignore::lineToRegex()
- Gitignore::buildRegex in vendor/
symfony/ finder/ Gitignore.php
File
-
vendor/
symfony/ finder/ Gitignore.php, line 65
Class
- Gitignore
- Gitignore matches against text.
Namespace
Symfony\Component\FinderCode
private static function lineToRegex(string $gitignoreLine) : string {
if ('' === $gitignoreLine) {
return '$f';
// always false
}
$slashPos = strpos($gitignoreLine, '/');
if (false !== $slashPos && \strlen($gitignoreLine) - 1 !== $slashPos) {
if (0 === $slashPos) {
$gitignoreLine = substr($gitignoreLine, 1);
}
$isAbsolute = true;
}
else {
$isAbsolute = false;
}
$regex = preg_quote(str_replace('\\', '', $gitignoreLine), '~');
$regex = preg_replace_callback('~\\\\\\[((?:\\\\!)?)([^\\[\\]]*)\\\\\\]~', fn(array $matches): string => '[' . ('' !== $matches[1] ? '^' : '') . str_replace('\\-', '-', $matches[2]) . ']', $regex);
$regex = preg_replace('~(?:(?:\\\\\\*){2,}(/?))+~', '(?:(?:(?!//).(?<!//))+$1)?', $regex);
$regex = preg_replace('~\\\\\\*~', '[^/]*', $regex);
$regex = preg_replace('~\\\\\\?~', '[^/]', $regex);
return ($isAbsolute ? '' : '(?:[^/]+/)*') . $regex . (!str_ends_with($gitignoreLine, '/') ? '(?:$|/)' : '');
}