Skip to content

Commit

Permalink
Fix UTF8 cyrillic line ending issue
Browse files Browse the repository at this point in the history
This fixes issue #15 in a very hacky way.
The pure cyrillic detection must be replaced with a
more generic approach. But for now I could not figure
it out. So this hack must do for now.
  • Loading branch information
sebastianfeldmann committed Dec 20, 2021
1 parent bdf4944 commit 8a932e9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/Output/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public static function trimEmptyLines(array $lines): array
*/
public static function normalizeLineEndings(string $text): string
{
return preg_replace('~(BSR_ANYCRLF)*\R~', "\n", $text);
$mod = preg_match('/[\p{Cyrillic}]/u', $text) ? 'u' : '';
return preg_replace('~(*BSR_UNICODE)\R~' . $mod, "\n", $text);
}
}
18 changes: 17 additions & 1 deletion tests/cli/Output/UtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,28 @@ public function testTrimEmptyLinesEmptyLinesAtTheEnd()
/**
* Tests Util::normalizeLineEndings
*/
public function testNormalizeLineEndings(): void
public function testNormalizeLineEndingsASCII(): void
{
$text = "test\ntest\r\ntest\r\ntest";
$this->assertEquals("test\ntest\ntest\ntest", Util::normalizeLineEndings($text));
}

/**
* Tests Util::normalizeLineEndings
*/
public function testNormalizeLineEndingsUTF8(): void
{
$uft8text = "test\ftest\x0btest\r\ntest\x85test";
$this->assertEquals("test\ntest\ntest\ntest\ntest", Util::normalizeLineEndings($uft8text));
}

/**
* Tests Util::normalizeLineEndings
*/
public function testNormalizeLineEndingsCyrillic(): void
{
$cyrillicText = "text: хо\ntext: хо\r\n";
$expected = "text: хо\ntext: хо\n";
$this->assertEquals($expected, Util::normalizeLineEndings($cyrillicText));
}
}

0 comments on commit 8a932e9

Please sign in to comment.