From 1c164ec7f21a78025475c561a70b94d1e3dd6bb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A5rten=20Nordheim?= Date: Wed, 2 Jun 2021 16:16:39 +0200 Subject: [PATCH] QLatin1String: Add overloads taking QLatin1Char Without the overloads using QLatin1Char in QL1S member functions results in the QL1Char being converted to QChar and QL1String being converted to QString. Change-Id: Ic19545539a207f025a6293f0b2d929de475dc166 Reviewed-by: Thiago Macieira --- src/corelib/text/qstring.cpp | 36 +++++++++++++++++++ src/corelib/text/qstring.h | 12 +++++++ .../tst_qstringapisymmetry.cpp | 15 ++++++++ 3 files changed, 63 insertions(+) diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index d68721b0dfe..8f3bde843e6 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -9020,6 +9020,12 @@ QString &QString::setRawData(const QChar *unicode, qsizetype size) \sa operator==(), operator<(), operator>() */ +/*! + \fn int QLatin1String::compare(QLatin1Char ch, Qt::CaseSensitivity cs) const + \since 6.3 + \overload +*/ + /*! \fn bool QLatin1String::startsWith(QStringView str, Qt::CaseSensitivity cs) const @@ -9041,6 +9047,12 @@ QString &QString::setRawData(const QChar *unicode, qsizetype size) \sa endsWith() */ +/*! + \fn bool QLatin1String::startsWith(QLatin1Char ch, Qt::CaseSensitivity cs) const + \since 6.3 + \overload +*/ + /*! \fn bool QLatin1String::endsWith(QStringView str, Qt::CaseSensitivity cs) const \since 5.10 @@ -9061,6 +9073,12 @@ QString &QString::setRawData(const QChar *unicode, qsizetype size) \sa startsWith() */ +/*! + \fn bool QLatin1String::endsWith(QLatin1Char ch, Qt::CaseSensitivity cs) const + \since 6.3 + \overload +*/ + /*! \fn qsizetype QLatin1String::indexOf(QStringView str, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const \fn qsizetype QLatin1String::indexOf(QLatin1String l1, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const @@ -9081,6 +9099,12 @@ QString &QString::setRawData(const QChar *unicode, qsizetype size) \sa QString::indexOf() */ +/*! + \fn qsizetype QLatin1String::indexOf(QLatin1Char ch, qsizetype from, Qt::CaseSensitivity cs) const + \since 6.3 + \overload +*/ + /*! \fn bool QLatin1String::contains(QStringView str, Qt::CaseSensitivity cs) const \fn bool QLatin1String::contains(QLatin1String l1, Qt::CaseSensitivity cs) const @@ -9098,6 +9122,12 @@ QString &QString::setRawData(const QChar *unicode, qsizetype size) QString::indexOf() */ +/*! + \fn bool QLatin1String::contains(QLatin1Char ch, Qt::CaseSensitivity cs) const + \since 6.3 + \overload +*/ + /*! \fn qsizetype QLatin1String::lastIndexOf(QStringView str, qsizetype from, Qt::CaseSensitivity cs) const \fn qsizetype QLatin1String::lastIndexOf(QLatin1String l1, qsizetype from, Qt::CaseSensitivity cs) const @@ -9119,6 +9149,12 @@ QString &QString::setRawData(const QChar *unicode, qsizetype size) QString::indexOf() */ +/*! + \fn qsizetype QLatin1String::lastIndexOf(QLatin1Char ch, qsizetype from, Qt::CaseSensitivity cs) const + \since 6.3 + \overload +*/ + /*! \fn QLatin1String::const_iterator QLatin1String::begin() const \since 5.10 diff --git a/src/corelib/text/qstring.h b/src/corelib/text/qstring.h index 5e215625c6b..e17bcbfbf6d 100644 --- a/src/corelib/text/qstring.h +++ b/src/corelib/text/qstring.h @@ -117,6 +117,8 @@ public: { return isEmpty() || front() == c ? size() - 1 : uchar(m_data[0]) - c.unicode() ; } [[nodiscard]] int compare(QChar c, Qt::CaseSensitivity cs) const noexcept { return QtPrivate::compareStrings(*this, QStringView(&c, 1), cs); } + [[nodiscard]] int compare(QLatin1Char c, Qt::CaseSensitivity cs) const noexcept + { char ch = c.toLatin1(); return QtPrivate::compareStrings(*this, QLatin1String(&ch, 1), cs); } [[nodiscard]] bool startsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept { return QtPrivate::startsWith(*this, s, cs); } @@ -126,6 +128,8 @@ public: { return !isEmpty() && front() == c; } [[nodiscard]] inline bool startsWith(QChar c, Qt::CaseSensitivity cs) const noexcept { return QtPrivate::startsWith(*this, QStringView(&c, 1), cs); } + [[nodiscard]] inline bool startsWith(QLatin1Char c, Qt::CaseSensitivity cs) const noexcept + { char ch = c.toLatin1(); return QtPrivate::startsWith(*this, QLatin1String(&ch, 1), cs); } [[nodiscard]] bool endsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept { return QtPrivate::endsWith(*this, s, cs); } @@ -135,6 +139,8 @@ public: { return !isEmpty() && back() == c; } [[nodiscard]] inline bool endsWith(QChar c, Qt::CaseSensitivity cs) const noexcept { return QtPrivate::endsWith(*this, QStringView(&c, 1), cs); } + [[nodiscard]] inline bool endsWith(QLatin1Char c, Qt::CaseSensitivity cs) const noexcept + { char ch = c.toLatin1(); return QtPrivate::endsWith(*this, QLatin1String(&ch, 1), cs); } [[nodiscard]] qsizetype indexOf(QStringView s, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept { return QtPrivate::findString(*this, from, s, cs); } @@ -142,6 +148,8 @@ public: { return QtPrivate::findString(*this, from, s, cs); } [[nodiscard]] qsizetype indexOf(QChar c, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept { return QtPrivate::findString(*this, from, QStringView(&c, 1), cs); } + [[nodiscard]] qsizetype indexOf(QLatin1Char c, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept + { char ch = c.toLatin1(); return QtPrivate::findString(*this, from, QLatin1String(&ch, 1), cs); } [[nodiscard]] bool contains(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept { return indexOf(s, 0, cs) != -1; } @@ -149,6 +157,8 @@ public: { return indexOf(s, 0, cs) != -1; } [[nodiscard]] inline bool contains(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept { return indexOf(QStringView(&c, 1), 0, cs) != -1; } + [[nodiscard]] inline bool contains(QLatin1Char c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept + { char ch = c.toLatin1(); return indexOf(QLatin1String(&ch, 1), 0, cs) != -1; } [[nodiscard]] qsizetype lastIndexOf(QStringView s, qsizetype from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept { return QtPrivate::lastIndexOf(*this, from, s, cs); } @@ -156,6 +166,8 @@ public: { return QtPrivate::lastIndexOf(*this, from, s, cs); } [[nodiscard]] qsizetype lastIndexOf(QChar c, qsizetype from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept { return QtPrivate::lastIndexOf(*this, from, QStringView(&c, 1), cs); } + [[nodiscard]] qsizetype lastIndexOf(QLatin1Char c, qsizetype from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept + { char ch = c.toLatin1(); return QtPrivate::lastIndexOf(*this, from, QLatin1String(&ch, 1), cs); } using value_type = const char; using reference = value_type&; diff --git a/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp b/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp index 58a3b56b683..48855592dcb 100644 --- a/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp +++ b/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp @@ -337,6 +337,8 @@ private Q_SLOTS: void member_compare_QLatin1String_QChar() { member_compare_impl(); } void member_compare_QLatin1String_char16_t_data() { member_compare_data(false); } void member_compare_QLatin1String_char16_t() { member_compare_impl(); } + void member_compare_QLatin1String_QLatin1Char_data() { member_compare_data(false); } + void member_compare_QLatin1String_QLatin1Char() { member_compare_impl(); } void member_compare_QLatin1String_QString_data() { member_compare_data(); } void member_compare_QLatin1String_QString() { member_compare_impl(); } void member_compare_QLatin1String_QStringView_data() { member_compare_data(); } @@ -404,6 +406,8 @@ private Q_SLOTS: void startsWith_QLatin1String_QChar() { startsWith_impl(); } void startsWith_QLatin1String_char16_t_data() { startsWith_data(false); } void startsWith_QLatin1String_char16_t() { startsWith_impl(); } + void startsWith_QLatin1String_QLatin1Char_data() { startsWith_data(false); } + void startsWith_QLatin1String_QLatin1Char() { startsWith_impl(); } void endsWith_QString_QString_data() { endsWith_data(); } void endsWith_QString_QString() { endsWith_impl(); } @@ -437,6 +441,8 @@ private Q_SLOTS: void endsWith_QLatin1String_QChar() { endsWith_impl(); } void endsWith_QLatin1String_char16_t_data() { endsWith_data(false); } void endsWith_QLatin1String_char16_t() { endsWith_impl(); } + void endsWith_QLatin1String_QLatin1Char_data() { endsWith_data(false); } + void endsWith_QLatin1String_QLatin1Char() { endsWith_impl(); } private: void split_data(bool rhsHasVariableLength = true); @@ -505,6 +511,8 @@ private Q_SLOTS: void tok_QLatin1String_QChar() { tok_impl(); } void tok_QLatin1String_char16_t_data() { tok_data(false); } void tok_QLatin1String_char16_t() { tok_impl(); } + void tok_QLatin1String_QLatin1Char_data() { tok_data(false); } + void tok_QLatin1String_QLatin1Char() { tok_impl(); } void tok_const_char16_t_star_QString_data() { tok_data(); } void tok_const_char16_t_star_QString() { tok_impl(); } @@ -741,6 +749,8 @@ private Q_SLOTS: void indexOf_QLatin1String_QChar() { indexOf_impl(); } void indexOf_QLatin1String_char16_t_data() { indexOf_data(false); } void indexOf_QLatin1String_char16_t() { indexOf_impl(); } + void indexOf_QLatin1String_QLatin1Char_data() { indexOf_data(false); } + void indexOf_QLatin1String_QLatin1Char() { indexOf_impl(); } void indexOf_QStringView_QString_data() { indexOf_data(); } void indexOf_QStringView_QString() { indexOf_impl(); } @@ -780,6 +790,8 @@ private Q_SLOTS: void contains_QLatin1String_QChar() { contains_impl(); } void contains_QLatin1String_char16_t_data() { contains_data(false); } void contains_QLatin1String_char16_t() { contains_impl(); } + void contains_QLatin1String_QLatin1Char_data() { contains_data(false); } + void contains_QLatin1String_QLatin1Char() { contains_impl(); } void contains_QStringView_QString_data() { contains_data(); } void contains_QStringView_QString() { contains_impl(); } @@ -819,6 +831,8 @@ private Q_SLOTS: void lastIndexOf_QLatin1String_QChar() { lastIndexOf_impl(); } void lastIndexOf_QLatin1String_char16_t_data() { lastIndexOf_data(false); } void lastIndexOf_QLatin1String_char16_t() { lastIndexOf_impl(); } + void lastIndexOf_QLatin1String_QLatin1Char_data() { lastIndexOf_data(false); } + void lastIndexOf_QLatin1String_QLatin1Char() { lastIndexOf_impl(); } void lastIndexOf_QStringView_QString_data() { lastIndexOf_data(); } void lastIndexOf_QStringView_QString() { lastIndexOf_impl(); } @@ -907,6 +921,7 @@ template Str make(QStringView sf, QLatin1String l1, const QByteArra /*end*/ MAKE(QChar) { return sv.isEmpty() ? QChar() : sv.at(0); } MAKE(char16_t) { return sv.isEmpty() ? char16_t() : char16_t{sv.at(0).unicode()}; } +MAKE(QLatin1Char) { return l1.isEmpty() ? QLatin1Char('\0') : l1.at(0); } MAKE(QString) { return sv.toString(); } MAKE(QStringView) { return sv; } MAKE(QLatin1String) { return l1; }