6.0.0-rc4Source

QStringMatcher Class

The QStringMatcher class holds a sequence of characters that can be quickly matched in a Unicode string. More…

Header#include <QStringMatcher>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt Core C++ Classes

List of all members, including inherited members

Public Functions

QStringMatcher()
QStringMatcher(const QString &pattern, Qt::CaseSensitivity cs = Qt::CaseSensitive)
QStringMatcher(QStringView pattern, Qt::CaseSensitivity cs = Qt::CaseSensitive)
QStringMatcher(const QChar *uc, qsizetype length, Qt::CaseSensitivity cs = Qt::CaseSensitive)
voidsetPattern(const QString &pattern)
voidsetCaseSensitivity(Qt::CaseSensitivity cs)
QStringpattern() const
QStringViewpatternView() const
Qt::CaseSensitivitycaseSensitivity() const
qsizetypeindexIn(const QString &str, qsizetype from = 0) const
qsizetypeindexIn(QStringView str, qsizetype from = 0) const
qsizetypeindexIn(const QChar *str, qsizetype length, qsizetype from = 0) const

Detailed Description

Searching for the same pattern in many strings is faster with a QStringMatcher than with QString::indexOf(), because the matcher prepares a skip table for the pattern once:

QStringMatcher matcher(u"needle");
for (const QString &line : lines)
    if (matcher.indexIn(line) != -1)
        ++hits;

See also QString::indexOf() and QStringView.

Differences from Qt

The matcher uses the Boyer-Moore-Horspool algorithm on UTF-16 code units. Case-insensitive matching folds each code unit with QChar::toCaseFolded(), so characters outside the Basic Multilingual Plane are compared exactly.

Member Function Documentation

QStringMatcher::QStringMatcher()

Constructs an empty matcher that matches nothing. Call setPattern() to give it a pattern.

[explicit] QStringMatcher::QStringMatcher(const QString &pattern, Qt::CaseSensitivity cs = Qt::CaseSensitive)

Constructs a matcher for pattern with the case sensitivity cs.

[explicit] QStringMatcher::QStringMatcher(QStringView pattern, Qt::CaseSensitivity cs = Qt::CaseSensitive)

This is an overloaded function.

Constructs a matcher for the characters viewed by pattern with the case sensitivity cs.

This function was introduced in 5.14.

QStringMatcher::QStringMatcher(const QChar *uc, qsizetype length, Qt::CaseSensitivity cs = Qt::CaseSensitive)

This is an overloaded function.

Constructs a matcher for the length characters at uc with the case sensitivity cs.

Qt::CaseSensitivity QStringMatcher::caseSensitivity() const

Returns the case sensitivity of the matcher.

See also setCaseSensitivity().

qsizetype QStringMatcher::indexIn(const QString &str, qsizetype from = 0) const

Searches the string str from index position from (default 0) and returns the index of the first occurrence of the pattern, or -1 if it is not found. A negative from counts from the end of the string.

qsizetype QStringMatcher::indexIn(QStringView str, qsizetype from = 0) const

This is an overloaded function.

Searches the characters viewed by str from index position from.

This function was introduced in 5.14.

qsizetype QStringMatcher::indexIn(const QChar *str, qsizetype length, qsizetype from = 0) const

This is an overloaded function.

Searches the length characters at str from index position from.

QString QStringMatcher::pattern() const

Returns the pattern that the matcher searches for.

See also setPattern().

QStringView QStringMatcher::patternView() const

Returns a view of the pattern that the matcher searches for. The view is valid as long as the matcher and its pattern are not changed.

This function was introduced in 6.0.

void QStringMatcher::setCaseSensitivity(Qt::CaseSensitivity cs)

Sets the case sensitivity of the matcher to cs.

See also caseSensitivity() and setPattern().

void QStringMatcher::setPattern(const QString &pattern)

Sets the pattern to search for to pattern.

See also pattern() and setCaseSensitivity().