diff options
Diffstat (limited to 'src/corelib/text/qstringview.cpp')
-rw-r--r-- | src/corelib/text/qstringview.cpp | 317 |
1 files changed, 179 insertions, 138 deletions
diff --git a/src/corelib/text/qstringview.cpp b/src/corelib/text/qstringview.cpp index faaed16044..29b83ffe8f 100644 --- a/src/corelib/text/qstringview.cpp +++ b/src/corelib/text/qstringview.cpp @@ -1,45 +1,7 @@ -/**************************************************************************** -** -** Copyright (C) 2017 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@kdab.com> -** Contact: http://www.qt.io/licensing/ -** -** This file is part of the QtCore module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2017 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@kdab.com> +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include "qstringview.h" -#include "qstring.h" -#include "qlocale_p.h" QT_BEGIN_NAMESPACE @@ -70,7 +32,7 @@ QT_BEGIN_NAMESPACE When used as an interface type, QStringView allows a single function to accept a wide variety of UTF-16 string data sources. One function accepting QStringView thus replaces three function overloads (taking QString and - \c{(const QChar*, int)}), while at the same time enabling even more string data + \c{(const QChar*, qsizetype)}), while at the same time enabling even more string data sources to be passed to the function, such as \c{u"Hello World"}, a \c char16_t string literal. @@ -87,11 +49,11 @@ QT_BEGIN_NAMESPACE QChar constructor by itself. \li \e QString: if you store an unmodified copy of the string and thus would like to take advantage of QString's implicit sharing. - \li QLatin1String: if you can implement the function without converting the - QLatin1String to UTF-16 first; users expect a function overloaded on - QLatin1String to perform strictly less memory allocations than the + \li QLatin1StringView: if you can implement the function without converting the + QLatin1StringView to UTF-16 first; users expect a function overloaded on + QLatin1StringView to perform strictly less memory allocations than the semantically equivalent call of the QStringView version, involving - construction of a QString from the QLatin1String. + construction of a QString from the QLatin1StringView. \endlist QStringView can also be used as the return value of a function. If you call a @@ -217,7 +179,7 @@ QT_BEGIN_NAMESPACE */ /*! - \fn template <typename Char> QStringView::QStringView(const Char *str, qsizetype len) + \fn template <typename Char, QStringView::if_compatible_char<Char> = true> QStringView::QStringView(const Char *str, qsizetype len) Constructs a string view on \a str with length \a len. @@ -233,7 +195,7 @@ QT_BEGIN_NAMESPACE */ /*! - \fn template <typename Char> QStringView::QStringView(const Char *first, const Char *last) + \fn template <typename Char, QStringView::if_compatible_char<Char> = true> QStringView::QStringView(const Char *first, const Char *last) Constructs a string view on \a first with length (\a last - \a first). @@ -300,26 +262,26 @@ QT_BEGIN_NAMESPACE */ /*! - \fn template <typename Container, if_compatible_container<Container>> QStringView::QStringView(const Container &str) + \fn template <typename Container, QStringView::if_compatible_container<Container>> QStringView::QStringView(const Container &str) - Constructs a string view on \a str. The length is taken from \c{str.size()}. + Constructs a string view on \a str. The length is taken from \c{std::size(str)}. - \c{str.data()} must remain valid for the lifetime of this string view object. + \c{std::data(str)} must remain valid for the lifetime of this string view object. - This constructor only participates in overload resolution if \c StdBasicString is an - instantiation of \c std::basic_string with a compatible character type. The + This constructor only participates in overload resolution if \c Container is a + container with a compatible character type as \c{value_type}. The compatible character types are: \c QChar, \c ushort, \c char16_t and (on platforms, such as Windows, where it is a 16-bit type) \c wchar_t. - The string view will be empty if and only if \c{str.empty()}. It is unspecified - whether this constructor can result in a null string view (\c{str.data()} would + The string view will be empty if and only if \c{std::size(str) == 0}. It is unspecified + whether this constructor can result in a null string view (\c{std::data(str)} would have to return \nullptr for this). \sa isNull(), isEmpty() */ /*! - \fn template <typename Char, size_t Size> static QStringView QStringView::fromArray(const Char (&string)[Size]) noexcept + \fn template <typename Char, size_t Size, QStringView::if_compatible_char<Char> = true> static QStringView QStringView::fromArray(const Char (&string)[Size]) noexcept Constructs a string view on the full character string literal \a string, including any trailing \c{Char(0)}. If you don't want the @@ -343,18 +305,16 @@ QT_BEGIN_NAMESPACE Returns a deep copy of this string view's data as a QString. The return value will be the null QString if and only if this string view is null. - - \warning QStringView can store strings with more than 2\sup{30} characters - while QString cannot. Calling this function on a string view for which size() - returns a value greater than \c{INT_MAX / 2} constitutes undefined behavior. */ /*! \fn const QChar *QStringView::data() const +//! [const-pointer-to-first-ch] Returns a const pointer to the first character in the string view. \note The character array represented by the return value is \e not null-terminated. +//! [const-pointer-to-first-ch] \sa begin(), end(), utf16() */ @@ -363,9 +323,7 @@ QT_BEGIN_NAMESPACE \fn const QChar *QStringView::constData() const \since 6.0 - Returns a const pointer to the first character in the string view. - - \note The character array represented by the return value is \e not null-terminated. + \include qstringview.cpp const-pointer-to-first-ch \sa data(), begin(), end(), utf16() */ @@ -373,12 +331,10 @@ QT_BEGIN_NAMESPACE /*! \fn const storage_type *QStringView::utf16() const - Returns a const pointer to the first character in the string view. + \include qstringview.cpp const-pointer-to-first-ch \c{storage_type} is \c{char16_t}. - \note The character array represented by the return value is \e not null-terminated. - \sa begin(), end(), data() */ @@ -515,7 +471,7 @@ QT_BEGIN_NAMESPACE /*! \fn qsizetype QStringView::size() const - Returns the size of this string view, in UTF-16 code points (that is, + Returns the size of this string view, in UTF-16 code units (that is, surrogate pairs count as two for the purposes of this function, the same as in QString). @@ -523,7 +479,7 @@ QT_BEGIN_NAMESPACE */ /*! - \fn int QStringView::length() const + \fn QStringView::length() const Same as size(). @@ -554,7 +510,7 @@ QT_BEGIN_NAMESPACE /*! \fn template <typename...Args> QString QStringView::arg(Args &&...args) const - \fn template <typename...Args> QString QLatin1String::arg(Args &&...args) const + \fn template <typename...Args> QString QLatin1StringView::arg(Args &&...args) const \fn template <typename...Args> QString QString::arg(Args &&...args) const \since 5.14 @@ -564,7 +520,7 @@ QT_BEGIN_NAMESPACE second of the \a args the \c{%N} with the next-lowest \c{N} etc. \c Args can consist of anything that implicitly converts to QString, - QStringView or QLatin1String. + QStringView or QLatin1StringView. In addition, the following types are also supported: QChar, QLatin1Char. @@ -578,8 +534,10 @@ QT_BEGIN_NAMESPACE This function is provided for STL compatibility. +//! [calling-on-empty-is-UB] \warning Calling this function on an empty string view constitutes undefined behavior. +//! [calling-on-empty-is-UB] \sa back(), first(), last() */ @@ -591,8 +549,7 @@ QT_BEGIN_NAMESPACE This function is provided for STL compatibility. - \warning Calling this function on an empty string view constitutes - undefined behavior. + \include qstringview.cpp calling-on-empty-is-UB \sa front(), first(), last() */ @@ -604,8 +561,7 @@ QT_BEGIN_NAMESPACE This function is provided for compatibility with other Qt containers. - \warning Calling this function on an empty string view constitutes - undefined behavior. + \include qstringview.cpp calling-on-empty-is-UB \sa front(), back(), last() */ @@ -617,8 +573,7 @@ QT_BEGIN_NAMESPACE This function is provided for compatibility with other Qt containers. - \warning Calling this function on an empty string view constitutes - undefined behavior. + \include qstringview.cpp calling-on-empty-is-UB \sa back(), front(), first() */ @@ -699,8 +654,10 @@ QT_BEGIN_NAMESPACE Returns a string view that points to \a n characters of this string view, starting at position \a pos. +//! [UB-sliced-index-length] \note The behavior is undefined when \a pos < 0, \a n < 0, or \a pos + \a n > size(). +//! [UB-sliced-index-length] \sa first(), last(), chopped(), chop(), truncate() */ @@ -713,7 +670,9 @@ QT_BEGIN_NAMESPACE Returns a string view starting at position \a pos in this object, and extending to its end. +//! [UB-sliced-index-only] \note The behavior is undefined when \a pos < 0 or \a pos > size(). +//! [UB-sliced-index-only] \sa first(), last(), chopped(), chop(), truncate() */ @@ -769,37 +728,50 @@ QT_BEGIN_NAMESPACE \fn int QStringView::compare(QStringView str, Qt::CaseSensitivity cs) const \since 5.12 - Returns an integer that compares to zero as this string view compares to the - string view \a str. + Compares this string view with string view \a str and returns a negative integer if + this string view is less than \a str, a positive integer if it is greater than + \a str, and zero if they are equal. - If \a cs is Qt::CaseSensitive (the default), the comparison is case sensitive; - otherwise the comparison is case-insensitive. + \include qstring.qdocinc {search-comparison-case-sensitivity} {comparison} \sa operator==(), operator<(), operator>() */ /*! - \fn int QStringView::compare(QLatin1String l1, Qt::CaseSensitivity cs) const + \fn int QStringView::compare(QUtf8StringView str, Qt::CaseSensitivity cs) const + \since 6.5 + + Compares this string view with QUtf8StringView \a str and returns a negative integer if + this string view is less than \a str, a positive integer if it is greater than + \a str, and zero if they are equal. + + \include qstring.qdocinc {search-comparison-case-sensitivity} {comparison} + + \sa operator==(), operator<(), operator>() +*/ + +/*! + \fn int QStringView::compare(QLatin1StringView l1, Qt::CaseSensitivity cs) const \fn int QStringView::compare(QChar ch) const \fn int QStringView::compare(QChar ch, Qt::CaseSensitivity cs) const \since 5.15 - Returns an integer that compares to zero as this string view compares to the - Latin-1 string \a l1, or character \a ch, respectively. + Compares this string view to the Latin-1 string view \a l1, or the character \a ch. + Returns a negative integer if this string view is less than \a l1 or \a ch, + a positive integer if it is greater than \a l1 or \a ch, and zero if they are equal. - If \a cs is Qt::CaseSensitive (the default), the comparison is case sensitive; - otherwise the comparison is case-insensitive. + \include qstring.qdocinc {search-comparison-case-sensitivity} {comparison} \sa operator==(), operator<(), operator>() */ /*! - \fn QStringView::operator==(QStringView lhs, QStringView rhs) - \fn QStringView::operator!=(QStringView lhs, QStringView rhs) - \fn QStringView::operator< (QStringView lhs, QStringView rhs) - \fn QStringView::operator<=(QStringView lhs, QStringView rhs) - \fn QStringView::operator> (QStringView lhs, QStringView rhs) - \fn QStringView::operator>=(QStringView lhs, QStringView rhs) + \fn QStringView::operator==(const QStringView &lhs, const QStringView &rhs) + \fn QStringView::operator!=(const QStringView &lhs, const QStringView &rhs) + \fn QStringView::operator< (const QStringView &lhs, const QStringView &rhs) + \fn QStringView::operator<=(const QStringView &lhs, const QStringView &rhs) + \fn QStringView::operator> (const QStringView &lhs, const QStringView &rhs) + \fn QStringView::operator>=(const QStringView &lhs, const QStringView &rhs) Operators for comparing \a lhs to \a rhs. @@ -807,85 +779,106 @@ QT_BEGIN_NAMESPACE */ /*! + \fn int QStringView::localeAwareCompare(QStringView other) const + \since 6.4 + + Compares this string view with the \a other string view and returns + an integer less than, equal to, or greater than zero if this string + view is less than, equal to, or greater than the \a other string view. + + The comparison is performed in a locale- and also platform-dependent + manner. Use this function to present sorted lists of strings to the + user. + + \sa {Comparing Strings} +*/ + +/* +//! [utf16-or-latin1-or-ch] +the UTF-16 string viewed by \a str, the Latin-1 string viewed by \a l1, +or the character \a ch +//! [utf16-or-latin1-or-ch] +*/ + +/*! \fn bool QStringView::startsWith(QStringView str, Qt::CaseSensitivity cs) const - \fn bool QStringView::startsWith(QLatin1String l1, Qt::CaseSensitivity cs) const + \fn bool QStringView::startsWith(QLatin1StringView l1, Qt::CaseSensitivity cs) const \fn bool QStringView::startsWith(QChar ch) const \fn bool QStringView::startsWith(QChar ch, Qt::CaseSensitivity cs) const - Returns \c true if this string view starts with string view \a str, - Latin-1 string \a l1, or character \a ch, respectively; - otherwise returns \c false. + Returns \c true if this string view starts with + \include qstringview.cpp utf16-or-latin1-or-ch + respectively; otherwise returns \c false. - If \a cs is Qt::CaseSensitive (the default), the search is case-sensitive; - otherwise the search is case-insensitive. + \include qstring.qdocinc {search-comparison-case-sensitivity} {search} \sa endsWith() */ /*! \fn bool QStringView::endsWith(QStringView str, Qt::CaseSensitivity cs) const - \fn bool QStringView::endsWith(QLatin1String l1, Qt::CaseSensitivity cs) const + \fn bool QStringView::endsWith(QLatin1StringView l1, Qt::CaseSensitivity cs) const \fn bool QStringView::endsWith(QChar ch) const \fn bool QStringView::endsWith(QChar ch, Qt::CaseSensitivity cs) const - Returns \c true if this string view ends with string view \a str, - Latin-1 string \a l1, or character \a ch, respectively; - otherwise returns \c false. + Returns \c true if this string view ends with + \include qstringview.cpp utf16-or-latin1-or-ch + respectively; otherwise returns \c false. - If \a cs is Qt::CaseSensitive (the default), the search is case-sensitive; - otherwise the search is case-insensitive. + \include qstring.qdocinc {search-comparison-case-sensitivity} {search} \sa startsWith() */ /*! \fn qsizetype QStringView::indexOf(QStringView str, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const - \fn qsizetype QStringView::indexOf(QLatin1String l1, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const + \fn qsizetype QStringView::indexOf(QLatin1StringView l1, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const \fn qsizetype QStringView::indexOf(QChar c, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const \since 5.14 - Returns the index position of the first occurrence of the string view \a str, - Latin-1 string \a l1, or character \a ch, respectively, in this string view, - searching forward from index position \a from. Returns -1 if \a str is not found. + Returns the index position of the first occurrence of + \include qstringview.cpp utf16-or-latin1-or-ch + respectively, in this string view, searching forward from index position + \a from. Returns -1 if \a str, \a l1 or \a ch is not found, respectively. - If \a cs is Qt::CaseSensitive (default), the search is case - sensitive; otherwise the search is case insensitive. + \include qstring.qdocinc {search-comparison-case-sensitivity} {search} - If \a from is -1, the search starts at the last character; if it is - -2, at the next to last character and so on. + \include qstring.qdocinc negative-index-start-search-from-end \sa QString::indexOf() */ /*! \fn bool QStringView::contains(QStringView str, Qt::CaseSensitivity cs) const - \fn bool QStringView::contains(QLatin1String l1, Qt::CaseSensitivity cs) const + \fn bool QStringView::contains(QLatin1StringView l1, Qt::CaseSensitivity cs) const \fn bool QStringView::contains(QChar c, Qt::CaseSensitivity cs) const \since 5.14 - Returns \c true if this string view contains an occurrence of the string view - \a str, Latin-1 string \a l1, or character \a ch; otherwise returns \c false. + Returns \c true if this string view contains an occurrence of + \include qstringview.cpp utf16-or-latin1-or-ch + respectively; otherwise returns \c false. - If \a cs is Qt::CaseSensitive (the default), the search is - case-sensitive; otherwise the search is case-insensitive. + \include qstring.qdocinc {search-comparison-case-sensitivity} {search} \sa indexOf() */ /*! \fn qsizetype QStringView::lastIndexOf(QStringView str, qsizetype from, Qt::CaseSensitivity cs) const - \fn qsizetype QStringView::lastIndexOf(QLatin1String l1, qsizetype from, Qt::CaseSensitivity cs) const + \fn qsizetype QStringView::lastIndexOf(QLatin1StringView l1, qsizetype from, Qt::CaseSensitivity cs) const \fn qsizetype QStringView::lastIndexOf(QChar c, qsizetype from, Qt::CaseSensitivity cs) const \since 5.14 - Returns the index position of the last occurrence of the string view \a str, - Latin-1 string \a l1, or character \a ch, respectively, in this string view, - searching backward from index position \a from. If \a from is -1, - the search starts at the last character; if \a from is -2, at the next to last - character and so on. Returns -1 if \a str is not found. + Returns the index position of the last occurrence of + \include qstringview.cpp utf16-or-latin1-or-ch + respectively, in this string view, searching backward from index + position \a from. - If \a cs is Qt::CaseSensitive (default), the search is case - sensitive; otherwise the search is case insensitive. + \include qstring.qdocinc negative-index-start-search-from-end + + Returns -1 if \a str, \a l1 or \a c is not found, respectively. + + \include qstring.qdocinc {search-comparison-case-sensitivity} {search} \note When searching for a 0-length \a str or \a l1, the match at the end of the data is excluded from the search by a negative \a @@ -900,17 +893,16 @@ QT_BEGIN_NAMESPACE /*! \fn qsizetype QStringView::lastIndexOf(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const - \fn qsizetype QStringView::lastIndexOf(QLatin1String l1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const + \fn qsizetype QStringView::lastIndexOf(QLatin1StringView l1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const \since 6.2 \overload lastIndexOf() - Returns the index position of the last occurrence of the string view \a str - or Latin-1 string \a l1, respectively, in this string view, - searching backward from the last character of this string view. - Returns -1 if \a str is not found. + Returns the index position of the last occurrence of the UTF-16 string viewed + by \a str or the Latin-1 string viewed by \a l1 respectively, in this string + view searching backward from the last character of this string view. Returns + -1 if \a str or \a l1 is not found, respectively. - If \a cs is Qt::CaseSensitive (default), the search is case - sensitive; otherwise the search is case insensitive. + \include qstring.qdocinc {search-comparison-case-sensitivity} {search} \sa QString::lastIndexOf() */ @@ -945,9 +937,11 @@ QT_BEGIN_NAMESPACE Returns the index position of the last match of the regular expression \a re in the string view, which starts before the index - position \a from. If \a from is -1, the search starts at the last - character; if \a from is -2, at the next to last character and so - on. Returns -1 if \a re didn't match anywhere. + position \a from. + + \include qstring.qdocinc negative-index-start-search-from-end + + Returns -1 if \a re didn't match anywhere. If the match is successful and \a rmatch is not \nullptr, it also writes the results of the match into the QRegularExpressionMatch object @@ -1103,6 +1097,32 @@ QT_BEGIN_NAMESPACE */ /*! + \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 @@ -1131,8 +1151,7 @@ QT_BEGIN_NAMESPACE Returns the number of occurrences of the character \a ch in the string view. - If \a cs is Qt::CaseSensitive (default), the search is - case sensitive; otherwise the search is case insensitive. + \include qstring.qdocinc {search-comparison-case-sensitivity} {search} \sa QString::count(), contains(), indexOf() */ @@ -1146,8 +1165,21 @@ QT_BEGIN_NAMESPACE Returns the number of (potentially overlapping) occurrences of the string view \a str in this string view. - If \a cs is Qt::CaseSensitive (default), the search is - case sensitive; otherwise the search is case insensitive. + \include qstring.qdocinc {search-comparison-case-sensitivity} {search} + + \sa QString::count(), contains(), indexOf() +*/ + +/*! + \fn qsizetype QStringView::count(QLatin1StringView l1, Qt::CaseSensitivity cs) const noexcept + + \since 6.4 + \overload count() + + Returns the number of (potentially overlapping) occurrences of the + Latin-1 string viewed by \a l1 in this string view. + + \include qstring.qdocinc {search-comparison-case-sensitivity} {search} \sa QString::count(), contains(), indexOf() */ @@ -1373,7 +1405,7 @@ QT_BEGIN_NAMESPACE /*! \fn template <typename Needle, typename...Flags> auto QStringView::tokenize(Needle &&sep, Flags...flags) const - \fn template <typename Needle, typename...Flags> auto QLatin1String::tokenize(Needle &&sep, Flags...flags) const + \fn template <typename Needle, typename...Flags> auto QLatin1StringView::tokenize(Needle &&sep, Flags...flags) const \fn template <typename Needle, typename...Flags> auto QString::tokenize(Needle &&sep, Flags...flags) const & \fn template <typename Needle, typename...Flags> auto QString::tokenize(Needle &&sep, Flags...flags) const && \fn template <typename Needle, typename...Flags> auto QString::tokenize(Needle &&sep, Flags...flags) && @@ -1412,4 +1444,13 @@ QT_BEGIN_NAMESPACE \sa QStringTokenizer, qTokenize() */ +/*! + \fn QStringView::operator std::u16string_view() const + \since 6.7 + + Converts this QStringView object to a \c{std::u16string_view} object. + The returned view will have the same data pointer and length of + this view. +*/ + QT_END_NAMESPACE |