Skip to content
Open
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
5 changes: 4 additions & 1 deletion src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,10 @@ function split(ReadableStream $source, string $delimiter, ?Cancellation $cancell
$split = \explode($delimiter, $buffer);
$buffer = \array_pop($split);

yield from $split;
// Don't use yield from to avoid reusing the keys from $split
foreach ($split as $v) {
yield $v;
}
}

if ($buffer !== '') {
Expand Down
14 changes: 14 additions & 0 deletions test/SplitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ public function testMultiLineSlow(): void
$this->check(["a", "bc", "\r", "\n\r\nef\r", "\n"], ["abc", "", "ef"]);
}

public function testKey(): void
{
$stream = new ReadableIterableStream(Pipeline::fromIterable(["a|b|c", "|", "||d|e|f"]));

$lines = [];
$expectedK = 0;
foreach (split($stream, '|') as $k => $line) {
$this->assertEquals($expectedK++, $k);
$lines[] = $line;
}

self::assertSame(["a", "b", "c", "", "", "d", "e", "f"], $lines);
}

public function testCustomDelimiter(): void
{
$stream = new ReadableIterableStream(Pipeline::fromIterable(["a|b|c", "|", "||d|e|f"]));
Expand Down
Loading