From 600b85defa9db9aac73205c23e9cf65d37210ded Mon Sep 17 00:00:00 2001 From: Marco Bubke Date: Mon, 28 Nov 2016 13:57:33 +0100 Subject: [PATCH] Utils: Add carriage return strip method to SmallString Sometimes you want to compare unix and windows texts in tests. This is a convenient function to remove the carriage returns. Change-Id: I164298b70d3d775dd45903ea3753ac0e68ed2fdc Reviewed-by: Tim Jenssen --- src/libs/utils/smallstring.h | 9 +++++++++ tests/unit/unittest/smallstring-test.cpp | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/src/libs/utils/smallstring.h b/src/libs/utils/smallstring.h index 2a28b7c5b81..b0af23aac9f 100644 --- a/src/libs/utils/smallstring.h +++ b/src/libs/utils/smallstring.h @@ -472,6 +472,15 @@ public: setSize(newSize); } + BasicSmallString toCarriageReturnsStripped() const + { + BasicSmallString text = *this; + + text.replace("\r", ""); + + return text; + } + constexpr static size_type shortStringCapacity() noexcept { diff --git a/tests/unit/unittest/smallstring-test.cpp b/tests/unit/unittest/smallstring-test.cpp index 90e93986a5b..70e944ffa42 100644 --- a/tests/unit/unittest/smallstring-test.cpp +++ b/tests/unit/unittest/smallstring-test.cpp @@ -1050,3 +1050,12 @@ TEST(SmallString, ReplaceByPositionEqualSizedTexts) ASSERT_THAT(text, SmallString("this is very very very very very much text")); } +TEST(SmallString, CompareTextWithDifferentLineEndings) +{ + SmallString unixText("some \ntext"); + SmallString windowsText("some \n\rtext"); + + auto convertedText = windowsText.toCarriageReturnsStripped(); + + ASSERT_THAT(unixText, convertedText); +}