Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qbytearraymatcher.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QBYTEARRAYMATCHER_H
5#define QBYTEARRAYMATCHER_H
6
7#include <QtCore/qbytearray.h>
8
9#include <QtCore/q20algorithm.h>
10#include <iterator>
11#include <limits>
12
14
15
16class QByteArrayMatcherPrivate;
17
18class Q_CORE_EXPORT QByteArrayMatcher
19{
20public:
22 explicit QByteArrayMatcher(const QByteArray &pattern);
26 explicit QByteArrayMatcher(const char *pattern, qsizetype length = -1);
29
30 QByteArrayMatcher &operator=(const QByteArrayMatcher &other);
31
32 void setPattern(const QByteArray &pattern);
33
34#if QT_CORE_REMOVED_SINCE(6, 3)
35 qsizetype indexIn(const QByteArray &ba, qsizetype from = 0) const;
36#else
38 qsizetype indexIn(const QByteArray &ba, qsizetype from = 0) const
39 { return indexIn(QByteArrayView{ba}, from); }
40#endif
41 qsizetype indexIn(const char *str, qsizetype len, qsizetype from = 0) const;
42 qsizetype indexIn(QByteArrayView data, qsizetype from = 0) const;
43 inline QByteArray pattern() const
44 {
45 if (q_pattern.isNull())
46 return QByteArray(reinterpret_cast<const char*>(p.p), p.l);
47 return q_pattern;
48 }
49
50private:
51 QByteArrayMatcherPrivate *d;
52 QByteArray q_pattern;
53 struct Data {
54 uchar q_skiptable[256];
55 const uchar *p;
56 qsizetype l;
57 };
58 union {
59 uint dummy[256];
60 Data p;
61 };
62};
63
65{
66 alignas(16)
67 struct Skiptable {
68 uchar data[256];
69 } m_skiptable;
70protected:
71 explicit constexpr QStaticByteArrayMatcherBase(const char *pattern, size_t n) noexcept
72 : m_skiptable(generate(pattern, n)) {}
73 // compiler-generated copy/more ctors/assignment operators are ok!
75
76#if QT_CORE_REMOVED_SINCE(6, 3) && QT_POINTER_SIZE != 4
77 Q_CORE_EXPORT int indexOfIn(const char *needle, uint nlen, const char *haystack, int hlen, int from) const noexcept;
78#endif
79 Q_CORE_EXPORT qsizetype indexOfIn(const char *needle, size_t nlen,
80 const char *haystack, qsizetype hlen,
81 qsizetype from) const noexcept;
82
83private:
84 static constexpr Skiptable generate(const char *pattern, size_t n) noexcept
85 {
86 const auto uchar_max = (std::numeric_limits<uchar>::max)();
87 uchar max = n > uchar_max ? uchar_max : uchar(n);
88 Skiptable table = {};
89 q20::fill(std::begin(table.data), std::end(table.data), max);
90 pattern += n - max;
91 while (max--)
92 table.data[uchar(*pattern++)] = max;
93 return table;
94 }
95};
96
97template <size_t N>
99{
100 char m_pattern[N];
101 // N includes the terminating '\0'!
102 static_assert(N > 2, "QStaticByteArrayMatcher makes no sense for finding a single-char pattern");
103public:
104 explicit constexpr QStaticByteArrayMatcher(const char (&patternToMatch)[N]) noexcept
105 : QStaticByteArrayMatcherBase(patternToMatch, N - 1), m_pattern()
106 {
107 for (size_t i = 0; i < N; ++i)
108 m_pattern[i] = patternToMatch[i];
109 }
110
112 qsizetype indexIn(const QByteArray &haystack, qsizetype from = 0) const noexcept
113 { return this->indexOfIn(m_pattern, N - 1, haystack.data(), haystack.size(), from); }
114 qsizetype indexIn(const char *haystack, qsizetype hlen, qsizetype from = 0) const noexcept
115 { return this->indexOfIn(m_pattern, N - 1, haystack, hlen, from); }
116 qsizetype indexIn(QByteArrayView haystack, qsizetype from = 0) const noexcept
117 { return this->indexOfIn(m_pattern, N - 1, haystack.data(), haystack.size(), from); }
118
119 QByteArray pattern() const { return QByteArray(m_pattern, qsizetype(N - 1)); }
120};
121
122template <size_t N>
123constexpr QStaticByteArrayMatcher<N> qMakeStaticByteArrayMatcher(const char (&pattern)[N]) noexcept
124{ return QStaticByteArrayMatcher<N>(pattern); }
125
127
128#endif // QBYTEARRAYMATCHER_H
\inmodule QtCore
QByteArrayMatcher(QByteArrayView pattern)
Q_WEAK_OVERLOAD qsizetype indexIn(const QByteArray &ba, qsizetype from=0) const
QByteArray pattern() const
Returns the byte array pattern that this byte array matcher will search for.
\inmodule QtCore
Definition qbytearray.h:57
Non-template base class of QStaticByteArrayMatcher.
constexpr QStaticByteArrayMatcherBase(const char *pattern, size_t n) noexcept
Q_CORE_EXPORT qsizetype indexOfIn(const char *needle, size_t nlen, const char *haystack, qsizetype hlen, qsizetype from) const noexcept
~QStaticByteArrayMatcherBase()=default
The QStaticByteArrayMatcher class is a compile-time version of QByteArrayMatcher.
qsizetype indexIn(const char *haystack, qsizetype hlen, qsizetype from=0) const noexcept
Searches the char string haystack, which has length hlen, from byte position from (default 0,...
qsizetype indexIn(QByteArrayView haystack, qsizetype from=0) const noexcept
constexpr QStaticByteArrayMatcher(const char(&patternToMatch)[N]) noexcept
QByteArray pattern() const
Returns the byte array pattern that this byte array matcher will search for.
Q_WEAK_OVERLOAD qsizetype indexIn(const QByteArray &haystack, qsizetype from=0) const noexcept
Searches the char string haystack, from byte position from (default 0, i.e.
QString str
[2]
Combined button and popup list for selecting options.
constexpr void fill(ForwardIterator first, ForwardIterator last, const Value &value)
constexpr QStaticByteArrayMatcher< N > qMakeStaticByteArrayMatcher(const char(&pattern)[N]) noexcept
#define Q_WEAK_OVERLOAD
typedef QByteArray(EGLAPIENTRYP PFNQGSGETDISPLAYSPROC)()
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLenum GLuint GLenum GLsizei length
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLfloat n
GLfloat GLfloat p
[1]
GLenum GLsizei len
GLubyte * pattern
GLenum GLenum GLsizei void * table
unsigned char uchar
Definition qtypes.h:32
ptrdiff_t qsizetype
Definition qtypes.h:165
unsigned int uint
Definition qtypes.h:34
QByteArray ba
[0]
QSharedPointer< T > other(t)
[5]