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// Qt-Security score:critical reason:data-parser
4
5#ifndef QBYTEARRAYMATCHER_H
6#define QBYTEARRAYMATCHER_H
7
8#include <QtCore/qbytearray.h>
9
10#include <QtCore/q20algorithm.h>
11#include <iterator>
12#include <limits>
13
14QT_BEGIN_NAMESPACE
15
16
17class QByteArrayMatcherPrivate;
18
19class Q_CORE_EXPORT QByteArrayMatcher
20{
21public:
22 QByteArrayMatcher();
23 explicit QByteArrayMatcher(const QByteArray &pattern);
24 explicit QByteArrayMatcher(QByteArrayView pattern)
25 : QByteArrayMatcher(pattern.data(), pattern.size())
26 {}
27 explicit QByteArrayMatcher(const char *pattern, qsizetype length = -1);
28 QByteArrayMatcher(const QByteArrayMatcher &other);
29 ~QByteArrayMatcher();
30
31 QByteArrayMatcher &operator=(const QByteArrayMatcher &other);
32
33 void setPattern(const QByteArray &pattern);
34
35#if QT_CORE_REMOVED_SINCE(6, 3)
36 qsizetype indexIn(const QByteArray &ba, qsizetype from = 0) const;
37#else
38 Q_WEAK_OVERLOAD
39 qsizetype indexIn(const QByteArray &ba, qsizetype from = 0) const
40 { return indexIn(QByteArrayView{ba}, from); }
41#endif
42 qsizetype indexIn(const char *str, qsizetype len, qsizetype from = 0) const;
43 qsizetype indexIn(QByteArrayView data, qsizetype from = 0) const;
44 inline QByteArray pattern() const
45 {
46 if (q_pattern.isNull())
47 return QByteArray(reinterpret_cast<const char*>(p.p), p.l);
48 return q_pattern;
49 }
50
51private:
52 QByteArrayMatcherPrivate *d;
53 QByteArray q_pattern;
54 struct Data {
55 uchar q_skiptable[256];
56 const uchar *p;
57 qsizetype l;
58 };
59 union {
60 uint dummy[256];
61 Data p;
62 };
63};
64
66{
67 alignas(16)
68 struct Skiptable {
69 uchar data[256];
70 } m_skiptable;
71protected:
72 explicit constexpr QStaticByteArrayMatcherBase(const char *pattern, size_t n) noexcept
74 // compiler-generated copy/more ctors/assignment operators are ok!
76
77#if QT_CORE_REMOVED_SINCE(6, 3) && QT_POINTER_SIZE != 4
78 Q_CORE_EXPORT int indexOfIn(const char *needle, uint nlen, const char *haystack, int hlen, int from) const noexcept;
79#endif
80 Q_CORE_EXPORT qsizetype indexOfIn(const char *needle, size_t nlen,
81 const char *haystack, qsizetype hlen,
82 qsizetype from) const noexcept;
83
84private:
85 static constexpr Skiptable generate(const char *pattern, size_t n) noexcept
86 {
87 const auto uchar_max = (std::numeric_limits<uchar>::max)();
88 uchar max = n > uchar_max ? uchar_max : uchar(n);
89 Skiptable table = {};
90 q20::fill(std::begin(table.data), std::end(table.data), max);
91 pattern += n - max;
92 while (max--)
93 table.data[uchar(*pattern++)] = max;
94 return table;
95 }
96};
97
98template <size_t N>
100{
101 char m_pattern[N];
102 // N includes the terminating '\0'!
103 static_assert(N > 2, "QStaticByteArrayMatcher makes no sense for finding a single-char pattern");
104public:
105 explicit constexpr QStaticByteArrayMatcher(const char (&patternToMatch)[N]) noexcept
106 : QStaticByteArrayMatcherBase(patternToMatch, N - 1), m_pattern()
107 {
108 for (size_t i = 0; i < N; ++i)
109 m_pattern[i] = patternToMatch[i];
110 }
111
112 Q_WEAK_OVERLOAD
113 qsizetype indexIn(const QByteArray &haystack, qsizetype from = 0) const noexcept
114 { return this->indexOfIn(m_pattern, N - 1, haystack.data(), haystack.size(), from); }
115 qsizetype indexIn(const char *haystack, qsizetype hlen, qsizetype from = 0) const noexcept
116 { return this->indexOfIn(m_pattern, N - 1, haystack, hlen, from); }
118 { return this->indexOfIn(m_pattern, N - 1, haystack.data(), haystack.size(), from); }
119
121};
122
123template <size_t N>
124constexpr QStaticByteArrayMatcher<N> qMakeStaticByteArrayMatcher(const char (&pattern)[N]) noexcept
125{ return QStaticByteArrayMatcher<N>(pattern); }
126
127QT_END_NAMESPACE
128
129#endif // QBYTEARRAYMATCHER_H
\inmodule QtCore
\inmodule QtCore
Definition qfile.h:96
bool unload(UnloadFlag flag=UnloadSys)
Definition qlibrary.cpp:556
QtPluginInstanceFunction loadPlugin()
Definition qlibrary.cpp:583
void setLoadHints(QLibrary::LoadHints lh)
Definition qlibrary.cpp:487
void updatePluginState()
Definition qlibrary.cpp:724
QFunctionPointer resolve(const char *)
Definition qlibrary.cpp:480
QObject * pluginInstance()
Definition qlibrary.cpp:501
static void cleanup()
Definition qlibrary.cpp:340
static void releaseLibrary(QLibraryPrivate *lib)
Definition qlibrary.cpp:429
static QLibraryPrivate * findOrCreate(const QString &fileName, const QString &version, QLibrary::LoadHints loadHints)
Definition qlibrary.cpp:395
\inmodule QtCore \reentrant
Definition qlibrary.h:17
\inmodule QtCore
Definition qmutex.h:346
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:282
static constexpr bool PluginMustMatchQtDebug
Definition qlibrary.cpp:46
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:680
static Q_CONSTINIT bool qt_library_data_once
Definition qlibrary.cpp:333
static QLibraryScanResult findPatternUnloaded(const QString &library, QLibraryPrivate *lib)
Definition qlibrary.cpp:216
static constexpr bool QtBuildIsDebug
Definition qlibrary.cpp:56
static void qlibraryCleanup()
Definition qlibrary.cpp:378
QT_REQUIRE_CONFIG(library)