]> git.lyx.org Git - lyx.git/blob - boost/boost/regex.hpp
update boost
[lyx.git] / boost / boost / regex.hpp
1 /*
2  *
3  * Copyright (c) 1998-2002
4  * Dr John Maddock
5  *
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.
13  *
14  */
15
16  /*
17   *   LOCATION:    see http://www.boost.org for most recent version.
18   *   FILE         regex.cpp
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.
23   */
24
25
26 /* start with C compatibility API */
27
28 #ifndef BOOST_RE_REGEX_HPP
29 #define BOOST_RE_REGEX_HPP
30
31 #include <boost/cregex.hpp>
32
33 #ifdef __cplusplus
34
35 // what follows is all C++ don't include in C builds!!
36
37 #ifdef BOOST_REGEX_DEBUG
38 # include <iosfwd>
39 #endif
40
41 #include <new>
42 #include <boost/regex/config.hpp>
43 #include <cstring>
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>
52
53
54 namespace boost{
55
56 #ifdef __BORLANDC__
57    #pragma option push -a8 -b -Vx -Ve -pc -w-8027
58 #endif
59
60 namespace re_detail{
61
62 struct re_set_long;
63 struct re_syntax_base;
64
65 } // namespace re_detail
66
67 namespace deprecated{
68 //
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> {};
73
74 template<>
75 class char_regex_traits_i<char> : public regex_traits<char>
76 {
77 public:
78    typedef char char_type;
79    typedef unsigned char uchar_type;
80    typedef unsigned int size_type;
81    typedef regex_traits<char> base_type;
82
83    char BOOST_REGEX_CALL translate(char c, bool)const
84    {
85       return static_cast<const regex_traits<char>*>(this)->translate(c, true);
86    }
87 };
88
89 #ifndef BOOST_NO_WREGEX
90 template<>
91 class char_regex_traits_i<wchar_t> : public regex_traits<wchar_t>
92 {
93 public:
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;
98
99    wchar_t BOOST_REGEX_CALL translate(wchar_t c, bool)const
100    {
101       return static_cast<const regex_traits<wchar_t>*>(this)->translate(c, true);
102    }
103    boost::uint_fast32_t BOOST_REGEX_CALL lookup_classname(const wchar_t* first, const wchar_t* last)const
104    {
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;
108       return result;
109    }
110 };
111 #endif
112 } // namespace deprecated
113
114
115 namespace re_detail{
116
117 enum mask_type
118 {
119    mask_take = 1,
120    mask_skip = 2,
121    mask_any = mask_skip | mask_take,
122    mask_all = mask_any
123 };
124
125 struct _narrow_type{};
126 struct _wide_type{};
127
128 template <class charT>
129 class is_byte;
130
131 template<>
132 class is_byte<char>
133 {
134 public:
135    typedef _narrow_type width_type;
136 };
137
138 template<>
139 class is_byte<unsigned char>
140 {
141 public:
142    typedef _narrow_type width_type;
143 };
144
145 template<>
146 class is_byte<signed char>
147 {
148 public:
149    typedef _narrow_type width_type;
150 };
151
152 template <class charT>
153 class is_byte
154 {
155 public:
156    typedef _wide_type width_type;
157 };
158
159
160 //
161 // compiled structures
162 //
163 // the following defs describe the format of the compiled string
164 //
165
166 //
167 // enum syntax_element_type
168 // describes the type of a record
169 enum syntax_element_type
170 {
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
193 };
194
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);
198 #endif
199
200 union offset_type
201 {
202    re_syntax_base* p;
203    std::size_t i;
204 };
205
206 //
207 // struct re_syntax_base
208 // base class for all syntax types:
209 struct re_syntax_base
210 {
211    syntax_element_type type;
212    offset_type next;
213    unsigned int can_be_null;
214 };
215
216 //
217 // struct re_brace
218 // marks start or end of (...)
219 struct re_brace : public re_syntax_base
220 {
221    int index;
222 };
223
224 //
225 // struct re_literal
226 // marks a literal string and
227 // is followed by an array of charT[length]:
228 struct re_literal : public re_syntax_base
229 {
230    unsigned int length;
231 };
232
233 //
234 // struct re_long_set
235 // provides data for sets [...] containing
236 // wide characters
237 struct re_set_long : public re_syntax_base
238 {
239    unsigned int csingles, cranges, cequivalents;
240    boost::uint_fast32_t cclasses;
241    bool isnot;
242 };
243
244 //
245 // struct re_set
246 // provides a map of bools for sets containing
247 // narrow, single byte characters.
248 struct re_set : public re_syntax_base
249 {
250    unsigned char _map[256];
251 };
252
253 //
254 // struct re_jump
255 // provides alternative next destination
256 struct re_jump : public re_syntax_base
257 {
258    offset_type alt;
259    unsigned char _map[256];
260 };
261
262 //
263 // struct re_repeat
264 // provides repeat expressions
265 struct re_repeat : public re_jump
266 {
267    unsigned min, max;
268    int id;
269    bool leading;
270    bool greedy;
271    bool singleton;
272 };
273
274
275 //
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
280 // bytes to insert
281 enum re_jump_size_type
282 {
283    re_jump_size = (sizeof(re_jump) + padding_mask) & ~(padding_mask),
284    re_repeater_size = (sizeof(re_repeat) + padding_mask) & ~(padding_mask)
285 };
286
287 } // namespace re_detail
288
289 //
290 // class basic_regex
291 // handles error codes and flags
292
293 class BOOST_REGEX_DECL regbase
294 {
295 public:
296    enum flag_type_
297    {
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 \|
308
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
314
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,
322       sed = basic,
323       perl = normal
324    };
325    typedef unsigned int flag_type;
326
327    enum restart_info
328    {
329       restart_any = 0,
330       restart_word = 1,
331       restart_line = 2,
332       restart_buf = 3,
333       restart_continue = 4,
334       restart_lit = 5,
335       restart_fixed_lit = 6
336    };
337
338    flag_type BOOST_REGEX_CALL flags()const
339    {
340       return _flags;
341    }
342
343    regbase();
344    regbase(const regbase& b);
345 protected:
346    flag_type _flags;
347 };
348
349 //
350 // some forward declarations:
351 namespace re_detail{
352 template <class iterator, class Allocator>
353 class _priv_match_data;
354
355 #if defined(BOOST_NO_STD_ITERATOR_TRAITS) || defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
356
357 template <class T>
358 struct regex_iterator_traits 
359 {
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;
366 #else
367   typedef std::ptrdiff_t                difference_type;
368   typedef value_type*                   pointer;
369   typedef value_type&                   reference;
370 #endif
371 };
372
373 template <class T>
374 struct pointer_iterator_traits
375 {
376    typedef std::ptrdiff_t difference_type;
377    typedef T value_type;
378    typedef T* pointer;
379    typedef T& reference;
380    typedef std::random_access_iterator_tag iterator_category;
381 };
382 template <class T>
383 struct const_pointer_iterator_traits
384 {
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;
390 };
391
392 template<>
393 struct regex_iterator_traits<char*> : pointer_iterator_traits<char>{};
394 template<>
395 struct regex_iterator_traits<const char*> : const_pointer_iterator_traits<char>{};
396 template<>
397 struct regex_iterator_traits<wchar_t*> : pointer_iterator_traits<wchar_t>{};
398 template<>
399 struct regex_iterator_traits<const wchar_t*> : const_pointer_iterator_traits<wchar_t>{};
400
401 #if defined(__SGI_STL_PORT) && defined(__STL_DEBUG)
402 template<>
403 struct regex_iterator_traits<std::string::iterator> : pointer_iterator_traits<char>{};
404 template<>
405 struct regex_iterator_traits<std::string::const_iterator> : const_pointer_iterator_traits<char>{};
406 #ifndef BOOST_NO_STD_WSTRING
407 template<>
408 struct regex_iterator_traits<std::wstring::iterator> : pointer_iterator_traits<wchar_t>{};
409 template<>
410 struct regex_iterator_traits<std::wstring::const_iterator> : const_pointer_iterator_traits<wchar_t>{};
411 #endif // BOOST_NO_WSTRING
412 #endif // stport
413
414 #else
415
416 template <class T>
417 struct regex_iterator_traits : public std::iterator_traits<T> {};
418
419 #endif
420
421 template <class I>
422 struct def_alloc_param_traits
423 {
424    typedef typename regex_iterator_traits<I>::value_type const_value_type;
425    typedef typename remove_cv<const_value_type>::type type;
426 };
427 template <>
428 struct def_alloc_param_traits<const char*>
429 {
430    typedef char type;
431 };
432 template <>
433 struct def_alloc_param_traits<const wchar_t*>
434 {
435    typedef wchar_t type;
436 };
437
438 }
439
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) >
443 #else
444 BOOST_DEFAULT_ALLOCATOR(re_detail::def_alloc_param_traits<iterator>::type) >
445 #endif
446 class match_results;
447
448 //
449 // class reg_expression
450 // represents the compiled
451 // regular expression:
452 //
453
454 #ifdef BOOST_REGEX_NO_FWD
455 template <class charT, class traits = regex_traits<charT>, class Allocator = BOOST_DEFAULT_ALLOCATOR(charT) >
456 #else
457 template <class charT, class traits, class Allocator >
458 #endif
459 class reg_expression : public regbase
460 {
461 public:
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;
465    // typedefs:
466    typedef charT char_type;
467    typedef traits traits_type;
468
469    // locale_type
470    // placeholder for actual locale type used by the
471    // traits class to localise *this.
472    typedef typename traits::locale_type locale_type;
473    // value_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;
481    // difference_type
482    typedef typename Allocator::difference_type difference_type;
483    // size_type
484    typedef typename Allocator::size_type size_type;   
485    // allocator_type
486    typedef Allocator allocator_type;
487    typedef Allocator alloc_type;
488    // flag_type
489    typedef regbase::flag_type flag_type;
490    
491 public:
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&);
497    ~reg_expression();
498    reg_expression& BOOST_REGEX_CALL operator=(const reg_expression&);
499    reg_expression& BOOST_REGEX_CALL operator=(const charT* ptr)
500    {
501       set_expression(ptr, regbase::normal | regbase::use_except);
502       return *this;
503    }
504
505    //
506    // assign:
507    reg_expression& assign(const reg_expression& that)
508    { return *this = that; }
509    reg_expression& assign(const charT* ptr, flag_type f = regbase::normal)
510    {
511       set_expression(ptr, f | regbase::use_except);
512       return *this;
513    }
514
515    reg_expression& assign(const charT* first,
516                           const charT* last,
517                           flag_type f = regbase::normal)
518    {
519       set_expression(first, last, f | regbase::use_except);
520       return *this;
521    }
522 #ifndef BOOST_NO_MEMBER_TEMPLATES
523
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); }
527
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); }
531
532    template <class I>
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)
535    {
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);
540    }
541
542    template <class ST, class SA>
543    reg_expression& BOOST_REGEX_CALL operator=(const std::basic_string<charT, ST, SA>& p)
544    {
545       set_expression(p.c_str(), p.c_str() + p.size(), regbase::normal | regbase::use_except);
546       return *this;
547    }
548
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)
553    {
554       set_expression(s.c_str(), s.c_str() + s.size(), f | regbase::use_except);
555       return *this;
556    }
557
558    template <class fwd_iterator>
559    reg_expression& BOOST_REGEX_CALL assign(fwd_iterator first,
560                           fwd_iterator last,
561                           flag_type f = regbase::normal)
562    {
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);
567       return *this;
568    }
569 #else
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); }
572
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); }
575
576    reg_expression& BOOST_REGEX_CALL operator=(const std::basic_string<charT>& p)
577    {
578       set_expression(p.c_str(), p.c_str() + p.size(), regbase::normal | regbase::use_except);
579       return *this;
580    }
581
582    reg_expression& BOOST_REGEX_CALL assign(
583        const std::basic_string<charT>& s,
584        flag_type f = regbase::normal)
585    {
586       set_expression(s.c_str(), s.c_str() + s.size(), f | regbase::use_except);
587       return *this;
588    }
589
590 #endif
591
592
593    //
594    // allocator access:
595    Allocator BOOST_REGEX_CALL get_allocator()const;
596    //
597    // locale:
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(); }
600    //
601    // flags:
602    flag_type BOOST_REGEX_CALL getflags()const
603    { return flags(); }
604    //
605    // str:
606    std::basic_string<charT> BOOST_REGEX_CALL str()const
607    {
608       std::basic_string<charT> result;
609       if(this->error_code() == 0)
610          result = std::basic_string<charT>(_expression, _expression_len);
611       return result;
612    }
613    //
614    // begin, end:
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); }
619    //
620    // swap:
621    void BOOST_REGEX_CALL swap(reg_expression&)throw();
622    //
623    // size:
624    size_type BOOST_REGEX_CALL size()const
625    { return (this->error_code() ? 0 : _expression_len); }
626    //
627    // max_size:
628    size_type BOOST_REGEX_CALL max_size()const
629    { return UINT_MAX; }
630    //
631    // empty:
632    bool BOOST_REGEX_CALL empty()const
633    { return 0 != this->error_code(); }
634
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;
638    //
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); }
645    //
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
649    {
650       return error_code_;
651    }
652
653 private:
654    traits_type traits_inst;
655    re_detail::raw_storage<Allocator> data;
656    unsigned _restart_type;
657    unsigned marks;
658    int repeats;
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_;
666    charT* _expression;
667
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);
679
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);
688
689 protected:
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)
701    { return e.pkmp; }
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&);
704 };
705
706 template <class charT, class traits, class Allocator>
707 inline void BOOST_REGEX_CALL reg_expression<charT, traits, Allocator>::swap(reg_expression& that)throw()
708 {
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);
713    that = *this;
714    *this = e;
715 }
716
717
718 //
719 // class match_results and match_results_base
720 // handles what matched where
721
722 template <class iterator>
723 struct sub_match
724 {
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;
728 #else
729    typedef typename re_detail::regex_iterator_traits<iterator>::difference_type       difference_type;
730 #endif
731    typedef iterator                                                  iterator_type;
732    
733    iterator first;
734    iterator second;
735    bool matched;
736
737    operator std::basic_string<value_type> ()const
738    {
739       std::basic_string<value_type> result;
740       std::size_t len = boost::re_detail::distance((iterator)first, (iterator)second);
741       result.reserve(len);
742       iterator i = first;
743       while(i != second)
744       {
745          result.append(1, *i);
746          ++i;
747       }
748       return result;
749    }
750    #ifdef BOOST_OLD_REGEX_H
751    //
752    // the following are deprecated, do not use!!
753    //
754    operator int()const;
755    operator unsigned int()const;
756    operator short()const
757    {
758       return (short)(int)(*this);
759    }
760    operator unsigned short()const
761    {
762       return (unsigned short)(unsigned int)(*this);
763    }
764    #endif
765    sub_match() { matched = false; }
766    sub_match(iterator i) : first(i), second(i), matched(false) {}
767
768    bool operator==(const sub_match& that)const
769    {
770       return (first == that.first) && (second == that.second) && (matched == that.matched);
771    }
772    bool BOOST_REGEX_CALL operator !=(const sub_match& that)const
773    { return !(*this == that); }
774
775    difference_type BOOST_REGEX_CALL length()const
776    {
777       difference_type n = boost::re_detail::distance((iterator)first, (iterator)second);
778       return n;
779    }
780 };
781
782 #ifdef BOOST_OLD_REGEX_H
783 namespace re_detail{
784 template <class iterator, class charT>
785 int do_toi(iterator i, iterator j, char c, int radix)
786 {
787    std::string s(i, j);
788    char* p;
789    int result = std::strtol(s.c_str(), &p, radix);
790 #ifndef BOOST_NO_EXCEPTIONS
791    if(*p)throw bad_pattern("Bad sub-expression");
792 #endif
793    BOOST_REGEX_NOEH_ASSERT(0 == *p)
794    return result;
795 }
796
797 //
798 // helper:
799 template <class I, class charT>
800 int do_toi(I& i, I j, charT c)
801 {
802    int result = 0;
803    while((i != j) && (isdigit(*i)))
804    {
805       result = result*10 + (*i - '0');
806       ++i;
807    }
808    return result;
809 }
810 }
811
812
813 template <class iterator>
814 sub_match<iterator>::operator int()const
815 {
816    iterator i = first;
817    iterator j = second;
818 #ifndef BOOST_NO_EXCEPTIONS
819    if(i == j)throw bad_pattern("Bad sub-expression");
820 #endif
821    BOOST_REGEX_NOEH_ASSERT(i != j)
822    int neg = 1;
823    if((i != j) && (*i == '-'))
824    {
825       neg = -1;
826       ++i;
827    }
828    neg *= re_detail::do_toi(i, j, *i);
829 #ifndef BOOST_NO_EXCEPTIONS
830    if(i != j)throw bad_pattern("Bad sub-expression");
831 #endif
832    BOOST_REGEX_NOEH_ASSERT(i == j)
833    return neg;
834 }
835 template <class iterator>
836 sub_match<iterator>::operator unsigned int()const
837 {
838    iterator i = first;
839    iterator j = second;
840 #ifndef BOOST_NO_EXCEPTIONS
841    if(i == j)
842       throw bad_pattern("Bad sub-expression");
843 #endif
844    BOOST_REGEX_NOEH_ASSERT(i != j)
845    return re_detail::do_toi(i, j, *first);
846 }
847 #endif
848
849 namespace re_detail{
850
851 template <class iterator, class Allocator = BOOST_DEFAULT_ALLOCATOR(typename def_alloc_param_traits<iterator>::type) >
852 class match_results_base
853 {
854 public:
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;
861 #else
862    typedef std::ptrdiff_t                                            difference_type;
863    typedef typename re_detail::regex_iterator_traits<iterator>::value_type char_type;
864 #endif
865    typedef sub_match<iterator>                                       value_type;
866    typedef iterator                                                  iterator_type;
867
868 protected:
869    typedef typename boost::detail::rebind_allocator<char, Allocator>::type c_alloc;
870    
871    struct c_reference : public c_alloc
872    {
873       std::size_t cmatches;
874       unsigned count;
875       sub_match<iterator> head, tail, null;
876       unsigned int lines;
877       iterator line_pos, base;
878       c_reference(const Allocator& a)
879          : c_alloc(a), cmatches(0), count(0), lines(0) {  }
880
881       bool operator==(const c_reference& that)const
882       {
883          return (cmatches == that.cmatches) &&
884                   (count == that.count) &&
885                   (head == that.head) &&
886                   (tail == that.tail) &&
887                   (lines == that.lines) &&
888                   (base == that.base);
889       }
890       bool operator!=(const c_reference& that)const
891       { return !(*this == that); }
892    };
893
894    c_reference* ref;
895
896    void BOOST_REGEX_CALL cow();
897
898    // protected contructor for derived class...
899    match_results_base(bool){}
900    void BOOST_REGEX_CALL m_free();
901
902 public:
903
904    match_results_base(const Allocator& a = Allocator());
905
906    match_results_base(const match_results_base& m)
907    {
908       ref = m.ref;
909       ++(ref->count);
910    }
911
912    match_results_base& BOOST_REGEX_CALL operator=(const match_results_base& m);
913
914    ~match_results_base()
915    {
916       m_free();
917    }
918
919    size_type BOOST_REGEX_CALL size()const
920    {
921       //return (*this)[0].matched ? ref->cmatches : 0;
922       return ref->cmatches;
923    }
924
925    const sub_match<iterator>& BOOST_REGEX_CALL operator[](int n) const
926    {
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;
930    }
931
932    Allocator BOOST_REGEX_CALL allocator()const;
933
934    difference_type BOOST_REGEX_CALL length(unsigned int sub = 0)const
935    {
936       jm_assert(ref->cmatches);
937       const sub_match<iterator>& m = (*this)[sub];
938       if(m.matched == false)
939          return 0;
940       difference_type n = boost::re_detail::distance((iterator)m.first, (iterator)m.second);
941       return n;
942    }
943
944    std::basic_string<char_type> str(int i)const
945    {
946       return static_cast<std::basic_string<char_type> >((*this)[i]);
947    }
948
949    unsigned int BOOST_REGEX_CALL line()const
950    {
951       return ref->lines;
952    }
953
954    difference_type BOOST_REGEX_CALL position(unsigned int sub = 0)const
955    {
956       jm_assert(ref->cmatches);
957       const sub_match<iterator>& s = (*this)[sub];
958       if(s.matched == false)
959          return -1;
960       difference_type n = boost::re_detail::distance((iterator)(ref->base), (iterator)(s.first));
961       return n;
962    }
963
964    iterator BOOST_REGEX_CALL line_start()const
965    {
966       return ref->line_pos;
967    }
968
969    void swap(match_results_base& that)
970    {
971       c_reference* t = that.ref;
972       that.ref = ref;
973       ref = t;
974    }
975
976    bool operator==(const match_results_base& that)const;
977    bool operator<(const match_results_base& that)const
978    { return position() < that.position(); }
979
980    friend class match_results<iterator, Allocator>;
981
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);
986
987    void BOOST_REGEX_CALL set_first(iterator i);
988    void BOOST_REGEX_CALL set_first(iterator i, std::size_t pos);
989
990    void BOOST_REGEX_CALL set_second(iterator i)
991    {
992       cow();
993       ((sub_match<iterator>*)(ref+1))->second = i;
994       ((sub_match<iterator>*)(ref+1))->matched = true;
995       ref->tail.first = i;
996       ref->tail.matched = (ref->tail.first == ref->tail.second) ? false : true;
997    }
998
999    void BOOST_REGEX_CALL set_second(iterator i, std::size_t pos, bool m = true)
1000    {
1001       cow();
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;
1004       if(pos == 0)
1005       {
1006          ref->tail.first = i;
1007          ref->tail.matched = (ref->tail.first == ref->tail.second) ? false : true;
1008       }
1009    }
1010
1011    void BOOST_REGEX_CALL set_line(unsigned int i, iterator pos)
1012    {
1013       ref->lines = i;
1014       ref->line_pos = pos;
1015    }
1016
1017    void BOOST_REGEX_CALL set_base(iterator pos)
1018    {
1019       ref->base = pos;
1020    }
1021 };
1022
1023 template <class iterator, class Allocator>
1024 void BOOST_REGEX_CALL match_results_base<iterator, Allocator>::set_first(iterator i)
1025 {
1026    cow();
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;
1031    p1->first = i;
1032    p1->matched = false;
1033    ++p1;
1034    while(p1 != p2)
1035    {
1036       p1->matched = false;
1037       p1->first = ref->tail.second;
1038       p1->second = ref->tail.second;
1039       ++p1;
1040    }
1041 }
1042
1043 template <class iterator, class Allocator>
1044 void BOOST_REGEX_CALL match_results_base<iterator, Allocator>::set_first(iterator i, std::size_t pos)
1045 {
1046    cow();
1047    ((sub_match<iterator>*)((char*)ref + sizeof(c_reference) + sizeof(sub_match<iterator>) * pos))->first = i;
1048    if(pos == 0)
1049    {
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;
1054       p1->first = i;
1055       p1->matched = false;
1056       ++p1;
1057       while(p1 != p2)
1058       {
1059          p1->matched = false;
1060          p1->first = ref->tail.second;
1061          p1->second = ref->tail.second;
1062          ++p1;
1063       }
1064    }
1065 }
1066
1067
1068 template <class iterator, class Allocator>
1069 match_results_base<iterator, Allocator>::match_results_base(const Allocator& a)
1070 {
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
1074    try
1075    {
1076 #endif
1077       new (ref) c_reference(a);
1078       ref->cmatches = 1;
1079       ref->count = 1;
1080       // construct the sub_match<iterator>:
1081 #ifndef BOOST_NO_EXCEPTIONS
1082       try
1083       {
1084 #endif
1085          new ((sub_match<iterator>*)(ref+1)) sub_match<iterator>();
1086 #ifndef BOOST_NO_EXCEPTIONS
1087       }
1088       catch(...)
1089       {
1090          ::boost::re_detail::pointer_destroy(ref);
1091          throw;
1092       }
1093    }
1094    catch(...)
1095    {
1096       c_alloc(a).deallocate((char*)(void*)ref, sizeof(sub_match<iterator>) + sizeof(c_reference));
1097       throw;
1098    }
1099 #endif
1100 }
1101
1102 template <class iterator, class Allocator>
1103 Allocator BOOST_REGEX_CALL match_results_base<iterator, Allocator>::allocator()const
1104 {
1105   return *((c_alloc*)ref);
1106 }
1107
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)
1110 {
1111    if(ref != m.ref)
1112    {
1113       m_free();
1114       ref = m.ref;
1115       ++(ref->count);
1116    }
1117    return *this;
1118 }
1119
1120
1121 template <class iterator, class Allocator>
1122 void BOOST_REGEX_CALL match_results_base<iterator, Allocator>::m_free()
1123 {
1124    if(--(ref->count) == 0)
1125    {
1126       c_alloc a(*ref);
1127       sub_match<iterator>* p1, *p2;
1128       p1 = (sub_match<iterator>*)(ref+1);
1129       p2 = p1 + ref->cmatches;
1130       while(p1 != p2)
1131       {
1132          ::boost::re_detail::pointer_destroy(p1);
1133          ++p1;
1134       }
1135       ::boost::re_detail::pointer_destroy(ref);
1136       a.deallocate((char*)(void*)ref, sizeof(sub_match<iterator>) * ref->cmatches + sizeof(c_reference));
1137    }
1138 }
1139
1140 template <class iterator, class Allocator>
1141 bool match_results_base<iterator, Allocator>::operator==(const match_results_base<iterator, Allocator>& that)const
1142 {
1143    if(*ref != *(that.ref))
1144       return false;
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);
1148    while(p1 != p2)
1149    {
1150       if(*p1 != *p3)
1151          return false;
1152       ++p1;
1153       ++p3;
1154    }
1155    return true;
1156 }
1157
1158 template <class iterator, class Allocator>
1159 void BOOST_REGEX_CALL match_results_base<iterator, Allocator>::set_size(size_type n)
1160 {
1161    if(ref->cmatches != n)
1162    {
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
1166       try
1167       {
1168 #endif
1169          new (newref) c_reference(*ref);
1170          newref->count = 1;
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
1176          try
1177          {
1178 #endif
1179             while(p1 != p2)
1180             {
1181                new (p1) sub_match<iterator>();
1182                ++p1;
1183             }
1184             m_free();
1185 #ifndef BOOST_NO_EXCEPTIONS
1186          }
1187          catch(...)
1188          {
1189             p2 = (sub_match<iterator>*)(newref+1);
1190             while(p2 != p1)
1191             {
1192                ::boost::re_detail::pointer_destroy(p2);
1193                ++p2;
1194             }
1195             ::boost::re_detail::pointer_destroy(ref);
1196             throw;
1197          }
1198 #endif
1199          ref = newref;
1200 #ifndef BOOST_NO_EXCEPTIONS
1201       }
1202       catch(...)
1203       {
1204          ref->deallocate((char*)(void*)newref, sizeof(sub_match<iterator>) * n + sizeof(c_reference));
1205          throw;
1206       }
1207 #endif
1208    }
1209 }
1210
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)
1213 {
1214    if(ref->cmatches != n)
1215    {
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
1219       try{
1220 #endif
1221          new (newref) c_reference(*ref);
1222          newref->count = 1;
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
1227          try
1228          {
1229 #endif
1230             while(p1 != p2)
1231             {
1232                new (p1) sub_match<iterator>(j);
1233                ++p1;
1234             }
1235             m_free();
1236 #ifndef BOOST_NO_EXCEPTIONS
1237          }
1238          catch(...)
1239          { 
1240             p2 = (sub_match<iterator>*)(newref+1);
1241             while(p2 != p1)
1242             {
1243                ::boost::re_detail::pointer_destroy(p2);
1244                ++p2;
1245             }
1246             ::boost::re_detail::pointer_destroy(ref);
1247             throw; 
1248          }
1249 #endif
1250          ref = newref;
1251 #ifndef BOOST_NO_EXCEPTIONS
1252       }
1253       catch(...)
1254       { 
1255          ref->deallocate((char*)(void*)newref, sizeof(sub_match<iterator>) * n + sizeof(c_reference)); 
1256          throw; 
1257       }
1258 #endif
1259    }
1260    else
1261    {
1262       cow();
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;
1267       while(p1 != p2)
1268       {
1269          p1->first = j;
1270          p1->second = j;
1271          p1->matched = false;
1272          ++p1;
1273       }                                 
1274    }
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;
1280 }
1281
1282 template <class iterator, class Allocator>
1283 inline void BOOST_REGEX_CALL match_results_base<iterator, Allocator>::init_fail(iterator i, iterator j)
1284 {
1285    set_size(ref->cmatches, i, j);
1286 }
1287
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)
1290 {
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;
1299    std::size_t i;
1300    for(i = 0; i < ref->cmatches; ++i)
1301    {
1302       //
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;
1308
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)))
1312          break;
1313       if((p1->matched == true) && (p2->matched == false))
1314          return;
1315       ++p1;
1316       ++p2;
1317    }
1318    if(i == ref->cmatches)
1319       return;
1320    if(base2 < base1)
1321       *this = m;
1322    else if((len2 > len1) || ((p1->matched == false) && (p2->matched == true)) )
1323       *this = m;
1324 }
1325
1326 template <class iterator, class Allocator>
1327 void BOOST_REGEX_CALL match_results_base<iterator, Allocator>::cow()
1328 {
1329    if(ref->count > 1)
1330    {
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
1334       try{
1335 #endif
1336          new (newref) c_reference(*ref);
1337          newref->count = 1;
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
1342          try{
1343 #endif
1344             while(p1 != p2)
1345             {
1346                new (p1) sub_match<iterator>(*p3);
1347                ++p1;
1348                ++p3;
1349             }
1350 #ifndef BOOST_NO_EXCEPTIONS
1351          }
1352          catch(...)
1353          { 
1354             p2 = (sub_match<iterator>*)(newref+1);
1355             while(p2 != p1)
1356             {
1357                ::boost::re_detail::pointer_destroy(p2);
1358                ++p2;
1359             }
1360             ::boost::re_detail::pointer_destroy(ref);
1361             throw; 
1362          }
1363 #endif
1364       --(ref->count);
1365       ref = newref;
1366 #ifndef BOOST_NO_EXCEPTIONS
1367       }
1368       catch(...)
1369       { 
1370          ref->deallocate((char*)(void*)newref, sizeof(sub_match<iterator>) * ref->cmatches + sizeof(c_reference)); 
1371          throw; 
1372       }
1373 #endif
1374    }
1375 }
1376
1377 } // namespace re_detail
1378
1379 //
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
1384
1385 template <class iterator, class Allocator>
1386 class match_results : public re_detail::match_results_base<iterator, Allocator>
1387 {
1388    typedef re_detail::match_results_base<iterator, Allocator> base_type;
1389 public:
1390
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;
1397
1398    explicit match_results(const Allocator& a = Allocator())
1399       : re_detail::match_results_base<iterator, Allocator>(a){}
1400
1401    match_results(const re_detail::match_results_base<iterator, Allocator>& m)
1402       : re_detail::match_results_base<iterator, Allocator>(m){}
1403
1404    match_results& operator=(const re_detail::match_results_base<iterator, Allocator>& m)
1405    {
1406       // shallow copy
1407       base_type::operator=(m);
1408       return *this;
1409    }
1410
1411    match_results(const match_results& m);
1412    match_results& operator=(const match_results& m);
1413    //
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:
1418    //
1419    size_type size()const
1420    { return static_cast<const base_type*>(this)->size(); }
1421
1422    const sub_match<iterator>& operator[](int n) const
1423    { return (*static_cast<const base_type*>(this))[n]; }
1424
1425    Allocator allocator()const
1426    { return static_cast<const base_type*>(this)->allocator(); }
1427
1428    difference_type length(int sub = 0)const
1429    { return static_cast<const base_type*>(this)->length(sub); }
1430
1431    difference_type position(unsigned int sub = 0)const
1432    { return static_cast<const base_type*>(this)->position(sub); }
1433
1434    unsigned int line()const
1435    { return static_cast<const base_type*>(this)->line(); }
1436
1437    iterator line_start()const
1438    { return static_cast<const base_type*>(this)->line_start(); }
1439
1440    std::basic_string<char_type> str(int sub = 0)const
1441    { return static_cast<const base_type*>(this)->str(sub); }
1442
1443    void swap(match_results& that)
1444    { static_cast<base_type*>(this)->swap(that); }
1445
1446    bool operator==(const match_results& that)const
1447    { return static_cast<const base_type&>(*this) == static_cast<const base_type&>(that); }
1448
1449    bool operator<(const match_results& that) const
1450    { return position() < that.position(); }
1451 };
1452
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)
1456 {
1457    this->ref =
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
1463    try{
1464 #endif
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
1471       try{
1472 #endif
1473          while(p1 != p2)
1474          {
1475             new (p1) sub_match<iterator>(*p3);
1476             ++p1;
1477             ++p3;
1478          }
1479 #ifndef BOOST_NO_EXCEPTIONS
1480       }
1481       catch(...)
1482       { 
1483          p2 = (sub_match<iterator>*)(this->ref+1);
1484          while(p2 != p1)
1485          {
1486             re_detail::pointer_destroy(p2);
1487             ++p2;
1488          }
1489          re_detail::pointer_destroy(this->ref);
1490          throw; 
1491       }
1492    }
1493    catch(...)
1494    { 
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));
1496       throw; 
1497    }
1498 #endif
1499 }
1500
1501 template <class iterator, class Allocator>
1502 match_results<iterator, Allocator>& match_results<iterator, Allocator>::operator=(const match_results<iterator, Allocator>& m)
1503 {
1504    match_results<iterator, Allocator> t(m);
1505    this->swap(t);
1506    return *this;
1507 }
1508
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, 
1512                           iterator last, 
1513                           const re_set_long* set_, 
1514                           const reg_expression<charT, traits_type, Allocator>& e);
1515 } // namepsace re_detail
1516
1517 #ifdef __BORLANDC__
1518   #pragma option pop
1519 #endif
1520
1521 } // namespace boost
1522
1523 #include <boost/regex/detail/regex_compile.hpp>
1524
1525 //
1526 // template instances:
1527 //
1528 #define BOOST_REGEX_CHAR_T char
1529 #ifdef BOOST_REGEX_NARROW_INSTANTIATE
1530 #  define BOOST_REGEX_INSTANTIATE
1531 #endif
1532 #include <boost/regex/detail/instances.hpp>
1533 #undef BOOST_REGEX_CHAR_T
1534 #ifdef BOOST_REGEX_INSTANTIATE
1535 #  undef BOOST_REGEX_INSTANTIATE
1536 #endif
1537
1538 #ifndef BOOST_NO_WREGEX
1539 #define BOOST_REGEX_CHAR_T wchar_t
1540 #ifdef BOOST_REGEX_WIDE_INSTANTIATE
1541 #  define BOOST_REGEX_INSTANTIATE
1542 #endif
1543 #include <boost/regex/detail/instances.hpp>
1544 #undef BOOST_REGEX_CHAR_T
1545 #ifdef BOOST_REGEX_INSTANTIATE
1546 #  undef BOOST_REGEX_INSTANTIATE
1547 #endif
1548 #endif
1549
1550
1551 namespace boost{
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;
1556 #endif
1557 #endif
1558
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;
1564 #endif
1565
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>
1570
1571
1572 #endif  // __cplusplus
1573
1574 #endif  // include
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601