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   *                common to both the recursive and non-recursive versions.
18   */
19 
20 #ifndef BOOST_REGEX_V4_PERL_MATCHER_COMMON_HPP
21 #define BOOST_REGEX_V4_PERL_MATCHER_COMMON_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 __BORLANDC__
35 #  pragma option push -w-8008 -w-8066
36 #endif
37 #ifdef BOOST_MSVC
38 #  pragma warning(push)
39 #  pragma warning(disable: 4800)
40 #endif
41 
42 namespace boost{
43 namespace re_detail{
44 
45 template <class BidiIterator, class Allocator, class traits>
construct_init(const basic_regex<char_type,traits> & e,match_flag_type f)46 void perl_matcher<BidiIterator, Allocator, traits>::construct_init(const basic_regex<char_type, traits>& e, match_flag_type f)
47 {
48    typedef typename regex_iterator_traits<BidiIterator>::iterator_category category;
49    typedef typename basic_regex<char_type, traits>::flag_type expression_flag_type;
50 
51    if(e.empty())
52    {
53       // precondition failure: e is not a valid regex.
54       std::invalid_argument ex("Invalid regular expression object");
55       boost::throw_exception(ex);
56    }
57    pstate = 0;
58    m_match_flags = f;
59    estimate_max_state_count(static_cast<category*>(0));
60    expression_flag_type re_f = re.flags();
61    icase = re_f & regex_constants::icase;
62    if(!(m_match_flags & (match_perl|match_posix)))
63    {
64       if((re_f & (regbase::main_option_type|regbase::no_perl_ex)) == 0)
65          m_match_flags |= match_perl;
66       else if((re_f & (regbase::main_option_type|regbase::emacs_ex)) == (regbase::basic_syntax_group|regbase::emacs_ex))
67          m_match_flags |= match_perl;
68       else if((re_f & (regbase::main_option_type|regbase::literal)) == (regbase::literal))
69          m_match_flags |= match_perl;
70       else
71          m_match_flags |= match_posix;
72    }
73    if(m_match_flags & match_posix)
74    {
75       m_temp_match.reset(new match_results<BidiIterator, Allocator>());
76       m_presult = m_temp_match.get();
77    }
78    else
79       m_presult = &m_result;
80 #ifdef BOOST_REGEX_NON_RECURSIVE
81    m_stack_base = 0;
82    m_backup_state = 0;
83 #endif
84    // find the value to use for matching word boundaries:
85    m_word_mask = re.get_data().m_word_mask;
86    // find bitmask to use for matching '.':
87    match_any_mask = static_cast<unsigned char>((f & match_not_dot_newline) ? re_detail::test_not_newline : re_detail::test_newline);
88 }
89 
90 template <class BidiIterator, class Allocator, class traits>
estimate_max_state_count(std::random_access_iterator_tag *)91 void perl_matcher<BidiIterator, Allocator, traits>::estimate_max_state_count(std::random_access_iterator_tag*)
92 {
93    //
94    // How many states should we allow our machine to visit before giving up?
95    // This is a heuristic: it takes the greater of O(N^2) and O(NS^2)
96    // where N is the length of the string, and S is the number of states
97    // in the machine.  It's tempting to up this to O(N^2S) or even O(N^2S^2)
98    // but these take unreasonably amounts of time to bale out in pathological
99    // cases.
100    //
101    // Calculate NS^2 first:
102    //
103    static const std::ptrdiff_t k = 100000;
104    std::ptrdiff_t dist = boost::re_detail::distance(base, last);
105    if(dist == 0)
106       dist = 1;
107    std::ptrdiff_t states = re.size();
108    if(states == 0)
109       states = 1;
110    states *= states;
111    if((std::numeric_limits<std::ptrdiff_t>::max)() / dist < states)
112    {
113       max_state_count = (std::min)((std::ptrdiff_t)BOOST_REGEX_MAX_STATE_COUNT, (std::numeric_limits<std::ptrdiff_t>::max)() - 2);
114       return;
115    }
116    states *= dist;
117    if((std::numeric_limits<std::ptrdiff_t>::max)() - k < states)
118    {
119       max_state_count = (std::min)((std::ptrdiff_t)BOOST_REGEX_MAX_STATE_COUNT, (std::numeric_limits<std::ptrdiff_t>::max)() - 2);
120       return;
121    }
122    states += k;
123 
124    max_state_count = states;
125 
126    //
127    // Now calculate N^2:
128    //
129    states = dist;
130    if((std::numeric_limits<std::ptrdiff_t>::max)() / dist < states)
131    {
132       max_state_count = (std::min)((std::ptrdiff_t)BOOST_REGEX_MAX_STATE_COUNT, (std::numeric_limits<std::ptrdiff_t>::max)() - 2);
133       return;
134    }
135    states *= dist;
136    if((std::numeric_limits<std::ptrdiff_t>::max)() - k < states)
137    {
138       max_state_count = (std::min)((std::ptrdiff_t)BOOST_REGEX_MAX_STATE_COUNT, (std::numeric_limits<std::ptrdiff_t>::max)() - 2);
139       return;
140    }
141    states += k;
142    //
143    // N^2 can be a very large number indeed, to prevent things getting out
144    // of control, cap the max states:
145    //
146    if(states > BOOST_REGEX_MAX_STATE_COUNT)
147       states = BOOST_REGEX_MAX_STATE_COUNT;
148    //
149    // If (the possibly capped) N^2 is larger than our first estimate,
150    // use this instead:
151    //
152    if(states > max_state_count)
153       max_state_count = states;
154 }
155 
156 template <class BidiIterator, class Allocator, class traits>
estimate_max_state_count(void *)157 inline void perl_matcher<BidiIterator, Allocator, traits>::estimate_max_state_count(void*)
158 {
159    // we don't know how long the sequence is:
160    max_state_count = BOOST_REGEX_MAX_STATE_COUNT;
161 }
162 
163 #ifdef BOOST_REGEX_HAS_MS_STACK_GUARD
164 template <class BidiIterator, class Allocator, class traits>
protected_call(protected_proc_type proc)165 inline bool perl_matcher<BidiIterator, Allocator, traits>::protected_call(
166    protected_proc_type proc)
167 {
168    ::boost::re_detail::concrete_protected_call
169       <perl_matcher<BidiIterator, Allocator, traits> >
170       obj(this, proc);
171    return obj.execute();
172 
173 }
174 #endif
175 
176 template <class BidiIterator, class Allocator, class traits>
match()177 inline bool perl_matcher<BidiIterator, Allocator, traits>::match()
178 {
179 #ifdef BOOST_REGEX_HAS_MS_STACK_GUARD
180    return protected_call(&perl_matcher<BidiIterator, Allocator, traits>::match_imp);
181 #else
182    return match_imp();
183 #endif
184 }
185 
186 template <class BidiIterator, class Allocator, class traits>
match_imp()187 bool perl_matcher<BidiIterator, Allocator, traits>::match_imp()
188 {
189    // initialise our stack if we are non-recursive:
190 #ifdef BOOST_REGEX_NON_RECURSIVE
191    save_state_init init(&m_stack_base, &m_backup_state);
192    used_block_count = BOOST_REGEX_MAX_BLOCKS;
193 #if !defined(BOOST_NO_EXCEPTIONS)
194    try{
195 #endif
196 #endif
197 
198    // reset our state machine:
199    position = base;
200    search_base = base;
201    state_count = 0;
202    m_match_flags |= regex_constants::match_all;
203    m_presult->set_size((m_match_flags & match_nosubs) ? 1 : 1 + re.mark_count(), search_base, last);
204    m_presult->set_base(base);
205    m_presult->set_named_subs(this->re.get_named_subs());
206    if(m_match_flags & match_posix)
207       m_result = *m_presult;
208    verify_options(re.flags(), m_match_flags);
209    if(0 == match_prefix())
210       return false;
211    return (m_result[0].second == last) && (m_result[0].first == base);
212 
213 #if defined(BOOST_REGEX_NON_RECURSIVE) && !defined(BOOST_NO_EXCEPTIONS)
214    }
215    catch(...)
216    {
217       // unwind all pushed states, apart from anything else this
218       // ensures that all the states are correctly destructed
219       // not just the memory freed.
220       while(unwind(true)){}
221       throw;
222    }
223 #endif
224 }
225 
226 template <class BidiIterator, class Allocator, class traits>
find()227 inline bool perl_matcher<BidiIterator, Allocator, traits>::find()
228 {
229 #ifdef BOOST_REGEX_HAS_MS_STACK_GUARD
230    return protected_call(&perl_matcher<BidiIterator, Allocator, traits>::find_imp);
231 #else
232    return find_imp();
233 #endif
234 }
235 
236 template <class BidiIterator, class Allocator, class traits>
find_imp()237 bool perl_matcher<BidiIterator, Allocator, traits>::find_imp()
238 {
239    static matcher_proc_type const s_find_vtable[7] =
240    {
241       &perl_matcher<BidiIterator, Allocator, traits>::find_restart_any,
242       &perl_matcher<BidiIterator, Allocator, traits>::find_restart_word,
243       &perl_matcher<BidiIterator, Allocator, traits>::find_restart_line,
244       &perl_matcher<BidiIterator, Allocator, traits>::find_restart_buf,
245       &perl_matcher<BidiIterator, Allocator, traits>::match_prefix,
246       &perl_matcher<BidiIterator, Allocator, traits>::find_restart_lit,
247       &perl_matcher<BidiIterator, Allocator, traits>::find_restart_lit,
248    };
249 
250    // initialise our stack if we are non-recursive:
251 #ifdef BOOST_REGEX_NON_RECURSIVE
252    save_state_init init(&m_stack_base, &m_backup_state);
253    used_block_count = BOOST_REGEX_MAX_BLOCKS;
254 #if !defined(BOOST_NO_EXCEPTIONS)
255    try{
256 #endif
257 #endif
258 
259    state_count = 0;
260    if((m_match_flags & regex_constants::match_init) == 0)
261    {
262       // reset our state machine:
263       search_base = position = base;
264       pstate = re.get_first_state();
265       m_presult->set_size((m_match_flags & match_nosubs) ? 1 : 1 + re.mark_count(), base, last);
266       m_presult->set_base(base);
267       m_presult->set_named_subs(this->re.get_named_subs());
268       m_match_flags |= regex_constants::match_init;
269    }
270    else
271    {
272       // start again:
273       search_base = position = m_result[0].second;
274       // If last match was null and match_not_null was not set then increment
275       // our start position, otherwise we go into an infinite loop:
276       if(((m_match_flags & match_not_null) == 0) && (m_result.length() == 0))
277       {
278          if(position == last)
279             return false;
280          else
281             ++position;
282       }
283       // reset $` start:
284       m_presult->set_size((m_match_flags & match_nosubs) ? 1 : 1 + re.mark_count(), search_base, last);
285       //if((base != search_base) && (base == backstop))
286       //   m_match_flags |= match_prev_avail;
287    }
288    if(m_match_flags & match_posix)
289    {
290       m_result.set_size(1 + re.mark_count(), base, last);
291       m_result.set_base(base);
292    }
293 
294    verify_options(re.flags(), m_match_flags);
295    // find out what kind of expression we have:
296    unsigned type = (m_match_flags & match_continuous) ?
297       static_cast<unsigned int>(regbase::restart_continue)
298          : static_cast<unsigned int>(re.get_restart_type());
299 
300    // call the appropriate search routine:
301    matcher_proc_type proc = s_find_vtable[type];
302    return (this->*proc)();
303 
304 #if defined(BOOST_REGEX_NON_RECURSIVE) && !defined(BOOST_NO_EXCEPTIONS)
305    }
306    catch(...)
307    {
308       // unwind all pushed states, apart from anything else this
309       // ensures that all the states are correctly destructed
310       // not just the memory freed.
311       while(unwind(true)){}
312       throw;
313    }
314 #endif
315 }
316 
317 template <class BidiIterator, class Allocator, class traits>
match_prefix()318 bool perl_matcher<BidiIterator, Allocator, traits>::match_prefix()
319 {
320    m_has_partial_match = false;
321    m_has_found_match = false;
322    pstate = re.get_first_state();
323    m_presult->set_first(position);
324    restart = position;
325    match_all_states();
326    if(!m_has_found_match && m_has_partial_match && (m_match_flags & match_partial))
327    {
328       m_has_found_match = true;
329       m_presult->set_second(last, 0, false);
330       position = last;
331       if((m_match_flags & match_posix) == match_posix)
332       {
333          m_result.maybe_assign(*m_presult);
334       }
335    }
336 #ifdef BOOST_REGEX_MATCH_EXTRA
337    if(m_has_found_match && (match_extra & m_match_flags))
338    {
339       //
340       // we have a match, reverse the capture information:
341       //
342       for(unsigned i = 0; i < m_presult->size(); ++i)
343       {
344          typename sub_match<BidiIterator>::capture_sequence_type & seq = ((*m_presult)[i]).get_captures();
345          std::reverse(seq.begin(), seq.end());
346       }
347    }
348 #endif
349    if(!m_has_found_match)
350       position = restart; // reset search postion
351    return m_has_found_match;
352 }
353 
354 template <class BidiIterator, class Allocator, class traits>
match_literal()355 bool perl_matcher<BidiIterator, Allocator, traits>::match_literal()
356 {
357    unsigned int len = static_cast<const re_literal*>(pstate)->length;
358    const char_type* what = reinterpret_cast<const char_type*>(static_cast<const re_literal*>(pstate) + 1);
359    //
360    // compare string with what we stored in
361    // our records:
362    for(unsigned int i = 0; i < len; ++i, ++position)
363    {
364       if((position == last) || (traits_inst.translate(*position, icase) != what[i]))
365          return false;
366    }
367    pstate = pstate->next.p;
368    return true;
369 }
370 
371 template <class BidiIterator, class Allocator, class traits>
match_start_line()372 bool perl_matcher<BidiIterator, Allocator, traits>::match_start_line()
373 {
374    if(position == backstop)
375    {
376       if((m_match_flags & match_prev_avail) == 0)
377       {
378          if((m_match_flags & match_not_bol) == 0)
379          {
380             pstate = pstate->next.p;
381             return true;
382          }
383          return false;
384       }
385    }
386    else if(m_match_flags & match_single_line)
387       return false;
388 
389    // check the previous value character:
390    BidiIterator t(position);
391    --t;
392    if(position != last)
393    {
394       if(is_separator(*t) && !((*t == static_cast<char_type>('\r')) && (*position == static_cast<char_type>('\n'))) )
395       {
396          pstate = pstate->next.p;
397          return true;
398       }
399    }
400    else if(is_separator(*t))
401    {
402       pstate = pstate->next.p;
403       return true;
404    }
405    return false;
406 }
407 
408 template <class BidiIterator, class Allocator, class traits>
match_end_line()409 bool perl_matcher<BidiIterator, Allocator, traits>::match_end_line()
410 {
411    if(position != last)
412    {
413       if(m_match_flags & match_single_line)
414          return false;
415       // we're not yet at the end so *first is always valid:
416       if(is_separator(*position))
417       {
418          if((position != backstop) || (m_match_flags & match_prev_avail))
419          {
420             // check that we're not in the middle of \r\n sequence
421             BidiIterator t(position);
422             --t;
423             if((*t == static_cast<char_type>('\r')) && (*position == static_cast<char_type>('\n')))
424             {
425                return false;
426             }
427          }
428          pstate = pstate->next.p;
429          return true;
430       }
431    }
432    else if((m_match_flags & match_not_eol) == 0)
433    {
434       pstate = pstate->next.p;
435       return true;
436    }
437    return false;
438 }
439 
440 template <class BidiIterator, class Allocator, class traits>
match_wild()441 bool perl_matcher<BidiIterator, Allocator, traits>::match_wild()
442 {
443    if(position == last)
444       return false;
445    if(is_separator(*position) && ((match_any_mask & static_cast<const re_dot*>(pstate)->mask) == 0))
446       return false;
447    if((*position == char_type(0)) && (m_match_flags & match_not_dot_null))
448       return false;
449    pstate = pstate->next.p;
450    ++position;
451    return true;
452 }
453 
454 template <class BidiIterator, class Allocator, class traits>
match_word_boundary()455 bool perl_matcher<BidiIterator, Allocator, traits>::match_word_boundary()
456 {
457    bool b; // indcates whether next character is a word character
458    if(position != last)
459    {
460       // prev and this character must be opposites:
461       b = traits_inst.isctype(*position, m_word_mask);
462    }
463    else
464    {
465       b = (m_match_flags & match_not_eow) ? true : false;
466    }
467    if((position == backstop) && ((m_match_flags & match_prev_avail) == 0))
468    {
469       if(m_match_flags & match_not_bow)
470          b ^= true;
471       else
472          b ^= false;
473    }
474    else
475    {
476       --position;
477       b ^= traits_inst.isctype(*position, m_word_mask);
478       ++position;
479    }
480    if(b)
481    {
482       pstate = pstate->next.p;
483       return true;
484    }
485    return false; // no match if we get to here...
486 }
487 
488 template <class BidiIterator, class Allocator, class traits>
match_within_word()489 bool perl_matcher<BidiIterator, Allocator, traits>::match_within_word()
490 {
491    if(position == last)
492       return false;
493    // both prev and this character must be m_word_mask:
494    bool prev = traits_inst.isctype(*position, m_word_mask);
495    {
496       bool b;
497       if((position == backstop) && ((m_match_flags & match_prev_avail) == 0))
498          return false;
499       else
500       {
501          --position;
502          b = traits_inst.isctype(*position, m_word_mask);
503          ++position;
504       }
505       if(b == prev)
506       {
507          pstate = pstate->next.p;
508          return true;
509       }
510    }
511    return false;
512 }
513 
514 template <class BidiIterator, class Allocator, class traits>
match_word_start()515 bool perl_matcher<BidiIterator, Allocator, traits>::match_word_start()
516 {
517    if(position == last)
518       return false; // can't be starting a word if we're already at the end of input
519    if(!traits_inst.isctype(*position, m_word_mask))
520       return false; // next character isn't a word character
521    if((position == backstop) && ((m_match_flags & match_prev_avail) == 0))
522    {
523       if(m_match_flags & match_not_bow)
524          return false; // no previous input
525    }
526    else
527    {
528       // otherwise inside buffer:
529       BidiIterator t(position);
530       --t;
531       if(traits_inst.isctype(*t, m_word_mask))
532          return false; // previous character not non-word
533    }
534    // OK we have a match:
535    pstate = pstate->next.p;
536    return true;
537 }
538 
539 template <class BidiIterator, class Allocator, class traits>
match_word_end()540 bool perl_matcher<BidiIterator, Allocator, traits>::match_word_end()
541 {
542    if((position == backstop) && ((m_match_flags & match_prev_avail) == 0))
543       return false;  // start of buffer can't be end of word
544    BidiIterator t(position);
545    --t;
546    if(traits_inst.isctype(*t, m_word_mask) == false)
547       return false;  // previous character wasn't a word character
548 
549    if(position == last)
550    {
551       if(m_match_flags & match_not_eow)
552          return false; // end of buffer but not end of word
553    }
554    else
555    {
556       // otherwise inside buffer:
557       if(traits_inst.isctype(*position, m_word_mask))
558          return false; // next character is a word character
559    }
560    pstate = pstate->next.p;
561    return true;      // if we fall through to here then we've succeeded
562 }
563 
564 template <class BidiIterator, class Allocator, class traits>
match_buffer_start()565 bool perl_matcher<BidiIterator, Allocator, traits>::match_buffer_start()
566 {
567    if((position != backstop) || (m_match_flags & match_not_bob))
568       return false;
569    // OK match:
570    pstate = pstate->next.p;
571    return true;
572 }
573 
574 template <class BidiIterator, class Allocator, class traits>
match_buffer_end()575 bool perl_matcher<BidiIterator, Allocator, traits>::match_buffer_end()
576 {
577    if((position != last) || (m_match_flags & match_not_eob))
578       return false;
579    // OK match:
580    pstate = pstate->next.p;
581    return true;
582 }
583 
584 template <class BidiIterator, class Allocator, class traits>
match_backref()585 bool perl_matcher<BidiIterator, Allocator, traits>::match_backref()
586 {
587    //
588    // Compare with what we previously matched.
589    // Note that this succeeds if the backref did not partisipate
590    // in the match, this is in line with ECMAScript, but not Perl
591    // or PCRE.
592    //
593    int index = static_cast<const re_brace*>(pstate)->index;
594    if(index >= 10000)
595    {
596       named_subexpressions::range_type r = re.get_data().equal_range(index);
597       BOOST_ASSERT(r.first != r.second);
598       do
599       {
600          index = r.first->index;
601          ++r.first;
602       }while((r.first != r.second) && ((*m_presult)[index].matched != true));
603    }
604 
605    if((m_match_flags & match_perl) && !(*m_presult)[index].matched)
606       return false;
607 
608    BidiIterator i = (*m_presult)[index].first;
609    BidiIterator j = (*m_presult)[index].second;
610    while(i != j)
611    {
612       if((position == last) || (traits_inst.translate(*position, icase) != traits_inst.translate(*i, icase)))
613          return false;
614       ++i;
615       ++position;
616    }
617    pstate = pstate->next.p;
618    return true;
619 }
620 
621 template <class BidiIterator, class Allocator, class traits>
match_long_set()622 bool perl_matcher<BidiIterator, Allocator, traits>::match_long_set()
623 {
624    typedef typename traits::char_class_type char_class_type;
625    // let the traits class do the work:
626    if(position == last)
627       return false;
628    BidiIterator t = re_is_set_member(position, last, static_cast<const re_set_long<char_class_type>*>(pstate), re.get_data(), icase);
629    if(t != position)
630    {
631       pstate = pstate->next.p;
632       position = t;
633       return true;
634    }
635    return false;
636 }
637 
638 template <class BidiIterator, class Allocator, class traits>
match_set()639 bool perl_matcher<BidiIterator, Allocator, traits>::match_set()
640 {
641    if(position == last)
642       return false;
643    if(static_cast<const re_set*>(pstate)->_map[static_cast<unsigned char>(traits_inst.translate(*position, icase))])
644    {
645       pstate = pstate->next.p;
646       ++position;
647       return true;
648    }
649    return false;
650 }
651 
652 template <class BidiIterator, class Allocator, class traits>
match_jump()653 bool perl_matcher<BidiIterator, Allocator, traits>::match_jump()
654 {
655    pstate = static_cast<const re_jump*>(pstate)->alt.p;
656    return true;
657 }
658 
659 template <class BidiIterator, class Allocator, class traits>
match_combining()660 bool perl_matcher<BidiIterator, Allocator, traits>::match_combining()
661 {
662    if(position == last)
663       return false;
664    if(is_combining(traits_inst.translate(*position, icase)))
665       return false;
666    ++position;
667    while((position != last) && is_combining(traits_inst.translate(*position, icase)))
668       ++position;
669    pstate = pstate->next.p;
670    return true;
671 }
672 
673 template <class BidiIterator, class Allocator, class traits>
match_soft_buffer_end()674 bool perl_matcher<BidiIterator, Allocator, traits>::match_soft_buffer_end()
675 {
676    if(m_match_flags & match_not_eob)
677       return false;
678    BidiIterator p(position);
679    while((p != last) && is_separator(traits_inst.translate(*p, icase)))++p;
680    if(p != last)
681       return false;
682    pstate = pstate->next.p;
683    return true;
684 }
685 
686 template <class BidiIterator, class Allocator, class traits>
match_restart_continue()687 bool perl_matcher<BidiIterator, Allocator, traits>::match_restart_continue()
688 {
689    if(position == search_base)
690    {
691       pstate = pstate->next.p;
692       return true;
693    }
694    return false;
695 }
696 
697 template <class BidiIterator, class Allocator, class traits>
match_backstep()698 bool perl_matcher<BidiIterator, Allocator, traits>::match_backstep()
699 {
700 #ifdef BOOST_MSVC
701 #pragma warning(push)
702 #pragma warning(disable:4127)
703 #endif
704    if( ::boost::is_random_access_iterator<BidiIterator>::value)
705    {
706       std::ptrdiff_t maxlen = ::boost::re_detail::distance(backstop, position);
707       if(maxlen < static_cast<const re_brace*>(pstate)->index)
708          return false;
709       std::advance(position, -static_cast<const re_brace*>(pstate)->index);
710    }
711    else
712    {
713       int c = static_cast<const re_brace*>(pstate)->index;
714       while(c--)
715       {
716          if(position == backstop)
717             return false;
718          --position;
719       }
720    }
721    pstate = pstate->next.p;
722    return true;
723 #ifdef BOOST_MSVC
724 #pragma warning(pop)
725 #endif
726 }
727 
728 template <class BidiIterator, class Allocator, class traits>
match_assert_backref()729 inline bool perl_matcher<BidiIterator, Allocator, traits>::match_assert_backref()
730 {
731    // return true if marked sub-expression N has been matched:
732    int index = static_cast<const re_brace*>(pstate)->index;
733    bool result = false;
734    if(index == 9999)
735    {
736       // Magic value for a (DEFINE) block:
737       return false;
738    }
739    else if(index > 0)
740    {
741       // Have we matched subexpression "index"?
742       // Check if index is a hash value:
743       if(index >= 10000)
744       {
745          named_subexpressions::range_type r = re.get_data().equal_range(index);
746          while(r.first != r.second)
747          {
748             if((*m_presult)[r.first->index].matched)
749             {
750                result = true;
751                break;
752             }
753             ++r.first;
754          }
755       }
756       else
757       {
758          result = (*m_presult)[index].matched;
759       }
760       pstate = pstate->next.p;
761    }
762    else
763    {
764       // Have we recursed into subexpression "index"?
765       // If index == 0 then check for any recursion at all, otherwise for recursion to -index-1.
766       int idx = -index-1;
767       if(idx >= 10000)
768       {
769          named_subexpressions::range_type r = re.get_data().equal_range(idx);
770          int stack_index = recursion_stack.empty() ? -1 : recursion_stack.back().idx;
771          while(r.first != r.second)
772          {
773             result |= (stack_index == r.first->index);
774             if(result)break;
775             ++r.first;
776          }
777       }
778       else
779       {
780          result = !recursion_stack.empty() && ((recursion_stack.back().idx == idx) || (index == 0));
781       }
782       pstate = pstate->next.p;
783    }
784    return result;
785 }
786 
787 template <class BidiIterator, class Allocator, class traits>
match_toggle_case()788 bool perl_matcher<BidiIterator, Allocator, traits>::match_toggle_case()
789 {
790    // change our case sensitivity:
791    this->icase = static_cast<const re_case*>(pstate)->icase;
792    pstate = pstate->next.p;
793    return true;
794 }
795 
796 
797 template <class BidiIterator, class Allocator, class traits>
find_restart_any()798 bool perl_matcher<BidiIterator, Allocator, traits>::find_restart_any()
799 {
800 #ifdef BOOST_MSVC
801 #pragma warning(push)
802 #pragma warning(disable:4127)
803 #endif
804    const unsigned char* _map = re.get_map();
805    while(true)
806    {
807       // skip everything we can't match:
808       while((position != last) && !can_start(*position, _map, (unsigned char)mask_any) )
809          ++position;
810       if(position == last)
811       {
812          // run out of characters, try a null match if possible:
813          if(re.can_be_null())
814             return match_prefix();
815          break;
816       }
817       // now try and obtain a match:
818       if(match_prefix())
819          return true;
820       if(position == last)
821          return false;
822       ++position;
823    }
824    return false;
825 #ifdef BOOST_MSVC
826 #pragma warning(pop)
827 #endif
828 }
829 
830 template <class BidiIterator, class Allocator, class traits>
find_restart_word()831 bool perl_matcher<BidiIterator, Allocator, traits>::find_restart_word()
832 {
833 #ifdef BOOST_MSVC
834 #pragma warning(push)
835 #pragma warning(disable:4127)
836 #endif
837    // do search optimised for word starts:
838    const unsigned char* _map = re.get_map();
839    if((m_match_flags & match_prev_avail) || (position != base))
840       --position;
841    else if(match_prefix())
842       return true;
843    do
844    {
845       while((position != last) && traits_inst.isctype(*position, m_word_mask))
846          ++position;
847       while((position != last) && !traits_inst.isctype(*position, m_word_mask))
848          ++position;
849       if(position == last)
850          break;
851 
852       if(can_start(*position, _map, (unsigned char)mask_any) )
853       {
854          if(match_prefix())
855             return true;
856       }
857       if(position == last)
858          break;
859    } while(true);
860    return false;
861 #ifdef BOOST_MSVC
862 #pragma warning(pop)
863 #endif
864 }
865 
866 template <class BidiIterator, class Allocator, class traits>
find_restart_line()867 bool perl_matcher<BidiIterator, Allocator, traits>::find_restart_line()
868 {
869    // do search optimised for line starts:
870    const unsigned char* _map = re.get_map();
871    if(match_prefix())
872       return true;
873    while(position != last)
874    {
875       while((position != last) && !is_separator(*position))
876          ++position;
877       if(position == last)
878          return false;
879       ++position;
880       if(position == last)
881       {
882          if(re.can_be_null() && match_prefix())
883             return true;
884          return false;
885       }
886 
887       if( can_start(*position, _map, (unsigned char)mask_any) )
888       {
889          if(match_prefix())
890             return true;
891       }
892       if(position == last)
893          return false;
894       //++position;
895    }
896    return false;
897 }
898 
899 template <class BidiIterator, class Allocator, class traits>
find_restart_buf()900 bool perl_matcher<BidiIterator, Allocator, traits>::find_restart_buf()
901 {
902    if((position == base) && ((m_match_flags & match_not_bob) == 0))
903       return match_prefix();
904    return false;
905 }
906 
907 template <class BidiIterator, class Allocator, class traits>
find_restart_lit()908 bool perl_matcher<BidiIterator, Allocator, traits>::find_restart_lit()
909 {
910 #if 0
911    if(position == last)
912       return false; // can't possibly match if we're at the end already
913 
914    unsigned type = (m_match_flags & match_continuous) ?
915       static_cast<unsigned int>(regbase::restart_continue)
916          : static_cast<unsigned int>(re.get_restart_type());
917 
918    const kmp_info<char_type>* info = access::get_kmp(re);
919    int len = info->len;
920    const char_type* x = info->pstr;
921    int j = 0;
922    while (position != last)
923    {
924       while((j > -1) && (x[j] != traits_inst.translate(*position, icase)))
925          j = info->kmp_next[j];
926       ++position;
927       ++j;
928       if(j >= len)
929       {
930          if(type == regbase::restart_fixed_lit)
931          {
932             std::advance(position, -j);
933             restart = position;
934             std::advance(restart, len);
935             m_result.set_first(position);
936             m_result.set_second(restart);
937             position = restart;
938             return true;
939          }
940          else
941          {
942             restart = position;
943             std::advance(position, -j);
944             if(match_prefix())
945                return true;
946             else
947             {
948                for(int k = 0; (restart != position) && (k < j); ++k, --restart)
949                      {} // dwa 10/20/2000 - warning suppression for MWCW
950                if(restart != last)
951                   ++restart;
952                position = restart;
953                j = 0;  //we could do better than this...
954             }
955          }
956       }
957    }
958    if((m_match_flags & match_partial) && (position == last) && j)
959    {
960       // we need to check for a partial match:
961       restart = position;
962       std::advance(position, -j);
963       return match_prefix();
964    }
965 #endif
966    return false;
967 }
968 
969 } // namespace re_detail
970 
971 } // namespace boost
972 
973 #ifdef BOOST_MSVC
974 #  pragma warning(pop)
975 #endif
976 
977 #ifdef __BORLANDC__
978 #  pragma option pop
979 #endif
980 #ifdef BOOST_MSVC
981 #pragma warning(push)
982 #pragma warning(disable: 4103)
983 #endif
984 #ifdef BOOST_HAS_ABI_HEADERS
985 #  include BOOST_ABI_SUFFIX
986 #endif
987 #ifdef BOOST_MSVC
988 #pragma warning(pop)
989 #endif
990 
991 #endif
992 
993