Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-11-16 09:32:53

0001 /*
0002  *
0003  * Copyright (c) 2004
0004  * John Maddock
0005  *
0006  * Use, modification and distribution are subject to the 
0007  * Boost Software License, Version 1.0. (See accompanying file 
0008  * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0009  *
0010  */
0011  
0012  /*
0013   *   LOCATION:    see http://www.boost.org for most recent version.
0014   *   FILE         c_regex_traits.hpp
0015   *   VERSION      see <boost/version.hpp>
0016   *   DESCRIPTION: Declares regular expression traits class that wraps the global C locale.
0017   */
0018 
0019 #ifndef BOOST_C_REGEX_TRAITS_HPP_INCLUDED
0020 #define BOOST_C_REGEX_TRAITS_HPP_INCLUDED
0021 
0022 #include <boost/regex/config.hpp>
0023 #include <boost/regex/v5/regex_workaround.hpp>
0024 #include <cctype>
0025 
0026 namespace boost{
0027 
0028    namespace BOOST_REGEX_DETAIL_NS {
0029 
0030       enum
0031       {
0032          char_class_space = 1 << 0,
0033          char_class_print = 1 << 1,
0034          char_class_cntrl = 1 << 2,
0035          char_class_upper = 1 << 3,
0036          char_class_lower = 1 << 4,
0037          char_class_alpha = 1 << 5,
0038          char_class_digit = 1 << 6,
0039          char_class_punct = 1 << 7,
0040          char_class_xdigit = 1 << 8,
0041          char_class_alnum = char_class_alpha | char_class_digit,
0042          char_class_graph = char_class_alnum | char_class_punct,
0043          char_class_blank = 1 << 9,
0044          char_class_word = 1 << 10,
0045          char_class_unicode = 1 << 11,
0046          char_class_horizontal = 1 << 12,
0047          char_class_vertical = 1 << 13
0048       };
0049 
0050    }
0051 
0052 template <class charT>
0053 struct c_regex_traits;
0054 
0055 template<>
0056 struct c_regex_traits<char>
0057 {
0058    c_regex_traits(){}
0059    typedef char char_type;
0060    typedef std::size_t size_type;
0061    typedef std::string string_type;
0062    struct locale_type{};
0063    typedef std::uint32_t char_class_type;
0064 
0065    static size_type length(const char_type* p) 
0066    { 
0067       return (std::strlen)(p); 
0068    }
0069 
0070    char translate(char c) const 
0071    { 
0072       return c; 
0073    }
0074    char translate_nocase(char c) const 
0075    { 
0076       return static_cast<char>((std::tolower)(static_cast<unsigned char>(c))); 
0077    }
0078 
0079    static string_type  transform(const char* p1, const char* p2);
0080    static string_type  transform_primary(const char* p1, const char* p2);
0081 
0082    static char_class_type  lookup_classname(const char* p1, const char* p2);
0083    static string_type  lookup_collatename(const char* p1, const char* p2);
0084 
0085    static bool  isctype(char, char_class_type);
0086    static int  value(char, int);
0087 
0088    locale_type imbue(locale_type l)
0089    { return l; }
0090    locale_type getloc()const
0091    { return locale_type(); }
0092 
0093 private:
0094    // this type is not copyable:
0095    c_regex_traits(const c_regex_traits&);
0096    c_regex_traits& operator=(const c_regex_traits&);
0097 };
0098 
0099 #ifndef BOOST_NO_WREGEX
0100 template<>
0101 struct c_regex_traits<wchar_t>
0102 {
0103    c_regex_traits(){}
0104    typedef wchar_t char_type;
0105    typedef std::size_t size_type;
0106    typedef std::wstring string_type;
0107    struct locale_type{};
0108    typedef std::uint32_t char_class_type;
0109 
0110    static size_type length(const char_type* p) 
0111    { 
0112       return (std::wcslen)(p); 
0113    }
0114 
0115    wchar_t translate(wchar_t c) const 
0116    { 
0117       return c; 
0118    }
0119    wchar_t translate_nocase(wchar_t c) const 
0120    { 
0121       return (std::towlower)(c); 
0122    }
0123 
0124    static string_type  transform(const wchar_t* p1, const wchar_t* p2);
0125    static string_type  transform_primary(const wchar_t* p1, const wchar_t* p2);
0126 
0127    static char_class_type  lookup_classname(const wchar_t* p1, const wchar_t* p2);
0128    static string_type  lookup_collatename(const wchar_t* p1, const wchar_t* p2);
0129 
0130    static bool  isctype(wchar_t, char_class_type);
0131    static int  value(wchar_t, int);
0132 
0133    locale_type imbue(locale_type l)
0134    { return l; }
0135    locale_type getloc()const
0136    { return locale_type(); }
0137 
0138 private:
0139    // this type is not copyable:
0140    c_regex_traits(const c_regex_traits&);
0141    c_regex_traits& operator=(const c_regex_traits&);
0142 };
0143 
0144 #endif // BOOST_NO_WREGEX
0145 
0146 inline c_regex_traits<char>::string_type  c_regex_traits<char>::transform(const char* p1, const char* p2)
0147 {
0148    std::string result(10, ' ');
0149    std::size_t s = result.size();
0150    std::size_t r;
0151    std::string src(p1, p2);
0152    while (s < (r = std::strxfrm(&*result.begin(), src.c_str(), s)))
0153    {
0154 #if defined(_CPPLIB_VER)
0155       //
0156       // A bug in VC11 and 12 causes the program to hang if we pass a null-string
0157       // to std::strxfrm, but only for certain locales :-(
0158       // Probably effects Intel and Clang or any compiler using the VC std library (Dinkumware).
0159       //
0160       if (r == INT_MAX)
0161       {
0162          result.erase();
0163          result.insert(result.begin(), static_cast<char>(0));
0164          return result;
0165       }
0166 #endif
0167       result.append(r - s + 3, ' ');
0168       s = result.size();
0169    }
0170    result.erase(r);
0171    return result;
0172 }
0173 
0174 inline c_regex_traits<char>::string_type  c_regex_traits<char>::transform_primary(const char* p1, const char* p2)
0175 {
0176    static char s_delim;
0177    static const int s_collate_type = ::boost::BOOST_REGEX_DETAIL_NS::find_sort_syntax(static_cast<c_regex_traits<char>*>(0), &s_delim);
0178    std::string result;
0179    //
0180    // What we do here depends upon the format of the sort key returned by
0181    // sort key returned by this->transform:
0182    //
0183    switch (s_collate_type)
0184    {
0185    case ::boost::BOOST_REGEX_DETAIL_NS::sort_C:
0186    case ::boost::BOOST_REGEX_DETAIL_NS::sort_unknown:
0187       // the best we can do is translate to lower case, then get a regular sort key:
0188    {
0189       result.assign(p1, p2);
0190       for (std::string::size_type i = 0; i < result.size(); ++i)
0191          result[i] = static_cast<char>((std::tolower)(static_cast<unsigned char>(result[i])));
0192       result = transform(&*result.begin(), &*result.begin() + result.size());
0193       break;
0194    }
0195    case ::boost::BOOST_REGEX_DETAIL_NS::sort_fixed:
0196    {
0197       // get a regular sort key, and then truncate it:
0198       result = transform(p1, p2);
0199       result.erase(s_delim);
0200       break;
0201    }
0202    case ::boost::BOOST_REGEX_DETAIL_NS::sort_delim:
0203       // get a regular sort key, and then truncate everything after the delim:
0204       result = transform(p1, p2);
0205       if ((!result.empty()) && (result[0] == s_delim))
0206          break;
0207       std::size_t i;
0208       for (i = 0; i < result.size(); ++i)
0209       {
0210          if (result[i] == s_delim)
0211             break;
0212       }
0213       result.erase(i);
0214       break;
0215    }
0216    if (result.empty())
0217       result = std::string(1, char(0));
0218    return result;
0219 }
0220 
0221 inline c_regex_traits<char>::char_class_type  c_regex_traits<char>::lookup_classname(const char* p1, const char* p2)
0222 {
0223    using namespace BOOST_REGEX_DETAIL_NS;
0224    static const char_class_type masks[] =
0225    {
0226       0,
0227       char_class_alnum,
0228       char_class_alpha,
0229       char_class_blank,
0230       char_class_cntrl,
0231       char_class_digit,
0232       char_class_digit,
0233       char_class_graph,
0234       char_class_horizontal,
0235       char_class_lower,
0236       char_class_lower,
0237       char_class_print,
0238       char_class_punct,
0239       char_class_space,
0240       char_class_space,
0241       char_class_upper,
0242       char_class_unicode,
0243       char_class_upper,
0244       char_class_vertical,
0245       char_class_alnum | char_class_word,
0246       char_class_alnum | char_class_word,
0247       char_class_xdigit,
0248    };
0249 
0250    int idx = ::boost::BOOST_REGEX_DETAIL_NS::get_default_class_id(p1, p2);
0251    if (idx < 0)
0252    {
0253       std::string s(p1, p2);
0254       for (std::string::size_type i = 0; i < s.size(); ++i)
0255          s[i] = static_cast<char>((std::tolower)(static_cast<unsigned char>(s[i])));
0256       idx = ::boost::BOOST_REGEX_DETAIL_NS::get_default_class_id(&*s.begin(), &*s.begin() + s.size());
0257    }
0258    BOOST_REGEX_ASSERT(std::size_t(idx) + 1u < sizeof(masks) / sizeof(masks[0]));
0259    return masks[idx + 1];
0260 }
0261 
0262 inline bool  c_regex_traits<char>::isctype(char c, char_class_type mask)
0263 {
0264    using namespace BOOST_REGEX_DETAIL_NS;
0265    return
0266       ((mask & char_class_space) && (std::isspace)(static_cast<unsigned char>(c)))
0267       || ((mask & char_class_print) && (std::isprint)(static_cast<unsigned char>(c)))
0268       || ((mask & char_class_cntrl) && (std::iscntrl)(static_cast<unsigned char>(c)))
0269       || ((mask & char_class_upper) && (std::isupper)(static_cast<unsigned char>(c)))
0270       || ((mask & char_class_lower) && (std::islower)(static_cast<unsigned char>(c)))
0271       || ((mask & char_class_alpha) && (std::isalpha)(static_cast<unsigned char>(c)))
0272       || ((mask & char_class_digit) && (std::isdigit)(static_cast<unsigned char>(c)))
0273       || ((mask & char_class_punct) && (std::ispunct)(static_cast<unsigned char>(c)))
0274       || ((mask & char_class_xdigit) && (std::isxdigit)(static_cast<unsigned char>(c)))
0275       || ((mask & char_class_blank) && (std::isspace)(static_cast<unsigned char>(c)) && !::boost::BOOST_REGEX_DETAIL_NS::is_separator(c))
0276       || ((mask & char_class_word) && (c == '_'))
0277       || ((mask & char_class_vertical) && (::boost::BOOST_REGEX_DETAIL_NS::is_separator(c) || (c == '\v')))
0278       || ((mask & char_class_horizontal) && (std::isspace)(static_cast<unsigned char>(c)) && !::boost::BOOST_REGEX_DETAIL_NS::is_separator(c) && (c != '\v'));
0279 }
0280 
0281 inline c_regex_traits<char>::string_type  c_regex_traits<char>::lookup_collatename(const char* p1, const char* p2)
0282 {
0283    std::string s(p1, p2);
0284    s = ::boost::BOOST_REGEX_DETAIL_NS::lookup_default_collate_name(s);
0285    if (s.empty() && (p2 - p1 == 1))
0286       s.append(1, *p1);
0287    return s;
0288 }
0289 
0290 inline int  c_regex_traits<char>::value(char c, int radix)
0291 {
0292    char b[2] = { c, '\0', };
0293    char* ep;
0294    int result = std::strtol(b, &ep, radix);
0295    if (ep == b)
0296       return -1;
0297    return result;
0298 }
0299 
0300 #ifndef BOOST_NO_WREGEX
0301 
0302 inline c_regex_traits<wchar_t>::string_type  c_regex_traits<wchar_t>::transform(const wchar_t* p1, const wchar_t* p2)
0303 {
0304    std::size_t r;
0305    std::size_t s = 10;
0306    std::wstring src(p1, p2);
0307    std::wstring result(s, L' ');
0308    while (s < (r = std::wcsxfrm(&*result.begin(), src.c_str(), s)))
0309    {
0310 #if defined(_CPPLIB_VER)
0311       //
0312       // A bug in VC11 and 12 causes the program to hang if we pass a null-string
0313       // to std::strxfrm, but only for certain locales :-(
0314       // Probably effects Intel and Clang or any compiler using the VC std library (Dinkumware).
0315       //
0316       if (r == INT_MAX)
0317       {
0318          result.erase();
0319          result.insert(result.begin(), static_cast<wchar_t>(0));
0320          return result;
0321       }
0322 #endif
0323       result.append(r - s + 3, L' ');
0324       s = result.size();
0325    }
0326    result.erase(r);
0327    return result;
0328 }
0329 
0330 inline c_regex_traits<wchar_t>::string_type  c_regex_traits<wchar_t>::transform_primary(const wchar_t* p1, const wchar_t* p2)
0331 {
0332    static wchar_t s_delim;
0333    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);
0334    std::wstring result;
0335    //
0336    // What we do here depends upon the format of the sort key returned by
0337    // sort key returned by this->transform:
0338    //
0339    switch (s_collate_type)
0340    {
0341    case ::boost::BOOST_REGEX_DETAIL_NS::sort_C:
0342    case ::boost::BOOST_REGEX_DETAIL_NS::sort_unknown:
0343       // the best we can do is translate to lower case, then get a regular sort key:
0344    {
0345       result.assign(p1, p2);
0346       for (std::wstring::size_type i = 0; i < result.size(); ++i)
0347          result[i] = (std::towlower)(result[i]);
0348       result = c_regex_traits<wchar_t>::transform(&*result.begin(), &*result.begin() + result.size());
0349       break;
0350    }
0351    case ::boost::BOOST_REGEX_DETAIL_NS::sort_fixed:
0352    {
0353       // get a regular sort key, and then truncate it:
0354       result = c_regex_traits<wchar_t>::transform(&*result.begin(), &*result.begin() + result.size());
0355       result.erase(s_delim);
0356       break;
0357    }
0358    case ::boost::BOOST_REGEX_DETAIL_NS::sort_delim:
0359       // get a regular sort key, and then truncate everything after the delim:
0360       result = c_regex_traits<wchar_t>::transform(&*result.begin(), &*result.begin() + result.size());
0361       if ((!result.empty()) && (result[0] == s_delim))
0362          break;
0363       std::size_t i;
0364       for (i = 0; i < result.size(); ++i)
0365       {
0366          if (result[i] == s_delim)
0367             break;
0368       }
0369       result.erase(i);
0370       break;
0371    }
0372    if (result.empty())
0373       result = std::wstring(1, char(0));
0374    return result;
0375 }
0376 
0377 inline c_regex_traits<wchar_t>::char_class_type  c_regex_traits<wchar_t>::lookup_classname(const wchar_t* p1, const wchar_t* p2)
0378 {
0379    using namespace BOOST_REGEX_DETAIL_NS;
0380    static const char_class_type masks[] =
0381    {
0382       0,
0383       char_class_alnum,
0384       char_class_alpha,
0385       char_class_blank,
0386       char_class_cntrl,
0387       char_class_digit,
0388       char_class_digit,
0389       char_class_graph,
0390       char_class_horizontal,
0391       char_class_lower,
0392       char_class_lower,
0393       char_class_print,
0394       char_class_punct,
0395       char_class_space,
0396       char_class_space,
0397       char_class_upper,
0398       char_class_unicode,
0399       char_class_upper,
0400       char_class_vertical,
0401       char_class_alnum | char_class_word,
0402       char_class_alnum | char_class_word,
0403       char_class_xdigit,
0404    };
0405 
0406    int idx = ::boost::BOOST_REGEX_DETAIL_NS::get_default_class_id(p1, p2);
0407    if (idx < 0)
0408    {
0409       std::wstring s(p1, p2);
0410       for (std::wstring::size_type i = 0; i < s.size(); ++i)
0411          s[i] = (std::towlower)(s[i]);
0412       idx = ::boost::BOOST_REGEX_DETAIL_NS::get_default_class_id(&*s.begin(), &*s.begin() + s.size());
0413    }
0414    BOOST_REGEX_ASSERT(idx + 1 < static_cast<int>(sizeof(masks) / sizeof(masks[0])));
0415    return masks[idx + 1];
0416 }
0417 
0418 inline bool  c_regex_traits<wchar_t>::isctype(wchar_t c, char_class_type mask)
0419 {
0420    using namespace BOOST_REGEX_DETAIL_NS;
0421    return
0422       ((mask & char_class_space) && (std::iswspace)(c))
0423       || ((mask & char_class_print) && (std::iswprint)(c))
0424       || ((mask & char_class_cntrl) && (std::iswcntrl)(c))
0425       || ((mask & char_class_upper) && (std::iswupper)(c))
0426       || ((mask & char_class_lower) && (std::iswlower)(c))
0427       || ((mask & char_class_alpha) && (std::iswalpha)(c))
0428       || ((mask & char_class_digit) && (std::iswdigit)(c))
0429       || ((mask & char_class_punct) && (std::iswpunct)(c))
0430       || ((mask & char_class_xdigit) && (std::iswxdigit)(c))
0431       || ((mask & char_class_blank) && (std::iswspace)(c) && !::boost::BOOST_REGEX_DETAIL_NS::is_separator(c))
0432       || ((mask & char_class_word) && (c == '_'))
0433       || ((mask & char_class_unicode) && (c & ~static_cast<wchar_t>(0xff)))
0434       || ((mask & char_class_vertical) && (::boost::BOOST_REGEX_DETAIL_NS::is_separator(c) || (c == L'\v')))
0435       || ((mask & char_class_horizontal) && (std::iswspace)(c) && !::boost::BOOST_REGEX_DETAIL_NS::is_separator(c) && (c != L'\v'));
0436 }
0437 
0438 inline c_regex_traits<wchar_t>::string_type  c_regex_traits<wchar_t>::lookup_collatename(const wchar_t* p1, const wchar_t* p2)
0439 {
0440    std::string name;
0441    // Usual msvc warning suppression does not work here with std::string template constructor.... use a workaround instead:
0442    for (const wchar_t* pos = p1; pos != p2; ++pos)
0443       name.push_back((char)*pos);
0444    name = ::boost::BOOST_REGEX_DETAIL_NS::lookup_default_collate_name(name);
0445    if (!name.empty())
0446       return string_type(name.begin(), name.end());
0447    if (p2 - p1 == 1)
0448       return string_type(1, *p1);
0449    return string_type();
0450 }
0451 
0452 inline int  c_regex_traits<wchar_t>::value(wchar_t c, int radix)
0453 {
0454 #ifdef BOOST_BORLANDC
0455    // workaround for broken wcstol:
0456    if ((std::iswxdigit)(c) == 0)
0457       return -1;
0458 #endif
0459    wchar_t b[2] = { c, '\0', };
0460    wchar_t* ep;
0461    int result = std::wcstol(b, &ep, radix);
0462    if (ep == b)
0463       return -1;
0464    return result;
0465 }
0466 
0467 #endif
0468 
0469 }
0470 
0471 #endif
0472 
0473 
0474