QStaticLatin1StringMatcher Class
template <Qt::CaseSensitivity CS, size_t N> class QStaticLatin1StringMatcherQStaticLatin1StringMatcher 类是QLatin1StringMatcher 的编译时版本。更多内容...
| 头文件: | #include <QStaticLatin1StringMatcher> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS Core) target_link_libraries(mytarget PRIVATE Qt6::Core) |
| qmake: | QT += core |
| 自: | Qt 6.7 |
- 所有成员(包括继承的成员)列表
- QStaticLatin1StringMatcher 属于字符串数据类。
公共函数
| qsizetype | indexIn(QLatin1StringView haystack, qsizetype from = 0) const |
| qsizetype | indexIn(QStringView haystack, qsizetype from = 0) const |
相关的非成员
(since 6.7) auto | qMakeStaticCaseInsensitiveLatin1StringMatcher(const char (&patternToMatch)[N]) |
(since 6.7) auto | qMakeStaticCaseSensitiveLatin1StringMatcher(const char (&patternToMatch)[N]) |
详细说明
QStaticLatin1StringMatcher<CS, N> 是一个模板类,其中 `CS ` 是用于匹配的 `Qt::CaseSensitivity `,而 `N ` 是要搜索的模式的长度(包括字符串字面量的空终止符)。
当您的代码需要在 Latin-1 字符串中高效搜索编译时已知的子字符串时,此类非常有用。例如,在解析器中,这种情况很常见。 使用匹配器对象的indexIn() 方法比使用待搜索字符串的 indexOf() 成员方法更快,特别是在需要反复搜索该子字符串,或者在大型 Latin-1 字符串中进行搜索时——这类字符串可能包含许多与待查找子字符串前缀匹配的项。
与QLatin1StringMatcher 不同,该类在编译时就计算出了内部表示形式,因此即使您仅进行一次性的Latin-1字符串匹配,它也能带来好处。
通过调用qMakeStaticCaseSensitiveLatin1StringMatcher()或qMakeStaticCaseInsensitiveLatin1StringMatcher()并传入作为C字符串字面量的待搜索Latin-1字符串,来创建QStaticLatin1StringMatcher。将该函数的返回值存储在static constexpr auto 变量中,这样你就无需显式地传递N 模板参数。
然后,对要搜索的QLatin1StringView 调用indexIn(),就像对QLatin1StringMatcher 一样。
由于该类旨在在编译时完成所有预先计算,因此它不提供 setPattern() 或 setCaseSensitivity() 方法。
注意: 目前不支持INTEGRITY 操作系统。
另请参阅 QLatin1StringMatcher 、QStaticByteArrayMatcher 和QByteArrayMatcher 。
成员函数文档
[constexpr noexcept] qsizetype QStaticLatin1StringMatcher::indexIn(QLatin1StringView haystack, qsizetype from = 0) const
[constexpr noexcept] qsizetype QStaticLatin1StringMatcher::indexIn(QStringView haystack, qsizetype from = 0) const
在QLatin1StringView haystack 中,从字节位置from (默认值为0,即从第一个字节开始)开始,搜索在构造函数中设置的QLatin1StringView pattern()模式。采用同样在构造函数中设置的大小写敏感规则。
返回haystack 中pattern()匹配的位置,若未找到匹配项,则返回-1。
相关非成员
[constexpr noexcept, since 6.7] template <size_t N> auto qMakeStaticCaseInsensitiveLatin1StringMatcher(const char (&patternToMatch)[N])
返回一个QStaticLatin1StringMatcher ,其中N 值由传入的patternToMatch 自动确定,且不区分大小写。
要充分利用此函数,请将结果赋值给一个static constexpr auto 变量:
static constexpr auto matcher = qMakeStaticCaseInsensitiveLatin1StringMatcher("needle");该函数于 Qt 6.7 中引入。
[constexpr noexcept, since 6.7] template <size_t N> auto qMakeStaticCaseSensitiveLatin1StringMatcher(const char (&patternToMatch)[N])
返回一个QStaticLatin1StringMatcher ,其中N 会根据传入的patternToMatch 自动确定,且区分大小写。
要充分利用此函数,请将结果赋值给一个static constexpr auto 变量:
static constexpr auto matcher = qMakeStaticCaseSensitiveLatin1StringMatcher("needle");该函数于 Qt 6.7 中引入。
© 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.