From 6276427438d8247348f3bc9643d915d725f229d8 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Fri, 18 May 2012 18:58:29 +0200 Subject: [PATCH] QString::append: add (const QChar*, int len) overload MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Both insert and replace have this overload, so one reason to add it to append(), too, is consistency. But I can also make good use of this overload in the the new QStringList::join(QChar) overload, so it's actually useful in its own right. Change-Id: Iccd48f9cb84831399e4db7e3e78eba25c0ced30d Reviewed-by: João Abecasis Reviewed-by: Thiago Macieira --- src/corelib/tools/qstring.cpp | 18 +++++++++++++ src/corelib/tools/qstring.h | 1 + .../corelib/tools/qstring/tst_qstring.cpp | 26 ++++++++++++++++--- 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp index f4240e964f..5ec0b63f8e 100644 --- a/src/corelib/tools/qstring.cpp +++ b/src/corelib/tools/qstring.cpp @@ -1505,6 +1505,24 @@ QString &QString::append(const QString &str) return *this; } +/*! + \overload append() + \since 5.0 + + Appends \a len characters from the QChar array \a str to this string. +*/ +QString &QString::append(const QChar *str, int len) +{ + if (str && len > 0) { + if (d->ref.isShared() || uint(d->size + len) + 1u > d->alloc) + reallocData(uint(d->size + len) + 1u, true); + memcpy(d->data() + d->size, str, len * sizeof(QChar)); + d->size += len; + d->data()[d->size] = '\0'; + } + return *this; +} + /*! \overload append() diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h index 2edfc04502..dfc974f72e 100644 --- a/src/corelib/tools/qstring.h +++ b/src/corelib/tools/qstring.h @@ -391,6 +391,7 @@ public: inline QString &insert(int i, const QString &s) { return insert(i, s.constData(), 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); diff --git a/tests/auto/corelib/tools/qstring/tst_qstring.cpp b/tests/auto/corelib/tools/qstring/tst_qstring.cpp index 7476fa0c38..3891a715f7 100644 --- a/tests/auto/corelib/tools/qstring/tst_qstring.cpp +++ b/tests/auto/corelib/tools/qstring/tst_qstring.cpp @@ -2012,9 +2012,29 @@ void tst_QString::insert() void tst_QString::append() { - QString a; - a = "<>ABCABCABCABC"; - QCOMPARE(a.append(">"),(QString)"<>ABCABCABCABC>"); + { + QString a; + a = "<>ABCABCABCABC"; + QCOMPARE(a.append(">"),QString("<>ABCABCABCABC>")); + } + + { + QString a; + static const QChar unicode[] = { 'H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!' }; + a.append(unicode, sizeof unicode / sizeof *unicode); + QCOMPARE(a, QLatin1String("Hello, World!")); + static const QChar nl('\n'); + a.append(&nl, 1); + QCOMPARE(a, QLatin1String("Hello, World!\n")); + a.append(unicode, sizeof unicode / sizeof *unicode); + QCOMPARE(a, QLatin1String("Hello, World!\nHello, World!")); + a.append(unicode, 0); // no-op + QCOMPARE(a, QLatin1String("Hello, World!\nHello, World!")); + a.append(unicode, -1); // no-op + QCOMPARE(a, QLatin1String("Hello, World!\nHello, World!")); + a.append(0, 1); // no-op + QCOMPARE(a, QLatin1String("Hello, World!\nHello, World!")); + } } void tst_QString::append_bytearray_data()