summaryrefslogtreecommitdiffstats
path: root/src/corelib/text
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@kdab.com>2020-05-08 22:18:14 +0200
committerVolker Hilsheimer <volker.hilsheimer@qt.io>2020-05-21 05:26:18 +0200
commit899aef6ab37ce51f05c921631a56d149c6d2cd28 (patch)
tree88e8b28438d827b534a202f336184ef302c8e09e /src/corelib/text
parent0f2825685e8ad78640e92fa8277fd369a6c084c9 (diff)
QString: optimize insert() of a substring
The old code malloc()ed a buffer to hold a copy of the data if a substring of *this was to be inserted. Instead, use a QVarLengthArray. Change-Id: Ia3b4d7509bff2375ec0da5c890ecff2e9f7f335c Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io> (cherry picked from commit 2ed048fa8dd506a8e57292e49dadd37011354a83) Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/text')
-rw-r--r--src/corelib/text/qstring.cpp7
1 files changed, 2 insertions, 5 deletions
diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp
index d3a84c9149..6075285c71 100644
--- a/src/corelib/text/qstring.cpp
+++ b/src/corelib/text/qstring.cpp
@@ -2600,11 +2600,8 @@ QString& QString::insert(int i, const QChar *unicode, int size)
const std::less<const ushort*> less = {};
if (!less(s, d->data()) && less(s, d->data() + d->alloc)) {
// Part of me - take a copy
- ushort *tmp = static_cast<ushort *>(::malloc(size * sizeof(QChar)));
- Q_CHECK_PTR(tmp);
- memcpy(tmp, s, size * sizeof(QChar));
- insert(i, reinterpret_cast<const QChar *>(tmp), size);
- ::free(tmp);
+ const QVarLengthArray<ushort> copy(s, s + size);
+ insert(i, reinterpret_cast<const QChar *>(copy.data()), size);
return *this;
}