]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/regex/v5/cpp_regex_traits.hpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / boost / regex / v5 / cpp_regex_traits.hpp
1 /*
2 *
3 * Copyright (c) 2004 John Maddock
4 * Copyright 2011 Garmin Ltd. or its subsidiaries
5 *
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)
9 *
10 */
11
12 /*
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.
17 */
18
19 #ifndef BOOST_CPP_REGEX_TRAITS_HPP_INCLUDED
20 #define BOOST_CPP_REGEX_TRAITS_HPP_INCLUDED
21
22 #include <boost/regex/config.hpp>
23 #include <cstdint>
24 #include <locale>
25 #include <type_traits>
26
27 #include <boost/regex/pattern_except.hpp>
28 #include <boost/regex/v5/regex_traits_defaults.hpp>
29
30 #ifdef BOOST_HAS_THREADS
31 #include <mutex>
32 #endif
33 #include <boost/regex/v5/primary_transform.hpp>
34 #include <boost/regex/v5/object_cache.hpp>
35
36 #include <climits>
37 #include <ios>
38 #include <istream>
39
40 #ifdef BOOST_REGEX_MSVC
41 #pragma warning(push)
42 #pragma warning(disable:4786 4251)
43 #endif
44
45 namespace boost{
46
47 //
48 // forward declaration is needed by some compilers:
49 //
50 template <class charT>
51 class cpp_regex_traits;
52
53 namespace BOOST_REGEX_DETAIL_NS{
54
55 //
56 // class parser_buf:
57 // acts as a stream buffer which wraps around a pair of pointers:
58 //
59 template <class charT,
60 class traits = ::std::char_traits<charT> >
61 class parser_buf : public ::std::basic_streambuf<charT, traits>
62 {
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;
69 public:
70 parser_buf() : base_type() { setbuf(0, 0); }
71 const charT* getnext() { return this->gptr(); }
72 protected:
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;
76 private:
77 parser_buf& operator=(const parser_buf&);
78 parser_buf(const parser_buf&);
79 };
80
81 template<class charT, class traits>
82 std::basic_streambuf<charT, traits>*
83 parser_buf<charT, traits>::setbuf(char_type* s, streamsize n)
84 {
85 this->setg(s, s, s + n);
86 return this;
87 }
88
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)
92 {
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))
99 {
100 case ::std::ios_base::beg:
101 if((off < 0) || (off > size))
102 return pos_type(off_type(-1));
103 else
104 this->setg(g, g + off, g + size);
105 break;
106 case ::std::ios_base::end:
107 if((off < 0) || (off > size))
108 return pos_type(off_type(-1));
109 else
110 this->setg(g, g + size - off, g + size);
111 break;
112 case ::std::ios_base::cur:
113 {
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));
117 else
118 this->setg(g, g + newpos, g + size);
119 break;
120 }
121 default: ;
122 }
123 #ifdef BOOST_REGEX_MSVC
124 #pragma warning(push)
125 #pragma warning(disable:4244)
126 #endif
127 return static_cast<pos_type>(this->gptr() - this->eback());
128 #ifdef BOOST_REGEX_MSVC
129 #pragma warning(pop)
130 #endif
131 }
132
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)
136 {
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)
142 {
143 this->setg(g, g + off_type(sp), g + size);
144 }
145 return pos_type(off_type(-1));
146 }
147
148 //
149 // class cpp_regex_traits_base:
150 // acts as a container for locale and the facets we are using.
151 //
152 template <class charT>
153 struct cpp_regex_traits_base
154 {
155 cpp_regex_traits_base(const std::locale& l)
156 { (void)imbue(l); }
157 std::locale imbue(const std::locale& l);
158
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;
163
164 bool operator<(const cpp_regex_traits_base& b)const
165 {
166 if(m_pctype == b.m_pctype)
167 {
168 if(m_pmessages == b.m_pmessages)
169 {
170 return m_pcollate < b.m_pcollate;
171 }
172 return m_pmessages < b.m_pmessages;
173 }
174 return m_pctype < b.m_pctype;
175 }
176 bool operator==(const cpp_regex_traits_base& b)const
177 {
178 return (m_pctype == b.m_pctype)
179 && (m_pmessages == b.m_pmessages)
180 && (m_pcollate == b.m_pcollate);
181 }
182 };
183
184 template <class charT>
185 std::locale cpp_regex_traits_base<charT>::imbue(const std::locale& l)
186 {
187 std::locale result(m_locale);
188 m_locale = l;
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);
192 return result;
193 }
194
195 //
196 // class cpp_regex_traits_char_layer:
197 // implements methods that require specialization for narrow characters:
198 //
199 template <class charT>
200 class cpp_regex_traits_char_layer : public cpp_regex_traits_base<charT>
201 {
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;
205 public:
206 cpp_regex_traits_char_layer(const std::locale& l)
207 : cpp_regex_traits_base<charT>(l)
208 {
209 init();
210 }
211 cpp_regex_traits_char_layer(const cpp_regex_traits_base<charT>& b)
212 : cpp_regex_traits_base<charT>(b)
213 {
214 init();
215 }
216 void init();
217
218 regex_constants::syntax_type syntax_type(charT c)const
219 {
220 map_iterator_type i = m_char_map.find(c);
221 return ((i == m_char_map.end()) ? 0 : i->second);
222 }
223 regex_constants::escape_syntax_type escape_syntax_type(charT c) const
224 {
225 map_iterator_type i = m_char_map.find(c);
226 if(i == m_char_map.end())
227 {
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;
230 return 0;
231 }
232 return i->second;
233 }
234
235 private:
236 string_type get_default_message(regex_constants::syntax_type);
237 // TODO: use a hash table when available!
238 map_type m_char_map;
239 };
240
241 template <class charT>
242 void cpp_regex_traits_char_layer<charT>::init()
243 {
244 // we need to start by initialising our syntax map so we know which
245 // character is used for which purpose:
246 #ifndef __IBMCPP__
247 typename std::messages<charT>::catalog cat = static_cast<std::messages<char>::catalog>(-1);
248 #else
249 typename std::messages<charT>::catalog cat = reinterpret_cast<std::messages<char>::catalog>(-1);
250 #endif
251 std::string cat_name(cpp_regex_traits<charT>::get_catalog_name());
252 if((!cat_name.empty()) && (this->m_pmessages != 0))
253 {
254 cat = this->m_pmessages->open(
255 cat_name,
256 this->m_locale);
257 if((int)cat < 0)
258 {
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);
262 }
263 }
264 //
265 // if we have a valid catalog then load our messages:
266 //
267 if((int)cat >= 0)
268 {
269 #ifndef BOOST_NO_EXCEPTIONS
270 try{
271 #endif
272 for(regex_constants::syntax_type i = 1; i < regex_constants::syntax_max; ++i)
273 {
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)
276 {
277 m_char_map[mss[j]] = i;
278 }
279 }
280 this->m_pmessages->close(cat);
281 #ifndef BOOST_NO_EXCEPTIONS
282 }
283 catch(...)
284 {
285 if(this->m_pmessages)
286 this->m_pmessages->close(cat);
287 throw;
288 }
289 #endif
290 }
291 else
292 {
293 for(regex_constants::syntax_type i = 1; i < regex_constants::syntax_max; ++i)
294 {
295 const char* ptr = get_default_syntax(i);
296 while(ptr && *ptr)
297 {
298 m_char_map[this->m_pctype->widen(*ptr)] = i;
299 ++ptr;
300 }
301 }
302 }
303 }
304
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)
308 {
309 const char* ptr = get_default_syntax(i);
310 string_type result;
311 while(ptr && *ptr)
312 {
313 result.append(1, this->m_pctype->widen(*ptr));
314 ++ptr;
315 }
316 return result;
317 }
318
319 //
320 // specialized version for narrow characters:
321 //
322 template <>
323 class cpp_regex_traits_char_layer<char> : public cpp_regex_traits_base<char>
324 {
325 typedef std::string string_type;
326 public:
327 cpp_regex_traits_char_layer(const std::locale& l)
328 : cpp_regex_traits_base<char>(l)
329 {
330 init();
331 }
332 cpp_regex_traits_char_layer(const cpp_regex_traits_base<char>& l)
333 : cpp_regex_traits_base<char>(l)
334 {
335 init();
336 }
337
338 regex_constants::syntax_type syntax_type(char c)const
339 {
340 return m_char_map[static_cast<unsigned char>(c)];
341 }
342 regex_constants::escape_syntax_type escape_syntax_type(char c) const
343 {
344 return m_char_map[static_cast<unsigned char>(c)];
345 }
346
347 private:
348 regex_constants::syntax_type m_char_map[1u << CHAR_BIT];
349 void init();
350 };
351
352 //
353 // class cpp_regex_traits_implementation:
354 // provides pimpl implementation for cpp_regex_traits.
355 //
356 template <class charT>
357 class cpp_regex_traits_implementation : public cpp_regex_traits_char_layer<charT>
358 {
359 public:
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;
368
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)
374 {
375 init();
376 }
377 cpp_regex_traits_implementation(const cpp_regex_traits_base<charT>& l)
378 : cpp_regex_traits_char_layer<charT>(l)
379 {
380 init();
381 }
382 std::string error_string(regex_constants::error_type n) const
383 {
384 if(!m_error_strings.empty())
385 {
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;
388 }
389 return get_default_error_string(n);
390 }
391 char_class_type lookup_classname(const charT* p1, const charT* p2) const
392 {
393 char_class_type result = lookup_classname_imp(p1, p2);
394 if(result == 0)
395 {
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());
399 }
400 return result;
401 }
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;
405 private:
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
411 //
412 // helpers:
413 //
414 char_class_type lookup_classname_imp(const charT* p1, const charT* p2) const;
415 void init();
416 };
417
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;
428
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
432 {
433 //
434 // PRECONDITIONS:
435 //
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...
440 //
441 BOOST_REGEX_ASSERT(*p2 == 0);
442 string_type result;
443 #if defined(_CPPLIB_VER)
444 //
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).
448 //
449 if(*p1 == 0)
450 {
451 return string_type(1, charT(0));
452 }
453 #endif
454 //
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...
458 //
459 #ifndef BOOST_NO_EXCEPTIONS
460 try{
461 #endif
462 //
463 // What we do here depends upon the format of the sort key returned by
464 // sort key returned by this->transform:
465 //
466 switch(m_collate_type)
467 {
468 case sort_C:
469 case sort_unknown:
470 // the best we can do is translate to lower case, then get a regular sort key:
471 {
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());
475 break;
476 }
477 case sort_fixed:
478 {
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);
482 break;
483 }
484 case sort_delim:
485 // get a regular sort key, and then truncate everything after the delim:
486 result.assign(this->m_pcollate->transform(p1, p2));
487 std::size_t i;
488 for(i = 0; i < result.size(); ++i)
489 {
490 if(result[i] == m_collate_delim)
491 break;
492 }
493 result.erase(i);
494 break;
495 }
496 #ifndef BOOST_NO_EXCEPTIONS
497 }catch(...){}
498 #endif
499 while((!result.empty()) && (charT(0) == *result.rbegin()))
500 result.erase(result.size() - 1);
501 if(result.empty())
502 {
503 // character is ignorable at the primary level:
504 result = string_type(1, charT(0));
505 }
506 return result;
507 }
508
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
512 {
513 //
514 // PRECONDITIONS:
515 //
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...
520 //
521 BOOST_REGEX_ASSERT(*p2 == 0);
522 //
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...
526 //
527 string_type result, result2;
528 #if defined(_CPPLIB_VER)
529 //
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).
533 //
534 if(*p1 == 0)
535 {
536 return result;
537 }
538 #endif
539 #ifndef BOOST_NO_EXCEPTIONS
540 try{
541 #endif
542 result = this->m_pcollate->transform(p1, p2);
543 //
544 // some implementations (Dinkumware) append unnecessary trailing \0's:
545 while((!result.empty()) && (charT(0) == *result.rbegin()))
546 result.erase(result.size() - 1);
547 //
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 :-(
555 //
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)
559 {
560 if(static_cast<uchar_type>(result[i]) == (std::numeric_limits<uchar_type>::max)())
561 {
562 result2.append(1, charT((std::numeric_limits<uchar_type>::max)())).append(1, charT('b'));
563 }
564 else
565 {
566 result2.append(1, static_cast<charT>(1 + static_cast<uchar_type>(result[i]))).append(1, charT('b') - 1);
567 }
568 }
569 BOOST_REGEX_ASSERT(std::find(result2.begin(), result2.end(), charT(0)) == result2.end());
570 #ifndef BOOST_NO_EXCEPTIONS
571 }
572 catch(...)
573 {
574 }
575 #endif
576 return result2;
577 }
578
579
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
583 {
584 typedef typename std::map<string_type, string_type>::const_iterator iter_type;
585 if(!m_custom_collate_names.empty())
586 {
587 iter_type pos = m_custom_collate_names.find(string_type(p1, p2));
588 if(pos != m_custom_collate_names.end())
589 return pos->second;
590 }
591 std::string name(p1, p2);
592 name = lookup_default_collate_name(name);
593 if(!name.empty())
594 return string_type(name.begin(), name.end());
595 if(p2 - p1 == 1)
596 return string_type(1, *p1);
597 return string_type();
598 }
599
600 template <class charT>
601 void cpp_regex_traits_implementation<charT>::init()
602 {
603 #ifndef __IBMCPP__
604 typename std::messages<charT>::catalog cat = static_cast<std::messages<char>::catalog>(-1);
605 #else
606 typename std::messages<charT>::catalog cat = reinterpret_cast<std::messages<char>::catalog>(-1);
607 #endif
608 std::string cat_name(cpp_regex_traits<charT>::get_catalog_name());
609 if((!cat_name.empty()) && (this->m_pmessages != 0))
610 {
611 cat = this->m_pmessages->open(
612 cat_name,
613 this->m_locale);
614 if((int)cat < 0)
615 {
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);
619 }
620 }
621 //
622 // if we have a valid catalog then load our messages:
623 //
624 if((int)cat >= 0)
625 {
626 //
627 // Error messages:
628 //
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))
632 {
633 const char* p = get_default_error_string(i);
634 string_type default_message;
635 while(*p)
636 {
637 default_message.append(1, this->m_pctype->widen(*p));
638 ++p;
639 }
640 string_type s = this->m_pmessages->get(cat, 0, i+200, default_message);
641 std::string result;
642 for(std::string::size_type j = 0; j < s.size(); ++j)
643 {
644 result.append(1, this->m_pctype->narrow(s[j], 0));
645 }
646 m_error_strings[i] = result;
647 }
648 //
649 // Custom class names:
650 //
651 static const char_class_type masks[16] =
652 {
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,
669 };
670 static const string_type null_string;
671 for(unsigned int j = 0; j <= 13; ++j)
672 {
673 string_type s(this->m_pmessages->get(cat, 0, j+300, null_string));
674 if(!s.empty())
675 this->m_custom_class_names[s] = masks[j];
676 }
677 }
678 //
679 // get the collation format used by m_pcollate:
680 //
681 m_collate_type = BOOST_REGEX_DETAIL_NS::find_sort_syntax(this, &m_collate_delim);
682 }
683
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
687 {
688 static const char_class_type masks[22] =
689 {
690 0,
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),
712 };
713 if(!m_custom_class_names.empty())
714 {
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())
718 return pos->second;
719 }
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];
723 }
724
725 template <class charT>
726 inline std::shared_ptr<const cpp_regex_traits_implementation<charT> > create_cpp_regex_traits(const std::locale& l)
727 {
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);
730 }
731
732 } // BOOST_REGEX_DETAIL_NS
733
734 template <class charT>
735 class cpp_regex_traits
736 {
737 private:
738 typedef std::ctype<charT> ctype_type;
739 public:
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;
745
746 struct boost_extensions_tag{};
747
748 cpp_regex_traits()
749 : m_pimpl(BOOST_REGEX_DETAIL_NS::create_cpp_regex_traits<charT>(std::locale()))
750 { }
751 static size_type length(const char_type* p)
752 {
753 return std::char_traits<charT>::length(p);
754 }
755 regex_constants::syntax_type syntax_type(charT c)const
756 {
757 return m_pimpl->syntax_type(c);
758 }
759 regex_constants::escape_syntax_type escape_syntax_type(charT c) const
760 {
761 return m_pimpl->escape_syntax_type(c);
762 }
763 charT translate(charT c) const
764 {
765 return c;
766 }
767 charT translate_nocase(charT c) const
768 {
769 return m_pimpl->m_pctype->tolower(c);
770 }
771 charT translate(charT c, bool icase) const
772 {
773 return icase ? m_pimpl->m_pctype->tolower(c) : c;
774 }
775 charT tolower(charT c) const
776 {
777 return m_pimpl->m_pctype->tolower(c);
778 }
779 charT toupper(charT c) const
780 {
781 return m_pimpl->m_pctype->toupper(c);
782 }
783 string_type transform(const charT* p1, const charT* p2) const
784 {
785 return m_pimpl->transform(p1, p2);
786 }
787 string_type transform_primary(const charT* p1, const charT* p2) const
788 {
789 return m_pimpl->transform_primary(p1, p2);
790 }
791 char_class_type lookup_classname(const charT* p1, const charT* p2) const
792 {
793 return m_pimpl->lookup_classname(p1, p2);
794 }
795 string_type lookup_collatename(const charT* p1, const charT* p2) const
796 {
797 return m_pimpl->lookup_collatename(p1, p2);
798 }
799 bool isctype(charT c, char_class_type f) const
800 {
801 typedef typename std::ctype<charT>::mask ctype_mask;
802
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);
816
817 if((f & mask_base)
818 && (m_pimpl->m_pctype->is(
819 static_cast<ctype_mask>(f & mask_base), c)))
820 return true;
821 else if((f & BOOST_REGEX_DETAIL_NS::cpp_regex_traits_implementation<charT>::mask_unicode) && BOOST_REGEX_DETAIL_NS::is_extended(c))
822 return true;
823 else if((f & BOOST_REGEX_DETAIL_NS::cpp_regex_traits_implementation<charT>::mask_word) && (c == '_'))
824 return true;
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))
828 return true;
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')))
831 return true;
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))
834 return true;
835 #ifdef __CYGWIN__
836 //
837 // Cygwin has a buggy ctype facet, see https://www.cygwin.com/ml/cygwin/2012-08/msg00178.html:
838 //
839 else if((f & std::ctype<charT>::xdigit) == std::ctype<charT>::xdigit)
840 {
841 if((c >= 'a') && (c <= 'f'))
842 return true;
843 if((c >= 'A') && (c <= 'F'))
844 return true;
845 }
846 #endif
847 return false;
848 }
849 std::intmax_t toi(const charT*& p1, const charT* p2, int radix)const;
850 int value(charT c, int radix)const
851 {
852 const charT* pc = &c;
853 return (int)toi(pc, pc + 1, radix);
854 }
855 locale_type imbue(locale_type l)
856 {
857 std::locale result(getloc());
858 m_pimpl = BOOST_REGEX_DETAIL_NS::create_cpp_regex_traits<charT>(l);
859 return result;
860 }
861 locale_type getloc()const
862 {
863 return m_pimpl->m_locale;
864 }
865 std::string error_string(regex_constants::error_type n) const
866 {
867 return m_pimpl->error_string(n);
868 }
869
870 //
871 // extension:
872 // set the name of the message catalog in use (defaults to "boost_regex").
873 //
874 static std::string catalog_name(const std::string& name);
875 static std::string get_catalog_name();
876
877 private:
878 std::shared_ptr<const BOOST_REGEX_DETAIL_NS::cpp_regex_traits_implementation<charT> > m_pimpl;
879 //
880 // catalog name handler:
881 //
882 static std::string& get_catalog_name_inst();
883
884 #ifdef BOOST_HAS_THREADS
885 static std::mutex& get_mutex_inst();
886 #endif
887 };
888
889
890 template <class charT>
891 std::intmax_t cpp_regex_traits<charT>::toi(const charT*& first, const charT* last, int radix)const
892 {
893 BOOST_REGEX_DETAIL_NS::parser_buf<charT> sbuf; // buffer for parsing numbers.
894 std::basic_istream<charT> is(&sbuf); // stream for parsing numbers.
895
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());
898
899 sbuf.pubsetbuf(const_cast<charT*>(static_cast<const charT*>(first)), static_cast<std::streamsize>(last-first));
900 is.clear();
901 if(std::abs(radix) == 16) is >> std::hex;
902 else if(std::abs(radix) == 8) is >> std::oct;
903 else is >> std::dec;
904 std::intmax_t val;
905 if(is >> val)
906 {
907 first = first + ((last - first) - sbuf.in_avail());
908 return val;
909 }
910 else
911 return -1;
912 }
913
914 template <class charT>
915 std::string cpp_regex_traits<charT>::catalog_name(const std::string& name)
916 {
917 #ifdef BOOST_HAS_THREADS
918 std::lock_guard<std::mutex> lk(get_mutex_inst());
919 #endif
920 std::string result(get_catalog_name_inst());
921 get_catalog_name_inst() = name;
922 return result;
923 }
924
925 template <class charT>
926 std::string& cpp_regex_traits<charT>::get_catalog_name_inst()
927 {
928 static std::string s_name;
929 return s_name;
930 }
931
932 template <class charT>
933 std::string cpp_regex_traits<charT>::get_catalog_name()
934 {
935 #ifdef BOOST_HAS_THREADS
936 std::lock_guard<std::mutex> lk(get_mutex_inst());
937 #endif
938 std::string result(get_catalog_name_inst());
939 return result;
940 }
941
942 #ifdef BOOST_HAS_THREADS
943 template <class charT>
944 std::mutex& cpp_regex_traits<charT>::get_mutex_inst()
945 {
946 static std::mutex s_mutex;
947 return s_mutex;
948 }
949 #endif
950
951 namespace BOOST_REGEX_DETAIL_NS {
952
953 inline void cpp_regex_traits_char_layer<char>::init()
954 {
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));
958 #ifndef __IBMCPP__
959 std::messages<char>::catalog cat = static_cast<std::messages<char>::catalog>(-1);
960 #else
961 std::messages<char>::catalog cat = reinterpret_cast<std::messages<char>::catalog>(-1);
962 #endif
963 std::string cat_name(cpp_regex_traits<char>::get_catalog_name());
964 if ((!cat_name.empty()) && (m_pmessages != 0))
965 {
966 cat = this->m_pmessages->open(
967 cat_name,
968 this->m_locale);
969 if ((int)cat < 0)
970 {
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);
974 }
975 }
976 //
977 // if we have a valid catalog then load our messages:
978 //
979 if ((int)cat >= 0)
980 {
981 #ifndef BOOST_NO_EXCEPTIONS
982 try {
983 #endif
984 for (regex_constants::syntax_type i = 1; i < regex_constants::syntax_max; ++i)
985 {
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)
988 {
989 m_char_map[static_cast<unsigned char>(mss[j])] = i;
990 }
991 }
992 this->m_pmessages->close(cat);
993 #ifndef BOOST_NO_EXCEPTIONS
994 }
995 catch (...)
996 {
997 this->m_pmessages->close(cat);
998 throw;
999 }
1000 #endif
1001 }
1002 else
1003 {
1004 for (regex_constants::syntax_type j = 1; j < regex_constants::syntax_max; ++j)
1005 {
1006 const char* ptr = get_default_syntax(j);
1007 while (ptr && *ptr)
1008 {
1009 m_char_map[static_cast<unsigned char>(*ptr)] = j;
1010 ++ptr;
1011 }
1012 }
1013 }
1014 //
1015 // finish off by calculating our escape types:
1016 //
1017 unsigned char i = 'A';
1018 do
1019 {
1020 if (m_char_map[i] == 0)
1021 {
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;
1026 }
1027 } while (0xFF != i++);
1028 }
1029
1030 } // namespace detail
1031
1032
1033 } // boost
1034
1035 #ifdef BOOST_REGEX_MSVC
1036 #pragma warning(pop)
1037 #endif
1038
1039
1040 #endif