600
|
1 |
/*
|
|
2 |
*
|
|
3 |
* Copyright (c) 2002
|
|
4 |
* John Maddock
|
|
5 |
*
|
|
6 |
* Use, modification and distribution are subject to the
|
|
7 |
* Boost Software License, Version 1.0. (See accompanying file
|
|
8 |
* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
9 |
*
|
|
10 |
*/
|
|
11 |
|
|
12 |
/*
|
|
13 |
* LOCATION: see http://www.boost.org for most recent version.
|
|
14 |
* FILE perl_matcher_common.cpp
|
|
15 |
* VERSION see <boost/version.hpp>
|
|
16 |
* DESCRIPTION: Definitions of perl_matcher member functions that are
|
|
17 |
* specific to the recursive implementation.
|
|
18 |
*/
|
|
19 |
|
|
20 |
#ifndef BOOST_REGEX_V4_PERL_MATCHER_RECURSIVE_HPP
|
|
21 |
#define BOOST_REGEX_V4_PERL_MATCHER_RECURSIVE_HPP
|
|
22 |
|
|
23 |
#ifdef BOOST_MSVC
|
|
24 |
#pragma warning(push)
|
|
25 |
#pragma warning(disable: 4103)
|
|
26 |
#endif
|
|
27 |
#ifdef BOOST_HAS_ABI_HEADERS
|
|
28 |
# include BOOST_ABI_PREFIX
|
|
29 |
#endif
|
|
30 |
#ifdef BOOST_MSVC
|
|
31 |
#pragma warning(pop)
|
|
32 |
#endif
|
|
33 |
|
|
34 |
#ifdef BOOST_MSVC
|
|
35 |
#pragma warning(push)
|
|
36 |
#pragma warning(disable: 4800)
|
|
37 |
#endif
|
|
38 |
|
|
39 |
namespace boost{
|
|
40 |
namespace re_detail{
|
|
41 |
|
|
42 |
template <class BidiIterator>
|
|
43 |
class backup_subex
|
|
44 |
{
|
|
45 |
int index;
|
|
46 |
sub_match<BidiIterator> sub;
|
|
47 |
public:
|
|
48 |
template <class A>
|
|
49 |
backup_subex(const match_results<BidiIterator, A>& w, int i)
|
|
50 |
: index(i), sub(w[i], false) {}
|
|
51 |
template <class A>
|
|
52 |
void restore(match_results<BidiIterator, A>& w)
|
|
53 |
{
|
|
54 |
w.set_first(sub.first, index);
|
|
55 |
w.set_second(sub.second, index, sub.matched);
|
|
56 |
}
|
|
57 |
const sub_match<BidiIterator>& get() { return sub; }
|
|
58 |
};
|
|
59 |
|
|
60 |
template <class BidiIterator, class Allocator, class traits>
|
|
61 |
bool perl_matcher<BidiIterator, Allocator, traits>::match_all_states()
|
|
62 |
{
|
|
63 |
static matcher_proc_type const s_match_vtable[29] =
|
|
64 |
{
|
|
65 |
(&perl_matcher<BidiIterator, Allocator, traits>::match_startmark),
|
|
66 |
&perl_matcher<BidiIterator, Allocator, traits>::match_endmark,
|
|
67 |
&perl_matcher<BidiIterator, Allocator, traits>::match_literal,
|
|
68 |
&perl_matcher<BidiIterator, Allocator, traits>::match_start_line,
|
|
69 |
&perl_matcher<BidiIterator, Allocator, traits>::match_end_line,
|
|
70 |
&perl_matcher<BidiIterator, Allocator, traits>::match_wild,
|
|
71 |
&perl_matcher<BidiIterator, Allocator, traits>::match_match,
|
|
72 |
&perl_matcher<BidiIterator, Allocator, traits>::match_word_boundary,
|
|
73 |
&perl_matcher<BidiIterator, Allocator, traits>::match_within_word,
|
|
74 |
&perl_matcher<BidiIterator, Allocator, traits>::match_word_start,
|
|
75 |
&perl_matcher<BidiIterator, Allocator, traits>::match_word_end,
|
|
76 |
&perl_matcher<BidiIterator, Allocator, traits>::match_buffer_start,
|
|
77 |
&perl_matcher<BidiIterator, Allocator, traits>::match_buffer_end,
|
|
78 |
&perl_matcher<BidiIterator, Allocator, traits>::match_backref,
|
|
79 |
&perl_matcher<BidiIterator, Allocator, traits>::match_long_set,
|
|
80 |
&perl_matcher<BidiIterator, Allocator, traits>::match_set,
|
|
81 |
&perl_matcher<BidiIterator, Allocator, traits>::match_jump,
|
|
82 |
&perl_matcher<BidiIterator, Allocator, traits>::match_alt,
|
|
83 |
&perl_matcher<BidiIterator, Allocator, traits>::match_rep,
|
|
84 |
&perl_matcher<BidiIterator, Allocator, traits>::match_combining,
|
|
85 |
&perl_matcher<BidiIterator, Allocator, traits>::match_soft_buffer_end,
|
|
86 |
&perl_matcher<BidiIterator, Allocator, traits>::match_restart_continue,
|
|
87 |
(::boost::is_random_access_iterator<BidiIterator>::value ? &perl_matcher<BidiIterator, Allocator, traits>::match_dot_repeat_fast : &perl_matcher<BidiIterator, Allocator, traits>::match_dot_repeat_slow),
|
|
88 |
&perl_matcher<BidiIterator, Allocator, traits>::match_char_repeat,
|
|
89 |
&perl_matcher<BidiIterator, Allocator, traits>::match_set_repeat,
|
|
90 |
&perl_matcher<BidiIterator, Allocator, traits>::match_long_set_repeat,
|
|
91 |
&perl_matcher<BidiIterator, Allocator, traits>::match_backstep,
|
|
92 |
&perl_matcher<BidiIterator, Allocator, traits>::match_assert_backref,
|
|
93 |
&perl_matcher<BidiIterator, Allocator, traits>::match_toggle_case,
|
|
94 |
};
|
|
95 |
|
|
96 |
if(state_count > max_state_count)
|
|
97 |
raise_error(traits_inst, regex_constants::error_space);
|
|
98 |
while(pstate)
|
|
99 |
{
|
|
100 |
matcher_proc_type proc = s_match_vtable[pstate->type];
|
|
101 |
++state_count;
|
|
102 |
if(!(this->*proc)())
|
|
103 |
{
|
|
104 |
if((m_match_flags & match_partial) && (position == last) && (position != search_base))
|
|
105 |
m_has_partial_match = true;
|
|
106 |
return 0;
|
|
107 |
}
|
|
108 |
}
|
|
109 |
return true;
|
|
110 |
}
|
|
111 |
|
|
112 |
template <class BidiIterator, class Allocator, class traits>
|
|
113 |
bool perl_matcher<BidiIterator, Allocator, traits>::match_startmark()
|
|
114 |
{
|
|
115 |
int index = static_cast<const re_brace*>(pstate)->index;
|
|
116 |
bool r = true;
|
|
117 |
switch(index)
|
|
118 |
{
|
|
119 |
case 0:
|
|
120 |
pstate = pstate->next.p;
|
|
121 |
break;
|
|
122 |
case -1:
|
|
123 |
case -2:
|
|
124 |
{
|
|
125 |
// forward lookahead assert:
|
|
126 |
BidiIterator old_position(position);
|
|
127 |
const re_syntax_base* next_pstate = static_cast<const re_jump*>(pstate->next.p)->alt.p->next.p;
|
|
128 |
pstate = pstate->next.p->next.p;
|
|
129 |
r = match_all_states();
|
|
130 |
pstate = next_pstate;
|
|
131 |
position = old_position;
|
|
132 |
if((r && (index != -1)) || (!r && (index != -2)))
|
|
133 |
r = false;
|
|
134 |
else
|
|
135 |
r = true;
|
|
136 |
break;
|
|
137 |
}
|
|
138 |
case -3:
|
|
139 |
{
|
|
140 |
// independent sub-expression:
|
|
141 |
bool old_independent = m_independent;
|
|
142 |
m_independent = true;
|
|
143 |
const re_syntax_base* next_pstate = static_cast<const re_jump*>(pstate->next.p)->alt.p->next.p;
|
|
144 |
pstate = pstate->next.p->next.p;
|
|
145 |
r = match_all_states();
|
|
146 |
pstate = next_pstate;
|
|
147 |
m_independent = old_independent;
|
|
148 |
#ifdef BOOST_REGEX_MATCH_EXTRA
|
|
149 |
if(r && (m_match_flags & match_extra))
|
|
150 |
{
|
|
151 |
//
|
|
152 |
// our captures have been stored in *m_presult
|
|
153 |
// we need to unpack them, and insert them
|
|
154 |
// back in the right order when we unwind the stack:
|
|
155 |
//
|
|
156 |
unsigned i;
|
|
157 |
match_results<BidiIterator, Allocator> tm(*m_presult);
|
|
158 |
for(i = 0; i < tm.size(); ++i)
|
|
159 |
(*m_presult)[i].get_captures().clear();
|
|
160 |
// match everything else:
|
|
161 |
r = match_all_states();
|
|
162 |
// now place the stored captures back:
|
|
163 |
for(i = 0; i < tm.size(); ++i)
|
|
164 |
{
|
|
165 |
typedef typename sub_match<BidiIterator>::capture_sequence_type seq;
|
|
166 |
seq& s1 = (*m_presult)[i].get_captures();
|
|
167 |
const seq& s2 = tm[i].captures();
|
|
168 |
s1.insert(
|
|
169 |
s1.end(),
|
|
170 |
s2.begin(),
|
|
171 |
s2.end());
|
|
172 |
}
|
|
173 |
}
|
|
174 |
#endif
|
|
175 |
break;
|
|
176 |
}
|
|
177 |
case -4:
|
|
178 |
{
|
|
179 |
// conditional expression:
|
|
180 |
const re_alt* alt = static_cast<const re_alt*>(pstate->next.p);
|
|
181 |
BOOST_ASSERT(alt->type == syntax_element_alt);
|
|
182 |
pstate = alt->next.p;
|
|
183 |
if(pstate->type == syntax_element_assert_backref)
|
|
184 |
{
|
|
185 |
if(!match_assert_backref())
|
|
186 |
pstate = alt->alt.p;
|
|
187 |
break;
|
|
188 |
}
|
|
189 |
else
|
|
190 |
{
|
|
191 |
// zero width assertion, have to match this recursively:
|
|
192 |
BOOST_ASSERT(pstate->type == syntax_element_startmark);
|
|
193 |
bool negated = static_cast<const re_brace*>(pstate)->index == -2;
|
|
194 |
BidiIterator saved_position = position;
|
|
195 |
const re_syntax_base* next_pstate = static_cast<const re_jump*>(pstate->next.p)->alt.p->next.p;
|
|
196 |
pstate = pstate->next.p->next.p;
|
|
197 |
bool r = match_all_states();
|
|
198 |
position = saved_position;
|
|
199 |
if(negated)
|
|
200 |
r = !r;
|
|
201 |
if(r)
|
|
202 |
pstate = next_pstate;
|
|
203 |
else
|
|
204 |
pstate = alt->alt.p;
|
|
205 |
break;
|
|
206 |
}
|
|
207 |
}
|
|
208 |
default:
|
|
209 |
{
|
|
210 |
BOOST_ASSERT(index > 0);
|
|
211 |
if((m_match_flags & match_nosubs) == 0)
|
|
212 |
{
|
|
213 |
backup_subex<BidiIterator> sub(*m_presult, index);
|
|
214 |
m_presult->set_first(position, index);
|
|
215 |
pstate = pstate->next.p;
|
|
216 |
r = match_all_states();
|
|
217 |
if(r == false)
|
|
218 |
sub.restore(*m_presult);
|
|
219 |
#ifdef BOOST_REGEX_MATCH_EXTRA
|
|
220 |
//
|
|
221 |
// we have a match, push the capture information onto the stack:
|
|
222 |
//
|
|
223 |
else if(sub.get().matched && (match_extra & m_match_flags))
|
|
224 |
((*m_presult)[index]).get_captures().push_back(sub.get());
|
|
225 |
#endif
|
|
226 |
}
|
|
227 |
else
|
|
228 |
{
|
|
229 |
pstate = pstate->next.p;
|
|
230 |
}
|
|
231 |
break;
|
|
232 |
}
|
|
233 |
}
|
|
234 |
return r;
|
|
235 |
}
|
|
236 |
|
|
237 |
template <class BidiIterator, class Allocator, class traits>
|
|
238 |
bool perl_matcher<BidiIterator, Allocator, traits>::match_alt()
|
|
239 |
{
|
|
240 |
bool take_first, take_second;
|
|
241 |
const re_alt* jmp = static_cast<const re_alt*>(pstate);
|
|
242 |
|
|
243 |
// find out which of these two alternatives we need to take:
|
|
244 |
if(position == last)
|
|
245 |
{
|
|
246 |
take_first = jmp->can_be_null & mask_take;
|
|
247 |
take_second = jmp->can_be_null & mask_skip;
|
|
248 |
}
|
|
249 |
else
|
|
250 |
{
|
|
251 |
take_first = can_start(*position, jmp->_map, (unsigned char)mask_take);
|
|
252 |
take_second = can_start(*position, jmp->_map, (unsigned char)mask_skip);
|
|
253 |
}
|
|
254 |
|
|
255 |
if(take_first)
|
|
256 |
{
|
|
257 |
// we can take the first alternative,
|
|
258 |
// see if we need to push next alternative:
|
|
259 |
if(take_second)
|
|
260 |
{
|
|
261 |
BidiIterator oldposition(position);
|
|
262 |
const re_syntax_base* old_pstate = jmp->alt.p;
|
|
263 |
pstate = pstate->next.p;
|
|
264 |
if(!match_all_states())
|
|
265 |
{
|
|
266 |
pstate = old_pstate;
|
|
267 |
position = oldposition;
|
|
268 |
}
|
|
269 |
return true;
|
|
270 |
}
|
|
271 |
pstate = pstate->next.p;
|
|
272 |
return true;
|
|
273 |
}
|
|
274 |
if(take_second)
|
|
275 |
{
|
|
276 |
pstate = jmp->alt.p;
|
|
277 |
return true;
|
|
278 |
}
|
|
279 |
return false; // neither option is possible
|
|
280 |
}
|
|
281 |
|
|
282 |
template <class BidiIterator, class Allocator, class traits>
|
|
283 |
bool perl_matcher<BidiIterator, Allocator, traits>::match_rep()
|
|
284 |
{
|
|
285 |
#ifdef BOOST_MSVC
|
|
286 |
#pragma warning(push)
|
|
287 |
#pragma warning(disable:4127 4244)
|
|
288 |
#endif
|
|
289 |
const re_repeat* rep = static_cast<const re_repeat*>(pstate);
|
|
290 |
//
|
|
291 |
// Always copy the repeat count, so that the state is restored
|
|
292 |
// when we exit this scope:
|
|
293 |
//
|
|
294 |
repeater_count<BidiIterator> r(rep->state_id, &next_count, position);
|
|
295 |
//
|
|
296 |
// If we've had at least one repeat already, and the last one
|
|
297 |
// matched the NULL string then set the repeat count to
|
|
298 |
// maximum:
|
|
299 |
//
|
|
300 |
next_count->check_null_repeat(position, rep->max);
|
|
301 |
|
|
302 |
// find out which of these two alternatives we need to take:
|
|
303 |
bool take_first, take_second;
|
|
304 |
if(position == last)
|
|
305 |
{
|
|
306 |
take_first = rep->can_be_null & mask_take;
|
|
307 |
take_second = rep->can_be_null & mask_skip;
|
|
308 |
}
|
|
309 |
else
|
|
310 |
{
|
|
311 |
take_first = can_start(*position, rep->_map, (unsigned char)mask_take);
|
|
312 |
take_second = can_start(*position, rep->_map, (unsigned char)mask_skip);
|
|
313 |
}
|
|
314 |
|
|
315 |
if(next_count->get_count() < rep->min)
|
|
316 |
{
|
|
317 |
// we must take the repeat:
|
|
318 |
if(take_first)
|
|
319 |
{
|
|
320 |
// increase the counter:
|
|
321 |
++(*next_count);
|
|
322 |
pstate = rep->next.p;
|
|
323 |
return match_all_states();
|
|
324 |
}
|
|
325 |
return false;
|
|
326 |
}
|
|
327 |
bool greedy = (rep->greedy) && (!(m_match_flags & regex_constants::match_any) || m_independent);
|
|
328 |
if(greedy)
|
|
329 |
{
|
|
330 |
// try and take the repeat if we can:
|
|
331 |
if((next_count->get_count() < rep->max) && take_first)
|
|
332 |
{
|
|
333 |
// store position in case we fail:
|
|
334 |
BidiIterator pos = position;
|
|
335 |
// increase the counter:
|
|
336 |
++(*next_count);
|
|
337 |
pstate = rep->next.p;
|
|
338 |
if(match_all_states())
|
|
339 |
return true;
|
|
340 |
// failed repeat, reset posistion and fall through for alternative:
|
|
341 |
position = pos;
|
|
342 |
}
|
|
343 |
if(take_second)
|
|
344 |
{
|
|
345 |
pstate = rep->alt.p;
|
|
346 |
return true;
|
|
347 |
}
|
|
348 |
return false; // can't take anything, fail...
|
|
349 |
}
|
|
350 |
else // non-greedy
|
|
351 |
{
|
|
352 |
// try and skip the repeat if we can:
|
|
353 |
if(take_second)
|
|
354 |
{
|
|
355 |
// store position in case we fail:
|
|
356 |
BidiIterator pos = position;
|
|
357 |
pstate = rep->alt.p;
|
|
358 |
if(match_all_states())
|
|
359 |
return true;
|
|
360 |
// failed alternative, reset posistion and fall through for repeat:
|
|
361 |
position = pos;
|
|
362 |
}
|
|
363 |
if((next_count->get_count() < rep->max) && take_first)
|
|
364 |
{
|
|
365 |
// increase the counter:
|
|
366 |
++(*next_count);
|
|
367 |
pstate = rep->next.p;
|
|
368 |
return match_all_states();
|
|
369 |
}
|
|
370 |
}
|
|
371 |
return false;
|
|
372 |
#ifdef BOOST_MSVC
|
|
373 |
#pragma warning(pop)
|
|
374 |
#endif
|
|
375 |
}
|
|
376 |
|
|
377 |
template <class BidiIterator, class Allocator, class traits>
|
|
378 |
bool perl_matcher<BidiIterator, Allocator, traits>::match_dot_repeat_slow()
|
|
379 |
{
|
|
380 |
#ifdef BOOST_MSVC
|
|
381 |
#pragma warning(push)
|
|
382 |
#pragma warning(disable:4127)
|
|
383 |
#endif
|
|
384 |
unsigned count = 0;
|
|
385 |
const re_repeat* rep = static_cast<const re_repeat*>(pstate);
|
|
386 |
re_syntax_base* psingle = rep->next.p;
|
|
387 |
// match compulsary repeats first:
|
|
388 |
while(count < rep->min)
|
|
389 |
{
|
|
390 |
pstate = psingle;
|
|
391 |
if(!match_wild())
|
|
392 |
return false;
|
|
393 |
++count;
|
|
394 |
}
|
|
395 |
bool greedy = (rep->greedy) && (!(m_match_flags & regex_constants::match_any) || m_independent);
|
|
396 |
if(greedy)
|
|
397 |
{
|
|
398 |
// normal repeat:
|
|
399 |
while(count < rep->max)
|
|
400 |
{
|
|
401 |
pstate = psingle;
|
|
402 |
if(!match_wild())
|
|
403 |
break;
|
|
404 |
++count;
|
|
405 |
}
|
|
406 |
if((rep->leading) && (count < rep->max))
|
|
407 |
restart = position;
|
|
408 |
pstate = rep;
|
|
409 |
return backtrack_till_match(count - rep->min);
|
|
410 |
}
|
|
411 |
else
|
|
412 |
{
|
|
413 |
// non-greedy, keep trying till we get a match:
|
|
414 |
BidiIterator save_pos;
|
|
415 |
do
|
|
416 |
{
|
|
417 |
if((rep->leading) && (rep->max == UINT_MAX))
|
|
418 |
restart = position;
|
|
419 |
pstate = rep->alt.p;
|
|
420 |
save_pos = position;
|
|
421 |
++state_count;
|
|
422 |
if(match_all_states())
|
|
423 |
return true;
|
|
424 |
if(count >= rep->max)
|
|
425 |
return false;
|
|
426 |
++count;
|
|
427 |
pstate = psingle;
|
|
428 |
position = save_pos;
|
|
429 |
if(!match_wild())
|
|
430 |
return false;
|
|
431 |
}while(true);
|
|
432 |
}
|
|
433 |
#ifdef BOOST_MSVC
|
|
434 |
#pragma warning(pop)
|
|
435 |
#endif
|
|
436 |
}
|
|
437 |
|
|
438 |
template <class BidiIterator, class Allocator, class traits>
|
|
439 |
bool perl_matcher<BidiIterator, Allocator, traits>::match_dot_repeat_fast()
|
|
440 |
{
|
|
441 |
#ifdef BOOST_MSVC
|
|
442 |
#pragma warning(push)
|
|
443 |
#pragma warning(disable:4127)
|
|
444 |
#endif
|
|
445 |
if(m_match_flags & match_not_dot_null)
|
|
446 |
return match_dot_repeat_slow();
|
|
447 |
if((static_cast<const re_dot*>(pstate->next.p)->mask & match_any_mask) == 0)
|
|
448 |
return match_dot_repeat_slow();
|
|
449 |
//
|
|
450 |
// start by working out how much we can skip:
|
|
451 |
//
|
|
452 |
const re_repeat* rep = static_cast<const re_repeat*>(pstate);
|
|
453 |
#ifdef BOOST_MSVC
|
|
454 |
#pragma warning(push)
|
|
455 |
#pragma warning(disable:4267)
|
|
456 |
#endif
|
|
457 |
bool greedy = (rep->greedy) && (!(m_match_flags & regex_constants::match_any) || m_independent);
|
|
458 |
std::size_t count = (std::min)(static_cast<std::size_t>(::boost::re_detail::distance(position, last)), static_cast<std::size_t>(greedy ? rep->max : rep->min));
|
|
459 |
if(rep->min > count)
|
|
460 |
{
|
|
461 |
position = last;
|
|
462 |
return false; // not enough text left to match
|
|
463 |
}
|
|
464 |
std::advance(position, count);
|
|
465 |
#ifdef BOOST_MSVC
|
|
466 |
#pragma warning(pop)
|
|
467 |
#endif
|
|
468 |
if((rep->leading) && (count < rep->max) && greedy)
|
|
469 |
restart = position;
|
|
470 |
if(greedy)
|
|
471 |
return backtrack_till_match(count - rep->min);
|
|
472 |
|
|
473 |
// non-greedy, keep trying till we get a match:
|
|
474 |
BidiIterator save_pos;
|
|
475 |
do
|
|
476 |
{
|
|
477 |
while((position != last) && (count < rep->max) && !can_start(*position, rep->_map, mask_skip))
|
|
478 |
{
|
|
479 |
++position;
|
|
480 |
++count;
|
|
481 |
}
|
|
482 |
if((rep->leading) && (rep->max == UINT_MAX))
|
|
483 |
restart = position;
|
|
484 |
pstate = rep->alt.p;
|
|
485 |
save_pos = position;
|
|
486 |
++state_count;
|
|
487 |
if(match_all_states())
|
|
488 |
return true;
|
|
489 |
if(count >= rep->max)
|
|
490 |
return false;
|
|
491 |
if(save_pos == last)
|
|
492 |
return false;
|
|
493 |
position = ++save_pos;
|
|
494 |
++count;
|
|
495 |
}while(true);
|
|
496 |
#ifdef BOOST_MSVC
|
|
497 |
#pragma warning(pop)
|
|
498 |
#endif
|
|
499 |
}
|
|
500 |
|
|
501 |
template <class BidiIterator, class Allocator, class traits>
|
|
502 |
bool perl_matcher<BidiIterator, Allocator, traits>::match_char_repeat()
|
|
503 |
{
|
|
504 |
#ifdef BOOST_MSVC
|
|
505 |
#pragma warning(push)
|
|
506 |
#pragma warning(disable:4127)
|
|
507 |
#pragma warning(disable:4267)
|
|
508 |
#endif
|
|
509 |
#ifdef __BORLANDC__
|
|
510 |
#pragma option push -w-8008 -w-8066 -w-8004
|
|
511 |
#endif
|
|
512 |
const re_repeat* rep = static_cast<const re_repeat*>(pstate);
|
|
513 |
BOOST_ASSERT(1 == static_cast<const re_literal*>(rep->next.p)->length);
|
|
514 |
const char_type what = *reinterpret_cast<const char_type*>(static_cast<const re_literal*>(rep->next.p) + 1);
|
|
515 |
//
|
|
516 |
// start by working out how much we can skip:
|
|
517 |
//
|
|
518 |
bool greedy = (rep->greedy) && (!(m_match_flags & regex_constants::match_any) || m_independent);
|
|
519 |
std::size_t count, desired;
|
|
520 |
if(::boost::is_random_access_iterator<BidiIterator>::value)
|
|
521 |
{
|
|
522 |
desired =
|
|
523 |
(std::min)(
|
|
524 |
(std::size_t)(greedy ? rep->max : rep->min),
|
|
525 |
(std::size_t)::boost::re_detail::distance(position, last));
|
|
526 |
count = desired;
|
|
527 |
++desired;
|
|
528 |
if(icase)
|
|
529 |
{
|
|
530 |
while(--desired && (traits_inst.translate_nocase(*position) == what))
|
|
531 |
{
|
|
532 |
++position;
|
|
533 |
}
|
|
534 |
}
|
|
535 |
else
|
|
536 |
{
|
|
537 |
while(--desired && (traits_inst.translate(*position) == what))
|
|
538 |
{
|
|
539 |
++position;
|
|
540 |
}
|
|
541 |
}
|
|
542 |
count = count - desired;
|
|
543 |
}
|
|
544 |
else
|
|
545 |
{
|
|
546 |
count = 0;
|
|
547 |
desired = greedy ? rep->max : rep->min;
|
|
548 |
while((count < desired) && (position != last) && (traits_inst.translate(*position, icase) == what))
|
|
549 |
{
|
|
550 |
++position;
|
|
551 |
++count;
|
|
552 |
}
|
|
553 |
}
|
|
554 |
if((rep->leading) && (count < rep->max) && greedy)
|
|
555 |
restart = position;
|
|
556 |
if(count < rep->min)
|
|
557 |
return false;
|
|
558 |
|
|
559 |
if(greedy)
|
|
560 |
return backtrack_till_match(count - rep->min);
|
|
561 |
|
|
562 |
// non-greedy, keep trying till we get a match:
|
|
563 |
BidiIterator save_pos;
|
|
564 |
do
|
|
565 |
{
|
|
566 |
while((position != last) && (count < rep->max) && !can_start(*position, rep->_map, mask_skip))
|
|
567 |
{
|
|
568 |
if((traits_inst.translate(*position, icase) == what))
|
|
569 |
{
|
|
570 |
++position;
|
|
571 |
++count;
|
|
572 |
}
|
|
573 |
else
|
|
574 |
return false; // counldn't repeat even though it was the only option
|
|
575 |
}
|
|
576 |
if((rep->leading) && (rep->max == UINT_MAX))
|
|
577 |
restart = position;
|
|
578 |
pstate = rep->alt.p;
|
|
579 |
save_pos = position;
|
|
580 |
++state_count;
|
|
581 |
if(match_all_states())
|
|
582 |
return true;
|
|
583 |
if(count >= rep->max)
|
|
584 |
return false;
|
|
585 |
position = save_pos;
|
|
586 |
if(position == last)
|
|
587 |
return false;
|
|
588 |
if(traits_inst.translate(*position, icase) == what)
|
|
589 |
{
|
|
590 |
++position;
|
|
591 |
++count;
|
|
592 |
}
|
|
593 |
else
|
|
594 |
{
|
|
595 |
return false;
|
|
596 |
}
|
|
597 |
}while(true);
|
|
598 |
#ifdef __BORLANDC__
|
|
599 |
#pragma option pop
|
|
600 |
#endif
|
|
601 |
#ifdef BOOST_MSVC
|
|
602 |
#pragma warning(pop)
|
|
603 |
#endif
|
|
604 |
}
|
|
605 |
|
|
606 |
template <class BidiIterator, class Allocator, class traits>
|
|
607 |
bool perl_matcher<BidiIterator, Allocator, traits>::match_set_repeat()
|
|
608 |
{
|
|
609 |
#ifdef BOOST_MSVC
|
|
610 |
#pragma warning(push)
|
|
611 |
#pragma warning(disable:4127)
|
|
612 |
#endif
|
|
613 |
#ifdef __BORLANDC__
|
|
614 |
#pragma option push -w-8008 -w-8066 -w-8004
|
|
615 |
#endif
|
|
616 |
const re_repeat* rep = static_cast<const re_repeat*>(pstate);
|
|
617 |
const unsigned char* map = static_cast<const re_set*>(rep->next.p)->_map;
|
|
618 |
unsigned count = 0;
|
|
619 |
//
|
|
620 |
// start by working out how much we can skip:
|
|
621 |
//
|
|
622 |
bool greedy = (rep->greedy) && (!(m_match_flags & regex_constants::match_any) || m_independent);
|
|
623 |
std::size_t desired = greedy ? rep->max : rep->min;
|
|
624 |
if(::boost::is_random_access_iterator<BidiIterator>::value)
|
|
625 |
{
|
|
626 |
BidiIterator end = position;
|
|
627 |
std::advance(end, (std::min)((std::size_t)::boost::re_detail::distance(position, last), desired));
|
|
628 |
BidiIterator origin(position);
|
|
629 |
while((position != end) && map[static_cast<unsigned char>(traits_inst.translate(*position, icase))])
|
|
630 |
{
|
|
631 |
++position;
|
|
632 |
}
|
|
633 |
count = (unsigned)::boost::re_detail::distance(origin, position);
|
|
634 |
}
|
|
635 |
else
|
|
636 |
{
|
|
637 |
while((count < desired) && (position != last) && map[static_cast<unsigned char>(traits_inst.translate(*position, icase))])
|
|
638 |
{
|
|
639 |
++position;
|
|
640 |
++count;
|
|
641 |
}
|
|
642 |
}
|
|
643 |
if((rep->leading) && (count < rep->max) && greedy)
|
|
644 |
restart = position;
|
|
645 |
if(count < rep->min)
|
|
646 |
return false;
|
|
647 |
|
|
648 |
if(greedy)
|
|
649 |
return backtrack_till_match(count - rep->min);
|
|
650 |
|
|
651 |
// non-greedy, keep trying till we get a match:
|
|
652 |
BidiIterator save_pos;
|
|
653 |
do
|
|
654 |
{
|
|
655 |
while((position != last) && (count < rep->max) && !can_start(*position, rep->_map, mask_skip))
|
|
656 |
{
|
|
657 |
if(map[static_cast<unsigned char>(traits_inst.translate(*position, icase))])
|
|
658 |
{
|
|
659 |
++position;
|
|
660 |
++count;
|
|
661 |
}
|
|
662 |
else
|
|
663 |
return false; // counldn't repeat even though it was the only option
|
|
664 |
}
|
|
665 |
if((rep->leading) && (rep->max == UINT_MAX))
|
|
666 |
restart = position;
|
|
667 |
pstate = rep->alt.p;
|
|
668 |
save_pos = position;
|
|
669 |
++state_count;
|
|
670 |
if(match_all_states())
|
|
671 |
return true;
|
|
672 |
if(count >= rep->max)
|
|
673 |
return false;
|
|
674 |
position = save_pos;
|
|
675 |
if(position == last)
|
|
676 |
return false;
|
|
677 |
if(map[static_cast<unsigned char>(traits_inst.translate(*position, icase))])
|
|
678 |
{
|
|
679 |
++position;
|
|
680 |
++count;
|
|
681 |
}
|
|
682 |
else
|
|
683 |
{
|
|
684 |
return false;
|
|
685 |
}
|
|
686 |
}while(true);
|
|
687 |
#ifdef __BORLANDC__
|
|
688 |
#pragma option pop
|
|
689 |
#endif
|
|
690 |
#ifdef BOOST_MSVC
|
|
691 |
#pragma warning(pop)
|
|
692 |
#endif
|
|
693 |
}
|
|
694 |
|
|
695 |
template <class BidiIterator, class Allocator, class traits>
|
|
696 |
bool perl_matcher<BidiIterator, Allocator, traits>::match_long_set_repeat()
|
|
697 |
{
|
|
698 |
#ifdef BOOST_MSVC
|
|
699 |
#pragma warning(push)
|
|
700 |
#pragma warning(disable:4127)
|
|
701 |
#endif
|
|
702 |
#ifdef __BORLANDC__
|
|
703 |
#pragma option push -w-8008 -w-8066 -w-8004
|
|
704 |
#endif
|
|
705 |
typedef typename traits::char_class_type char_class_type;
|
|
706 |
const re_repeat* rep = static_cast<const re_repeat*>(pstate);
|
|
707 |
const re_set_long<char_class_type>* set = static_cast<const re_set_long<char_class_type>*>(pstate->next.p);
|
|
708 |
unsigned count = 0;
|
|
709 |
//
|
|
710 |
// start by working out how much we can skip:
|
|
711 |
//
|
|
712 |
bool greedy = (rep->greedy) && (!(m_match_flags & regex_constants::match_any) || m_independent);
|
|
713 |
std::size_t desired = greedy ? rep->max : rep->min;
|
|
714 |
if(::boost::is_random_access_iterator<BidiIterator>::value)
|
|
715 |
{
|
|
716 |
BidiIterator end = position;
|
|
717 |
std::advance(end, (std::min)((std::size_t)::boost::re_detail::distance(position, last), desired));
|
|
718 |
BidiIterator origin(position);
|
|
719 |
while((position != end) && (position != re_is_set_member(position, last, set, re.get_data(), icase)))
|
|
720 |
{
|
|
721 |
++position;
|
|
722 |
}
|
|
723 |
count = (unsigned)::boost::re_detail::distance(origin, position);
|
|
724 |
}
|
|
725 |
else
|
|
726 |
{
|
|
727 |
while((count < desired) && (position != last) && (position != re_is_set_member(position, last, set, re.get_data(), icase)))
|
|
728 |
{
|
|
729 |
++position;
|
|
730 |
++count;
|
|
731 |
}
|
|
732 |
}
|
|
733 |
if((rep->leading) && (count < rep->max) && greedy)
|
|
734 |
restart = position;
|
|
735 |
if(count < rep->min)
|
|
736 |
return false;
|
|
737 |
|
|
738 |
if(greedy)
|
|
739 |
return backtrack_till_match(count - rep->min);
|
|
740 |
|
|
741 |
// non-greedy, keep trying till we get a match:
|
|
742 |
BidiIterator save_pos;
|
|
743 |
do
|
|
744 |
{
|
|
745 |
while((position != last) && (count < rep->max) && !can_start(*position, rep->_map, mask_skip))
|
|
746 |
{
|
|
747 |
if(position != re_is_set_member(position, last, set, re.get_data(), icase))
|
|
748 |
{
|
|
749 |
++position;
|
|
750 |
++count;
|
|
751 |
}
|
|
752 |
else
|
|
753 |
return false; // counldn't repeat even though it was the only option
|
|
754 |
}
|
|
755 |
if((rep->leading) && (rep->max == UINT_MAX))
|
|
756 |
restart = position;
|
|
757 |
pstate = rep->alt.p;
|
|
758 |
save_pos = position;
|
|
759 |
++state_count;
|
|
760 |
if(match_all_states())
|
|
761 |
return true;
|
|
762 |
if(count >= rep->max)
|
|
763 |
return false;
|
|
764 |
position = save_pos;
|
|
765 |
if(position == last)
|
|
766 |
return false;
|
|
767 |
if(position != re_is_set_member(position, last, set, re.get_data(), icase))
|
|
768 |
{
|
|
769 |
++position;
|
|
770 |
++count;
|
|
771 |
}
|
|
772 |
else
|
|
773 |
{
|
|
774 |
return false;
|
|
775 |
}
|
|
776 |
}while(true);
|
|
777 |
#ifdef __BORLANDC__
|
|
778 |
#pragma option pop
|
|
779 |
#endif
|
|
780 |
#ifdef BOOST_MSVC
|
|
781 |
#pragma warning(pop)
|
|
782 |
#endif
|
|
783 |
}
|
|
784 |
|
|
785 |
template <class BidiIterator, class Allocator, class traits>
|
|
786 |
bool perl_matcher<BidiIterator, Allocator, traits>::backtrack_till_match(std::size_t count)
|
|
787 |
{
|
|
788 |
#ifdef BOOST_MSVC
|
|
789 |
#pragma warning(push)
|
|
790 |
#pragma warning(disable:4127)
|
|
791 |
#endif
|
|
792 |
if((m_match_flags & match_partial) && (position == last))
|
|
793 |
m_has_partial_match = true;
|
|
794 |
|
|
795 |
const re_repeat* rep = static_cast<const re_repeat*>(pstate);
|
|
796 |
BidiIterator backtrack = position;
|
|
797 |
if(position == last)
|
|
798 |
{
|
|
799 |
if(rep->can_be_null & mask_skip)
|
|
800 |
{
|
|
801 |
pstate = rep->alt.p;
|
|
802 |
if(match_all_states())
|
|
803 |
return true;
|
|
804 |
}
|
|
805 |
if(count)
|
|
806 |
{
|
|
807 |
position = --backtrack;
|
|
808 |
--count;
|
|
809 |
}
|
|
810 |
else
|
|
811 |
return false;
|
|
812 |
}
|
|
813 |
do
|
|
814 |
{
|
|
815 |
while(count && !can_start(*position, rep->_map, mask_skip))
|
|
816 |
{
|
|
817 |
--position;
|
|
818 |
--count;
|
|
819 |
++state_count;
|
|
820 |
}
|
|
821 |
pstate = rep->alt.p;
|
|
822 |
backtrack = position;
|
|
823 |
if(match_all_states())
|
|
824 |
return true;
|
|
825 |
if(count == 0)
|
|
826 |
return false;
|
|
827 |
position = --backtrack;
|
|
828 |
++state_count;
|
|
829 |
--count;
|
|
830 |
}while(true);
|
|
831 |
#ifdef BOOST_MSVC
|
|
832 |
#pragma warning(pop)
|
|
833 |
#endif
|
|
834 |
}
|
|
835 |
|
|
836 |
} // namespace re_detail
|
|
837 |
} // namespace boost
|
|
838 |
#ifdef BOOST_MSVC
|
|
839 |
#pragma warning(pop)
|
|
840 |
#endif
|
|
841 |
|
|
842 |
#ifdef BOOST_MSVC
|
|
843 |
#pragma warning(push)
|
|
844 |
#pragma warning(disable: 4103)
|
|
845 |
#endif
|
|
846 |
#ifdef BOOST_HAS_ABI_HEADERS
|
|
847 |
# include BOOST_ABI_SUFFIX
|
|
848 |
#endif
|
|
849 |
#ifdef BOOST_MSVC
|
|
850 |
#pragma warning(pop)
|
|
851 |
#endif
|
|
852 |
|
|
853 |
#endif
|
|
854 |
|