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_V5_BASIC_REGEX_HPP
20 #define BOOST_REGEX_V5_BASIC_REGEX_HPP
25 #ifdef BOOST_REGEX_MSVC
27 #pragma warning(disable : 4251)
28 #if BOOST_REGEX_MSVC < 1700
29 # pragma warning(disable : 4231)
31 #if BOOST_REGEX_MSVC < 1600
32 #pragma warning(disable : 4660)
34 #if BOOST_REGEX_MSVC < 1910
35 #pragma warning(disable:4800)
39 namespace BOOST_REGEX_DETAIL_NS{
42 // forward declaration, we will need this one later:
44 template <class charT, class traits>
45 class basic_regex_parser;
48 void bubble_down_one(I first, I last)
53 while((next != first) && (*next < *(next-1)))
55 (next-1)->swap(*next);
61 static const int hash_value_mask = 1 << (std::numeric_limits<int>::digits - 1);
63 template <class Iterator>
64 inline int hash_value_from_capture_name(Iterator i, Iterator j)
69 r ^= *i + 0x9e3779b9 + (r << 6) + (r >> 2);
72 r %= ((std::numeric_limits<int>::max)());
73 return static_cast<int>(r) | hash_value_mask;
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 ::std::shared_ptr<
166 ::boost::regex_traits_wrapper<traits> >& t)
167 : m_ptraits(t), m_flags(0), m_status(0), m_expression(0), m_expression_len(0),
168 m_mark_count(0), m_first_state(0), m_restart_type(0),
170 m_can_be_null(0), m_word_mask(0), m_has_recursions(false), m_disable_match_any(false) {}
172 : m_ptraits(new ::boost::regex_traits_wrapper<traits>()), m_flags(0), m_status(0), m_expression(0), m_expression_len(0),
173 m_mark_count(0), m_first_state(0), m_restart_type(0),
175 m_can_be_null(0), m_word_mask(0), m_has_recursions(false), 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 ::std::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 imbue(locale_type l)
227 return this->m_ptraits->imbue(l);
229 locale_type getloc()const
231 return this->m_ptraits->getloc();
233 std::basic_string<charT> 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 expression()const
242 return this->m_expression;
244 std::pair<const_iterator, const_iterator> 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 begin()const
254 return (this->m_status ? 0 : this->m_expression);
256 const_iterator end()const
258 return (this->m_status ? 0 : this->m_expression + this->m_expression_len);
260 flag_type flags()const
262 return this->m_flags;
264 size_type size()const
266 return this->m_expression_len;
270 return this->m_status;
272 size_type 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& operator=(const basic_regex& that)
358 basic_regex& 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);
390 template <class ST, class SA>
391 unsigned int set_expression(const std::basic_string<charT, ST, SA>& p, flag_type f = regex_constants::normal)
393 return set_expression(p.data(), p.data() + p.size(), f);
396 template <class ST, class SA>
397 explicit basic_regex(const std::basic_string<charT, ST, SA>& p, flag_type f = regex_constants::normal)
402 template <class InputIterator>
403 basic_regex(InputIterator arg_first, InputIterator arg_last, flag_type f = regex_constants::normal)
405 typedef typename traits::string_type seq_type;
406 seq_type a(arg_first, arg_last);
408 assign(static_cast<const charT*>(&*a.begin()), static_cast<const charT*>(&*a.begin() + a.size()), f);
410 assign(static_cast<const charT*>(0), static_cast<const charT*>(0), f);
413 template <class ST, class SA>
414 basic_regex& operator=(const std::basic_string<charT, ST, SA>& p)
416 return assign(p.data(), p.data() + p.size(), regex_constants::normal);
419 template <class string_traits, class A>
421 const std::basic_string<charT, string_traits, A>& s,
422 flag_type f = regex_constants::normal)
424 return assign(s.data(), s.data() + s.size(), f);
427 template <class InputIterator>
428 basic_regex& assign(InputIterator arg_first,
429 InputIterator arg_last,
430 flag_type f = regex_constants::normal)
432 typedef typename traits::string_type seq_type;
433 seq_type a(arg_first, arg_last);
436 const charT* p1 = &*a.begin();
437 const charT* p2 = &*a.begin() + a.size();
438 return assign(p1, p2, f);
440 return assign(static_cast<const charT*>(0), static_cast<const charT*>(0), f);
445 locale_type imbue(locale_type l);
446 locale_type getloc()const
448 return m_pimpl.get() ? m_pimpl->getloc() : locale_type();
452 // retained for backwards compatibility only, "flags"
453 // is now the preferred name:
454 flag_type getflags()const
458 flag_type flags()const
460 return m_pimpl.get() ? m_pimpl->flags() : 0;
464 std::basic_string<charT> str()const
466 return m_pimpl.get() ? m_pimpl->str() : std::basic_string<charT>();
469 // begin, end, subexpression:
470 std::pair<const_iterator, const_iterator> subexpression(std::size_t n)const
472 #ifdef BOOST_REGEX_STANDALONE
474 throw std::logic_error("Can't access subexpressions in an invalid regex.");
477 boost::throw_exception(std::logic_error("Can't access subexpressions in an invalid regex."));
479 return m_pimpl->subexpression(n);
481 const_iterator begin()const
483 return (m_pimpl.get() ? m_pimpl->begin() : 0);
485 const_iterator end()const
487 return (m_pimpl.get() ? m_pimpl->end() : 0);
491 void swap(basic_regex& that)throw()
493 m_pimpl.swap(that.m_pimpl);
497 size_type size()const
499 return (m_pimpl.get() ? m_pimpl->size() : 0);
503 size_type max_size()const
511 return (m_pimpl.get() ? 0 != m_pimpl->status() : true);
514 size_type mark_count()const
516 return (m_pimpl.get() ? m_pimpl->mark_count() : 0);
521 return (m_pimpl.get() ? m_pimpl->status() : regex_constants::error_empty);
524 int compare(const basic_regex& that) const
526 if(m_pimpl.get() == that.m_pimpl.get())
530 if(!that.m_pimpl.get())
532 if(status() != that.status())
533 return status() - that.status();
534 if(flags() != that.flags())
535 return flags() - that.flags();
536 return str().compare(that.str());
538 bool operator==(const basic_regex& e)const
540 return compare(e) == 0;
542 bool operator != (const basic_regex& e)const
544 return compare(e) != 0;
546 bool operator<(const basic_regex& e)const
548 return compare(e) < 0;
550 bool operator>(const basic_regex& e)const
552 return compare(e) > 0;
554 bool operator<=(const basic_regex& e)const
556 return compare(e) <= 0;
558 bool operator>=(const basic_regex& e)const
560 return compare(e) >= 0;
564 // The following are deprecated as public interfaces
565 // but are available for compatibility with earlier versions.
566 const charT* expression()const
568 return (m_pimpl.get() && !m_pimpl->status() ? m_pimpl->expression() : 0);
570 unsigned int set_expression(const charT* p1, const charT* p2, flag_type f = regex_constants::normal)
572 assign(p1, p2, f | regex_constants::no_except);
575 unsigned int set_expression(const charT* p, flag_type f = regex_constants::normal)
577 assign(p, f | regex_constants::no_except);
580 unsigned int error_code()const
585 // private access methods:
587 const BOOST_REGEX_DETAIL_NS::re_syntax_base* get_first_state()const
589 BOOST_REGEX_ASSERT(0 != m_pimpl.get());
590 return m_pimpl->get_first_state();
592 unsigned get_restart_type()const
594 BOOST_REGEX_ASSERT(0 != m_pimpl.get());
595 return m_pimpl->get_restart_type();
597 const unsigned char* get_map()const
599 BOOST_REGEX_ASSERT(0 != m_pimpl.get());
600 return m_pimpl->get_map();
602 const ::boost::regex_traits_wrapper<traits>& get_traits()const
604 BOOST_REGEX_ASSERT(0 != m_pimpl.get());
605 return m_pimpl->get_traits();
607 bool can_be_null()const
609 BOOST_REGEX_ASSERT(0 != m_pimpl.get());
610 return m_pimpl->can_be_null();
612 const BOOST_REGEX_DETAIL_NS::regex_data<charT, traits>& get_data()const
614 BOOST_REGEX_ASSERT(0 != m_pimpl.get());
615 return m_pimpl->get_data();
617 std::shared_ptr<BOOST_REGEX_DETAIL_NS::named_subexpressions > get_named_subs()const
623 std::shared_ptr<BOOST_REGEX_DETAIL_NS::basic_regex_implementation<charT, traits> > m_pimpl;
627 // out of line members;
628 // these are the only members that mutate the basic_regex object,
629 // and are designed to provide the strong exception guarantee
630 // (in the event of a throw, the state of the object remains unchanged).
632 template <class charT, class traits>
633 basic_regex<charT, traits>& basic_regex<charT, traits>::do_assign(const charT* p1,
637 std::shared_ptr<BOOST_REGEX_DETAIL_NS::basic_regex_implementation<charT, traits> > temp;
640 temp = std::shared_ptr<BOOST_REGEX_DETAIL_NS::basic_regex_implementation<charT, traits> >(new BOOST_REGEX_DETAIL_NS::basic_regex_implementation<charT, traits>());
644 temp = std::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));
646 temp->assign(p1, p2, f);
651 template <class charT, class traits>
652 typename basic_regex<charT, traits>::locale_type basic_regex<charT, traits>::imbue(locale_type l)
654 std::shared_ptr<BOOST_REGEX_DETAIL_NS::basic_regex_implementation<charT, traits> > temp(new BOOST_REGEX_DETAIL_NS::basic_regex_implementation<charT, traits>());
655 locale_type result = temp->imbue(l);
663 template <class charT, class traits>
664 void swap(basic_regex<charT, traits>& e1, basic_regex<charT, traits>& e2)
669 template <class charT, class traits, class traits2>
670 std::basic_ostream<charT, traits>&
671 operator << (std::basic_ostream<charT, traits>& os,
672 const basic_regex<charT, traits2>& e)
674 return (os << e.str());
678 // class reg_expression:
679 // this is provided for backwards compatibility only,
680 // it is deprecated, no not use!
682 #ifdef BOOST_REGEX_NO_FWD
683 template <class charT, class traits = regex_traits<charT> >
685 template <class charT, class traits >
687 class reg_expression : public basic_regex<charT, traits>
690 typedef typename basic_regex<charT, traits>::flag_type flag_type;
691 typedef typename basic_regex<charT, traits>::size_type size_type;
692 explicit reg_expression(){}
693 explicit reg_expression(const charT* p, flag_type f = regex_constants::normal)
694 : basic_regex<charT, traits>(p, f){}
695 reg_expression(const charT* p1, const charT* p2, flag_type f = regex_constants::normal)
696 : basic_regex<charT, traits>(p1, p2, f){}
697 reg_expression(const charT* p, size_type len, flag_type f)
698 : basic_regex<charT, traits>(p, len, f){}
699 reg_expression(const reg_expression& that)
700 : basic_regex<charT, traits>(that) {}
702 reg_expression& operator=(const reg_expression& that)
704 return this->assign(that);
707 template <class ST, class SA>
708 explicit reg_expression(const std::basic_string<charT, ST, SA>& p, flag_type f = regex_constants::normal)
709 : basic_regex<charT, traits>(p, f)
713 template <class InputIterator>
714 reg_expression(InputIterator arg_first, InputIterator arg_last, flag_type f = regex_constants::normal)
715 : basic_regex<charT, traits>(arg_first, arg_last, f)
719 template <class ST, class SA>
720 reg_expression& operator=(const std::basic_string<charT, ST, SA>& p)
728 #ifdef BOOST_REGEX_MSVC
729 #pragma warning (pop)