QStaticByteArrayMatcher 類

The QStaticByteArrayMatcher class is a compile-time version of QByteArrayMatcher . 更多...

頭: #include <QStaticByteArrayMatcher>
qmake: QT += core
Since: Qt 5.9

公共函數

int indexIn (const QByteArray & haystack , int from = ...) const
int indexIn (const char * haystack , int hlen , int from = ...) const
QByteArray pattern () const
QStaticByteArrayMatcher<N> qMakeStaticByteArrayMatcher (const char (&)[N] pattern = N)

詳細描述

The QStaticByteArrayMatcher class is a compile-time version of QByteArrayMatcher .

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.

不像 QByteArrayMatcher , this class calculates the internal representation at compile-time , if your compiler supports C++14-level constexpr (C++11 is not sufficient), so it can even benefit if you are doing one-off byte array matches.

創建 QStaticByteArrayMatcher 通過調用 qMakeStaticByteArrayMatcher (), passing it the C string literal you want to search for. Store the return value of that function in a static const auto variable, so you don't need to pass the N template parameter explicitly:

static const auto matcher = qMakeStaticByteArrayMatcher("needle");
					

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.

注意: Qt 檢測必要 C++ 14 編譯器支持,通過特徵測試推薦從 C++ 委員會標準文檔 6 .

另請參閱 QByteArrayMatcher and QStringMatcher .

成員函數文檔編製

int QStaticByteArrayMatcher:: indexIn (const QByteArray & haystack , int from = ...) const

Searches the char string haystack , from byte position 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 haystack , or -1 if no match was found.

int QStaticByteArrayMatcher:: indexIn (const char * haystack , int hlen , int from = ...) const

Searches the char string haystack , which has length hlen , from byte position 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 haystack , or -1 if no match was found.

QByteArray QStaticByteArrayMatcher:: pattern () const

Returns the byte array pattern that this byte array matcher will search for.

另請參閱 QByteArrayMatcher::setPattern ().

相關非成員

QStaticByteArrayMatcher < N > qMakeStaticByteArrayMatcher (const char (&)[ N ] pattern = N)

返迴 QStaticByteArrayMatcher with the correct N determined automatically from the pattern passed.

To take full advantage of this function, assign the result to an auto 變量:

static const auto matcher = qMakeStaticByteArrayMatcher("needle");
					

該函數在 Qt 5.9 引入。