1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtCore module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qstringmatcher.h"
#include "qunicodetables_p.h"
QT_BEGIN_NAMESPACE
static void bm_init_skiptable(const ushort *uc, int len, uchar *skiptable, Qt::CaseSensitivity cs)
{
int l = qMin(len, 255);
memset(skiptable, l, 256*sizeof(uchar));
uc += len - l;
if (cs == Qt::CaseSensitive) {
while (l--) {
skiptable[*uc & 0xff] = l;
uc++;
}
} else {
const ushort *start = uc;
while (l--) {
skiptable[foldCase(uc, start) & 0xff] = l;
uc++;
}
}
}
static inline int bm_find(const ushort *uc, uint l, int index, const ushort *puc, uint pl,
const uchar *skiptable, Qt::CaseSensitivity cs)
{
if (pl == 0)
return index > (int)l ? -1 : index;
const uint pl_minus_one = pl - 1;
register const ushort *current = uc + index + pl_minus_one;
const ushort *end = uc + l;
if (cs == Qt::CaseSensitive) {
while (current < end) {
uint skip = skiptable[*current & 0xff];
if (!skip) {
// possible match
while (skip < pl) {
if (*(current - skip) != puc[pl_minus_one-skip])
break;
skip++;
}
if (skip > pl_minus_one) // we have a match
return (current - uc) - pl_minus_one;
// in case we don't have a match we are a bit inefficient as we only skip by one
// when we have the non matching char in the string.
if (skiptable[*(current - skip) & 0xff] == pl)
skip = pl - skip;
else
skip = 1;
}
if (current > end - skip)
break;
current += skip;
}
} else {
while (current < end) {
uint skip = skiptable[foldCase(current, uc) & 0xff];
if (!skip) {
// possible match
while (skip < pl) {
if (foldCase(current - skip, uc) != foldCase(puc + pl_minus_one - skip, puc))
break;
skip++;
}
if (skip > pl_minus_one) // we have a match
return (current - uc) - pl_minus_one;
// in case we don't have a match we are a bit inefficient as we only skip by one
// when we have the non matching char in the string.
if (skiptable[foldCase(current - skip, uc) & 0xff] == pl)
skip = pl - skip;
else
skip = 1;
}
if (current > end - skip)
break;
current += skip;
}
}
return -1; // not found
}
/*!
\class QStringMatcher
\brief The QStringMatcher class holds a sequence of characters that
can be quickly matched in a Unicode string.
\ingroup tools
\ingroup string-processing
This class is useful when you have a sequence of \l{QChar}s that
you want to repeatedly match against some strings (perhaps in a
loop), or when you want to search for the same sequence of
characters multiple times in the same string. Using a matcher
object and indexIn() is faster than matching a plain QString with
QString::indexOf() if repeated matching takes place. This class
offers no benefit if you are doing one-off string matches.
Create the QStringMatcher with the QString you want to search
for. Then call indexIn() on the QString that you want to search.
\sa QString, QByteArrayMatcher, QRegExp
*/
/*!
Constructs an empty string matcher that won't match anything.
Call setPattern() to give it a pattern to match.
*/
QStringMatcher::QStringMatcher()
: d_ptr(0), q_cs(Qt::CaseSensitive)
{
qMemSet(q_data, 0, sizeof(q_data));
}
/*!
Constructs a string matcher that will search for \a pattern, with
case sensitivity \a cs.
Call indexIn() to perform a search.
*/
QStringMatcher::QStringMatcher(const QString &pattern, Qt::CaseSensitivity cs)
: d_ptr(0), q_pattern(pattern), q_cs(cs)
{
p.uc = pattern.unicode();
p.len = pattern.size();
bm_init_skiptable((const ushort *)p.uc, p.len, p.q_skiptable, cs);
}
/*!
\fn QStringMatcher::QStringMatcher(const QChar *uc, int length, Qt::CaseSensitivity cs)
\since 4.5
Constructs a string matcher that will search for the pattern referred to
by \a uc with the given \a length and case sensitivity specified by \a cs.
*/
QStringMatcher::QStringMatcher(const QChar *uc, int len, Qt::CaseSensitivity cs)
: d_ptr(0), q_cs(cs)
{
p.uc = uc;
p.len = len;
bm_init_skiptable((const ushort *)p.uc, len, p.q_skiptable, cs);
}
/*!
Copies the \a other string matcher to this string matcher.
*/
QStringMatcher::QStringMatcher(const QStringMatcher &other)
: d_ptr(0)
{
operator=(other);
}
/*!
Destroys the string matcher.
*/
QStringMatcher::~QStringMatcher()
{
}
/*!
Assigns the \a other string matcher to this string matcher.
*/
QStringMatcher &QStringMatcher::operator=(const QStringMatcher &other)
{
if (this != &other) {
q_pattern = other.q_pattern;
q_cs = other.q_cs;
qMemCopy(q_data, other.q_data, sizeof(q_data));
}
return *this;
}
/*!
Sets the string that this string matcher will search for to \a
pattern.
\sa pattern(), setCaseSensitivity(), indexIn()
*/
void QStringMatcher::setPattern(const QString &pattern)
{
q_pattern = pattern;
p.uc = pattern.unicode();
p.len = pattern.size();
bm_init_skiptable((const ushort *)pattern.unicode(), pattern.size(), p.q_skiptable, q_cs);
}
/*!
\fn QString QStringMatcher::pattern() const
Returns the string pattern that this string matcher will search
for.
\sa setPattern()
*/
QString QStringMatcher::pattern() const
{
if (!q_pattern.isEmpty())
return q_pattern;
return QString(p.uc, p.len);
}
/*!
Sets the case sensitivity setting of this string matcher to \a
cs.
\sa caseSensitivity(), setPattern(), indexIn()
*/
void QStringMatcher::setCaseSensitivity(Qt::CaseSensitivity cs)
{
if (cs == q_cs)
return;
bm_init_skiptable((const ushort *)q_pattern.unicode(), q_pattern.size(), p.q_skiptable, cs);
q_cs = cs;
}
/*!
Searches the string \a str from character position \a from
(default 0, i.e. from the first character), for the string
pattern() that was set in the constructor or in the most recent
call to setPattern(). Returns the position where the pattern()
matched in \a str, or -1 if no match was found.
\sa setPattern(), setCaseSensitivity()
*/
int QStringMatcher::indexIn(const QString &str, int from) const
{
if (from < 0)
from = 0;
return bm_find((const ushort *)str.unicode(), str.size(), from,
(const ushort *)p.uc, p.len,
p.q_skiptable, q_cs);
}
/*!
\since 4.5
Searches the string starting at \a str (of length \a length) from
character position \a from (default 0, i.e. from the first
character), for the string pattern() that was set in the
constructor or in the most recent call to setPattern(). Returns
the position where the pattern() matched in \a str, or -1 if no
match was found.
\sa setPattern(), setCaseSensitivity()
*/
int QStringMatcher::indexIn(const QChar *str, int length, int from) const
{
if (from < 0)
from = 0;
return bm_find((const ushort *)str, length, from,
(const ushort *)p.uc, p.len,
p.q_skiptable, q_cs);
}
/*!
\fn Qt::CaseSensitivity QStringMatcher::caseSensitivity() const
Returns the case sensitivity setting for this string matcher.
\sa setCaseSensitivity()
*/
/*!
\internal
*/
int qFindStringBoyerMoore(
const QChar *haystack, int haystackLen, int haystackOffset,
const QChar *needle, int needleLen, Qt::CaseSensitivity cs)
{
uchar skiptable[256];
bm_init_skiptable((const ushort *)needle, needleLen, skiptable, cs);
if (haystackOffset < 0)
haystackOffset = 0;
return bm_find((const ushort *)haystack, haystackLen, haystackOffset,
(const ushort *)needle, needleLen, skiptable, cs);
}
QT_END_NAMESPACE
|