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/container_hash/hash.hpp>
27 #pragma warning(disable: 4103)
29 #ifdef BOOST_HAS_ABI_HEADERS
30 # include BOOST_ABI_PREFIX
39 #pragma warning(disable : 4251)
41 # pragma warning(disable : 4231)
44 #pragma warning(disable : 4660)
47 #pragma warning(disable:4800)
51 namespace BOOST_REGEX_DETAIL_NS{
54 // forward declaration, we will need this one later:
56 template <class charT, class traits>
57 class basic_regex_parser;
60 void bubble_down_one(I first, I last)
65 while((next != first) && (*next < *(next-1)))
67 (next-1)->swap(*next);
73 template <class Iterator>
74 inline int hash_value_from_capture_name(Iterator i, Iterator j)
76 std::size_t r = boost::hash_range(i, j);
77 r %= ((std::numeric_limits<int>::max)() - 10001);
79 return static_cast<int>(r);
82 class named_subexpressions
87 template <class charT>
88 name(const charT* i, const charT* j, int idx)
91 hash = hash_value_from_capture_name(i, j);
99 bool operator < (const name& other)const
101 return hash < other.hash;
103 bool operator == (const name& other)const
105 return hash == other.hash;
107 void swap(name& other)
109 std::swap(index, other.index);
110 std::swap(hash, other.hash);
114 typedef std::vector<name>::const_iterator const_iterator;
115 typedef std::pair<const_iterator, const_iterator> range_type;
117 named_subexpressions(){}
119 template <class charT>
120 void set_name(const charT* i, const charT* j, int index)
122 m_sub_names.push_back(name(i, j, index));
123 bubble_down_one(m_sub_names.begin(), m_sub_names.end());
125 template <class charT>
126 int get_id(const charT* i, const charT* j)const
129 typename std::vector<name>::const_iterator pos = std::lower_bound(m_sub_names.begin(), m_sub_names.end(), t);
130 if((pos != m_sub_names.end()) && (*pos == t))
136 template <class charT>
137 range_type equal_range(const charT* i, const charT* j)const
140 return std::equal_range(m_sub_names.begin(), m_sub_names.end(), t);
142 int get_id(int h)const
145 std::vector<name>::const_iterator pos = std::lower_bound(m_sub_names.begin(), m_sub_names.end(), t);
146 if((pos != m_sub_names.end()) && (*pos == t))
152 range_type equal_range(int h)const
155 return std::equal_range(m_sub_names.begin(), m_sub_names.end(), t);
158 std::vector<name> m_sub_names;
163 // represents the data we wish to expose to the matching algorithms.
165 template <class charT, class traits>
166 struct regex_data : public named_subexpressions
168 typedef regex_constants::syntax_option_type flag_type;
169 typedef std::size_t size_type;
171 regex_data(const ::boost::shared_ptr<
172 ::boost::regex_traits_wrapper<traits> >& t)
173 : m_ptraits(t), m_expression(0), m_expression_len(0), m_disable_match_any(false) {}
175 : m_ptraits(new ::boost::regex_traits_wrapper<traits>()), m_expression(0), m_expression_len(0), m_disable_match_any(false) {}
178 ::boost::regex_traits_wrapper<traits>
179 > m_ptraits; // traits class instance
180 flag_type m_flags; // flags with which we were compiled
181 int m_status; // error code (0 implies OK).
182 const charT* m_expression; // the original expression
183 std::ptrdiff_t m_expression_len; // the length of the original expression
184 size_type m_mark_count; // the number of marked sub-expressions
185 BOOST_REGEX_DETAIL_NS::re_syntax_base* m_first_state; // the first state of the machine
186 unsigned m_restart_type; // search optimisation type
187 unsigned char m_startmap[1 << CHAR_BIT]; // which characters can start a match
188 unsigned int m_can_be_null; // whether we can match a null string
189 BOOST_REGEX_DETAIL_NS::raw_storage m_data; // the buffer in which our states are constructed
190 typename traits::char_class_type m_word_mask; // mask used to determine if a character is a word character
193 std::size_t, std::size_t> > m_subs; // Position of sub-expressions within the *string*.
194 bool m_has_recursions; // whether we have recursive expressions;
195 bool m_disable_match_any; // when set we need to disable the match_any flag as it causes different/buggy behaviour.
198 // class basic_regex_implementation
199 // pimpl implementation class for basic_regex.
201 template <class charT, class traits>
202 class basic_regex_implementation
203 : public regex_data<charT, traits>
206 typedef regex_constants::syntax_option_type flag_type;
207 typedef std::ptrdiff_t difference_type;
208 typedef std::size_t size_type;
209 typedef typename traits::locale_type locale_type;
210 typedef const charT* const_iterator;
212 basic_regex_implementation(){}
213 basic_regex_implementation(const ::boost::shared_ptr<
214 ::boost::regex_traits_wrapper<traits> >& t)
215 : regex_data<charT, traits>(t) {}
216 void assign(const charT* arg_first,
217 const charT* arg_last,
220 regex_data<charT, traits>* pdat = this;
221 basic_regex_parser<charT, traits> parser(pdat);
222 parser.parse(arg_first, arg_last, f);
225 locale_type BOOST_REGEX_CALL imbue(locale_type l)
227 return this->m_ptraits->imbue(l);
229 locale_type BOOST_REGEX_CALL getloc()const
231 return this->m_ptraits->getloc();
233 std::basic_string<charT> BOOST_REGEX_CALL str()const
235 std::basic_string<charT> result;
236 if(this->m_status == 0)
237 result = std::basic_string<charT>(this->m_expression, this->m_expression_len);
240 const_iterator BOOST_REGEX_CALL expression()const
242 return this->m_expression;
244 std::pair<const_iterator, const_iterator> BOOST_REGEX_CALL subexpression(std::size_t n)const
246 const std::pair<std::size_t, std::size_t>& pi = this->m_subs.at(n);
247 std::pair<const_iterator, const_iterator> p(expression() + pi.first, expression() + pi.second);
252 const_iterator BOOST_REGEX_CALL begin()const
254 return (this->m_status ? 0 : this->m_expression);
256 const_iterator BOOST_REGEX_CALL end()const
258 return (this->m_status ? 0 : this->m_expression + this->m_expression_len);
260 flag_type BOOST_REGEX_CALL flags()const
262 return this->m_flags;
264 size_type BOOST_REGEX_CALL size()const
266 return this->m_expression_len;
268 int BOOST_REGEX_CALL status()const
270 return this->m_status;
272 size_type BOOST_REGEX_CALL mark_count()const
274 return this->m_mark_count - 1;
276 const BOOST_REGEX_DETAIL_NS::re_syntax_base* get_first_state()const
278 return this->m_first_state;
280 unsigned get_restart_type()const
282 return this->m_restart_type;
284 const unsigned char* get_map()const
286 return this->m_startmap;
288 const ::boost::regex_traits_wrapper<traits>& get_traits()const
290 return *(this->m_ptraits);
292 bool can_be_null()const
294 return this->m_can_be_null;
296 const regex_data<charT, traits>& get_data()const
298 basic_regex_implementation<charT, traits> const* p = this;
299 return *static_cast<const regex_data<charT, traits>*>(p);
303 } // namespace BOOST_REGEX_DETAIL_NS
305 // class basic_regex:
306 // represents the compiled
307 // regular expression:
310 #ifdef BOOST_REGEX_NO_FWD
311 template <class charT, class traits = regex_traits<charT> >
313 template <class charT, class traits >
315 class basic_regex : public regbase
319 typedef std::size_t traits_size_type;
320 typedef typename traits::string_type traits_string_type;
321 typedef charT char_type;
322 typedef traits traits_type;
324 typedef charT value_type;
325 typedef charT& reference;
326 typedef const charT& const_reference;
327 typedef const charT* const_iterator;
328 typedef const_iterator iterator;
329 typedef std::ptrdiff_t difference_type;
330 typedef std::size_t size_type;
331 typedef regex_constants::syntax_option_type flag_type;
333 // placeholder for actual locale type used by the
334 // traits class to localise *this.
335 typedef typename traits::locale_type locale_type;
338 explicit basic_regex(){}
339 explicit basic_regex(const charT* p, flag_type f = regex_constants::normal)
343 basic_regex(const charT* p1, const charT* p2, flag_type f = regex_constants::normal)
347 basic_regex(const charT* p, size_type len, flag_type f)
351 basic_regex(const basic_regex& that)
352 : m_pimpl(that.m_pimpl) {}
354 basic_regex& BOOST_REGEX_CALL operator=(const basic_regex& that)
358 basic_regex& BOOST_REGEX_CALL operator=(const charT* ptr)
365 basic_regex& assign(const basic_regex& that)
367 m_pimpl = that.m_pimpl;
370 basic_regex& assign(const charT* p, flag_type f = regex_constants::normal)
372 return assign(p, p + traits::length(p), f);
374 basic_regex& assign(const charT* p, size_type len, flag_type f)
376 return assign(p, p + len, f);
379 basic_regex& do_assign(const charT* p1,
383 basic_regex& assign(const charT* p1,
385 flag_type f = regex_constants::normal)
387 return do_assign(p1, p2, f);
389 #if !defined(BOOST_NO_MEMBER_TEMPLATES)
391 template <class ST, class SA>
392 unsigned int BOOST_REGEX_CALL set_expression(const std::basic_string<charT, ST, SA>& p, flag_type f = regex_constants::normal)
394 return set_expression(p.data(), p.data() + p.size(), f);
397 template <class ST, class SA>
398 explicit basic_regex(const std::basic_string<charT, ST, SA>& p, flag_type f = regex_constants::normal)
403 template <class InputIterator>
404 basic_regex(InputIterator arg_first, InputIterator arg_last, flag_type f = regex_constants::normal)
406 typedef typename traits::string_type seq_type;
407 seq_type a(arg_first, arg_last);
409 assign(static_cast<const charT*>(&*a.begin()), static_cast<const charT*>(&*a.begin() + a.size()), f);
411 assign(static_cast<const charT*>(0), static_cast<const charT*>(0), f);
414 template <class ST, class SA>
415 basic_regex& BOOST_REGEX_CALL operator=(const std::basic_string<charT, ST, SA>& p)
417 return assign(p.data(), p.data() + p.size(), regex_constants::normal);
420 template <class string_traits, class A>
421 basic_regex& BOOST_REGEX_CALL assign(
422 const std::basic_string<charT, string_traits, A>& s,
423 flag_type f = regex_constants::normal)
425 return assign(s.data(), s.data() + s.size(), f);
428 template <class InputIterator>
429 basic_regex& BOOST_REGEX_CALL assign(InputIterator arg_first,
430 InputIterator arg_last,
431 flag_type f = regex_constants::normal)
433 typedef typename traits::string_type seq_type;
434 seq_type a(arg_first, arg_last);
437 const charT* p1 = &*a.begin();
438 const charT* p2 = &*a.begin() + a.size();
439 return assign(p1, p2, f);
441 return assign(static_cast<const charT*>(0), static_cast<const charT*>(0), f);
444 unsigned int BOOST_REGEX_CALL set_expression(const std::basic_string<charT>& p, flag_type f = regex_constants::normal)
446 return set_expression(p.data(), p.data() + p.size(), f);
449 basic_regex(const std::basic_string<charT>& p, flag_type f = regex_constants::normal)
454 basic_regex& BOOST_REGEX_CALL operator=(const std::basic_string<charT>& p)
456 return assign(p.data(), p.data() + p.size(), regex_constants::normal);
459 basic_regex& BOOST_REGEX_CALL assign(
460 const std::basic_string<charT>& s,
461 flag_type f = regex_constants::normal)
463 return assign(s.data(), s.data() + s.size(), f);
470 locale_type BOOST_REGEX_CALL imbue(locale_type l);
471 locale_type BOOST_REGEX_CALL getloc()const
473 return m_pimpl.get() ? m_pimpl->getloc() : locale_type();
477 // retained for backwards compatibility only, "flags"
478 // is now the preferred name:
479 flag_type BOOST_REGEX_CALL getflags()const
483 flag_type BOOST_REGEX_CALL flags()const
485 return m_pimpl.get() ? m_pimpl->flags() : 0;
489 std::basic_string<charT> BOOST_REGEX_CALL str()const
491 return m_pimpl.get() ? m_pimpl->str() : std::basic_string<charT>();
494 // begin, end, subexpression:
495 std::pair<const_iterator, const_iterator> BOOST_REGEX_CALL subexpression(std::size_t n)const
498 boost::throw_exception(std::logic_error("Can't access subexpressions in an invalid regex."));
499 return m_pimpl->subexpression(n);
501 const_iterator BOOST_REGEX_CALL begin()const
503 return (m_pimpl.get() ? m_pimpl->begin() : 0);
505 const_iterator BOOST_REGEX_CALL end()const
507 return (m_pimpl.get() ? m_pimpl->end() : 0);
511 void BOOST_REGEX_CALL swap(basic_regex& that)throw()
513 m_pimpl.swap(that.m_pimpl);
517 size_type BOOST_REGEX_CALL size()const
519 return (m_pimpl.get() ? m_pimpl->size() : 0);
523 size_type BOOST_REGEX_CALL max_size()const
529 bool BOOST_REGEX_CALL empty()const
531 return (m_pimpl.get() ? 0 != m_pimpl->status() : true);
534 size_type BOOST_REGEX_CALL mark_count()const
536 return (m_pimpl.get() ? m_pimpl->mark_count() : 0);
541 return (m_pimpl.get() ? m_pimpl->status() : regex_constants::error_empty);
544 int BOOST_REGEX_CALL compare(const basic_regex& that) const
546 if(m_pimpl.get() == that.m_pimpl.get())
550 if(!that.m_pimpl.get())
552 if(status() != that.status())
553 return status() - that.status();
554 if(flags() != that.flags())
555 return flags() - that.flags();
556 return str().compare(that.str());
558 bool BOOST_REGEX_CALL operator==(const basic_regex& e)const
560 return compare(e) == 0;
562 bool BOOST_REGEX_CALL operator != (const basic_regex& e)const
564 return compare(e) != 0;
566 bool BOOST_REGEX_CALL operator<(const basic_regex& e)const
568 return compare(e) < 0;
570 bool BOOST_REGEX_CALL operator>(const basic_regex& e)const
572 return compare(e) > 0;
574 bool BOOST_REGEX_CALL operator<=(const basic_regex& e)const
576 return compare(e) <= 0;
578 bool BOOST_REGEX_CALL operator>=(const basic_regex& e)const
580 return compare(e) >= 0;
584 // The following are deprecated as public interfaces
585 // but are available for compatibility with earlier versions.
586 const charT* BOOST_REGEX_CALL expression()const
588 return (m_pimpl.get() && !m_pimpl->status() ? m_pimpl->expression() : 0);
590 unsigned int BOOST_REGEX_CALL set_expression(const charT* p1, const charT* p2, flag_type f = regex_constants::normal)
592 assign(p1, p2, f | regex_constants::no_except);
595 unsigned int BOOST_REGEX_CALL set_expression(const charT* p, flag_type f = regex_constants::normal)
597 assign(p, f | regex_constants::no_except);
600 unsigned int BOOST_REGEX_CALL error_code()const
605 // private access methods:
607 const BOOST_REGEX_DETAIL_NS::re_syntax_base* get_first_state()const
609 BOOST_ASSERT(0 != m_pimpl.get());
610 return m_pimpl->get_first_state();
612 unsigned get_restart_type()const
614 BOOST_ASSERT(0 != m_pimpl.get());
615 return m_pimpl->get_restart_type();
617 const unsigned char* get_map()const
619 BOOST_ASSERT(0 != m_pimpl.get());
620 return m_pimpl->get_map();
622 const ::boost::regex_traits_wrapper<traits>& get_traits()const
624 BOOST_ASSERT(0 != m_pimpl.get());
625 return m_pimpl->get_traits();
627 bool can_be_null()const
629 BOOST_ASSERT(0 != m_pimpl.get());
630 return m_pimpl->can_be_null();
632 const BOOST_REGEX_DETAIL_NS::regex_data<charT, traits>& get_data()const
634 BOOST_ASSERT(0 != m_pimpl.get());
635 return m_pimpl->get_data();
637 boost::shared_ptr<BOOST_REGEX_DETAIL_NS::named_subexpressions > get_named_subs()const
643 shared_ptr<BOOST_REGEX_DETAIL_NS::basic_regex_implementation<charT, traits> > m_pimpl;
647 // out of line members;
648 // these are the only members that mutate the basic_regex object,
649 // and are designed to provide the strong exception guarentee
650 // (in the event of a throw, the state of the object remains unchanged).
652 template <class charT, class traits>
653 basic_regex<charT, traits>& basic_regex<charT, traits>::do_assign(const charT* p1,
657 shared_ptr<BOOST_REGEX_DETAIL_NS::basic_regex_implementation<charT, traits> > temp;
660 temp = shared_ptr<BOOST_REGEX_DETAIL_NS::basic_regex_implementation<charT, traits> >(new BOOST_REGEX_DETAIL_NS::basic_regex_implementation<charT, traits>());
664 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));
666 temp->assign(p1, p2, f);
671 template <class charT, class traits>
672 typename basic_regex<charT, traits>::locale_type BOOST_REGEX_CALL basic_regex<charT, traits>::imbue(locale_type l)
674 shared_ptr<BOOST_REGEX_DETAIL_NS::basic_regex_implementation<charT, traits> > temp(new BOOST_REGEX_DETAIL_NS::basic_regex_implementation<charT, traits>());
675 locale_type result = temp->imbue(l);
683 template <class charT, class traits>
684 void swap(basic_regex<charT, traits>& e1, basic_regex<charT, traits>& e2)
689 #ifndef BOOST_NO_STD_LOCALE
690 template <class charT, class traits, class traits2>
691 std::basic_ostream<charT, traits>&
692 operator << (std::basic_ostream<charT, traits>& os,
693 const basic_regex<charT, traits2>& e)
695 return (os << e.str());
698 template <class traits>
699 std::ostream& operator << (std::ostream& os, const basic_regex<char, traits>& e)
701 return (os << e.str());
706 // class reg_expression:
707 // this is provided for backwards compatibility only,
708 // it is deprecated, no not use!
710 #ifdef BOOST_REGEX_NO_FWD
711 template <class charT, class traits = regex_traits<charT> >
713 template <class charT, class traits >
715 class reg_expression : public basic_regex<charT, traits>
718 typedef typename basic_regex<charT, traits>::flag_type flag_type;
719 typedef typename basic_regex<charT, traits>::size_type size_type;
720 explicit reg_expression(){}
721 explicit reg_expression(const charT* p, flag_type f = regex_constants::normal)
722 : basic_regex<charT, traits>(p, f){}
723 reg_expression(const charT* p1, const charT* p2, flag_type f = regex_constants::normal)
724 : basic_regex<charT, traits>(p1, p2, f){}
725 reg_expression(const charT* p, size_type len, flag_type f)
726 : basic_regex<charT, traits>(p, len, f){}
727 reg_expression(const reg_expression& that)
728 : basic_regex<charT, traits>(that) {}
730 reg_expression& BOOST_REGEX_CALL operator=(const reg_expression& that)
732 return this->assign(that);
735 #if !defined(BOOST_NO_MEMBER_TEMPLATES)
736 template <class ST, class SA>
737 explicit reg_expression(const std::basic_string<charT, ST, SA>& p, flag_type f = regex_constants::normal)
738 : basic_regex<charT, traits>(p, f)
742 template <class InputIterator>
743 reg_expression(InputIterator arg_first, InputIterator arg_last, flag_type f = regex_constants::normal)
744 : basic_regex<charT, traits>(arg_first, arg_last, f)
748 template <class ST, class SA>
749 reg_expression& BOOST_REGEX_CALL operator=(const std::basic_string<charT, ST, SA>& p)
755 explicit reg_expression(const std::basic_string<charT>& p, flag_type f = regex_constants::normal)
756 : basic_regex<charT, traits>(p, f)
760 reg_expression& BOOST_REGEX_CALL operator=(const std::basic_string<charT>& p)
770 #pragma warning (pop)
776 #pragma warning(push)
777 #pragma warning(disable: 4103)
779 #ifdef BOOST_HAS_ABI_HEADERS
780 # include BOOST_ABI_SUFFIX