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>
24 #ifndef BOOST_NO_STD_LOCALE
26 #ifndef BOOST_RE_PAT_EXCEPT_HPP
27 #include <boost/regex/pattern_except.hpp>
29 #ifndef BOOST_REGEX_TRAITS_DEFAULTS_HPP_INCLUDED
30 #include <boost/regex/v4/regex_traits_defaults.hpp>
32 #ifdef BOOST_HAS_THREADS
33 #include <boost/regex/pending/static_mutex.hpp>
35 #ifndef BOOST_REGEX_PRIMARY_TRANSFORM
36 #include <boost/regex/v4/primary_transform.hpp>
38 #ifndef BOOST_REGEX_OBJECT_CACHE_HPP
39 #include <boost/regex/pending/object_cache.hpp>
48 #pragma warning(disable: 4103)
50 #ifdef BOOST_HAS_ABI_HEADERS
51 # include BOOST_ABI_PREFIX
59 #pragma warning(disable:4786)
65 // forward declaration is needed by some compilers:
67 template <class charT>
68 class cpp_regex_traits;
74 // acts as a stream buffer which wraps around a pair of pointers:
76 template <class charT,
77 class traits = ::std::char_traits<charT> >
78 class parser_buf : public ::std::basic_streambuf<charT, traits>
80 typedef ::std::basic_streambuf<charT, traits> base_type;
81 typedef typename base_type::int_type int_type;
82 typedef typename base_type::char_type char_type;
83 typedef typename base_type::pos_type pos_type;
84 typedef ::std::streamsize streamsize;
85 typedef typename base_type::off_type off_type;
87 parser_buf() : base_type() { setbuf(0, 0); }
88 const charT* getnext() { return this->gptr(); }
90 std::basic_streambuf<charT, traits>* setbuf(char_type* s, streamsize n);
91 typename parser_buf<charT, traits>::pos_type seekpos(pos_type sp, ::std::ios_base::openmode which);
92 typename parser_buf<charT, traits>::pos_type seekoff(off_type off, ::std::ios_base::seekdir way, ::std::ios_base::openmode which);
94 parser_buf& operator=(const parser_buf&);
95 parser_buf(const parser_buf&);
98 template<class charT, class traits>
99 std::basic_streambuf<charT, traits>*
100 parser_buf<charT, traits>::setbuf(char_type* s, streamsize n)
102 this->setg(s, s, s + n);
106 template<class charT, class traits>
107 typename parser_buf<charT, traits>::pos_type
108 parser_buf<charT, traits>::seekoff(off_type off, ::std::ios_base::seekdir way, ::std::ios_base::openmode which)
110 if(which & ::std::ios_base::out)
111 return pos_type(off_type(-1));
112 std::ptrdiff_t size = this->egptr() - this->eback();
113 std::ptrdiff_t pos = this->gptr() - this->eback();
114 charT* g = this->eback();
117 case ::std::ios_base::beg:
118 if((off < 0) || (off > size))
119 return pos_type(off_type(-1));
121 this->setg(g, g + off, g + size);
123 case ::std::ios_base::end:
124 if((off < 0) || (off > size))
125 return pos_type(off_type(-1));
127 this->setg(g, g + size - off, g + size);
129 case ::std::ios_base::cur:
131 std::ptrdiff_t newpos = static_cast<std::ptrdiff_t>(pos + off);
132 if((newpos < 0) || (newpos > size))
133 return pos_type(off_type(-1));
135 this->setg(g, g + newpos, g + size);
141 #pragma warning(push)
142 #pragma warning(disable:4244)
144 return static_cast<pos_type>(this->gptr() - this->eback());
150 template<class charT, class traits>
151 typename parser_buf<charT, traits>::pos_type
152 parser_buf<charT, traits>::seekpos(pos_type sp, ::std::ios_base::openmode which)
154 if(which & ::std::ios_base::out)
155 return pos_type(off_type(-1));
156 off_type size = static_cast<off_type>(this->egptr() - this->eback());
157 charT* g = this->eback();
158 if(off_type(sp) <= size)
160 this->setg(g, g + off_type(sp), g + size);
162 return pos_type(off_type(-1));
166 // class cpp_regex_traits_base:
167 // acts as a container for locale and the facets we are using.
169 template <class charT>
170 struct cpp_regex_traits_base
172 cpp_regex_traits_base(const std::locale& l)
174 std::locale imbue(const std::locale& l);
176 std::locale m_locale;
177 std::ctype<charT> const* m_pctype;
178 #ifndef BOOST_NO_STD_MESSAGES
179 std::messages<charT> const* m_pmessages;
181 std::collate<charT> const* m_pcollate;
183 bool operator<(const cpp_regex_traits_base& b)const
185 if(m_pctype == b.m_pctype)
187 #ifndef BOOST_NO_STD_MESSAGES
188 if(m_pmessages == b.m_pmessages)
190 return m_pcollate < b.m_pcollate;
192 return m_pmessages < b.m_pmessages;
194 return m_pcollate < b.m_pcollate;
197 return m_pctype < b.m_pctype;
199 bool operator==(const cpp_regex_traits_base& b)const
201 return (m_pctype == b.m_pctype)
202 #ifndef BOOST_NO_STD_MESSAGES
203 && (m_pmessages == b.m_pmessages)
205 && (m_pcollate == b.m_pcollate);
209 template <class charT>
210 std::locale cpp_regex_traits_base<charT>::imbue(const std::locale& l)
212 std::locale result(m_locale);
214 m_pctype = &BOOST_USE_FACET(std::ctype<charT>, l);
215 #ifndef BOOST_NO_STD_MESSAGES
216 m_pmessages = BOOST_HAS_FACET(std::messages<charT>, l) ? &BOOST_USE_FACET(std::messages<charT>, l) : 0;
218 m_pcollate = &BOOST_USE_FACET(std::collate<charT>, l);
223 // class cpp_regex_traits_char_layer:
224 // implements methods that require specialisation for narrow characters:
226 template <class charT>
227 class cpp_regex_traits_char_layer : public cpp_regex_traits_base<charT>
229 typedef std::basic_string<charT> string_type;
230 typedef std::map<charT, regex_constants::syntax_type> map_type;
231 typedef typename map_type::const_iterator map_iterator_type;
233 cpp_regex_traits_char_layer(const std::locale& l)
234 : cpp_regex_traits_base<charT>(l)
238 cpp_regex_traits_char_layer(const cpp_regex_traits_base<charT>& b)
239 : cpp_regex_traits_base<charT>(b)
245 regex_constants::syntax_type syntax_type(charT c)const
247 map_iterator_type i = m_char_map.find(c);
248 return ((i == m_char_map.end()) ? 0 : i->second);
250 regex_constants::escape_syntax_type escape_syntax_type(charT c) const
252 map_iterator_type i = m_char_map.find(c);
253 if(i == m_char_map.end())
255 if(this->m_pctype->is(std::ctype_base::lower, c)) return regex_constants::escape_type_class;
256 if(this->m_pctype->is(std::ctype_base::upper, c)) return regex_constants::escape_type_not_class;
263 string_type get_default_message(regex_constants::syntax_type);
264 // TODO: use a hash table when available!
268 template <class charT>
269 void cpp_regex_traits_char_layer<charT>::init()
271 // we need to start by initialising our syntax map so we know which
272 // character is used for which purpose:
273 #ifndef BOOST_NO_STD_MESSAGES
275 typename std::messages<charT>::catalog cat = static_cast<std::messages<char>::catalog>(-1);
277 typename std::messages<charT>::catalog cat = reinterpret_cast<std::messages<char>::catalog>(-1);
279 std::string cat_name(cpp_regex_traits<charT>::get_catalog_name());
280 if(cat_name.size() && (this->m_pmessages != 0))
282 cat = this->m_pmessages->open(
287 std::string m("Unable to open message catalog: ");
288 std::runtime_error err(m + cat_name);
289 boost::re_detail::raise_runtime_error(err);
293 // if we have a valid catalog then load our messages:
297 #ifndef BOOST_NO_EXCEPTIONS
300 for(regex_constants::syntax_type i = 1; i < regex_constants::syntax_max; ++i)
302 string_type mss = this->m_pmessages->get(cat, 0, i, get_default_message(i));
303 for(typename string_type::size_type j = 0; j < mss.size(); ++j)
305 m_char_map[mss[j]] = i;
308 this->m_pmessages->close(cat);
309 #ifndef BOOST_NO_EXCEPTIONS
313 if(this->m_pmessages)
314 this->m_pmessages->close(cat);
322 for(regex_constants::syntax_type i = 1; i < regex_constants::syntax_max; ++i)
324 const char* ptr = get_default_syntax(i);
327 m_char_map[this->m_pctype->widen(*ptr)] = i;
331 #ifndef BOOST_NO_STD_MESSAGES
336 template <class charT>
337 typename cpp_regex_traits_char_layer<charT>::string_type
338 cpp_regex_traits_char_layer<charT>::get_default_message(regex_constants::syntax_type i)
340 const char* ptr = get_default_syntax(i);
344 result.append(1, this->m_pctype->widen(*ptr));
351 // specialised version for narrow characters:
354 class BOOST_REGEX_DECL cpp_regex_traits_char_layer<char> : public cpp_regex_traits_base<char>
356 typedef std::string string_type;
358 cpp_regex_traits_char_layer(const std::locale& l)
359 : cpp_regex_traits_base<char>(l)
363 cpp_regex_traits_char_layer(const cpp_regex_traits_base<char>& l)
364 : cpp_regex_traits_base<char>(l)
369 regex_constants::syntax_type syntax_type(char c)const
371 return m_char_map[static_cast<unsigned char>(c)];
373 regex_constants::escape_syntax_type escape_syntax_type(char c) const
375 return m_char_map[static_cast<unsigned char>(c)];
379 regex_constants::syntax_type m_char_map[1u << CHAR_BIT];
383 #ifdef BOOST_REGEX_BUGGY_CTYPE_FACET
386 char_class_space=1<<0,
387 char_class_print=1<<1,
388 char_class_cntrl=1<<2,
389 char_class_upper=1<<3,
390 char_class_lower=1<<4,
391 char_class_alpha=1<<5,
392 char_class_digit=1<<6,
393 char_class_punct=1<<7,
394 char_class_xdigit=1<<8,
395 char_class_alnum=char_class_alpha|char_class_digit,
396 char_class_graph=char_class_alnum|char_class_punct,
397 char_class_blank=1<<9,
398 char_class_word=1<<10,
399 char_class_unicode=1<<11,
400 char_class_horizontal_space=1<<12,
401 char_class_vertical_space=1<<13
407 // class cpp_regex_traits_implementation:
408 // provides pimpl implementation for cpp_regex_traits.
410 template <class charT>
411 class cpp_regex_traits_implementation : public cpp_regex_traits_char_layer<charT>
414 typedef typename cpp_regex_traits<charT>::char_class_type char_class_type;
415 typedef typename std::ctype<charT>::mask native_mask_type;
416 #ifndef BOOST_REGEX_BUGGY_CTYPE_FACET
417 BOOST_STATIC_CONSTANT(char_class_type, mask_blank = 1u << 24);
418 BOOST_STATIC_CONSTANT(char_class_type, mask_word = 1u << 25);
419 BOOST_STATIC_CONSTANT(char_class_type, mask_unicode = 1u << 26);
420 BOOST_STATIC_CONSTANT(char_class_type, mask_horizontal = 1u << 27);
421 BOOST_STATIC_CONSTANT(char_class_type, mask_vertical = 1u << 28);
424 typedef std::basic_string<charT> string_type;
425 typedef charT char_type;
426 //cpp_regex_traits_implementation();
427 cpp_regex_traits_implementation(const std::locale& l)
428 : cpp_regex_traits_char_layer<charT>(l)
432 cpp_regex_traits_implementation(const cpp_regex_traits_base<charT>& l)
433 : cpp_regex_traits_char_layer<charT>(l)
437 std::string error_string(regex_constants::error_type n) const
439 if(!m_error_strings.empty())
441 std::map<int, std::string>::const_iterator p = m_error_strings.find(n);
442 return (p == m_error_strings.end()) ? std::string(get_default_error_string(n)) : p->second;
444 return get_default_error_string(n);
446 char_class_type lookup_classname(const charT* p1, const charT* p2) const
448 char_class_type result = lookup_classname_imp(p1, p2);
451 string_type temp(p1, p2);
452 this->m_pctype->tolower(&*temp.begin(), &*temp.begin() + temp.size());
453 result = lookup_classname_imp(&*temp.begin(), &*temp.begin() + temp.size());
457 string_type lookup_collatename(const charT* p1, const charT* p2) const;
458 string_type transform_primary(const charT* p1, const charT* p2) const;
459 string_type transform(const charT* p1, const charT* p2) const;
461 std::map<int, std::string> m_error_strings; // error messages indexed by numberic ID
462 std::map<string_type, char_class_type> m_custom_class_names; // character class names
463 std::map<string_type, string_type> m_custom_collate_names; // collating element names
464 unsigned m_collate_type; // the form of the collation string
465 charT m_collate_delim; // the collation group delimiter
469 char_class_type lookup_classname_imp(const charT* p1, const charT* p2) const;
471 #ifdef BOOST_REGEX_BUGGY_CTYPE_FACET
473 bool isctype(charT c, char_class_type m)const;
477 #ifndef BOOST_REGEX_BUGGY_CTYPE_FACET
478 #if !defined(BOOST_NO_INCLASS_MEMBER_INITIALIZATION)
480 template <class charT>
481 typename cpp_regex_traits_implementation<charT>::char_class_type const cpp_regex_traits_implementation<charT>::mask_blank;
482 template <class charT>
483 typename cpp_regex_traits_implementation<charT>::char_class_type const cpp_regex_traits_implementation<charT>::mask_word;
484 template <class charT>
485 typename cpp_regex_traits_implementation<charT>::char_class_type const cpp_regex_traits_implementation<charT>::mask_unicode;
486 template <class charT>
487 typename cpp_regex_traits_implementation<charT>::char_class_type const cpp_regex_traits_implementation<charT>::mask_vertical;
488 template <class charT>
489 typename cpp_regex_traits_implementation<charT>::char_class_type const cpp_regex_traits_implementation<charT>::mask_horizontal;
494 template <class charT>
495 typename cpp_regex_traits_implementation<charT>::string_type
496 cpp_regex_traits_implementation<charT>::transform_primary(const charT* p1, const charT* p2) const
501 // A bug in gcc 3.2 (and maybe other versions as well) treats
502 // p1 as a null terminated string, for efficiency reasons
503 // we work around this elsewhere, but just assert here that
504 // we adhere to gcc's (buggy) preconditions...
506 BOOST_ASSERT(*p2 == 0);
510 // swallowing all exceptions here is a bad idea
511 // however at least one std lib will always throw
512 // std::bad_alloc for certain arguments...
516 // What we do here depends upon the format of the sort key returned by
517 // sort key returned by this->transform:
519 switch(m_collate_type)
523 // the best we can do is translate to lower case, then get a regular sort key:
525 result.assign(p1, p2);
526 this->m_pctype->tolower(&*result.begin(), &*result.begin() + result.size());
527 result = this->m_pcollate->transform(&*result.begin(), &*result.begin() + result.size());
532 // get a regular sort key, and then truncate it:
533 result.assign(this->m_pcollate->transform(p1, p2));
534 result.erase(this->m_collate_delim);
538 // get a regular sort key, and then truncate everything after the delim:
539 result.assign(this->m_pcollate->transform(p1, p2));
541 for(i = 0; i < result.size(); ++i)
543 if(result[i] == m_collate_delim)
550 while(result.size() && (charT(0) == *result.rbegin()))
551 result.erase(result.size() - 1);
554 // character is ignorable at the primary level:
555 result = string_type(1, charT(0));
560 template <class charT>
561 typename cpp_regex_traits_implementation<charT>::string_type
562 cpp_regex_traits_implementation<charT>::transform(const charT* p1, const charT* p2) const
567 // A bug in gcc 3.2 (and maybe other versions as well) treats
568 // p1 as a null terminated string, for efficiency reasons
569 // we work around this elsewhere, but just assert here that
570 // we adhere to gcc's (buggy) preconditions...
572 BOOST_ASSERT(*p2 == 0);
574 // swallowing all exceptions here is a bad idea
575 // however at least one std lib will always throw
576 // std::bad_alloc for certain arguments...
580 result = this->m_pcollate->transform(p1, p2);
582 // Borland's STLPort version returns a NULL-terminated
583 // string that has garbage at the end - each call to
584 // std::collate<wchar_t>::transform returns a different string!
585 // So as a workaround, we'll truncate the string at the first NULL
586 // which _seems_ to work....
587 #if BOOST_WORKAROUND(__BORLANDC__, < 0x580)
588 result.erase(result.find(charT(0)));
591 // some implementations (Dinkumware) append unnecessary trailing \0's:
592 while(result.size() && (charT(0) == *result.rbegin()))
593 result.erase(result.size() - 1);
595 BOOST_ASSERT(std::find(result.begin(), result.end(), charT(0)) == result.end());
604 template <class charT>
605 typename cpp_regex_traits_implementation<charT>::string_type
606 cpp_regex_traits_implementation<charT>::lookup_collatename(const charT* p1, const charT* p2) const
608 typedef typename std::map<string_type, string_type>::const_iterator iter_type;
609 if(m_custom_collate_names.size())
611 iter_type pos = m_custom_collate_names.find(string_type(p1, p2));
612 if(pos != m_custom_collate_names.end())
615 #if !defined(BOOST_NO_TEMPLATED_ITERATOR_CONSTRUCTORS)\
616 && !BOOST_WORKAROUND(BOOST_MSVC, < 1300)\
617 && !BOOST_WORKAROUND(__BORLANDC__, <= 0x0551)
618 std::string name(p1, p2);
621 const charT* p0 = p1;
623 name.append(1, char(*p0++));
625 name = lookup_default_collate_name(name);
626 #if !defined(BOOST_NO_TEMPLATED_ITERATOR_CONSTRUCTORS)\
627 && !BOOST_WORKAROUND(BOOST_MSVC, < 1300)\
628 && !BOOST_WORKAROUND(__BORLANDC__, <= 0x0551)
630 return string_type(name.begin(), name.end());
635 typedef std::string::const_iterator iter;
636 iter b = name.begin();
639 result.append(1, charT(*b++));
644 return string_type(1, *p1);
645 return string_type();
648 template <class charT>
649 void cpp_regex_traits_implementation<charT>::init()
651 #ifndef BOOST_NO_STD_MESSAGES
653 typename std::messages<charT>::catalog cat = static_cast<std::messages<char>::catalog>(-1);
655 typename std::messages<charT>::catalog cat = reinterpret_cast<std::messages<char>::catalog>(-1);
657 std::string cat_name(cpp_regex_traits<charT>::get_catalog_name());
658 if(cat_name.size() && (this->m_pmessages != 0))
660 cat = this->m_pmessages->open(
665 std::string m("Unable to open message catalog: ");
666 std::runtime_error err(m + cat_name);
667 boost::re_detail::raise_runtime_error(err);
671 // if we have a valid catalog then load our messages:
678 for(boost::regex_constants::error_type i = static_cast<boost::regex_constants::error_type>(0);
679 i <= boost::regex_constants::error_unknown;
680 i = static_cast<boost::regex_constants::error_type>(i + 1))
682 const char* p = get_default_error_string(i);
683 string_type default_message;
686 default_message.append(1, this->m_pctype->widen(*p));
689 string_type s = this->m_pmessages->get(cat, 0, i+200, default_message);
691 for(std::string::size_type j = 0; j < s.size(); ++j)
693 result.append(1, this->m_pctype->narrow(s[j], 0));
695 m_error_strings[i] = result;
698 // Custom class names:
700 #ifndef BOOST_REGEX_BUGGY_CTYPE_FACET
701 static const char_class_type masks[16] =
703 std::ctype<charT>::alnum,
704 std::ctype<charT>::alpha,
705 std::ctype<charT>::cntrl,
706 std::ctype<charT>::digit,
707 std::ctype<charT>::graph,
708 cpp_regex_traits_implementation<charT>::mask_horizontal,
709 std::ctype<charT>::lower,
710 std::ctype<charT>::print,
711 std::ctype<charT>::punct,
712 std::ctype<charT>::space,
713 std::ctype<charT>::upper,
714 cpp_regex_traits_implementation<charT>::mask_vertical,
715 std::ctype<charT>::xdigit,
716 cpp_regex_traits_implementation<charT>::mask_blank,
717 cpp_regex_traits_implementation<charT>::mask_word,
718 cpp_regex_traits_implementation<charT>::mask_unicode,
721 static const char_class_type masks[16] =
723 ::boost::re_detail::char_class_alnum,
724 ::boost::re_detail::char_class_alpha,
725 ::boost::re_detail::char_class_cntrl,
726 ::boost::re_detail::char_class_digit,
727 ::boost::re_detail::char_class_graph,
728 ::boost::re_detail::char_class_horizontal_space,
729 ::boost::re_detail::char_class_lower,
730 ::boost::re_detail::char_class_print,
731 ::boost::re_detail::char_class_punct,
732 ::boost::re_detail::char_class_space,
733 ::boost::re_detail::char_class_upper,
734 ::boost::re_detail::char_class_vertical_space,
735 ::boost::re_detail::char_class_xdigit,
736 ::boost::re_detail::char_class_blank,
737 ::boost::re_detail::char_class_word,
738 ::boost::re_detail::char_class_unicode,
741 static const string_type null_string;
742 for(unsigned int j = 0; j <= 13; ++j)
744 string_type s(this->m_pmessages->get(cat, 0, j+300, null_string));
746 this->m_custom_class_names[s] = masks[j];
751 // get the collation format used by m_pcollate:
753 m_collate_type = re_detail::find_sort_syntax(this, &m_collate_delim);
756 template <class charT>
757 typename cpp_regex_traits_implementation<charT>::char_class_type
758 cpp_regex_traits_implementation<charT>::lookup_classname_imp(const charT* p1, const charT* p2) const
760 #ifndef BOOST_REGEX_BUGGY_CTYPE_FACET
761 static const char_class_type masks[22] =
764 std::ctype<char>::alnum,
765 std::ctype<char>::alpha,
766 cpp_regex_traits_implementation<charT>::mask_blank,
767 std::ctype<char>::cntrl,
768 std::ctype<char>::digit,
769 std::ctype<char>::digit,
770 std::ctype<char>::graph,
771 cpp_regex_traits_implementation<charT>::mask_horizontal,
772 std::ctype<char>::lower,
773 std::ctype<char>::lower,
774 std::ctype<char>::print,
775 std::ctype<char>::punct,
776 std::ctype<char>::space,
777 std::ctype<char>::space,
778 std::ctype<char>::upper,
779 cpp_regex_traits_implementation<charT>::mask_unicode,
780 std::ctype<char>::upper,
781 cpp_regex_traits_implementation<charT>::mask_vertical,
782 std::ctype<char>::alnum | cpp_regex_traits_implementation<charT>::mask_word,
783 std::ctype<char>::alnum | cpp_regex_traits_implementation<charT>::mask_word,
784 std::ctype<char>::xdigit,
787 static const char_class_type masks[22] =
790 ::boost::re_detail::char_class_alnum,
791 ::boost::re_detail::char_class_alpha,
792 ::boost::re_detail::char_class_blank,
793 ::boost::re_detail::char_class_cntrl,
794 ::boost::re_detail::char_class_digit,
795 ::boost::re_detail::char_class_digit,
796 ::boost::re_detail::char_class_graph,
797 ::boost::re_detail::char_class_horizontal_space,
798 ::boost::re_detail::char_class_lower,
799 ::boost::re_detail::char_class_lower,
800 ::boost::re_detail::char_class_print,
801 ::boost::re_detail::char_class_punct,
802 ::boost::re_detail::char_class_space,
803 ::boost::re_detail::char_class_space,
804 ::boost::re_detail::char_class_upper,
805 ::boost::re_detail::char_class_unicode,
806 ::boost::re_detail::char_class_upper,
807 ::boost::re_detail::char_class_vertical_space,
808 ::boost::re_detail::char_class_alnum | ::boost::re_detail::char_class_word,
809 ::boost::re_detail::char_class_alnum | ::boost::re_detail::char_class_word,
810 ::boost::re_detail::char_class_xdigit,
813 if(m_custom_class_names.size())
815 typedef typename std::map<std::basic_string<charT>, char_class_type>::const_iterator map_iter;
816 map_iter pos = m_custom_class_names.find(string_type(p1, p2));
817 if(pos != m_custom_class_names.end())
820 std::size_t state_id = 1 + re_detail::get_default_class_id(p1, p2);
821 BOOST_ASSERT(state_id < sizeof(masks) / sizeof(masks[0]));
822 return masks[state_id];
825 #ifdef BOOST_REGEX_BUGGY_CTYPE_FACET
826 template <class charT>
827 bool cpp_regex_traits_implementation<charT>::isctype(const charT c, char_class_type mask) const
830 ((mask & ::boost::re_detail::char_class_space) && (m_pctype->is(std::ctype<charT>::space, c)))
831 || ((mask & ::boost::re_detail::char_class_print) && (m_pctype->is(std::ctype<charT>::print, c)))
832 || ((mask & ::boost::re_detail::char_class_cntrl) && (m_pctype->is(std::ctype<charT>::cntrl, c)))
833 || ((mask & ::boost::re_detail::char_class_upper) && (m_pctype->is(std::ctype<charT>::upper, c)))
834 || ((mask & ::boost::re_detail::char_class_lower) && (m_pctype->is(std::ctype<charT>::lower, c)))
835 || ((mask & ::boost::re_detail::char_class_alpha) && (m_pctype->is(std::ctype<charT>::alpha, c)))
836 || ((mask & ::boost::re_detail::char_class_digit) && (m_pctype->is(std::ctype<charT>::digit, c)))
837 || ((mask & ::boost::re_detail::char_class_punct) && (m_pctype->is(std::ctype<charT>::punct, c)))
838 || ((mask & ::boost::re_detail::char_class_xdigit) && (m_pctype->is(std::ctype<charT>::xdigit, c)))
839 || ((mask & ::boost::re_detail::char_class_blank) && (m_pctype->is(std::ctype<charT>::space, c)) && !::boost::re_detail::is_separator(c))
840 || ((mask & ::boost::re_detail::char_class_word) && (c == '_'))
841 || ((mask & ::boost::re_detail::char_class_unicode) && ::boost::re_detail::is_extended(c))
842 || ((mask & ::boost::re_detail::char_class_vertical_space) && (is_separator(c) || (c == '\v')))
843 || ((mask & ::boost::re_detail::char_class_horizontal_space) && m_pctype->is(std::ctype<charT>::space, c) && !(is_separator(c) || (c == '\v')));
848 template <class charT>
849 inline boost::shared_ptr<const cpp_regex_traits_implementation<charT> > create_cpp_regex_traits(const std::locale& l BOOST_APPEND_EXPLICIT_TEMPLATE_TYPE(charT))
851 cpp_regex_traits_base<charT> key(l);
852 return ::boost::object_cache<cpp_regex_traits_base<charT>, cpp_regex_traits_implementation<charT> >::get(key, 5);
857 template <class charT>
858 class cpp_regex_traits
861 typedef std::ctype<charT> ctype_type;
863 typedef charT char_type;
864 typedef std::size_t size_type;
865 typedef std::basic_string<char_type> string_type;
866 typedef std::locale locale_type;
867 typedef boost::uint_least32_t char_class_type;
869 struct boost_extensions_tag{};
872 : m_pimpl(re_detail::create_cpp_regex_traits<charT>(std::locale()))
874 static size_type length(const char_type* p)
876 return std::char_traits<charT>::length(p);
878 regex_constants::syntax_type syntax_type(charT c)const
880 return m_pimpl->syntax_type(c);
882 regex_constants::escape_syntax_type escape_syntax_type(charT c) const
884 return m_pimpl->escape_syntax_type(c);
886 charT translate(charT c) const
890 charT translate_nocase(charT c) const
892 return m_pimpl->m_pctype->tolower(c);
894 charT translate(charT c, bool icase) const
896 return icase ? m_pimpl->m_pctype->tolower(c) : c;
898 charT tolower(charT c) const
900 return m_pimpl->m_pctype->tolower(c);
902 charT toupper(charT c) const
904 return m_pimpl->m_pctype->toupper(c);
906 string_type transform(const charT* p1, const charT* p2) const
908 return m_pimpl->transform(p1, p2);
910 string_type transform_primary(const charT* p1, const charT* p2) const
912 return m_pimpl->transform_primary(p1, p2);
914 char_class_type lookup_classname(const charT* p1, const charT* p2) const
916 return m_pimpl->lookup_classname(p1, p2);
918 string_type lookup_collatename(const charT* p1, const charT* p2) const
920 return m_pimpl->lookup_collatename(p1, p2);
922 bool isctype(charT c, char_class_type f) const
924 #ifndef BOOST_REGEX_BUGGY_CTYPE_FACET
925 typedef typename std::ctype<charT>::mask ctype_mask;
927 static const ctype_mask mask_base =
928 static_cast<ctype_mask>(
929 std::ctype<charT>::alnum
930 | std::ctype<charT>::alpha
931 | std::ctype<charT>::cntrl
932 | std::ctype<charT>::digit
933 | std::ctype<charT>::graph
934 | std::ctype<charT>::lower
935 | std::ctype<charT>::print
936 | std::ctype<charT>::punct
937 | std::ctype<charT>::space
938 | std::ctype<charT>::upper
939 | std::ctype<charT>::xdigit);
942 && (m_pimpl->m_pctype->is(
943 static_cast<ctype_mask>(f & mask_base), c)))
945 else if((f & re_detail::cpp_regex_traits_implementation<charT>::mask_unicode) && re_detail::is_extended(c))
947 else if((f & re_detail::cpp_regex_traits_implementation<charT>::mask_word) && (c == '_'))
949 else if((f & re_detail::cpp_regex_traits_implementation<charT>::mask_blank)
950 && m_pimpl->m_pctype->is(std::ctype<charT>::space, c)
951 && !re_detail::is_separator(c))
953 else if((f & re_detail::cpp_regex_traits_implementation<charT>::mask_vertical)
954 && (::boost::re_detail::is_separator(c) || (c == '\v')))
956 else if((f & re_detail::cpp_regex_traits_implementation<charT>::mask_horizontal)
957 && this->isctype(c, std::ctype<charT>::space) && !this->isctype(c, re_detail::cpp_regex_traits_implementation<charT>::mask_vertical))
961 return m_pimpl->isctype(c, f);
964 int toi(const charT*& p1, const charT* p2, int radix)const;
965 int value(charT c, int radix)const
967 const charT* pc = &c;
968 return toi(pc, pc + 1, radix);
970 locale_type imbue(locale_type l)
972 std::locale result(getloc());
973 m_pimpl = re_detail::create_cpp_regex_traits<charT>(l);
976 locale_type getloc()const
978 return m_pimpl->m_locale;
980 std::string error_string(regex_constants::error_type n) const
982 return m_pimpl->error_string(n);
987 // set the name of the message catalog in use (defaults to "boost_regex").
989 static std::string catalog_name(const std::string& name);
990 static std::string get_catalog_name();
993 boost::shared_ptr<const re_detail::cpp_regex_traits_implementation<charT> > m_pimpl;
995 // catalog name handler:
997 static std::string& get_catalog_name_inst();
999 #ifdef BOOST_HAS_THREADS
1000 static static_mutex& get_mutex_inst();
1005 template <class charT>
1006 int cpp_regex_traits<charT>::toi(const charT*& first, const charT* last, int radix)const
1008 re_detail::parser_buf<charT> sbuf; // buffer for parsing numbers.
1009 std::basic_istream<charT> is(&sbuf); // stream for parsing numbers.
1011 // we do NOT want to parse any thousands separators inside the stream:
1012 last = std::find(first, last, BOOST_USE_FACET(std::numpunct<charT>, is.getloc()).thousands_sep());
1014 sbuf.pubsetbuf(const_cast<charT*>(static_cast<const charT*>(first)), static_cast<std::streamsize>(last-first));
1016 if(std::abs(radix) == 16) is >> std::hex;
1017 else if(std::abs(radix) == 8) is >> std::oct;
1018 else is >> std::dec;
1022 first = first + ((last - first) - sbuf.in_avail());
1029 template <class charT>
1030 std::string cpp_regex_traits<charT>::catalog_name(const std::string& name)
1032 #ifdef BOOST_HAS_THREADS
1033 static_mutex::scoped_lock lk(get_mutex_inst());
1035 std::string result(get_catalog_name_inst());
1036 get_catalog_name_inst() = name;
1040 template <class charT>
1041 std::string& cpp_regex_traits<charT>::get_catalog_name_inst()
1043 static std::string s_name;
1047 template <class charT>
1048 std::string cpp_regex_traits<charT>::get_catalog_name()
1050 #ifdef BOOST_HAS_THREADS
1051 static_mutex::scoped_lock lk(get_mutex_inst());
1053 std::string result(get_catalog_name_inst());
1057 #ifdef BOOST_HAS_THREADS
1058 template <class charT>
1059 static_mutex& cpp_regex_traits<charT>::get_mutex_inst()
1061 static static_mutex s_mutex = BOOST_STATIC_MUTEX_INIT;
1070 #pragma warning(pop)
1074 #pragma warning(push)
1075 #pragma warning(disable: 4103)
1077 #ifdef BOOST_HAS_ABI_HEADERS
1078 # include BOOST_ABI_SUFFIX
1081 #pragma warning(pop)