Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleaning up parsing #15

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading