Skip to content

Commit

Permalink
Fixed line ending issue in SuffixLines (#1043)
Browse files Browse the repository at this point in the history
  • Loading branch information
siad007 authored Mar 15, 2019
1 parent 5f5cf7d commit 60a093b
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions classes/phing/filters/SuffixLines.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ class SuffixLines extends BaseParamFilterReader implements ChainableReader
*/
private $suffix = null;

/** @var string */
private $queuedData;

/**
* Adds a suffix to each line of input stream and returns resulting stream.
*
Expand All @@ -62,21 +65,36 @@ 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 = preg_split("~\R~", $buffer);
$filtered = [];

foreach ($lines as $line) {
$filtered[] = $line . $this->suffix;
if ($this->queuedData !== null) {
$ch = $this->queuedData[0];
$this->queuedData = substr($this->queuedData, 1);
if ($this->queuedData === '') {
$this->queuedData = null;
}
} else {
$this->queuedData = $this->readLine();
if ($this->queuedData === null) {
$ch = -1;
} else {
if ($this->suffix !== null) {
$lf = '';
if (StringHelper::endsWith($this->queuedData, "\r\n")) {
$lf = "\r\n";
} elseif (StringHelper::endsWith($this->queuedData, "\n")) {
$lf = "\n";
}
$this->queuedData = substr($this->queuedData, 0, strlen($this->queuedData) - strlen($lf)) . $this->suffix . $lf;
}
return $this->read();
}
}

$filtered_buffer = implode(PHP_EOL, $filtered);

return $filtered_buffer;
return $ch;
}

/**
Expand Down

0 comments on commit 60a093b

Please sign in to comment.