diff --git a/classes/phing/filters/BaseFilterReader.php b/classes/phing/filters/BaseFilterReader.php index fc394c09f3..3ada0817a3 100644 --- a/classes/phing/filters/BaseFilterReader.php +++ b/classes/phing/filters/BaseFilterReader.php @@ -119,7 +119,7 @@ public function read($len = null) * Reads a line of text ending with '\n' (or until the end of the stream). * The returned String retains the '\n'. * - * @return string the line read, or null if the end of the + * @return string|null the line read, or null if the end of the * stream has already been reached * * @throws IOException if the underlying reader throws one during diff --git a/classes/phing/filters/PrefixLines.php b/classes/phing/filters/PrefixLines.php index 119be7aa17..f2c736faa0 100644 --- a/classes/phing/filters/PrefixLines.php +++ b/classes/phing/filters/PrefixLines.php @@ -49,13 +49,17 @@ class PrefixLines extends BaseParamFilterReader implements ChainableReader * * @var string */ - private $prefix = null; + private $prefix; + + /** @var string|null $queuedData */ + private $queuedData; /** * Adds a prefix to each line of input stream and returns resulting stream. * - * @param int $len + * @param int $len * @return mixed buffer, -1 on EOF + * @throws IOException */ public function read($len = null) { @@ -64,23 +68,31 @@ public function read($len = null) $this->setInitialized(true); } - $buffer = $this->in->read($len); + $ch = -1; - if ($buffer === -1) { - return -1; + if ($this->queuedData !== null && $this->queuedData === '') { + $this->queuedData = null; } - $lines = explode("\n", $buffer); - $filtered = []; - - foreach ($lines as $line) { - $line = $this->prefix . $line; - $filtered[] = $line; + if ($this->queuedData !== null) { + $ch = $this->queuedData[0]; + $this->queuedData = (string) substr($this->queuedData, 1); + if ($this->queuedData === '') { + $this->queuedData = null; + } + } else { + $this->queuedData = $this->readLine(); + if ($this->queuedData === null) { + $ch = -1; + } else { + if ($this->prefix !== null) { + $this->queuedData = $this->prefix . $this->queuedData; + } + return $this->read(); + } } - $filtered_buffer = implode("\n", $filtered); - - return $filtered_buffer; + return $ch; } /** diff --git a/test/classes/phing/filters/PrefixLinesTest.php b/test/classes/phing/filters/PrefixLinesTest.php new file mode 100644 index 0000000000..39599583dc --- /dev/null +++ b/test/classes/phing/filters/PrefixLinesTest.php @@ -0,0 +1,47 @@ +. + */ + +/** + * @author Siad Ardroumli + * @package phing.filters + */ +class PrefixLinesTest extends BuildFileTest +{ + public function setUp(): void + { + $this->configureProject(PHING_TEST_BASE . "/etc/filters/prefixlines.xml"); + } + + public function tearDown(): void + { + $this->executeTarget("cleanup"); + } + + public function testPrefixLines() + { + $this->executeTarget("testPrefixLines"); + + $result = $this->getProject()->resolveFile("result/prefixlines.test"); + + $this->assertStringEqualsFile( + $result->getAbsolutePath(), + 'FooThis is line 1 with alpha.FooThis is line 2 with beta.FooThis is line 3 with gamma.Foo' + ); + } +} diff --git a/test/etc/filters/input/prefixlines.test b/test/etc/filters/input/prefixlines.test new file mode 100644 index 0000000000..2600cdb1f5 --- /dev/null +++ b/test/etc/filters/input/prefixlines.test @@ -0,0 +1,3 @@ +This is line 1 with alpha. +This is line 2 with beta. +This is line 3 with gamma. diff --git a/test/etc/filters/prefixlines.xml b/test/etc/filters/prefixlines.xml new file mode 100644 index 0000000000..85460c8066 --- /dev/null +++ b/test/etc/filters/prefixlines.xml @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + + + + + + + + + +