QtBase  v6.3.1
qbytearraymatcher.h
Go to the documentation of this file.
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 #include <limits>
46 
48 
49 
50 class QByteArrayMatcherPrivate;
51 
52 class Q_CORE_EXPORT QByteArrayMatcher
53 {
54 public:
56  explicit QByteArrayMatcher(const QByteArray &pattern);
59  {}
60  explicit QByteArrayMatcher(const char *pattern, qsizetype length = -1);
63 
64  QByteArrayMatcher &operator=(const QByteArrayMatcher &other);
65 
66  void setPattern(const QByteArray &pattern);
67 
68 #if QT_CORE_REMOVED_SINCE(6, 3)
69  qsizetype indexIn(const QByteArray &ba, qsizetype from = 0) const;
70 #else
72  qsizetype indexIn(const QByteArray &ba, qsizetype from = 0) const
73  { return indexIn(QByteArrayView{ba}, from); }
74 #endif
75  qsizetype indexIn(const char *str, qsizetype len, qsizetype from = 0) const;
76  qsizetype indexIn(QByteArrayView data, qsizetype from = 0) const;
77  inline QByteArray pattern() const
78  {
79  if (q_pattern.isNull())
80  return QByteArray(reinterpret_cast<const char*>(p.p), p.l);
81  return q_pattern;
82  }
83 
84 private:
85  QByteArrayMatcherPrivate *d;
86  QByteArray q_pattern;
87  struct Data {
88  uchar q_skiptable[256];
89  const uchar *p;
90  qsizetype l;
91  };
92  union {
93  uint dummy[256];
95  };
96 };
97 
99 {
100  alignas(16)
101  struct Skiptable {
102  uchar data[256];
103  } m_skiptable;
104 protected:
105  explicit constexpr QStaticByteArrayMatcherBase(const char *pattern, size_t n) noexcept
106  : m_skiptable(generate(pattern, n)) {}
107  // compiler-generated copy/more ctors/assignment operators are ok!
109 
110 #if QT_CORE_REMOVED_SINCE(6, 3) && QT_POINTER_SIZE != 4
111  Q_CORE_EXPORT int indexOfIn(const char *needle, uint nlen, const char *haystack, int hlen, int from) const noexcept;
112 #endif
113  Q_CORE_EXPORT qsizetype indexOfIn(const char *needle, size_t nlen,
114  const char *haystack, qsizetype hlen,
115  qsizetype from) const noexcept;
116 
117 private:
118  static constexpr Skiptable generate(const char *pattern, size_t n) noexcept
119  {
120  const auto uchar_max = (std::numeric_limits<uchar>::max)();
121  uchar max = n > uchar_max ? uchar_max : uchar(n);
122  Skiptable table = {
123  // this verbose initialization code aims to avoid some opaque error messages
124  // even on powerful compilers such as GCC 5.3. Even though for GCC a loop
125  // format can be found that v5.3 groks, it's probably better to go with this
126  // for the time being:
127  {
128  max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max,
129  max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max,
130  max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max,
131  max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max,
132  max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max,
133  max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max,
134  max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max,
135  max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max,
136 
137  max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max,
138  max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max,
139  max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max,
140  max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max,
141  max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max,
142  max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max,
143  max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max,
144  max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max,
145  }
146  };
147  pattern += n - max;
148  while (max--)
149  table.data[uchar(*pattern++)] = max;
150  return table;
151  }
152 };
153 
154 template <size_t N>
156 {
157  char m_pattern[N];
158  // N includes the terminating '\0'!
159  static_assert(N > 2, "QStaticByteArrayMatcher makes no sense for finding a single-char pattern");
160 public:
161  explicit constexpr QStaticByteArrayMatcher(const char (&patternToMatch)[N]) noexcept
162  : QStaticByteArrayMatcherBase(patternToMatch, N - 1), m_pattern()
163  {
164  for (size_t i = 0; i < N; ++i)
165  m_pattern[i] = patternToMatch[i];
166  }
167 
169  qsizetype indexIn(const QByteArray &haystack, qsizetype from = 0) const noexcept
170  { return this->indexOfIn(m_pattern, N - 1, haystack.data(), haystack.size(), from); }
171  qsizetype indexIn(const char *haystack, qsizetype hlen, qsizetype from = 0) const noexcept
172  { return this->indexOfIn(m_pattern, N - 1, haystack, hlen, from); }
173  qsizetype indexIn(QByteArrayView haystack, qsizetype from = 0) const noexcept
174  { return this->indexOfIn(m_pattern, N - 1, haystack.data(), haystack.size(), from); }
175 
176  QByteArray pattern() const { return QByteArray(m_pattern, qsizetype(N - 1)); }
177 };
178 
179 template <size_t N>
182 
184 
185 #endif // QBYTEARRAYMATCHER_H
small capitals from c petite p scientific i
[1]
Definition: afcover.h:80
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:85
char * data()
Definition: qbytearray.h:516
qsizetype size() const noexcept
Definition: qbytearray.h:470
The QByteArrayMatcher class holds a sequence of bytes that can be quickly matched in a byte array.
QByteArrayMatcher(QByteArrayView pattern)
Q_WEAK_OVERLOAD qsizetype indexIn(const QByteArray &ba, qsizetype from=0) const
QByteArray pattern() const
constexpr qsizetype size() const noexcept
constexpr const_pointer data() const noexcept
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
qsizetype indexIn(QByteArrayView haystack, qsizetype from=0) const noexcept
constexpr QStaticByteArrayMatcher(const char(&patternToMatch)[N]) noexcept
template< size_t N > QStaticByteArrayMatcher qMakeStaticByteArrayMatcher(const char(&pattern)[N])
QByteArray pattern() const
Q_WEAK_OVERLOAD qsizetype indexIn(const QByteArray &haystack, qsizetype from=0) const noexcept
QString str
[2]
auto it unsigned count const
Definition: hb-iter.hh:848
#define Q_WEAK_OVERLOAD
typedef QByteArray(EGLAPIENTRYP PFNQGSGETDISPLAYSPROC)()
QT_BEGIN_INCLUDE_NAMESPACE typedef unsigned char uchar
Definition: qglobal.h:332
ptrdiff_t qsizetype
Definition: qglobal.h:308
unsigned int uint
Definition: qglobal.h:334
GLenum GLuint GLenum GLsizei length
Definition: qopengl.h:270
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLfloat n
GLenum GLsizei len
Definition: qopenglext.h:3292
GLfloat GLfloat p
[1]
Definition: qopenglext.h:12698
GLubyte * pattern
Definition: qopenglext.h:2744
GLenum GLenum GLsizei void * table
Definition: qopenglext.h:2745
@ Data
QByteArray ba
[0]
QSharedPointer< T > other(t)
[5]
QThreadStorage< int * > dummy[8]