Skip to content

Commit

Permalink
Use consistent keys in split
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Oct 16, 2024
1 parent daa00f2 commit f5a9fbf
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,21 @@ function getStderr(): WritableResourceStream
function split(ReadableStream $source, string $delimiter, ?Cancellation $cancellation = null): \Traversable
{
$buffer = '';
$k = 0;

while (null !== $chunk = $source->read($cancellation)) {
$buffer .= $chunk;

$split = \explode($delimiter, $buffer);
$buffer = \array_pop($split);

yield from $split;
foreach ($split as $v) {
yield $k++ => $v;
}
}

if ($buffer !== '') {
yield $buffer;
yield $k => $buffer;
}
}

Expand Down

0 comments on commit f5a9fbf

Please sign in to comment.