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 4660 4800)
45 // forward declaration, we will need this one later:
47 template <class charT, class traits>
48 class basic_regex_parser;
51 void bubble_down_one(I first, I last)
56 while((next != first) && (*next < *(next-1)))
58 (next-1)->swap(*next);
64 template <class Iterator>
65 inline int hash_value_from_capture_name(Iterator i, Iterator j)
67 std::size_t r = boost::hash_range(i, j);
68 r %= ((std::numeric_limits<int>::max)() - 10001);
70 return static_cast<int>(r);
73 class named_subexpressions
78 template <class charT>
79 name(const charT* i, const charT* j, int idx)
82 hash = hash_value_from_capture_name(i, j);
90 bool operator < (const name& other)const
92 return hash < other.hash;
94 bool operator == (const name& other)const
96 return hash == other.hash;
98 void swap(name& other)
100 std::swap(index, other.index);
101 std::swap(hash, other.hash);
105 typedef std::vector<name>::const_iterator const_iterator;
106 typedef std::pair<const_iterator, const_iterator> range_type;
108 named_subexpressions(){}
110 template <class charT>
111 void set_name(const charT* i, const charT* j, int index)
113 m_sub_names.push_back(name(i, j, index));
114 bubble_down_one(m_sub_names.begin(), m_sub_names.end());
116 template <class charT>
117 int get_id(const charT* i, const charT* j)const
120 typename std::vector<name>::const_iterator pos = std::lower_bound(m_sub_names.begin(), m_sub_names.end(), t);
121 if((pos != m_sub_names.end()) && (*pos == t))
127 template <class charT>
128 range_type equal_range(const charT* i, const charT* j)const
131 return std::equal_range(m_sub_names.begin(), m_sub_names.end(), t);
133 int get_id(int h)const
136 std::vector<name>::const_iterator pos = std::lower_bound(m_sub_names.begin(), m_sub_names.end(), t);
137 if((pos != m_sub_names.end()) && (*pos == t))
143 range_type equal_range(int h)const
146 return std::equal_range(m_sub_names.begin(), m_sub_names.end(), t);
149 std::vector<name> m_sub_names;
154 // represents the data we wish to expose to the matching algorithms.
156 template <class charT, class traits>
157 struct regex_data : public named_subexpressions
159 typedef regex_constants::syntax_option_type flag_type;
160 typedef std::size_t size_type;
162 regex_data(const ::boost::shared_ptr<
163 ::boost::regex_traits_wrapper<traits> >& t)
164 : m_ptraits(t), m_expression(0), m_expression_len(0) {}
166 : m_ptraits(new ::boost::regex_traits_wrapper<traits>()), m_expression(0), m_expression_len(0) {}
169 ::boost::regex_traits_wrapper<traits>
170 > m_ptraits; // traits class instance
171 flag_type m_flags; // flags with which we were compiled
172 int m_status; // error code (0 implies OK).
173 const charT* m_expression; // the original expression
174 std::ptrdiff_t m_expression_len; // the length of the original expression
175 size_type m_mark_count; // the number of marked sub-expressions
176 re_detail::re_syntax_base* m_first_state; // the first state of the machine
177 unsigned m_restart_type; // search optimisation type
178 unsigned char m_startmap[1 << CHAR_BIT]; // which characters can start a match
179 unsigned int m_can_be_null; // whether we can match a null string
180 re_detail::raw_storage m_data; // the buffer in which our states are constructed
181 typename traits::char_class_type m_word_mask; // mask used to determine if a character is a word character
184 std::size_t, std::size_t> > m_subs; // Position of sub-expressions within the *string*.
185 bool m_has_recursions; // whether we have recursive expressions;
188 // class basic_regex_implementation
189 // pimpl implementation class for basic_regex.
191 template <class charT, class traits>
192 class basic_regex_implementation
193 : public regex_data<charT, traits>
196 typedef regex_constants::syntax_option_type flag_type;
197 typedef std::ptrdiff_t difference_type;
198 typedef std::size_t size_type;
199 typedef typename traits::locale_type locale_type;
200 typedef const charT* const_iterator;
202 basic_regex_implementation(){}
203 basic_regex_implementation(const ::boost::shared_ptr<
204 ::boost::regex_traits_wrapper<traits> >& t)
205 : regex_data<charT, traits>(t) {}
206 void assign(const charT* arg_first,
207 const charT* arg_last,
210 regex_data<charT, traits>* pdat = this;
211 basic_regex_parser<charT, traits> parser(pdat);
212 parser.parse(arg_first, arg_last, f);
215 locale_type BOOST_REGEX_CALL imbue(locale_type l)
217 return this->m_ptraits->imbue(l);
219 locale_type BOOST_REGEX_CALL getloc()const
221 return this->m_ptraits->getloc();
223 std::basic_string<charT> BOOST_REGEX_CALL str()const
225 std::basic_string<charT> result;
226 if(this->m_status == 0)
227 result = std::basic_string<charT>(this->m_expression, this->m_expression_len);
230 const_iterator BOOST_REGEX_CALL expression()const
232 return this->m_expression;
234 std::pair<const_iterator, const_iterator> BOOST_REGEX_CALL subexpression(std::size_t n)const
237 boost::throw_exception(std::out_of_range("0 is not a valid subexpression index."));
238 const std::pair<std::size_t, std::size_t>& pi = this->m_subs.at(n - 1);
239 std::pair<const_iterator, const_iterator> p(expression() + pi.first, expression() + pi.second);
244 const_iterator BOOST_REGEX_CALL begin()const
246 return (!this->m_status ? 0 : this->m_expression);
248 const_iterator BOOST_REGEX_CALL end()const
250 return (!this->m_status ? 0 : this->m_expression + this->m_expression_len);
252 flag_type BOOST_REGEX_CALL flags()const
254 return this->m_flags;
256 size_type BOOST_REGEX_CALL size()const
258 return this->m_expression_len;
260 int BOOST_REGEX_CALL status()const
262 return this->m_status;
264 size_type BOOST_REGEX_CALL mark_count()const
266 return this->m_mark_count;
268 const re_detail::re_syntax_base* get_first_state()const
270 return this->m_first_state;
272 unsigned get_restart_type()const
274 return this->m_restart_type;
276 const unsigned char* get_map()const
278 return this->m_startmap;
280 const ::boost::regex_traits_wrapper<traits>& get_traits()const
282 return *(this->m_ptraits);
284 bool can_be_null()const
286 return this->m_can_be_null;
288 const regex_data<charT, traits>& get_data()const
290 basic_regex_implementation<charT, traits> const* p = this;
291 return *static_cast<const regex_data<charT, traits>*>(p);
295 } // namespace re_detail
297 // class basic_regex:
298 // represents the compiled
299 // regular expression:
302 #ifdef BOOST_REGEX_NO_FWD
303 template <class charT, class traits = regex_traits<charT> >
305 template <class charT, class traits >
307 class basic_regex : public regbase
311 typedef std::size_t traits_size_type;
312 typedef typename traits::string_type traits_string_type;
313 typedef charT char_type;
314 typedef traits traits_type;
316 typedef charT value_type;
317 typedef charT& reference;
318 typedef const charT& const_reference;
319 typedef const charT* const_iterator;
320 typedef const_iterator iterator;
321 typedef std::ptrdiff_t difference_type;
322 typedef std::size_t size_type;
323 typedef regex_constants::syntax_option_type flag_type;
325 // placeholder for actual locale type used by the
326 // traits class to localise *this.
327 typedef typename traits::locale_type locale_type;
330 explicit basic_regex(){}
331 explicit basic_regex(const charT* p, flag_type f = regex_constants::normal)
335 basic_regex(const charT* p1, const charT* p2, flag_type f = regex_constants::normal)
339 basic_regex(const charT* p, size_type len, flag_type f)
343 basic_regex(const basic_regex& that)
344 : m_pimpl(that.m_pimpl) {}
346 basic_regex& BOOST_REGEX_CALL operator=(const basic_regex& that)
350 basic_regex& BOOST_REGEX_CALL operator=(const charT* ptr)
357 basic_regex& assign(const basic_regex& that)
359 m_pimpl = that.m_pimpl;
362 basic_regex& assign(const charT* p, flag_type f = regex_constants::normal)
364 return assign(p, p + traits::length(p), f);
366 basic_regex& assign(const charT* p, size_type len, flag_type f)
368 return assign(p, p + len, f);
371 basic_regex& do_assign(const charT* p1,
375 basic_regex& assign(const charT* p1,
377 flag_type f = regex_constants::normal)
379 return do_assign(p1, p2, f);
381 #if !defined(BOOST_NO_MEMBER_TEMPLATES)
383 template <class ST, class SA>
384 unsigned int BOOST_REGEX_CALL set_expression(const std::basic_string<charT, ST, SA>& p, flag_type f = regex_constants::normal)
386 return set_expression(p.data(), p.data() + p.size(), f);
389 template <class ST, class SA>
390 explicit basic_regex(const std::basic_string<charT, ST, SA>& p, flag_type f = regex_constants::normal)
395 template <class InputIterator>
396 basic_regex(InputIterator arg_first, InputIterator arg_last, flag_type f = regex_constants::normal)
398 typedef typename traits::string_type seq_type;
399 seq_type a(arg_first, arg_last);
401 assign(static_cast<const charT*>(&*a.begin()), static_cast<const charT*>(&*a.begin() + a.size()), f);
403 assign(static_cast<const charT*>(0), static_cast<const charT*>(0), f);
406 template <class ST, class SA>
407 basic_regex& BOOST_REGEX_CALL operator=(const std::basic_string<charT, ST, SA>& p)
409 return assign(p.data(), p.data() + p.size(), regex_constants::normal);
412 template <class string_traits, class A>
413 basic_regex& BOOST_REGEX_CALL assign(
414 const std::basic_string<charT, string_traits, A>& s,
415 flag_type f = regex_constants::normal)
417 return assign(s.data(), s.data() + s.size(), f);
420 template <class InputIterator>
421 basic_regex& BOOST_REGEX_CALL assign(InputIterator arg_first,
422 InputIterator arg_last,
423 flag_type f = regex_constants::normal)
425 typedef typename traits::string_type seq_type;
426 seq_type a(arg_first, arg_last);
429 const charT* p1 = &*a.begin();
430 const charT* p2 = &*a.begin() + a.size();
431 return assign(p1, p2, f);
433 return assign(static_cast<const charT*>(0), static_cast<const charT*>(0), f);
436 unsigned int BOOST_REGEX_CALL set_expression(const std::basic_string<charT>& p, flag_type f = regex_constants::normal)
438 return set_expression(p.data(), p.data() + p.size(), f);
441 basic_regex(const std::basic_string<charT>& p, flag_type f = regex_constants::normal)
446 basic_regex& BOOST_REGEX_CALL operator=(const std::basic_string<charT>& p)
448 return assign(p.data(), p.data() + p.size(), regex_constants::normal);
451 basic_regex& BOOST_REGEX_CALL assign(
452 const std::basic_string<charT>& s,
453 flag_type f = regex_constants::normal)
455 return assign(s.data(), s.data() + s.size(), f);
462 locale_type BOOST_REGEX_CALL imbue(locale_type l);
463 locale_type BOOST_REGEX_CALL getloc()const
465 return m_pimpl.get() ? m_pimpl->getloc() : locale_type();
469 // retained for backwards compatibility only, "flags"
470 // is now the preferred name:
471 flag_type BOOST_REGEX_CALL getflags()const
475 flag_type BOOST_REGEX_CALL flags()const
477 return m_pimpl.get() ? m_pimpl->flags() : 0;
481 std::basic_string<charT> BOOST_REGEX_CALL str()const
483 return m_pimpl.get() ? m_pimpl->str() : std::basic_string<charT>();
486 // begin, end, subexpression:
487 std::pair<const_iterator, const_iterator> BOOST_REGEX_CALL subexpression(std::size_t n)const
490 boost::throw_exception(std::logic_error("Can't access subexpressions in an invalid regex."));
491 return m_pimpl->subexpression(n);
493 const_iterator BOOST_REGEX_CALL begin()const
495 return (m_pimpl.get() ? m_pimpl->begin() : 0);
497 const_iterator BOOST_REGEX_CALL end()const
499 return (m_pimpl.get() ? m_pimpl->end() : 0);
503 void BOOST_REGEX_CALL swap(basic_regex& that)throw()
505 m_pimpl.swap(that.m_pimpl);
509 size_type BOOST_REGEX_CALL size()const
511 return (m_pimpl.get() ? m_pimpl->size() : 0);
515 size_type BOOST_REGEX_CALL max_size()const
521 bool BOOST_REGEX_CALL empty()const
523 return (m_pimpl.get() ? 0 != m_pimpl->status() : true);
526 size_type BOOST_REGEX_CALL mark_count()const
528 return (m_pimpl.get() ? m_pimpl->mark_count() : 0);
533 return (m_pimpl.get() ? m_pimpl->status() : regex_constants::error_empty);
536 int BOOST_REGEX_CALL compare(const basic_regex& that) const
538 if(m_pimpl.get() == that.m_pimpl.get())
542 if(!that.m_pimpl.get())
544 if(status() != that.status())
545 return status() - that.status();
546 if(flags() != that.flags())
547 return flags() - that.flags();
548 return str().compare(that.str());
550 bool BOOST_REGEX_CALL operator==(const basic_regex& e)const
552 return compare(e) == 0;
554 bool BOOST_REGEX_CALL operator != (const basic_regex& e)const
556 return compare(e) != 0;
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;
576 // The following are deprecated as public interfaces
577 // but are available for compatibility with earlier versions.
578 const charT* BOOST_REGEX_CALL expression()const
580 return (m_pimpl.get() && !m_pimpl->status() ? m_pimpl->expression() : 0);
582 unsigned int BOOST_REGEX_CALL set_expression(const charT* p1, const charT* p2, flag_type f = regex_constants::normal)
584 assign(p1, p2, f | regex_constants::no_except);
587 unsigned int BOOST_REGEX_CALL set_expression(const charT* p, flag_type f = regex_constants::normal)
589 assign(p, f | regex_constants::no_except);
592 unsigned int BOOST_REGEX_CALL error_code()const
597 // private access methods:
599 const re_detail::re_syntax_base* get_first_state()const
601 BOOST_ASSERT(0 != m_pimpl.get());
602 return m_pimpl->get_first_state();
604 unsigned get_restart_type()const
606 BOOST_ASSERT(0 != m_pimpl.get());
607 return m_pimpl->get_restart_type();
609 const unsigned char* get_map()const
611 BOOST_ASSERT(0 != m_pimpl.get());
612 return m_pimpl->get_map();
614 const ::boost::regex_traits_wrapper<traits>& get_traits()const
616 BOOST_ASSERT(0 != m_pimpl.get());
617 return m_pimpl->get_traits();
619 bool can_be_null()const
621 BOOST_ASSERT(0 != m_pimpl.get());
622 return m_pimpl->can_be_null();
624 const re_detail::regex_data<charT, traits>& get_data()const
626 BOOST_ASSERT(0 != m_pimpl.get());
627 return m_pimpl->get_data();
629 boost::shared_ptr<re_detail::named_subexpressions > get_named_subs()const
635 shared_ptr<re_detail::basic_regex_implementation<charT, traits> > m_pimpl;
639 // out of line members;
640 // these are the only members that mutate the basic_regex object,
641 // and are designed to provide the strong exception guarentee
642 // (in the event of a throw, the state of the object remains unchanged).
644 template <class charT, class traits>
645 basic_regex<charT, traits>& basic_regex<charT, traits>::do_assign(const charT* p1,
649 shared_ptr<re_detail::basic_regex_implementation<charT, traits> > temp;
652 temp = shared_ptr<re_detail::basic_regex_implementation<charT, traits> >(new re_detail::basic_regex_implementation<charT, traits>());
656 temp = shared_ptr<re_detail::basic_regex_implementation<charT, traits> >(new re_detail::basic_regex_implementation<charT, traits>(m_pimpl->m_ptraits));
658 temp->assign(p1, p2, f);
663 template <class charT, class traits>
664 typename basic_regex<charT, traits>::locale_type BOOST_REGEX_CALL basic_regex<charT, traits>::imbue(locale_type l)
666 shared_ptr<re_detail::basic_regex_implementation<charT, traits> > temp(new re_detail::basic_regex_implementation<charT, traits>());
667 locale_type result = temp->imbue(l);
675 template <class charT, class traits>
676 void swap(basic_regex<charT, traits>& e1, basic_regex<charT, traits>& e2)
681 #ifndef BOOST_NO_STD_LOCALE
682 template <class charT, class traits, class traits2>
683 std::basic_ostream<charT, traits>&
684 operator << (std::basic_ostream<charT, traits>& os,
685 const basic_regex<charT, traits2>& e)
687 return (os << e.str());
690 template <class traits>
691 std::ostream& operator << (std::ostream& os, const basic_regex<char, traits>& e)
693 return (os << e.str());
698 // class reg_expression:
699 // this is provided for backwards compatibility only,
700 // it is deprecated, no not use!
702 #ifdef BOOST_REGEX_NO_FWD
703 template <class charT, class traits = regex_traits<charT> >
705 template <class charT, class traits >
707 class reg_expression : public basic_regex<charT, traits>
710 typedef typename basic_regex<charT, traits>::flag_type flag_type;
711 typedef typename basic_regex<charT, traits>::size_type size_type;
712 explicit reg_expression(){}
713 explicit reg_expression(const charT* p, flag_type f = regex_constants::normal)
714 : basic_regex<charT, traits>(p, f){}
715 reg_expression(const charT* p1, const charT* p2, flag_type f = regex_constants::normal)
716 : basic_regex<charT, traits>(p1, p2, f){}
717 reg_expression(const charT* p, size_type len, flag_type f)
718 : basic_regex<charT, traits>(p, len, f){}
719 reg_expression(const reg_expression& that)
720 : basic_regex<charT, traits>(that) {}
722 reg_expression& BOOST_REGEX_CALL operator=(const reg_expression& that)
724 return this->assign(that);
727 #if !defined(BOOST_NO_MEMBER_TEMPLATES)
728 template <class ST, class SA>
729 explicit reg_expression(const std::basic_string<charT, ST, SA>& p, flag_type f = regex_constants::normal)
730 : basic_regex<charT, traits>(p, f)
734 template <class InputIterator>
735 reg_expression(InputIterator arg_first, InputIterator arg_last, flag_type f = regex_constants::normal)
736 : basic_regex<charT, traits>(arg_first, arg_last, f)
740 template <class ST, class SA>
741 reg_expression& BOOST_REGEX_CALL operator=(const std::basic_string<charT, ST, SA>& p)
747 explicit reg_expression(const std::basic_string<charT>& p, flag_type f = regex_constants::normal)
748 : basic_regex<charT, traits>(p, f)
752 reg_expression& BOOST_REGEX_CALL operator=(const std::basic_string<charT>& p)
762 #pragma warning (pop)
768 #pragma warning(push)
769 #pragma warning(disable: 4103)
771 #ifdef BOOST_HAS_ABI_HEADERS
772 # include BOOST_ABI_SUFFIX