1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtCore module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qbytearraymatcher.h"
48 static inline void bm_init_skiptable(const uchar *cc, int len, uchar *skiptable)
50 int l = qMin(len, 255);
51 memset(skiptable, l, 256*sizeof(uchar));
57 static inline int bm_find(const uchar *cc, int l, int index, const uchar *puc, uint pl,
58 const uchar *skiptable)
61 return index > l ? -1 : index;
62 const uint pl_minus_one = pl - 1;
64 register const uchar *current = cc + index + pl_minus_one;
65 const uchar *end = cc + l;
66 while (current < end) {
67 uint skip = skiptable[*current];
71 if (*(current - skip) != puc[pl_minus_one - skip])
75 if (skip > pl_minus_one) // we have a match
76 return (current - cc) - skip + 1;
78 // in case we don't have a match we are a bit inefficient as we only skip by one
79 // when we have the non matching char in the string.
80 if (skiptable[*(current - skip)] == pl)
85 if (current > end - skip)
89 return -1; // not found
92 /*! \class QByteArrayMatcher
93 \brief The QByteArrayMatcher class holds a sequence of bytes that
94 can be quickly matched in a byte array.
97 \ingroup string-processing
99 This class is useful when you have a sequence of bytes that you
100 want to repeatedly match against some byte arrays (perhaps in a
101 loop), or when you want to search for the same sequence of bytes
102 multiple times in the same byte array. Using a matcher object and
103 indexIn() is faster than matching a plain QByteArray with
104 QByteArray::indexOf() if repeated matching takes place. This
105 class offers no benefit if you are doing one-off byte array
108 Create the QByteArrayMatcher with the QByteArray you want to
109 search for. Then call indexIn() on the QByteArray that you want to
112 \sa QByteArray, QStringMatcher
116 Constructs an empty byte array matcher that won't match anything.
117 Call setPattern() to give it a pattern to match.
119 QByteArrayMatcher::QByteArrayMatcher()
124 qMemSet(p.q_skiptable, 0, sizeof(p.q_skiptable));
128 Constructs a byte array matcher from \a pattern. \a pattern
129 has the given \a length. \a pattern must remain in scope, but
130 the destructor does not delete \a pattern.
132 QByteArrayMatcher::QByteArrayMatcher(const char *pattern, int length)
135 p.p = reinterpret_cast<const uchar *>(pattern);
137 bm_init_skiptable(p.p, p.l, p.q_skiptable);
141 Constructs a byte array matcher that will search for \a pattern.
142 Call indexIn() to perform a search.
144 QByteArrayMatcher::QByteArrayMatcher(const QByteArray &pattern)
145 : d(0), q_pattern(pattern)
147 p.p = reinterpret_cast<const uchar *>(pattern.constData());
148 p.l = pattern.size();
149 bm_init_skiptable(p.p, p.l, p.q_skiptable);
153 Copies the \a other byte array matcher to this byte array matcher.
155 QByteArrayMatcher::QByteArrayMatcher(const QByteArrayMatcher &other)
162 Destroys the byte array matcher.
164 QByteArrayMatcher::~QByteArrayMatcher()
169 Assigns the \a other byte array matcher to this byte array matcher.
171 QByteArrayMatcher &QByteArrayMatcher::operator=(const QByteArrayMatcher &other)
173 q_pattern = other.q_pattern;
174 memcpy(&p, &other.p, sizeof(p));
179 Sets the byte array that this byte array matcher will search for
182 \sa pattern(), indexIn()
184 void QByteArrayMatcher::setPattern(const QByteArray &pattern)
187 p.p = reinterpret_cast<const uchar *>(pattern.constData());
188 p.l = pattern.size();
189 bm_init_skiptable(p.p, p.l, p.q_skiptable);
193 Searches the byte array \a ba, from byte position \a from (default
194 0, i.e. from the first byte), for the byte array pattern() that
195 was set in the constructor or in the most recent call to
196 setPattern(). Returns the position where the pattern() matched in
197 \a ba, or -1 if no match was found.
199 int QByteArrayMatcher::indexIn(const QByteArray &ba, int from) const
203 return bm_find(reinterpret_cast<const uchar *>(ba.constData()), ba.size(), from,
204 p.p, p.l, p.q_skiptable);
208 Searches the char string \a str, which has length \a len, from
209 byte position \a from (default 0, i.e. from the first byte), for
210 the byte array pattern() that was set in the constructor or in the
211 most recent call to setPattern(). Returns the position where the
212 pattern() matched in \a str, or -1 if no match was found.
214 int QByteArrayMatcher::indexIn(const char *str, int len, int from) const
218 return bm_find(reinterpret_cast<const uchar *>(str), len, from,
219 p.p, p.l, p.q_skiptable);
223 \fn QByteArray QByteArrayMatcher::pattern() const
225 Returns the byte array pattern that this byte array matcher will
232 static int findChar(const char *str, int len, char ch, int from)
234 const uchar *s = (const uchar *)str;
237 from = qMax(from + len, 0);
239 const uchar *n = s + from - 1;
240 const uchar *e = s + len;
250 static int qFindByteArrayBoyerMoore(
251 const char *haystack, int haystackLen, int haystackOffset,
252 const char *needle, int needleLen)
254 uchar skiptable[256];
255 bm_init_skiptable((const uchar *)needle, needleLen, skiptable);
256 if (haystackOffset < 0)
258 return bm_find((const uchar *)haystack, haystackLen, haystackOffset,
259 (const uchar *)needle, needleLen, skiptable);
263 if (sl_minus_1 < sizeof(uint) * CHAR_BIT) \
264 hashHaystack -= (a) << sl_minus_1; \
270 const char *haystack0, int haystackLen, int from,
271 const char *needle, int needleLen)
273 const int l = haystackLen;
274 const int sl = needleLen;
277 if (uint(sl + from) > (uint)l)
285 return findChar(haystack0, haystackLen, needle[0], from);
288 We use the Boyer-Moore algorithm in cases where the overhead
289 for the skip table should pay off, otherwise we use a simple
292 if (l > 500 && sl > 5)
293 return qFindByteArrayBoyerMoore(haystack0, haystackLen, from,
297 We use some hashing for efficiency's sake. Instead of
298 comparing strings, we compare the hash value of str with that
299 of a part of this QString. Only if that matches, we call memcmp().
301 const char *haystack = haystack0 + from;
302 const char *end = haystack0 + (l - sl);
303 const uint sl_minus_1 = sl - 1;
304 uint hashNeedle = 0, hashHaystack = 0;
306 for (idx = 0; idx < sl; ++idx) {
307 hashNeedle = ((hashNeedle<<1) + needle[idx]);
308 hashHaystack = ((hashHaystack<<1) + haystack[idx]);
310 hashHaystack -= *(haystack + sl_minus_1);
312 while (haystack <= end) {
313 hashHaystack += *(haystack + sl_minus_1);
314 if (hashHaystack == hashNeedle && *needle == *haystack
315 && memcmp(needle, haystack, sl) == 0)
316 return haystack - haystack0;