1 // std::ctype implementation details, DragonFly version -*- C++ -*- 2 3 // Copyright (C) 2014-2018 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 <bkoz@redhat.com> 30 // Modified for DragonFly by John Marino <gnugcc@marino.st> 31 32 #include <locale> 33 #include <cstring> 34 #include <cstdio> 35 36 #ifndef _ISbit 37 #define _ISbit(bit) ((bit) < 8 ? ((1 << (bit)) << 8) : ((1 << (bit)) >> 8)) 38 #endif 39 40 namespace std _GLIBCXX_VISIBILITY(default) 41 { 42 _GLIBCXX_BEGIN_NAMESPACE_VERSION 43 44 // NB: The other ctype<char> specializations are in src/locale.cc and 45 // various /config/os/* files. 46 ctype_byname<char>::ctype_byname(const char* __s, size_t __refs) 47 : ctype<char>(0, false, __refs) 48 { 49 if (std::strcmp(__s, "C") != 0 && std::strcmp(__s, "POSIX") != 0) 50 { 51 this->_S_destroy_c_locale(this->_M_c_locale_ctype); 52 this->_S_create_c_locale(this->_M_c_locale_ctype, __s); 53 } 54 } 55 56 ctype_byname<char>::~ctype_byname() 57 { } 58 59 #ifdef _GLIBCXX_USE_WCHAR_T 60 ctype<wchar_t>::__wmask_type 61 ctype<wchar_t>::_M_convert_to_wmask(const mask __m) const throw() 62 { 63 __wmask_type __ret; 64 switch (__m) 65 { 66 case space: 67 __ret = wctype_l("space", (locale_t)_M_c_locale_ctype); 68 break; 69 case print: 70 __ret = wctype_l("print", (locale_t)_M_c_locale_ctype); 71 break; 72 case cntrl: 73 __ret = wctype_l("cntrl", (locale_t)_M_c_locale_ctype); 74 break; 75 case upper: 76 __ret = wctype_l("upper", (locale_t)_M_c_locale_ctype); 77 break; 78 case lower: 79 __ret = wctype_l("lower", (locale_t)_M_c_locale_ctype); 80 break; 81 case alpha: 82 __ret = wctype_l("alpha", (locale_t)_M_c_locale_ctype); 83 break; 84 case digit: 85 __ret = wctype_l("digit", (locale_t)_M_c_locale_ctype); 86 break; 87 case punct: 88 __ret = wctype_l("punct", (locale_t)_M_c_locale_ctype); 89 break; 90 case xdigit: 91 __ret = wctype_l("xdigit", (locale_t)_M_c_locale_ctype); 92 break; 93 case alnum: 94 __ret = wctype_l("alnum", (locale_t)_M_c_locale_ctype); 95 break; 96 case graph: 97 __ret = wctype_l("graph", (locale_t)_M_c_locale_ctype); 98 break; 99 case blank: 100 __ret = wctype_l("blank", (locale_t)_M_c_locale_ctype); 101 break; 102 default: 103 __ret = __wmask_type(); 104 } 105 return __ret; 106 } 107 108 wchar_t 109 ctype<wchar_t>::do_toupper(wchar_t __c) const 110 { return towupper_l(__c, (locale_t)_M_c_locale_ctype); } 111 112 const wchar_t* 113 ctype<wchar_t>::do_toupper(wchar_t* __lo, const wchar_t* __hi) const 114 { 115 while (__lo < __hi) 116 { 117 *__lo = towupper_l(*__lo, (locale_t)_M_c_locale_ctype); 118 ++__lo; 119 } 120 return __hi; 121 } 122 123 wchar_t 124 ctype<wchar_t>::do_tolower(wchar_t __c) const 125 { return towlower_l(__c, (locale_t)_M_c_locale_ctype); } 126 127 const wchar_t* 128 ctype<wchar_t>::do_tolower(wchar_t* __lo, const wchar_t* __hi) const 129 { 130 while (__lo < __hi) 131 { 132 *__lo = towlower_l(*__lo, (locale_t)_M_c_locale_ctype); 133 ++__lo; 134 } 135 return __hi; 136 } 137 138 wchar_t 139 ctype<wchar_t>:: 140 do_widen(char __c) const 141 { return _M_widen[static_cast<unsigned char>(__c)]; } 142 143 const char* 144 ctype<wchar_t>:: 145 do_widen(const char* __lo, const char* __hi, wchar_t* __dest) const 146 { 147 while (__lo < __hi) 148 { 149 *__dest = _M_widen[static_cast<unsigned char>(*__lo)]; 150 ++__lo; 151 ++__dest; 152 } 153 return __hi; 154 } 155 156 char 157 ctype<wchar_t>:: 158 do_narrow(wchar_t __wc, char __dfault) const 159 { 160 if (__wc >= 0 && __wc < 128 && _M_narrow_ok) 161 return _M_narrow[__wc]; 162 __c_locale __old = (__c_locale)uselocale((locale_t)_M_c_locale_ctype); 163 const int __c = wctob(__wc); 164 uselocale((locale_t)__old); 165 return (__c == EOF ? __dfault : static_cast<char>(__c)); 166 } 167 168 const wchar_t* 169 ctype<wchar_t>:: 170 do_narrow(const wchar_t* __lo, const wchar_t* __hi, char __dfault, 171 char* __dest) const 172 { 173 __c_locale __old = (__c_locale)uselocale((locale_t)_M_c_locale_ctype); 174 if (_M_narrow_ok) 175 while (__lo < __hi) 176 { 177 if (*__lo >= 0 && *__lo < 128) 178 *__dest = _M_narrow[*__lo]; 179 else 180 { 181 const int __c = wctob(*__lo); 182 *__dest = (__c == EOF ? __dfault : static_cast<char>(__c)); 183 } 184 ++__lo; 185 ++__dest; 186 } 187 else 188 while (__lo < __hi) 189 { 190 const int __c = wctob(*__lo); 191 *__dest = (__c == EOF ? __dfault : static_cast<char>(__c)); 192 ++__lo; 193 ++__dest; 194 } 195 uselocale((locale_t)__old); 196 return __hi; 197 } 198 199 void 200 ctype<wchar_t>::_M_initialize_ctype() throw() 201 { 202 __c_locale __old = (__c_locale)uselocale((locale_t)_M_c_locale_ctype); 203 wint_t __i; 204 for (__i = 0; __i < 128; ++__i) 205 { 206 const int __c = wctob(__i); 207 if (__c == EOF) 208 break; 209 else 210 _M_narrow[__i] = static_cast<char>(__c); 211 } 212 if (__i == 128) 213 _M_narrow_ok = true; 214 else 215 _M_narrow_ok = false; 216 for (size_t __j = 0; 217 __j < sizeof(_M_widen) / sizeof(wint_t); ++__j) 218 _M_widen[__j] = btowc(__j); 219 220 for (size_t __k = 0; __k <= 11; ++__k) 221 { 222 _M_bit[__k] = static_cast<mask>(_ISbit(__k)); 223 _M_wmask[__k] = _M_convert_to_wmask(_M_bit[__k]); 224 } 225 uselocale((locale_t)__old); 226 } 227 #endif // _GLIBCXX_USE_WCHAR_T 228 229 _GLIBCXX_END_NAMESPACE_VERSION 230 } // namespace 231