From f5a9fbf950f250c2a28a05196820bcdf5de25a45 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Wed, 16 Oct 2024 14:00:07 +0200 Subject: [PATCH 1/3] Use consistent keys in split --- src/functions.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/functions.php b/src/functions.php index 17cd3f9..3b33060 100644 --- a/src/functions.php +++ b/src/functions.php @@ -131,6 +131,7 @@ function getStderr(): WritableResourceStream function split(ReadableStream $source, string $delimiter, ?Cancellation $cancellation = null): \Traversable { $buffer = ''; + $k = 0; while (null !== $chunk = $source->read($cancellation)) { $buffer .= $chunk; @@ -138,11 +139,13 @@ function split(ReadableStream $source, string $delimiter, ?Cancellation $cancell $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; } } From 464d2ba7b35e1fbb63483483cb1e24684dc1c6a0 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Wed, 16 Oct 2024 17:40:59 +0200 Subject: [PATCH 2/3] Cleanup --- src/functions.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/functions.php b/src/functions.php index 3b33060..c453ff9 100644 --- a/src/functions.php +++ b/src/functions.php @@ -131,7 +131,6 @@ function getStderr(): WritableResourceStream function split(ReadableStream $source, string $delimiter, ?Cancellation $cancellation = null): \Traversable { $buffer = ''; - $k = 0; while (null !== $chunk = $source->read($cancellation)) { $buffer .= $chunk; @@ -139,13 +138,14 @@ function split(ReadableStream $source, string $delimiter, ?Cancellation $cancell $split = \explode($delimiter, $buffer); $buffer = \array_pop($split); + // Don't use yield from to avoid reusing the keys from $split foreach ($split as $v) { - yield $k++ => $v; + yield $v; } } if ($buffer !== '') { - yield $k => $buffer; + yield $buffer; } } From 72bd47e2169b6e15d62f33a866dbce6cf7687a6f Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Sun, 16 Mar 2025 18:37:54 +0100 Subject: [PATCH 3/3] Add testcase --- test/SplitTest.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/SplitTest.php b/test/SplitTest.php index 128d64c..aeb3844 100644 --- a/test/SplitTest.php +++ b/test/SplitTest.php @@ -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"]));