| 1 | /* |
|---|
| 2 | * |
|---|
| 3 | * Copyright (c) 2004 |
|---|
| 4 | * John Maddock |
|---|
| 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: c_regex_traits.cpp |
|---|
| 15 | * VERSION: see <boost/version.hpp> |
|---|
| 16 | * DESCRIPTION: Implements out of line c_regex_traits<char> members |
|---|
| 17 | */ |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | #define BOOST_REGEX_SOURCE |
|---|
| 21 | |
|---|
| 22 | #include <boost/config.hpp> |
|---|
| 23 | #include <boost/detail/workaround.hpp> |
|---|
| 24 | |
|---|
| 25 | #if !BOOST_WORKAROUND(__BORLANDC__, < 0x560) |
|---|
| 26 | |
|---|
| 27 | #include <boost/regex/v4/c_regex_traits.hpp> |
|---|
| 28 | #include <boost/regex/v4/primary_transform.hpp> |
|---|
| 29 | #include <boost/regex/v4/regex_traits_defaults.hpp> |
|---|
| 30 | |
|---|
| 31 | #ifdef BOOST_NO_STDC_NAMESPACE |
|---|
| 32 | namespace std{ |
|---|
| 33 | using ::strxfrm; using ::isspace; |
|---|
| 34 | using ::ispunct; using ::isalpha; |
|---|
| 35 | using ::isalnum; using ::iscntrl; |
|---|
| 36 | using ::isprint; using ::isupper; |
|---|
| 37 | using ::islower; using ::isdigit; |
|---|
| 38 | using ::isxdigit; using ::strtol; |
|---|
| 39 | } |
|---|
| 40 | #endif |
|---|
| 41 | |
|---|
| 42 | #ifdef BOOST_HAS_ABI_HEADERS |
|---|
| 43 | # include BOOST_ABI_PREFIX |
|---|
| 44 | #endif |
|---|
| 45 | |
|---|
| 46 | namespace boost{ |
|---|
| 47 | |
|---|
| 48 | c_regex_traits<char>::string_type BOOST_REGEX_CALL c_regex_traits<char>::transform(const char* p1, const char* p2) |
|---|
| 49 | { |
|---|
| 50 | std::string result(10, ' '); |
|---|
| 51 | std::size_t s = result.size(); |
|---|
| 52 | std::size_t r; |
|---|
| 53 | std::string src(p1, p2); |
|---|
| 54 | while(s < (r = std::strxfrm(&*result.begin(), src.c_str(), s))) |
|---|
| 55 | { |
|---|
| 56 | result.append(r - s + 3, ' '); |
|---|
| 57 | s = result.size(); |
|---|
| 58 | } |
|---|
| 59 | result.erase(r); |
|---|
| 60 | return result; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | c_regex_traits<char>::string_type BOOST_REGEX_CALL c_regex_traits<char>::transform_primary(const char* p1, const char* p2) |
|---|
| 64 | { |
|---|
| 65 | static char s_delim; |
|---|
| 66 | static const int s_collate_type = ::boost::re_detail::find_sort_syntax(static_cast<c_regex_traits<char>*>(0), &s_delim); |
|---|
| 67 | std::string result; |
|---|
| 68 | // |
|---|
| 69 | // What we do here depends upon the format of the sort key returned by |
|---|
| 70 | // sort key returned by this->transform: |
|---|
| 71 | // |
|---|
| 72 | switch(s_collate_type) |
|---|
| 73 | { |
|---|
| 74 | case ::boost::re_detail::sort_C: |
|---|
| 75 | case ::boost::re_detail::sort_unknown: |
|---|
| 76 | // the best we can do is translate to lower case, then get a regular sort key: |
|---|
| 77 | { |
|---|
| 78 | result.assign(p1, p2); |
|---|
| 79 | for(std::string::size_type i = 0; i < result.size(); ++i) |
|---|
| 80 | result[i] = static_cast<char>((std::tolower)(static_cast<unsigned char>(result[i]))); |
|---|
| 81 | result = transform(&*result.begin(), &*result.begin() + result.size()); |
|---|
| 82 | break; |
|---|
| 83 | } |
|---|
| 84 | case ::boost::re_detail::sort_fixed: |
|---|
| 85 | { |
|---|
| 86 | // get a regular sort key, and then truncate it: |
|---|
| 87 | result = transform(p1, p2); |
|---|
| 88 | result.erase(s_delim); |
|---|
| 89 | break; |
|---|
| 90 | } |
|---|
| 91 | case ::boost::re_detail::sort_delim: |
|---|
| 92 | // get a regular sort key, and then truncate everything after the delim: |
|---|
| 93 | result = transform(p1, p2); |
|---|
| 94 | if(result.size() && (result[0] == s_delim)) |
|---|
| 95 | break; |
|---|
| 96 | std::size_t i; |
|---|
| 97 | for(i = 0; i < result.size(); ++i) |
|---|
| 98 | { |
|---|
| 99 | if(result[i] == s_delim) |
|---|
| 100 | break; |
|---|
| 101 | } |
|---|
| 102 | result.erase(i); |
|---|
| 103 | break; |
|---|
| 104 | } |
|---|
| 105 | if(result.empty()) |
|---|
| 106 | result = std::string(1, char(0)); |
|---|
| 107 | return result; |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | enum |
|---|
| 111 | { |
|---|
| 112 | char_class_space=1<<0, |
|---|
| 113 | char_class_print=1<<1, |
|---|
| 114 | char_class_cntrl=1<<2, |
|---|
| 115 | char_class_upper=1<<3, |
|---|
| 116 | char_class_lower=1<<4, |
|---|
| 117 | char_class_alpha=1<<5, |
|---|
| 118 | char_class_digit=1<<6, |
|---|
| 119 | char_class_punct=1<<7, |
|---|
| 120 | char_class_xdigit=1<<8, |
|---|
| 121 | char_class_alnum=char_class_alpha|char_class_digit, |
|---|
| 122 | char_class_graph=char_class_alnum|char_class_punct, |
|---|
| 123 | char_class_blank=1<<9, |
|---|
| 124 | char_class_word=1<<10, |
|---|
| 125 | char_class_unicode=1<<11 |
|---|
| 126 | }; |
|---|
| 127 | |
|---|
| 128 | c_regex_traits<char>::char_class_type BOOST_REGEX_CALL c_regex_traits<char>::lookup_classname(const char* p1, const char* p2) |
|---|
| 129 | { |
|---|
| 130 | static const char_class_type masks[] = |
|---|
| 131 | { |
|---|
| 132 | 0, |
|---|
| 133 | char_class_alnum, |
|---|
| 134 | char_class_alpha, |
|---|
| 135 | char_class_blank, |
|---|
| 136 | char_class_cntrl, |
|---|
| 137 | char_class_digit, |
|---|
| 138 | char_class_digit, |
|---|
| 139 | char_class_graph, |
|---|
| 140 | char_class_lower, |
|---|
| 141 | char_class_lower, |
|---|
| 142 | char_class_print, |
|---|
| 143 | char_class_punct, |
|---|
| 144 | char_class_space, |
|---|
| 145 | char_class_space, |
|---|
| 146 | char_class_upper, |
|---|
| 147 | char_class_unicode, |
|---|
| 148 | char_class_upper, |
|---|
| 149 | char_class_alnum | char_class_word, |
|---|
| 150 | char_class_alnum | char_class_word, |
|---|
| 151 | char_class_xdigit, |
|---|
| 152 | }; |
|---|
| 153 | |
|---|
| 154 | int id = ::boost::re_detail::get_default_class_id(p1, p2); |
|---|
| 155 | if(id < 0) |
|---|
| 156 | { |
|---|
| 157 | std::string s(p1, p2); |
|---|
| 158 | for(std::string::size_type i = 0; i < s.size(); ++i) |
|---|
| 159 | s[i] = static_cast<char>((std::tolower)(static_cast<unsigned char>(s[i]))); |
|---|
| 160 | id = ::boost::re_detail::get_default_class_id(&*s.begin(), &*s.begin() + s.size()); |
|---|
| 161 | } |
|---|
| 162 | BOOST_ASSERT(std::size_t(id+1) < sizeof(masks) / sizeof(masks[0])); |
|---|
| 163 | return masks[id+1]; |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | bool BOOST_REGEX_CALL c_regex_traits<char>::isctype(char c, char_class_type mask) |
|---|
| 167 | { |
|---|
| 168 | return |
|---|
| 169 | ((mask & char_class_space) && (std::isspace)(static_cast<unsigned char>(c))) |
|---|
| 170 | || ((mask & char_class_print) && (std::isprint)(static_cast<unsigned char>(c))) |
|---|
| 171 | || ((mask & char_class_cntrl) && (std::iscntrl)(static_cast<unsigned char>(c))) |
|---|
| 172 | || ((mask & char_class_upper) && (std::isupper)(static_cast<unsigned char>(c))) |
|---|
| 173 | || ((mask & char_class_lower) && (std::islower)(static_cast<unsigned char>(c))) |
|---|
| 174 | || ((mask & char_class_alpha) && (std::isalpha)(static_cast<unsigned char>(c))) |
|---|
| 175 | || ((mask & char_class_digit) && (std::isdigit)(static_cast<unsigned char>(c))) |
|---|
| 176 | || ((mask & char_class_punct) && (std::ispunct)(static_cast<unsigned char>(c))) |
|---|
| 177 | || ((mask & char_class_xdigit) && (std::isxdigit)(static_cast<unsigned char>(c))) |
|---|
| 178 | || ((mask & char_class_blank) && (std::isspace)(static_cast<unsigned char>(c)) && !::boost::re_detail::is_separator(c)) |
|---|
| 179 | || ((mask & char_class_word) && (c == '_')); |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | c_regex_traits<char>::string_type BOOST_REGEX_CALL c_regex_traits<char>::lookup_collatename(const char* p1, const char* p2) |
|---|
| 183 | { |
|---|
| 184 | std::string s(p1, p2); |
|---|
| 185 | s = ::boost::re_detail::lookup_default_collate_name(s); |
|---|
| 186 | if(s.empty() && (p2-p1 == 1)) |
|---|
| 187 | s.append(1, *p1); |
|---|
| 188 | return s; |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | int BOOST_REGEX_CALL c_regex_traits<char>::value(char c, int radix) |
|---|
| 192 | { |
|---|
| 193 | char b[2] = { c, '\0', }; |
|---|
| 194 | char* ep; |
|---|
| 195 | int result = std::strtol(b, &ep, radix); |
|---|
| 196 | if(ep == b) |
|---|
| 197 | return -1; |
|---|
| 198 | return result; |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | } |
|---|
| 202 | #ifdef BOOST_HAS_ABI_HEADERS |
|---|
| 203 | # include BOOST_ABI_SUFFIX |
|---|
| 204 | #endif |
|---|
| 205 | |
|---|
| 206 | #endif |
|---|