QStaticByteArrayMatcher Class

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)

详细说明

该类在以下情况下非常有用:当您有一个字节序列,想反复与某些字节数组匹配时(也许是在循环中),或者当您想在同一个字节数组中多次搜索相同的字节序列时。使用匹配器对象和indexIn() 比使用QByteArray::indexOf() 匹配普通QByteArray 更快,尤其是在重复匹配的情况下。

QByteArrayMatcher 不同,该类会在编译时计算内部表示,因此如果您要进行一次性的字节数组匹配,该类甚至能让您受益。

调用qMakeStaticByteArrayMatcher() 创建 QStaticByteArrayMatcher,并将要搜索的 C 字符串字面传递给它。将该函数的返回值存储在static const auto 变量中,因此无需明确传递N 模板参数:

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

然后在要搜索的QByteArray 上调用indexIn() ,就像使用QByteArrayMatcher 一样。

由于该类是在编译时进行所有前期计算的,因此不提供 setPattern() 方法。

另请参见 QByteArrayMatcherQStringMatcher

成员函数文档

[noexcept] qsizetype QStaticByteArrayMatcher::indexIn(const QByteArray &haystack, qsizetype from = 0) const

搜索字符串haystack ,从字节位置from (默认为 0,即从第一个字节开始),查找构造函数中设置的字节数组pattern() 。

返回pattern() 在haystack 中匹配的位置,如果没有找到匹配,则返回-1。

[noexcept] qsizetype QStaticByteArrayMatcher::indexIn(const char *haystack, qsizetype hlen, qsizetype from = 0) const

在长度为hlen 的字符串haystack 中,从字节位置from (默认为 0,即从第一个字节开始)搜索构造函数中设置的字节数组pattern() 。

返回pattern() 在haystack 中匹配的位置,如果未找到匹配位置,则返回-1。

QByteArray QStaticByteArrayMatcher::pattern() const

返回字节数组匹配器要搜索的字节数组模式。

另请参阅 QByteArrayMatcher::setPattern()。

相关非成员

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

返回一个QStaticByteArrayMatcher ,其正确的N 会根据传递的pattern 自动确定。

要充分利用此函数,请将结果赋值给auto 变量:

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

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.