diff options
author | David Faure <david.faure@kdab.com> | 2018-10-25 11:07:58 +0200 |
---|---|---|
committer | David Faure <david.faure@kdab.com> | 2018-11-06 09:49:10 +0000 |
commit | fc88dd52a42da682cbd360916be7c9f94a69b72c (patch) | |
tree | f658891358cf0ef572672386c187783ffffd7ff9 /src/corelib/tools/qbytearraylist.cpp | |
parent | a76f8caf29c9acf83f12dfe90f68cd3f13e45456 (diff) |
QByteArrayList: add indexOf(const char*) overload
This avoids memory allocation and data copying in e.g.
QObject::property().
Detected by heaptrack's "Temporary allocations" counter in an
application using the breeze widget style (many animations).
Change-Id: Iabdb58a3e504cb121cce906ef707b0722de89df6
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/tools/qbytearraylist.cpp')
-rw-r--r-- | src/corelib/tools/qbytearraylist.cpp | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/src/corelib/tools/qbytearraylist.cpp b/src/corelib/tools/qbytearraylist.cpp index c815e766ab..d04555ed4d 100644 --- a/src/corelib/tools/qbytearraylist.cpp +++ b/src/corelib/tools/qbytearraylist.cpp @@ -150,4 +150,26 @@ QByteArray QtPrivate::QByteArrayList_join(const QByteArrayList *that, const char return res; } +/*! + \fn int QByteArrayList::indexOf(const char *needle, int from) const + + Returns the index position of the first occurrence of \a needle in + the list, searching forward from index position \a from. Returns + -1 if no item matched. + + \a needle must be NUL-terminated. + + This overload doesn't require creating a QByteArray, thus saving a + memory allocation and some CPU time. + + \since 5.13 + \overload +*/ + +int QtPrivate::QByteArrayList_indexOf(const QByteArrayList *that, const char *needle, int from) +{ + const auto it = std::find_if(that->begin() + from, that->end(), [needle](const QByteArray &item) { return item == needle; }); + return it == that->end() ? -1 : int(std::distance(that->begin(), it)); +} + QT_END_NAMESPACE |