3 * Copyright (c) 1998-2004 John Maddock
4 * Copyright 2011 Garmin Ltd. or its subsidiaries
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 #include <boost/type_traits/is_same.hpp>
23 #include <boost/functional/hash.hpp>
27 #pragma warning(disable: 4103)
29 #ifdef BOOST_HAS_ABI_HEADERS
30 # include BOOST_ABI_PREFIX
39 #pragma warning(disable : 4251 4231 4800)
41 #pragma warning(disable : 4660)
45 namespace BOOST_REGEX_DETAIL_NS{
48 // forward declaration, we will need this one later:
50 template <class charT, class traits>
51 class basic_regex_parser;
54 void bubble_down_one(I first, I last)
59 while((next != first) && (*next < *(next-1)))
61 (next-1)->swap(*next);
67 template <class Iterator>
68 inline int hash_value_from_capture_name(Iterator i, Iterator j)
70 std::size_t r = boost::hash_range(i, j);
71 r %= ((std::numeric_limits<int>::max)() - 10001);
73 return static_cast<int>(r);
76 class named_subexpressions
81 template <class charT>
82 name(const charT* i, const charT* j, int idx)
85 hash = hash_value_from_capture_name(i, j);
93 bool operator < (const name& other)const
95 return hash < other.hash;
97 bool operator == (const name& other)const
99 return hash == other.hash;
101 void swap(name& other)
103 std::swap(index, other.index);
104 std::swap(hash, other.hash);
108 typedef std::vector<name>::const_iterator const_iterator;
109 typedef std::pair<const_iterator, const_iterator> range_type;
111 named_subexpressions(){}
113 template <class charT>
114 void set_name(const charT* i, const charT* j, int index)
116 m_sub_names.push_back(name(i, j, index));
117 bubble_down_one(m_sub_names.begin(), m_sub_names.end());
119 template <class charT>
120 int get_id(const charT* i, const charT* j)const
123 typename std::vector<name>::const_iterator pos = std::lower_bound(m_sub_names.begin(), m_sub_names.end(), t);
124 if((pos != m_sub_names.end()) && (*pos == t))
130 template <class charT>
131 range_type equal_range(const charT* i, const charT* j)const
134 return std::equal_range(m_sub_names.begin(), m_sub_names.end(), t);
136 int get_id(int h)const
139 std::vector<name>::const_iterator pos = std::lower_bound(m_sub_names.begin(), m_sub_names.end(), t);
140 if((pos != m_sub_names.end()) && (*pos == t))
146 range_type equal_range(int h)const
149 return std::equal_range(m_sub_names.begin(), m_sub_names.end(), t);
152 std::vector<name> m_sub_names;
157 // represents the data we wish to expose to the matching algorithms.
159 template <class charT, class traits>
160 struct regex_data : public named_subexpressions
162 typedef regex_constants::syntax_option_type flag_type;
163 typedef std::size_t size_type;
165 regex_data(const ::boost::shared_ptr<
166 ::boost::regex_traits_wrapper<traits> >& t)
167 : m_ptraits(t), m_expression(0), m_expression_len(0), m_disable_match_any(false) {}
169 : m_ptraits(new ::boost::regex_traits_wrapper<traits>()), m_expression(0), m_expression_len(0), m_disable_match_any(false) {}
172 ::boost::regex_traits_wrapper<traits>
173 > m_ptraits; // traits class instance
174 flag_type m_flags; // flags with which we were compiled
175 int m_status; // error code (0 implies OK).
176 const charT* m_expression; // the original expression
177 std::ptrdiff_t m_expression_len; // the length of the original expression
178 size_type m_mark_count; // the number of marked sub-expressions
179 BOOST_REGEX_DETAIL_NS::re_syntax_base* m_first_state; // the first state of the machine
180 unsigned m_restart_type; // search optimisation type
181 unsigned char m_startmap[1 << CHAR_BIT]; // which characters can start a match
182 unsigned int m_can_be_null; // whether we can match a null string
183 BOOST_REGEX_DETAIL_NS::raw_storage m_data; // the buffer in which our states are constructed
184 typename traits::char_class_type m_word_mask; // mask used to determine if a character is a word character
187 std::size_t, std::size_t> > m_subs; // Position of sub-expressions within the *string*.
188 bool m_has_recursions; // whether we have recursive expressions;
189 bool m_disable_match_any; // when set we need to disable the match_any flag as it causes different/buggy behaviour.
192 // class basic_regex_implementation
193 // pimpl implementation class for basic_regex.
195 template <class charT, class traits>
196 class basic_regex_implementation
197 : public regex_data<charT, traits>
200 typedef regex_constants::syntax_option_type flag_type;
201 typedef std::ptrdiff_t difference_type;
202 typedef std::size_t size_type;
203 typedef typename traits::locale_type locale_type;
204 typedef const charT* const_iterator;
206 basic_regex_implementation(){}
207 basic_regex_implementation(const ::boost::shared_ptr<
208 ::boost::regex_traits_wrapper<traits> >& t)
209 : regex_data<charT, traits>(t) {}
210 void assign(const charT* arg_first,
211 const charT* arg_last,
214 regex_data<charT, traits>* pdat = this;
215 basic_regex_parser<charT, traits> parser(pdat);
216 parser.parse(arg_first, arg_last, f);
219 locale_type BOOST_REGEX_CALL imbue(locale_type l)
221 return this->m_ptraits->imbue(l);
223 locale_type BOOST_REGEX_CALL getloc()const
225 return this->m_ptraits->getloc();
227 std::basic_string<charT> BOOST_REGEX_CALL str()const
229 std::basic_string<charT> result;
230 if(this->m_status == 0)
231 result = std::basic_string<charT>(this->m_expression, this->m_expression_len);
234 const_iterator BOOST_REGEX_CALL expression()const
236 return this->m_expression;
238 std::pair<const_iterator, const_iterator> BOOST_REGEX_CALL subexpression(std::size_t n)const
240 const std::pair<std::size_t, std::size_t>& pi = this->m_subs.at(n);
241 std::pair<const_iterator, const_iterator> p(expression() + pi.first, expression() + pi.second);
246 const_iterator BOOST_REGEX_CALL begin()const
248 return (this->m_status ? 0 : this->m_expression);
250 const_iterator BOOST_REGEX_CALL end()const
252 return (this->m_status ? 0 : this->m_expression + this->m_expression_len);
254 flag_type BOOST_REGEX_CALL flags()const
256 return this->m_flags;
258 size_type BOOST_REGEX_CALL size()const
260 return this->m_expression_len;
262 int BOOST_REGEX_CALL status()const
264 return this->m_status;
266 size_type BOOST_REGEX_CALL mark_count()const
268 return this->m_mark_count - 1;
270 const BOOST_REGEX_DETAIL_NS::re_syntax_base* get_first_state()const
272 return this->m_first_state;
274 unsigned get_restart_type()const
276 return this->m_restart_type;
278 const unsigned char* get_map()const
280 return this->m_startmap;
282 const ::boost::regex_traits_wrapper<traits>& get_traits()const
284 return *(this->m_ptraits);
286 bool can_be_null()const
288 return this->m_can_be_null;
290 const regex_data<charT, traits>& get_data()const
292 basic_regex_implementation<charT, traits> const* p = this;
293 return *static_cast<const regex_data<charT, traits>*>(p);
297 } // namespace BOOST_REGEX_DETAIL_NS
299 // class basic_regex:
300 // represents the compiled
301 // regular expression:
304 #ifdef BOOST_REGEX_NO_FWD
305 template <class charT, class traits = regex_traits<charT> >
307 template <class charT, class traits >
309 class basic_regex : public regbase
313 typedef std::size_t traits_size_type;
314 typedef typename traits::string_type traits_string_type;
315 typedef charT char_type;
316 typedef traits traits_type;
318 typedef charT value_type;
319 typedef charT& reference;
320 typedef const charT& const_reference;
321 typedef const charT* const_iterator;
322 typedef const_iterator iterator;
323 typedef std::ptrdiff_t difference_type;
324 typedef std::size_t size_type;
325 typedef regex_constants::syntax_option_type flag_type;
327 // placeholder for actual locale type used by the
328 // traits class to localise *this.
329 typedef typename traits::locale_type locale_type;
332 explicit basic_regex(){}
333 explicit basic_regex(const charT* p, flag_type f = regex_constants::normal)
337 basic_regex(const charT* p1, const charT* p2, flag_type f = regex_constants::normal)
341 basic_regex(const charT* p, size_type len, flag_type f)
345 basic_regex(const basic_regex& that)
346 : m_pimpl(that.m_pimpl) {}
348 basic_regex& BOOST_REGEX_CALL operator=(const basic_regex& that)
352 basic_regex& BOOST_REGEX_CALL operator=(const charT* ptr)
359 basic_regex& assign(const basic_regex& that)
361 m_pimpl = that.m_pimpl;
364 basic_regex& assign(const charT* p, flag_type f = regex_constants::normal)
366 return assign(p, p + traits::length(p), f);
368 basic_regex& assign(const charT* p, size_type len, flag_type f)
370 return assign(p, p + len, f);
373 basic_regex& do_assign(const charT* p1,
377 basic_regex& assign(const charT* p1,
379 flag_type f = regex_constants::normal)
381 return do_assign(p1, p2, f);
383 #if !defined(BOOST_NO_MEMBER_TEMPLATES)
385 template <class ST, class SA>
386 unsigned int BOOST_REGEX_CALL set_expression(const std::basic_string<charT, ST, SA>& p, flag_type f = regex_constants::normal)
388 return set_expression(p.data(), p.data() + p.size(), f);
391 template <class ST, class SA>
392 explicit basic_regex(const std::basic_string<charT, ST, SA>& p, flag_type f = regex_constants::normal)
397 template <class InputIterator>
398 basic_regex(InputIterator arg_first, InputIterator arg_last, flag_type f = regex_constants::normal)
400 typedef typename traits::string_type seq_type;
401 seq_type a(arg_first, arg_last);
403 assign(static_cast<const charT*>(&*a.begin()), static_cast<const charT*>(&*a.begin() + a.size()), f);
405 assign(static_cast<const charT*>(0), static_cast<const charT*>(0), f);
408 template <class ST, class SA>
409 basic_regex& BOOST_REGEX_CALL operator=(const std::basic_string<charT, ST, SA>& p)
411 return assign(p.data(), p.data() + p.size(), regex_constants::normal);
414 template <class string_traits, class A>
415 basic_regex& BOOST_REGEX_CALL assign(
416 const std::basic_string<charT, string_traits, A>& s,
417 flag_type f = regex_constants::normal)
419 return assign(s.data(), s.data() + s.size(), f);
422 template <class InputIterator>
423 basic_regex& BOOST_REGEX_CALL assign(InputIterator arg_first,
424 InputIterator arg_last,
425 flag_type f = regex_constants::normal)
427 typedef typename traits::string_type seq_type;
428 seq_type a(arg_first, arg_last);
431 const charT* p1 = &*a.begin();
432 const charT* p2 = &*a.begin() + a.size();
433 return assign(p1, p2, f);
435 return assign(static_cast<const charT*>(0), static_cast<const charT*>(0), f);
438 unsigned int BOOST_REGEX_CALL set_expression(const std::basic_string<charT>& p, flag_type f = regex_constants::normal)
440 return set_expression(p.data(), p.data() + p.size(), f);
443 basic_regex(const std::basic_string<charT>& p, flag_type f = regex_constants::normal)
448 basic_regex& BOOST_REGEX_CALL operator=(const std::basic_string<charT>& p)
450 return assign(p.data(), p.data() + p.size(), regex_constants::normal);
453 basic_regex& BOOST_REGEX_CALL assign(
454 const std::basic_string<charT>& s,
455 flag_type f = regex_constants::normal)
457 return assign(s.data(), s.data() + s.size(), f);
464 locale_type BOOST_REGEX_CALL imbue(locale_type l);
465 locale_type BOOST_REGEX_CALL getloc()const
467 return m_pimpl.get() ? m_pimpl->getloc() : locale_type();
471 // retained for backwards compatibility only, "flags"
472 // is now the preferred name:
473 flag_type BOOST_REGEX_CALL getflags()const
477 flag_type BOOST_REGEX_CALL flags()const
479 return m_pimpl.get() ? m_pimpl->flags() : 0;
483 std::basic_string<charT> BOOST_REGEX_CALL str()const
485 return m_pimpl.get() ? m_pimpl->str() : std::basic_string<charT>();
488 // begin, end, subexpression:
489 std::pair<const_iterator, const_iterator> BOOST_REGEX_CALL subexpression(std::size_t n)const
492 boost::throw_exception(std::logic_error("Can't access subexpressions in an invalid regex."));
493 return m_pimpl->subexpression(n);
495 const_iterator BOOST_REGEX_CALL begin()const
497 return (m_pimpl.get() ? m_pimpl->begin() : 0);
499 const_iterator BOOST_REGEX_CALL end()const
501 return (m_pimpl.get() ? m_pimpl->end() : 0);
505 void BOOST_REGEX_CALL swap(basic_regex& that)throw()
507 m_pimpl.swap(that.m_pimpl);
511 size_type BOOST_REGEX_CALL size()const
513 return (m_pimpl.get() ? m_pimpl->size() : 0);
517 size_type BOOST_REGEX_CALL max_size()const
523 bool BOOST_REGEX_CALL empty()const
525 return (m_pimpl.get() ? 0 != m_pimpl->status() : true);
528 size_type BOOST_REGEX_CALL mark_count()const
530 return (m_pimpl.get() ? m_pimpl->mark_count() : 0);
535 return (m_pimpl.get() ? m_pimpl->status() : regex_constants::error_empty);
538 int BOOST_REGEX_CALL compare(const basic_regex& that) const
540 if(m_pimpl.get() == that.m_pimpl.get())
544 if(!that.m_pimpl.get())
546 if(status() != that.status())
547 return status() - that.status();
548 if(flags() != that.flags())
549 return flags() - that.flags();
550 return str().compare(that.str());
552 bool BOOST_REGEX_CALL operator==(const basic_regex& e)const
554 return compare(e) == 0;
556 bool BOOST_REGEX_CALL operator != (const basic_regex& e)const
558 return compare(e) != 0;
560 bool BOOST_REGEX_CALL operator<(const basic_regex& e)const
562 return compare(e) < 0;
564 bool BOOST_REGEX_CALL operator>(const basic_regex& e)const
566 return compare(e) > 0;
568 bool BOOST_REGEX_CALL operator<=(const basic_regex& e)const
570 return compare(e) <= 0;
572 bool BOOST_REGEX_CALL operator>=(const basic_regex& e)const
574 return compare(e) >= 0;
578 // The following are deprecated as public interfaces
579 // but are available for compatibility with earlier versions.
580 const charT* BOOST_REGEX_CALL expression()const
582 return (m_pimpl.get() && !m_pimpl->status() ? m_pimpl->expression() : 0);
584 unsigned int BOOST_REGEX_CALL set_expression(const charT* p1, const charT* p2, flag_type f = regex_constants::normal)
586 assign(p1, p2, f | regex_constants::no_except);
589 unsigned int BOOST_REGEX_CALL set_expression(const charT* p, flag_type f = regex_constants::normal)
591 assign(p, f | regex_constants::no_except);
594 unsigned int BOOST_REGEX_CALL error_code()const
599 // private access methods:
601 const BOOST_REGEX_DETAIL_NS::re_syntax_base* get_first_state()const
603 BOOST_ASSERT(0 != m_pimpl.get());
604 return m_pimpl->get_first_state();
606 unsigned get_restart_type()const
608 BOOST_ASSERT(0 != m_pimpl.get());
609 return m_pimpl->get_restart_type();
611 const unsigned char* get_map()const
613 BOOST_ASSERT(0 != m_pimpl.get());
614 return m_pimpl->get_map();
616 const ::boost::regex_traits_wrapper<traits>& get_traits()const
618 BOOST_ASSERT(0 != m_pimpl.get());
619 return m_pimpl->get_traits();
621 bool can_be_null()const
623 BOOST_ASSERT(0 != m_pimpl.get());
624 return m_pimpl->can_be_null();
626 const BOOST_REGEX_DETAIL_NS::regex_data<charT, traits>& get_data()const
628 BOOST_ASSERT(0 != m_pimpl.get());
629 return m_pimpl->get_data();
631 boost::shared_ptr<BOOST_REGEX_DETAIL_NS::named_subexpressions > get_named_subs()const
637 shared_ptr<BOOST_REGEX_DETAIL_NS::basic_regex_implementation<charT, traits> > m_pimpl;
641 // out of line members;
642 // these are the only members that mutate the basic_regex object,
643 // and are designed to provide the strong exception guarentee
644 // (in the event of a throw, the state of the object remains unchanged).
646 template <class charT, class traits>
647 basic_regex<charT, traits>& basic_regex<charT, traits>::do_assign(const charT* p1,
651 shared_ptr<BOOST_REGEX_DETAIL_NS::basic_regex_implementation<charT, traits> > temp;
654 temp = shared_ptr<BOOST_REGEX_DETAIL_NS::basic_regex_implementation<charT, traits> >(new BOOST_REGEX_DETAIL_NS::basic_regex_implementation<charT, traits>());
658 temp = shared_ptr<BOOST_REGEX_DETAIL_NS::basic_regex_implementation<charT, traits> >(new BOOST_REGEX_DETAIL_NS::basic_regex_implementation<charT, traits>(m_pimpl->m_ptraits));
660 temp->assign(p1, p2, f);
665 template <class charT, class traits>
666 typename basic_regex<charT, traits>::locale_type BOOST_REGEX_CALL basic_regex<charT, traits>::imbue(locale_type l)
668 shared_ptr<BOOST_REGEX_DETAIL_NS::basic_regex_implementation<charT, traits> > temp(new BOOST_REGEX_DETAIL_NS::basic_regex_implementation<charT, traits>());
669 locale_type result = temp->imbue(l);
677 template <class charT, class traits>
678 void swap(basic_regex<charT, traits>& e1, basic_regex<charT, traits>& e2)
683 #ifndef BOOST_NO_STD_LOCALE
684 template <class charT, class traits, class traits2>
685 std::basic_ostream<charT, traits>&
686 operator << (std::basic_ostream<charT, traits>& os,
687 const basic_regex<charT, traits2>& e)
689 return (os << e.str());
692 template <class traits>
693 std::ostream& operator << (std::ostream& os, const basic_regex<char, traits>& e)
695 return (os << e.str());
700 // class reg_expression:
701 // this is provided for backwards compatibility only,
702 // it is deprecated, no not use!
704 #ifdef BOOST_REGEX_NO_FWD
705 template <class charT, class traits = regex_traits<charT> >
707 template <class charT, class traits >
709 class reg_expression : public basic_regex<charT, traits>
712 typedef typename basic_regex<charT, traits>::flag_type flag_type;
713 typedef typename basic_regex<charT, traits>::size_type size_type;
714 explicit reg_expression(){}
715 explicit reg_expression(const charT* p, flag_type f = regex_constants::normal)
716 : basic_regex<charT, traits>(p, f){}
717 reg_expression(const charT* p1, const charT* p2, flag_type f = regex_constants::normal)
718 : basic_regex<charT, traits>(p1, p2, f){}
719 reg_expression(const charT* p, size_type len, flag_type f)
720 : basic_regex<charT, traits>(p, len, f){}
721 reg_expression(const reg_expression& that)
722 : basic_regex<charT, traits>(that) {}
724 reg_expression& BOOST_REGEX_CALL operator=(const reg_expression& that)
726 return this->assign(that);
729 #if !defined(BOOST_NO_MEMBER_TEMPLATES)
730 template <class ST, class SA>
731 explicit reg_expression(const std::basic_string<charT, ST, SA>& p, flag_type f = regex_constants::normal)
732 : basic_regex<charT, traits>(p, f)
736 template <class InputIterator>
737 reg_expression(InputIterator arg_first, InputIterator arg_last, flag_type f = regex_constants::normal)
738 : basic_regex<charT, traits>(arg_first, arg_last, f)
742 template <class ST, class SA>
743 reg_expression& BOOST_REGEX_CALL operator=(const std::basic_string<charT, ST, SA>& p)
749 explicit reg_expression(const std::basic_string<charT>& p, flag_type f = regex_constants::normal)
750 : basic_regex<charT, traits>(p, f)
754 reg_expression& BOOST_REGEX_CALL operator=(const std::basic_string<charT>& p)
764 #pragma warning (pop)
770 #pragma warning(push)
771 #pragma warning(disable: 4103)
773 #ifdef BOOST_HAS_ABI_HEADERS
774 # include BOOST_ABI_SUFFIX