From 2d8d27c9ba97f9dc22bce23f1c1d85a56fdd1310 Mon Sep 17 00:00:00 2001 From: Jarek Kobus Date: Fri, 3 Feb 2023 04:01:00 +0100 Subject: [PATCH] StringUtils: Add joinStrings method See QTBUG-110900. In meantime, StringUtils addition. Change-Id: I700321a65d2d00b85daa412c77b573457312ad3c Reviewed-by: Orgad Shaneh --- src/libs/utils/stringutils.cpp | 13 +++ src/libs/utils/stringutils.h | 3 + .../utils/stringutils/tst_stringutils.cpp | 79 ++++++++++++++++--- 3 files changed, 82 insertions(+), 13 deletions(-) diff --git a/src/libs/utils/stringutils.cpp b/src/libs/utils/stringutils.cpp index 8d389599c5d..84f3924fea0 100644 --- a/src/libs/utils/stringutils.cpp +++ b/src/libs/utils/stringutils.cpp @@ -454,6 +454,19 @@ QTCREATOR_UTILS_EXPORT QString normalizeNewlines(const QString &text) return res; } +QTCREATOR_UTILS_EXPORT QString joinStrings(const QStringList &strings, QChar separator) +{ + QString result; + for (const QString &string : strings) { + if (string.isEmpty()) + continue; + if (!result.isEmpty()) + result += separator; + result += string; + } + return result; +} + QTCREATOR_UTILS_EXPORT QString appendHelper(const QString &base, int n) { return base + QString::number(n); diff --git a/src/libs/utils/stringutils.h b/src/libs/utils/stringutils.h index 8e9630b7d71..d881bca73db 100644 --- a/src/libs/utils/stringutils.h +++ b/src/libs/utils/stringutils.h @@ -109,5 +109,8 @@ QTCREATOR_UTILS_EXPORT QStringView chopIfEndsWith(QStringView str, QChar c); QTCREATOR_UTILS_EXPORT QString normalizeNewlines(const QString &text); +// Skips empty parts - see QTBUG-110900 +QTCREATOR_UTILS_EXPORT QString joinStrings(const QStringList &strings, QChar separator); + } // namespace Utils diff --git a/tests/auto/utils/stringutils/tst_stringutils.cpp b/tests/auto/utils/stringutils/tst_stringutils.cpp index 4eddc4b47df..08bffa5c86f 100644 --- a/tests/auto/utils/stringutils/tst_stringutils.cpp +++ b/tests/auto/utils/stringutils/tst_stringutils.cpp @@ -69,10 +69,12 @@ private slots: void testWithTildeHomePath(); void testMacroExpander_data(); void testMacroExpander(); - void testStripAccelerator(); void testStripAccelerator_data(); - void testParseUsedPortFromNetstatOutput(); + void testStripAccelerator(); void testParseUsedPortFromNetstatOutput_data(); + void testParseUsedPortFromNetstatOutput(); + void testJoinStrings_data(); + void testJoinStrings(); private: TestMacroExpander mx; @@ -169,13 +171,6 @@ void tst_StringUtils::testMacroExpander() QCOMPARE(in, out); } -void tst_StringUtils::testStripAccelerator() -{ - QFETCH(QString, expected); - - QCOMPARE(Utils::stripAccelerator(QString::fromUtf8(QTest::currentDataTag())), expected); -} - void tst_StringUtils::testStripAccelerator_data() { QTest::addColumn("expected"); @@ -193,12 +188,11 @@ void tst_StringUtils::testStripAccelerator_data() QTest::newRow("Test&") << "Test"; } -void tst_StringUtils::testParseUsedPortFromNetstatOutput() +void tst_StringUtils::testStripAccelerator() { - QFETCH(QString, line); - QFETCH(int, port); + QFETCH(QString, expected); - QCOMPARE(Utils::parseUsedPortFromNetstatOutput(line.toUtf8()), port); + QCOMPARE(Utils::stripAccelerator(QString::fromUtf8(QTest::currentDataTag())), expected); } void tst_StringUtils::testParseUsedPortFromNetstatOutput_data() @@ -246,6 +240,65 @@ void tst_StringUtils::testParseUsedPortFromNetstatOutput_data() QTest::newRow("QnxA") << "tcp6 0 0 *.22 *.* LISTEN " << 22; } +void tst_StringUtils::testParseUsedPortFromNetstatOutput() +{ + QFETCH(QString, line); + QFETCH(int, port); + + QCOMPARE(Utils::parseUsedPortFromNetstatOutput(line.toUtf8()), port); +} + +struct JoinData +{ + QStringList input; + QString output = {}; + QChar separator = '\n'; +}; + +void tst_StringUtils::testJoinStrings_data() +{ + QTest::addColumn("data"); + + QTest::newRow("0") << JoinData{}; + + QTest::newRow("1") << JoinData{{"one"}, "one"}; + QTest::newRow("1_Null") << JoinData{{{}}}; + QTest::newRow("1_Empty") << JoinData{{""}}; + + QTest::newRow("2") << JoinData{{"first", "second"}, "first\nsecond"}; + QTest::newRow("2_Null") << JoinData{{{}, {}}}; + QTest::newRow("2_Empty") << JoinData{{"", ""}}; + QTest::newRow("2_1stNull") << JoinData{{{}, "second"}, "second"}; + QTest::newRow("2_1stEmpty") << JoinData{{"", "second"}, "second"}; + QTest::newRow("2_2ndNull") << JoinData{{"first", {}}, "first"}; + QTest::newRow("2_2ndEmpty") << JoinData{{"first", ""}, "first"}; + + QTest::newRow("3") << JoinData{{"first", "second", "third"}, "first\nsecond\nthird"}; + QTest::newRow("3_Null") << JoinData{{{}, {}, {}}}; + QTest::newRow("3_Empty") << JoinData{{"", "", ""}}; + QTest::newRow("3_1stNull") << JoinData{{{}, "second", "third"}, "second\nthird"}; + QTest::newRow("3_1stEmpty") << JoinData{{"", "second", "third"}, "second\nthird"}; + QTest::newRow("3_2ndNull") << JoinData{{"first", {}, "third"}, "first\nthird"}; + QTest::newRow("3_2ndEmpty") << JoinData{{"first", "", "third"}, "first\nthird"}; + QTest::newRow("3_3rdNull") << JoinData{{"first", "second", {}}, "first\nsecond"}; + QTest::newRow("3_3rdEmpty") << JoinData{{"first", "second", ""}, "first\nsecond"}; + QTest::newRow("3_1stNonNull") << JoinData{{"first", {}, {}}, "first"}; + QTest::newRow("3_1stNonEmpty") << JoinData{{"first", "", ""}, "first"}; + QTest::newRow("3_2ndNonNull") << JoinData{{{}, "second", {}}, "second"}; + QTest::newRow("3_2ndNonEmpty") << JoinData{{"", "second", ""}, "second"}; + QTest::newRow("3_2ndNonNull") << JoinData{{{}, {}, "third"}, "third"}; + QTest::newRow("3_3ndNonEmpty") << JoinData{{"", "", "third"}, "third"}; + + QTest::newRow("DotSeparator") << JoinData{{"first", "second"}, "first.second", '.'}; +} + +void tst_StringUtils::testJoinStrings() +{ + QFETCH(JoinData, data); + + QCOMPARE(Utils::joinStrings(data.input, data.separator), data.output); +} + QTEST_GUILESS_MAIN(tst_StringUtils) #include "tst_stringutils.moc"