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)
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 #if !BOOST_WORKAROUND(BOOST_MSVC, < 1310)
90 template <class C, class T, class A>
91 inline int string_compare(const std::basic_string<C,T,A>& s, const C* p)
95 if(s.empty() || ((s.size() == 1) && (s[0] == 0)))
102 #if !BOOST_WORKAROUND(BOOST_MSVC, < 1310)
103 template <class C, class T, class A>
104 inline int string_compare(const std::basic_string<C,T,A>& s, const C* p)
108 if(s.empty() || ((s.size() == 1) && (s[0] == 0)))
114 inline int string_compare(const std::string& s, const char* p)
115 { return std::strcmp(s.c_str(), p); }
116 # ifndef BOOST_NO_WREGEX
117 inline int string_compare(const std::wstring& s, const wchar_t* p)
118 { return std::wcscmp(s.c_str(), p); }
121 template <class Seq, class C>
122 inline int string_compare(const Seq& s, const C* p)
125 while((i < s.size()) && (p[i] == s[i]))
129 return (i == s.size()) ? -p[i] : s[i] - p[i];
131 # define STR_COMP(s,p) string_compare(s,p)
133 template<class charT>
134 inline const charT* re_skip_past_null(const charT* p)
136 while (*p != static_cast<charT>(0)) ++p;
140 template <class iterator, class charT, class traits_type, class char_classT>
141 iterator BOOST_REGEX_CALL re_is_set_member(iterator next,
143 const re_set_long<char_classT>* set_,
144 const regex_data<charT, traits_type>& e, bool icase)
146 const charT* p = reinterpret_cast<const charT*>(set_+1);
149 //bool icase = e.m_flags & regex_constants::icase;
151 if(next == last) return next;
153 typedef typename traits_type::string_type traits_string_type;
154 const ::boost::regex_traits_wrapper<traits_type>& traits_inst = *(e.m_ptraits);
156 // dwa 9/13/00 suppress incorrect MSVC warning - it claims this is never
160 // try and match a single character, could be a multi-character
161 // collating element...
162 for(i = 0; i < set_->csingles; ++i)
165 if(*p == static_cast<charT>(0))
167 // treat null string as special case:
168 if(traits_inst.translate(*ptr, icase) != *p)
170 while(*p == static_cast<charT>(0))++p;
173 return set_->isnot ? next : (ptr == next) ? ++next : ptr;
177 while(*p && (ptr != last))
179 if(traits_inst.translate(*ptr, icase) != *p)
185 if(*p == static_cast<charT>(0)) // if null we've matched
186 return set_->isnot ? next : (ptr == next) ? ++next : ptr;
188 p = re_skip_past_null(p); // skip null
192 charT col = traits_inst.translate(*next, icase);
195 if(set_->cranges || set_->cequivalents)
197 traits_string_type s1;
199 // try and match a range, NB only a single character can match
202 if((e.m_flags & regex_constants::collate) == 0)
206 charT a[2] = { col, charT(0), };
207 s1 = traits_inst.transform(a, a + 1);
209 for(i = 0; i < set_->cranges; ++i)
211 if(STR_COMP(s1, p) >= 0)
215 if(STR_COMP(s1, p) <= 0)
216 return set_->isnot ? next : ++next;
224 // skip second string
230 // try and match an equivalence class, NB only a single character can match
231 if(set_->cequivalents)
233 charT a[2] = { col, charT(0), };
234 s1 = traits_inst.transform_primary(a, a +1);
235 for(i = 0; i < set_->cequivalents; ++i)
237 if(STR_COMP(s1, p) == 0)
238 return set_->isnot ? next : ++next;
245 if(traits_inst.isctype(col, set_->cclasses) == true)
246 return set_->isnot ? next : ++next;
247 if((set_->cnclasses != 0) && (traits_inst.isctype(col, set_->cnclasses) == false))
248 return set_->isnot ? next : ++next;
249 return set_->isnot ? ++next : next;
252 template <class BidiIterator>
255 repeater_count** stack;
256 repeater_count* next;
258 std::size_t count; // the number of iterations so far
259 BidiIterator start_pos; // where the last repeat started
261 repeater_count(repeater_count** s)
268 repeater_count(int i, repeater_count** s, BidiIterator start)
275 if(state_id > next->state_id)
279 repeater_count* p = next;
280 while(p && (p->state_id != state_id))
285 start_pos = p->start_pos;
296 std::size_t get_count() { return count; }
297 int get_id() { return state_id; }
298 std::size_t operator++() { return ++count; }
299 bool check_null_repeat(const BidiIterator& pos, std::size_t max)
301 // this is called when we are about to start a new repeat,
302 // if the last one was NULL move our count to max,
303 // otherwise save the current position.
304 bool result = (count == 0) ? false : (pos == start_pos);
315 enum saved_state_type
318 saved_type_paren = 1,
319 saved_type_recurse = 2,
320 saved_type_assertion = 3,
322 saved_state_repeater_count = 5,
323 saved_state_extra_block = 6,
324 saved_state_greedy_single_repeat = 7,
325 saved_state_rep_slow_dot = 8,
326 saved_state_rep_fast_dot = 9,
327 saved_state_rep_char = 10,
328 saved_state_rep_short_set = 11,
329 saved_state_rep_long_set = 12,
330 saved_state_non_greedy_long_repeat = 13,
331 saved_state_count = 14
334 template <class Results>
335 struct recursion_info
337 typedef typename Results::value_type value_type;
338 typedef typename value_type::iterator iterator;
340 const re_syntax_base* preturn_address;
342 repeater_count<iterator>* repeater_stack;
346 #pragma warning(push)
347 #pragma warning(disable : 4251 4231 4660)
350 template <class BidiIterator, class Allocator, class traits>
354 typedef typename traits::char_type char_type;
355 typedef perl_matcher<BidiIterator, Allocator, traits> self_type;
356 typedef bool (self_type::*matcher_proc_type)(void);
357 typedef std::size_t traits_size_type;
358 typedef typename is_byte<char_type>::width_type width_type;
359 typedef typename regex_iterator_traits<BidiIterator>::difference_type difference_type;
360 typedef match_results<BidiIterator, Allocator> results_type;
362 perl_matcher(BidiIterator first, BidiIterator end,
363 match_results<BidiIterator, Allocator>& what,
364 const basic_regex<char_type, traits>& e,
367 : m_result(what), base(first), last(end),
368 position(first), backstop(l_base), re(e), traits_inst(e.get_traits()),
369 m_independent(false), next_count(&rep_obj), rep_obj(&next_count), recursion_stack_position(0)
371 construct_init(e, f);
377 void setf(match_flag_type f)
378 { m_match_flags |= f; }
379 void unsetf(match_flag_type f)
380 { m_match_flags &= ~f; }
383 void construct_init(const basic_regex<char_type, traits>& e, match_flag_type f);
387 #ifdef BOOST_REGEX_HAS_MS_STACK_GUARD
388 typedef bool (perl_matcher::*protected_proc_type)();
389 bool protected_call(protected_proc_type);
391 void estimate_max_state_count(std::random_access_iterator_tag*);
392 void estimate_max_state_count(void*);
394 bool match_all_states();
396 // match procs, stored in s_match_vtable:
397 bool match_startmark();
398 bool match_endmark();
399 bool match_literal();
400 bool match_start_line();
401 bool match_end_line();
404 bool match_word_boundary();
405 bool match_within_word();
406 bool match_word_start();
407 bool match_word_end();
408 bool match_buffer_start();
409 bool match_buffer_end();
410 bool match_backref();
411 bool match_long_set();
416 bool match_combining();
417 bool match_soft_buffer_end();
418 bool match_restart_continue();
419 bool match_long_set_repeat();
420 bool match_set_repeat();
421 bool match_char_repeat();
422 bool match_dot_repeat_fast();
423 bool match_dot_repeat_slow();
424 bool match_dot_repeat_dispatch()
426 return ::boost::is_random_access_iterator<BidiIterator>::value ? match_dot_repeat_fast() : match_dot_repeat_slow();
428 bool match_backstep();
429 bool match_assert_backref();
430 bool match_toggle_case();
431 #ifdef BOOST_REGEX_RECURSIVE
432 bool backtrack_till_match(std::size_t count);
434 bool match_recursion();
436 // find procs stored in s_find_vtable:
437 bool find_restart_any();
438 bool find_restart_word();
439 bool find_restart_line();
440 bool find_restart_buf();
441 bool find_restart_lit();
444 // final result structure to be filled in:
445 match_results<BidiIterator, Allocator>& m_result;
446 // temporary result for POSIX matches:
447 scoped_ptr<match_results<BidiIterator, Allocator> > m_temp_match;
448 // pointer to actual result structure to fill in:
449 match_results<BidiIterator, Allocator>* m_presult;
450 // start of sequence being searched:
452 // end of sequence being searched:
454 // current character being examined:
455 BidiIterator position;
456 // where to restart next search after failed match attempt:
457 BidiIterator restart;
458 // where the current search started from, acts as base for $` during grep:
459 BidiIterator search_base;
460 // how far we can go back when matching lookbehind:
461 BidiIterator backstop;
462 // the expression being examined:
463 const basic_regex<char_type, traits>& re;
464 // the expression's traits class:
465 const ::boost::regex_traits_wrapper<traits>& traits_inst;
466 // the next state in the machine being matched:
467 const re_syntax_base* pstate;
468 // matching flags in use:
469 match_flag_type m_match_flags;
470 // how many states we have examined so far:
471 boost::uintmax_t state_count;
472 // max number of states to examine before giving up:
473 boost::uintmax_t max_state_count;
474 // whether we should ignore case or not:
476 // set to true when (position == last), indicates that we may have a partial match:
477 bool m_has_partial_match;
478 // set to true whenever we get a match:
479 bool m_has_found_match;
480 // set to true whenever we're inside an independent sub-expression:
482 // the current repeat being examined:
483 repeater_count<BidiIterator>* next_count;
484 // the first repeat being examined (top of linked list):
485 repeater_count<BidiIterator> rep_obj;
486 // the mask to pass when matching word boundaries:
487 typename traits::char_class_type m_word_mask;
488 // the bitmask to use when determining whether a match_any matches a newline or not:
489 unsigned char match_any_mask;
490 // recursion information:
491 recursion_info<results_type> recursion_stack[50];
492 unsigned recursion_stack_position;
494 #ifdef BOOST_REGEX_NON_RECURSIVE
496 // additional members for non-recursive version:
498 typedef bool (self_type::*unwind_proc_type)(bool);
502 bool unwind_end(bool);
503 bool unwind_paren(bool);
504 bool unwind_recursion_stopper(bool);
505 bool unwind_assertion(bool);
506 bool unwind_alt(bool);
507 bool unwind_repeater_counter(bool);
508 bool unwind_extra_block(bool);
509 bool unwind_greedy_single_repeat(bool);
510 bool unwind_slow_dot_repeat(bool);
511 bool unwind_fast_dot_repeat(bool);
512 bool unwind_char_repeat(bool);
513 bool unwind_short_set_repeat(bool);
514 bool unwind_long_set_repeat(bool);
515 bool unwind_non_greedy_repeat(bool);
516 bool unwind_recursion(bool);
517 bool unwind_recursion_pop(bool);
518 void destroy_single_repeat();
519 void push_matched_paren(int index, const sub_match<BidiIterator>& sub);
520 void push_recursion_stopper();
521 void push_assertion(const re_syntax_base* ps, bool positive);
522 void push_alt(const re_syntax_base* ps);
523 void push_repeater_count(int i, repeater_count<BidiIterator>** s);
524 void push_single_repeat(std::size_t c, const re_repeat* r, BidiIterator last_position, int state_id);
525 void push_non_greedy_repeat(const re_syntax_base* ps);
526 void push_recursion(int id, const re_syntax_base* p, results_type* presults);
527 void push_recursion_pop();
529 // pointer to base of stack:
530 saved_state* m_stack_base;
531 // pointer to current stack position:
532 saved_state* m_backup_state;
533 // determines what value to return when unwinding from recursion,
534 // allows for mixed recursive/non-recursive algorithm:
535 bool m_recursive_result;
536 // how many memory blocks have we used up?:
537 unsigned used_block_count;
540 // these operations aren't allowed, so are declared private,
541 // bodies are provided to keep explicit-instantiation requests happy:
542 perl_matcher& operator=(const perl_matcher&)
546 perl_matcher(const perl_matcher& that)
547 : m_result(that.m_result), re(that.re), traits_inst(that.traits_inst), rep_obj(0) {}
554 } // namespace re_detail
557 #pragma warning(push)
558 #pragma warning(disable: 4103)
560 #ifdef BOOST_HAS_ABI_HEADERS
561 # include BOOST_ABI_SUFFIX
570 # pragma warning(pop)
574 // include the implementation of perl_matcher:
576 #ifdef BOOST_REGEX_RECURSIVE
577 #include <boost/regex/v4/perl_matcher_recursive.hpp>
579 #include <boost/regex/v4/perl_matcher_non_recursive.hpp>
581 // this one has to be last:
582 #include <boost/regex/v4/perl_matcher_common.hpp>