summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDennis Oberst <dennis.oberst@qt.io>2023-05-25 12:51:19 +0200
committerMarc Mutz <marc.mutz@qt.io>2023-06-04 06:33:12 +0000
commit54d8d8055edafcd571fe190eb6077d14a6ff8ce1 (patch)
treec3f60a331e5c05fc67af902c0ea041c2d9da15cd /src
parentef1be84551ae1e131dbee8212fd1bd6064e889da (diff)
QString: add STL-style assign() [1/4]: non-(it,it) overloads
Implemented assign() methods for QString to align with the criteria of std::basic_string, addressing the previously missing functionality. This is a subset of the overloads provided by the standard. Reference: https://en.cppreference.com/w/cpp/string/basic_string/assign The assign(it, it) overload is a bit more complicated and will be added in follow-up patches. [ChangeLog][QtCore][QString] Added assign(). Task-number: QTBUG-106198 Change-Id: Ia1481d184865f46db872cf94c266fef83b962351 Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Marc Mutz <marc.mutz@qt.io> Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/corelib/text/qstring.cpp46
-rw-r--r--src/corelib/text/qstring.h10
2 files changed, 56 insertions, 0 deletions
diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp
index b6b6f3c8a1..de1409960a 100644
--- a/src/corelib/text/qstring.cpp
+++ b/src/corelib/text/qstring.cpp
@@ -3310,6 +3310,52 @@ QString &QString::append(QChar ch)
*/
/*!
+ \fn QString &QString::assign(QAnyStringView v)
+ \since 6.6
+
+ Replaces the contents of this string with a copy of \a v and returns a
+ reference to this string.
+
+ The size of this string will be equal to the size of \a v, converted to
+ UTF-16 as if by \c{v.toString()}. Unlike QAnyStringView::toString(), however,
+ this function only allocates memory if the estimated size exceeds the capacity
+ of this string or this string is shared.
+
+ \sa QAnyStringView::toString()
+*/
+
+/*!
+ \fn QString &QString::assign(qsizetype n, QChar c)
+ \since 6.6
+
+ Replaces the contents of this string with \a n copies of \a c and
+ returns a reference to this string.
+
+ The size of this string will be equal to \a n, which has to be non-negative.
+
+ This function will only allocate memory if \a n exceeds the capacity of this
+ string or this string is shared.
+
+ \sa fill()
+*/
+
+QString &QString::assign(QAnyStringView s)
+{
+ if (s.size() <= capacity() && isDetached()) {
+ const auto offset = d.freeSpaceAtBegin();
+ if (offset)
+ d.setBegin(d.begin() - offset);
+ resize(0);
+ s.visit([this](auto input) {
+ this->append(input);
+ });
+ } else {
+ *this = s.toString();
+ }
+ return *this;
+}
+
+/*!
\fn QString &QString::remove(qsizetype position, qsizetype n)
Removes \a n characters from the string, starting at the given \a
diff --git a/src/corelib/text/qstring.h b/src/corelib/text/qstring.h
index 62eb64d775..71b712c3e9 100644
--- a/src/corelib/text/qstring.h
+++ b/src/corelib/text/qstring.h
@@ -36,6 +36,8 @@ Q_FORWARD_DECLARE_CF_TYPE(CFString);
Q_FORWARD_DECLARE_OBJC_CLASS(NSString);
#endif
+class tst_QString;
+
QT_BEGIN_NAMESPACE
class QRegularExpression;
@@ -116,6 +118,8 @@ constexpr QChar QAnyStringView::back() const
class Q_CORE_EXPORT QString
{
typedef QTypedArrayData<char16_t> Data;
+
+ friend class ::tst_QString;
public:
typedef QStringPrivate DataPointer;
@@ -375,6 +379,12 @@ public:
inline QString &prepend(QLatin1StringView s) { return insert(0, s); }
QString &prepend(QUtf8StringView s) { return insert(0, s); }
+ QString &assign(QAnyStringView s);
+ inline QString &assign(qsizetype n, QChar c)
+ {
+ Q_ASSERT(n >= 0);
+ return fill(c, n);
+ }
inline QString &operator+=(QChar c) { return append(c); }
inline QString &operator+=(const QString &s) { return append(s); }