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:
Jarek Kobus
2023-02-03 14:52:48 +01:00
parent ebcd8bcd51
commit d829d9ff33
3 changed files with 84 additions and 1 deletions

View File

@@ -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);