3 * Copyright (c) 1998-2002
6 * Permission to use, copy, modify, distribute and sell this software
7 * and its documentation for any purpose is hereby granted without fee,
8 * provided that the above copyright notice appear in all copies and
9 * that both that copyright notice and this permission notice appear
10 * in supporting documentation. Dr John Maddock makes no representations
11 * about the suitability of this software for any purpose.
12 * It is provided "as is" without express or implied warranty.
17 * LOCATION: see http://www.boost.org for most recent version.
19 * VERSION see <boost/version.hpp>
20 * DESCRIPTION: Declares boost::reg_expression<> and associated
21 * functions and classes. This header is the main
22 * entry point for the template regex code.
26 /* start with C compatibility API */
28 #ifndef BOOST_RE_REGEX_HPP
29 #define BOOST_RE_REGEX_HPP
31 #include <boost/cregex.hpp>
35 // what follows is all C++ don't include in C builds!!
37 #ifdef BOOST_REGEX_DEBUG
42 #include <boost/regex/config.hpp>
44 #include <boost/regex_fwd.hpp>
45 #include <boost/regex/detail/regex_stack.hpp>
46 #include <boost/regex/detail/regex_raw_buffer.hpp>
47 #include <boost/regex/detail/regex_kmp.hpp>
48 #include <boost/regex/pattern_except.hpp>
49 #include <boost/regex/regex_traits.hpp>
50 #include <boost/type_traits/cv_traits.hpp>
51 #include <boost/scoped_array.hpp>
57 #pragma option push -a8 -b -Vx -Ve -pc -w-8027
63 struct re_syntax_base;
65 } // namespace re_detail
69 // class char_regex_traits_i
70 // provides case insensitive traits classes (deprecated):
71 template <class charT>
72 class char_regex_traits_i : public regex_traits<charT> {};
75 class char_regex_traits_i<char> : public regex_traits<char>
78 typedef char char_type;
79 typedef unsigned char uchar_type;
80 typedef unsigned int size_type;
81 typedef regex_traits<char> base_type;
83 char BOOST_REGEX_CALL translate(char c, bool)const
85 return static_cast<const regex_traits<char>*>(this)->translate(c, true);
89 #ifndef BOOST_NO_WREGEX
91 class char_regex_traits_i<wchar_t> : public regex_traits<wchar_t>
94 typedef wchar_t char_type;
95 typedef unsigned short uchar_type;
96 typedef unsigned int size_type;
97 typedef regex_traits<wchar_t> base_type;
99 wchar_t BOOST_REGEX_CALL translate(wchar_t c, bool)const
101 return static_cast<const regex_traits<wchar_t>*>(this)->translate(c, true);
103 boost::uint_fast32_t BOOST_REGEX_CALL lookup_classname(const wchar_t* first, const wchar_t* last)const
105 boost::uint_fast32_t result = static_cast<const regex_traits<wchar_t>*>(this)->lookup_classname(first, last);
106 if((result & base_type::char_class_upper) == base_type::char_class_upper)
107 result |= base_type::char_class_alpha;
112 } // namespace deprecated
121 mask_any = mask_skip | mask_take,
125 struct _narrow_type{};
128 template <class charT>
135 typedef _narrow_type width_type;
139 class is_byte<unsigned char>
142 typedef _narrow_type width_type;
146 class is_byte<signed char>
149 typedef _narrow_type width_type;
152 template <class charT>
156 typedef _wide_type width_type;
161 // compiled structures
163 // the following defs describe the format of the compiled string
167 // enum syntax_element_type
168 // describes the type of a record
169 enum syntax_element_type
171 syntax_element_startmark = 0,
172 syntax_element_endmark = syntax_element_startmark + 1,
173 syntax_element_literal = syntax_element_endmark + 1,
174 syntax_element_start_line = syntax_element_literal + 1,
175 syntax_element_end_line = syntax_element_start_line + 1,
176 syntax_element_wild = syntax_element_end_line + 1,
177 syntax_element_match = syntax_element_wild + 1,
178 syntax_element_word_boundary = syntax_element_match + 1,
179 syntax_element_within_word = syntax_element_word_boundary + 1,
180 syntax_element_word_start = syntax_element_within_word + 1,
181 syntax_element_word_end = syntax_element_word_start + 1,
182 syntax_element_buffer_start = syntax_element_word_end + 1,
183 syntax_element_buffer_end = syntax_element_buffer_start + 1,
184 syntax_element_backref = syntax_element_buffer_end + 1,
185 syntax_element_long_set = syntax_element_backref + 1,
186 syntax_element_set = syntax_element_long_set + 1,
187 syntax_element_jump = syntax_element_set + 1,
188 syntax_element_alt = syntax_element_jump + 1,
189 syntax_element_rep = syntax_element_alt + 1,
190 syntax_element_combining = syntax_element_rep + 1,
191 syntax_element_soft_buffer_end = syntax_element_combining + 1,
192 syntax_element_restart_continue = syntax_element_soft_buffer_end + 1
195 #ifdef BOOST_REGEX_DEBUG
196 // dwa 09/26/00 - This is needed to suppress warnings about an ambiguous conversion
197 std::ostream& operator<<(std::ostream&, syntax_element_type);
207 // struct re_syntax_base
208 // base class for all syntax types:
209 struct re_syntax_base
211 syntax_element_type type;
213 unsigned int can_be_null;
218 // marks start or end of (...)
219 struct re_brace : public re_syntax_base
226 // marks a literal string and
227 // is followed by an array of charT[length]:
228 struct re_literal : public re_syntax_base
234 // struct re_long_set
235 // provides data for sets [...] containing
237 struct re_set_long : public re_syntax_base
239 unsigned int csingles, cranges, cequivalents;
240 boost::uint_fast32_t cclasses;
246 // provides a map of bools for sets containing
247 // narrow, single byte characters.
248 struct re_set : public re_syntax_base
250 unsigned char _map[256];
255 // provides alternative next destination
256 struct re_jump : public re_syntax_base
259 unsigned char _map[256];
264 // provides repeat expressions
265 struct re_repeat : public re_jump
276 // enum re_jump_size_type
277 // provides compiled size of re_jump
278 // allowing for trailing alignment
279 // provide this so we know how many
281 enum re_jump_size_type
283 re_jump_size = (sizeof(re_jump) + padding_mask) & ~(padding_mask),
284 re_repeater_size = (sizeof(re_repeat) + padding_mask) & ~(padding_mask)
287 } // namespace re_detail
291 // handles error codes and flags
293 class BOOST_REGEX_DECL regbase
298 escape_in_lists = 1, // '\' special inside [...]
299 char_classes = escape_in_lists << 1, // [[:CLASS:]] allowed
300 intervals = char_classes << 1, // {x,y} allowed
301 limited_ops = intervals << 1, // all of + ? and | are normal characters
302 newline_alt = limited_ops << 1, // \n is the same as |
303 bk_plus_qm = newline_alt << 1, // uses \+ and \?
304 bk_braces = bk_plus_qm << 1, // uses \{ and \}
305 bk_parens = bk_braces << 1, // uses \( and \)
306 bk_refs = bk_parens << 1, // \d allowed
307 bk_vbar = bk_refs << 1, // uses \|
309 use_except = bk_vbar << 1, // exception on error
310 failbit = use_except << 1, // error flag
311 literal = failbit << 1, // all characters are literals
312 icase = literal << 1, // characters are matched regardless of case
313 nocollate = icase << 1, // don't use locale specific collation
315 basic = char_classes | intervals | limited_ops | bk_braces | bk_parens | bk_refs,
316 extended = char_classes | intervals | bk_refs,
317 normal = escape_in_lists | char_classes | intervals | bk_refs | nocollate,
318 emacs = bk_braces | bk_parens | bk_refs | bk_vbar,
319 awk = extended | escape_in_lists,
320 grep = basic | newline_alt,
321 egrep = extended | newline_alt,
325 typedef unsigned int flag_type;
333 restart_continue = 4,
335 restart_fixed_lit = 6
338 flag_type BOOST_REGEX_CALL flags()const
344 regbase(const regbase& b);
350 // some forward declarations:
352 template <class iterator, class Allocator>
353 class _priv_match_data;
355 #if defined(BOOST_NO_STD_ITERATOR_TRAITS) || defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
358 struct regex_iterator_traits
360 typedef typename T::iterator_category iterator_category;
361 typedef typename T::value_type value_type;
362 #if !defined(BOOST_NO_STD_ITERATOR)
363 typedef typename T::difference_type difference_type;
364 typedef typename T::pointer pointer;
365 typedef typename T::reference reference;
367 typedef std::ptrdiff_t difference_type;
368 typedef value_type* pointer;
369 typedef value_type& reference;
374 struct pointer_iterator_traits
376 typedef std::ptrdiff_t difference_type;
377 typedef T value_type;
379 typedef T& reference;
380 typedef std::random_access_iterator_tag iterator_category;
383 struct const_pointer_iterator_traits
385 typedef std::ptrdiff_t difference_type;
386 typedef T value_type;
387 typedef const T* pointer;
388 typedef const T& reference;
389 typedef std::random_access_iterator_tag iterator_category;
393 struct regex_iterator_traits<char*> : pointer_iterator_traits<char>{};
395 struct regex_iterator_traits<const char*> : const_pointer_iterator_traits<char>{};
397 struct regex_iterator_traits<wchar_t*> : pointer_iterator_traits<wchar_t>{};
399 struct regex_iterator_traits<const wchar_t*> : const_pointer_iterator_traits<wchar_t>{};
401 #if defined(__SGI_STL_PORT) && defined(__STL_DEBUG)
403 struct regex_iterator_traits<std::string::iterator> : pointer_iterator_traits<char>{};
405 struct regex_iterator_traits<std::string::const_iterator> : const_pointer_iterator_traits<char>{};
406 #ifndef BOOST_NO_STD_WSTRING
408 struct regex_iterator_traits<std::wstring::iterator> : pointer_iterator_traits<wchar_t>{};
410 struct regex_iterator_traits<std::wstring::const_iterator> : const_pointer_iterator_traits<wchar_t>{};
411 #endif // BOOST_NO_WSTRING
417 struct regex_iterator_traits : public std::iterator_traits<T> {};
422 struct def_alloc_param_traits
424 typedef typename regex_iterator_traits<I>::value_type const_value_type;
425 typedef typename remove_cv<const_value_type>::type type;
428 struct def_alloc_param_traits<const char*>
433 struct def_alloc_param_traits<const wchar_t*>
435 typedef wchar_t type;
440 template <class iterator, class Allocator =
441 #if !(defined(BOOST_MSVC) && (BOOST_MSVC <= 1300))
442 BOOST_DEFAULT_ALLOCATOR(typename re_detail::def_alloc_param_traits<iterator>::type) >
444 BOOST_DEFAULT_ALLOCATOR(re_detail::def_alloc_param_traits<iterator>::type) >
449 // class reg_expression
450 // represents the compiled
451 // regular expression:
454 #ifdef BOOST_REGEX_NO_FWD
455 template <class charT, class traits = regex_traits<charT>, class Allocator = BOOST_DEFAULT_ALLOCATOR(charT) >
457 template <class charT, class traits, class Allocator >
459 class reg_expression : public regbase
462 typedef typename traits::size_type traits_size_type;
463 typedef typename traits::uchar_type traits_uchar_type;
464 typedef typename traits::string_type traits_string_type;
466 typedef charT char_type;
467 typedef traits traits_type;
470 // placeholder for actual locale type used by the
471 // traits class to localise *this.
472 typedef typename traits::locale_type locale_type;
474 typedef charT value_type;
475 // reference, const_reference
476 typedef charT& reference;
477 typedef const charT& const_reference;
478 // iterator, const_iterator
479 typedef const charT* const_iterator;
480 typedef const_iterator iterator;
482 typedef typename Allocator::difference_type difference_type;
484 typedef typename Allocator::size_type size_type;
486 typedef Allocator allocator_type;
487 typedef Allocator alloc_type;
489 typedef regbase::flag_type flag_type;
492 explicit reg_expression(const Allocator& a = Allocator());
493 explicit reg_expression(const charT* p, flag_type f = regbase::normal, const Allocator& a = Allocator());
494 reg_expression(const charT* p1, const charT* p2, flag_type f = regbase::normal, const Allocator& a = Allocator());
495 reg_expression(const charT* p, size_type len, flag_type f, const Allocator& a = Allocator());
496 reg_expression(const reg_expression&);
498 reg_expression& BOOST_REGEX_CALL operator=(const reg_expression&);
499 reg_expression& BOOST_REGEX_CALL operator=(const charT* ptr)
501 set_expression(ptr, regbase::normal | regbase::use_except);
507 reg_expression& assign(const reg_expression& that)
508 { return *this = that; }
509 reg_expression& assign(const charT* ptr, flag_type f = regbase::normal)
511 set_expression(ptr, f | regbase::use_except);
515 reg_expression& assign(const charT* first,
517 flag_type f = regbase::normal)
519 set_expression(first, last, f | regbase::use_except);
522 #ifndef BOOST_NO_MEMBER_TEMPLATES
524 template <class ST, class SA>
525 unsigned int BOOST_REGEX_CALL set_expression(const std::basic_string<charT, ST, SA>& p, flag_type f = regbase::normal)
526 { return set_expression(p.data(), p.data() + p.size(), f); }
528 template <class ST, class SA>
529 explicit reg_expression(const std::basic_string<charT, ST, SA>& p, flag_type f = regbase::normal, const Allocator& a = Allocator())
530 : data(a), pkmp(0), error_code_(REG_EMPTY), _expression(0) { set_expression(p, f | regbase::use_except); }
533 reg_expression(I first, I last, flag_type f = regbase::normal, const Allocator& al = Allocator())
534 : data(al), pkmp(0), error_code_(REG_EMPTY), _expression(0)
536 size_type len = last-first;
537 scoped_array<charT> a(new charT[len]);
538 std::copy(first, last, a.get());
539 set_expression(a.get(), a.get() + len, f | regbase::use_except);
542 template <class ST, class SA>
543 reg_expression& BOOST_REGEX_CALL operator=(const std::basic_string<charT, ST, SA>& p)
545 set_expression(p.c_str(), p.c_str() + p.size(), regbase::normal | regbase::use_except);
549 template <class string_traits, class A>
550 reg_expression& BOOST_REGEX_CALL assign(
551 const std::basic_string<charT, string_traits, A>& s,
552 flag_type f = regbase::normal)
554 set_expression(s.c_str(), s.c_str() + s.size(), f | regbase::use_except);
558 template <class fwd_iterator>
559 reg_expression& BOOST_REGEX_CALL assign(fwd_iterator first,
561 flag_type f = regbase::normal)
563 size_type len = last-first;
564 scoped_array<charT> a(new charT[len]);
565 std::copy(first, last, a.get());
566 set_expression(a.get(), a.get() + len, f | regbase::use_except);
570 unsigned int BOOST_REGEX_CALL set_expression(const std::basic_string<charT>& p, flag_type f = regbase::normal)
571 { return set_expression(p.data(), p.data() + p.size(), f | regbase::use_except); }
573 reg_expression(const std::basic_string<charT>& p, flag_type f = regbase::normal, const Allocator& a = Allocator())
574 : data(a), pkmp(0) { set_expression(p, f | regbase::use_except); }
576 reg_expression& BOOST_REGEX_CALL operator=(const std::basic_string<charT>& p)
578 set_expression(p.c_str(), p.c_str() + p.size(), regbase::normal | regbase::use_except);
582 reg_expression& BOOST_REGEX_CALL assign(
583 const std::basic_string<charT>& s,
584 flag_type f = regbase::normal)
586 set_expression(s.c_str(), s.c_str() + s.size(), f | regbase::use_except);
595 Allocator BOOST_REGEX_CALL get_allocator()const;
598 locale_type BOOST_REGEX_CALL imbue(locale_type l){ return traits_inst.imbue(l); }
599 locale_type BOOST_REGEX_CALL getloc()const{ return traits_inst.getloc(); }
602 flag_type BOOST_REGEX_CALL getflags()const
606 std::basic_string<charT> BOOST_REGEX_CALL str()const
608 std::basic_string<charT> result;
609 if(this->error_code() == 0)
610 result = std::basic_string<charT>(_expression, _expression_len);
615 const_iterator BOOST_REGEX_CALL begin()const
616 { return (this->error_code() ? 0 : _expression); }
617 const_iterator BOOST_REGEX_CALL end()const
618 { return (this->error_code() ? 0 : _expression + _expression_len); }
621 void BOOST_REGEX_CALL swap(reg_expression&)throw();
624 size_type BOOST_REGEX_CALL size()const
625 { return (this->error_code() ? 0 : _expression_len); }
628 size_type BOOST_REGEX_CALL max_size()const
632 bool BOOST_REGEX_CALL empty()const
633 { return 0 != this->error_code(); }
635 unsigned BOOST_REGEX_CALL mark_count()const { return (this->error_code() ? 0 : marks); }
636 bool BOOST_REGEX_CALL operator==(const reg_expression&)const;
637 bool BOOST_REGEX_CALL operator<(const reg_expression&)const;
639 // The following are deprecated as public interfaces
640 // but are available for compatibility with earlier versions.
641 allocator_type BOOST_REGEX_CALL allocator()const;
642 const charT* BOOST_REGEX_CALL expression()const { return (this->error_code() ? 0 : _expression); }
643 unsigned int BOOST_REGEX_CALL set_expression(const charT* p, const charT* end, flag_type f = regbase::normal);
644 unsigned int BOOST_REGEX_CALL set_expression(const charT* p, flag_type f = regbase::normal) { return set_expression(p, p + traits_type::length(p), f); }
646 // this should be private but template friends don't work:
647 const traits_type& get_traits()const { return traits_inst; }
648 unsigned int BOOST_REGEX_CALL error_code()const
654 traits_type traits_inst;
655 re_detail::raw_storage<Allocator> data;
656 unsigned _restart_type;
659 unsigned char* startmap;
660 std::size_t _expression_len;
661 std::size_t _leading_len;
662 const charT* _leading_string;
663 std::size_t _leading_string_len;
664 re_detail::kmp_info<charT>* pkmp;
665 unsigned error_code_;
668 void BOOST_REGEX_CALL compile_maps();
669 void BOOST_REGEX_CALL compile_map(re_detail::re_syntax_base* node, unsigned char* _map, unsigned int* pnull, unsigned char mask, re_detail::re_syntax_base* terminal = 0)const;
670 bool BOOST_REGEX_CALL probe_start(re_detail::re_syntax_base* node, charT c, re_detail::re_syntax_base* terminal)const;
671 bool BOOST_REGEX_CALL probe_start_null(re_detail::re_syntax_base* node, re_detail::re_syntax_base* terminal)const;
672 void BOOST_REGEX_CALL fixup_apply(re_detail::re_syntax_base* b, unsigned cbraces);
673 void BOOST_REGEX_CALL move_offsets(re_detail::re_syntax_base* j, unsigned size);
674 re_detail::re_syntax_base* BOOST_REGEX_CALL compile_set(const charT*& first, const charT* last);
675 re_detail::re_syntax_base* BOOST_REGEX_CALL compile_set_aux(re_detail::jstack<traits_string_type, Allocator>& singles, re_detail::jstack<traits_string_type, Allocator>& ranges, re_detail::jstack<boost::uint_fast32_t, Allocator>& classes, re_detail::jstack<traits_string_type, Allocator>& equivalents, bool isnot, const re_detail::_narrow_type&);
676 re_detail::re_syntax_base* BOOST_REGEX_CALL compile_set_aux(re_detail::jstack<traits_string_type, Allocator>& singles, re_detail::jstack<traits_string_type, Allocator>& ranges, re_detail::jstack<boost::uint_fast32_t, Allocator>& classes, re_detail::jstack<traits_string_type, Allocator>& equivalents, bool isnot, const re_detail::_wide_type&);
677 re_detail::re_syntax_base* BOOST_REGEX_CALL compile_set_simple(re_detail::re_syntax_base* dat, unsigned long cls, bool isnot = false);
678 unsigned int BOOST_REGEX_CALL parse_inner_set(const charT*& first, const charT* last);
680 re_detail::re_syntax_base* BOOST_REGEX_CALL add_simple(re_detail::re_syntax_base* dat, re_detail::syntax_element_type type, unsigned int size = sizeof(re_detail::re_syntax_base));
681 re_detail::re_syntax_base* BOOST_REGEX_CALL add_literal(re_detail::re_syntax_base* dat, charT c);
682 charT BOOST_REGEX_CALL parse_escape(const charT*& first, const charT* last);
683 void BOOST_REGEX_CALL parse_range(const charT*& first, const charT* last, unsigned& min, unsigned& max);
684 bool BOOST_REGEX_CALL skip_space(const charT*& first, const charT* last);
685 unsigned int BOOST_REGEX_CALL probe_restart(re_detail::re_syntax_base* dat);
686 unsigned int BOOST_REGEX_CALL fixup_leading_rep(re_detail::re_syntax_base* dat, re_detail::re_syntax_base* end);
687 void BOOST_REGEX_CALL fail(unsigned int err);
690 static int BOOST_REGEX_CALL repeat_count(const reg_expression& e)
691 { return e.repeats; }
692 static unsigned int BOOST_REGEX_CALL restart_type(const reg_expression& e)
693 { return e._restart_type; }
694 static const re_detail::re_syntax_base* BOOST_REGEX_CALL first(const reg_expression& e)
695 { return (const re_detail::re_syntax_base*)e.data.data(); }
696 static const unsigned char* BOOST_REGEX_CALL get_map(const reg_expression& e)
697 { return e.startmap; }
698 static std::size_t BOOST_REGEX_CALL leading_length(const reg_expression& e)
699 { return e._leading_len; }
700 static const re_detail::kmp_info<charT>* get_kmp(const reg_expression& e)
702 static bool BOOST_REGEX_CALL can_start(charT c, const unsigned char* _map, unsigned char mask, const re_detail::_wide_type&);
703 static bool BOOST_REGEX_CALL can_start(charT c, const unsigned char* _map, unsigned char mask, const re_detail::_narrow_type&);
706 template <class charT, class traits, class Allocator>
707 inline void BOOST_REGEX_CALL reg_expression<charT, traits, Allocator>::swap(reg_expression& that)throw()
709 // this is not as efficient as it should be,
710 // however swapping traits classes is problematic
711 // so just use 'brute force' method for now:
712 reg_expression<charT, traits, Allocator> e(that);
719 // class match_results and match_results_base
720 // handles what matched where
722 template <class iterator>
725 typedef typename re_detail::regex_iterator_traits<iterator>::value_type value_type;
726 #if defined(BOOST_NO_STD_ITERATOR_TRAITS) || defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
727 typedef std::ptrdiff_t difference_type;
729 typedef typename re_detail::regex_iterator_traits<iterator>::difference_type difference_type;
731 typedef iterator iterator_type;
737 operator std::basic_string<value_type> ()const
739 std::basic_string<value_type> result;
740 std::size_t len = boost::re_detail::distance((iterator)first, (iterator)second);
745 result.append(1, *i);
750 #ifdef BOOST_OLD_REGEX_H
752 // the following are deprecated, do not use!!
755 operator unsigned int()const;
756 operator short()const
758 return (short)(int)(*this);
760 operator unsigned short()const
762 return (unsigned short)(unsigned int)(*this);
765 sub_match() { matched = false; }
766 sub_match(iterator i) : first(i), second(i), matched(false) {}
768 bool operator==(const sub_match& that)const
770 return (first == that.first) && (second == that.second) && (matched == that.matched);
772 bool BOOST_REGEX_CALL operator !=(const sub_match& that)const
773 { return !(*this == that); }
775 difference_type BOOST_REGEX_CALL length()const
777 difference_type n = boost::re_detail::distance((iterator)first, (iterator)second);
782 #ifdef BOOST_OLD_REGEX_H
784 template <class iterator, class charT>
785 int do_toi(iterator i, iterator j, char c, int radix)
789 int result = std::strtol(s.c_str(), &p, radix);
790 #ifndef BOOST_NO_EXCEPTIONS
791 if(*p)throw bad_pattern("Bad sub-expression");
793 BOOST_REGEX_NOEH_ASSERT(0 == *p)
799 template <class I, class charT>
800 int do_toi(I& i, I j, charT c)
803 while((i != j) && (isdigit(*i)))
805 result = result*10 + (*i - '0');
813 template <class iterator>
814 sub_match<iterator>::operator int()const
818 #ifndef BOOST_NO_EXCEPTIONS
819 if(i == j)throw bad_pattern("Bad sub-expression");
821 BOOST_REGEX_NOEH_ASSERT(i != j)
823 if((i != j) && (*i == '-'))
828 neg *= re_detail::do_toi(i, j, *i);
829 #ifndef BOOST_NO_EXCEPTIONS
830 if(i != j)throw bad_pattern("Bad sub-expression");
832 BOOST_REGEX_NOEH_ASSERT(i == j)
835 template <class iterator>
836 sub_match<iterator>::operator unsigned int()const
840 #ifndef BOOST_NO_EXCEPTIONS
842 throw bad_pattern("Bad sub-expression");
844 BOOST_REGEX_NOEH_ASSERT(i != j)
845 return re_detail::do_toi(i, j, *first);
851 template <class iterator, class Allocator = BOOST_DEFAULT_ALLOCATOR(typename def_alloc_param_traits<iterator>::type) >
852 class match_results_base
855 typedef Allocator alloc_type;
856 typedef typename boost::detail::rebind_allocator<iterator, Allocator>::type iterator_alloc;
857 typedef typename iterator_alloc::size_type size_type;
858 #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(BOOST_NO_STD_ITERATOR_TRAITS)
859 typedef typename std::iterator_traits<iterator>::difference_type difference_type;
860 typedef typename std::iterator_traits<iterator>::value_type char_type;
862 typedef std::ptrdiff_t difference_type;
863 typedef typename re_detail::regex_iterator_traits<iterator>::value_type char_type;
865 typedef sub_match<iterator> value_type;
866 typedef iterator iterator_type;
869 typedef typename boost::detail::rebind_allocator<char, Allocator>::type c_alloc;
871 struct c_reference : public c_alloc
873 std::size_t cmatches;
875 sub_match<iterator> head, tail, null;
877 iterator line_pos, base;
878 c_reference(const Allocator& a)
879 : c_alloc(a), cmatches(0), count(0), lines(0) { }
881 bool operator==(const c_reference& that)const
883 return (cmatches == that.cmatches) &&
884 (count == that.count) &&
885 (head == that.head) &&
886 (tail == that.tail) &&
887 (lines == that.lines) &&
890 bool operator!=(const c_reference& that)const
891 { return !(*this == that); }
896 void BOOST_REGEX_CALL cow();
898 // protected contructor for derived class...
899 match_results_base(bool){}
900 void BOOST_REGEX_CALL m_free();
904 match_results_base(const Allocator& a = Allocator());
906 match_results_base(const match_results_base& m)
912 match_results_base& BOOST_REGEX_CALL operator=(const match_results_base& m);
914 ~match_results_base()
919 size_type BOOST_REGEX_CALL size()const
921 //return (*this)[0].matched ? ref->cmatches : 0;
922 return ref->cmatches;
925 const sub_match<iterator>& BOOST_REGEX_CALL operator[](int n) const
927 if((n >= 0) && ((unsigned int)n < ref->cmatches))
928 return *(sub_match<iterator>*)((char*)ref + sizeof(c_reference) + sizeof(sub_match<iterator>)*n);
929 return (n == -1) ? ref->head : (n == -2) ? ref->tail : ref->null;
932 Allocator BOOST_REGEX_CALL allocator()const;
934 difference_type BOOST_REGEX_CALL length(unsigned int sub = 0)const
936 jm_assert(ref->cmatches);
937 const sub_match<iterator>& m = (*this)[sub];
938 if(m.matched == false)
940 difference_type n = boost::re_detail::distance((iterator)m.first, (iterator)m.second);
944 std::basic_string<char_type> str(int i)const
946 return static_cast<std::basic_string<char_type> >((*this)[i]);
949 unsigned int BOOST_REGEX_CALL line()const
954 difference_type BOOST_REGEX_CALL position(unsigned int sub = 0)const
956 jm_assert(ref->cmatches);
957 const sub_match<iterator>& s = (*this)[sub];
958 if(s.matched == false)
960 difference_type n = boost::re_detail::distance((iterator)(ref->base), (iterator)(s.first));
964 iterator BOOST_REGEX_CALL line_start()const
966 return ref->line_pos;
969 void swap(match_results_base& that)
971 c_reference* t = that.ref;
976 bool operator==(const match_results_base& that)const;
977 bool operator<(const match_results_base& that)const
978 { return position() < that.position(); }
980 friend class match_results<iterator, Allocator>;
982 void BOOST_REGEX_CALL set_size(size_type n);
983 void BOOST_REGEX_CALL set_size(size_type n, iterator i, iterator j);
984 void BOOST_REGEX_CALL maybe_assign(const match_results_base& m);
985 void BOOST_REGEX_CALL init_fail(iterator i, iterator j);
987 void BOOST_REGEX_CALL set_first(iterator i);
988 void BOOST_REGEX_CALL set_first(iterator i, std::size_t pos);
990 void BOOST_REGEX_CALL set_second(iterator i)
993 ((sub_match<iterator>*)(ref+1))->second = i;
994 ((sub_match<iterator>*)(ref+1))->matched = true;
996 ref->tail.matched = (ref->tail.first == ref->tail.second) ? false : true;
999 void BOOST_REGEX_CALL set_second(iterator i, std::size_t pos, bool m = true)
1002 ((sub_match<iterator>*)((char*)ref + sizeof(c_reference) + sizeof(sub_match<iterator>) * pos))->second = i;
1003 ((sub_match<iterator>*)((char*)ref + sizeof(c_reference) + sizeof(sub_match<iterator>) * pos))->matched = m;
1006 ref->tail.first = i;
1007 ref->tail.matched = (ref->tail.first == ref->tail.second) ? false : true;
1011 void BOOST_REGEX_CALL set_line(unsigned int i, iterator pos)
1014 ref->line_pos = pos;
1017 void BOOST_REGEX_CALL set_base(iterator pos)
1023 template <class iterator, class Allocator>
1024 void BOOST_REGEX_CALL match_results_base<iterator, Allocator>::set_first(iterator i)
1027 ref->head.second = i;
1028 ref->head.matched = (ref->head.first == ref->head.second) ? false : true;
1029 sub_match<iterator>* p1 = (sub_match<iterator>*)(ref+1);
1030 sub_match<iterator>* p2 = p1 + ref->cmatches;
1032 p1->matched = false;
1036 p1->matched = false;
1037 p1->first = ref->tail.second;
1038 p1->second = ref->tail.second;
1043 template <class iterator, class Allocator>
1044 void BOOST_REGEX_CALL match_results_base<iterator, Allocator>::set_first(iterator i, std::size_t pos)
1047 ((sub_match<iterator>*)((char*)ref + sizeof(c_reference) + sizeof(sub_match<iterator>) * pos))->first = i;
1050 ref->head.second = i;
1051 ref->head.matched = (ref->head.first == ref->head.second) ? false : true;
1052 sub_match<iterator>* p1 = (sub_match<iterator>*)(ref+1);
1053 sub_match<iterator>* p2 = p1 + ref->cmatches;
1055 p1->matched = false;
1059 p1->matched = false;
1060 p1->first = ref->tail.second;
1061 p1->second = ref->tail.second;
1068 template <class iterator, class Allocator>
1069 match_results_base<iterator, Allocator>::match_results_base(const Allocator& a)
1071 ref = (c_reference*)c_alloc(a).allocate(sizeof(sub_match<iterator>) + sizeof(c_reference));
1072 BOOST_REGEX_NOEH_ASSERT(ref)
1073 #ifndef BOOST_NO_EXCEPTIONS
1077 new (ref) c_reference(a);
1080 // construct the sub_match<iterator>:
1081 #ifndef BOOST_NO_EXCEPTIONS
1085 new ((sub_match<iterator>*)(ref+1)) sub_match<iterator>();
1086 #ifndef BOOST_NO_EXCEPTIONS
1090 ::boost::re_detail::pointer_destroy(ref);
1096 c_alloc(a).deallocate((char*)(void*)ref, sizeof(sub_match<iterator>) + sizeof(c_reference));
1102 template <class iterator, class Allocator>
1103 Allocator BOOST_REGEX_CALL match_results_base<iterator, Allocator>::allocator()const
1105 return *((c_alloc*)ref);
1108 template <class iterator, class Allocator>
1109 inline match_results_base<iterator, Allocator>& BOOST_REGEX_CALL match_results_base<iterator, Allocator>::operator=(const match_results_base<iterator, Allocator>& m)
1121 template <class iterator, class Allocator>
1122 void BOOST_REGEX_CALL match_results_base<iterator, Allocator>::m_free()
1124 if(--(ref->count) == 0)
1127 sub_match<iterator>* p1, *p2;
1128 p1 = (sub_match<iterator>*)(ref+1);
1129 p2 = p1 + ref->cmatches;
1132 ::boost::re_detail::pointer_destroy(p1);
1135 ::boost::re_detail::pointer_destroy(ref);
1136 a.deallocate((char*)(void*)ref, sizeof(sub_match<iterator>) * ref->cmatches + sizeof(c_reference));
1140 template <class iterator, class Allocator>
1141 bool match_results_base<iterator, Allocator>::operator==(const match_results_base<iterator, Allocator>& that)const
1143 if(*ref != *(that.ref))
1145 const sub_match<iterator>* p1 = (sub_match<iterator>*)(ref+1);
1146 const sub_match<iterator>* p2 = p1 + ref->cmatches;
1147 const sub_match<iterator>* p3 = (sub_match<iterator>*)(that.ref+1);
1158 template <class iterator, class Allocator>
1159 void BOOST_REGEX_CALL match_results_base<iterator, Allocator>::set_size(size_type n)
1161 if(ref->cmatches != n)
1163 c_reference* newref = (c_reference*)ref->allocate(sizeof(sub_match<iterator>) * n + sizeof(c_reference));
1164 BOOST_REGEX_NOEH_ASSERT(newref)
1165 #ifndef BOOST_NO_EXCEPTIONS
1169 new (newref) c_reference(*ref);
1171 newref->cmatches = n;
1172 sub_match<iterator>* p1, *p2;
1173 p1 = (sub_match<iterator>*)(newref+1);
1174 p2 = p1 + newref->cmatches;
1175 #ifndef BOOST_NO_EXCEPTIONS
1181 new (p1) sub_match<iterator>();
1185 #ifndef BOOST_NO_EXCEPTIONS
1189 p2 = (sub_match<iterator>*)(newref+1);
1192 ::boost::re_detail::pointer_destroy(p2);
1195 ::boost::re_detail::pointer_destroy(ref);
1200 #ifndef BOOST_NO_EXCEPTIONS
1204 ref->deallocate((char*)(void*)newref, sizeof(sub_match<iterator>) * n + sizeof(c_reference));
1211 template <class iterator, class Allocator>
1212 void BOOST_REGEX_CALL match_results_base<iterator, Allocator>::set_size(size_type n, iterator i, iterator j)
1214 if(ref->cmatches != n)
1216 c_reference* newref = (c_reference*)ref->allocate(sizeof(sub_match<iterator>) * n + sizeof(c_reference));
1217 BOOST_REGEX_NOEH_ASSERT(newref)
1218 #ifndef BOOST_NO_EXCEPTIONS
1221 new (newref) c_reference(*ref);
1223 newref->cmatches = n;
1224 sub_match<iterator>* p1 = (sub_match<iterator>*)(newref+1);
1225 sub_match<iterator>* p2 = p1 + newref->cmatches;
1226 #ifndef BOOST_NO_EXCEPTIONS
1232 new (p1) sub_match<iterator>(j);
1236 #ifndef BOOST_NO_EXCEPTIONS
1240 p2 = (sub_match<iterator>*)(newref+1);
1243 ::boost::re_detail::pointer_destroy(p2);
1246 ::boost::re_detail::pointer_destroy(ref);
1251 #ifndef BOOST_NO_EXCEPTIONS
1255 ref->deallocate((char*)(void*)newref, sizeof(sub_match<iterator>) * n + sizeof(c_reference));
1263 // set iterators to be i, matched to false:
1264 sub_match<iterator>* p1, *p2;
1265 p1 = (sub_match<iterator>*)(ref+1);
1266 p2 = p1 + ref->cmatches;
1271 p1->matched = false;
1275 ref->head.first = i;
1276 ref->tail.second = j;
1277 ref->head.matched = ref->tail.matched = true;
1278 ref->null.first = ref->null.second = j;
1279 ref->null.matched = false;
1282 template <class iterator, class Allocator>
1283 inline void BOOST_REGEX_CALL match_results_base<iterator, Allocator>::init_fail(iterator i, iterator j)
1285 set_size(ref->cmatches, i, j);
1288 template <class iterator, class Allocator>
1289 void BOOST_REGEX_CALL match_results_base<iterator, Allocator>::maybe_assign(const match_results_base<iterator, Allocator>& m)
1291 sub_match<iterator>* p1, *p2;
1292 p1 = (sub_match<iterator>*)(ref+1);
1293 p2 = (sub_match<iterator>*)(m.ref+1);
1294 iterator base = (*this)[-1].first;
1295 std::size_t len1 = 0;
1296 std::size_t len2 = 0;
1297 std::size_t base1 = 0;
1298 std::size_t base2 = 0;
1300 for(i = 0; i < ref->cmatches; ++i)
1303 // leftmost takes priority over longest:
1304 base1 = boost::re_detail::distance(base, p1->first);
1305 base2 = boost::re_detail::distance(base, p2->first);
1306 if(base1 < base2) return;
1307 if(base2 < base1) break;
1309 len1 = boost::re_detail::distance(p1->first, p1->second);
1310 len2 = boost::re_detail::distance(p2->first, p2->second);
1311 if((len1 != len2) || ((p1->matched == false) && (p2->matched == true)))
1313 if((p1->matched == true) && (p2->matched == false))
1318 if(i == ref->cmatches)
1322 else if((len2 > len1) || ((p1->matched == false) && (p2->matched == true)) )
1326 template <class iterator, class Allocator>
1327 void BOOST_REGEX_CALL match_results_base<iterator, Allocator>::cow()
1331 c_reference* newref = (c_reference*)ref->allocate(sizeof(sub_match<iterator>) * ref->cmatches + sizeof(c_reference));
1332 BOOST_REGEX_NOEH_ASSERT(newref)
1333 #ifndef BOOST_NO_EXCEPTIONS
1336 new (newref) c_reference(*ref);
1338 sub_match<iterator>* p1 = (sub_match<iterator>*)(newref+1);
1339 sub_match<iterator>* p2 = p1 + newref->cmatches;
1340 sub_match<iterator>* p3 = (sub_match<iterator>*)(ref+1);
1341 #ifndef BOOST_NO_EXCEPTIONS
1346 new (p1) sub_match<iterator>(*p3);
1350 #ifndef BOOST_NO_EXCEPTIONS
1354 p2 = (sub_match<iterator>*)(newref+1);
1357 ::boost::re_detail::pointer_destroy(p2);
1360 ::boost::re_detail::pointer_destroy(ref);
1366 #ifndef BOOST_NO_EXCEPTIONS
1370 ref->deallocate((char*)(void*)newref, sizeof(sub_match<iterator>) * ref->cmatches + sizeof(c_reference));
1377 } // namespace re_detail
1380 // class match_results
1381 // encapsulates match_results_base, does a deep copy rather than
1382 // reference counting to ensure thread safety when copying
1383 // other match_results instances
1385 template <class iterator, class Allocator>
1386 class match_results : public re_detail::match_results_base<iterator, Allocator>
1388 typedef re_detail::match_results_base<iterator, Allocator> base_type;
1391 typedef typename base_type::alloc_type alloc_type;
1392 typedef typename base_type::size_type size_type;
1393 typedef typename base_type::char_type char_type;
1394 typedef typename base_type::value_type value_type;
1395 typedef typename base_type::difference_type difference_type;
1396 typedef typename base_type::iterator_type iterator_type;
1398 explicit match_results(const Allocator& a = Allocator())
1399 : re_detail::match_results_base<iterator, Allocator>(a){}
1401 match_results(const re_detail::match_results_base<iterator, Allocator>& m)
1402 : re_detail::match_results_base<iterator, Allocator>(m){}
1404 match_results& operator=(const re_detail::match_results_base<iterator, Allocator>& m)
1407 base_type::operator=(m);
1411 match_results(const match_results& m);
1412 match_results& operator=(const match_results& m);
1414 // the following function definitions should *not* be required, except
1415 // when this class is used as a template inside another template definition,
1416 // in which members of the base class are not visible to the calling code.
1417 // As a workaround we define simple forwarding functions:
1419 size_type size()const
1420 { return static_cast<const base_type*>(this)->size(); }
1422 const sub_match<iterator>& operator[](int n) const
1423 { return (*static_cast<const base_type*>(this))[n]; }
1425 Allocator allocator()const
1426 { return static_cast<const base_type*>(this)->allocator(); }
1428 difference_type length(int sub = 0)const
1429 { return static_cast<const base_type*>(this)->length(sub); }
1431 difference_type position(unsigned int sub = 0)const
1432 { return static_cast<const base_type*>(this)->position(sub); }
1434 unsigned int line()const
1435 { return static_cast<const base_type*>(this)->line(); }
1437 iterator line_start()const
1438 { return static_cast<const base_type*>(this)->line_start(); }
1440 std::basic_string<char_type> str(int sub = 0)const
1441 { return static_cast<const base_type*>(this)->str(sub); }
1443 void swap(match_results& that)
1444 { static_cast<base_type*>(this)->swap(that); }
1446 bool operator==(const match_results& that)const
1447 { return static_cast<const base_type&>(*this) == static_cast<const base_type&>(that); }
1449 bool operator<(const match_results& that) const
1450 { return position() < that.position(); }
1453 template <class iterator, class Allocator>
1454 match_results<iterator, Allocator>::match_results(const match_results<iterator, Allocator>& m)
1455 : re_detail::match_results_base<iterator, Allocator>(false)
1458 reinterpret_cast<typename re_detail::match_results_base<iterator, Allocator>::c_reference *>
1459 (m.ref->allocate(sizeof(sub_match<iterator>) * m.ref->cmatches +
1460 sizeof(typename re_detail::match_results_base<iterator, Allocator>::c_reference)));
1461 BOOST_REGEX_NOEH_ASSERT(this->ref)
1462 #ifndef BOOST_NO_EXCEPTIONS
1465 new (this->ref) typename re_detail::match_results_base<iterator, Allocator>::c_reference(*m.ref);
1466 this->ref->count = 1;
1467 sub_match<iterator>* p1 = (sub_match<iterator>*)(this->ref+1);
1468 sub_match<iterator>* p2 = p1 + this->ref->cmatches;
1469 sub_match<iterator>* p3 = (sub_match<iterator>*)(m.ref+1);
1470 #ifndef BOOST_NO_EXCEPTIONS
1475 new (p1) sub_match<iterator>(*p3);
1479 #ifndef BOOST_NO_EXCEPTIONS
1483 p2 = (sub_match<iterator>*)(this->ref+1);
1486 re_detail::pointer_destroy(p2);
1489 re_detail::pointer_destroy(this->ref);
1495 m.ref->deallocate((char*)(void*)this->ref, sizeof(sub_match<iterator>) * m.ref->cmatches + sizeof(typename re_detail::match_results_base<iterator, Allocator>::c_reference));
1501 template <class iterator, class Allocator>
1502 match_results<iterator, Allocator>& match_results<iterator, Allocator>::operator=(const match_results<iterator, Allocator>& m)
1504 match_results<iterator, Allocator> t(m);
1509 namespace re_detail{
1510 template <class iterator, class charT, class traits_type, class Allocator>
1511 iterator BOOST_REGEX_CALL re_is_set_member(iterator next,
1513 const re_set_long* set_,
1514 const reg_expression<charT, traits_type, Allocator>& e);
1515 } // namepsace re_detail
1521 } // namespace boost
1523 #include <boost/regex/detail/regex_compile.hpp>
1526 // template instances:
1528 #define BOOST_REGEX_CHAR_T char
1529 #ifdef BOOST_REGEX_NARROW_INSTANTIATE
1530 # define BOOST_REGEX_INSTANTIATE
1532 #include <boost/regex/detail/instances.hpp>
1533 #undef BOOST_REGEX_CHAR_T
1534 #ifdef BOOST_REGEX_INSTANTIATE
1535 # undef BOOST_REGEX_INSTANTIATE
1538 #ifndef BOOST_NO_WREGEX
1539 #define BOOST_REGEX_CHAR_T wchar_t
1540 #ifdef BOOST_REGEX_WIDE_INSTANTIATE
1541 # define BOOST_REGEX_INSTANTIATE
1543 #include <boost/regex/detail/instances.hpp>
1544 #undef BOOST_REGEX_CHAR_T
1545 #ifdef BOOST_REGEX_INSTANTIATE
1546 # undef BOOST_REGEX_INSTANTIATE
1552 #ifdef BOOST_REGEX_NO_FWD
1553 typedef reg_expression<char, regex_traits<char>, BOOST_DEFAULT_ALLOCATOR(char)> regex;
1554 #ifndef BOOST_NO_WREGEX
1555 typedef reg_expression<wchar_t, regex_traits<wchar_t>, BOOST_DEFAULT_ALLOCATOR(wchar_t)> wregex;
1559 typedef match_results<const char*> cmatch;
1560 typedef match_results<std::string::const_iterator> smatch;
1561 #ifndef BOOST_NO_WREGEX
1562 typedef match_results<const wchar_t*> wcmatch;
1563 typedef match_results<std::wstring::const_iterator> wsmatch;
1566 } // namespace boost
1567 #include <boost/regex/detail/regex_match.hpp>
1568 #include <boost/regex/detail/regex_format.hpp>
1569 #include <boost/regex/detail/regex_split.hpp>
1572 #endif // __cplusplus