| 1 | // std::ctype implementation details, generic version -*- C++ -*- |
| 2 | |
| 3 | // Copyright (C) 2001-2025 Free Software Foundation, Inc. |
| 4 | // |
| 5 | // This file is part of the GNU ISO C++ Library. This library is free |
| 6 | // software; you can redistribute it and/or modify it under the |
| 7 | // terms of the GNU General Public License as published by the |
| 8 | // Free Software Foundation; either version 3, or (at your option) |
| 9 | // any later version. |
| 10 | |
| 11 | // This library is distributed in the hope that it will be useful, |
| 12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | // GNU General Public License for more details. |
| 15 | |
| 16 | // Under Section 7 of GPL version 3, you are granted additional |
| 17 | // permissions described in the GCC Runtime Library Exception, version |
| 18 | // 3.1, as published by the Free Software Foundation. |
| 19 | |
| 20 | // You should have received a copy of the GNU General Public License and |
| 21 | // a copy of the GCC Runtime Library Exception along with this program; |
| 22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 23 | // <http://www.gnu.org/licenses/>. |
| 24 | |
| 25 | // |
| 26 | // ISO C++ 14882: 22.2.1.1.2 ctype virtual functions. |
| 27 | // |
| 28 | |
| 29 | // Written by Benjamin Kosnik <[email protected]> |
| 30 | |
| 31 | #include <locale> |
| 32 | #include <cstdlib> |
| 33 | #include <cstring> |
| 34 | #include <cstdio> |
| 35 | |
| 36 | namespace std _GLIBCXX_VISIBILITY(default) |
| 37 | { |
| 38 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
| 39 | |
| 40 | // NB: The other ctype<char> specializations are in src/locale.cc and |
| 41 | // various /config/os/* files. |
| 42 | ctype_byname<char>::ctype_byname(const char* __s, size_t __refs) |
| 43 | : ctype<char>(0, false, __refs) |
| 44 | { |
| 45 | if (std::strcmp(__s, "C") != 0 && std::strcmp(__s, "POSIX") != 0) |
| 46 | { |
| 47 | this->_S_destroy_c_locale(this->_M_c_locale_ctype); |
| 48 | this->_S_create_c_locale(this->_M_c_locale_ctype, __s); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | ctype_byname<char>::~ctype_byname() |
| 53 | { } |
| 54 | |
| 55 | #ifdef _GLIBCXX_USE_WCHAR_T |
| 56 | |
| 57 | // Values which index into _M_bit and _M_wmask for each classifier |
| 58 | enum { |
| 59 | _id_alnum, _id_alpha, _id_blank, _id_cntrl, |
| 60 | _id_digit, _id_graph, _id_lower, _id_print, |
| 61 | _id_punct, _id_space, _id_upper, _id_xdigit, |
| 62 | _id_num, |
| 63 | }; |
| 64 | |
| 65 | ctype<wchar_t>::__wmask_type |
| 66 | ctype<wchar_t>::_M_convert_to_wmask(const mask __m) const throw() |
| 67 | { |
| 68 | __wmask_type __ret; |
| 69 | switch (__m) |
| 70 | { |
| 71 | case space: |
| 72 | __ret = wctype("space"); |
| 73 | break; |
| 74 | case print: |
| 75 | __ret = wctype("print"); |
| 76 | break; |
| 77 | case cntrl: |
| 78 | __ret = wctype("cntrl"); |
| 79 | break; |
| 80 | case upper: |
| 81 | __ret = wctype("upper"); |
| 82 | break; |
| 83 | case lower: |
| 84 | __ret = wctype("lower"); |
| 85 | break; |
| 86 | case alpha: |
| 87 | __ret = wctype("alpha"); |
| 88 | break; |
| 89 | case digit: |
| 90 | __ret = wctype("digit"); |
| 91 | break; |
| 92 | case punct: |
| 93 | __ret = wctype("punct"); |
| 94 | break; |
| 95 | case xdigit: |
| 96 | __ret = wctype("xdigit"); |
| 97 | break; |
| 98 | case alnum: |
| 99 | __ret = wctype("alnum"); |
| 100 | break; |
| 101 | case graph: |
| 102 | __ret = wctype("graph"); |
| 103 | break; |
| 104 | default: |
| 105 | // For some targets ctype_base::blank == ctype_base::space so check |
| 106 | // here to avoid a duplicate case error. |
| 107 | if (__m == blank) |
| 108 | __ret = wctype("blank"); |
| 109 | else |
| 110 | __ret = __wmask_type(); |
| 111 | } |
| 112 | return __ret; |
| 113 | }; |
| 114 | |
| 115 | wchar_t |
| 116 | ctype<wchar_t>::do_toupper(wchar_t __c) const |
| 117 | { return towupper(__c); } |
| 118 | |
| 119 | const wchar_t* |
| 120 | ctype<wchar_t>::do_toupper(wchar_t* __lo, const wchar_t* __hi) const |
| 121 | { |
| 122 | while (__lo < __hi) |
| 123 | { |
| 124 | *__lo = towupper(*__lo); |
| 125 | ++__lo; |
| 126 | } |
| 127 | return __hi; |
| 128 | } |
| 129 | |
| 130 | wchar_t |
| 131 | ctype<wchar_t>::do_tolower(wchar_t __c) const |
| 132 | { return towlower(__c); } |
| 133 | |
| 134 | const wchar_t* |
| 135 | ctype<wchar_t>::do_tolower(wchar_t* __lo, const wchar_t* __hi) const |
| 136 | { |
| 137 | while (__lo < __hi) |
| 138 | { |
| 139 | *__lo = towlower(*__lo); |
| 140 | ++__lo; |
| 141 | } |
| 142 | return __hi; |
| 143 | } |
| 144 | |
| 145 | bool |
| 146 | ctype<wchar_t>:: |
| 147 | do_is(mask __m, char_type __c) const |
| 148 | { |
| 149 | bool __ret = false; |
| 150 | |
| 151 | // Each _M_bit may have more than one bit set. Only check tests for which |
| 152 | // all of the matching _M_bit values are set. This skips tests with some |
| 153 | // bits which are not set. For example: |
| 154 | // |
| 155 | // alpha = lower|upper |
| 156 | // __c = 'a' |
| 157 | // __m = upper |
| 158 | // |
| 159 | // We evaluate iswupper (which returns false), but we skip iswalpha (which |
| 160 | // would return true) because __m is missing the lower bit, so the return |
| 161 | // value is false. |
| 162 | for (size_t __bitcur = 0; __bitcur <= _id_num; ++__bitcur) |
| 163 | if ((__m & _M_bit[__bitcur]) == _M_bit[__bitcur] |
| 164 | && iswctype(__c, _M_wmask[__bitcur])) |
| 165 | { |
| 166 | __ret = true; |
| 167 | break; |
| 168 | } |
| 169 | return __ret; |
| 170 | } |
| 171 | |
| 172 | const wchar_t* |
| 173 | ctype<wchar_t>:: |
| 174 | do_is(const wchar_t* __lo, const wchar_t* __hi, mask* __vec) const |
| 175 | { |
| 176 | for (;__lo < __hi; ++__vec, ++__lo) |
| 177 | { |
| 178 | |
| 179 | // Start with all bits set. Iterate over all wctypes, clearing bits for |
| 180 | // non-matching values. For example: |
| 181 | // |
| 182 | // alpha == lower|upper |
| 183 | // *__lo == 'a' |
| 184 | // |
| 185 | // __m = <all bits> |
| 186 | // iswalpha and iswlower match |
| 187 | // __m left alone |
| 188 | // iswupper does not match |
| 189 | // __m &= ~upper |
| 190 | // |
| 191 | // The return value will satisfy the following conditions: |
| 192 | // |
| 193 | // (__m & alpha) != 0 |
| 194 | // (__m & lower) != 0 |
| 195 | // (__m & upper) == 0 |
| 196 | mask __m = (alnum|alpha|blank|cntrl|digit|graph| |
| 197 | lower|print|punct|space|upper|xdigit); |
| 198 | for (size_t __bitcur = 0; __bitcur < _id_num; ++__bitcur) |
| 199 | if (!iswctype(*__lo, _M_wmask[__bitcur])) |
| 200 | __m &= ~_M_bit[__bitcur]; |
| 201 | *__vec = __m; |
| 202 | } |
| 203 | return __hi; |
| 204 | } |
| 205 | |
| 206 | const wchar_t* |
| 207 | ctype<wchar_t>:: |
| 208 | do_scan_is(mask __m, const wchar_t* __lo, const wchar_t* __hi) const |
| 209 | { |
| 210 | while (__lo < __hi && !this->do_is(__m, *__lo)) |
| 211 | ++__lo; |
| 212 | return __lo; |
| 213 | } |
| 214 | |
| 215 | const wchar_t* |
| 216 | ctype<wchar_t>:: |
| 217 | do_scan_not(mask __m, const char_type* __lo, const char_type* __hi) const |
| 218 | { |
| 219 | while (__lo < __hi && this->do_is(__m, *__lo) != 0) |
| 220 | ++__lo; |
| 221 | return __lo; |
| 222 | } |
| 223 | |
| 224 | wchar_t |
| 225 | ctype<wchar_t>:: |
| 226 | do_widen(char __c) const |
| 227 | { return _M_widen[static_cast<unsigned char>(__c)]; } |
| 228 | |
| 229 | const char* |
| 230 | ctype<wchar_t>:: |
| 231 | do_widen(const char* __lo, const char* __hi, wchar_t* __dest) const |
| 232 | { |
| 233 | while (__lo < __hi) |
| 234 | { |
| 235 | *__dest = _M_widen[static_cast<unsigned char>(*__lo)]; |
| 236 | ++__lo; |
| 237 | ++__dest; |
| 238 | } |
| 239 | return __hi; |
| 240 | } |
| 241 | |
| 242 | char |
| 243 | ctype<wchar_t>:: |
| 244 | do_narrow(wchar_t __wc, char __dfault) const |
| 245 | { |
| 246 | if (__wc >= 0 && __wc < 128 && _M_narrow_ok) |
| 247 | return _M_narrow[__wc]; |
| 248 | const int __c = wctob(__wc); |
| 249 | return (__c == EOF ? __dfault : static_cast<char>(__c)); |
| 250 | } |
| 251 | |
| 252 | const wchar_t* |
| 253 | ctype<wchar_t>:: |
| 254 | do_narrow(const wchar_t* __lo, const wchar_t* __hi, char __dfault, |
| 255 | char* __dest) const |
| 256 | { |
| 257 | if (_M_narrow_ok) |
| 258 | while (__lo < __hi) |
| 259 | { |
| 260 | if (*__lo >= 0 && *__lo < 128) |
| 261 | *__dest = _M_narrow[*__lo]; |
| 262 | else |
| 263 | { |
| 264 | const int __c = wctob(*__lo); |
| 265 | *__dest = (__c == EOF ? __dfault : static_cast<char>(__c)); |
| 266 | } |
| 267 | ++__lo; |
| 268 | ++__dest; |
| 269 | } |
| 270 | else |
| 271 | while (__lo < __hi) |
| 272 | { |
| 273 | const int __c = wctob(*__lo); |
| 274 | *__dest = (__c == EOF ? __dfault : static_cast<char>(__c)); |
| 275 | ++__lo; |
| 276 | ++__dest; |
| 277 | } |
| 278 | return __hi; |
| 279 | } |
| 280 | |
| 281 | void |
| 282 | ctype<wchar_t>::_M_initialize_ctype() throw() |
| 283 | { |
| 284 | wint_t __i; |
| 285 | for (__i = 0; __i < 128; ++__i) |
| 286 | { |
| 287 | const int __c = wctob(__i); |
| 288 | if (__c == EOF) |
| 289 | break; |
| 290 | else |
| 291 | _M_narrow[__i] = static_cast<char>(__c); |
| 292 | } |
| 293 | if (__i == 128) |
| 294 | _M_narrow_ok = true; |
| 295 | else |
| 296 | _M_narrow_ok = false; |
| 297 | for (size_t __i = 0; |
| 298 | __i < sizeof(_M_widen) / sizeof(wint_t); ++__i) |
| 299 | _M_widen[__i] = btowc(__i); |
| 300 | |
| 301 | // Set these directly from the OS provided values. |
| 302 | // We can't simply iterate over bit values because |
| 303 | // they may have more than one bit set |
| 304 | _M_bit[_id_alnum] = alnum; |
| 305 | _M_bit[_id_alpha] = alpha; |
| 306 | _M_bit[_id_blank] = blank; |
| 307 | _M_bit[_id_cntrl] = cntrl; |
| 308 | _M_bit[_id_digit] = digit; |
| 309 | _M_bit[_id_graph] = graph; |
| 310 | _M_bit[_id_lower] = lower; |
| 311 | _M_bit[_id_print] = print; |
| 312 | _M_bit[_id_punct] = punct; |
| 313 | _M_bit[_id_space] = space; |
| 314 | _M_bit[_id_upper] = upper; |
| 315 | _M_bit[_id_xdigit] = xdigit; |
| 316 | |
| 317 | for (size_t __i = 0; __i <= _id_num; __i++) |
| 318 | _M_wmask[__i] = _M_convert_to_wmask(_M_bit[__i]); |
| 319 | } |
| 320 | #endif // _GLIBCXX_USE_WCHAR_T |
| 321 | |
| 322 | _GLIBCXX_END_NAMESPACE_VERSION |
| 323 | } // namespace |