3 * Copyright (c) 1998-2000
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.
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 compatability 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!!
42 #include <boost/re_detail/regex_config.hpp>
43 #if !defined(BOOST_RE_NO_TYPEINFO)
47 #include <boost/re_detail/regex_stack.hpp>
48 #include <boost/re_detail/regex_raw_buffer.hpp>
49 #include <boost/re_detail/regex_kmp.hpp>
50 #include <boost/pattern_except.hpp>
51 #include <boost/regex_traits.hpp>
52 #include <boost/type_traits.hpp>
58 #if __BORLANDC__ == 0x530
59 #pragma option push -a4 -b -Ve
60 #elif __BORLANDC__ > 0x530
61 #pragma option push -a8 -b -Ve
68 struct re_syntax_base;
70 } // namespace re_detail
74 // class char_regex_traits_i
75 // provides case insensitive traits classes (deprecated):
76 template <class charT>
77 class char_regex_traits_i : public regex_traits<charT> {};
80 class char_regex_traits_i<char> : public regex_traits<char>
83 typedef char char_type;
84 typedef unsigned char uchar_type;
85 typedef unsigned int size_type;
86 typedef regex_traits<char> base_type;
88 char BOOST_RE_CALL translate(char c, bool)const
90 return static_cast<const regex_traits<char>*>(this)->translate(c, true);
94 #ifndef BOOST_RE_NO_WCSTRING
96 class char_regex_traits_i<wchar_t> : public regex_traits<wchar_t>
99 typedef wchar_t char_type;
100 typedef unsigned short uchar_type;
101 typedef unsigned int size_type;
102 typedef regex_traits<wchar_t> base_type;
104 wchar_t BOOST_RE_CALL translate(wchar_t c, bool)const
106 return static_cast<const regex_traits<wchar_t>*>(this)->translate(c, true);
108 jm_uintfast32_t BOOST_RE_CALL lookup_classname(const wchar_t* first, const wchar_t* last)const
110 jm_uintfast32_t result = static_cast<const regex_traits<wchar_t>*>(this)->lookup_classname(first, last);
111 if((result & base_type::char_class_upper) == base_type::char_class_upper)
112 result |= base_type::char_class_alpha;
117 } // namespace deprecated
126 mask_any = mask_skip | mask_take,
130 struct _narrow_type{};
133 template <class charT>
140 typedef _narrow_type width_type;
144 class is_byte<unsigned char>
147 typedef _narrow_type width_type;
151 class is_byte<signed char>
154 typedef _narrow_type width_type;
157 template <class charT>
161 typedef _wide_type width_type;
166 // compiled structures
168 // the following defs describe the format of the compiled string
172 // enum syntax_element_type
173 // describes the type of a record
174 enum syntax_element_type
176 syntax_element_startmark = 0,
177 syntax_element_endmark = syntax_element_startmark + 1,
178 syntax_element_literal = syntax_element_endmark + 1,
179 syntax_element_start_line = syntax_element_literal + 1,
180 syntax_element_end_line = syntax_element_start_line + 1,
181 syntax_element_wild = syntax_element_end_line + 1,
182 syntax_element_match = syntax_element_wild + 1,
183 syntax_element_word_boundary = syntax_element_match + 1,
184 syntax_element_within_word = syntax_element_word_boundary + 1,
185 syntax_element_word_start = syntax_element_within_word + 1,
186 syntax_element_word_end = syntax_element_word_start + 1,
187 syntax_element_buffer_start = syntax_element_word_end + 1,
188 syntax_element_buffer_end = syntax_element_buffer_start + 1,
189 syntax_element_backref = syntax_element_buffer_end + 1,
190 syntax_element_long_set = syntax_element_backref + 1,
191 syntax_element_set = syntax_element_long_set + 1,
192 syntax_element_jump = syntax_element_set + 1,
193 syntax_element_alt = syntax_element_jump + 1,
194 syntax_element_rep = syntax_element_alt + 1,
195 syntax_element_combining = syntax_element_rep + 1,
196 syntax_element_soft_buffer_end = syntax_element_combining + 1,
197 syntax_element_restart_continue = syntax_element_soft_buffer_end + 1
200 #ifdef BOOST_RE_DEBUG
201 // dwa 09/26/00 - This is needed to suppress warnings about an ambiguous conversion
202 std::ostream& operator<<(std::ostream&, syntax_element_type);
212 // struct re_syntax_base
213 // base class for all syntax types:
214 struct re_syntax_base
216 syntax_element_type type;
218 unsigned int can_be_null;
223 // marks start or end of (...)
224 struct re_brace : public re_syntax_base
231 // marks a literal string and
232 // is followed by an array of charT[length]:
233 struct re_literal : public re_syntax_base
239 // struct re_long_set
240 // provides data for sets [...] containing
242 struct re_set_long : public re_syntax_base
244 unsigned int csingles, cranges, cequivalents;
245 jm_uintfast32_t cclasses;
251 // provides a map of bools for sets containing
252 // narrow, single byte characters.
253 struct re_set : public re_syntax_base
255 unsigned char _map[256];
260 // provides alternative next destination
261 struct re_jump : public re_syntax_base
264 unsigned char _map[256];
269 // provides repeat expressions
270 struct re_repeat : public re_jump
280 // enum re_jump_size_type
281 // provides compiled size of re_jump
282 // allowing for trailing alignment
283 // provide this so we know how many
285 enum re_jump_size_type
287 re_jump_size = (sizeof(re_jump) + padding_mask) & ~(padding_mask),
288 re_repeater_size = (sizeof(re_repeat) + padding_mask) & ~(padding_mask)
291 } // namespace re_detail
295 // handles error codes and flags
297 class BOOST_RE_IX_DECL regbase
302 escape_in_lists = 1, // '\' special inside [...]
303 char_classes = escape_in_lists << 1, // [[:CLASS:]] allowed
304 intervals = char_classes << 1, // {x,y} allowed
305 limited_ops = intervals << 1, // all of + ? and | are normal characters
306 newline_alt = limited_ops << 1, // \n is the same as |
307 bk_plus_qm = newline_alt << 1, // uses \+ and \?
308 bk_braces = bk_plus_qm << 1, // uses \{ and \}
309 bk_parens = bk_braces << 1, // uses \( and \)
310 bk_refs = bk_parens << 1, // \d allowed
311 bk_vbar = bk_refs << 1, // uses \|
313 use_except = bk_vbar << 1, // exception on error
314 failbit = use_except << 1, // error flag
315 literal = failbit << 1, // all characters are literals
316 icase = literal << 1, // characters are matched regardless of case
317 nocollate = icase << 1, // don't use locale specific collation
319 basic = char_classes | intervals | limited_ops | bk_braces | bk_parens | bk_refs,
320 extended = char_classes | intervals | bk_refs,
321 normal = escape_in_lists | char_classes | intervals | bk_refs | nocollate,
322 emacs = bk_braces | bk_parens | bk_refs | bk_vbar,
323 awk = extended | escape_in_lists,
324 grep = basic | newline_alt,
325 egrep = extended | newline_alt,
329 typedef unsigned int flag_type;
337 restart_continue = 4,
339 restart_fixed_lit = 6
342 flag_type BOOST_RE_CALL flags()const
348 regbase(const regbase& b);
354 // some forward declarations:
356 template <class iterator, class Allocator>
357 class _priv_match_data;
359 #if defined(BOOST_NO_STD_ITERATOR_TRAITS) || defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
362 struct regex_iterator_traits
364 typedef typename T::iterator_category iterator_category;
365 typedef typename T::value_type value_type;
367 typedef typename T::difference_type difference_type;
368 typedef typename T::pointer pointer;
369 typedef typename T::reference reference;
371 typedef std::ptrdiff_t difference_type;
372 typedef value_type* pointer;
373 typedef value_type& reference;
378 struct pointer_iterator_traits
380 typedef std::ptrdiff_t difference_type;
381 typedef T value_type;
383 typedef T& reference;
384 typedef std::random_access_iterator_tag iterator_category;
387 struct const_pointer_iterator_traits
389 typedef std::ptrdiff_t difference_type;
390 typedef T value_type;
391 typedef const T* pointer;
392 typedef const T& reference;
393 typedef std::random_access_iterator_tag iterator_category;
397 struct regex_iterator_traits<char*> : pointer_iterator_traits<char>{};
399 struct regex_iterator_traits<const char*> : const_pointer_iterator_traits<char>{};
401 struct regex_iterator_traits<wchar_t*> : pointer_iterator_traits<wchar_t>{};
403 struct regex_iterator_traits<const wchar_t*> : const_pointer_iterator_traits<wchar_t>{};
405 #if defined(__SGI_STL_PORT) && defined(__STL_DEBUG)
407 struct regex_iterator_traits<std::string::iterator> : pointer_iterator_traits<char>{};
409 struct regex_iterator_traits<std::string::const_iterator> : const_pointer_iterator_traits<char>{};
410 #ifndef BOOST_NO_WSTRING
412 struct regex_iterator_traits<std::wstring::iterator> : pointer_iterator_traits<wchar_t>{};
414 struct regex_iterator_traits<std::wstring::const_iterator> : const_pointer_iterator_traits<wchar_t>{};
415 #endif // BOOST_NO_WSTRING
421 struct regex_iterator_traits : public std::iterator_traits<T> {};
426 struct def_alloc_param_traits
428 typedef typename regex_iterator_traits<I>::value_type const_value_type;
429 typedef typename remove_cv<const_value_type>::type type;
434 template <class iterator, class Allocator BOOST_RE_DEF_ALLOC_PARAM(typename re_detail::def_alloc_param_traits<iterator>::type) >
438 // class reg_expression
439 // represents the compiled
440 // regular expression:
443 #if defined(BOOST_RE_NO_TEMPLATE_SWITCH_MERGE) && !defined(BOOST_RE_NO_NAMESPACES)
446 // template don't merge if they contain switch statements so declare these
447 // templates in unnamed namespace (ie with internal linkage), each translation
448 // unit then gets its own local copy, it works seemlessly but bloats the app.
452 template <class charT, class traits BOOST_RE_TRICKY_DEFAULT_PARAM(regex_traits<charT>), class Allocator BOOST_RE_DEF_ALLOC_PARAM(charT) >
453 class reg_expression : public regbase
455 typedef typename traits::size_type traits_size_type;
456 typedef typename traits::uchar_type traits_uchar_type;
457 typedef typename traits::string_type traits_string_type;
460 typedef charT char_type;
461 typedef traits traits_type;
464 // placeholder for actual locale type used by the
465 // traits class to localise *this.
466 typedef typename traits::locale_type locale_type;
468 typedef charT value_type;
469 // reference, const_reference
470 typedef charT& reference;
471 typedef const charT& const_reference;
472 // iterator, const_iterator
473 typedef const charT* const_iterator;
474 typedef const_iterator iterator;
476 typedef typename Allocator::difference_type difference_type;
478 typedef typename Allocator::size_type size_type;
480 typedef Allocator allocator_type;
481 typedef Allocator alloc_type;
483 typedef regbase::flag_type flag_type;
486 explicit reg_expression(const Allocator& a = Allocator());
487 explicit reg_expression(const charT* p, flag_type f = regbase::normal, const Allocator& a = Allocator());
488 reg_expression(const charT* p1, const charT* p2, flag_type f = regbase::normal, const Allocator& a = Allocator());
489 reg_expression(const charT* p, size_type len, flag_type f, const Allocator& a = Allocator());
490 reg_expression(const reg_expression&);
492 reg_expression& BOOST_RE_CALL operator=(const reg_expression&);
493 reg_expression& BOOST_RE_CALL operator=(const charT* ptr)
495 set_expression(ptr, regbase::normal | regbase::use_except);
501 reg_expression& assign(const reg_expression& that)
502 { return *this = that; }
503 reg_expression& assign(const charT* ptr, flag_type f = regbase::normal)
505 set_expression(ptr, f | regbase::use_except);
509 reg_expression& assign(const charT* first,
511 flag_type f = regbase::normal)
513 set_expression(first, last, f | regbase::use_except);
516 #ifndef BOOST_RE_NO_MEMBER_TEMPLATES
518 template <class ST, class SA>
519 unsigned int BOOST_RE_CALL set_expression(const std::basic_string<charT, ST, SA>& p, flag_type f = regbase::normal)
520 { return set_expression(p.data(), p.data() + p.size(), f); }
522 template <class ST, class SA>
523 explicit reg_expression(const std::basic_string<charT, ST, SA>& p, flag_type f = regbase::normal, const Allocator& a = Allocator())
524 : data(a), pkmp(0) { set_expression(p, f); }
527 reg_expression(I first, I last, flag_type f = regbase::normal, const Allocator& al = Allocator())
530 size_type len = last-first;
531 scoped_array<charT> a(new charT[len]);
532 std::copy(first, last, a.get());
533 set_expression(a.get(), a.get() + len, f | regbase::use_except);
536 template <class ST, class SA>
537 reg_expression& BOOST_RE_CALL operator=(const std::basic_string<charT, ST, SA>& p)
539 set_expression(p.c_str(), p.c_str() + p.size(), regbase::normal | regbase::use_except);
543 template <class string_traits, class A>
544 reg_expression& BOOST_RE_CALL assign(
545 const std::basic_string<charT, string_traits, A>& s,
546 flag_type f = regbase::normal)
548 set_expression(s.c_str(), s.c_str() + s.size(), f | regbase::use_except);
552 template <class fwd_iterator>
553 reg_expression& BOOST_RE_CALL assign(fwd_iterator first,
555 flag_type f = regbase::normal)
557 size_type len = last-first;
558 scoped_array<charT> a(new charT[len]);
559 std::copy(first, last, a.get());
560 set_expression(a.get(), a.get() + len, f | regbase::use_except);
563 #elif !defined(BOOST_RE_NO_STRING_DEF_ARGS)
564 unsigned int BOOST_RE_CALL set_expression(const std::basic_string<charT>& p, flag_type f = regbase::normal)
565 { return set_expression(p.data(), p.data() + p.size(), f); }
567 reg_expression(const std::basic_string<charT>& p, flag_type f = regbase::normal, const Allocator& a = Allocator())
568 : data(a), pkmp(0) { set_expression(p, f); }
570 reg_expression& BOOST_RE_CALL operator=(const std::basic_string<charT>& p)
572 set_expression(p.c_str(), p.c_str() + p.size(), regbase::normal | regbase::use_except);
576 reg_expression& BOOST_RE_CALL assign(
577 const std::basic_string<charT>& s,
578 flag_type f = regbase::normal)
580 set_expression(s.c_str(), s.c_str() + s.size(), f | regbase::use_except);
589 Allocator BOOST_RE_CALL get_allocator()const;
592 locale_type BOOST_RE_CALL imbue(locale_type l){ return traits_inst.imbue(l); }
593 locale_type BOOST_RE_CALL getloc()const{ return traits_inst.getloc(); }
596 flag_type BOOST_RE_CALL getflags()const
600 std::basic_string<charT> BOOST_RE_CALL str()const
601 { return std::basic_string<charT>(_expression, _expression_len); }
604 const_iterator BOOST_RE_CALL begin()const
605 { return _expression; }
606 const_iterator BOOST_RE_CALL end()const
607 { return _expression + _expression_len; }
610 void BOOST_RE_CALL swap(reg_expression&)throw();
613 size_type BOOST_RE_CALL size()const
614 { return _expression_len; }
617 size_type BOOST_RE_CALL max_size()const
621 bool BOOST_RE_CALL empty()const
622 { return this->error_code(); }
624 unsigned BOOST_RE_CALL mark_count()const { return marks; }
625 bool BOOST_RE_CALL operator==(const reg_expression&)const;
626 bool BOOST_RE_CALL operator<(const reg_expression&)const;
628 // The following are deprecated as public interfaces
629 // but are available for compatability with earlier versions.
630 allocator_type BOOST_RE_CALL allocator()const;
631 const charT* BOOST_RE_CALL expression()const { return _expression; }
632 unsigned int BOOST_RE_CALL set_expression(const charT* p, const charT* end, flag_type f = regbase::normal);
633 unsigned int BOOST_RE_CALL set_expression(const charT* p, flag_type f = regbase::normal) { return set_expression(p, p + traits_type::length(p), f); }
635 // this should be private but template friends don't work:
636 const traits_type& get_traits()const { return traits_inst; }
637 unsigned int BOOST_RE_CALL error_code()const
643 re_detail::raw_storage<Allocator> data;
644 unsigned _restart_type;
647 unsigned char* startmap;
649 unsigned _expression_len;
650 unsigned int _leading_len;
651 const charT* _leading_string;
652 unsigned int _leading_string_len;
653 re_detail::kmp_info<charT>* pkmp;
654 traits_type traits_inst;
655 unsigned error_code_;
657 void BOOST_RE_CALL compile_maps();
658 void BOOST_RE_CALL compile_map(re_detail::re_syntax_base* node, unsigned char* _map, unsigned int* pnull, unsigned char mask, re_detail::re_syntax_base* terminal = NULL)const;
659 bool BOOST_RE_CALL probe_start(re_detail::re_syntax_base* node, charT c, re_detail::re_syntax_base* terminal)const;
660 bool BOOST_RE_CALL probe_start_null(re_detail::re_syntax_base* node, re_detail::re_syntax_base* terminal)const;
661 void BOOST_RE_CALL fixup_apply(re_detail::re_syntax_base* b, unsigned cbraces);
662 void BOOST_RE_CALL move_offsets(re_detail::re_syntax_base* j, unsigned size);
663 re_detail::re_syntax_base* BOOST_RE_CALL compile_set(const charT*& first, const charT* last);
664 re_detail::re_syntax_base* BOOST_RE_CALL compile_set_aux(re_detail::jstack<traits_string_type, Allocator>& singles, re_detail::jstack<traits_string_type, Allocator>& ranges, re_detail::jstack<jm_uintfast32_t, Allocator>& classes, re_detail::jstack<traits_string_type, Allocator>& equivalents, bool isnot, const re_detail::_narrow_type&);
665 re_detail::re_syntax_base* BOOST_RE_CALL compile_set_aux(re_detail::jstack<traits_string_type, Allocator>& singles, re_detail::jstack<traits_string_type, Allocator>& ranges, re_detail::jstack<jm_uintfast32_t, Allocator>& classes, re_detail::jstack<traits_string_type, Allocator>& equivalents, bool isnot, const re_detail::_wide_type&);
666 re_detail::re_syntax_base* BOOST_RE_CALL compile_set_simple(re_detail::re_syntax_base* dat, unsigned long cls, bool isnot = false);
667 unsigned int BOOST_RE_CALL parse_inner_set(const charT*& first, const charT* last);
669 re_detail::re_syntax_base* BOOST_RE_CALL add_simple(re_detail::re_syntax_base* dat, re_detail::syntax_element_type type, unsigned int size = sizeof(re_detail::re_syntax_base));
670 re_detail::re_syntax_base* BOOST_RE_CALL add_literal(re_detail::re_syntax_base* dat, charT c);
671 charT BOOST_RE_CALL parse_escape(const charT*& first, const charT* last);
672 void BOOST_RE_CALL parse_range(const charT*& first, const charT* last, unsigned& min, unsigned& max);
673 bool BOOST_RE_CALL skip_space(const charT*& first, const charT* last);
674 unsigned int BOOST_RE_CALL probe_restart(re_detail::re_syntax_base* dat);
675 unsigned int BOOST_RE_CALL fixup_leading_rep(re_detail::re_syntax_base* dat, re_detail::re_syntax_base* end);
676 void BOOST_RE_CALL fail(unsigned int err);
679 static int BOOST_RE_CALL repeat_count(const reg_expression& e)
680 { return e.repeats; }
681 static unsigned int BOOST_RE_CALL restart_type(const reg_expression& e)
682 { return e._restart_type; }
683 static const re_detail::re_syntax_base* BOOST_RE_CALL first(const reg_expression& e)
684 { return (const re_detail::re_syntax_base*)e.data.data(); }
685 static const unsigned char* BOOST_RE_CALL get_map(const reg_expression& e)
686 { return e.startmap; }
687 static unsigned int BOOST_RE_CALL leading_length(const reg_expression& e)
688 { return e._leading_len; }
689 static const re_detail::kmp_info<charT>* get_kmp(const reg_expression& e)
691 static bool BOOST_RE_CALL can_start(charT c, const unsigned char* _map, unsigned char mask, const re_detail::_wide_type&);
692 static bool BOOST_RE_CALL can_start(charT c, const unsigned char* _map, unsigned char mask, const re_detail::_narrow_type&);
695 template <class charT, class traits, class Allocator>
696 void BOOST_RE_CALL reg_expression<charT, traits, Allocator>::swap(reg_expression& that)throw()
698 // this is not as efficient as it should be,
699 // however swapping traits classes is problematic
700 // so just use 'brute force' method for now:
701 reg_expression<charT, traits, Allocator> e(that);
707 #if defined(BOOST_RE_NO_TEMPLATE_SWITCH_MERGE) && !defined(BOOST_RE_NO_NAMESPACES)
712 // class match_results and match_results_base
713 // handles what matched where
715 template <class iterator>
718 typedef typename re_detail::regex_iterator_traits<iterator>::value_type value_type;
719 #if defined(BOOST_NO_STD_ITERATOR_TRAITS) || defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
720 typedef std::ptrdiff_t difference_type;
722 typedef typename re_detail::regex_iterator_traits<iterator>::difference_type difference_type;
724 typedef iterator iterator_type;
730 operator std::basic_string<value_type> ()const
732 std::basic_string<value_type> result;
734 BOOST_RE_DISTANCE((iterator)first, (iterator)second, len);
739 result.append(1, *i);
744 #ifdef BOOST_OLD_REGEX_H
746 // the following are deprecated, do not use!!
749 operator unsigned int()const;
750 operator short()const
752 return (short)(int)(*this);
754 operator unsigned short()const
756 return (unsigned short)(unsigned int)(*this);
759 sub_match() { matched = false; }
760 sub_match(iterator i) : first(i), second(i), matched(false) {}
762 bool operator==(const sub_match& that)const
764 return (first == that.first) && (second == that.second) && (matched == that.matched);
766 bool BOOST_RE_CALL operator !=(const sub_match& that)const
767 { return !(*this == that); }
769 difference_type BOOST_RE_CALL length()const
772 BOOST_RE_DISTANCE((iterator)first, (iterator)second, n);
777 #ifdef BOOST_OLD_REGEX_H
779 template <class iterator, class charT>
780 int do_toi(iterator i, iterator j, char c, int radix)
784 int result = std::strtol(s.c_str(), &p, radix);
785 if(*p)throw bad_pattern("Bad sub-expression");
791 template <class I, class charT>
792 int do_toi(I& i, I j, charT c)
795 while((i != j) && (isdigit(*i)))
797 result = result*10 + (*i - '0');
805 template <class iterator>
806 sub_match<iterator>::operator int()const
810 if(i == j)throw bad_pattern("Bad sub-expression");
812 if((i != j) && (*i == '-'))
817 neg *= re_detail::do_toi(i, j, *i);
818 if(i != j)throw bad_pattern("Bad sub-expression");
821 template <class iterator>
822 sub_match<iterator>::operator unsigned int()const
827 throw bad_pattern("Bad sub-expression");
828 return re_detail::do_toi(i, j, *first);
834 template <class iterator, class Allocator BOOST_RE_DEF_ALLOC_PARAM(typename def_alloc_param_traits<iterator>::type) >
835 class match_results_base
838 typedef Allocator alloc_type;
839 typedef typename REBIND_TYPE(iterator, Allocator)::size_type size_type;
840 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
841 typedef typename std::iterator_traits<iterator>::difference_type difference_type;
842 typedef typename std::iterator_traits<iterator>::value_type char_type;
844 typedef std::ptrdiff_t difference_type;
845 typedef typename re_detail::regex_iterator_traits<iterator>::value_type char_type;
847 typedef sub_match<iterator> value_type;
848 typedef iterator iterator_type;
851 typedef BOOST_RE_MAYBE_TYPENAME REBIND_TYPE(char, Allocator) c_alloc;
853 struct c_reference : public c_alloc
855 unsigned int cmatches;
857 sub_match<iterator> head, tail, null;
859 iterator line_pos, base;
860 c_reference(const Allocator& a) : c_alloc(a) { }
862 bool operator==(const c_reference& that)const
864 return (cmatches == that.cmatches) &&
865 (count == that.count) &&
866 (head == that.head) &&
867 (tail == that.tail) &&
868 (lines == that.lines) &&
871 bool operator!=(const c_reference& that)const
872 { return !(*this == that); }
877 void BOOST_RE_CALL cow();
879 // protected contructor for derived class...
880 match_results_base(bool){}
881 void BOOST_RE_CALL free();
885 match_results_base(const Allocator& a = Allocator());
887 match_results_base(const match_results_base& m)
893 match_results_base& BOOST_RE_CALL operator=(const match_results_base& m);
895 ~match_results_base()
900 size_type BOOST_RE_CALL size()const
902 //return (*this)[0].matched ? ref->cmatches : 0;
903 return ref->cmatches;
906 const sub_match<iterator>& BOOST_RE_CALL operator[](int n) const
908 if((n >= 0) && ((unsigned int)n < ref->cmatches))
909 return *(sub_match<iterator>*)((char*)ref + sizeof(c_reference) + sizeof(sub_match<iterator>)*n);
910 return (n == -1) ? ref->head : (n == -2) ? ref->tail : ref->null;
913 Allocator BOOST_RE_CALL allocator()const;
915 difference_type BOOST_RE_CALL length(unsigned int sub = 0)const
917 jm_assert(ref->cmatches);
918 const sub_match<iterator>& m = (*this)[sub];
919 if(m.matched == false)
922 BOOST_RE_DISTANCE((iterator)m.first, (iterator)m.second, n);
926 std::basic_string<char_type> str(int i)const
928 return static_cast<std::basic_string<char_type> >((*this)[i]);
931 unsigned int BOOST_RE_CALL line()const
936 difference_type BOOST_RE_CALL position(unsigned int sub = 0)const
938 jm_assert(ref->cmatches);
939 const sub_match<iterator>& s = (*this)[sub];
940 if(s.matched == false)
943 BOOST_RE_DISTANCE((iterator)ref->base, (iterator)s.first, n);
947 iterator BOOST_RE_CALL line_start()const
949 return ref->line_pos;
952 void swap(match_results_base& that)
954 c_reference* t = that.ref;
959 bool operator==(const match_results_base& that)const;
960 bool operator<(const match_results_base& that)const
961 { return position() < that.position(); }
963 friend class match_results<iterator, Allocator>;
965 void BOOST_RE_CALL set_size(size_type n);
966 void BOOST_RE_CALL set_size(size_type n, iterator i, iterator j);
967 void BOOST_RE_CALL maybe_assign(const match_results_base& m);
968 void BOOST_RE_CALL init_fail(iterator i, iterator j);
970 void BOOST_RE_CALL set_first(iterator i);
971 void BOOST_RE_CALL set_first(iterator i, size_t pos);
973 void BOOST_RE_CALL set_second(iterator i)
976 ((sub_match<iterator>*)(ref+1))->second = i;
977 ((sub_match<iterator>*)(ref+1))->matched = true;
979 ref->tail.matched = (ref->tail.first == ref->tail.second) ? false : true;
982 void BOOST_RE_CALL set_second(iterator i, size_t pos)
985 ((sub_match<iterator>*)((char*)ref + sizeof(c_reference) + sizeof(sub_match<iterator>) * pos))->second = i;
986 ((sub_match<iterator>*)((char*)ref + sizeof(c_reference) + sizeof(sub_match<iterator>) * pos))->matched = true;
990 ref->tail.matched = (ref->tail.first == ref->tail.second) ? false : true;
994 void BOOST_RE_CALL set_line(unsigned int i, iterator pos)
1000 void BOOST_RE_CALL set_base(iterator pos)
1006 template <class iterator, class Allocator>
1007 void BOOST_RE_CALL match_results_base<iterator, Allocator>::set_first(iterator i)
1010 ref->head.second = i;
1011 ref->head.matched = (ref->head.first == ref->head.second) ? false : true;
1012 sub_match<iterator>* p1 = (sub_match<iterator>*)(ref+1);
1013 sub_match<iterator>* p2 = p1 + ref->cmatches;
1015 p1->matched = false;
1019 p1->matched = false;
1020 p1->first = ref->tail.second;
1021 p1->second = ref->tail.second;
1026 template <class iterator, class Allocator>
1027 void BOOST_RE_CALL match_results_base<iterator, Allocator>::set_first(iterator i, size_t pos)
1030 ((sub_match<iterator>*)((char*)ref + sizeof(c_reference) + sizeof(sub_match<iterator>) * pos))->first = i;
1033 ref->head.second = i;
1034 ref->head.matched = (ref->head.first == ref->head.second) ? false : true;
1035 sub_match<iterator>* p1 = (sub_match<iterator>*)(ref+1);
1036 sub_match<iterator>* p2 = p1 + ref->cmatches;
1038 p1->matched = false;
1042 p1->matched = false;
1043 p1->first = ref->tail.second;
1044 p1->second = ref->tail.second;
1051 template <class iterator, class Allocator>
1052 match_results_base<iterator, Allocator>::match_results_base(const Allocator& a)
1054 ref = (c_reference*)c_alloc(a).allocate(sizeof(sub_match<iterator>) + sizeof(c_reference));
1057 new (ref) c_reference(a);
1060 // construct the sub_match<iterator>:
1063 new ((sub_match<iterator>*)(ref+1)) sub_match<iterator>();
1073 c_alloc(a).deallocate((char*)(void*)ref, sizeof(sub_match<iterator>) + sizeof(c_reference));
1078 template <class iterator, class Allocator>
1079 Allocator BOOST_RE_CALL match_results_base<iterator, Allocator>::allocator()const
1081 return *((c_alloc*)ref);
1084 template <class iterator, class Allocator>
1085 inline match_results_base<iterator, Allocator>& BOOST_RE_CALL match_results_base<iterator, Allocator>::operator=(const match_results_base<iterator, Allocator>& m)
1097 template <class iterator, class Allocator>
1098 void BOOST_RE_CALL match_results_base<iterator, Allocator>::free()
1100 if(--(ref->count) == 0)
1103 sub_match<iterator>* p1, *p2;
1104 p1 = (sub_match<iterator>*)(ref+1);
1105 p2 = p1 + ref->cmatches;
1112 a.deallocate((char*)(void*)ref, sizeof(sub_match<iterator>) * ref->cmatches + sizeof(c_reference));
1116 template <class iterator, class Allocator>
1117 bool match_results_base<iterator, Allocator>::operator==(const match_results_base<iterator, Allocator>& that)const
1119 if(*ref != *(that.ref))
1121 const sub_match<iterator>* p1 = (sub_match<iterator>*)(ref+1);
1122 const sub_match<iterator>* p2 = p1 + ref->cmatches;
1123 const sub_match<iterator>* p3 = (sub_match<iterator>*)(that.ref+1);
1134 template <class iterator, class Allocator>
1135 void BOOST_RE_CALL match_results_base<iterator, Allocator>::set_size(size_type n)
1137 if(ref->cmatches != n)
1139 c_reference* newref = (c_reference*)ref->allocate(sizeof(sub_match<iterator>) * n + sizeof(c_reference));
1142 new (newref) c_reference(*ref);
1144 newref->cmatches = n;
1145 sub_match<iterator>* p1, *p2;
1146 p1 = (sub_match<iterator>*)(newref+1);
1147 p2 = p1 + newref->cmatches;
1152 new (p1) sub_match<iterator>();
1159 p2 = (sub_match<iterator>*)(newref+1);
1172 ref->deallocate((char*)(void*)newref, sizeof(sub_match<iterator>) * n + sizeof(c_reference));
1178 template <class iterator, class Allocator>
1179 void BOOST_RE_CALL match_results_base<iterator, Allocator>::set_size(size_type n, iterator i, iterator j)
1181 if(ref->cmatches != n)
1183 c_reference* newref = (c_reference*)ref->allocate(sizeof(sub_match<iterator>) * n + sizeof(c_reference));;
1185 new (newref) c_reference(*ref);
1187 newref->cmatches = n;
1188 sub_match<iterator>* p1 = (sub_match<iterator>*)(newref+1);
1189 sub_match<iterator>* p2 = p1 + newref->cmatches;
1194 new (p1) sub_match<iterator>(j);
1201 p2 = (sub_match<iterator>*)(newref+1);
1214 ref->deallocate((char*)(void*)newref, sizeof(sub_match<iterator>) * n + sizeof(c_reference));
1221 // set iterators to be i, matched to false:
1222 sub_match<iterator>* p1, *p2;
1223 p1 = (sub_match<iterator>*)(ref+1);
1224 p2 = p1 + ref->cmatches;
1229 p1->matched = false;
1233 ref->head.first = i;
1234 ref->tail.second = j;
1235 ref->head.matched = ref->tail.matched = true;
1236 ref->null.first = ref->null.second = j;
1237 ref->null.matched = false;
1240 template <class iterator, class Allocator>
1241 inline void BOOST_RE_CALL match_results_base<iterator, Allocator>::init_fail(iterator i, iterator j)
1243 set_size(ref->cmatches, i, j);
1246 template <class iterator, class Allocator>
1247 void BOOST_RE_CALL match_results_base<iterator, Allocator>::maybe_assign(const match_results_base<iterator, Allocator>& m)
1249 sub_match<iterator>* p1, *p2;
1250 p1 = (sub_match<iterator>*)(ref+1);
1251 p2 = (sub_match<iterator>*)(m.ref+1);
1252 iterator base = (*this)[-1].first;
1253 unsigned int len1 = 0;
1254 unsigned int len2 = 0;
1255 unsigned int base1 = 0;
1256 unsigned int base2 = 0;
1258 for(i = 0; i < ref->cmatches; ++i)
1261 // leftmost takes priority over longest:
1262 BOOST_RE_DISTANCE(base, p1->first, base1);
1263 BOOST_RE_DISTANCE(base, p2->first, base2);
1264 if(base1 < base2) return;
1265 if(base2 < base1) break;
1267 BOOST_RE_DISTANCE(p1->first, p1->second, len1);
1268 BOOST_RE_DISTANCE(p2->first, p2->second, len2);
1269 if((len1 != len2) || ((p1->matched == false) && (p2->matched == true)))
1271 if((p1->matched == true) && (p2->matched == false))
1276 if(i == ref->cmatches)
1280 else if((len2 > len1) || ((p1->matched == false) && (p2->matched == true)) )
1284 template <class iterator, class Allocator>
1285 void BOOST_RE_CALL match_results_base<iterator, Allocator>::cow()
1289 c_reference* newref = (c_reference*)ref->allocate(sizeof(sub_match<iterator>) * ref->cmatches + sizeof(c_reference));
1291 new (newref) c_reference(*ref);
1293 sub_match<iterator>* p1 = (sub_match<iterator>*)(newref+1);
1294 sub_match<iterator>* p2 = p1 + newref->cmatches;
1295 sub_match<iterator>* p3 = (sub_match<iterator>*)(ref+1);
1299 new (p1) sub_match<iterator>(*p3);
1306 p2 = (sub_match<iterator>*)(newref+1);
1320 ref->deallocate((char*)(void*)newref, sizeof(sub_match<iterator>) * ref->cmatches + sizeof(c_reference));
1326 } // namespace re_detail
1329 // class match_results
1330 // encapsulates match_results_base, does a deep copy rather than
1331 // reference counting to ensure thread safety when copying
1332 // other match_results instances
1334 template <class iterator, class Allocator>
1335 class match_results : public re_detail::match_results_base<iterator, Allocator>
1337 typedef re_detail::match_results_base<iterator, Allocator> base_type;
1340 typedef typename base_type::alloc_type alloc_type;
1341 typedef typename base_type::size_type size_type;
1342 typedef typename base_type::char_type char_type;
1343 typedef typename base_type::value_type value_type;
1344 typedef typename base_type::difference_type difference_type;
1345 typedef typename base_type::iterator_type iterator_type;
1347 explicit match_results(const Allocator& a = Allocator())
1348 : re_detail::match_results_base<iterator, Allocator>(a){}
1350 match_results(const re_detail::match_results_base<iterator, Allocator>& m)
1351 : re_detail::match_results_base<iterator, Allocator>(m){}
1353 match_results& operator=(const re_detail::match_results_base<iterator, Allocator>& m)
1356 base_type::operator=(m);
1360 match_results(const match_results& m);
1361 match_results& operator=(const match_results& m);
1363 // the following function definitions should *not* be required, except
1364 // when this class is used as a template inside another template definition,
1365 // in which members of the base class are not visible to the calling code.
1366 // As a workaround we define simple forwarding functions:
1368 size_type size()const
1369 { return static_cast<const base_type*>(this)->size(); }
1371 const sub_match<iterator>& operator[](int n) const
1372 { return (*static_cast<const base_type*>(this))[n]; }
1374 Allocator allocator()const
1375 { return static_cast<const base_type*>(this)->allocator(); }
1377 difference_type length(int sub = 0)const
1378 { return static_cast<const base_type*>(this)->length(sub); }
1380 difference_type position(unsigned int sub = 0)const
1381 { return static_cast<const base_type*>(this)->position(sub); }
1383 unsigned int line()const
1384 { return static_cast<const base_type*>(this)->line(); }
1386 iterator line_start()const
1387 { return static_cast<const base_type*>(this)->line_start(); }
1389 std::basic_string<char_type> str(int sub = 0)const
1390 { return static_cast<const base_type*>(this)->str(sub); }
1392 void swap(match_results& that)
1393 { static_cast<base_type*>(this)->swap(that); }
1395 bool operator==(const match_results& that)const
1396 { return static_cast<const base_type&>(*this) == static_cast<const base_type&>(that); }
1398 bool operator<(const match_results& that) const
1399 { return position() < that.position(); }
1402 template <class iterator, class Allocator>
1403 match_results<iterator, Allocator>::match_results(const match_results<iterator, Allocator>& m)
1404 : re_detail::match_results_base<iterator, Allocator>(false)
1407 reinterpret_cast<typename re_detail::match_results_base<iterator, Allocator>::c_reference *>
1408 (m.ref->allocate(sizeof(sub_match<iterator>) * m.ref->cmatches +
1409 sizeof(typename re_detail::match_results_base<iterator, Allocator>::c_reference)));
1411 new (this->ref) typename re_detail::match_results_base<iterator, Allocator>::c_reference(*m.ref);
1412 this->ref->count = 1;
1413 sub_match<iterator>* p1 = (sub_match<iterator>*)(this->ref+1);
1414 sub_match<iterator>* p2 = p1 + this->ref->cmatches;
1415 sub_match<iterator>* p3 = (sub_match<iterator>*)(m.ref+1);
1419 new (p1) sub_match<iterator>(*p3);
1426 p2 = (sub_match<iterator>*)(this->ref+1);
1429 re_detail::jm_destroy(p2);
1432 re_detail::jm_destroy(this->ref);
1438 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));
1443 template <class iterator, class Allocator>
1444 match_results<iterator, Allocator>& match_results<iterator, Allocator>::operator=(const match_results<iterator, Allocator>& m)
1446 match_results<iterator, Allocator> t(m);
1451 namespace re_detail{
1452 template <class iterator, class charT, class traits_type, class Allocator>
1453 iterator BOOST_RE_CALL re_is_set_member(iterator next,
1456 const reg_expression<charT, traits_type, Allocator>& e);
1457 } // namepsace re_detail
1460 #if __BORLANDC__ > 0x520
1465 } // namespace boost
1467 #include <boost/re_detail/regex_compile.hpp>
1471 typedef reg_expression<char, regex_traits<char>, BOOST_RE_DEF_ALLOC(char)> regex;
1472 #ifndef BOOST_RE_NO_WCSTRING
1473 typedef reg_expression<wchar_t, regex_traits<wchar_t>, BOOST_RE_DEF_ALLOC(wchar_t)> wregex;
1476 typedef match_results<const char*> cmatch;
1477 #ifndef BOOST_RE_NO_WCSTRING
1478 typedef match_results<const wchar_t*> wcmatch;
1481 } // namespace boost
1483 #include <boost/re_detail/regex_match.hpp>
1484 #include <boost/re_detail/regex_format.hpp>
1485 #include <boost/re_detail/regex_split.hpp>
1488 #endif // __cplusplus