From 8a932e99e9455981fb32fa6c085492462fe8f8cf Mon Sep 17 00:00:00 2001 From: Sebastian Feldmann Date: Mon, 20 Dec 2021 15:59:32 +0100 Subject: [PATCH] Fix UTF8 cyrillic line ending issue 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. --- src/Output/Util.php | 3 ++- tests/cli/Output/UtilTest.php | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/Output/Util.php b/src/Output/Util.php index 6fac662..6db76f0 100644 --- a/src/Output/Util.php +++ b/src/Output/Util.php @@ -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); } } diff --git a/tests/cli/Output/UtilTest.php b/tests/cli/Output/UtilTest.php index e0c8900..881cb3b 100644 --- a/tests/cli/Output/UtilTest.php +++ b/tests/cli/Output/UtilTest.php @@ -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)); + } }