forked from qt-creator/qt-creator
StringUtils: Add trimFront(), trimBack() and trim() methods
Change-Id: I900c30111c79bee548c4861b082d7b035a5dc43d Reviewed-by: <github-actions-qt-creator@cristianadam.eu> Reviewed-by: Orgad Shaneh <orgads@gmail.com>
This commit is contained in:
@@ -454,6 +454,10 @@ QTCREATOR_UTILS_EXPORT QString normalizeNewlines(const QString &text)
|
||||
return res;
|
||||
}
|
||||
|
||||
/*!
|
||||
Joins all the not empty string list's \a strings into a single string with each element
|
||||
separated by the given separator (which can be an empty string).
|
||||
*/
|
||||
QTCREATOR_UTILS_EXPORT QString joinStrings(const QStringList &strings, QChar separator)
|
||||
{
|
||||
QString result;
|
||||
@@ -467,6 +471,52 @@ QTCREATOR_UTILS_EXPORT QString joinStrings(const QStringList &strings, QChar sep
|
||||
return result;
|
||||
}
|
||||
|
||||
/*!
|
||||
Returns a copy of \a string that has \a ch characters removed from the start.
|
||||
*/
|
||||
QTCREATOR_UTILS_EXPORT QString trimFront(const QString &string, QChar ch)
|
||||
{
|
||||
const int size = string.size();
|
||||
int i = 0;
|
||||
while (i < size) {
|
||||
if (string.at(i) != ch)
|
||||
break;
|
||||
++i;
|
||||
}
|
||||
if (i == 0)
|
||||
return string;
|
||||
if (i == size)
|
||||
return {};
|
||||
return string.sliced(i);
|
||||
}
|
||||
|
||||
/*!
|
||||
Returns a copy of \a string that has \a ch characters removed from the end.
|
||||
*/
|
||||
QTCREATOR_UTILS_EXPORT QString trimBack(const QString &string, QChar ch)
|
||||
{
|
||||
const int size = string.size();
|
||||
int i = 0;
|
||||
while (i < size) {
|
||||
if (string.at(size - i - 1) != ch)
|
||||
break;
|
||||
++i;
|
||||
}
|
||||
if (i == 0)
|
||||
return string;
|
||||
if (i == size)
|
||||
return {};
|
||||
return string.chopped(i);
|
||||
}
|
||||
|
||||
/*!
|
||||
Returns a copy of \a string that has \a ch characters removed from the start and the end.
|
||||
*/
|
||||
QTCREATOR_UTILS_EXPORT QString trim(const QString &string, QChar ch)
|
||||
{
|
||||
return trimFront(trimBack(string, ch), ch);
|
||||
}
|
||||
|
||||
QTCREATOR_UTILS_EXPORT QString appendHelper(const QString &base, int n)
|
||||
{
|
||||
return base + QString::number(n);
|
||||
|
@@ -111,6 +111,8 @@ QTCREATOR_UTILS_EXPORT QString normalizeNewlines(const QString &text);
|
||||
|
||||
// Skips empty parts - see QTBUG-110900
|
||||
QTCREATOR_UTILS_EXPORT QString joinStrings(const QStringList &strings, QChar separator);
|
||||
|
||||
QTCREATOR_UTILS_EXPORT QString trimFront(const QString &string, QChar ch);
|
||||
QTCREATOR_UTILS_EXPORT QString trimBack(const QString &string, QChar ch);
|
||||
QTCREATOR_UTILS_EXPORT QString trim(const QString &string, QChar ch);
|
||||
|
||||
} // namespace Utils
|
||||
|
@@ -75,6 +75,8 @@ private slots:
|
||||
void testParseUsedPortFromNetstatOutput();
|
||||
void testJoinStrings_data();
|
||||
void testJoinStrings();
|
||||
void testTrim_data();
|
||||
void testTrim();
|
||||
|
||||
private:
|
||||
TestMacroExpander mx;
|
||||
@@ -299,6 +301,35 @@ void tst_StringUtils::testJoinStrings()
|
||||
QCOMPARE(Utils::joinStrings(data.input, data.separator), data.output);
|
||||
}
|
||||
|
||||
struct TrimData
|
||||
{
|
||||
QString input;
|
||||
QString front = {};
|
||||
QString back = {};
|
||||
QString bothSides = {};
|
||||
QChar ch = ' ';
|
||||
};
|
||||
|
||||
void tst_StringUtils::testTrim_data()
|
||||
{
|
||||
QTest::addColumn<TrimData>("data");
|
||||
|
||||
QTest::newRow("Empty") << TrimData{};
|
||||
QTest::newRow("AllToRemove") << TrimData{" "};
|
||||
QTest::newRow("BothSides") << TrimData{" foo ", "foo ", " foo", "foo"};
|
||||
QTest::newRow("BothSidesLong") << TrimData{" foo ", "foo ", " foo", "foo"};
|
||||
QTest::newRow("CharInside") << TrimData{" foo bar ", "foo bar ", " foo bar", "foo bar"};
|
||||
}
|
||||
|
||||
void tst_StringUtils::testTrim()
|
||||
{
|
||||
QFETCH(TrimData, data);
|
||||
|
||||
QCOMPARE(Utils::trimFront(data.input, data.ch), data.front);
|
||||
QCOMPARE(Utils::trimBack(data.input, data.ch), data.back);
|
||||
QCOMPARE(Utils::trim(data.input, data.ch), data.bothSides);
|
||||
}
|
||||
|
||||
QTEST_GUILESS_MAIN(tst_StringUtils)
|
||||
|
||||
#include "tst_stringutils.moc"
|
||||
|
Reference in New Issue
Block a user