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)
12 #ifndef BOOST_REGEX_MATCHER_HPP
13 #define BOOST_REGEX_MATCHER_HPP
15 #include <boost/regex/v4/iterator_category.hpp>
19 #pragma warning(disable: 4103)
21 #ifdef BOOST_HAS_ABI_HEADERS
22 # include BOOST_ABI_PREFIX
29 # pragma warning(push)
30 # pragma warning(disable: 4800)
34 namespace BOOST_REGEX_DETAIL_NS{
37 // error checking API:
39 BOOST_REGEX_DECL void BOOST_REGEX_CALL verify_options(boost::regex_constants::syntax_option_type ef, match_flag_type mf);
41 // function can_start:
43 template <class charT>
44 inline bool can_start(charT c, const unsigned char* map, unsigned char mask)
46 return ((c < static_cast<charT>(0)) ? true : ((c >= static_cast<charT>(1 << CHAR_BIT)) ? true : map[c] & mask));
48 inline bool can_start(char c, const unsigned char* map, unsigned char mask)
50 return map[(unsigned char)c] & mask;
52 inline bool can_start(signed char c, const unsigned char* map, unsigned char mask)
54 return map[(unsigned char)c] & mask;
56 inline bool can_start(unsigned char c, const unsigned char* map, unsigned char mask)
60 inline bool can_start(unsigned short c, const unsigned char* map, unsigned char mask)
62 return ((c >= (1 << CHAR_BIT)) ? true : map[c] & mask);
64 #if !defined(__hpux) && !defined(__WINSCW__)// WCHAR_MIN not usable in pp-directives.
65 #if defined(WCHAR_MIN) && (WCHAR_MIN == 0) && !defined(BOOST_NO_INTRINSIC_WCHAR_T)
66 inline bool can_start(wchar_t c, const unsigned char* map, unsigned char mask)
68 return ((c >= static_cast<wchar_t>(1u << CHAR_BIT)) ? true : map[c] & mask);
72 #if !defined(BOOST_NO_INTRINSIC_WCHAR_T)
73 inline bool can_start(unsigned int c, const unsigned char* map, unsigned char mask)
75 return (((c >= static_cast<unsigned int>(1u << CHAR_BIT)) ? true : map[c] & mask));
81 // Unfortunately Rogue Waves standard library appears to have a bug
82 // in std::basic_string::compare that results in eroneous answers
83 // in some cases (tested with Borland C++ 5.1, Rogue Wave lib version
84 // 0x020101) the test case was:
85 // {39135,0} < {0xff,0}
86 // which succeeds when it should not.
89 template <class C, class T, class A>
90 inline int string_compare(const std::basic_string<C,T,A>& s, const C* p)
94 if(s.empty() || ((s.size() == 1) && (s[0] == 0)))
100 template <class C, class T, class A>
101 inline int string_compare(const std::basic_string<C,T,A>& s, const C* p)
105 if(s.empty() || ((s.size() == 1) && (s[0] == 0)))
110 inline int string_compare(const std::string& s, const char* p)
111 { return std::strcmp(s.c_str(), p); }
112 # ifndef BOOST_NO_WREGEX
113 inline int string_compare(const std::wstring& s, const wchar_t* p)
114 { return std::wcscmp(s.c_str(), p); }
117 template <class Seq, class C>
118 inline int string_compare(const Seq& s, const C* p)
121 while((i < s.size()) && (p[i] == s[i]))
125 return (i == s.size()) ? -(int)p[i] : (int)s[i] - (int)p[i];
127 # define STR_COMP(s,p) string_compare(s,p)
129 template<class charT>
130 inline const charT* re_skip_past_null(const charT* p)
132 while (*p != static_cast<charT>(0)) ++p;
136 template <class iterator, class charT, class traits_type, class char_classT>
137 iterator BOOST_REGEX_CALL re_is_set_member(iterator next,
139 const re_set_long<char_classT>* set_,
140 const regex_data<charT, traits_type>& e, bool icase)
142 const charT* p = reinterpret_cast<const charT*>(set_+1);
145 //bool icase = e.m_flags & regex_constants::icase;
147 if(next == last) return next;
149 typedef typename traits_type::string_type traits_string_type;
150 const ::boost::regex_traits_wrapper<traits_type>& traits_inst = *(e.m_ptraits);
152 // dwa 9/13/00 suppress incorrect MSVC warning - it claims this is never
156 // try and match a single character, could be a multi-character
157 // collating element...
158 for(i = 0; i < set_->csingles; ++i)
161 if(*p == static_cast<charT>(0))
163 // treat null string as special case:
164 if(traits_inst.translate(*ptr, icase))
169 return set_->isnot ? next : (ptr == next) ? ++next : ptr;
173 while(*p && (ptr != last))
175 if(traits_inst.translate(*ptr, icase) != *p)
181 if(*p == static_cast<charT>(0)) // if null we've matched
182 return set_->isnot ? next : (ptr == next) ? ++next : ptr;
184 p = re_skip_past_null(p); // skip null
188 charT col = traits_inst.translate(*next, icase);
191 if(set_->cranges || set_->cequivalents)
193 traits_string_type s1;
195 // try and match a range, NB only a single character can match
198 if((e.m_flags & regex_constants::collate) == 0)
202 charT a[2] = { col, charT(0), };
203 s1 = traits_inst.transform(a, a + 1);
205 for(i = 0; i < set_->cranges; ++i)
207 if(STR_COMP(s1, p) >= 0)
211 if(STR_COMP(s1, p) <= 0)
212 return set_->isnot ? next : ++next;
220 // skip second string
226 // try and match an equivalence class, NB only a single character can match
227 if(set_->cequivalents)
229 charT a[2] = { col, charT(0), };
230 s1 = traits_inst.transform_primary(a, a +1);
231 for(i = 0; i < set_->cequivalents; ++i)
233 if(STR_COMP(s1, p) == 0)
234 return set_->isnot ? next : ++next;
241 if(traits_inst.isctype(col, set_->cclasses) == true)
242 return set_->isnot ? next : ++next;
243 if((set_->cnclasses != 0) && (traits_inst.isctype(col, set_->cnclasses) == false))
244 return set_->isnot ? next : ++next;
245 return set_->isnot ? ++next : next;
248 template <class BidiIterator>
251 repeater_count** stack;
252 repeater_count* next;
254 std::size_t count; // the number of iterations so far
255 BidiIterator start_pos; // where the last repeat started
257 repeater_count* unwind_until(int n, repeater_count* p, int current_recursion_id)
259 while(p && (p->state_id != n))
261 if(-2 - current_recursion_id == p->state_id)
264 if(p && (p->state_id < 0))
266 p = unwind_until(p->state_id, p, current_recursion_id);
275 repeater_count(repeater_count** s) : stack(s), next(0), state_id(-1), count(0), start_pos() {}
277 repeater_count(int i, repeater_count** s, BidiIterator start, int current_recursion_id)
284 if((state_id > next->state_id) && (next->state_id >= 0))
288 repeater_count* p = next;
289 p = unwind_until(state_id, p, current_recursion_id);
293 start_pos = p->start_pos;
304 std::size_t get_count() { return count; }
305 int get_id() { return state_id; }
306 std::size_t operator++() { return ++count; }
307 bool check_null_repeat(const BidiIterator& pos, std::size_t max)
309 // this is called when we are about to start a new repeat,
310 // if the last one was NULL move our count to max,
311 // otherwise save the current position.
312 bool result = (count == 0) ? false : (pos == start_pos);
323 enum saved_state_type
326 saved_type_paren = 1,
327 saved_type_recurse = 2,
328 saved_type_assertion = 3,
330 saved_state_repeater_count = 5,
331 saved_state_extra_block = 6,
332 saved_state_greedy_single_repeat = 7,
333 saved_state_rep_slow_dot = 8,
334 saved_state_rep_fast_dot = 9,
335 saved_state_rep_char = 10,
336 saved_state_rep_short_set = 11,
337 saved_state_rep_long_set = 12,
338 saved_state_non_greedy_long_repeat = 13,
339 saved_state_count = 14
342 template <class Results>
343 struct recursion_info
345 typedef typename Results::value_type value_type;
346 typedef typename value_type::iterator iterator;
348 const re_syntax_base* preturn_address;
350 repeater_count<iterator>* repeater_stack;
351 iterator location_of_start;
355 #pragma warning(push)
356 #pragma warning(disable : 4251 4231)
357 # if BOOST_MSVC < 1600
358 # pragma warning(disable : 4660)
362 template <class BidiIterator, class Allocator, class traits>
366 typedef typename traits::char_type char_type;
367 typedef perl_matcher<BidiIterator, Allocator, traits> self_type;
368 typedef bool (self_type::*matcher_proc_type)(void);
369 typedef std::size_t traits_size_type;
370 typedef typename is_byte<char_type>::width_type width_type;
371 typedef typename regex_iterator_traits<BidiIterator>::difference_type difference_type;
372 typedef match_results<BidiIterator, Allocator> results_type;
374 perl_matcher(BidiIterator first, BidiIterator end,
375 match_results<BidiIterator, Allocator>& what,
376 const basic_regex<char_type, traits>& e,
379 : m_result(what), base(first), last(end),
380 position(first), backstop(l_base), re(e), traits_inst(e.get_traits()),
381 m_independent(false), next_count(&rep_obj), rep_obj(&next_count)
382 #ifdef BOOST_REGEX_NON_RECURSIVE
386 construct_init(e, f);
392 void setf(match_flag_type f)
393 { m_match_flags |= f; }
394 void unsetf(match_flag_type f)
395 { m_match_flags &= ~f; }
398 void construct_init(const basic_regex<char_type, traits>& e, match_flag_type f);
402 #ifdef BOOST_REGEX_HAS_MS_STACK_GUARD
403 typedef bool (perl_matcher::*protected_proc_type)();
404 bool protected_call(protected_proc_type);
406 void estimate_max_state_count(std::random_access_iterator_tag*);
407 void estimate_max_state_count(void*);
409 bool match_all_states();
411 // match procs, stored in s_match_vtable:
412 bool match_startmark();
413 bool match_endmark();
414 bool match_literal();
415 bool match_start_line();
416 bool match_end_line();
419 bool match_word_boundary();
420 bool match_within_word();
421 bool match_word_start();
422 bool match_word_end();
423 bool match_buffer_start();
424 bool match_buffer_end();
425 bool match_backref();
426 bool match_long_set();
431 bool match_combining();
432 bool match_soft_buffer_end();
433 bool match_restart_continue();
434 bool match_long_set_repeat();
435 bool match_set_repeat();
436 bool match_char_repeat();
437 bool match_dot_repeat_fast();
438 bool match_dot_repeat_slow();
439 bool match_dot_repeat_dispatch()
441 return ::boost::is_random_access_iterator<BidiIterator>::value ? match_dot_repeat_fast() : match_dot_repeat_slow();
443 bool match_backstep();
444 bool match_assert_backref();
445 bool match_toggle_case();
446 #ifdef BOOST_REGEX_RECURSIVE
447 bool backtrack_till_match(std::size_t count);
449 bool match_recursion();
454 bool skip_until_paren(int index, bool match = true);
456 // find procs stored in s_find_vtable:
457 bool find_restart_any();
458 bool find_restart_word();
459 bool find_restart_line();
460 bool find_restart_buf();
461 bool find_restart_lit();
464 // final result structure to be filled in:
465 match_results<BidiIterator, Allocator>& m_result;
466 // temporary result for POSIX matches:
467 scoped_ptr<match_results<BidiIterator, Allocator> > m_temp_match;
468 // pointer to actual result structure to fill in:
469 match_results<BidiIterator, Allocator>* m_presult;
470 // start of sequence being searched:
472 // end of sequence being searched:
474 // current character being examined:
475 BidiIterator position;
476 // where to restart next search after failed match attempt:
477 BidiIterator restart;
478 // where the current search started from, acts as base for $` during grep:
479 BidiIterator search_base;
480 // how far we can go back when matching lookbehind:
481 BidiIterator backstop;
482 // the expression being examined:
483 const basic_regex<char_type, traits>& re;
484 // the expression's traits class:
485 const ::boost::regex_traits_wrapper<traits>& traits_inst;
486 // the next state in the machine being matched:
487 const re_syntax_base* pstate;
488 // matching flags in use:
489 match_flag_type m_match_flags;
490 // how many states we have examined so far:
491 std::ptrdiff_t state_count;
492 // max number of states to examine before giving up:
493 std::ptrdiff_t max_state_count;
494 // whether we should ignore case or not:
496 // set to true when (position == last), indicates that we may have a partial match:
497 bool m_has_partial_match;
498 // set to true whenever we get a match:
499 bool m_has_found_match;
500 // set to true whenever we're inside an independent sub-expression:
502 // the current repeat being examined:
503 repeater_count<BidiIterator>* next_count;
504 // the first repeat being examined (top of linked list):
505 repeater_count<BidiIterator> rep_obj;
506 // the mask to pass when matching word boundaries:
507 typename traits::char_class_type m_word_mask;
508 // the bitmask to use when determining whether a match_any matches a newline or not:
509 unsigned char match_any_mask;
510 // recursion information:
511 std::vector<recursion_info<results_type> > recursion_stack;
512 #ifdef BOOST_REGEX_RECURSIVE
513 // Set to false by a (*COMMIT):
514 bool m_can_backtrack;
518 #ifdef BOOST_REGEX_NON_RECURSIVE
520 // additional members for non-recursive version:
522 typedef bool (self_type::*unwind_proc_type)(bool);
526 bool unwind_end(bool);
527 bool unwind_paren(bool);
528 bool unwind_recursion_stopper(bool);
529 bool unwind_assertion(bool);
530 bool unwind_alt(bool);
531 bool unwind_repeater_counter(bool);
532 bool unwind_extra_block(bool);
533 bool unwind_greedy_single_repeat(bool);
534 bool unwind_slow_dot_repeat(bool);
535 bool unwind_fast_dot_repeat(bool);
536 bool unwind_char_repeat(bool);
537 bool unwind_short_set_repeat(bool);
538 bool unwind_long_set_repeat(bool);
539 bool unwind_non_greedy_repeat(bool);
540 bool unwind_recursion(bool);
541 bool unwind_recursion_pop(bool);
542 bool unwind_commit(bool);
543 bool unwind_then(bool);
544 bool unwind_case(bool);
545 void destroy_single_repeat();
546 void push_matched_paren(int index, const sub_match<BidiIterator>& sub);
547 void push_recursion_stopper();
548 void push_assertion(const re_syntax_base* ps, bool positive);
549 void push_alt(const re_syntax_base* ps);
550 void push_repeater_count(int i, repeater_count<BidiIterator>** s);
551 void push_single_repeat(std::size_t c, const re_repeat* r, BidiIterator last_position, int state_id);
552 void push_non_greedy_repeat(const re_syntax_base* ps);
553 void push_recursion(int idx, const re_syntax_base* p, results_type* presults, results_type* presults2);
554 void push_recursion_pop();
555 void push_case_change(bool);
557 // pointer to base of stack:
558 saved_state* m_stack_base;
559 // pointer to current stack position:
560 saved_state* m_backup_state;
561 // how many memory blocks have we used up?:
562 unsigned used_block_count;
563 // determines what value to return when unwinding from recursion,
564 // allows for mixed recursive/non-recursive algorithm:
565 bool m_recursive_result;
566 // We have unwound to a lookahead/lookbehind, used by COMMIT/PRUNE/SKIP:
567 bool m_unwound_lookahead;
568 // We have unwound to an alternative, used by THEN:
570 // We are unwinding a commit - used by independent subs to determine whether to stop there or carry on unwinding:
571 //bool m_unwind_commit;
573 unsigned m_recursions;
576 // these operations aren't allowed, so are declared private,
577 // bodies are provided to keep explicit-instantiation requests happy:
578 perl_matcher& operator=(const perl_matcher&)
582 perl_matcher(const perl_matcher& that)
583 : m_result(that.m_result), re(that.re), traits_inst(that.traits_inst), rep_obj(0) {}
590 } // namespace BOOST_REGEX_DETAIL_NS
593 #pragma warning(push)
594 #pragma warning(disable: 4103)
596 #ifdef BOOST_HAS_ABI_HEADERS
597 # include BOOST_ABI_SUFFIX
606 # pragma warning(pop)
610 // include the implementation of perl_matcher:
612 #ifdef BOOST_REGEX_RECURSIVE
613 #include <boost/regex/v4/perl_matcher_recursive.hpp>
615 #include <boost/regex/v4/perl_matcher_non_recursive.hpp>
617 // this one has to be last:
618 #include <boost/regex/v4/perl_matcher_common.hpp>