- Qt 6.7
- Qt Core
- C++ Classes
- QByteArrayMatcher
- Qt 6.7.0 Reference Documentation

# QByteArrayMatcher Class

The QByteArrayMatcher class holds a sequence of bytes that can be quickly matched in a byte array. More...

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

- List of all members, including inherited members
- QByteArrayMatcher is part of Classes for String Data.

## Public Functions

| QByteArrayMatcher()
| QByteArrayMatcher(const QByteArray &pattern)
| QByteArrayMatcher(QByteArrayView pattern)
| QByteArrayMatcher(const char *pattern, qsizetype length = -1)
| QByteArrayMatcher(const QByteArrayMatcher &other)
| ~QByteArrayMatcher()
qsizetype | indexIn(const char *str, qsizetype len, qsizetype from = 0) const
qsizetype | indexIn(QByteArrayView data, qsizetype from = 0) const
QByteArray | pattern() const
void | setPattern(const QByteArray &pattern)
QByteArrayMatcher & | operator=(const QByteArrayMatcher &other)

## Detailed Description

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() if repeated matching takes place. This class offers no benefit if you are doing one-off byte array matches.

Create the QByteArrayMatcher with the QByteArray you want to search for. Then call indexIn() on the QByteArray that you want to search.

See also QByteArray and QStringMatcher.

## Member Function Documentation

### QByteArrayMatcher::QByteArrayMatcher()

Constructs an empty byte array matcher that won't match anything. Call setPattern() to give it a pattern to match.

### `[explicit]` QByteArrayMatcher::QByteArrayMatcher(const QByteArray &pattern)

Constructs a byte array matcher that will search for pattern. Call indexIn() to perform a search.

### `[explicit, since 6.3]` QByteArrayMatcher::QByteArrayMatcher(QByteArrayView pattern)

This is an overloaded function.

Constructs a byte array matcher that will search for pattern. Call indexIn() to perform a search.

Note: the data that pattern is referencing must remain valid while this object is used.

This function was introduced in Qt 6.3.

### `[explicit]` QByteArrayMatcher::QByteArrayMatcher(const char *pattern, qsizetype length = -1)

Constructs a byte array matcher from pattern. pattern has the given length. Call indexIn() to perform a search.

Note: the data that pattern is referencing must remain valid while this object is used.

### QByteArrayMatcher::QByteArrayMatcher(const QByteArrayMatcher &other)

Copies the other byte array matcher to this byte array matcher.

### `[noexcept]` QByteArrayMatcher::~QByteArrayMatcher()

Destroys the byte array matcher.

### qsizetype QByteArrayMatcher::indexIn(const char *str, qsizetype len, qsizetype from = 0) const

Searches the char string str, which has length len, from byte position from (default 0, i.e. from the first byte), for the byte array pattern() that was set in the constructor or in the most recent call to setPattern(). Returns the position where the pattern() matched in str, or -1 if no match was found.

### `[since 6.3]` qsizetype QByteArrayMatcher::indexIn(QByteArrayView data, qsizetype from = 0) const

This is an overloaded function.

Searches the byte array data, from byte position from (default 0, i.e. from the first byte), for the byte array pattern() that was set in the constructor or in the most recent call to setPattern(). Returns the position where the pattern() matched in data, or -1 if no match was found.

This function was introduced in Qt 6.3.

### QByteArray QByteArrayMatcher::pattern() const

Returns the byte array pattern that this byte array matcher will search for.

See also setPattern().

### void QByteArrayMatcher::setPattern(const QByteArray &pattern)

Sets the byte array that this byte array matcher will search for to pattern.

See also pattern() and indexIn().

### QByteArrayMatcher &QByteArrayMatcher::operator=(const QByteArrayMatcher &other)

Assigns the other byte array matcher to this byte array matcher.