1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtCore module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #ifndef QBYTEARRAYMATCHER_H |
41 | #define QBYTEARRAYMATCHER_H |
42 | |
43 | #include <QtCore/qbytearray.h> |
44 | |
45 | QT_BEGIN_NAMESPACE |
46 | |
47 | |
48 | class QByteArrayMatcherPrivate; |
49 | |
50 | class Q_CORE_EXPORT QByteArrayMatcher |
51 | { |
52 | public: |
53 | QByteArrayMatcher(); |
54 | explicit QByteArrayMatcher(const QByteArray &pattern); |
55 | explicit QByteArrayMatcher(const char *pattern, qsizetype length); |
56 | QByteArrayMatcher(const QByteArrayMatcher &other); |
57 | ~QByteArrayMatcher(); |
58 | |
59 | QByteArrayMatcher &operator=(const QByteArrayMatcher &other); |
60 | |
61 | void setPattern(const QByteArray &pattern); |
62 | |
63 | qsizetype indexIn(const QByteArray &ba, qsizetype from = 0) const; |
64 | qsizetype indexIn(const char *str, qsizetype len, qsizetype from = 0) const; |
65 | inline QByteArray pattern() const |
66 | { |
67 | if (q_pattern.isNull()) |
68 | return QByteArray(reinterpret_cast<const char*>(p.p), p.l); |
69 | return q_pattern; |
70 | } |
71 | |
72 | private: |
73 | QByteArrayMatcherPrivate *d; |
74 | QByteArray q_pattern; |
75 | struct Data { |
76 | uchar q_skiptable[256]; |
77 | const uchar *p; |
78 | qsizetype l; |
79 | }; |
80 | union { |
81 | uint dummy[256]; |
82 | Data p; |
83 | }; |
84 | }; |
85 | |
86 | class QStaticByteArrayMatcherBase |
87 | { |
88 | alignas(16) |
89 | struct Skiptable { |
90 | uchar data[256]; |
91 | } m_skiptable; |
92 | protected: |
93 | explicit constexpr QStaticByteArrayMatcherBase(const char *pattern, uint n) noexcept |
94 | : m_skiptable(generate(pattern, n)) {} |
95 | // compiler-generated copy/more ctors/assignment operators are ok! |
96 | // compiler-generated dtor is ok! |
97 | |
98 | Q_CORE_EXPORT int indexOfIn(const char *needle, uint nlen, const char *haystack, int hlen, int from) const noexcept; |
99 | |
100 | private: |
101 | static constexpr Skiptable generate(const char *pattern, uint n) noexcept |
102 | { |
103 | const auto uchar_max = (std::numeric_limits<uchar>::max)(); |
104 | uchar max = n > uchar_max ? uchar_max : uchar(n); |
105 | Skiptable table = { |
106 | // this verbose initialization code aims to avoid some opaque error messages |
107 | // even on powerful compilers such as GCC 5.3. Even though for GCC a loop |
108 | // format can be found that v5.3 groks, it's probably better to go with this |
109 | // for the time being: |
110 | { |
111 | max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, |
112 | max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, |
113 | max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, |
114 | max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, |
115 | max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, |
116 | max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, |
117 | max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, |
118 | max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, |
119 | |
120 | max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, |
121 | max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, |
122 | max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, |
123 | max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, |
124 | max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, |
125 | max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, |
126 | max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, |
127 | max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, |
128 | } |
129 | }; |
130 | pattern += n - max; |
131 | while (max--) |
132 | table.data[uchar(*pattern++)] = max; |
133 | return table; |
134 | } |
135 | }; |
136 | |
137 | template <uint N> |
138 | class QStaticByteArrayMatcher : QStaticByteArrayMatcherBase |
139 | { |
140 | char m_pattern[N]; |
141 | static_assert(N > 2, "QStaticByteArrayMatcher makes no sense for finding a single-char pattern" ); |
142 | public: |
143 | explicit constexpr QStaticByteArrayMatcher(const char (&patternToMatch)[N]) noexcept |
144 | : QStaticByteArrayMatcherBase(patternToMatch, N - 1), m_pattern() |
145 | { |
146 | for (uint i = 0; i < N; ++i) |
147 | m_pattern[i] = patternToMatch[i]; |
148 | } |
149 | |
150 | int indexIn(const QByteArray &haystack, int from = 0) const noexcept |
151 | { return this->indexOfIn(m_pattern, N - 1, haystack.data(), haystack.size(), from); } |
152 | int indexIn(const char *haystack, int hlen, int from = 0) const noexcept |
153 | { return this->indexOfIn(m_pattern, N - 1, haystack, hlen, from); } |
154 | |
155 | QByteArray pattern() const { return QByteArray(m_pattern, int(N - 1)); } |
156 | }; |
157 | |
158 | template <uint N> |
159 | constexpr QStaticByteArrayMatcher<N> qMakeStaticByteArrayMatcher(const char (&pattern)[N]) noexcept |
160 | { return QStaticByteArrayMatcher<N>(pattern); } |
161 | |
162 | QT_END_NAMESPACE |
163 | |
164 | #endif // QBYTEARRAYMATCHER_H |
165 | |