From 3238445b270d5fb98f4008cb10a8adcc2d35165c Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Sun, 25 Oct 2015 19:02:40 +0100 Subject: QString: add append, op+=, prepend, insert taking QStringView [ChangeLog][QtCore][QString] Now supports appending, prepending and inserting QStringViews. Change-Id: I7538c050c67590f27d91443eda0b94a4b80b62f2 Reviewed-by: Anton Kudryavtsev Reviewed-by: Lars Knoll --- src/corelib/text/qstring.cpp | 35 +++++++++++++++++++++++++ src/corelib/text/qstring.h | 14 ++++++++++ tests/auto/corelib/text/qstring/tst_qstring.cpp | 25 ++++++++++++++++++ 3 files changed, 74 insertions(+) diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index 3f24665a8a..fa61b5939d 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -2500,6 +2500,19 @@ QString &QString::operator=(QChar ch) */ +/*! + \fn QString& QString::insert(int position, QStringView str) + \since 6.0 + \overload insert() + + Inserts the string view \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(). +*/ + + /*! \fn QString& QString::insert(int position, const char *str) \since 5.5 @@ -2765,6 +2778,14 @@ QString &QString::append(QChar ch) returns a reference to this string. */ +/*! \fn QString &QString::prepend(QStringView str) + \since 6.0 + \overload prepend() + + Prepends the string view \a str to the beginning of this string and + returns a reference to this string. +*/ + /*! \fn QString &QString::prepend(const QByteArray &ba) \overload prepend() @@ -5914,6 +5935,13 @@ 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 6.0 + \overload operator+=() + + Appends the string view \a str to this string. +*/ + /*! \fn QString &QString::operator+=(char ch) \overload operator+=() @@ -11037,6 +11065,13 @@ QStringRef QStringRef::appendTo(QString *string) const lists of strings to the user. */ +/*! + \fn QString &QString::append(QStringView str) + \since 6.0 + + Appends the given string view \a str to this string and returns the result. +*/ + /*! \fn QString &QString::append(const QStringRef &reference) \since 4.4 diff --git a/src/corelib/text/qstring.h b/src/corelib/text/qstring.h index a841a0c123..f099683e28 100644 --- a/src/corelib/text/qstring.h +++ b/src/corelib/text/qstring.h @@ -537,18 +537,29 @@ public: QString &insert(int i, QChar c); QString &insert(int i, const QChar *uc, int len); +#if QT_STRINGVIEW_LEVEL < 2 inline QString &insert(int i, const QString &s) { return insert(i, s.constData(), s.length()); } inline QString &insert(int i, const QStringRef &s); +#endif + inline QString &insert(int i, QStringView v) { return insert(i, v.data(), v.length()); } QString &insert(int i, QLatin1String s); + QString &append(QChar c); QString &append(const QChar *uc, int len); +#if QT_STRINGVIEW_LEVEL < 2 QString &append(const QString &s); QString &append(const QStringRef &s); +#endif + inline QString &append(QStringView v) { return append(v.data(), v.length()); } QString &append(QLatin1String s); + inline QString &prepend(QChar c) { return insert(0, c); } inline QString &prepend(const QChar *uc, int len) { return insert(0, uc, len); } +#if QT_STRINGVIEW_LEVEL < 2 inline QString &prepend(const QString &s) { return insert(0, s); } inline QString &prepend(const QStringRef &s) { return insert(0, s); } +#endif + inline QString &prepend(QStringView v) { return prepend(v.data(), v.length()); } inline QString &prepend(QLatin1String s) { return insert(0, s); } inline QString &operator+=(QChar c) { @@ -560,8 +571,11 @@ public: } inline QString &operator+=(QChar::SpecialCharacter c) { return append(QChar(c)); } +#if QT_STRINGVIEW_LEVEL < 2 inline QString &operator+=(const QString &s) { return append(s); } inline QString &operator+=(const QStringRef &s) { return append(s); } +#endif + inline QString &operator+=(QStringView v) { return append(v); } inline QString &operator+=(QLatin1String s) { return append(s); } QString &remove(int i, int len); diff --git a/tests/auto/corelib/text/qstring/tst_qstring.cpp b/tests/auto/corelib/text/qstring/tst_qstring.cpp index 2e7b0fb1ed..088225431c 100644 --- a/tests/auto/corelib/text/qstring/tst_qstring.cpp +++ b/tests/auto/corelib/text/qstring/tst_qstring.cpp @@ -137,6 +137,23 @@ public: { (s.*mf)(a1, ref()); } }; +template <> +class Arg : ArgBase +{ + QStringView view() const + { return this->pinned.isNull() ? QStringView() : QStringView(this->pinned) ; } +public: + explicit Arg(const char *str) : ArgBase(str) {} + + template + void apply0(QString &s, MemFun mf) const + { (s.*mf)(view()); } + + template + void apply1(QString &s, MemFun mf, A1 a1) const + { (s.*mf)(a1, view()); } +}; + template <> class Arg > : ArgBase { @@ -379,6 +396,8 @@ private slots: void prepend_qstring_data() { prepend_data(true); } void prepend_qstringref() { prepend_impl(); } void prepend_qstringref_data() { prepend_data(true); } + void prepend_qstringview() { prepend_impl(); } + void prepend_qstringview_data() { prepend_data(true); } void prepend_qlatin1string() { prepend_impl(); } void prepend_qlatin1string_data() { prepend_data(true); } void prepend_qcharstar_int() { prepend_impl, QString &(QString::*)(const QChar *, int)>(); } @@ -398,6 +417,8 @@ private slots: void append_qstring_data() { append_data(); } void append_qstringref() { append_impl(); } void append_qstringref_data() { append_data(); } + void append_qstringview() { append_impl(); } + void append_qstringview_data() { append_data(true); } void append_qlatin1string() { append_impl(); } void append_qlatin1string_data() { append_data(); } void append_qcharstar_int() { append_impl, QString&(QString::*)(const QChar *, int)>(); } @@ -418,6 +439,8 @@ private slots: void operator_pluseq_qstring_data() { operator_pluseq_data(); } void operator_pluseq_qstringref() { operator_pluseq_impl(); } void operator_pluseq_qstringref_data() { operator_pluseq_data(); } + void operator_pluseq_qstringview() { operator_pluseq_impl(); } + void operator_pluseq_qstringview_data() { operator_pluseq_data(true); } void operator_pluseq_qlatin1string() { operator_pluseq_impl(); } void operator_pluseq_qlatin1string_data() { operator_pluseq_data(); } void operator_pluseq_qchar() { operator_pluseq_impl(); } @@ -440,6 +463,8 @@ private slots: void insert_qstring_data() { insert_data(true); } void insert_qstringref() { insert_impl(); } void insert_qstringref_data() { insert_data(true); } + void insert_qstringview() { insert_impl(); } + void insert_qstringview_data() { insert_data(true); } void insert_qlatin1string() { insert_impl(); } void insert_qlatin1string_data() { insert_data(true); } void insert_qcharstar_int() { insert_impl, QString &(QString::*)(int, const QChar*, int) >(); } -- cgit v1.2.3