Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Gitignore.php

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\Finder

Code

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, '/') ? '(?:$|/)' : '');
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal