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
17 changes: 10 additions & 7 deletions lib/Horde/Stream/Filter/Eol.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ class Horde_Stream_Filter_Eol extends php_user_filter
protected $_search;

/**
* First character of a multi-character EOL.
* Partial EOL to be processed in the next iteration
*
* @var string
*/
protected $_split = null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you remove the $_split property?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous implementation saved the target EOL first character in this property. To my understanding this was wrong as we are searching for source EOLs.
My implementation uses the _prependNext property to keep track of a carriage return at the end of a bucket when we are searching for CR+LF EOLs.

private $_prependNext = '';

/**
* @see stream_filter_register()
Expand All @@ -72,9 +72,6 @@ public function onCreate()
} else {
$this->_search = array("\r\n", "\r", "\n");
$this->_replace = array("\n", "\n", $eol);
if (strlen($eol) > 1) {
$this->_split = $eol[0];
}
}

return true;
Expand All @@ -87,9 +84,15 @@ public function onCreate()
public function filter($in, $out, &$consumed, $closing)
{
while ($bucket = stream_bucket_make_writeable($in)) {
if (!is_null($this->_split) &&
($bucket->data[$bucket->datalen - 1] == $this->_split)) {
$bucket->data = $this->_prependNext . $bucket->data;
$this->_prependNext = '';
if (
!$closing &&
in_array("\r\n", $this->_search) &&
($bucket->data[$bucket->datalen - 1] == "\r")
) {
$bucket->data = substr($bucket->data, 0, -1);
$this->_prependNext = "\r";
}

$bucket->data = str_replace($this->_search, $this->_replace, $bucket->data);
Expand Down
48 changes: 16 additions & 32 deletions test/Horde/Stream/Filter/EolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,39 +31,9 @@ public function tearDown(): void
public static function lineEndingProvider()
{
return array(
array("\n", "A
B
C
D

E

F

G


H


I"),
array("\r", "A\rB\rC\rD\r\rE\r\rF\r\rG\r\r\rH\r\r\rI"),
array("\n", "A\nB\nC\nD\n\nE\n\nF\n\nG\n\n\nH\n\n\nI"),
array("\n", "A
B
C
D

E

F

G


H


I"),
array("\r\n", "A\r\nB\r\nC\r\nD\r\n\r\nE\r\n\r\nF\r\n\r\nG\r\n\r\n\r\nH\r\n\r\n\r\nI"),
array("", "ABCDEFGHI"),
);
}
Expand Down Expand Up @@ -110,4 +80,18 @@ public function testBug12673()
);
}

public function testUnixStyleNewLineSubstitution()
{
$test = str_repeat("A\r\n", 4000);
Copy link
Author

@bsod85 bsod85 Apr 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test string needs to be bigger than a stream bucket and the carriage return needs to be at the end of a bucket

$expectedResult = str_repeat("A\n", 4000);

rewind($this->fp);
fwrite($this->fp, $test);

$filter = stream_filter_prepend($this->fp, 'horde_eol', STREAM_FILTER_READ, array('eol' => "\n"));
rewind($this->fp);

$this->assertEquals($expectedResult, stream_get_contents($this->fp));
}

}