3 * Copyright (c) 2004 John Maddock
4 * Copyright 2011 Garmin Ltd. or its subsidiaries
6 * Use, modification and distribution are subject to the
7 * Boost Software License, Version 1.0. (See accompanying file
8 * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
13 * LOCATION: see http://www.boost.org for most recent version.
14 * FILE cpp_regex_traits.hpp
15 * VERSION see <boost/version.hpp>
16 * DESCRIPTION: Declares regular expression traits class cpp_regex_traits.
19 #ifndef BOOST_CPP_REGEX_TRAITS_HPP_INCLUDED
20 #define BOOST_CPP_REGEX_TRAITS_HPP_INCLUDED
22 #include <boost/config.hpp>
23 #include <boost/integer.hpp>
24 #include <boost/type_traits/make_unsigned.hpp>
26 #ifndef BOOST_NO_STD_LOCALE
28 #ifndef BOOST_RE_PAT_EXCEPT_HPP
29 #include <boost/regex/pattern_except.hpp>
31 #ifndef BOOST_REGEX_TRAITS_DEFAULTS_HPP_INCLUDED
32 #include <boost/regex/v4/regex_traits_defaults.hpp>
34 #ifdef BOOST_HAS_THREADS
35 #include <boost/regex/pending/static_mutex.hpp>
37 #ifndef BOOST_REGEX_PRIMARY_TRANSFORM
38 #include <boost/regex/v4/primary_transform.hpp>
40 #ifndef BOOST_REGEX_OBJECT_CACHE_HPP
41 #include <boost/regex/pending/object_cache.hpp>
50 #pragma warning(disable: 4103)
52 #ifdef BOOST_HAS_ABI_HEADERS
53 # include BOOST_ABI_PREFIX
61 #pragma warning(disable:4786 4251)
67 // forward declaration is needed by some compilers:
69 template <class charT>
70 class cpp_regex_traits;
76 // acts as a stream buffer which wraps around a pair of pointers:
78 template <class charT,
79 class traits = ::std::char_traits<charT> >
80 class parser_buf : public ::std::basic_streambuf<charT, traits>
82 typedef ::std::basic_streambuf<charT, traits> base_type;
83 typedef typename base_type::int_type int_type;
84 typedef typename base_type::char_type char_type;
85 typedef typename base_type::pos_type pos_type;
86 typedef ::std::streamsize streamsize;
87 typedef typename base_type::off_type off_type;
89 parser_buf() : base_type() { setbuf(0, 0); }
90 const charT* getnext() { return this->gptr(); }
92 std::basic_streambuf<charT, traits>* setbuf(char_type* s, streamsize n);
93 typename parser_buf<charT, traits>::pos_type seekpos(pos_type sp, ::std::ios_base::openmode which);
94 typename parser_buf<charT, traits>::pos_type seekoff(off_type off, ::std::ios_base::seekdir way, ::std::ios_base::openmode which);
96 parser_buf& operator=(const parser_buf&);
97 parser_buf(const parser_buf&);
100 template<class charT, class traits>
101 std::basic_streambuf<charT, traits>*
102 parser_buf<charT, traits>::setbuf(char_type* s, streamsize n)
104 this->setg(s, s, s + n);
108 template<class charT, class traits>
109 typename parser_buf<charT, traits>::pos_type
110 parser_buf<charT, traits>::seekoff(off_type off, ::std::ios_base::seekdir way, ::std::ios_base::openmode which)
112 typedef typename boost::int_t<sizeof(way) * CHAR_BIT>::least cast_type;
114 if(which & ::std::ios_base::out)
115 return pos_type(off_type(-1));
116 std::ptrdiff_t size = this->egptr() - this->eback();
117 std::ptrdiff_t pos = this->gptr() - this->eback();
118 charT* g = this->eback();
119 switch(static_cast<cast_type>(way))
121 case ::std::ios_base::beg:
122 if((off < 0) || (off > size))
123 return pos_type(off_type(-1));
125 this->setg(g, g + off, g + size);
127 case ::std::ios_base::end:
128 if((off < 0) || (off > size))
129 return pos_type(off_type(-1));
131 this->setg(g, g + size - off, g + size);
133 case ::std::ios_base::cur:
135 std::ptrdiff_t newpos = static_cast<std::ptrdiff_t>(pos + off);
136 if((newpos < 0) || (newpos > size))
137 return pos_type(off_type(-1));
139 this->setg(g, g + newpos, g + size);
145 #pragma warning(push)
146 #pragma warning(disable:4244)
148 return static_cast<pos_type>(this->gptr() - this->eback());
154 template<class charT, class traits>
155 typename parser_buf<charT, traits>::pos_type
156 parser_buf<charT, traits>::seekpos(pos_type sp, ::std::ios_base::openmode which)
158 if(which & ::std::ios_base::out)
159 return pos_type(off_type(-1));
160 off_type size = static_cast<off_type>(this->egptr() - this->eback());
161 charT* g = this->eback();
162 if(off_type(sp) <= size)
164 this->setg(g, g + off_type(sp), g + size);
166 return pos_type(off_type(-1));
170 // class cpp_regex_traits_base:
171 // acts as a container for locale and the facets we are using.
173 template <class charT>
174 struct cpp_regex_traits_base
176 cpp_regex_traits_base(const std::locale& l)
178 std::locale imbue(const std::locale& l);
180 std::locale m_locale;
181 std::ctype<charT> const* m_pctype;
182 #ifndef BOOST_NO_STD_MESSAGES
183 std::messages<charT> const* m_pmessages;
185 std::collate<charT> const* m_pcollate;
187 bool operator<(const cpp_regex_traits_base& b)const
189 if(m_pctype == b.m_pctype)
191 #ifndef BOOST_NO_STD_MESSAGES
192 if(m_pmessages == b.m_pmessages)
194 return m_pcollate < b.m_pcollate;
196 return m_pmessages < b.m_pmessages;
198 return m_pcollate < b.m_pcollate;
201 return m_pctype < b.m_pctype;
203 bool operator==(const cpp_regex_traits_base& b)const
205 return (m_pctype == b.m_pctype)
206 #ifndef BOOST_NO_STD_MESSAGES
207 && (m_pmessages == b.m_pmessages)
209 && (m_pcollate == b.m_pcollate);
213 template <class charT>
214 std::locale cpp_regex_traits_base<charT>::imbue(const std::locale& l)
216 std::locale result(m_locale);
218 m_pctype = &BOOST_USE_FACET(std::ctype<charT>, l);
219 #ifndef BOOST_NO_STD_MESSAGES
220 m_pmessages = BOOST_HAS_FACET(std::messages<charT>, l) ? &BOOST_USE_FACET(std::messages<charT>, l) : 0;
222 m_pcollate = &BOOST_USE_FACET(std::collate<charT>, l);
227 // class cpp_regex_traits_char_layer:
228 // implements methods that require specialisation for narrow characters:
230 template <class charT>
231 class cpp_regex_traits_char_layer : public cpp_regex_traits_base<charT>
233 typedef std::basic_string<charT> string_type;
234 typedef std::map<charT, regex_constants::syntax_type> map_type;
235 typedef typename map_type::const_iterator map_iterator_type;
237 cpp_regex_traits_char_layer(const std::locale& l)
238 : cpp_regex_traits_base<charT>(l)
242 cpp_regex_traits_char_layer(const cpp_regex_traits_base<charT>& b)
243 : cpp_regex_traits_base<charT>(b)
249 regex_constants::syntax_type syntax_type(charT c)const
251 map_iterator_type i = m_char_map.find(c);
252 return ((i == m_char_map.end()) ? 0 : i->second);
254 regex_constants::escape_syntax_type escape_syntax_type(charT c) const
256 map_iterator_type i = m_char_map.find(c);
257 if(i == m_char_map.end())
259 if(this->m_pctype->is(std::ctype_base::lower, c)) return regex_constants::escape_type_class;
260 if(this->m_pctype->is(std::ctype_base::upper, c)) return regex_constants::escape_type_not_class;
267 string_type get_default_message(regex_constants::syntax_type);
268 // TODO: use a hash table when available!
272 template <class charT>
273 void cpp_regex_traits_char_layer<charT>::init()
275 // we need to start by initialising our syntax map so we know which
276 // character is used for which purpose:
277 #ifndef BOOST_NO_STD_MESSAGES
279 typename std::messages<charT>::catalog cat = static_cast<std::messages<char>::catalog>(-1);
281 typename std::messages<charT>::catalog cat = reinterpret_cast<std::messages<char>::catalog>(-1);
283 std::string cat_name(cpp_regex_traits<charT>::get_catalog_name());
284 if(cat_name.size() && (this->m_pmessages != 0))
286 cat = this->m_pmessages->open(
291 std::string m("Unable to open message catalog: ");
292 std::runtime_error err(m + cat_name);
293 boost::re_detail::raise_runtime_error(err);
297 // if we have a valid catalog then load our messages:
301 #ifndef BOOST_NO_EXCEPTIONS
304 for(regex_constants::syntax_type i = 1; i < regex_constants::syntax_max; ++i)
306 string_type mss = this->m_pmessages->get(cat, 0, i, get_default_message(i));
307 for(typename string_type::size_type j = 0; j < mss.size(); ++j)
309 m_char_map[mss[j]] = i;
312 this->m_pmessages->close(cat);
313 #ifndef BOOST_NO_EXCEPTIONS
317 if(this->m_pmessages)
318 this->m_pmessages->close(cat);
326 for(regex_constants::syntax_type i = 1; i < regex_constants::syntax_max; ++i)
328 const char* ptr = get_default_syntax(i);
331 m_char_map[this->m_pctype->widen(*ptr)] = i;
335 #ifndef BOOST_NO_STD_MESSAGES
340 template <class charT>
341 typename cpp_regex_traits_char_layer<charT>::string_type
342 cpp_regex_traits_char_layer<charT>::get_default_message(regex_constants::syntax_type i)
344 const char* ptr = get_default_syntax(i);
348 result.append(1, this->m_pctype->widen(*ptr));
355 // specialised version for narrow characters:
358 class BOOST_REGEX_DECL cpp_regex_traits_char_layer<char> : public cpp_regex_traits_base<char>
360 typedef std::string string_type;
362 cpp_regex_traits_char_layer(const std::locale& l)
363 : cpp_regex_traits_base<char>(l)
367 cpp_regex_traits_char_layer(const cpp_regex_traits_base<char>& l)
368 : cpp_regex_traits_base<char>(l)
373 regex_constants::syntax_type syntax_type(char c)const
375 return m_char_map[static_cast<unsigned char>(c)];
377 regex_constants::escape_syntax_type escape_syntax_type(char c) const
379 return m_char_map[static_cast<unsigned char>(c)];
383 regex_constants::syntax_type m_char_map[1u << CHAR_BIT];
387 #ifdef BOOST_REGEX_BUGGY_CTYPE_FACET
390 char_class_space=1<<0,
391 char_class_print=1<<1,
392 char_class_cntrl=1<<2,
393 char_class_upper=1<<3,
394 char_class_lower=1<<4,
395 char_class_alpha=1<<5,
396 char_class_digit=1<<6,
397 char_class_punct=1<<7,
398 char_class_xdigit=1<<8,
399 char_class_alnum=char_class_alpha|char_class_digit,
400 char_class_graph=char_class_alnum|char_class_punct,
401 char_class_blank=1<<9,
402 char_class_word=1<<10,
403 char_class_unicode=1<<11,
404 char_class_horizontal_space=1<<12,
405 char_class_vertical_space=1<<13
411 // class cpp_regex_traits_implementation:
412 // provides pimpl implementation for cpp_regex_traits.
414 template <class charT>
415 class cpp_regex_traits_implementation : public cpp_regex_traits_char_layer<charT>
418 typedef typename cpp_regex_traits<charT>::char_class_type char_class_type;
419 typedef typename std::ctype<charT>::mask native_mask_type;
420 #ifndef BOOST_REGEX_BUGGY_CTYPE_FACET
421 BOOST_STATIC_CONSTANT(char_class_type, mask_blank = 1u << 24);
422 BOOST_STATIC_CONSTANT(char_class_type, mask_word = 1u << 25);
423 BOOST_STATIC_CONSTANT(char_class_type, mask_unicode = 1u << 26);
424 BOOST_STATIC_CONSTANT(char_class_type, mask_horizontal = 1u << 27);
425 BOOST_STATIC_CONSTANT(char_class_type, mask_vertical = 1u << 28);
428 typedef std::basic_string<charT> string_type;
429 typedef charT char_type;
430 //cpp_regex_traits_implementation();
431 cpp_regex_traits_implementation(const std::locale& l)
432 : cpp_regex_traits_char_layer<charT>(l)
436 cpp_regex_traits_implementation(const cpp_regex_traits_base<charT>& l)
437 : cpp_regex_traits_char_layer<charT>(l)
441 std::string error_string(regex_constants::error_type n) const
443 if(!m_error_strings.empty())
445 std::map<int, std::string>::const_iterator p = m_error_strings.find(n);
446 return (p == m_error_strings.end()) ? std::string(get_default_error_string(n)) : p->second;
448 return get_default_error_string(n);
450 char_class_type lookup_classname(const charT* p1, const charT* p2) const
452 char_class_type result = lookup_classname_imp(p1, p2);
455 string_type temp(p1, p2);
456 this->m_pctype->tolower(&*temp.begin(), &*temp.begin() + temp.size());
457 result = lookup_classname_imp(&*temp.begin(), &*temp.begin() + temp.size());
461 string_type lookup_collatename(const charT* p1, const charT* p2) const;
462 string_type transform_primary(const charT* p1, const charT* p2) const;
463 string_type transform(const charT* p1, const charT* p2) const;
465 std::map<int, std::string> m_error_strings; // error messages indexed by numberic ID
466 std::map<string_type, char_class_type> m_custom_class_names; // character class names
467 std::map<string_type, string_type> m_custom_collate_names; // collating element names
468 unsigned m_collate_type; // the form of the collation string
469 charT m_collate_delim; // the collation group delimiter
473 char_class_type lookup_classname_imp(const charT* p1, const charT* p2) const;
475 #ifdef BOOST_REGEX_BUGGY_CTYPE_FACET
477 bool isctype(charT c, char_class_type m)const;
481 #ifndef BOOST_REGEX_BUGGY_CTYPE_FACET
482 #if !defined(BOOST_NO_INCLASS_MEMBER_INITIALIZATION)
484 template <class charT>
485 typename cpp_regex_traits_implementation<charT>::char_class_type const cpp_regex_traits_implementation<charT>::mask_blank;
486 template <class charT>
487 typename cpp_regex_traits_implementation<charT>::char_class_type const cpp_regex_traits_implementation<charT>::mask_word;
488 template <class charT>
489 typename cpp_regex_traits_implementation<charT>::char_class_type const cpp_regex_traits_implementation<charT>::mask_unicode;
490 template <class charT>
491 typename cpp_regex_traits_implementation<charT>::char_class_type const cpp_regex_traits_implementation<charT>::mask_vertical;
492 template <class charT>
493 typename cpp_regex_traits_implementation<charT>::char_class_type const cpp_regex_traits_implementation<charT>::mask_horizontal;
498 template <class charT>
499 typename cpp_regex_traits_implementation<charT>::string_type
500 cpp_regex_traits_implementation<charT>::transform_primary(const charT* p1, const charT* p2) const
505 // A bug in gcc 3.2 (and maybe other versions as well) treats
506 // p1 as a null terminated string, for efficiency reasons
507 // we work around this elsewhere, but just assert here that
508 // we adhere to gcc's (buggy) preconditions...
510 BOOST_ASSERT(*p2 == 0);
512 #if defined(_CPPLIB_VER)
514 // A bug in VC11 and 12 causes the program to hang if we pass a null-string
515 // to std::collate::transform, but only for certain locales :-(
516 // Probably effects Intel and Clang or any compiler using the VC std library (Dinkumware).
520 return string_type(1, charT(0));
524 // swallowing all exceptions here is a bad idea
525 // however at least one std lib will always throw
526 // std::bad_alloc for certain arguments...
528 #ifndef BOOST_NO_EXCEPTIONS
532 // What we do here depends upon the format of the sort key returned by
533 // sort key returned by this->transform:
535 switch(m_collate_type)
539 // the best we can do is translate to lower case, then get a regular sort key:
541 result.assign(p1, p2);
542 this->m_pctype->tolower(&*result.begin(), &*result.begin() + result.size());
543 result = this->m_pcollate->transform(&*result.begin(), &*result.begin() + result.size());
548 // get a regular sort key, and then truncate it:
549 result.assign(this->m_pcollate->transform(p1, p2));
550 result.erase(this->m_collate_delim);
554 // get a regular sort key, and then truncate everything after the delim:
555 result.assign(this->m_pcollate->transform(p1, p2));
557 for(i = 0; i < result.size(); ++i)
559 if(result[i] == m_collate_delim)
565 #ifndef BOOST_NO_EXCEPTIONS
568 while(result.size() && (charT(0) == *result.rbegin()))
569 result.erase(result.size() - 1);
572 // character is ignorable at the primary level:
573 result = string_type(1, charT(0));
578 template <class charT>
579 typename cpp_regex_traits_implementation<charT>::string_type
580 cpp_regex_traits_implementation<charT>::transform(const charT* p1, const charT* p2) const
585 // A bug in gcc 3.2 (and maybe other versions as well) treats
586 // p1 as a null terminated string, for efficiency reasons
587 // we work around this elsewhere, but just assert here that
588 // we adhere to gcc's (buggy) preconditions...
590 BOOST_ASSERT(*p2 == 0);
592 // swallowing all exceptions here is a bad idea
593 // however at least one std lib will always throw
594 // std::bad_alloc for certain arguments...
596 string_type result, result2;
597 #if defined(_CPPLIB_VER)
599 // A bug in VC11 and 12 causes the program to hang if we pass a null-string
600 // to std::collate::transform, but only for certain locales :-(
601 // Probably effects Intel and Clang or any compiler using the VC std library (Dinkumware).
608 #ifndef BOOST_NO_EXCEPTIONS
611 result = this->m_pcollate->transform(p1, p2);
613 // Borland's STLPort version returns a NULL-terminated
614 // string that has garbage at the end - each call to
615 // std::collate<wchar_t>::transform returns a different string!
616 // So as a workaround, we'll truncate the string at the first NULL
617 // which _seems_ to work....
618 #if BOOST_WORKAROUND(__BORLANDC__, < 0x580)
619 result.erase(result.find(charT(0)));
622 // some implementations (Dinkumware) append unnecessary trailing \0's:
623 while(result.size() && (charT(0) == *result.rbegin()))
624 result.erase(result.size() - 1);
627 // We may have NULL's used as separators between sections of the collate string,
628 // an example would be Boost.Locale. We have no way to detect this case via
629 // #defines since this can be used with any compiler/platform combination.
630 // Unfortunately our state machine (which was devised when all implementations
631 // used underlying C language API's) can't cope with that case. One workaround
632 // is to replace each character with 2, fortunately this code isn't used that
633 // much as this is now slower than before :-(
635 typedef typename make_unsigned<charT>::type uchar_type;
636 result2.reserve(result.size() * 2 + 2);
637 for(unsigned i = 0; i < result.size(); ++i)
639 if(static_cast<uchar_type>(result[i]) == (std::numeric_limits<uchar_type>::max)())
641 result2.append(1, charT((std::numeric_limits<uchar_type>::max)())).append(1, charT('b'));
645 result2.append(1, static_cast<charT>(1 + static_cast<uchar_type>(result[i]))).append(1, charT('b') - 1);
648 BOOST_ASSERT(std::find(result2.begin(), result2.end(), charT(0)) == result2.end());
649 #ifndef BOOST_NO_EXCEPTIONS
659 template <class charT>
660 typename cpp_regex_traits_implementation<charT>::string_type
661 cpp_regex_traits_implementation<charT>::lookup_collatename(const charT* p1, const charT* p2) const
663 typedef typename std::map<string_type, string_type>::const_iterator iter_type;
664 if(m_custom_collate_names.size())
666 iter_type pos = m_custom_collate_names.find(string_type(p1, p2));
667 if(pos != m_custom_collate_names.end())
670 #if !defined(BOOST_NO_TEMPLATED_ITERATOR_CONSTRUCTORS)\
671 && !BOOST_WORKAROUND(__BORLANDC__, <= 0x0551)
672 std::string name(p1, p2);
675 const charT* p0 = p1;
677 name.append(1, char(*p0++));
679 name = lookup_default_collate_name(name);
680 #if !defined(BOOST_NO_TEMPLATED_ITERATOR_CONSTRUCTORS)\
681 && !BOOST_WORKAROUND(__BORLANDC__, <= 0x0551)
683 return string_type(name.begin(), name.end());
688 typedef std::string::const_iterator iter;
689 iter b = name.begin();
692 result.append(1, charT(*b++));
697 return string_type(1, *p1);
698 return string_type();
701 template <class charT>
702 void cpp_regex_traits_implementation<charT>::init()
704 #ifndef BOOST_NO_STD_MESSAGES
706 typename std::messages<charT>::catalog cat = static_cast<std::messages<char>::catalog>(-1);
708 typename std::messages<charT>::catalog cat = reinterpret_cast<std::messages<char>::catalog>(-1);
710 std::string cat_name(cpp_regex_traits<charT>::get_catalog_name());
711 if(cat_name.size() && (this->m_pmessages != 0))
713 cat = this->m_pmessages->open(
718 std::string m("Unable to open message catalog: ");
719 std::runtime_error err(m + cat_name);
720 boost::re_detail::raise_runtime_error(err);
724 // if we have a valid catalog then load our messages:
731 for(boost::regex_constants::error_type i = static_cast<boost::regex_constants::error_type>(0);
732 i <= boost::regex_constants::error_unknown;
733 i = static_cast<boost::regex_constants::error_type>(i + 1))
735 const char* p = get_default_error_string(i);
736 string_type default_message;
739 default_message.append(1, this->m_pctype->widen(*p));
742 string_type s = this->m_pmessages->get(cat, 0, i+200, default_message);
744 for(std::string::size_type j = 0; j < s.size(); ++j)
746 result.append(1, this->m_pctype->narrow(s[j], 0));
748 m_error_strings[i] = result;
751 // Custom class names:
753 #ifndef BOOST_REGEX_BUGGY_CTYPE_FACET
754 static const char_class_type masks[16] =
756 std::ctype<charT>::alnum,
757 std::ctype<charT>::alpha,
758 std::ctype<charT>::cntrl,
759 std::ctype<charT>::digit,
760 std::ctype<charT>::graph,
761 cpp_regex_traits_implementation<charT>::mask_horizontal,
762 std::ctype<charT>::lower,
763 std::ctype<charT>::print,
764 std::ctype<charT>::punct,
765 std::ctype<charT>::space,
766 std::ctype<charT>::upper,
767 cpp_regex_traits_implementation<charT>::mask_vertical,
768 std::ctype<charT>::xdigit,
769 cpp_regex_traits_implementation<charT>::mask_blank,
770 cpp_regex_traits_implementation<charT>::mask_word,
771 cpp_regex_traits_implementation<charT>::mask_unicode,
774 static const char_class_type masks[16] =
776 ::boost::re_detail::char_class_alnum,
777 ::boost::re_detail::char_class_alpha,
778 ::boost::re_detail::char_class_cntrl,
779 ::boost::re_detail::char_class_digit,
780 ::boost::re_detail::char_class_graph,
781 ::boost::re_detail::char_class_horizontal_space,
782 ::boost::re_detail::char_class_lower,
783 ::boost::re_detail::char_class_print,
784 ::boost::re_detail::char_class_punct,
785 ::boost::re_detail::char_class_space,
786 ::boost::re_detail::char_class_upper,
787 ::boost::re_detail::char_class_vertical_space,
788 ::boost::re_detail::char_class_xdigit,
789 ::boost::re_detail::char_class_blank,
790 ::boost::re_detail::char_class_word,
791 ::boost::re_detail::char_class_unicode,
794 static const string_type null_string;
795 for(unsigned int j = 0; j <= 13; ++j)
797 string_type s(this->m_pmessages->get(cat, 0, j+300, null_string));
799 this->m_custom_class_names[s] = masks[j];
804 // get the collation format used by m_pcollate:
806 m_collate_type = re_detail::find_sort_syntax(this, &m_collate_delim);
809 template <class charT>
810 typename cpp_regex_traits_implementation<charT>::char_class_type
811 cpp_regex_traits_implementation<charT>::lookup_classname_imp(const charT* p1, const charT* p2) const
813 #ifndef BOOST_REGEX_BUGGY_CTYPE_FACET
814 static const char_class_type masks[22] =
817 std::ctype<char>::alnum,
818 std::ctype<char>::alpha,
819 cpp_regex_traits_implementation<charT>::mask_blank,
820 std::ctype<char>::cntrl,
821 std::ctype<char>::digit,
822 std::ctype<char>::digit,
823 std::ctype<char>::graph,
824 cpp_regex_traits_implementation<charT>::mask_horizontal,
825 std::ctype<char>::lower,
826 std::ctype<char>::lower,
827 std::ctype<char>::print,
828 std::ctype<char>::punct,
829 std::ctype<char>::space,
830 std::ctype<char>::space,
831 std::ctype<char>::upper,
832 cpp_regex_traits_implementation<charT>::mask_unicode,
833 std::ctype<char>::upper,
834 cpp_regex_traits_implementation<charT>::mask_vertical,
835 std::ctype<char>::alnum | cpp_regex_traits_implementation<charT>::mask_word,
836 std::ctype<char>::alnum | cpp_regex_traits_implementation<charT>::mask_word,
837 std::ctype<char>::xdigit,
840 static const char_class_type masks[22] =
843 ::boost::re_detail::char_class_alnum,
844 ::boost::re_detail::char_class_alpha,
845 ::boost::re_detail::char_class_blank,
846 ::boost::re_detail::char_class_cntrl,
847 ::boost::re_detail::char_class_digit,
848 ::boost::re_detail::char_class_digit,
849 ::boost::re_detail::char_class_graph,
850 ::boost::re_detail::char_class_horizontal_space,
851 ::boost::re_detail::char_class_lower,
852 ::boost::re_detail::char_class_lower,
853 ::boost::re_detail::char_class_print,
854 ::boost::re_detail::char_class_punct,
855 ::boost::re_detail::char_class_space,
856 ::boost::re_detail::char_class_space,
857 ::boost::re_detail::char_class_upper,
858 ::boost::re_detail::char_class_unicode,
859 ::boost::re_detail::char_class_upper,
860 ::boost::re_detail::char_class_vertical_space,
861 ::boost::re_detail::char_class_alnum | ::boost::re_detail::char_class_word,
862 ::boost::re_detail::char_class_alnum | ::boost::re_detail::char_class_word,
863 ::boost::re_detail::char_class_xdigit,
866 if(m_custom_class_names.size())
868 typedef typename std::map<std::basic_string<charT>, char_class_type>::const_iterator map_iter;
869 map_iter pos = m_custom_class_names.find(string_type(p1, p2));
870 if(pos != m_custom_class_names.end())
873 std::size_t state_id = 1 + re_detail::get_default_class_id(p1, p2);
874 BOOST_ASSERT(state_id < sizeof(masks) / sizeof(masks[0]));
875 return masks[state_id];
878 #ifdef BOOST_REGEX_BUGGY_CTYPE_FACET
879 template <class charT>
880 bool cpp_regex_traits_implementation<charT>::isctype(const charT c, char_class_type mask) const
883 ((mask & ::boost::re_detail::char_class_space) && (this->m_pctype->is(std::ctype<charT>::space, c)))
884 || ((mask & ::boost::re_detail::char_class_print) && (this->m_pctype->is(std::ctype<charT>::print, c)))
885 || ((mask & ::boost::re_detail::char_class_cntrl) && (this->m_pctype->is(std::ctype<charT>::cntrl, c)))
886 || ((mask & ::boost::re_detail::char_class_upper) && (this->m_pctype->is(std::ctype<charT>::upper, c)))
887 || ((mask & ::boost::re_detail::char_class_lower) && (this->m_pctype->is(std::ctype<charT>::lower, c)))
888 || ((mask & ::boost::re_detail::char_class_alpha) && (this->m_pctype->is(std::ctype<charT>::alpha, c)))
889 || ((mask & ::boost::re_detail::char_class_digit) && (this->m_pctype->is(std::ctype<charT>::digit, c)))
890 || ((mask & ::boost::re_detail::char_class_punct) && (this->m_pctype->is(std::ctype<charT>::punct, c)))
891 || ((mask & ::boost::re_detail::char_class_xdigit) && (this->m_pctype->is(std::ctype<charT>::xdigit, c)))
892 || ((mask & ::boost::re_detail::char_class_blank) && (this->m_pctype->is(std::ctype<charT>::space, c)) && !::boost::re_detail::is_separator(c))
893 || ((mask & ::boost::re_detail::char_class_word) && (c == '_'))
894 || ((mask & ::boost::re_detail::char_class_unicode) && ::boost::re_detail::is_extended(c))
895 || ((mask & ::boost::re_detail::char_class_vertical_space) && (is_separator(c) || (c == '\v')))
896 || ((mask & ::boost::re_detail::char_class_horizontal_space) && this->m_pctype->is(std::ctype<charT>::space, c) && !(is_separator(c) || (c == '\v')));
901 template <class charT>
902 inline boost::shared_ptr<const cpp_regex_traits_implementation<charT> > create_cpp_regex_traits(const std::locale& l)
904 cpp_regex_traits_base<charT> key(l);
905 return ::boost::object_cache<cpp_regex_traits_base<charT>, cpp_regex_traits_implementation<charT> >::get(key, 5);
910 template <class charT>
911 class cpp_regex_traits
914 typedef std::ctype<charT> ctype_type;
916 typedef charT char_type;
917 typedef std::size_t size_type;
918 typedef std::basic_string<char_type> string_type;
919 typedef std::locale locale_type;
920 typedef boost::uint_least32_t char_class_type;
922 struct boost_extensions_tag{};
925 : m_pimpl(re_detail::create_cpp_regex_traits<charT>(std::locale()))
927 static size_type length(const char_type* p)
929 return std::char_traits<charT>::length(p);
931 regex_constants::syntax_type syntax_type(charT c)const
933 return m_pimpl->syntax_type(c);
935 regex_constants::escape_syntax_type escape_syntax_type(charT c) const
937 return m_pimpl->escape_syntax_type(c);
939 charT translate(charT c) const
943 charT translate_nocase(charT c) const
945 return m_pimpl->m_pctype->tolower(c);
947 charT translate(charT c, bool icase) const
949 return icase ? m_pimpl->m_pctype->tolower(c) : c;
951 charT tolower(charT c) const
953 return m_pimpl->m_pctype->tolower(c);
955 charT toupper(charT c) const
957 return m_pimpl->m_pctype->toupper(c);
959 string_type transform(const charT* p1, const charT* p2) const
961 return m_pimpl->transform(p1, p2);
963 string_type transform_primary(const charT* p1, const charT* p2) const
965 return m_pimpl->transform_primary(p1, p2);
967 char_class_type lookup_classname(const charT* p1, const charT* p2) const
969 return m_pimpl->lookup_classname(p1, p2);
971 string_type lookup_collatename(const charT* p1, const charT* p2) const
973 return m_pimpl->lookup_collatename(p1, p2);
975 bool isctype(charT c, char_class_type f) const
977 #ifndef BOOST_REGEX_BUGGY_CTYPE_FACET
978 typedef typename std::ctype<charT>::mask ctype_mask;
980 static const ctype_mask mask_base =
981 static_cast<ctype_mask>(
982 std::ctype<charT>::alnum
983 | std::ctype<charT>::alpha
984 | std::ctype<charT>::cntrl
985 | std::ctype<charT>::digit
986 | std::ctype<charT>::graph
987 | std::ctype<charT>::lower
988 | std::ctype<charT>::print
989 | std::ctype<charT>::punct
990 | std::ctype<charT>::space
991 | std::ctype<charT>::upper
992 | std::ctype<charT>::xdigit);
995 && (m_pimpl->m_pctype->is(
996 static_cast<ctype_mask>(f & mask_base), c)))
998 else if((f & re_detail::cpp_regex_traits_implementation<charT>::mask_unicode) && re_detail::is_extended(c))
1000 else if((f & re_detail::cpp_regex_traits_implementation<charT>::mask_word) && (c == '_'))
1002 else if((f & re_detail::cpp_regex_traits_implementation<charT>::mask_blank)
1003 && m_pimpl->m_pctype->is(std::ctype<charT>::space, c)
1004 && !re_detail::is_separator(c))
1006 else if((f & re_detail::cpp_regex_traits_implementation<charT>::mask_vertical)
1007 && (::boost::re_detail::is_separator(c) || (c == '\v')))
1009 else if((f & re_detail::cpp_regex_traits_implementation<charT>::mask_horizontal)
1010 && this->isctype(c, std::ctype<charT>::space) && !this->isctype(c, re_detail::cpp_regex_traits_implementation<charT>::mask_vertical))
1014 return m_pimpl->isctype(c, f);
1017 int toi(const charT*& p1, const charT* p2, int radix)const;
1018 int value(charT c, int radix)const
1020 const charT* pc = &c;
1021 return toi(pc, pc + 1, radix);
1023 locale_type imbue(locale_type l)
1025 std::locale result(getloc());
1026 m_pimpl = re_detail::create_cpp_regex_traits<charT>(l);
1029 locale_type getloc()const
1031 return m_pimpl->m_locale;
1033 std::string error_string(regex_constants::error_type n) const
1035 return m_pimpl->error_string(n);
1040 // set the name of the message catalog in use (defaults to "boost_regex").
1042 static std::string catalog_name(const std::string& name);
1043 static std::string get_catalog_name();
1046 boost::shared_ptr<const re_detail::cpp_regex_traits_implementation<charT> > m_pimpl;
1048 // catalog name handler:
1050 static std::string& get_catalog_name_inst();
1052 #ifdef BOOST_HAS_THREADS
1053 static static_mutex& get_mutex_inst();
1058 template <class charT>
1059 int cpp_regex_traits<charT>::toi(const charT*& first, const charT* last, int radix)const
1061 re_detail::parser_buf<charT> sbuf; // buffer for parsing numbers.
1062 std::basic_istream<charT> is(&sbuf); // stream for parsing numbers.
1064 // we do NOT want to parse any thousands separators inside the stream:
1065 last = std::find(first, last, BOOST_USE_FACET(std::numpunct<charT>, is.getloc()).thousands_sep());
1067 sbuf.pubsetbuf(const_cast<charT*>(static_cast<const charT*>(first)), static_cast<std::streamsize>(last-first));
1069 if(std::abs(radix) == 16) is >> std::hex;
1070 else if(std::abs(radix) == 8) is >> std::oct;
1071 else is >> std::dec;
1075 first = first + ((last - first) - sbuf.in_avail());
1082 template <class charT>
1083 std::string cpp_regex_traits<charT>::catalog_name(const std::string& name)
1085 #ifdef BOOST_HAS_THREADS
1086 static_mutex::scoped_lock lk(get_mutex_inst());
1088 std::string result(get_catalog_name_inst());
1089 get_catalog_name_inst() = name;
1093 template <class charT>
1094 std::string& cpp_regex_traits<charT>::get_catalog_name_inst()
1096 static std::string s_name;
1100 template <class charT>
1101 std::string cpp_regex_traits<charT>::get_catalog_name()
1103 #ifdef BOOST_HAS_THREADS
1104 static_mutex::scoped_lock lk(get_mutex_inst());
1106 std::string result(get_catalog_name_inst());
1110 #ifdef BOOST_HAS_THREADS
1111 template <class charT>
1112 static_mutex& cpp_regex_traits<charT>::get_mutex_inst()
1114 static static_mutex s_mutex = BOOST_STATIC_MUTEX_INIT;
1123 #pragma warning(pop)
1127 #pragma warning(push)
1128 #pragma warning(disable: 4103)
1130 #ifdef BOOST_HAS_ABI_HEADERS
1131 # include BOOST_ABI_SUFFIX
1134 #pragma warning(pop)