QStaticByteArrayMatcher Class
template <size_t N> class QStaticByteArrayMatcherQStaticByteArrayMatcher 类是QByteArrayMatcher 的编译时版本。更多内容...
| 头文件: | #include <QStaticByteArrayMatcher> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Core) target_link_libraries(mytarget PRIVATE Qt6::Core) |
| qmake: | QT += core |
- 所有成员的列表,包括继承的成员
- QStaticByteArrayMatcher 属于字符串数据类。
公共函数
| 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 (&pattern)[N]) |
详细说明
QStaticByteArrayMatcher<N> 是一个模板类,其中N 是要搜索的字节数组模式的长度(包括字符串文字的空终止符)。 当您有一段字节序列,需要将其与某些字节数组反复进行匹配(例如在循环中),或者需要在同一个字节数组中多次搜索相同的字节序列时,此类非常有用。 使用匹配器对象和 `indexIn()` 比使用 `QByteArray::indexOf()` 匹配普通的 `QByteArray ` 更快,特别是在进行重复匹配时。
与QByteArrayMatcher 不同,该类在编译时就计算出了内部表示,因此即使您只进行一次字节数组匹配,也能从中受益。
通过调用qMakeStaticByteArrayMatcher() 并传入要搜索的 C 字符串字面量来创建 QStaticByteArrayMatcher。将该函数的返回值存储在static const auto 变量中,这样就无需显式传递N 模板参数:
static const auto matcher = qMakeStaticByteArrayMatcher("needle");然后,像使用QByteArrayMatcher 一样,对要搜索的QByteArray 调用indexIn()。
由于该类旨在在编译时完成所有预先计算,因此它不提供 setPattern() 方法。
另请参阅 QByteArrayMatcher 和QStringMatcher 。
成员函数文档
[noexcept] qsizetype QStaticByteArrayMatcher::indexIn(const QByteArray &haystack, qsizetype from = 0) const
从字节位置from (默认值为 0,即从第一个字节开始)开始,在字符串haystack 中搜索在构造函数中设置的字节数组pattern()。
返回pattern() 在haystack 中匹配到的位置,若未找到匹配项,则返回 -1。
[noexcept] qsizetype QStaticByteArrayMatcher::indexIn(const char *haystack, qsizetype hlen, qsizetype from = 0) const
从字节位置from (默认值为 0,即从第一个字节开始)开始,在长度为hlen 的字符串haystack 中搜索构造函数中设置的字节数组pattern()。
返回pattern() 在haystack 中匹配到的位置,若未找到匹配项,则返回 -1。
QByteArray QStaticByteArrayMatcher::pattern() const
返回此字节数组匹配器将用于搜索的字节数组模式。
另请参阅 QByteArrayMatcher::setPattern()。
相关的非成员
[constexpr noexcept] template <size_t N> QStaticByteArrayMatcher<N> qMakeStaticByteArrayMatcher(const char (&pattern)[N])
返回一个QStaticByteArrayMatcher ,其中包含根据传入的pattern 自动确定的正确N 。
要充分利用此函数,请将结果赋值给一个auto 变量:
static const auto matcher = qMakeStaticByteArrayMatcher("needle");© 2026 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.