1 // std::ctype implementation details, GNU version -*- C++ -*- 2 3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 4 // Free Software Foundation, Inc. 5 // 6 // This file is part of the GNU ISO C++ Library. This library is free 7 // software; you can redistribute it and/or modify it under the 8 // terms of the GNU General Public License as published by the 9 // Free Software Foundation; either version 3, or (at your option) 10 // any later version. 11 12 // This library is distributed in the hope that it will be useful, 13 // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 // GNU General Public License for more details. 16 17 // Under Section 7 of GPL version 3, you are granted additional 18 // permissions described in the GCC Runtime Library Exception, version 19 // 3.1, as published by the Free Software Foundation. 20 21 // You should have received a copy of the GNU General Public License and 22 // a copy of the GCC Runtime Library Exception along with this program; 23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 24 // <http://www.gnu.org/licenses/>. 25 26 // 27 // ISO C++ 14882: 22.2.1.1.2 ctype virtual functions. 28 // 29 30 // Written by Benjamin Kosnik <bkoz@redhat.com> 31 32 #include <locale> 33 #include <bits/c++locale_internal.h> 34 #include <cstdlib> 35 #include <cstring> 36 #include <cstdio> 37 38 namespace std _GLIBCXX_VISIBILITY(default) 39 { 40 // NB: The other ctype<char> specializations are in src/locale.cc and 41 // various /config/os/* files. 42 43 ctype_byname<char>::ctype_byname(const char* __s, size_t __refs) 44 : ctype<char>(0, false, __refs) 45 { 46 if (std::strcmp(__s, "C") != 0 && std::strcmp(__s, "POSIX") != 0) 47 { 48 this->_S_destroy_c_locale(this->_M_c_locale_ctype); 49 this->_S_create_c_locale(this->_M_c_locale_ctype, __s); 50 } 51 } 52 53 ctype_byname<char>::~ctype_byname() 54 { } 55 56 #ifdef _GLIBCXX_USE_WCHAR_T 57 ctype<wchar_t>::__wmask_type 58 ctype<wchar_t>::_M_convert_to_wmask( 59 const mask __attribute__((__unused__)) __m) const throw() 60 { 61 // DragonFly uses the same codes for 'char' as 'wchar_t', so this routine 62 // never gets called. 63 return __wmask_type(); 64 }; 65 66 wchar_t 67 ctype<wchar_t>::do_toupper(wchar_t __c) const 68 { return towupper(__c); } 69 70 const wchar_t* 71 ctype<wchar_t>::do_toupper(wchar_t* __lo, const wchar_t* __hi) const 72 { 73 while (__lo < __hi) 74 { 75 *__lo = towupper(*__lo); 76 ++__lo; 77 } 78 return __hi; 79 } 80 81 wchar_t 82 ctype<wchar_t>::do_tolower(wchar_t __c) const 83 { return towlower(__c); } 84 85 const wchar_t* 86 ctype<wchar_t>::do_tolower(wchar_t* __lo, const wchar_t* __hi) const 87 { 88 while (__lo < __hi) 89 { 90 *__lo = towlower(*__lo); 91 ++__lo; 92 } 93 return __hi; 94 } 95 96 wchar_t 97 ctype<wchar_t>:: 98 do_widen(char __c) const 99 { return _M_widen[static_cast<unsigned char>(__c)]; } 100 101 const char* 102 ctype<wchar_t>:: 103 do_widen(const char* __lo, const char* __hi, wchar_t* __dest) const 104 { 105 while (__lo < __hi) 106 { 107 *__dest = _M_widen[static_cast<unsigned char>(*__lo)]; 108 ++__lo; 109 ++__dest; 110 } 111 return __hi; 112 } 113 114 char 115 ctype<wchar_t>:: 116 do_narrow(wchar_t __wc, char __dfault) const 117 { 118 if (__wc >= 0 && __wc < 128 && _M_narrow_ok) 119 return _M_narrow[__wc]; 120 const int __c = wctob(__wc); 121 return (__c == EOF ? __dfault : static_cast<char>(__c)); 122 } 123 124 const wchar_t* 125 ctype<wchar_t>:: 126 do_narrow(const wchar_t* __lo, const wchar_t* __hi, char __dfault, 127 char* __dest) const 128 { 129 if (_M_narrow_ok) 130 while (__lo < __hi) 131 { 132 if (*__lo >= 0 && *__lo < 128) 133 *__dest = _M_narrow[*__lo]; 134 else 135 { 136 const int __c = wctob(*__lo); 137 *__dest = (__c == EOF ? __dfault : static_cast<char>(__c)); 138 } 139 ++__lo; 140 ++__dest; 141 } 142 else 143 while (__lo < __hi) 144 { 145 const int __c = wctob(*__lo); 146 *__dest = (__c == EOF ? __dfault : static_cast<char>(__c)); 147 ++__lo; 148 ++__dest; 149 } 150 return __hi; 151 } 152 153 void 154 ctype<wchar_t>::_M_initialize_ctype() throw() 155 { 156 wint_t __i; 157 for (__i = 0; __i < 128; ++__i) 158 { 159 const int __c = wctob(__i); 160 if (__c == EOF) 161 break; 162 else 163 _M_narrow[__i] = static_cast<char>(__c); 164 } 165 if (__i == 128) 166 _M_narrow_ok = true; 167 else 168 _M_narrow_ok = false; 169 for (size_t __i = 0; 170 __i < sizeof(_M_widen) / sizeof(wint_t); ++__i) 171 _M_widen[__i] = btowc(__i); 172 } 173 #endif // _GLIBCXX_USE_WCHAR_T 174 } 175