Skip to content

Commit

Permalink
cleaning up parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
kellegous committed Oct 29, 2024
1 parent 31175e5 commit 291b5a4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 31 deletions.
21 changes: 6 additions & 15 deletions src/Owners.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,16 @@ private static function parseEntry(
string $line,
SourceInfo $sourceInfo
): Entry {
$comment_start = strpos($line, '#');
if ($comment_start === false) {
$content = trim($line);
$comment = '';
} else {
$content = trim(substr($line, 0, $comment_start));
$comment = trim(substr($line, $comment_start));
}

if ($content === '' && $comment === '') {
$line = trim($line);
if ($line === '') {
return new Blank($sourceInfo);
} elseif ($content === '' && $comment !== '') {
return new Comment($comment, $sourceInfo);
} elseif (str_starts_with($line, '#')) {
return new Comment($line, $sourceInfo);
}

return Rule::parse(
$content,
$sourceInfo,
$comment === '' ? null : $comment
$line,
$sourceInfo
);
}

Expand Down
42 changes: 26 additions & 16 deletions src/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,18 @@ public function __construct(
*
* @param string $line
* @param SourceInfo $sourceInfo
* @param string|null $comment
* @return Rule
* @throws ParseException
*/
public static function parse(
string $line,
SourceInfo $sourceInfo,
?string $comment
SourceInfo $sourceInfo
): Rule {
$ix = self::cutAfterPattern($line);
if ($ix === -1) {
$pattern = $line;
$owners = [];
} else {
$pattern = substr($line, 0, $ix);
$owners = preg_split('/\s+/', trim(substr($line, $ix)));
}
[$pattern, $line] = self::cutAfterPattern($line);
[$owners, $comment] = self::cutBeforeComment($line);
$owners = $owners !== ''
? preg_split('/\s+/', $owners)
: [];

if ($pattern === '' || !is_array($owners)) {
throw new ParseException(
Expand All @@ -86,12 +81,12 @@ public static function parse(
}

/**
* Finds the end of the pattern in the given line, taking into account escaping characters.
* Split the line after the pattern.
*
* @param string $line
* @return int
* @return array{string, string}
*/
private static function cutAfterPattern(string $line): int
private static function cutAfterPattern(string $line): array
{
$escaped = false;
for ($i = 0, $n = strlen($line); $i < $n; $i++) {
Expand All @@ -106,10 +101,25 @@ private static function cutAfterPattern(string $line): int
}

if (ctype_space($c)) {
return $i;
return [
substr($line, 0, $i),
substr($line, $i)
];
}
}
return -1;
return [$line, ''];
}

/**
* @param string $line
* @return array{string, string|null}
*/
private static function cutBeforeComment(string $line): array
{
$ix = strpos($line, '#');
return $ix === false
? [trim($line), null]
: [trim(substr($line, 0, $ix)), trim(substr($line, $ix))];
}

/**
Expand Down

0 comments on commit 291b5a4

Please sign in to comment.