diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2016-02-25 02:32:27 +0100 |
---|---|---|
committer | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2017-01-19 21:37:46 +0000 |
commit | 3af5cab0547fc7c6a82d94e8a2104f1b052ccd7d (patch) | |
tree | 544ca6119abae93cb65ef7b67db5b4f984f608c4 /src/corelib/tools/qbytearraymatcher.cpp | |
parent | a6cdfacf8d9c646333e81693e73e2c74173ad29a (diff) |
Long live QStaticByteArrayMatcher!
This is a version of QByteArrayMatcher that calculates the
Boyer-Moore skip table at compile-time instead of at run-time,
making this class more generally applicable than QByteArray-
Matcher itself, at least for statically-known strings.
The compile-time part requires C++14 constexpr support, but
the class should compile and work even in C++98 mode, just
with runtime initialization of the skip-table.
While touching tst_qbytearraymatcher, clean up the static
global QByteArrayMatchers there and add tests with needles
longer than 255 characters for QByteArrayMatcher, too.
[ChangeLog][QtCore] Added QStaticByteArrayMatcher.
Change-Id: I0662f262ab19b79ae4096f3ab384d5b3ada72347
Reviewed-by: David Faure <david.faure@kdab.com>
Diffstat (limited to 'src/corelib/tools/qbytearraymatcher.cpp')
-rw-r--r-- | src/corelib/tools/qbytearraymatcher.cpp | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/src/corelib/tools/qbytearraymatcher.cpp b/src/corelib/tools/qbytearraymatcher.cpp index d06ca1292a..fc9d12aba5 100644 --- a/src/corelib/tools/qbytearraymatcher.cpp +++ b/src/corelib/tools/qbytearraymatcher.cpp @@ -323,4 +323,112 @@ int qFindByteArray( return -1; } +/*! + \class QStaticByteArrayMatcherBase + \since 5.9 + \internal + \brief Non-template base class of QStaticByteArrayMatcher. +*/ + +/*! + \class QStaticByteArrayMatcher + \since 5.9 + \inmodule QtCore + \brief The QStaticByteArrayMatcher class is a compile-time version of QByteArrayMatcher + + \ingroup tools + \ingroup string-processing + + This class is useful when you have a sequence of bytes that you + want to repeatedly match against some byte arrays (perhaps in a + loop), or when you want to search for the same sequence of bytes + multiple times in the same byte array. Using a matcher object and + indexIn() is faster than matching a plain QByteArray with + QByteArray::indexOf(), in particular if repeated matching takes place. + + Unlike QByteArrayMatcher, this class calculates the internal + representation at \e{compile-time}, if your compiler supports + C++14-level \c{constexpr} (C++11 is not sufficient), so it can + even benefit if you are doing one-off byte array matches. + + Create the QStaticByteArrayMatcher by calling qMakeStaticByteArrayMatcher(), + passing it the C string literal you want to search for. Store the return + value of that function in a \c{static const auto} variable, so you don't need + to pass the \c{N} template parameter explicitly: + + \code + static const auto matcher = qMakeStaticByteArrayMatcher("needle"); + \endcode + + Then call indexIn() on the QByteArray in which you want to search, just like + with QByteArrayMatcher. + + Since this class is designed to do all the up-front calculations at compile-time, + it does not offer a setPattern() method. + + \sa QByteArrayMatcher, QStringMatcher +*/ + +/*! + \fn QStaticByteArrayMatcher::indexIn(const char *haystack, int hlen, int from) + + Searches the char string \a haystack, which has length \a hlen, from + byte position \a from (default 0, i.e. from the first byte), for + the byte array pattern() that was set in the constructor. + + Returns the position where the pattern() matched in \a haystack, or -1 if no match was found. +*/ + +/*! + \fn QStaticByteArrayMatcher::indexIn(const QByteArray &haystack, int from) + + Searches the char string \a haystack, from byte position \a from + (default 0, i.e. from the first byte), for the byte array pattern() + that was set in the constructor. + + Returns the position where the pattern() matched in \a haystack, or -1 if no match was found. +*/ + +/*! + \fn QByteArray QStaticByteArrayMatcher::pattern() const + + Returns the byte array pattern that this byte array matcher will + search for. + + \sa setPattern() +*/ + +/*! + \internal +*/ +int QStaticByteArrayMatcherBase::indexOfIn(const char *needle, uint nlen, const char *haystack, int hlen, int from) const Q_DECL_NOTHROW +{ + if (from < 0) + from = 0; + return bm_find(reinterpret_cast<const uchar *>(haystack), hlen, from, + reinterpret_cast<const uchar *>(needle), nlen, m_skiptable.data); +} + +/*! + \fn QStaticByteArrayMatcher::QStaticByteArrayMatcher(const char (&pattern)[N]) + \internal +*/ + +/*! + \fn qMakeStaticByteArrayMatcher(const char (&pattern)[N]) + \since 5.9 + \relates QStaticByteArrayMatcher + + Return a QStaticByteArrayMatcher with the correct \c{N} determined + automatically from the \a pattern passed. + + To take full advantage of this function, assign the result to an + \c{auto} variable: + + \code + static const auto matcher = qMakeStaticByteArrayMatcher("needle"); + \endcode +*/ + + QT_END_NAMESPACE |