summaryrefslogtreecommitdiffstats
path: root/src/corelib/text
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2021-08-30 09:32:30 -0700
committerThiago Macieira <thiago.macieira@intel.com>2021-09-01 11:32:03 -0700
commitf3743073a70086cfcf39a2daf2c605993ab25ce3 (patch)
treee8009fefede0d0ad30db95c7b27b6925df16eb28 /src/corelib/text
parent9258c509363bf118c67cc11e7479e08819d4f566 (diff)
qstrnlen: micro-optimize further
This is the kind of loop that the autovectorizer is pretty good at, but this is really just a type of memchr, so help dumber compilers and build modes without vectorization. Drive-up fix the style of the test code. Change-Id: Ie72b0dd0fbe84d2caae0fffd16a022a35fa24c17 Reviewed-by: Ievgenii Meshcheriakov <ievgenii.meshcheriakov@qt.io> Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'src/corelib/text')
-rw-r--r--src/corelib/text/qbytearrayalgorithms.h10
1 files changed, 4 insertions, 6 deletions
diff --git a/src/corelib/text/qbytearrayalgorithms.h b/src/corelib/text/qbytearrayalgorithms.h
index 638af2948b..a78e6e1709 100644
--- a/src/corelib/text/qbytearrayalgorithms.h
+++ b/src/corelib/text/qbytearrayalgorithms.h
@@ -96,12 +96,10 @@ inline size_t qstrlen(const char *str)
inline size_t qstrnlen(const char *str, size_t maxlen)
{
- size_t length = 0;
- if (str) {
- while (length < maxlen && *str++)
- length++;
- }
- return length;
+ if (!str)
+ return 0;
+ auto end = static_cast<const char *>(memchr(str, '\0', maxlen));
+ return end ? end - str : maxlen;
}
// implemented in qbytearray.cpp