summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAnton Kudryavtsev <anton.kudryavtsev@vk.team>2023-10-18 18:17:13 +0300
committerAnton Kudryavtsev <anton.kudryavtsev@vk.team>2023-11-02 13:50:42 +0300
commit4fad57e750b4c945a627278c5ecaa58e86460326 (patch)
tree20022bec80f7995ea6ad8a2cbade9126c501aa46
parentd10d76f9f20d48d69117d9f7dab089cb2a696b2e (diff)
QStringView: add isLower and isUpper
[ChangeLog][QtCore][QStringView] Added isLower() and isUpper() Change-Id: Ie6cd20bd375d42cbdfb17953b2307d025c31ec77 Reviewed-by: Edward Welbourne <edward.welbourne@qt.io> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
-rw-r--r--src/corelib/text/qstring.cpp41
-rw-r--r--src/corelib/text/qstringalgorithms.h3
-rw-r--r--src/corelib/text/qstringview.cpp26
-rw-r--r--src/corelib/text/qstringview.h5
-rw-r--r--tests/auto/corelib/text/qstring/tst_qstring.cpp2
5 files changed, 59 insertions, 18 deletions
diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp
index 93e541e712..dd02fa79fe 100644
--- a/src/corelib/text/qstring.cpp
+++ b/src/corelib/text/qstring.cpp
@@ -5416,6 +5416,27 @@ bool QString::endsWith(QChar c, Qt::CaseSensitivity cs) const
return foldCase(at(size() - 1)) == foldCase(c);
}
+static bool checkCase(QStringView s, QUnicodeTables::Case c) noexcept
+{
+ QStringIterator it(s);
+ while (it.hasNext()) {
+ const char32_t uc = it.next();
+ if (qGetProp(uc)->cases[c].diff)
+ return false;
+ }
+ return true;
+}
+
+bool QtPrivate::isLower(QStringView s) noexcept
+{
+ return checkCase(s, QUnicodeTables::LowerCase);
+}
+
+bool QtPrivate::isUpper(QStringView s) noexcept
+{
+ return checkCase(s, QUnicodeTables::UpperCase);
+}
+
/*!
Returns \c true if the string is uppercase, that is, it's identical
to its toUpper() folding.
@@ -5431,15 +5452,7 @@ bool QString::endsWith(QChar c, Qt::CaseSensitivity cs) const
*/
bool QString::isUpper() const
{
- QStringIterator it(*this);
-
- while (it.hasNext()) {
- const char32_t uc = it.next();
- if (qGetProp(uc)->cases[QUnicodeTables::UpperCase].diff)
- return false;
- }
-
- return true;
+ return QtPrivate::isUpper(qToStringViewIgnoringNull(*this));
}
/*!
@@ -5457,15 +5470,7 @@ bool QString::isUpper() const
*/
bool QString::isLower() const
{
- QStringIterator it(*this);
-
- while (it.hasNext()) {
- const char32_t uc = it.next();
- if (qGetProp(uc)->cases[QUnicodeTables::LowerCase].diff)
- return false;
- }
-
- return true;
+ return QtPrivate::isLower(qToStringViewIgnoringNull(*this));
}
static QByteArray qt_convert_to_latin1(QStringView string);
diff --git a/src/corelib/text/qstringalgorithms.h b/src/corelib/text/qstringalgorithms.h
index a8d43b3ed0..1f145ae096 100644
--- a/src/corelib/text/qstringalgorithms.h
+++ b/src/corelib/text/qstringalgorithms.h
@@ -65,6 +65,9 @@ namespace QtPrivate {
[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION QStringView trimmed(QStringView s) noexcept;
[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION QLatin1StringView trimmed(QLatin1StringView s) noexcept;
+[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isLower(QStringView s) noexcept;
+[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isUpper(QStringView s) noexcept;
+
[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype count(QStringView haystack, QChar needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype count(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept;
[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype count(QStringView haystack, QLatin1StringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive);
diff --git a/src/corelib/text/qstringview.cpp b/src/corelib/text/qstringview.cpp
index 6f26a04d34..0debfbf427 100644
--- a/src/corelib/text/qstringview.cpp
+++ b/src/corelib/text/qstringview.cpp
@@ -1094,6 +1094,32 @@ or the character \a ch
*/
/*!
+ \fn bool QStringView::isLower() const
+ \since 6.7
+ Returns \c true if this view is identical to its lowercase folding.
+
+ Note that this does \e not mean that the string view does not contain
+ uppercase letters (some uppercase letters do not have a lowercase
+ folding; they are left unchanged by toString().toLower()).
+ For more information, refer to the Unicode standard, section 3.13.
+
+ \sa QChar::toLower(), isUpper()
+*/
+
+/*!
+ \fn bool QStringView::isUpper() const
+ \since 6.7
+ Returns \c true if this view is identical to its uppercase folding.
+
+ Note that this does \e not mean that the the string view does not contain
+ lowercase letters (some lowercase letters do not have a uppercase
+ folding; they are left unchanged by toString().toUpper()).
+ For more information, refer to the Unicode standard, section 3.13.
+
+ \sa QChar::toUpper(), isLower()
+*/
+
+/*!
\fn QStringView::toWCharArray(wchar_t *array) const
\since 5.14
diff --git a/src/corelib/text/qstringview.h b/src/corelib/text/qstringview.h
index d62dd71bdf..e27f90c167 100644
--- a/src/corelib/text/qstringview.h
+++ b/src/corelib/text/qstringview.h
@@ -324,6 +324,11 @@ public:
[[nodiscard]] bool isValidUtf16() const noexcept
{ return QtPrivate::isValidUtf16(*this); }
+ [[nodiscard]] bool isUpper() const noexcept
+ { return QtPrivate::isUpper(*this); }
+ [[nodiscard]] bool isLower() const noexcept
+ { return QtPrivate::isLower(*this); }
+
[[nodiscard]] inline short toShort(bool *ok = nullptr, int base = 10) const;
[[nodiscard]] inline ushort toUShort(bool *ok = nullptr, int base = 10) const;
[[nodiscard]] inline int toInt(bool *ok = nullptr, int base = 10) const;
diff --git a/tests/auto/corelib/text/qstring/tst_qstring.cpp b/tests/auto/corelib/text/qstring/tst_qstring.cpp
index 94b84ffc4b..92b4a26640 100644
--- a/tests/auto/corelib/text/qstring/tst_qstring.cpp
+++ b/tests/auto/corelib/text/qstring/tst_qstring.cpp
@@ -2944,10 +2944,12 @@ void tst_QString::isLower_isUpper()
QFETCH(bool, isUpper);
QCOMPARE(string.isLower(), isLower);
+ QCOMPARE(QStringView(string).isLower(), isLower);
QCOMPARE(string.toLower() == string, isLower);
QVERIFY(string.toLower().isLower());
QCOMPARE(string.isUpper(), isUpper);
+ QCOMPARE(QStringView(string).isUpper(), isUpper);
QCOMPARE(string.toUpper() == string, isUpper);
QVERIFY(string.toUpper().isUpper());
}