summaryrefslogtreecommitdiffstats
path: root/src/corelib/text
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2020-07-19 10:55:55 -0700
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2020-07-31 06:01:39 +0000
commit8585263843147e8092ff5bbc2e13d10a1b7be0de (patch)
tree4fa6165a88f921da4d0901e8b9c0aadf2e4b9446 /src/corelib/text
parenta96fc6e65cba14d80665ef5f8932f004f7f4a1e3 (diff)
Fix warning in QStringView::compare(QChar) on qsizetype->int
size() - 1 is converted to int as the result, but size() is a qsizetype and could be bigger than INT_MAX. So rewrite to not depend on the cast. This was introduced on b2f79cceb11dfd15ac9eea631bc18ad6b036eb91. I could have replaced size() - 1 with int(size() > 1) - 1, but that's even more complex. To simplify, I split the function in two while retaining the C++11 constexpr requirements. Bonus: removes the use of the ambiguously-named "empty()" function that looks like a verb. Fixes: QTBUG-85665 Change-Id: Ieffc3453b88c4517a1dbfffd162338fdb084a376 Reviewed-by: Lars Knoll <lars.knoll@qt.io> (cherry picked from commit 61ccfefb25d61da95a1a1cdf0313da1448dc23c6) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'src/corelib/text')
-rw-r--r--src/corelib/text/qstringview.h5
1 files changed, 4 insertions, 1 deletions
diff --git a/src/corelib/text/qstringview.h b/src/corelib/text/qstringview.h
index a2e55f5b16..9b85dcffa5 100644
--- a/src/corelib/text/qstringview.h
+++ b/src/corelib/text/qstringview.h
@@ -259,7 +259,7 @@ public:
{ return QtPrivate::compareStrings(*this, other, cs); }
Q_REQUIRED_RESULT inline int compare(QLatin1String other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
Q_REQUIRED_RESULT Q_DECL_CONSTEXPR int compare(QChar c) const noexcept
- { return empty() || front() == c ? size() - 1 : *utf16() - c.unicode() ; }
+ { return size() >= 1 ? compare_single_char_helper(*utf16() - c.unicode()) : -1; }
Q_REQUIRED_RESULT int compare(QChar c, Qt::CaseSensitivity cs) const noexcept
{ return QtPrivate::compareStrings(*this, QStringView(&c, 1), cs); }
@@ -332,6 +332,9 @@ public:
private:
qsizetype m_size;
const storage_type *m_data;
+
+ Q_DECL_CONSTEXPR int compare_single_char_helper(int diff) const noexcept
+ { return diff ? diff : size() > 1 ? 1 : 0; }
};
Q_DECLARE_TYPEINFO(QStringView, Q_PRIMITIVE_TYPE);