Skip to content

Commit

Permalink
parse empty sequence elements as null
Browse files Browse the repository at this point in the history
  • Loading branch information
xabbuh committed Sep 16, 2024
1 parent 62f96e1 commit 7025b96
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,11 +355,18 @@ private static function parseSequence(string $sequence, int $flags, int &$i = 0,
++$i;

// [foo, bar, ...]
$lastToken = null;
while ($i < $len) {
if (']' === $sequence[$i]) {
return $output;
}
if (',' === $sequence[$i] || ' ' === $sequence[$i]) {
if (',' === $sequence[$i] && (null === $lastToken || 'separator' === $lastToken)) {
$output[] = null;
} elseif (',' === $sequence[$i]) {
$lastToken = 'separator';
}

++$i;

continue;
Expand Down Expand Up @@ -403,6 +410,7 @@ private static function parseSequence(string $sequence, int $flags, int &$i = 0,

$output[] = $value;

$lastToken = 'value';
++$i;
}

Expand Down
7 changes: 7 additions & 0 deletions Tests/InlineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1004,4 +1004,11 @@ public function testParseQuotedReferenceLikeStringsInSequence()

$this->assertSame(['&foo', '&bar', '&baz'], Inline::parse($yaml));
}

public function testParseSequenceWithEmptyElement()
{
$this->assertSame(['foo', null, 'bar'], Inline::parse('[foo, , bar]'));
$this->assertSame([null, 'foo', 'bar'], Inline::parse('[, foo, bar]'));
$this->assertSame(['foo', 'bar'], Inline::parse('[foo, bar, ]'));
}
}

0 comments on commit 7025b96

Please sign in to comment.