]> git.lyx.org Git - lyx.git/blob - boost/boost/regex/v4/basic_regex.hpp
Update in-source boost to latest updates from boost 1.34 branch.
[lyx.git] / boost / boost / regex / v4 / basic_regex.hpp
1 /*
2  *
3  * Copyright (c) 1998-2004
4  * John Maddock
5  *
6  * Distributed under the Boost Software License, Version 1.0.
7  * (See accompanying file LICENSE_1_0.txt or copy at
8  * http://www.boost.org/LICENSE_1_0.txt)
9  *
10  */
11
12  /*
13   *   LOCATION:    see http://www.boost.org/ for most recent version.
14   *   FILE         basic_regex.cpp
15   *   VERSION      see <boost/version.hpp>
16   *   DESCRIPTION: Declares template class basic_regex.
17   */
18
19 #ifndef BOOST_REGEX_V4_BASIC_REGEX_HPP
20 #define BOOST_REGEX_V4_BASIC_REGEX_HPP
21
22 #ifdef BOOST_HAS_ABI_HEADERS
23 #  include BOOST_ABI_PREFIX
24 #endif
25
26 namespace boost{
27 #ifdef BOOST_MSVC
28 #pragma warning(push)
29 #pragma warning(disable : 4251 4231 4660)
30 #endif
31
32 namespace re_detail{
33
34 //
35 // forward declaration, we will need this one later:
36 //
37 template <class charT, class traits>
38 class basic_regex_parser;
39
40 //
41 // class regex_data:
42 // represents the data we wish to expose to the matching algorithms.
43 //
44 template <class charT, class traits>
45 struct regex_data
46 {
47    typedef regex_constants::syntax_option_type   flag_type;
48    typedef std::size_t                           size_type;  
49
50    regex_data(const ::boost::shared_ptr<
51       ::boost::regex_traits_wrapper<traits> >& t) 
52       : m_ptraits(t), m_expression(0), m_expression_len(0) {}
53    regex_data() 
54       : m_ptraits(new ::boost::regex_traits_wrapper<traits>()), m_expression(0), m_expression_len(0) {}
55
56    ::boost::shared_ptr<
57       ::boost::regex_traits_wrapper<traits>
58       >                        m_ptraits;                 // traits class instance
59    flag_type                   m_flags;                   // flags with which we were compiled
60    int                         m_status;                  // error code (0 implies OK).
61    const charT*                m_expression;              // the original expression
62    std::ptrdiff_t              m_expression_len;          // the length of the original expression
63    size_type                   m_mark_count;              // the number of marked sub-expressions
64    re_detail::re_syntax_base*  m_first_state;             // the first state of the machine
65    unsigned                    m_restart_type;            // search optimisation type
66    unsigned char               m_startmap[1 << CHAR_BIT]; // which characters can start a match
67    unsigned int                m_can_be_null;             // whether we can match a null string
68    re_detail::raw_storage      m_data;                    // the buffer in which our states are constructed
69    typename traits::char_class_type    m_word_mask;       // mask used to determine if a character is a word character
70 };
71 //
72 // class basic_regex_implementation
73 // pimpl implementation class for basic_regex.
74 //
75 template <class charT, class traits>
76 class basic_regex_implementation
77    : public regex_data<charT, traits>
78 {
79 public:
80    typedef regex_constants::syntax_option_type   flag_type;
81    typedef std::ptrdiff_t                        difference_type;
82    typedef std::size_t                           size_type; 
83    typedef typename traits::locale_type          locale_type;
84    typedef const charT*                          const_iterator;
85
86    basic_regex_implementation(){}
87    basic_regex_implementation(const ::boost::shared_ptr<
88       ::boost::regex_traits_wrapper<traits> >& t)
89       : regex_data<charT, traits>(t) {}
90    void assign(const charT* arg_first,
91                           const charT* arg_last,
92                           flag_type f)
93    {
94       regex_data<charT, traits>* pdat = this;
95       basic_regex_parser<charT, traits> parser(pdat);
96       parser.parse(arg_first, arg_last, f);
97    }
98
99    locale_type BOOST_REGEX_CALL imbue(locale_type l)
100    { 
101       return this->m_ptraits->imbue(l); 
102    }
103    locale_type BOOST_REGEX_CALL getloc()const
104    { 
105       return this->m_ptraits->getloc(); 
106    }
107    std::basic_string<charT> BOOST_REGEX_CALL str()const
108    {
109       std::basic_string<charT> result;
110       if(this->m_status == 0)
111          result = std::basic_string<charT>(this->m_expression, this->m_expression_len);
112       return result;
113    }
114    const_iterator BOOST_REGEX_CALL expression()const
115    {
116       return this->m_expression;
117    }
118    //
119    // begin, end:
120    const_iterator BOOST_REGEX_CALL begin()const
121    { 
122       return (!this->m_status ? 0 : this->m_expression); 
123    }
124    const_iterator BOOST_REGEX_CALL end()const
125    { 
126       return (!this->m_status ? 0 : this->m_expression + this->m_expression_len); 
127    }
128    flag_type BOOST_REGEX_CALL flags()const
129    {
130       return this->m_flags;
131    }
132    size_type BOOST_REGEX_CALL size()const
133    {
134       return this->m_expression_len;
135    }
136    int BOOST_REGEX_CALL status()const
137    {
138       return this->m_status;
139    }
140    size_type BOOST_REGEX_CALL mark_count()const
141    {
142       return this->m_mark_count;
143    }
144    const re_detail::re_syntax_base* get_first_state()const
145    {
146       return this->m_first_state;
147    }
148    unsigned get_restart_type()const
149    {
150       return this->m_restart_type;
151    }
152    const unsigned char* get_map()const
153    {
154       return this->m_startmap;
155    }
156    const ::boost::regex_traits_wrapper<traits>& get_traits()const
157    {
158       return *(this->m_ptraits);
159    }
160    bool can_be_null()const
161    {
162       return this->m_can_be_null;
163    }
164    const regex_data<charT, traits>& get_data()const
165    {
166       basic_regex_implementation<charT, traits> const* p = this;
167       return *static_cast<const regex_data<charT, traits>*>(p);
168    }
169 };
170
171 } // namespace re_detail
172 //
173 // class basic_regex:
174 // represents the compiled
175 // regular expression:
176 //
177
178 #ifdef BOOST_REGEX_NO_FWD
179 template <class charT, class traits = regex_traits<charT> >
180 #else
181 template <class charT, class traits >
182 #endif
183 class basic_regex : public regbase
184 {
185 public:
186    // typedefs:
187    typedef typename traits::size_type            traits_size_type;
188    typedef typename traits::string_type          traits_string_type;
189    typedef charT                                 char_type;
190    typedef traits                                traits_type;
191
192    typedef charT                                 value_type;
193    typedef charT&                                reference;
194    typedef const charT&                          const_reference;
195    typedef const charT*                          const_iterator;
196    typedef const_iterator                        iterator;
197    typedef std::ptrdiff_t                        difference_type;
198    typedef std::size_t                           size_type;   
199    typedef regex_constants::syntax_option_type   flag_type;
200    // locale_type
201    // placeholder for actual locale type used by the
202    // traits class to localise *this.
203    typedef typename traits::locale_type          locale_type;
204    
205 public:
206    explicit basic_regex(){}
207    explicit basic_regex(const charT* p, flag_type f = regex_constants::normal)
208    {
209       assign(p, f);
210    }
211    basic_regex(const charT* p1, const charT* p2, flag_type f = regex_constants::normal)
212    {
213       assign(p1, p2, f);
214    }
215    basic_regex(const charT* p, size_type len, flag_type f)
216    {
217       assign(p, len, f);
218    }
219    basic_regex(const basic_regex& that)
220       : m_pimpl(that.m_pimpl) {}
221    ~basic_regex(){}
222    basic_regex& BOOST_REGEX_CALL operator=(const basic_regex& that)
223    {
224       return assign(that);
225    }
226    basic_regex& BOOST_REGEX_CALL operator=(const charT* ptr)
227    {
228       return assign(ptr);
229    }
230
231    //
232    // assign:
233    basic_regex& assign(const basic_regex& that)
234    { 
235       m_pimpl = that.m_pimpl;
236       return *this; 
237    }
238    basic_regex& assign(const charT* p, flag_type f = regex_constants::normal)
239    {
240       return assign(p, p + traits::length(p), f);
241    }
242    basic_regex& assign(const charT* p, size_type len, flag_type f)
243    {
244       return assign(p, p + len, f);
245    }
246 private:
247    basic_regex& do_assign(const charT* p1,
248                           const charT* p2,
249                           flag_type f);
250 public:
251    basic_regex& assign(const charT* p1,
252                           const charT* p2,
253                           flag_type f = regex_constants::normal)
254    {
255       return do_assign(p1, p2, f);
256    }
257 #if !defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(__IBMCPP__)
258
259    template <class ST, class SA>
260    unsigned int BOOST_REGEX_CALL set_expression(const std::basic_string<charT, ST, SA>& p, flag_type f = regex_constants::normal)
261    { 
262       return set_expression(p.data(), p.data() + p.size(), f); 
263    }
264
265    template <class ST, class SA>
266    explicit basic_regex(const std::basic_string<charT, ST, SA>& p, flag_type f = regex_constants::normal)
267    { 
268       assign(p, f); 
269    }
270
271    template <class InputIterator>
272    basic_regex(InputIterator arg_first, InputIterator arg_last, flag_type f = regex_constants::normal)
273    {
274       typedef typename traits::string_type seq_type;
275       seq_type a(arg_first, arg_last);
276       if(a.size())
277          assign(&*a.begin(), &*a.begin() + a.size(), f);
278       else
279          assign(static_cast<const charT*>(0), static_cast<const charT*>(0), f);
280    }
281
282    template <class ST, class SA>
283    basic_regex& BOOST_REGEX_CALL operator=(const std::basic_string<charT, ST, SA>& p)
284    {
285       return assign(p.data(), p.data() + p.size(), regex_constants::normal);
286    }
287
288    template <class string_traits, class A>
289    basic_regex& BOOST_REGEX_CALL assign(
290        const std::basic_string<charT, string_traits, A>& s,
291        flag_type f = regex_constants::normal)
292    {
293       return assign(s.data(), s.data() + s.size(), f);
294    }
295
296    template <class InputIterator>
297    basic_regex& BOOST_REGEX_CALL assign(InputIterator arg_first,
298                           InputIterator arg_last,
299                           flag_type f = regex_constants::normal)
300    {
301       typedef typename traits::string_type seq_type;
302       seq_type a(arg_first, arg_last);
303       if(a.size())
304       {
305          const charT* p1 = &*a.begin();
306          const charT* p2 = &*a.begin() + a.size();
307          return assign(p1, p2, f);
308       }
309       return assign(static_cast<const charT*>(0), static_cast<const charT*>(0), f);
310    }
311 #else
312    unsigned int BOOST_REGEX_CALL set_expression(const std::basic_string<charT>& p, flag_type f = regex_constants::normal)
313    { 
314       return set_expression(p.data(), p.data() + p.size(), f); 
315    }
316
317    basic_regex(const std::basic_string<charT>& p, flag_type f = regex_constants::normal)
318    { 
319       assign(p, f); 
320    }
321
322    basic_regex& BOOST_REGEX_CALL operator=(const std::basic_string<charT>& p)
323    {
324       return assign(p.data(), p.data() + p.size(), regex_constants::normal);
325    }
326
327    basic_regex& BOOST_REGEX_CALL assign(
328        const std::basic_string<charT>& s,
329        flag_type f = regex_constants::normal)
330    {
331       return assign(s.data(), s.data() + s.size(), f);
332    }
333
334 #endif
335
336    //
337    // locale:
338    locale_type BOOST_REGEX_CALL imbue(locale_type l);
339    locale_type BOOST_REGEX_CALL getloc()const
340    { 
341       return m_pimpl.get() ? m_pimpl->getloc() : locale_type(); 
342    }
343    //
344    // getflags:
345    // retained for backwards compatibility only, "flags"
346    // is now the preferred name:
347    flag_type BOOST_REGEX_CALL getflags()const
348    { 
349       return flags();
350    }
351    flag_type BOOST_REGEX_CALL flags()const
352    { 
353       return m_pimpl.get() ? m_pimpl->flags() : 0;
354    }
355    //
356    // str:
357    std::basic_string<charT> BOOST_REGEX_CALL str()const
358    {
359       return m_pimpl.get() ? m_pimpl->str() : std::basic_string<charT>();
360    }
361    //
362    // begin, end:
363    const_iterator BOOST_REGEX_CALL begin()const
364    { 
365       return (m_pimpl.get() ? m_pimpl->begin() : 0); 
366    }
367    const_iterator BOOST_REGEX_CALL end()const
368    { 
369       return (m_pimpl.get() ? m_pimpl->end() : 0); 
370    }
371    //
372    // swap:
373    void BOOST_REGEX_CALL swap(basic_regex& that)throw()
374    {
375       m_pimpl.swap(that.m_pimpl);
376    }
377    //
378    // size:
379    size_type BOOST_REGEX_CALL size()const
380    { 
381       return (m_pimpl.get() ? m_pimpl->size() : 0); 
382    }
383    //
384    // max_size:
385    size_type BOOST_REGEX_CALL max_size()const
386    { 
387       return UINT_MAX; 
388    }
389    //
390    // empty:
391    bool BOOST_REGEX_CALL empty()const
392    { 
393       return (m_pimpl.get() ? 0 != m_pimpl->status() : true); 
394    }
395
396    size_type BOOST_REGEX_CALL mark_count()const 
397    { 
398       return (m_pimpl.get() ? m_pimpl->mark_count() : 0); 
399    }
400
401    int status()const
402    {
403       return (m_pimpl.get() ? m_pimpl->status() : regex_constants::error_empty);
404    }
405
406    int BOOST_REGEX_CALL compare(const basic_regex& that) const
407    {
408       if(m_pimpl.get() == that.m_pimpl.get())
409          return 0;
410       if(!m_pimpl.get())
411          return -1;
412       if(!that.m_pimpl.get())
413          return 1;
414       if(status() != that.status())
415          return status() - that.status();
416       if(flags() != that.flags())
417          return flags() - that.flags();
418       return str().compare(that.str());
419    }
420    bool BOOST_REGEX_CALL operator==(const basic_regex& e)const
421    { 
422       return compare(e) == 0; 
423    }
424    bool BOOST_REGEX_CALL operator != (const basic_regex& e)const
425    { 
426       return compare(e) != 0; 
427    }
428    bool BOOST_REGEX_CALL operator<(const basic_regex& e)const
429    { 
430       return compare(e) < 0; 
431    }
432    bool BOOST_REGEX_CALL operator>(const basic_regex& e)const
433    { 
434       return compare(e) > 0; 
435    }
436    bool BOOST_REGEX_CALL operator<=(const basic_regex& e)const
437    { 
438       return compare(e) <= 0; 
439    }
440    bool BOOST_REGEX_CALL operator>=(const basic_regex& e)const
441    { 
442       return compare(e) >= 0; 
443    }
444
445    //
446    // The following are deprecated as public interfaces
447    // but are available for compatibility with earlier versions.
448    const charT* BOOST_REGEX_CALL expression()const 
449    { 
450       return (m_pimpl.get() && !m_pimpl->status() ? m_pimpl->expression() : 0); 
451    }
452    unsigned int BOOST_REGEX_CALL set_expression(const charT* p1, const charT* p2, flag_type f = regex_constants::normal)
453    {
454       assign(p1, p2, f | regex_constants::no_except);
455       return status();
456    }
457    unsigned int BOOST_REGEX_CALL set_expression(const charT* p, flag_type f = regex_constants::normal) 
458    { 
459       assign(p, f | regex_constants::no_except); 
460       return status();
461    }
462    unsigned int BOOST_REGEX_CALL error_code()const
463    {
464       return status();
465    }
466    //
467    // private access methods:
468    //
469    const re_detail::re_syntax_base* get_first_state()const
470    {
471       BOOST_ASSERT(0 != m_pimpl.get());
472       return m_pimpl->get_first_state();
473    }
474    unsigned get_restart_type()const
475    {
476       BOOST_ASSERT(0 != m_pimpl.get());
477       return m_pimpl->get_restart_type();
478    }
479    const unsigned char* get_map()const
480    {
481       BOOST_ASSERT(0 != m_pimpl.get());
482       return m_pimpl->get_map();
483    }
484    const ::boost::regex_traits_wrapper<traits>& get_traits()const
485    {
486       BOOST_ASSERT(0 != m_pimpl.get());
487       return m_pimpl->get_traits();
488    }
489    bool can_be_null()const
490    {
491       BOOST_ASSERT(0 != m_pimpl.get());
492       return m_pimpl->can_be_null();
493    }
494    const re_detail::regex_data<charT, traits>& get_data()const
495    {
496       BOOST_ASSERT(0 != m_pimpl.get());
497       return m_pimpl->get_data();
498    }
499
500 private:
501    shared_ptr<re_detail::basic_regex_implementation<charT, traits> > m_pimpl;
502 };
503
504 //
505 // out of line members;
506 // these are the only members that mutate the basic_regex object,
507 // and are designed to provide the strong exception guarentee
508 // (in the event of a throw, the state of the object remains unchanged).
509 //
510 template <class charT, class traits>
511 basic_regex<charT, traits>& basic_regex<charT, traits>::do_assign(const charT* p1,
512                         const charT* p2,
513                         flag_type f)
514 {
515    shared_ptr<re_detail::basic_regex_implementation<charT, traits> > temp;
516    if(!m_pimpl.get())
517    {
518       temp = shared_ptr<re_detail::basic_regex_implementation<charT, traits> >(new re_detail::basic_regex_implementation<charT, traits>());
519    }
520    else
521    {
522       temp = shared_ptr<re_detail::basic_regex_implementation<charT, traits> >(new re_detail::basic_regex_implementation<charT, traits>(m_pimpl->m_ptraits));
523    }
524    temp->assign(p1, p2, f);
525    temp.swap(m_pimpl);
526    return *this;
527 }
528
529 template <class charT, class traits>
530 typename basic_regex<charT, traits>::locale_type BOOST_REGEX_CALL basic_regex<charT, traits>::imbue(locale_type l)
531
532    shared_ptr<re_detail::basic_regex_implementation<charT, traits> > temp(new re_detail::basic_regex_implementation<charT, traits>());
533    locale_type result = temp->imbue(l);
534    temp.swap(m_pimpl);
535    return result;
536 }
537
538 //
539 // non-members:
540 //
541 template <class charT, class traits>
542 void swap(basic_regex<charT, traits>& e1, basic_regex<charT, traits>& e2)
543 {
544    e1.swap(e2);
545 }
546
547 #ifndef BOOST_NO_STD_LOCALE
548 template <class charT, class traits, class traits2>
549 std::basic_ostream<charT, traits>& 
550    operator << (std::basic_ostream<charT, traits>& os, 
551                 const basic_regex<charT, traits2>& e)
552 {
553    return (os << e.str());
554 }
555 #else
556 template <class traits>
557 std::ostream& operator << (std::ostream& os, const basic_regex<char, traits>& e)
558 {
559    return (os << e.str());
560 }
561 #endif
562
563 //
564 // class reg_expression:
565 // this is provided for backwards compatibility only,
566 // it is deprecated, no not use!
567 //
568 #ifdef BOOST_REGEX_NO_FWD
569 template <class charT, class traits = regex_traits<charT> >
570 #else
571 template <class charT, class traits >
572 #endif
573 class reg_expression : public basic_regex<charT, traits>
574 {
575 public:
576    typedef typename basic_regex<charT, traits>::flag_type flag_type;
577    typedef typename basic_regex<charT, traits>::size_type size_type;
578    explicit reg_expression(){}
579    explicit reg_expression(const charT* p, flag_type f = regex_constants::normal)
580       : basic_regex<charT, traits>(p, f){}
581    reg_expression(const charT* p1, const charT* p2, flag_type f = regex_constants::normal)
582       : basic_regex<charT, traits>(p1, p2, f){}
583    reg_expression(const charT* p, size_type len, flag_type f)
584       : basic_regex<charT, traits>(p, len, f){}
585    reg_expression(const reg_expression& that)
586       : basic_regex<charT, traits>(that) {}
587    ~reg_expression(){}
588    reg_expression& BOOST_REGEX_CALL operator=(const reg_expression& that)
589    {
590       return this->assign(that);
591    }
592
593 #if !defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(__IBMCPP__)
594    template <class ST, class SA>
595    explicit reg_expression(const std::basic_string<charT, ST, SA>& p, flag_type f = regex_constants::normal)
596    : basic_regex<charT, traits>(p, f)
597    { 
598    }
599
600    template <class InputIterator>
601    reg_expression(InputIterator arg_first, InputIterator arg_last, flag_type f = regex_constants::normal)
602    : basic_regex<charT, traits>(arg_first, arg_last, f)
603    {
604    }
605
606    template <class ST, class SA>
607    reg_expression& BOOST_REGEX_CALL operator=(const std::basic_string<charT, ST, SA>& p)
608    {
609       this->assign(p);
610       return *this;
611    }
612 #else
613    explicit reg_expression(const std::basic_string<charT>& p, flag_type f = regex_constants::normal)
614    : basic_regex<charT, traits>(p, f)
615    { 
616    }
617
618    reg_expression& BOOST_REGEX_CALL operator=(const std::basic_string<charT>& p)
619    {
620       this->assign(p);
621       return *this;
622    }
623 #endif
624
625 };
626
627 #ifdef BOOST_MSVC
628 #pragma warning (pop)
629 #endif
630
631 } // namespace boost
632
633 #ifdef BOOST_HAS_ABI_HEADERS
634 #  include BOOST_ABI_SUFFIX
635 #endif
636
637 #endif
638