diff --git a/src/corelib/configure.cmake b/src/corelib/configure.cmake index 7c07aa1c88a..b34086c8ada 100644 --- a/src/corelib/configure.cmake +++ b/src/corelib/configure.cmake @@ -324,6 +324,22 @@ linkat(AT_FDCWD, \"foo\", AT_FDCWD, \"bar\", AT_SYMLINK_FOLLOW); } ") +# memmem +qt_config_compile_test(memmem + LABEL "memmem()" + CODE +#define _APPLE_SAUCE 1 /* Apple doesn't require anything */ +"#define _BSD_SOURCE 1 /* For FreeBSD */ +#define _GNU_SOURCE 1 /* For glibc, Bionic */ +#include + +int main(void) +{ + const void *r = memmem(\"abc\", 3, \"bc\", 2); + (void)r; + return 0; +}") + # memrchr qt_config_compile_test(memrchr LABEL "memrchr()" @@ -569,6 +585,10 @@ qt_feature("std-atomic64" PUBLIC LABEL "64 bit atomic operations" CONDITION WrapAtomic_FOUND ) +qt_feature("memmem" PRIVATE + LABEL "C library function memmem()" + CONDITION TEST_memmem +) qt_feature("memrchr" PRIVATE LABEL "C library function memrchr()" CONDITION TEST_memrchr diff --git a/src/corelib/global/qconfig-bootstrapped.h b/src/corelib/global/qconfig-bootstrapped.h index 0596b9d9f4c..c2fe4661f6c 100644 --- a/src/corelib/global/qconfig-bootstrapped.h +++ b/src/corelib/global/qconfig-bootstrapped.h @@ -72,6 +72,7 @@ # define QT_FEATURE_linkat -1 #endif #define QT_FEATURE_lttng -1 +#define QT_FEATURE_memmem -1 #define QT_FEATURE_memrchr -1 #define QT_NO_QOBJECT #define QT_FEATURE_process -1 diff --git a/src/corelib/text/qbytearraymatcher.cpp b/src/corelib/text/qbytearraymatcher.cpp index a332f035efa..9f27e10f3d5 100644 --- a/src/corelib/text/qbytearraymatcher.cpp +++ b/src/corelib/text/qbytearraymatcher.cpp @@ -3,6 +3,11 @@ #include "qbytearraymatcher.h" +#include +#ifndef QT_BOOTSTRAPPED +# include +#endif + #include QT_BEGIN_NAMESPACE @@ -238,8 +243,10 @@ qsizetype QtPrivate::findByteArray(QByteArrayView haystack, qsizetype from, QByt const auto haystack0 = haystack.data(); const auto l = haystack.size(); const auto sl = needle.size(); +#if !QT_CONFIG(memmem) if (sl == 1) return findByteArray(haystack, from, needle.front()); +#endif if (from < 0) from += l; @@ -250,6 +257,11 @@ qsizetype QtPrivate::findByteArray(QByteArrayView haystack, qsizetype from, QByt if (!l) return -1; +#if QT_CONFIG(memmem) + auto where = memmem(haystack0 + from, l - from, needle.data(), sl); + return where ? static_cast(where) - haystack0 : -1; +#endif + /* We use the Boyer-Moore algorithm in cases where the overhead for the skip table should pay off, otherwise we use a simple