Line | Source | Count |
1 | | - |
2 | | - |
3 | static void bm_init_skiptable(const ushort *uc, int len, uchar *skiptable, Qt::CaseSensitivity cs) | - |
4 | { | - |
5 | int l = qMin(len, 255); | - |
6 | memset(skiptable, l, 256*sizeof(uchar)); | - |
7 | uc += len - l; | - |
8 | if (cs == Qt::CaseSensitive) { | - |
9 | while (l--) { | - |
10 | skiptable[*uc & 0xff] = l; | - |
11 | uc++; | - |
12 | } | - |
13 | } else { | - |
14 | const ushort *start = uc; | - |
15 | while (l--) { | - |
16 | skiptable[foldCase(uc, start) & 0xff] = l; | - |
17 | uc++; | - |
18 | } | - |
19 | } | - |
20 | } | - |
21 | | - |
22 | static inline int bm_find(const ushort *uc, uint l, int index, const ushort *puc, uint pl, | - |
23 | const uchar *skiptable, Qt::CaseSensitivity cs) | - |
24 | { | - |
25 | if (pl == 0) | - |
26 | return index > (int)l ? -1 : index; | - |
27 | const uint pl_minus_one = pl - 1; | - |
28 | | - |
29 | const ushort *current = uc + index + pl_minus_one; | - |
30 | const ushort *end = uc + l; | - |
31 | if (cs == Qt::CaseSensitive) { | - |
32 | while (current < end) { | - |
33 | uint skip = skiptable[*current & 0xff]; | - |
34 | if (!skip) { | - |
35 | | - |
36 | while (skip < pl) { | - |
37 | if (*(current - skip) != puc[pl_minus_one-skip]) | - |
38 | break; | - |
39 | skip++; | - |
40 | } | - |
41 | if (skip > pl_minus_one) | - |
42 | return (current - uc) - pl_minus_one; | - |
43 | | - |
44 | | - |
45 | | - |
46 | if (skiptable[*(current - skip) & 0xff] == pl) | - |
47 | skip = pl - skip; | - |
48 | else | - |
49 | skip = 1; | - |
50 | } | - |
51 | if (current > end - skip) | - |
52 | break; | - |
53 | current += skip; | - |
54 | } | - |
55 | } else { | - |
56 | while (current < end) { | - |
57 | uint skip = skiptable[foldCase(current, uc) & 0xff]; | - |
58 | if (!skip) { | - |
59 | | - |
60 | while (skip < pl) { | - |
61 | if (foldCase(current - skip, uc) != foldCase(puc + pl_minus_one - skip, puc)) | - |
62 | break; | - |
63 | skip++; | - |
64 | } | - |
65 | if (skip > pl_minus_one) | - |
66 | return (current - uc) - pl_minus_one; | - |
67 | | - |
68 | | - |
69 | if (skiptable[foldCase(current - skip, uc) & 0xff] == pl) | - |
70 | skip = pl - skip; | - |
71 | else | - |
72 | skip = 1; | - |
73 | } | - |
74 | if (current > end - skip) | - |
75 | break; | - |
76 | current += skip; | - |
77 | } | - |
78 | } | - |
79 | return -1; | - |
80 | } | - |
81 | QStringMatcher::QStringMatcher() | - |
82 | : d_ptr(0), q_cs(Qt::CaseSensitive) | - |
83 | { | - |
84 | memset(q_data, 0, sizeof(q_data)); | - |
85 | } | - |
86 | | - |
87 | | - |
88 | | - |
89 | | - |
90 | | - |
91 | | - |
92 | | - |
93 | QStringMatcher::QStringMatcher(const QString &pattern, Qt::CaseSensitivity cs) | - |
94 | : d_ptr(0), q_pattern(pattern), q_cs(cs) | - |
95 | { | - |
96 | p.uc = pattern.unicode(); | - |
97 | p.len = pattern.size(); | - |
98 | bm_init_skiptable((const ushort *)p.uc, p.len, p.q_skiptable, cs); | - |
99 | } | - |
100 | QStringMatcher::QStringMatcher(const QChar *uc, int len, Qt::CaseSensitivity cs) | - |
101 | : d_ptr(0), q_cs(cs) | - |
102 | { | - |
103 | p.uc = uc; | - |
104 | p.len = len; | - |
105 | bm_init_skiptable((const ushort *)p.uc, len, p.q_skiptable, cs); | - |
106 | } | - |
107 | | - |
108 | | - |
109 | | - |
110 | | - |
111 | QStringMatcher::QStringMatcher(const QStringMatcher &other) | - |
112 | : d_ptr(0) | - |
113 | { | - |
114 | operator=(other); | - |
115 | } | - |
116 | | - |
117 | | - |
118 | | - |
119 | | - |
120 | QStringMatcher::~QStringMatcher() | - |
121 | { | - |
122 | (void)d_ptr;; | - |
123 | } | - |
124 | | - |
125 | | - |
126 | | - |
127 | | - |
128 | QStringMatcher &QStringMatcher::operator=(const QStringMatcher &other) | - |
129 | { | - |
130 | if (this != &other) { | - |
131 | q_pattern = other.q_pattern; | - |
132 | q_cs = other.q_cs; | - |
133 | memcpy(q_data, other.q_data, sizeof(q_data)); | - |
134 | } | - |
135 | return *this; | - |
136 | } | - |
137 | | - |
138 | | - |
139 | | - |
140 | | - |
141 | | - |
142 | | - |
143 | | - |
144 | void QStringMatcher::setPattern(const QString &pattern) | - |
145 | { | - |
146 | q_pattern = pattern; | - |
147 | p.uc = pattern.unicode(); | - |
148 | p.len = pattern.size(); | - |
149 | bm_init_skiptable((const ushort *)pattern.unicode(), pattern.size(), p.q_skiptable, q_cs); | - |
150 | } | - |
151 | QString QStringMatcher::pattern() const | - |
152 | { | - |
153 | if (!q_pattern.isEmpty()) | - |
154 | return q_pattern; | - |
155 | return QString(p.uc, p.len); | - |
156 | } | - |
157 | | - |
158 | | - |
159 | | - |
160 | | - |
161 | | - |
162 | | - |
163 | | - |
164 | void QStringMatcher::setCaseSensitivity(Qt::CaseSensitivity cs) | - |
165 | { | - |
166 | if (cs == q_csTRUE | evaluated 3 times by 1 test | FALSE | evaluated 2 times by 1 test |
) | 2-3 |
167 | return;executed 3 times by 1 test: return; | 3 |
168 | bm_init_skiptable((const ushort *)pq_pattern.uc, punicode(), q_pattern.len,size(), p.q_skiptable, cs); | - |
169 | q_cs = cs; | - |
170 | }executed 2 times by 1 test: end of block | 2 |
171 | int QStringMatcher::indexIn(const QString &str, int from) const | - |
172 | { | - |
173 | if (from < 0) | - |
174 | from = 0; | - |
175 | return bm_find((const ushort *)str.unicode(), str.size(), from, | - |
176 | (const ushort *)p.uc, p.len, | - |
177 | p.q_skiptable, q_cs); | - |
178 | } | - |
179 | int QStringMatcher::indexIn(const QChar *str, int length, int from) const | - |
180 | { | - |
181 | if (from < 0) | - |
182 | from = 0; | - |
183 | return bm_find((const ushort *)str, length, from, | - |
184 | (const ushort *)p.uc, p.len, | - |
185 | p.q_skiptable, q_cs); | - |
186 | } | - |
187 | int qFindStringBoyerMoore( | - |
188 | const QChar *haystack, int haystackLen, int haystackOffset, | - |
189 | const QChar *needle, int needleLen, Qt::CaseSensitivity cs) | - |
190 | { | - |
191 | uchar skiptable[256]; | - |
192 | bm_init_skiptable((const ushort *)needle, needleLen, skiptable, cs); | - |
193 | if (haystackOffset < 0) | - |
194 | haystackOffset = 0; | - |
195 | return bm_find((const ushort *)haystack, haystackLen, haystackOffset, | - |
196 | (const ushort *)needle, needleLen, skiptable, cs); | - |
197 | } | - |
198 | | - |
199 | | - |
| | |