From 959808cc4db3309bb89e59dfd90870ee7ea34470 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Nowacki?= Date: Wed, 9 Jul 2014 15:56:11 +0200 Subject: [PATCH] Refactor QString::split functions. Implementation of the functions were moved to templatized helper functions that will be shared with QString::splitRef in future. Change-Id: Ie25fab57f77f5ceb11ced26ab7e7f86739f4c78b Reviewed-by: Thiago Macieira --- src/corelib/tools/qstring.cpp | 136 +++++++++++++++++++--------------- 1 file changed, 76 insertions(+), 60 deletions(-) diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp index ca6367b113..b8aa98b160 100644 --- a/src/corelib/tools/qstring.cpp +++ b/src/corelib/tools/qstring.cpp @@ -6624,6 +6624,27 @@ QString QString::number(double n, char f, int prec) return s; } +namespace { +template +static ResultList splitString(const QString &source, MidMethod mid, const Separator &sep, + QString::SplitBehavior behavior, Qt::CaseSensitivity cs, const int separatorSize) +{ + ResultList list; + int start = 0; + int end; + int extra = 0; + while ((end = source.indexOf(sep, start + extra, cs)) != -1) { + if (start != end || behavior == QString::KeepEmptyParts) + list.append((source.*mid)(start, end - start)); + start = end + separatorSize; + extra = (separatorSize == 0 ? 1 : 0); + } + if (start != source.size() || behavior == QString::KeepEmptyParts) + list.append((source.*mid)(start, -1)); + return list; +} +} // namespace + /*! Splits the string into substrings wherever \a sep occurs, and returns the list of those strings. If \a sep does not match @@ -6644,19 +6665,7 @@ QString QString::number(double n, char f, int prec) */ QStringList QString::split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const { - QStringList list; - int start = 0; - int extra = 0; - int end; - while ((end = indexOf(sep, start + extra, cs)) != -1) { - if (start != end || behavior == KeepEmptyParts) - list.append(mid(start, end - start)); - start = end + sep.size(); - extra = (sep.size() == 0 ? 1 : 0); - } - if (start != size() || behavior == KeepEmptyParts) - list.append(mid(start)); - return list; + return splitString(*this, &QString::mid, sep, behavior, cs, sep.size()); } /*! @@ -6664,20 +6673,32 @@ QStringList QString::split(const QString &sep, SplitBehavior behavior, Qt::CaseS */ QStringList QString::split(QChar sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const { - QStringList list; - int start = 0; - int end; - while ((end = indexOf(sep, start, cs)) != -1) { - if (start != end || behavior == KeepEmptyParts) - list.append(mid(start, end - start)); - start = end + 1; - } - if (start != size() || behavior == KeepEmptyParts) - list.append(mid(start)); - return list; + return splitString(*this, &QString::mid, sep, behavior, cs, 1); } #ifndef QT_NO_REGEXP +namespace { +template +static ResultList splitString(const QString &source, MidMethod mid, const QRegExp &rx, QString::SplitBehavior behavior) +{ + QRegExp rx2(rx); + ResultList list; + int start = 0; + int extra = 0; + int end; + while ((end = rx2.indexIn(source, start + extra)) != -1) { + int matchedLen = rx2.matchedLength(); + if (start != end || behavior == QString::KeepEmptyParts) + list.append((source.*mid)(start, end - start)); + start = end + matchedLen; + extra = (matchedLen == 0) ? 1 : 0; + } + if (start != source.size() || behavior == QString::KeepEmptyParts) + list.append((source.*mid)(start, -1)); + return list; +} +} // namespace + /*! \overload @@ -6706,26 +6727,41 @@ QStringList QString::split(QChar sep, SplitBehavior behavior, Qt::CaseSensitivit */ QStringList QString::split(const QRegExp &rx, SplitBehavior behavior) const { - QRegExp rx2(rx); - QStringList list; - int start = 0; - int extra = 0; - int end; - while ((end = rx2.indexIn(*this, start + extra)) != -1) { - int matchedLen = rx2.matchedLength(); - if (start != end || behavior == KeepEmptyParts) - list.append(mid(start, end - start)); - start = end + matchedLen; - extra = (matchedLen == 0) ? 1 : 0; - } - if (start != size() || behavior == KeepEmptyParts) - list.append(mid(start)); - return list; + return splitString(*this, &QString::mid, rx, behavior); } #endif #ifndef QT_NO_REGULAREXPRESSION #ifndef QT_BOOTSTRAPPED +namespace { +template +static ResultList splitString(const QString &source, MidMethod mid, const QRegularExpression &re, + QString::SplitBehavior behavior) +{ + ResultList list; + if (!re.isValid()) { + qWarning("QString::split: invalid QRegularExpression object"); + return list; + } + + int start = 0; + int end = 0; + QRegularExpressionMatchIterator iterator = re.globalMatch(source); + while (iterator.hasNext()) { + QRegularExpressionMatch match = iterator.next(); + end = match.capturedStart(); + if (start != end || behavior == QString::KeepEmptyParts) + list.append((source.*mid)(start, end - start)); + start = match.capturedEnd(); + } + + if (start != source.size() || behavior == QString::KeepEmptyParts) + list.append((source.*mid)(start, -1)); + + return list; +} +} // namespace + /*! \overload \since 5.0 @@ -6755,27 +6791,7 @@ QStringList QString::split(const QRegExp &rx, SplitBehavior behavior) const */ QStringList QString::split(const QRegularExpression &re, SplitBehavior behavior) const { - QStringList list; - if (!re.isValid()) { - qWarning("QString::split: invalid QRegularExpression object"); - return list; - } - - int start = 0; - int end = 0; - QRegularExpressionMatchIterator iterator = re.globalMatch(*this); - while (iterator.hasNext()) { - QRegularExpressionMatch match = iterator.next(); - end = match.capturedStart(); - if (start != end || behavior == KeepEmptyParts) - list.append(mid(start, end - start)); - start = match.capturedEnd(); - } - - if (start != size() || behavior == KeepEmptyParts) - list.append(mid(start)); - - return list; + return splitString(*this, &QString::mid, re, behavior); } #endif // QT_BOOTSTRAPPED #endif // QT_NO_REGULAREXPRESSION