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 c_regex_traits.hpp
15 * VERSION see <boost/version.hpp>
16 * DESCRIPTION: Declares regular expression traits class that wraps the global C locale.
19 #ifndef BOOST_C_REGEX_TRAITS_HPP_INCLUDED
20 #define BOOST_C_REGEX_TRAITS_HPP_INCLUDED
22 #include <boost/regex/config.hpp>
23 #include <boost/regex/v5/regex_workaround.hpp>
28 namespace BOOST_REGEX_DETAIL_NS {
32 char_class_space = 1 << 0,
33 char_class_print = 1 << 1,
34 char_class_cntrl = 1 << 2,
35 char_class_upper = 1 << 3,
36 char_class_lower = 1 << 4,
37 char_class_alpha = 1 << 5,
38 char_class_digit = 1 << 6,
39 char_class_punct = 1 << 7,
40 char_class_xdigit = 1 << 8,
41 char_class_alnum = char_class_alpha | char_class_digit,
42 char_class_graph = char_class_alnum | char_class_punct,
43 char_class_blank = 1 << 9,
44 char_class_word = 1 << 10,
45 char_class_unicode = 1 << 11,
46 char_class_horizontal = 1 << 12,
47 char_class_vertical = 1 << 13
52 template <class charT>
53 struct c_regex_traits;
56 struct c_regex_traits<char>
59 typedef char char_type;
60 typedef std::size_t size_type;
61 typedef std::string string_type;
63 typedef std::uint32_t char_class_type;
65 static size_type length(const char_type* p)
67 return (std::strlen)(p);
70 char translate(char c) const
74 char translate_nocase(char c) const
76 return static_cast<char>((std::tolower)(static_cast<unsigned char>(c)));
79 static string_type transform(const char* p1, const char* p2);
80 static string_type transform_primary(const char* p1, const char* p2);
82 static char_class_type lookup_classname(const char* p1, const char* p2);
83 static string_type lookup_collatename(const char* p1, const char* p2);
85 static bool isctype(char, char_class_type);
86 static int value(char, int);
88 locale_type imbue(locale_type l)
90 locale_type getloc()const
91 { return locale_type(); }
94 // this type is not copyable:
95 c_regex_traits(const c_regex_traits&);
96 c_regex_traits& operator=(const c_regex_traits&);
99 #ifndef BOOST_NO_WREGEX
101 struct c_regex_traits<wchar_t>
104 typedef wchar_t char_type;
105 typedef std::size_t size_type;
106 typedef std::wstring string_type;
107 struct locale_type{};
108 typedef std::uint32_t char_class_type;
110 static size_type length(const char_type* p)
112 return (std::wcslen)(p);
115 wchar_t translate(wchar_t c) const
119 wchar_t translate_nocase(wchar_t c) const
121 return (std::towlower)(c);
124 static string_type transform(const wchar_t* p1, const wchar_t* p2);
125 static string_type transform_primary(const wchar_t* p1, const wchar_t* p2);
127 static char_class_type lookup_classname(const wchar_t* p1, const wchar_t* p2);
128 static string_type lookup_collatename(const wchar_t* p1, const wchar_t* p2);
130 static bool isctype(wchar_t, char_class_type);
131 static int value(wchar_t, int);
133 locale_type imbue(locale_type l)
135 locale_type getloc()const
136 { return locale_type(); }
139 // this type is not copyable:
140 c_regex_traits(const c_regex_traits&);
141 c_regex_traits& operator=(const c_regex_traits&);
144 #endif // BOOST_NO_WREGEX
146 inline c_regex_traits<char>::string_type c_regex_traits<char>::transform(const char* p1, const char* p2)
148 std::string result(10, ' ');
149 std::size_t s = result.size();
151 std::string src(p1, p2);
152 while (s < (r = std::strxfrm(&*result.begin(), src.c_str(), s)))
154 #if defined(_CPPLIB_VER)
156 // A bug in VC11 and 12 causes the program to hang if we pass a null-string
157 // to std::strxfrm, but only for certain locales :-(
158 // Probably effects Intel and Clang or any compiler using the VC std library (Dinkumware).
163 result.insert(result.begin(), static_cast<char>(0));
167 result.append(r - s + 3, ' ');
174 inline c_regex_traits<char>::string_type c_regex_traits<char>::transform_primary(const char* p1, const char* p2)
177 static const int s_collate_type = ::boost::BOOST_REGEX_DETAIL_NS::find_sort_syntax(static_cast<c_regex_traits<char>*>(0), &s_delim);
180 // What we do here depends upon the format of the sort key returned by
181 // sort key returned by this->transform:
183 switch (s_collate_type)
185 case ::boost::BOOST_REGEX_DETAIL_NS::sort_C:
186 case ::boost::BOOST_REGEX_DETAIL_NS::sort_unknown:
187 // the best we can do is translate to lower case, then get a regular sort key:
189 result.assign(p1, p2);
190 for (std::string::size_type i = 0; i < result.size(); ++i)
191 result[i] = static_cast<char>((std::tolower)(static_cast<unsigned char>(result[i])));
192 result = transform(&*result.begin(), &*result.begin() + result.size());
195 case ::boost::BOOST_REGEX_DETAIL_NS::sort_fixed:
197 // get a regular sort key, and then truncate it:
198 result = transform(p1, p2);
199 result.erase(s_delim);
202 case ::boost::BOOST_REGEX_DETAIL_NS::sort_delim:
203 // get a regular sort key, and then truncate everything after the delim:
204 result = transform(p1, p2);
205 if ((!result.empty()) && (result[0] == s_delim))
208 for (i = 0; i < result.size(); ++i)
210 if (result[i] == s_delim)
217 result = std::string(1, char(0));
221 inline c_regex_traits<char>::char_class_type c_regex_traits<char>::lookup_classname(const char* p1, const char* p2)
223 using namespace BOOST_REGEX_DETAIL_NS;
224 static const char_class_type masks[] =
234 char_class_horizontal,
245 char_class_alnum | char_class_word,
246 char_class_alnum | char_class_word,
250 int idx = ::boost::BOOST_REGEX_DETAIL_NS::get_default_class_id(p1, p2);
253 std::string s(p1, p2);
254 for (std::string::size_type i = 0; i < s.size(); ++i)
255 s[i] = static_cast<char>((std::tolower)(static_cast<unsigned char>(s[i])));
256 idx = ::boost::BOOST_REGEX_DETAIL_NS::get_default_class_id(&*s.begin(), &*s.begin() + s.size());
258 BOOST_REGEX_ASSERT(std::size_t(idx) + 1u < sizeof(masks) / sizeof(masks[0]));
259 return masks[idx + 1];
262 inline bool c_regex_traits<char>::isctype(char c, char_class_type mask)
264 using namespace BOOST_REGEX_DETAIL_NS;
266 ((mask & char_class_space) && (std::isspace)(static_cast<unsigned char>(c)))
267 || ((mask & char_class_print) && (std::isprint)(static_cast<unsigned char>(c)))
268 || ((mask & char_class_cntrl) && (std::iscntrl)(static_cast<unsigned char>(c)))
269 || ((mask & char_class_upper) && (std::isupper)(static_cast<unsigned char>(c)))
270 || ((mask & char_class_lower) && (std::islower)(static_cast<unsigned char>(c)))
271 || ((mask & char_class_alpha) && (std::isalpha)(static_cast<unsigned char>(c)))
272 || ((mask & char_class_digit) && (std::isdigit)(static_cast<unsigned char>(c)))
273 || ((mask & char_class_punct) && (std::ispunct)(static_cast<unsigned char>(c)))
274 || ((mask & char_class_xdigit) && (std::isxdigit)(static_cast<unsigned char>(c)))
275 || ((mask & char_class_blank) && (std::isspace)(static_cast<unsigned char>(c)) && !::boost::BOOST_REGEX_DETAIL_NS::is_separator(c))
276 || ((mask & char_class_word) && (c == '_'))
277 || ((mask & char_class_vertical) && (::boost::BOOST_REGEX_DETAIL_NS::is_separator(c) || (c == '\v')))
278 || ((mask & char_class_horizontal) && (std::isspace)(static_cast<unsigned char>(c)) && !::boost::BOOST_REGEX_DETAIL_NS::is_separator(c) && (c != '\v'));
281 inline c_regex_traits<char>::string_type c_regex_traits<char>::lookup_collatename(const char* p1, const char* p2)
283 std::string s(p1, p2);
284 s = ::boost::BOOST_REGEX_DETAIL_NS::lookup_default_collate_name(s);
285 if (s.empty() && (p2 - p1 == 1))
290 inline int c_regex_traits<char>::value(char c, int radix)
292 char b[2] = { c, '\0', };
294 int result = std::strtol(b, &ep, radix);
300 #ifndef BOOST_NO_WREGEX
302 inline c_regex_traits<wchar_t>::string_type c_regex_traits<wchar_t>::transform(const wchar_t* p1, const wchar_t* p2)
306 std::wstring src(p1, p2);
307 std::wstring result(s, L' ');
308 while (s < (r = std::wcsxfrm(&*result.begin(), src.c_str(), s)))
310 #if defined(_CPPLIB_VER)
312 // A bug in VC11 and 12 causes the program to hang if we pass a null-string
313 // to std::strxfrm, but only for certain locales :-(
314 // Probably effects Intel and Clang or any compiler using the VC std library (Dinkumware).
319 result.insert(result.begin(), static_cast<wchar_t>(0));
323 result.append(r - s + 3, L' ');
330 inline c_regex_traits<wchar_t>::string_type c_regex_traits<wchar_t>::transform_primary(const wchar_t* p1, const wchar_t* p2)
332 static wchar_t s_delim;
333 static const int s_collate_type = ::boost::BOOST_REGEX_DETAIL_NS::find_sort_syntax(static_cast<const c_regex_traits<wchar_t>*>(0), &s_delim);
336 // What we do here depends upon the format of the sort key returned by
337 // sort key returned by this->transform:
339 switch (s_collate_type)
341 case ::boost::BOOST_REGEX_DETAIL_NS::sort_C:
342 case ::boost::BOOST_REGEX_DETAIL_NS::sort_unknown:
343 // the best we can do is translate to lower case, then get a regular sort key:
345 result.assign(p1, p2);
346 for (std::wstring::size_type i = 0; i < result.size(); ++i)
347 result[i] = (std::towlower)(result[i]);
348 result = c_regex_traits<wchar_t>::transform(&*result.begin(), &*result.begin() + result.size());
351 case ::boost::BOOST_REGEX_DETAIL_NS::sort_fixed:
353 // get a regular sort key, and then truncate it:
354 result = c_regex_traits<wchar_t>::transform(&*result.begin(), &*result.begin() + result.size());
355 result.erase(s_delim);
358 case ::boost::BOOST_REGEX_DETAIL_NS::sort_delim:
359 // get a regular sort key, and then truncate everything after the delim:
360 result = c_regex_traits<wchar_t>::transform(&*result.begin(), &*result.begin() + result.size());
361 if ((!result.empty()) && (result[0] == s_delim))
364 for (i = 0; i < result.size(); ++i)
366 if (result[i] == s_delim)
373 result = std::wstring(1, char(0));
377 inline c_regex_traits<wchar_t>::char_class_type c_regex_traits<wchar_t>::lookup_classname(const wchar_t* p1, const wchar_t* p2)
379 using namespace BOOST_REGEX_DETAIL_NS;
380 static const char_class_type masks[] =
390 char_class_horizontal,
401 char_class_alnum | char_class_word,
402 char_class_alnum | char_class_word,
406 int idx = ::boost::BOOST_REGEX_DETAIL_NS::get_default_class_id(p1, p2);
409 std::wstring s(p1, p2);
410 for (std::wstring::size_type i = 0; i < s.size(); ++i)
411 s[i] = (std::towlower)(s[i]);
412 idx = ::boost::BOOST_REGEX_DETAIL_NS::get_default_class_id(&*s.begin(), &*s.begin() + s.size());
414 BOOST_REGEX_ASSERT(idx + 1 < static_cast<int>(sizeof(masks) / sizeof(masks[0])));
415 return masks[idx + 1];
418 inline bool c_regex_traits<wchar_t>::isctype(wchar_t c, char_class_type mask)
420 using namespace BOOST_REGEX_DETAIL_NS;
422 ((mask & char_class_space) && (std::iswspace)(c))
423 || ((mask & char_class_print) && (std::iswprint)(c))
424 || ((mask & char_class_cntrl) && (std::iswcntrl)(c))
425 || ((mask & char_class_upper) && (std::iswupper)(c))
426 || ((mask & char_class_lower) && (std::iswlower)(c))
427 || ((mask & char_class_alpha) && (std::iswalpha)(c))
428 || ((mask & char_class_digit) && (std::iswdigit)(c))
429 || ((mask & char_class_punct) && (std::iswpunct)(c))
430 || ((mask & char_class_xdigit) && (std::iswxdigit)(c))
431 || ((mask & char_class_blank) && (std::iswspace)(c) && !::boost::BOOST_REGEX_DETAIL_NS::is_separator(c))
432 || ((mask & char_class_word) && (c == '_'))
433 || ((mask & char_class_unicode) && (c & ~static_cast<wchar_t>(0xff)))
434 || ((mask & char_class_vertical) && (::boost::BOOST_REGEX_DETAIL_NS::is_separator(c) || (c == L'\v')))
435 || ((mask & char_class_horizontal) && (std::iswspace)(c) && !::boost::BOOST_REGEX_DETAIL_NS::is_separator(c) && (c != L'\v'));
438 inline c_regex_traits<wchar_t>::string_type c_regex_traits<wchar_t>::lookup_collatename(const wchar_t* p1, const wchar_t* p2)
441 // Usual msvc warning suppression does not work here with std::string template constructor.... use a workaround instead:
442 for (const wchar_t* pos = p1; pos != p2; ++pos)
443 name.push_back((char)*pos);
444 name = ::boost::BOOST_REGEX_DETAIL_NS::lookup_default_collate_name(name);
446 return string_type(name.begin(), name.end());
448 return string_type(1, *p1);
449 return string_type();
452 inline int c_regex_traits<wchar_t>::value(wchar_t c, int radix)
454 #ifdef BOOST_BORLANDC
455 // workaround for broken wcstol:
456 if ((std::iswxdigit)(c) == 0)
459 wchar_t b[2] = { c, '\0', };
461 int result = std::wcstol(b, &ep, radix);