diff options
author | Lars Knoll <lars.knoll@qt.io> | 2020-09-23 13:09:23 +0200 |
---|---|---|
committer | Lars Knoll <lars.knoll@qt.io> | 2020-10-15 11:29:30 +0200 |
commit | 8e01088b555271886424dacd14fe95b1a89bb217 (patch) | |
tree | b542777ddd30e2273e65772dc59cf11d64a8f587 | |
parent | b09b87a664415ccf3574cfad94acd0cfd2af21c3 (diff) |
Add some missing QStringView overloads to QString
These are added to aid writing portable code between
Qt 5.15 and Qt 6.
[ChangeLog][QtCore][QStringView] A couple of methods have been
added to QStringView that make it easier to write code that is
portable between Qt 5.15 and Qt 6. Those include
QStringView::split(), QStringView::count(), number conversion
methods (QStringView::toInt() and friends). A couple of overloads
taking QStringView have been added to QRegularExpression (match()
and globalMatch()) and QString (append(), prepend(), insert() and
localeAwareCompare()).
Fixes: QTBUG-86516
Change-Id: I98779217c7a4107fbe086f956d24b1ce059194d7
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
-rw-r--r-- | src/corelib/text/qstring.cpp | 84 | ||||
-rw-r--r-- | src/corelib/text/qstring.h | 11 |
2 files changed, 95 insertions, 0 deletions
diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index 150d40c12c..f9ec7678fe 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -2533,6 +2533,21 @@ QString &QString::operator=(QChar ch) first extended using resize(). */ +/*! + \fn QString& QString::insert(int position, QStringView str) + \since 5.15.2 + \overload insert() + + Inserts the string reference \a str at the given index \a position and + returns a reference to this string. + + If the given \a position is greater than size(), the array is + first extended using resize(). + + \note This method has been added in 5.15.2 to simplify writing code that is portable + between Qt 5.15 and Qt 6. +*/ + /*! \fn QString& QString::insert(int position, const char *str) @@ -2797,6 +2812,16 @@ QString &QString::append(QChar ch) returns a reference to this string. */ +/*! + \fn QString &QString::prepend(QStringView str) + \since 5.15.2 + + Prepends the given string \a str to this string and returns the result. + + \note This method has been added in 5.15.2 to simplify writing code that is portable + between Qt 5.15 and Qt 6. +*/ + /*! \fn QString &QString::prepend(const QByteArray &ba) \overload prepend() @@ -6069,6 +6094,16 @@ QString& QString::fill(QChar ch, int size) Appends the string section referenced by \a str to this string. */ +/*! \fn QString &QString::operator+=(QStringView str) + \since 5.15.2 + \overload operator+=() + + Appends the string section referenced by \a str to this string. + + \note This method has been added in 5.15.2 to simplify writing code that is portable + between Qt 5.15 and Qt 6. +*/ + /*! \fn QString &QString::operator+=(char ch) \overload operator+=() @@ -6395,6 +6430,27 @@ int QString::compare_helper(const QChar *data1, int length1, QLatin1String s2, */ /*! + \fn int QString::localeAwareCompare(QStringView other) const + \since 5.15.2 + \overload localeAwareCompare() + + Compares this string with the \a other string and returns an + integer less than, equal to, or greater than zero if this string + is less than, equal to, or greater than the \a other string. + + The comparison is performed in a locale- and also + platform-dependent manner. Use this function to present sorted + lists of strings to the user. + + Same as \c {localeAwareCompare(*this, other)}. + + \note This method has been added in 5.15.2 to simplify writing code that is portable + between Qt 5.15 and Qt 6. + + \sa {Comparing Strings} +*/ + +/*! \fn int QString::localeAwareCompare(const QString &s1, const QStringRef &s2) \since 4.5 \overload localeAwareCompare() @@ -6410,6 +6466,24 @@ int QString::compare_helper(const QChar *data1, int length1, QLatin1String s2, \sa {Comparing Strings} */ +/*! + \fn int QString::localeAwareCompare(QStringView s1, QStringView s2) + \since 5.15.2 + \overload localeAwareCompare() + + Compares \a s1 with \a s2 and returns an integer less than, equal + to, or greater than zero if \a s1 is less than, equal to, or + greater than \a s2. + + The comparison is performed in a locale- and also + platform-dependent manner. Use this function to present sorted + lists of strings to the user. + + \note This method has been added in 5.15.2 to simplify writing code that is portable + between Qt 5.15 and Qt 6. + + \sa {Comparing Strings} +*/ #if !defined(CSTR_LESS_THAN) #define CSTR_LESS_THAN 1 @@ -11168,6 +11242,16 @@ QString &QString::append(const QStringRef &str) } /*! + \fn QString &QString::append(QStringView str) + \since 5.15.2 + + Appends the given string \a str to this string and returns the result. + + \note This method has been added in 5.15.2 to simplify writing code that is portable + between Qt 5.15 and Qt 6. +*/ + +/*! \fn QStringRef::left(int n) const \since 5.2 diff --git a/src/corelib/text/qstring.h b/src/corelib/text/qstring.h index 7906aeaffc..5a4ac497e0 100644 --- a/src/corelib/text/qstring.h +++ b/src/corelib/text/qstring.h @@ -541,17 +541,21 @@ public: QString &insert(int i, const QChar *uc, int len); inline QString &insert(int i, const QString &s) { return insert(i, s.constData(), s.length()); } inline QString &insert(int i, const QStringRef &s); + inline QString &insert(int i, QStringView s) + { return insert(i, s.data(), s.length()); } QString &insert(int i, QLatin1String s); QString &append(QChar c); QString &append(const QChar *uc, int len); QString &append(const QString &s); QString &append(const QStringRef &s); QString &append(QLatin1String s); + inline QString &append(QStringView s) { return append(s.data(), s.length()); } inline QString &prepend(QChar c) { return insert(0, c); } inline QString &prepend(const QChar *uc, int len) { return insert(0, uc, len); } inline QString &prepend(const QString &s) { return insert(0, s); } inline QString &prepend(const QStringRef &s) { return insert(0, s); } inline QString &prepend(QLatin1String s) { return insert(0, s); } + inline QString &prepend(QStringView s) { return insert(0, s); } inline QString &operator+=(QChar c) { if (d->ref.isShared() || uint(d->size) + 2u > d->alloc) @@ -565,6 +569,7 @@ public: inline QString &operator+=(const QString &s) { return append(s); } inline QString &operator+=(const QStringRef &s) { return append(s); } inline QString &operator+=(QLatin1String s) { return append(s); } + inline QString &operator+=(QStringView s) { return append(s); } QString &remove(int i, int len); QString &remove(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive); @@ -1780,8 +1785,10 @@ public: int localeAwareCompare(const QString &s) const; int localeAwareCompare(const QStringRef &s) const; + int localeAwareCompare(QStringView str) const; static int localeAwareCompare(const QStringRef &s1, const QString &s2); static int localeAwareCompare(const QStringRef &s1, const QStringRef &s2); + static int localeAwareCompare(QStringView s1, QStringView s2); Q_REQUIRED_RESULT QStringRef trimmed() const; short toShort(bool *ok = nullptr, int base = 10) const; @@ -2016,10 +2023,14 @@ inline int QStringRef::localeAwareCompare(const QString &s) const { return QString::localeAwareCompare_helper(constData(), length(), s.constData(), s.length()); } inline int QStringRef::localeAwareCompare(const QStringRef &s) const { return QString::localeAwareCompare_helper(constData(), length(), s.constData(), s.length()); } +inline int QStringRef::localeAwareCompare(QStringView s) const +{ return QString::localeAwareCompare_helper(constData(), length(), s.data(), s.size()); } inline int QStringRef::localeAwareCompare(const QStringRef &s1, const QString &s2) { return QString::localeAwareCompare_helper(s1.constData(), s1.length(), s2.constData(), s2.length()); } inline int QStringRef::localeAwareCompare(const QStringRef &s1, const QStringRef &s2) { return QString::localeAwareCompare_helper(s1.constData(), s1.length(), s2.constData(), s2.length()); } +inline int QStringRef::localeAwareCompare(QStringView s1, QStringView s2) +{ return QString::localeAwareCompare_helper(s1.data(), s1.size(), s2.data(), s2.size()); } #if QT_STRINGVIEW_LEVEL < 2 inline bool QStringRef::contains(const QString &s, Qt::CaseSensitivity cs) const |