3 * Copyright (c) 1998-2004
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)
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.
19 #ifndef BOOST_REGEX_V4_BASIC_REGEX_HPP
20 #define BOOST_REGEX_V4_BASIC_REGEX_HPP
22 #ifdef BOOST_HAS_ABI_HEADERS
23 # include BOOST_ABI_PREFIX
29 #pragma warning(disable : 4251 4231 4660)
35 // forward declaration, we will need this one later:
37 template <class charT, class traits>
38 class basic_regex_parser;
42 // represents the data we wish to expose to the matching algorithms.
44 template <class charT, class traits>
47 typedef regex_constants::syntax_option_type flag_type;
48 typedef std::size_t size_type;
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) {}
54 : m_ptraits(new ::boost::regex_traits_wrapper<traits>()), m_expression(0), m_expression_len(0) {}
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
72 // class basic_regex_implementation
73 // pimpl implementation class for basic_regex.
75 template <class charT, class traits>
76 class basic_regex_implementation
77 : public regex_data<charT, traits>
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;
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,
94 regex_data<charT, traits>* pdat = this;
95 basic_regex_parser<charT, traits> parser(pdat);
96 parser.parse(arg_first, arg_last, f);
99 locale_type BOOST_REGEX_CALL imbue(locale_type l)
101 return this->m_ptraits->imbue(l);
103 locale_type BOOST_REGEX_CALL getloc()const
105 return this->m_ptraits->getloc();
107 std::basic_string<charT> BOOST_REGEX_CALL str()const
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);
114 const_iterator BOOST_REGEX_CALL expression()const
116 return this->m_expression;
120 const_iterator BOOST_REGEX_CALL begin()const
122 return (!this->m_status ? 0 : this->m_expression);
124 const_iterator BOOST_REGEX_CALL end()const
126 return (!this->m_status ? 0 : this->m_expression + this->m_expression_len);
128 flag_type BOOST_REGEX_CALL flags()const
130 return this->m_flags;
132 size_type BOOST_REGEX_CALL size()const
134 return this->m_expression_len;
136 int BOOST_REGEX_CALL status()const
138 return this->m_status;
140 size_type BOOST_REGEX_CALL mark_count()const
142 return this->m_mark_count;
144 const re_detail::re_syntax_base* get_first_state()const
146 return this->m_first_state;
148 unsigned get_restart_type()const
150 return this->m_restart_type;
152 const unsigned char* get_map()const
154 return this->m_startmap;
156 const ::boost::regex_traits_wrapper<traits>& get_traits()const
158 return *(this->m_ptraits);
160 bool can_be_null()const
162 return this->m_can_be_null;
164 const regex_data<charT, traits>& get_data()const
166 basic_regex_implementation<charT, traits> const* p = this;
167 return *static_cast<const regex_data<charT, traits>*>(p);
171 } // namespace re_detail
173 // class basic_regex:
174 // represents the compiled
175 // regular expression:
178 #ifdef BOOST_REGEX_NO_FWD
179 template <class charT, class traits = regex_traits<charT> >
181 template <class charT, class traits >
183 class basic_regex : public regbase
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;
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;
201 // placeholder for actual locale type used by the
202 // traits class to localise *this.
203 typedef typename traits::locale_type locale_type;
206 explicit basic_regex(){}
207 explicit basic_regex(const charT* p, flag_type f = regex_constants::normal)
211 basic_regex(const charT* p1, const charT* p2, flag_type f = regex_constants::normal)
215 basic_regex(const charT* p, size_type len, flag_type f)
219 basic_regex(const basic_regex& that)
220 : m_pimpl(that.m_pimpl) {}
222 basic_regex& BOOST_REGEX_CALL operator=(const basic_regex& that)
226 basic_regex& BOOST_REGEX_CALL operator=(const charT* ptr)
233 basic_regex& assign(const basic_regex& that)
235 m_pimpl = that.m_pimpl;
238 basic_regex& assign(const charT* p, flag_type f = regex_constants::normal)
240 return assign(p, p + traits::length(p), f);
242 basic_regex& assign(const charT* p, size_type len, flag_type f)
244 return assign(p, p + len, f);
247 basic_regex& do_assign(const charT* p1,
251 basic_regex& assign(const charT* p1,
253 flag_type f = regex_constants::normal)
255 return do_assign(p1, p2, f);
257 #if !defined(BOOST_NO_MEMBER_TEMPLATES) && !defined(__IBMCPP__)
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)
262 return set_expression(p.data(), p.data() + p.size(), f);
265 template <class ST, class SA>
266 explicit basic_regex(const std::basic_string<charT, ST, SA>& p, flag_type f = regex_constants::normal)
271 template <class InputIterator>
272 basic_regex(InputIterator arg_first, InputIterator arg_last, flag_type f = regex_constants::normal)
274 typedef typename traits::string_type seq_type;
275 seq_type a(arg_first, arg_last);
277 assign(&*a.begin(), &*a.begin() + a.size(), f);
279 assign(static_cast<const charT*>(0), static_cast<const charT*>(0), f);
282 template <class ST, class SA>
283 basic_regex& BOOST_REGEX_CALL operator=(const std::basic_string<charT, ST, SA>& p)
285 return assign(p.data(), p.data() + p.size(), regex_constants::normal);
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)
293 return assign(s.data(), s.data() + s.size(), f);
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)
301 typedef typename traits::string_type seq_type;
302 seq_type a(arg_first, arg_last);
305 const charT* p1 = &*a.begin();
306 const charT* p2 = &*a.begin() + a.size();
307 return assign(p1, p2, f);
309 return assign(static_cast<const charT*>(0), static_cast<const charT*>(0), f);
312 unsigned int BOOST_REGEX_CALL set_expression(const std::basic_string<charT>& p, flag_type f = regex_constants::normal)
314 return set_expression(p.data(), p.data() + p.size(), f);
317 basic_regex(const std::basic_string<charT>& p, flag_type f = regex_constants::normal)
322 basic_regex& BOOST_REGEX_CALL operator=(const std::basic_string<charT>& p)
324 return assign(p.data(), p.data() + p.size(), regex_constants::normal);
327 basic_regex& BOOST_REGEX_CALL assign(
328 const std::basic_string<charT>& s,
329 flag_type f = regex_constants::normal)
331 return assign(s.data(), s.data() + s.size(), f);
338 locale_type BOOST_REGEX_CALL imbue(locale_type l);
339 locale_type BOOST_REGEX_CALL getloc()const
341 return m_pimpl.get() ? m_pimpl->getloc() : locale_type();
345 // retained for backwards compatibility only, "flags"
346 // is now the preferred name:
347 flag_type BOOST_REGEX_CALL getflags()const
351 flag_type BOOST_REGEX_CALL flags()const
353 return m_pimpl.get() ? m_pimpl->flags() : 0;
357 std::basic_string<charT> BOOST_REGEX_CALL str()const
359 return m_pimpl.get() ? m_pimpl->str() : std::basic_string<charT>();
363 const_iterator BOOST_REGEX_CALL begin()const
365 return (m_pimpl.get() ? m_pimpl->begin() : 0);
367 const_iterator BOOST_REGEX_CALL end()const
369 return (m_pimpl.get() ? m_pimpl->end() : 0);
373 void BOOST_REGEX_CALL swap(basic_regex& that)throw()
375 m_pimpl.swap(that.m_pimpl);
379 size_type BOOST_REGEX_CALL size()const
381 return (m_pimpl.get() ? m_pimpl->size() : 0);
385 size_type BOOST_REGEX_CALL max_size()const
391 bool BOOST_REGEX_CALL empty()const
393 return (m_pimpl.get() ? 0 != m_pimpl->status() : true);
396 size_type BOOST_REGEX_CALL mark_count()const
398 return (m_pimpl.get() ? m_pimpl->mark_count() : 0);
403 return (m_pimpl.get() ? m_pimpl->status() : regex_constants::error_empty);
406 int BOOST_REGEX_CALL compare(const basic_regex& that) const
408 if(m_pimpl.get() == that.m_pimpl.get())
412 if(!that.m_pimpl.get())
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());
420 bool BOOST_REGEX_CALL operator==(const basic_regex& e)const
422 return compare(e) == 0;
424 bool BOOST_REGEX_CALL operator != (const basic_regex& e)const
426 return compare(e) != 0;
428 bool BOOST_REGEX_CALL operator<(const basic_regex& e)const
430 return compare(e) < 0;
432 bool BOOST_REGEX_CALL operator>(const basic_regex& e)const
434 return compare(e) > 0;
436 bool BOOST_REGEX_CALL operator<=(const basic_regex& e)const
438 return compare(e) <= 0;
440 bool BOOST_REGEX_CALL operator>=(const basic_regex& e)const
442 return compare(e) >= 0;
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
450 return (m_pimpl.get() && !m_pimpl->status() ? m_pimpl->expression() : 0);
452 unsigned int BOOST_REGEX_CALL set_expression(const charT* p1, const charT* p2, flag_type f = regex_constants::normal)
454 assign(p1, p2, f | regex_constants::no_except);
457 unsigned int BOOST_REGEX_CALL set_expression(const charT* p, flag_type f = regex_constants::normal)
459 assign(p, f | regex_constants::no_except);
462 unsigned int BOOST_REGEX_CALL error_code()const
467 // private access methods:
469 const re_detail::re_syntax_base* get_first_state()const
471 BOOST_ASSERT(0 != m_pimpl.get());
472 return m_pimpl->get_first_state();
474 unsigned get_restart_type()const
476 BOOST_ASSERT(0 != m_pimpl.get());
477 return m_pimpl->get_restart_type();
479 const unsigned char* get_map()const
481 BOOST_ASSERT(0 != m_pimpl.get());
482 return m_pimpl->get_map();
484 const ::boost::regex_traits_wrapper<traits>& get_traits()const
486 BOOST_ASSERT(0 != m_pimpl.get());
487 return m_pimpl->get_traits();
489 bool can_be_null()const
491 BOOST_ASSERT(0 != m_pimpl.get());
492 return m_pimpl->can_be_null();
494 const re_detail::regex_data<charT, traits>& get_data()const
496 BOOST_ASSERT(0 != m_pimpl.get());
497 return m_pimpl->get_data();
501 shared_ptr<re_detail::basic_regex_implementation<charT, traits> > m_pimpl;
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).
510 template <class charT, class traits>
511 basic_regex<charT, traits>& basic_regex<charT, traits>::do_assign(const charT* p1,
515 shared_ptr<re_detail::basic_regex_implementation<charT, traits> > temp;
518 temp = shared_ptr<re_detail::basic_regex_implementation<charT, traits> >(new re_detail::basic_regex_implementation<charT, traits>());
522 temp = shared_ptr<re_detail::basic_regex_implementation<charT, traits> >(new re_detail::basic_regex_implementation<charT, traits>(m_pimpl->m_ptraits));
524 temp->assign(p1, p2, f);
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)
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);
541 template <class charT, class traits>
542 void swap(basic_regex<charT, traits>& e1, basic_regex<charT, traits>& e2)
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)
553 return (os << e.str());
556 template <class traits>
557 std::ostream& operator << (std::ostream& os, const basic_regex<char, traits>& e)
559 return (os << e.str());
564 // class reg_expression:
565 // this is provided for backwards compatibility only,
566 // it is deprecated, no not use!
568 #ifdef BOOST_REGEX_NO_FWD
569 template <class charT, class traits = regex_traits<charT> >
571 template <class charT, class traits >
573 class reg_expression : public basic_regex<charT, traits>
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) {}
588 reg_expression& BOOST_REGEX_CALL operator=(const reg_expression& that)
590 return this->assign(that);
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)
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)
606 template <class ST, class SA>
607 reg_expression& BOOST_REGEX_CALL operator=(const std::basic_string<charT, ST, SA>& p)
613 explicit reg_expression(const std::basic_string<charT>& p, flag_type f = regex_constants::normal)
614 : basic_regex<charT, traits>(p, f)
618 reg_expression& BOOST_REGEX_CALL operator=(const std::basic_string<charT>& p)
628 #pragma warning (pop)
633 #ifdef BOOST_HAS_ABI_HEADERS
634 # include BOOST_ABI_SUFFIX