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/regex/config.hpp>
25 #include <type_traits>
27 #include <boost/regex/pattern_except.hpp>
28 #include <boost/regex/v5/regex_traits_defaults.hpp>
30 #ifdef BOOST_HAS_THREADS
33 #include <boost/regex/v5/primary_transform.hpp>
34 #include <boost/regex/v5/object_cache.hpp>
40 #ifdef BOOST_REGEX_MSVC
42 #pragma warning(disable:4786 4251)
48 // forward declaration is needed by some compilers:
50 template <class charT>
51 class cpp_regex_traits;
53 namespace BOOST_REGEX_DETAIL_NS{
57 // acts as a stream buffer which wraps around a pair of pointers:
59 template <class charT,
60 class traits = ::std::char_traits<charT> >
61 class parser_buf : public ::std::basic_streambuf<charT, traits>
63 typedef ::std::basic_streambuf<charT, traits> base_type;
64 typedef typename base_type::int_type int_type;
65 typedef typename base_type::char_type char_type;
66 typedef typename base_type::pos_type pos_type;
67 typedef ::std::streamsize streamsize;
68 typedef typename base_type::off_type off_type;
70 parser_buf() : base_type() { setbuf(0, 0); }
71 const charT* getnext() { return this->gptr(); }
73 std::basic_streambuf<charT, traits>* setbuf(char_type* s, streamsize n) override;
74 typename parser_buf<charT, traits>::pos_type seekpos(pos_type sp, ::std::ios_base::openmode which) override;
75 typename parser_buf<charT, traits>::pos_type seekoff(off_type off, ::std::ios_base::seekdir way, ::std::ios_base::openmode which) override;
77 parser_buf& operator=(const parser_buf&);
78 parser_buf(const parser_buf&);
81 template<class charT, class traits>
82 std::basic_streambuf<charT, traits>*
83 parser_buf<charT, traits>::setbuf(char_type* s, streamsize n)
85 this->setg(s, s, s + n);
89 template<class charT, class traits>
90 typename parser_buf<charT, traits>::pos_type
91 parser_buf<charT, traits>::seekoff(off_type off, ::std::ios_base::seekdir way, ::std::ios_base::openmode which)
93 if(which & ::std::ios_base::out)
94 return pos_type(off_type(-1));
95 std::ptrdiff_t size = this->egptr() - this->eback();
96 std::ptrdiff_t pos = this->gptr() - this->eback();
97 charT* g = this->eback();
98 switch(static_cast<std::intmax_t>(way))
100 case ::std::ios_base::beg:
101 if((off < 0) || (off > size))
102 return pos_type(off_type(-1));
104 this->setg(g, g + off, g + size);
106 case ::std::ios_base::end:
107 if((off < 0) || (off > size))
108 return pos_type(off_type(-1));
110 this->setg(g, g + size - off, g + size);
112 case ::std::ios_base::cur:
114 std::ptrdiff_t newpos = static_cast<std::ptrdiff_t>(pos + off);
115 if((newpos < 0) || (newpos > size))
116 return pos_type(off_type(-1));
118 this->setg(g, g + newpos, g + size);
123 #ifdef BOOST_REGEX_MSVC
124 #pragma warning(push)
125 #pragma warning(disable:4244)
127 return static_cast<pos_type>(this->gptr() - this->eback());
128 #ifdef BOOST_REGEX_MSVC
133 template<class charT, class traits>
134 typename parser_buf<charT, traits>::pos_type
135 parser_buf<charT, traits>::seekpos(pos_type sp, ::std::ios_base::openmode which)
137 if(which & ::std::ios_base::out)
138 return pos_type(off_type(-1));
139 off_type size = static_cast<off_type>(this->egptr() - this->eback());
140 charT* g = this->eback();
141 if(off_type(sp) <= size)
143 this->setg(g, g + off_type(sp), g + size);
145 return pos_type(off_type(-1));
149 // class cpp_regex_traits_base:
150 // acts as a container for locale and the facets we are using.
152 template <class charT>
153 struct cpp_regex_traits_base
155 cpp_regex_traits_base(const std::locale& l)
157 std::locale imbue(const std::locale& l);
159 std::locale m_locale;
160 std::ctype<charT> const* m_pctype;
161 std::messages<charT> const* m_pmessages;
162 std::collate<charT> const* m_pcollate;
164 bool operator<(const cpp_regex_traits_base& b)const
166 if(m_pctype == b.m_pctype)
168 if(m_pmessages == b.m_pmessages)
170 return m_pcollate < b.m_pcollate;
172 return m_pmessages < b.m_pmessages;
174 return m_pctype < b.m_pctype;
176 bool operator==(const cpp_regex_traits_base& b)const
178 return (m_pctype == b.m_pctype)
179 && (m_pmessages == b.m_pmessages)
180 && (m_pcollate == b.m_pcollate);
184 template <class charT>
185 std::locale cpp_regex_traits_base<charT>::imbue(const std::locale& l)
187 std::locale result(m_locale);
189 m_pctype = &std::use_facet<std::ctype<charT>>(l);
190 m_pmessages = std::has_facet<std::messages<charT> >(l) ? &std::use_facet<std::messages<charT> >(l) : 0;
191 m_pcollate = &std::use_facet<std::collate<charT> >(l);
196 // class cpp_regex_traits_char_layer:
197 // implements methods that require specialization for narrow characters:
199 template <class charT>
200 class cpp_regex_traits_char_layer : public cpp_regex_traits_base<charT>
202 typedef std::basic_string<charT> string_type;
203 typedef std::map<charT, regex_constants::syntax_type> map_type;
204 typedef typename map_type::const_iterator map_iterator_type;
206 cpp_regex_traits_char_layer(const std::locale& l)
207 : cpp_regex_traits_base<charT>(l)
211 cpp_regex_traits_char_layer(const cpp_regex_traits_base<charT>& b)
212 : cpp_regex_traits_base<charT>(b)
218 regex_constants::syntax_type syntax_type(charT c)const
220 map_iterator_type i = m_char_map.find(c);
221 return ((i == m_char_map.end()) ? 0 : i->second);
223 regex_constants::escape_syntax_type escape_syntax_type(charT c) const
225 map_iterator_type i = m_char_map.find(c);
226 if(i == m_char_map.end())
228 if(this->m_pctype->is(std::ctype_base::lower, c)) return regex_constants::escape_type_class;
229 if(this->m_pctype->is(std::ctype_base::upper, c)) return regex_constants::escape_type_not_class;
236 string_type get_default_message(regex_constants::syntax_type);
237 // TODO: use a hash table when available!
241 template <class charT>
242 void cpp_regex_traits_char_layer<charT>::init()
244 // we need to start by initialising our syntax map so we know which
245 // character is used for which purpose:
247 typename std::messages<charT>::catalog cat = static_cast<std::messages<char>::catalog>(-1);
249 typename std::messages<charT>::catalog cat = reinterpret_cast<std::messages<char>::catalog>(-1);
251 std::string cat_name(cpp_regex_traits<charT>::get_catalog_name());
252 if((!cat_name.empty()) && (this->m_pmessages != 0))
254 cat = this->m_pmessages->open(
259 std::string m("Unable to open message catalog: ");
260 std::runtime_error err(m + cat_name);
261 boost::BOOST_REGEX_DETAIL_NS::raise_runtime_error(err);
265 // if we have a valid catalog then load our messages:
269 #ifndef BOOST_NO_EXCEPTIONS
272 for(regex_constants::syntax_type i = 1; i < regex_constants::syntax_max; ++i)
274 string_type mss = this->m_pmessages->get(cat, 0, i, get_default_message(i));
275 for(typename string_type::size_type j = 0; j < mss.size(); ++j)
277 m_char_map[mss[j]] = i;
280 this->m_pmessages->close(cat);
281 #ifndef BOOST_NO_EXCEPTIONS
285 if(this->m_pmessages)
286 this->m_pmessages->close(cat);
293 for(regex_constants::syntax_type i = 1; i < regex_constants::syntax_max; ++i)
295 const char* ptr = get_default_syntax(i);
298 m_char_map[this->m_pctype->widen(*ptr)] = i;
305 template <class charT>
306 typename cpp_regex_traits_char_layer<charT>::string_type
307 cpp_regex_traits_char_layer<charT>::get_default_message(regex_constants::syntax_type i)
309 const char* ptr = get_default_syntax(i);
313 result.append(1, this->m_pctype->widen(*ptr));
320 // specialized version for narrow characters:
323 class cpp_regex_traits_char_layer<char> : public cpp_regex_traits_base<char>
325 typedef std::string string_type;
327 cpp_regex_traits_char_layer(const std::locale& l)
328 : cpp_regex_traits_base<char>(l)
332 cpp_regex_traits_char_layer(const cpp_regex_traits_base<char>& l)
333 : cpp_regex_traits_base<char>(l)
338 regex_constants::syntax_type syntax_type(char c)const
340 return m_char_map[static_cast<unsigned char>(c)];
342 regex_constants::escape_syntax_type escape_syntax_type(char c) const
344 return m_char_map[static_cast<unsigned char>(c)];
348 regex_constants::syntax_type m_char_map[1u << CHAR_BIT];
353 // class cpp_regex_traits_implementation:
354 // provides pimpl implementation for cpp_regex_traits.
356 template <class charT>
357 class cpp_regex_traits_implementation : public cpp_regex_traits_char_layer<charT>
360 typedef typename cpp_regex_traits<charT>::char_class_type char_class_type;
361 typedef typename std::ctype<charT>::mask native_mask_type;
362 typedef typename std::make_unsigned<native_mask_type>::type unsigned_native_mask_type;
363 static const char_class_type mask_blank = 1u << 24;
364 static const char_class_type mask_word = 1u << 25;
365 static const char_class_type mask_unicode = 1u << 26;
366 static const char_class_type mask_horizontal = 1u << 27;
367 static const char_class_type mask_vertical = 1u << 28;
369 typedef std::basic_string<charT> string_type;
370 typedef charT char_type;
371 //cpp_regex_traits_implementation();
372 cpp_regex_traits_implementation(const std::locale& l)
373 : cpp_regex_traits_char_layer<charT>(l)
377 cpp_regex_traits_implementation(const cpp_regex_traits_base<charT>& l)
378 : cpp_regex_traits_char_layer<charT>(l)
382 std::string error_string(regex_constants::error_type n) const
384 if(!m_error_strings.empty())
386 std::map<int, std::string>::const_iterator p = m_error_strings.find(n);
387 return (p == m_error_strings.end()) ? std::string(get_default_error_string(n)) : p->second;
389 return get_default_error_string(n);
391 char_class_type lookup_classname(const charT* p1, const charT* p2) const
393 char_class_type result = lookup_classname_imp(p1, p2);
396 string_type temp(p1, p2);
397 this->m_pctype->tolower(&*temp.begin(), &*temp.begin() + temp.size());
398 result = lookup_classname_imp(&*temp.begin(), &*temp.begin() + temp.size());
402 string_type lookup_collatename(const charT* p1, const charT* p2) const;
403 string_type transform_primary(const charT* p1, const charT* p2) const;
404 string_type transform(const charT* p1, const charT* p2) const;
406 std::map<int, std::string> m_error_strings; // error messages indexed by numberic ID
407 std::map<string_type, char_class_type> m_custom_class_names; // character class names
408 std::map<string_type, string_type> m_custom_collate_names; // collating element names
409 unsigned m_collate_type; // the form of the collation string
410 charT m_collate_delim; // the collation group delimiter
414 char_class_type lookup_classname_imp(const charT* p1, const charT* p2) const;
418 template <class charT>
419 typename cpp_regex_traits_implementation<charT>::char_class_type const cpp_regex_traits_implementation<charT>::mask_blank;
420 template <class charT>
421 typename cpp_regex_traits_implementation<charT>::char_class_type const cpp_regex_traits_implementation<charT>::mask_word;
422 template <class charT>
423 typename cpp_regex_traits_implementation<charT>::char_class_type const cpp_regex_traits_implementation<charT>::mask_unicode;
424 template <class charT>
425 typename cpp_regex_traits_implementation<charT>::char_class_type const cpp_regex_traits_implementation<charT>::mask_vertical;
426 template <class charT>
427 typename cpp_regex_traits_implementation<charT>::char_class_type const cpp_regex_traits_implementation<charT>::mask_horizontal;
429 template <class charT>
430 typename cpp_regex_traits_implementation<charT>::string_type
431 cpp_regex_traits_implementation<charT>::transform_primary(const charT* p1, const charT* p2) const
436 // A bug in gcc 3.2 (and maybe other versions as well) treats
437 // p1 as a null terminated string, for efficiency reasons
438 // we work around this elsewhere, but just assert here that
439 // we adhere to gcc's (buggy) preconditions...
441 BOOST_REGEX_ASSERT(*p2 == 0);
443 #if defined(_CPPLIB_VER)
445 // A bug in VC11 and 12 causes the program to hang if we pass a null-string
446 // to std::collate::transform, but only for certain locales :-(
447 // Probably effects Intel and Clang or any compiler using the VC std library (Dinkumware).
451 return string_type(1, charT(0));
455 // swallowing all exceptions here is a bad idea
456 // however at least one std lib will always throw
457 // std::bad_alloc for certain arguments...
459 #ifndef BOOST_NO_EXCEPTIONS
463 // What we do here depends upon the format of the sort key returned by
464 // sort key returned by this->transform:
466 switch(m_collate_type)
470 // the best we can do is translate to lower case, then get a regular sort key:
472 result.assign(p1, p2);
473 this->m_pctype->tolower(&*result.begin(), &*result.begin() + result.size());
474 result = this->m_pcollate->transform(&*result.begin(), &*result.begin() + result.size());
479 // get a regular sort key, and then truncate it:
480 result.assign(this->m_pcollate->transform(p1, p2));
481 result.erase(this->m_collate_delim);
485 // get a regular sort key, and then truncate everything after the delim:
486 result.assign(this->m_pcollate->transform(p1, p2));
488 for(i = 0; i < result.size(); ++i)
490 if(result[i] == m_collate_delim)
496 #ifndef BOOST_NO_EXCEPTIONS
499 while((!result.empty()) && (charT(0) == *result.rbegin()))
500 result.erase(result.size() - 1);
503 // character is ignorable at the primary level:
504 result = string_type(1, charT(0));
509 template <class charT>
510 typename cpp_regex_traits_implementation<charT>::string_type
511 cpp_regex_traits_implementation<charT>::transform(const charT* p1, const charT* p2) const
516 // A bug in gcc 3.2 (and maybe other versions as well) treats
517 // p1 as a null terminated string, for efficiency reasons
518 // we work around this elsewhere, but just assert here that
519 // we adhere to gcc's (buggy) preconditions...
521 BOOST_REGEX_ASSERT(*p2 == 0);
523 // swallowing all exceptions here is a bad idea
524 // however at least one std lib will always throw
525 // std::bad_alloc for certain arguments...
527 string_type result, result2;
528 #if defined(_CPPLIB_VER)
530 // A bug in VC11 and 12 causes the program to hang if we pass a null-string
531 // to std::collate::transform, but only for certain locales :-(
532 // Probably effects Intel and Clang or any compiler using the VC std library (Dinkumware).
539 #ifndef BOOST_NO_EXCEPTIONS
542 result = this->m_pcollate->transform(p1, p2);
544 // some implementations (Dinkumware) append unnecessary trailing \0's:
545 while((!result.empty()) && (charT(0) == *result.rbegin()))
546 result.erase(result.size() - 1);
548 // We may have NULL's used as separators between sections of the collate string,
549 // an example would be Boost.Locale. We have no way to detect this case via
550 // #defines since this can be used with any compiler/platform combination.
551 // Unfortunately our state machine (which was devised when all implementations
552 // used underlying C language API's) can't cope with that case. One workaround
553 // is to replace each character with 2, fortunately this code isn't used that
554 // much as this is now slower than before :-(
556 typedef typename std::make_unsigned<charT>::type uchar_type;
557 result2.reserve(result.size() * 2 + 2);
558 for(unsigned i = 0; i < result.size(); ++i)
560 if(static_cast<uchar_type>(result[i]) == (std::numeric_limits<uchar_type>::max)())
562 result2.append(1, charT((std::numeric_limits<uchar_type>::max)())).append(1, charT('b'));
566 result2.append(1, static_cast<charT>(1 + static_cast<uchar_type>(result[i]))).append(1, charT('b') - 1);
569 BOOST_REGEX_ASSERT(std::find(result2.begin(), result2.end(), charT(0)) == result2.end());
570 #ifndef BOOST_NO_EXCEPTIONS
580 template <class charT>
581 typename cpp_regex_traits_implementation<charT>::string_type
582 cpp_regex_traits_implementation<charT>::lookup_collatename(const charT* p1, const charT* p2) const
584 typedef typename std::map<string_type, string_type>::const_iterator iter_type;
585 if(!m_custom_collate_names.empty())
587 iter_type pos = m_custom_collate_names.find(string_type(p1, p2));
588 if(pos != m_custom_collate_names.end())
591 std::string name(p1, p2);
592 name = lookup_default_collate_name(name);
594 return string_type(name.begin(), name.end());
596 return string_type(1, *p1);
597 return string_type();
600 template <class charT>
601 void cpp_regex_traits_implementation<charT>::init()
604 typename std::messages<charT>::catalog cat = static_cast<std::messages<char>::catalog>(-1);
606 typename std::messages<charT>::catalog cat = reinterpret_cast<std::messages<char>::catalog>(-1);
608 std::string cat_name(cpp_regex_traits<charT>::get_catalog_name());
609 if((!cat_name.empty()) && (this->m_pmessages != 0))
611 cat = this->m_pmessages->open(
616 std::string m("Unable to open message catalog: ");
617 std::runtime_error err(m + cat_name);
618 boost::BOOST_REGEX_DETAIL_NS::raise_runtime_error(err);
622 // if we have a valid catalog then load our messages:
629 for(boost::regex_constants::error_type i = static_cast<boost::regex_constants::error_type>(0);
630 i <= boost::regex_constants::error_unknown;
631 i = static_cast<boost::regex_constants::error_type>(i + 1))
633 const char* p = get_default_error_string(i);
634 string_type default_message;
637 default_message.append(1, this->m_pctype->widen(*p));
640 string_type s = this->m_pmessages->get(cat, 0, i+200, default_message);
642 for(std::string::size_type j = 0; j < s.size(); ++j)
644 result.append(1, this->m_pctype->narrow(s[j], 0));
646 m_error_strings[i] = result;
649 // Custom class names:
651 static const char_class_type masks[16] =
653 static_cast<unsigned_native_mask_type>(std::ctype<charT>::alnum),
654 static_cast<unsigned_native_mask_type>(std::ctype<charT>::alpha),
655 static_cast<unsigned_native_mask_type>(std::ctype<charT>::cntrl),
656 static_cast<unsigned_native_mask_type>(std::ctype<charT>::digit),
657 static_cast<unsigned_native_mask_type>(std::ctype<charT>::graph),
658 cpp_regex_traits_implementation<charT>::mask_horizontal,
659 static_cast<unsigned_native_mask_type>(std::ctype<charT>::lower),
660 static_cast<unsigned_native_mask_type>(std::ctype<charT>::print),
661 static_cast<unsigned_native_mask_type>(std::ctype<charT>::punct),
662 static_cast<unsigned_native_mask_type>(std::ctype<charT>::space),
663 static_cast<unsigned_native_mask_type>(std::ctype<charT>::upper),
664 cpp_regex_traits_implementation<charT>::mask_vertical,
665 static_cast<unsigned_native_mask_type>(std::ctype<charT>::xdigit),
666 cpp_regex_traits_implementation<charT>::mask_blank,
667 cpp_regex_traits_implementation<charT>::mask_word,
668 cpp_regex_traits_implementation<charT>::mask_unicode,
670 static const string_type null_string;
671 for(unsigned int j = 0; j <= 13; ++j)
673 string_type s(this->m_pmessages->get(cat, 0, j+300, null_string));
675 this->m_custom_class_names[s] = masks[j];
679 // get the collation format used by m_pcollate:
681 m_collate_type = BOOST_REGEX_DETAIL_NS::find_sort_syntax(this, &m_collate_delim);
684 template <class charT>
685 typename cpp_regex_traits_implementation<charT>::char_class_type
686 cpp_regex_traits_implementation<charT>::lookup_classname_imp(const charT* p1, const charT* p2) const
688 static const char_class_type masks[22] =
691 static_cast<unsigned_native_mask_type>(std::ctype<char>::alnum),
692 static_cast<unsigned_native_mask_type>(std::ctype<char>::alpha),
693 cpp_regex_traits_implementation<charT>::mask_blank,
694 static_cast<unsigned_native_mask_type>(std::ctype<char>::cntrl),
695 static_cast<unsigned_native_mask_type>(std::ctype<char>::digit),
696 static_cast<unsigned_native_mask_type>(std::ctype<char>::digit),
697 static_cast<unsigned_native_mask_type>(std::ctype<char>::graph),
698 cpp_regex_traits_implementation<charT>::mask_horizontal,
699 static_cast<unsigned_native_mask_type>(std::ctype<char>::lower),
700 static_cast<unsigned_native_mask_type>(std::ctype<char>::lower),
701 static_cast<unsigned_native_mask_type>(std::ctype<char>::print),
702 static_cast<unsigned_native_mask_type>(std::ctype<char>::punct),
703 static_cast<unsigned_native_mask_type>(std::ctype<char>::space),
704 static_cast<unsigned_native_mask_type>(std::ctype<char>::space),
705 static_cast<unsigned_native_mask_type>(std::ctype<char>::upper),
706 cpp_regex_traits_implementation<charT>::mask_unicode,
707 static_cast<unsigned_native_mask_type>(std::ctype<char>::upper),
708 cpp_regex_traits_implementation<charT>::mask_vertical,
709 static_cast<unsigned_native_mask_type>(std::ctype<char>::alnum) | cpp_regex_traits_implementation<charT>::mask_word,
710 static_cast<unsigned_native_mask_type>(std::ctype<char>::alnum) | cpp_regex_traits_implementation<charT>::mask_word,
711 static_cast<unsigned_native_mask_type>(std::ctype<char>::xdigit),
713 if(!m_custom_class_names.empty())
715 typedef typename std::map<std::basic_string<charT>, char_class_type>::const_iterator map_iter;
716 map_iter pos = m_custom_class_names.find(string_type(p1, p2));
717 if(pos != m_custom_class_names.end())
720 std::size_t state_id = 1 + BOOST_REGEX_DETAIL_NS::get_default_class_id(p1, p2);
721 BOOST_REGEX_ASSERT(state_id < sizeof(masks) / sizeof(masks[0]));
722 return masks[state_id];
725 template <class charT>
726 inline std::shared_ptr<const cpp_regex_traits_implementation<charT> > create_cpp_regex_traits(const std::locale& l)
728 cpp_regex_traits_base<charT> key(l);
729 return ::boost::object_cache<cpp_regex_traits_base<charT>, cpp_regex_traits_implementation<charT> >::get(key, 5);
732 } // BOOST_REGEX_DETAIL_NS
734 template <class charT>
735 class cpp_regex_traits
738 typedef std::ctype<charT> ctype_type;
740 typedef charT char_type;
741 typedef std::size_t size_type;
742 typedef std::basic_string<char_type> string_type;
743 typedef std::locale locale_type;
744 typedef std::uint_least32_t char_class_type;
746 struct boost_extensions_tag{};
749 : m_pimpl(BOOST_REGEX_DETAIL_NS::create_cpp_regex_traits<charT>(std::locale()))
751 static size_type length(const char_type* p)
753 return std::char_traits<charT>::length(p);
755 regex_constants::syntax_type syntax_type(charT c)const
757 return m_pimpl->syntax_type(c);
759 regex_constants::escape_syntax_type escape_syntax_type(charT c) const
761 return m_pimpl->escape_syntax_type(c);
763 charT translate(charT c) const
767 charT translate_nocase(charT c) const
769 return m_pimpl->m_pctype->tolower(c);
771 charT translate(charT c, bool icase) const
773 return icase ? m_pimpl->m_pctype->tolower(c) : c;
775 charT tolower(charT c) const
777 return m_pimpl->m_pctype->tolower(c);
779 charT toupper(charT c) const
781 return m_pimpl->m_pctype->toupper(c);
783 string_type transform(const charT* p1, const charT* p2) const
785 return m_pimpl->transform(p1, p2);
787 string_type transform_primary(const charT* p1, const charT* p2) const
789 return m_pimpl->transform_primary(p1, p2);
791 char_class_type lookup_classname(const charT* p1, const charT* p2) const
793 return m_pimpl->lookup_classname(p1, p2);
795 string_type lookup_collatename(const charT* p1, const charT* p2) const
797 return m_pimpl->lookup_collatename(p1, p2);
799 bool isctype(charT c, char_class_type f) const
801 typedef typename std::ctype<charT>::mask ctype_mask;
803 static const ctype_mask mask_base =
804 static_cast<ctype_mask>(
805 std::ctype<charT>::alnum
806 | std::ctype<charT>::alpha
807 | std::ctype<charT>::cntrl
808 | std::ctype<charT>::digit
809 | std::ctype<charT>::graph
810 | std::ctype<charT>::lower
811 | std::ctype<charT>::print
812 | std::ctype<charT>::punct
813 | std::ctype<charT>::space
814 | std::ctype<charT>::upper
815 | std::ctype<charT>::xdigit);
818 && (m_pimpl->m_pctype->is(
819 static_cast<ctype_mask>(f & mask_base), c)))
821 else if((f & BOOST_REGEX_DETAIL_NS::cpp_regex_traits_implementation<charT>::mask_unicode) && BOOST_REGEX_DETAIL_NS::is_extended(c))
823 else if((f & BOOST_REGEX_DETAIL_NS::cpp_regex_traits_implementation<charT>::mask_word) && (c == '_'))
825 else if((f & BOOST_REGEX_DETAIL_NS::cpp_regex_traits_implementation<charT>::mask_blank)
826 && m_pimpl->m_pctype->is(std::ctype<charT>::space, c)
827 && !BOOST_REGEX_DETAIL_NS::is_separator(c))
829 else if((f & BOOST_REGEX_DETAIL_NS::cpp_regex_traits_implementation<charT>::mask_vertical)
830 && (::boost::BOOST_REGEX_DETAIL_NS::is_separator(c) || (c == '\v')))
832 else if((f & BOOST_REGEX_DETAIL_NS::cpp_regex_traits_implementation<charT>::mask_horizontal)
833 && this->isctype(c, std::ctype<charT>::space) && !this->isctype(c, BOOST_REGEX_DETAIL_NS::cpp_regex_traits_implementation<charT>::mask_vertical))
837 // Cygwin has a buggy ctype facet, see https://www.cygwin.com/ml/cygwin/2012-08/msg00178.html:
839 else if((f & std::ctype<charT>::xdigit) == std::ctype<charT>::xdigit)
841 if((c >= 'a') && (c <= 'f'))
843 if((c >= 'A') && (c <= 'F'))
849 std::intmax_t toi(const charT*& p1, const charT* p2, int radix)const;
850 int value(charT c, int radix)const
852 const charT* pc = &c;
853 return (int)toi(pc, pc + 1, radix);
855 locale_type imbue(locale_type l)
857 std::locale result(getloc());
858 m_pimpl = BOOST_REGEX_DETAIL_NS::create_cpp_regex_traits<charT>(l);
861 locale_type getloc()const
863 return m_pimpl->m_locale;
865 std::string error_string(regex_constants::error_type n) const
867 return m_pimpl->error_string(n);
872 // set the name of the message catalog in use (defaults to "boost_regex").
874 static std::string catalog_name(const std::string& name);
875 static std::string get_catalog_name();
878 std::shared_ptr<const BOOST_REGEX_DETAIL_NS::cpp_regex_traits_implementation<charT> > m_pimpl;
880 // catalog name handler:
882 static std::string& get_catalog_name_inst();
884 #ifdef BOOST_HAS_THREADS
885 static std::mutex& get_mutex_inst();
890 template <class charT>
891 std::intmax_t cpp_regex_traits<charT>::toi(const charT*& first, const charT* last, int radix)const
893 BOOST_REGEX_DETAIL_NS::parser_buf<charT> sbuf; // buffer for parsing numbers.
894 std::basic_istream<charT> is(&sbuf); // stream for parsing numbers.
896 // we do NOT want to parse any thousands separators inside the stream:
897 last = std::find(first, last, std::use_facet<std::numpunct<charT>>(is.getloc()).thousands_sep());
899 sbuf.pubsetbuf(const_cast<charT*>(static_cast<const charT*>(first)), static_cast<std::streamsize>(last-first));
901 if(std::abs(radix) == 16) is >> std::hex;
902 else if(std::abs(radix) == 8) is >> std::oct;
907 first = first + ((last - first) - sbuf.in_avail());
914 template <class charT>
915 std::string cpp_regex_traits<charT>::catalog_name(const std::string& name)
917 #ifdef BOOST_HAS_THREADS
918 std::lock_guard<std::mutex> lk(get_mutex_inst());
920 std::string result(get_catalog_name_inst());
921 get_catalog_name_inst() = name;
925 template <class charT>
926 std::string& cpp_regex_traits<charT>::get_catalog_name_inst()
928 static std::string s_name;
932 template <class charT>
933 std::string cpp_regex_traits<charT>::get_catalog_name()
935 #ifdef BOOST_HAS_THREADS
936 std::lock_guard<std::mutex> lk(get_mutex_inst());
938 std::string result(get_catalog_name_inst());
942 #ifdef BOOST_HAS_THREADS
943 template <class charT>
944 std::mutex& cpp_regex_traits<charT>::get_mutex_inst()
946 static std::mutex s_mutex;
951 namespace BOOST_REGEX_DETAIL_NS {
953 inline void cpp_regex_traits_char_layer<char>::init()
955 // we need to start by initialising our syntax map so we know which
956 // character is used for which purpose:
957 std::memset(m_char_map, 0, sizeof(m_char_map));
959 std::messages<char>::catalog cat = static_cast<std::messages<char>::catalog>(-1);
961 std::messages<char>::catalog cat = reinterpret_cast<std::messages<char>::catalog>(-1);
963 std::string cat_name(cpp_regex_traits<char>::get_catalog_name());
964 if ((!cat_name.empty()) && (m_pmessages != 0))
966 cat = this->m_pmessages->open(
971 std::string m("Unable to open message catalog: ");
972 std::runtime_error err(m + cat_name);
973 boost::BOOST_REGEX_DETAIL_NS::raise_runtime_error(err);
977 // if we have a valid catalog then load our messages:
981 #ifndef BOOST_NO_EXCEPTIONS
984 for (regex_constants::syntax_type i = 1; i < regex_constants::syntax_max; ++i)
986 string_type mss = this->m_pmessages->get(cat, 0, i, get_default_syntax(i));
987 for (string_type::size_type j = 0; j < mss.size(); ++j)
989 m_char_map[static_cast<unsigned char>(mss[j])] = i;
992 this->m_pmessages->close(cat);
993 #ifndef BOOST_NO_EXCEPTIONS
997 this->m_pmessages->close(cat);
1004 for (regex_constants::syntax_type j = 1; j < regex_constants::syntax_max; ++j)
1006 const char* ptr = get_default_syntax(j);
1009 m_char_map[static_cast<unsigned char>(*ptr)] = j;
1015 // finish off by calculating our escape types:
1017 unsigned char i = 'A';
1020 if (m_char_map[i] == 0)
1022 if (this->m_pctype->is(std::ctype_base::lower, i))
1023 m_char_map[i] = regex_constants::escape_type_class;
1024 else if (this->m_pctype->is(std::ctype_base::upper, i))
1025 m_char_map[i] = regex_constants::escape_type_not_class;
1027 } while (0xFF != i++);
1030 } // namespace detail
1035 #ifdef BOOST_REGEX_MSVC
1036 #pragma warning(pop)