Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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
13QT_BEGIN_NAMESPACE
14
15
16class QByteArrayMatcherPrivate;
17
18class Q_CORE_EXPORT QByteArrayMatcher
19{
20public:
21 QByteArrayMatcher();
22 explicit QByteArrayMatcher(const QByteArray &pattern);
23 explicit QByteArrayMatcher(QByteArrayView pattern)
24 : QByteArrayMatcher(pattern.data(), pattern.size())
25 {}
26 explicit QByteArrayMatcher(const char *pattern, qsizetype length = -1);
27 QByteArrayMatcher(const QByteArrayMatcher &other);
28 ~QByteArrayMatcher();
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
37 Q_WEAK_OVERLOAD
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
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
111 Q_WEAK_OVERLOAD
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); }
117 { return this->indexOfIn(m_pattern, N - 1, haystack.data(), haystack.size(), from); }
118
120};
121
122template <size_t N>
123constexpr QStaticByteArrayMatcher<N> qMakeStaticByteArrayMatcher(const char (&pattern)[N]) noexcept
124{ return QStaticByteArrayMatcher<N>(pattern); }
125
126QT_END_NAMESPACE
127
128#endif // QBYTEARRAYMATCHER_H
\inmodule QtCore
\inmodule QtCore
Definition qfile.h:95
bool unload(UnloadFlag flag=UnloadSys)
Definition qlibrary.cpp:554
QtPluginInstanceFunction loadPlugin()
Definition qlibrary.cpp:581
void setLoadHints(QLibrary::LoadHints lh)
Definition qlibrary.cpp:485
void updatePluginState()
Definition qlibrary.cpp:722
QFunctionPointer resolve(const char *)
Definition qlibrary.cpp:478
QObject * pluginInstance()
Definition qlibrary.cpp:499
static void cleanup()
Definition qlibrary.cpp:338
static void releaseLibrary(QLibraryPrivate *lib)
Definition qlibrary.cpp:427
static QLibraryPrivate * findOrCreate(const QString &fileName, const QString &version, QLibrary::LoadHints loadHints)
Definition qlibrary.cpp:393
\inmodule QtCore \reentrant
Definition qlibrary.h:17
\inmodule QtCore
Definition qmutex.h:332
Non-template base class of QStaticByteArrayMatcher.
constexpr QStaticByteArrayMatcherBase(const char *pattern, size_t n) noexcept
~QStaticByteArrayMatcherBase()=default
The QStaticByteArrayMatcher class is a compile-time version of QByteArrayMatcher.
constexpr QStaticByteArrayMatcher(const char(&patternToMatch)[N]) noexcept
constexpr void fill(ForwardIterator first, ForwardIterator last, const Value &value)
constexpr OutputIterator transform(InputIterator first, InputIterator last, OutputIterator dest, UnaryFunction op)
constexpr bool is_sorted(ForwardIterator first, ForwardIterator last, BinaryPredicate p={})
constexpr OutputIterator copy_if(InputIterator first, InputIterator last, OutputIterator dest, UnaryPredicate pred)
constexpr OutputIterator copy(InputIterator first, InputIterator last, OutputIterator dest)
constexpr ForwardIterator is_sorted_until(ForwardIterator first, ForwardIterator last, BinaryPredicate p={})
constexpr OutputIterator fill_n(OutputIterator first, Size n, const Value &value)
constexpr OutputIterator copy_n(InputIterator first, Size n, OutputIterator dest)
constexpr QStaticByteArrayMatcher< N > qMakeStaticByteArrayMatcher(const char(&pattern)[N]) noexcept
Q_TRACE_POINT(qtcore, QLibraryPrivate_load_exit, bool success)
static void installCoverageTool(QLibraryPrivate *libPrivate)
Definition qlibrary.cpp:280
static constexpr bool PluginMustMatchQtDebug
Definition qlibrary.cpp:44
bool qt_debug_component()
Q_TRACE_POINT(qtcore, QLibraryPrivate_load_entry, const QString &fileName)
static bool qt_get_metadata(QLibraryPrivate *priv, QString *errMsg)
Definition qlibrary.cpp:678
static Q_CONSTINIT bool qt_library_data_once
Definition qlibrary.cpp:331
static QLibraryScanResult findPatternUnloaded(const QString &library, QLibraryPrivate *lib)
Definition qlibrary.cpp:214
static constexpr bool QtBuildIsDebug
Definition qlibrary.cpp:54
static void qlibraryCleanup()
Definition qlibrary.cpp:376
QT_REQUIRE_CONFIG(library)