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

Breadcrumb

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

class Parser

Same name in this branch
  1. 11.1.x vendor/open-telemetry/api/Baggage/Propagation/Parser.php \OpenTelemetry\API\Baggage\Propagation\Parser
  2. 11.1.x vendor/sebastian/cli-parser/src/Parser.php \SebastianBergmann\CliParser\Parser
  3. 11.1.x vendor/egulias/email-validator/src/Parser.php \Egulias\EmailValidator\Parser
  4. 11.1.x vendor/twig/twig/src/Parser.php \Twig\Parser
  5. 11.1.x vendor/symfony/css-selector/Parser/Parser.php \Symfony\Component\CssSelector\Parser\Parser
  6. 11.1.x vendor/symfony/yaml/Parser.php \Symfony\Component\Yaml\Parser
  7. 11.1.x vendor/mck89/peast/lib/Peast/Selector/Parser.php \Peast\Selector\Parser
  8. 11.1.x vendor/mck89/peast/lib/Peast/Syntax/Parser.php \Peast\Syntax\Parser

Unified diff parser.

Hierarchy

  • class \SebastianBergmann\Diff\Parser

Expanded class hierarchy of Parser

File

vendor/sebastian/diff/src/Parser.php, line 22

Namespace

SebastianBergmann\Diff
View source
final class Parser {
    
    /**
     * @return Diff[]
     */
    public function parse(string $string) : array {
        $lines = preg_split('(\\r\\n|\\r|\\n)', $string);
        if (!empty($lines) && $lines[count($lines) - 1] === '') {
            array_pop($lines);
        }
        $lineCount = count($lines);
        $diffs = [];
        $diff = null;
        $collected = [];
        for ($i = 0; $i < $lineCount; $i++) {
            if (preg_match('#^---\\h+"?(?P<file>[^\\v\\t"]+)#', $lines[$i], $fromMatch) && preg_match('#^\\+\\+\\+\\h+"?(?P<file>[^\\v\\t"]+)#', $lines[$i + 1], $toMatch)) {
                if ($diff !== null) {
                    $this->parseFileDiff($diff, $collected);
                    $diffs[] = $diff;
                    $collected = [];
                }
                assert(!empty($fromMatch['file']));
                assert(!empty($toMatch['file']));
                $diff = new Diff($fromMatch['file'], $toMatch['file']);
                $i++;
            }
            else {
                if (preg_match('/^(?:diff --git |index [\\da-f.]+|[+-]{3} [ab])/', $lines[$i])) {
                    continue;
                }
                $collected[] = $lines[$i];
            }
        }
        if ($diff !== null && count($collected)) {
            $this->parseFileDiff($diff, $collected);
            $diffs[] = $diff;
        }
        return $diffs;
    }
    private function parseFileDiff(Diff $diff, array $lines) : void {
        $chunks = [];
        $chunk = null;
        $diffLines = [];
        foreach ($lines as $line) {
            if (preg_match('/^@@\\s+-(?P<start>\\d+)(?:,\\s*(?P<startrange>\\d+))?\\s+\\+(?P<end>\\d+)(?:,\\s*(?P<endrange>\\d+))?\\s+@@/', $line, $match, PREG_UNMATCHED_AS_NULL)) {
                $chunk = new Chunk((int) $match['start'], isset($match['startrange']) ? max(0, (int) $match['startrange']) : 1, (int) $match['end'], isset($match['endrange']) ? max(0, (int) $match['endrange']) : 1);
                $chunks[] = $chunk;
                $diffLines = [];
                continue;
            }
            if (preg_match('/^(?P<type>[+ -])?(?P<line>.*)/', $line, $match)) {
                $type = Line::UNCHANGED;
                if ($match['type'] === '+') {
                    $type = Line::ADDED;
                }
                elseif ($match['type'] === '-') {
                    $type = Line::REMOVED;
                }
                $diffLines[] = new Line($type, $match['line']);
                $chunk?->setLines($diffLines);
            }
        }
        $diff->setChunks($chunks);
    }

}

Members

Title Sort descending Modifiers Object type Summary
Parser::parse public function
Parser::parseFileDiff private function

API Navigation

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