diff --git a/autotests/kstringhandlertest.cpp b/autotests/kstringhandlertest.cpp --- a/autotests/kstringhandlertest.cpp +++ b/autotests/kstringhandlertest.cpp @@ -1,6 +1,7 @@ #include "kstringhandlertest.h" +#include #include QTEST_MAIN(KStringHandlerTest) @@ -57,6 +58,8 @@ expected << QStringLiteral("Split") << QStringLiteral("me") << QStringLiteral("up ! I'm bored ! OK ?"); QCOMPARE(KStringHandler::perlSplit(QRegExp(QStringLiteral("[! ]")), QStringLiteral("Split me up ! I'm bored ! OK ?"), 3), expected); + QCOMPARE(KStringHandler::perlSplit(QRegularExpression(QStringLiteral("[! ]")), + QStringLiteral("Split me up ! I'm bored ! OK ?"), 3), expected); } void KStringHandlerTest::obscure() diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -140,7 +140,7 @@ GROUP_BASE_NAME KF VERSION ${KF5_VERSION} DEPRECATED_BASE_VERSION 0 - DEPRECATION_VERSIONS 4.0 5.0 5.2 5.65 + DEPRECATION_VERSIONS 4.0 5.0 5.2 5.65 5.67 EXCLUDE_DEPRECATED_BEFORE_AND_AT ${EXCLUDE_DEPRECATED_BEFORE_AND_AT} ) diff --git a/src/lib/text/kstringhandler.h b/src/lib/text/kstringhandler.h --- a/src/lib/text/kstringhandler.h +++ b/src/lib/text/kstringhandler.h @@ -27,6 +27,7 @@ class QChar; class QRegExp; +class QRegularExpression; class QString; class QStringList; @@ -139,6 +140,7 @@ const QString &s, int max = 0); +#if KCOREADDONS_ENABLE_DEPRECATED_SINCE(5, 67) /** * Split a QString into a QStringList in a similar fashion to the static * QStringList function in Qt, except you can specify a maximum number @@ -155,10 +157,37 @@ * @param s is the input string * @param max is the maximum number of extractions to perform, or 0. * @return A QStringList containing tokens extracted from s. + * + * @deprecated Since 5.67, use perlSplit(const QRegularExpression &sep, + * const QString &s, int max = 0) instead. */ +KCOREADDONS_DEPRECATED_VERSION(5, 67, "Use KStringHandler::perlSplit(const QRegularExpression &, const QString &, int)") KCOREADDONS_EXPORT QStringList perlSplit(const QRegExp &sep, const QString &s, int max = 0); +#endif + +/** + * Split a QString into a QStringList in a similar fashion to the static + * QStringList function in Qt, except you can specify a maximum number + * of tokens. If max is specified (!= 0) then only that number of tokens + * will be extracted. The final token will be the remainder of the string. + * + * Example: + * \code + * perlSplit(QRegularExpression("[! ]"), "Split me up ! I'm bored ! OK ?", 3) + * QStringList contains: "Split", "me", "up ! I'm bored ! OK ?" + * \endcode + * + * @param sep is the regular expression to use to delimit s. + * @param s is the input string + * @param max is the maximum number of extractions to perform, or 0. + * @return A QStringList containing tokens extracted from s. + * + * @since 5.67 + */ +KCOREADDONS_EXPORT QStringList perlSplit(const QRegularExpression &sep, + const QString &s, int max = 0); /** * This method auto-detects URLs in strings, and adds HTML markup to them diff --git a/src/lib/text/kstringhandler.cpp b/src/lib/text/kstringhandler.cpp --- a/src/lib/text/kstringhandler.cpp +++ b/src/lib/text/kstringhandler.cpp @@ -138,8 +138,14 @@ return l; } +#if KCOREADDONS_BUILD_DEPRECATED_SINCE(5, 67) QStringList KStringHandler::perlSplit(const QRegExp &sep, const QString &s, int max) { + // nothing to split + if (s.isEmpty()) { + return QStringList(); + } + bool ignoreMax = 0 == max; QStringList l; @@ -164,6 +170,40 @@ return l; } +#endif + +QStringList KStringHandler::perlSplit(const QRegularExpression &sep, const QString &s, int max) +{ + // nothing to split + if (s.isEmpty()) { + return QStringList(); + } + + bool ignoreMax = max == 0; + + QStringList list; + + int start = 0; + QRegularExpressionMatchIterator iter = sep.globalMatch(s); + QRegularExpressionMatch match; + QString chunk; + while (iter.hasNext() && (ignoreMax || list.count() < max - 1)) { + match = iter.next(); + chunk = s.mid(start, match.capturedStart() - start); + if (!chunk.isEmpty()) { + list.append(chunk); + } + start = match.capturedEnd(); + } + + // catch the remainder + chunk = s.mid(start, s.size() - start); + if (!chunk.isEmpty()) { + list.append(chunk); + } + + return list; +} QString KStringHandler::tagUrls(const QString &text) {