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)
31 #pragma warning(disable:4800)
36 namespace BOOST_REGEX_DETAIL_NS{
39 // error checking API:
41 BOOST_REGEX_DECL void BOOST_REGEX_CALL verify_options(boost::regex_constants::syntax_option_type ef, match_flag_type mf);
43 // function can_start:
45 template <class charT>
46 inline bool can_start(charT c, const unsigned char* map, unsigned char mask)
48 return ((c < static_cast<charT>(0)) ? true : ((c >= static_cast<charT>(1 << CHAR_BIT)) ? true : map[c] & mask));
50 inline bool can_start(char c, const unsigned char* map, unsigned char mask)
52 return map[(unsigned char)c] & mask;
54 inline bool can_start(signed char c, const unsigned char* map, unsigned char mask)
56 return map[(unsigned char)c] & mask;
58 inline bool can_start(unsigned char c, const unsigned char* map, unsigned char mask)
62 inline bool can_start(unsigned short c, const unsigned char* map, unsigned char mask)
64 return ((c >= (1 << CHAR_BIT)) ? true : map[c] & mask);
66 #if !defined(__hpux) && !defined(__WINSCW__)// WCHAR_MIN not usable in pp-directives.
67 #if defined(WCHAR_MIN) && (WCHAR_MIN == 0) && !defined(BOOST_NO_INTRINSIC_WCHAR_T)
68 inline bool can_start(wchar_t c, const unsigned char* map, unsigned char mask)
70 return ((c >= static_cast<wchar_t>(1u << CHAR_BIT)) ? true : map[c] & mask);
74 #if !defined(BOOST_NO_INTRINSIC_WCHAR_T)
75 inline bool can_start(unsigned int c, const unsigned char* map, unsigned char mask)
77 return (((c >= static_cast<unsigned int>(1u << CHAR_BIT)) ? true : map[c] & mask));
83 // Unfortunately Rogue Waves standard library appears to have a bug
84 // in std::basic_string::compare that results in eroneous answers
85 // in some cases (tested with Borland C++ 5.1, Rogue Wave lib version
86 // 0x020101) the test case was:
87 // {39135,0} < {0xff,0}
88 // which succeeds when it should not.
91 template <class C, class T, class A>
92 inline int string_compare(const std::basic_string<C,T,A>& s, const C* p)
96 if(s.empty() || ((s.size() == 1) && (s[0] == 0)))
102 template <class C, class T, class A>
103 inline int string_compare(const std::basic_string<C,T,A>& s, const C* p)
107 if(s.empty() || ((s.size() == 1) && (s[0] == 0)))
112 inline int string_compare(const std::string& s, const char* p)
113 { return std::strcmp(s.c_str(), p); }
114 # ifndef BOOST_NO_WREGEX
115 inline int string_compare(const std::wstring& s, const wchar_t* p)
116 { return std::wcscmp(s.c_str(), p); }
119 template <class Seq, class C>
120 inline int string_compare(const Seq& s, const C* p)
123 while((i < s.size()) && (p[i] == s[i]))
127 return (i == s.size()) ? -(int)p[i] : (int)s[i] - (int)p[i];
129 # define STR_COMP(s,p) string_compare(s,p)
131 template<class charT>
132 inline const charT* re_skip_past_null(const charT* p)
134 while (*p != static_cast<charT>(0)) ++p;
138 template <class iterator, class charT, class traits_type, class char_classT>
139 iterator BOOST_REGEX_CALL re_is_set_member(iterator next,
141 const re_set_long<char_classT>* set_,
142 const regex_data<charT, traits_type>& e, bool icase)
144 const charT* p = reinterpret_cast<const charT*>(set_+1);
147 //bool icase = e.m_flags & regex_constants::icase;
149 if(next == last) return next;
151 typedef typename traits_type::string_type traits_string_type;
152 const ::boost::regex_traits_wrapper<traits_type>& traits_inst = *(e.m_ptraits);
154 // dwa 9/13/00 suppress incorrect MSVC warning - it claims this is never
158 // try and match a single character, could be a multi-character
159 // collating element...
160 for(i = 0; i < set_->csingles; ++i)
163 if(*p == static_cast<charT>(0))
165 // treat null string as special case:
166 if(traits_inst.translate(*ptr, icase))
171 return set_->isnot ? next : (ptr == next) ? ++next : ptr;
175 while(*p && (ptr != last))
177 if(traits_inst.translate(*ptr, icase) != *p)
183 if(*p == static_cast<charT>(0)) // if null we've matched
184 return set_->isnot ? next : (ptr == next) ? ++next : ptr;
186 p = re_skip_past_null(p); // skip null
190 charT col = traits_inst.translate(*next, icase);
193 if(set_->cranges || set_->cequivalents)
195 traits_string_type s1;
197 // try and match a range, NB only a single character can match
200 if((e.m_flags & regex_constants::collate) == 0)
204 charT a[2] = { col, charT(0), };
205 s1 = traits_inst.transform(a, a + 1);
207 for(i = 0; i < set_->cranges; ++i)
209 if(STR_COMP(s1, p) >= 0)
213 if(STR_COMP(s1, p) <= 0)
214 return set_->isnot ? next : ++next;
222 // skip second string
228 // try and match an equivalence class, NB only a single character can match
229 if(set_->cequivalents)
231 charT a[2] = { col, charT(0), };
232 s1 = traits_inst.transform_primary(a, a +1);
233 for(i = 0; i < set_->cequivalents; ++i)
235 if(STR_COMP(s1, p) == 0)
236 return set_->isnot ? next : ++next;
243 if(traits_inst.isctype(col, set_->cclasses) == true)
244 return set_->isnot ? next : ++next;
245 if((set_->cnclasses != 0) && (traits_inst.isctype(col, set_->cnclasses) == false))
246 return set_->isnot ? next : ++next;
247 return set_->isnot ? ++next : next;
250 template <class BidiIterator>
253 repeater_count** stack;
254 repeater_count* next;
256 std::size_t count; // the number of iterations so far
257 BidiIterator start_pos; // where the last repeat started
259 repeater_count* unwind_until(int n, repeater_count* p, int current_recursion_id)
261 while(p && (p->state_id != n))
263 if(-2 - current_recursion_id == p->state_id)
266 if(p && (p->state_id < 0))
268 p = unwind_until(p->state_id, p, current_recursion_id);
277 repeater_count(repeater_count** s) : stack(s), next(0), state_id(-1), count(0), start_pos() {}
279 repeater_count(int i, repeater_count** s, BidiIterator start, int current_recursion_id)
286 if((state_id > next->state_id) && (next->state_id >= 0))
290 repeater_count* p = next;
291 p = unwind_until(state_id, p, current_recursion_id);
295 start_pos = p->start_pos;
306 std::size_t get_count() { return count; }
307 int get_id() { return state_id; }
308 std::size_t operator++() { return ++count; }
309 bool check_null_repeat(const BidiIterator& pos, std::size_t max)
311 // this is called when we are about to start a new repeat,
312 // if the last one was NULL move our count to max,
313 // otherwise save the current position.
314 bool result = (count == 0) ? false : (pos == start_pos);
325 enum saved_state_type
328 saved_type_paren = 1,
329 saved_type_recurse = 2,
330 saved_type_assertion = 3,
332 saved_state_repeater_count = 5,
333 saved_state_extra_block = 6,
334 saved_state_greedy_single_repeat = 7,
335 saved_state_rep_slow_dot = 8,
336 saved_state_rep_fast_dot = 9,
337 saved_state_rep_char = 10,
338 saved_state_rep_short_set = 11,
339 saved_state_rep_long_set = 12,
340 saved_state_non_greedy_long_repeat = 13,
341 saved_state_count = 14
344 template <class Results>
345 struct recursion_info
347 typedef typename Results::value_type value_type;
348 typedef typename value_type::iterator iterator;
350 const re_syntax_base* preturn_address;
352 repeater_count<iterator>* repeater_stack;
353 iterator location_of_start;
357 #pragma warning(push)
358 #pragma warning(disable : 4251)
359 #if BOOST_MSVC < 1700
360 # pragma warning(disable : 4231)
362 # if BOOST_MSVC < 1600
363 # pragma warning(disable : 4660)
367 template <class BidiIterator, class Allocator, class traits>
371 typedef typename traits::char_type char_type;
372 typedef perl_matcher<BidiIterator, Allocator, traits> self_type;
373 typedef bool (self_type::*matcher_proc_type)(void);
374 typedef std::size_t traits_size_type;
375 typedef typename is_byte<char_type>::width_type width_type;
376 typedef typename regex_iterator_traits<BidiIterator>::difference_type difference_type;
377 typedef match_results<BidiIterator, Allocator> results_type;
379 perl_matcher(BidiIterator first, BidiIterator end,
380 match_results<BidiIterator, Allocator>& what,
381 const basic_regex<char_type, traits>& e,
384 : m_result(what), base(first), last(end),
385 position(first), backstop(l_base), re(e), traits_inst(e.get_traits()),
386 m_independent(false), next_count(&rep_obj), rep_obj(&next_count)
387 #ifdef BOOST_REGEX_NON_RECURSIVE
391 construct_init(e, f);
397 void setf(match_flag_type f)
398 { m_match_flags |= f; }
399 void unsetf(match_flag_type f)
400 { m_match_flags &= ~f; }
403 void construct_init(const basic_regex<char_type, traits>& e, match_flag_type f);
407 #ifdef BOOST_REGEX_HAS_MS_STACK_GUARD
408 typedef bool (perl_matcher::*protected_proc_type)();
409 bool protected_call(protected_proc_type);
411 void estimate_max_state_count(std::random_access_iterator_tag*);
412 void estimate_max_state_count(void*);
414 bool match_all_states();
416 // match procs, stored in s_match_vtable:
417 bool match_startmark();
418 bool match_endmark();
419 bool match_literal();
420 bool match_start_line();
421 bool match_end_line();
424 bool match_word_boundary();
425 bool match_within_word();
426 bool match_word_start();
427 bool match_word_end();
428 bool match_buffer_start();
429 bool match_buffer_end();
430 bool match_backref();
431 bool match_long_set();
436 bool match_combining();
437 bool match_soft_buffer_end();
438 bool match_restart_continue();
439 bool match_long_set_repeat();
440 bool match_set_repeat();
441 bool match_char_repeat();
442 bool match_dot_repeat_fast();
443 bool match_dot_repeat_slow();
444 bool match_dot_repeat_dispatch()
446 return ::boost::is_random_access_iterator<BidiIterator>::value ? match_dot_repeat_fast() : match_dot_repeat_slow();
448 bool match_backstep();
449 bool match_assert_backref();
450 bool match_toggle_case();
451 #ifdef BOOST_REGEX_RECURSIVE
452 bool backtrack_till_match(std::size_t count);
454 bool match_recursion();
459 bool skip_until_paren(int index, bool match = true);
461 // find procs stored in s_find_vtable:
462 bool find_restart_any();
463 bool find_restart_word();
464 bool find_restart_line();
465 bool find_restart_buf();
466 bool find_restart_lit();
469 // final result structure to be filled in:
470 match_results<BidiIterator, Allocator>& m_result;
471 // temporary result for POSIX matches:
472 scoped_ptr<match_results<BidiIterator, Allocator> > m_temp_match;
473 // pointer to actual result structure to fill in:
474 match_results<BidiIterator, Allocator>* m_presult;
475 // start of sequence being searched:
477 // end of sequence being searched:
479 // current character being examined:
480 BidiIterator position;
481 // where to restart next search after failed match attempt:
482 BidiIterator restart;
483 // where the current search started from, acts as base for $` during grep:
484 BidiIterator search_base;
485 // how far we can go back when matching lookbehind:
486 BidiIterator backstop;
487 // the expression being examined:
488 const basic_regex<char_type, traits>& re;
489 // the expression's traits class:
490 const ::boost::regex_traits_wrapper<traits>& traits_inst;
491 // the next state in the machine being matched:
492 const re_syntax_base* pstate;
493 // matching flags in use:
494 match_flag_type m_match_flags;
495 // how many states we have examined so far:
496 std::ptrdiff_t state_count;
497 // max number of states to examine before giving up:
498 std::ptrdiff_t max_state_count;
499 // whether we should ignore case or not:
501 // set to true when (position == last), indicates that we may have a partial match:
502 bool m_has_partial_match;
503 // set to true whenever we get a match:
504 bool m_has_found_match;
505 // set to true whenever we're inside an independent sub-expression:
507 // the current repeat being examined:
508 repeater_count<BidiIterator>* next_count;
509 // the first repeat being examined (top of linked list):
510 repeater_count<BidiIterator> rep_obj;
511 // the mask to pass when matching word boundaries:
512 typename traits::char_class_type m_word_mask;
513 // the bitmask to use when determining whether a match_any matches a newline or not:
514 unsigned char match_any_mask;
515 // recursion information:
516 std::vector<recursion_info<results_type> > recursion_stack;
517 #ifdef BOOST_REGEX_RECURSIVE
518 // Set to false by a (*COMMIT):
519 bool m_can_backtrack;
523 #ifdef BOOST_REGEX_NON_RECURSIVE
525 // additional members for non-recursive version:
527 typedef bool (self_type::*unwind_proc_type)(bool);
531 bool unwind_end(bool);
532 bool unwind_paren(bool);
533 bool unwind_recursion_stopper(bool);
534 bool unwind_assertion(bool);
535 bool unwind_alt(bool);
536 bool unwind_repeater_counter(bool);
537 bool unwind_extra_block(bool);
538 bool unwind_greedy_single_repeat(bool);
539 bool unwind_slow_dot_repeat(bool);
540 bool unwind_fast_dot_repeat(bool);
541 bool unwind_char_repeat(bool);
542 bool unwind_short_set_repeat(bool);
543 bool unwind_long_set_repeat(bool);
544 bool unwind_non_greedy_repeat(bool);
545 bool unwind_recursion(bool);
546 bool unwind_recursion_pop(bool);
547 bool unwind_commit(bool);
548 bool unwind_then(bool);
549 bool unwind_case(bool);
550 void destroy_single_repeat();
551 void push_matched_paren(int index, const sub_match<BidiIterator>& sub);
552 void push_recursion_stopper();
553 void push_assertion(const re_syntax_base* ps, bool positive);
554 void push_alt(const re_syntax_base* ps);
555 void push_repeater_count(int i, repeater_count<BidiIterator>** s);
556 void push_single_repeat(std::size_t c, const re_repeat* r, BidiIterator last_position, int state_id);
557 void push_non_greedy_repeat(const re_syntax_base* ps);
558 void push_recursion(int idx, const re_syntax_base* p, results_type* presults, results_type* presults2);
559 void push_recursion_pop();
560 void push_case_change(bool);
562 // pointer to base of stack:
563 saved_state* m_stack_base;
564 // pointer to current stack position:
565 saved_state* m_backup_state;
566 // how many memory blocks have we used up?:
567 unsigned used_block_count;
568 // determines what value to return when unwinding from recursion,
569 // allows for mixed recursive/non-recursive algorithm:
570 bool m_recursive_result;
571 // We have unwound to a lookahead/lookbehind, used by COMMIT/PRUNE/SKIP:
572 bool m_unwound_lookahead;
573 // We have unwound to an alternative, used by THEN:
575 // We are unwinding a commit - used by independent subs to determine whether to stop there or carry on unwinding:
576 //bool m_unwind_commit;
578 unsigned m_recursions;
581 // these operations aren't allowed, so are declared private,
582 // bodies are provided to keep explicit-instantiation requests happy:
583 perl_matcher& operator=(const perl_matcher&)
587 perl_matcher(const perl_matcher& that)
588 : m_result(that.m_result), re(that.re), traits_inst(that.traits_inst), rep_obj(0) {}
595 } // namespace BOOST_REGEX_DETAIL_NS
598 #pragma warning(push)
599 #pragma warning(disable: 4103)
601 #ifdef BOOST_HAS_ABI_HEADERS
602 # include BOOST_ABI_SUFFIX
611 # pragma warning(pop)
615 // include the implementation of perl_matcher:
617 #ifdef BOOST_REGEX_RECURSIVE
618 #include <boost/regex/v4/perl_matcher_recursive.hpp>
620 #include <boost/regex/v4/perl_matcher_non_recursive.hpp>
622 // this one has to be last:
623 #include <boost/regex/v4/perl_matcher_common.hpp>