QStaticByteArrayMatcher 类

template <size_t N> class QStaticByteArrayMatcher

QStaticByteArrayMatcher 类是编译时版本的 QByteArrayMatcher . 更多...

头: #include <QStaticByteArrayMatcher>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core

公共函数

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

详细描述

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 , 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 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.

另请参阅 QByteArrayMatcher and QStringMatcher .

成员函数文档编制

[noexcept] qsizetype QStaticByteArrayMatcher:: indexIn (const QByteArray & haystack , qsizetype from = 0) 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.

[noexcept] qsizetype QStaticByteArrayMatcher:: indexIn (const char * haystack , qsizetype hlen , qsizetype from = 0) 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 ().

相关非成员

[constexpr noexcept] template <size_t N> QStaticByteArrayMatcher < N > qMakeStaticByteArrayMatcher (const char (&)[ N ] pattern )

返回 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");