1 // std::ctype implementation details, generic version -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 // Free Software Foundation, Inc.
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)
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.
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.
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/>.
27 // ISO C++ 14882: 22.2.1.1.2 ctype virtual functions.
30 // Written by Benjamin Kosnik <bkoz@redhat.com>
37 namespace std _GLIBCXX_VISIBILITY(default)
39 _GLIBCXX_BEGIN_NAMESPACE_VERSION
41 // NB: The other ctype<char> specializations are in src/locale.cc and
42 // various /config/os/* files.
43 ctype_byname<char>::ctype_byname(const char* __s, size_t __refs)
44 : ctype<char>(0, false, __refs)
46 if (std::strcmp(__s, "C") != 0 && std::strcmp(__s, "POSIX") != 0)
48 this->_S_destroy_c_locale(this->_M_c_locale_ctype);
49 this->_S_create_c_locale(this->_M_c_locale_ctype, __s);
53 ctype_byname<char>::~ctype_byname()
56 #ifdef _GLIBCXX_USE_WCHAR_T
57 ctype<wchar_t>::__wmask_type
58 ctype<wchar_t>::_M_convert_to_wmask(const mask __m) const throw()
64 __ret = wctype("space");
67 __ret = wctype("print");
70 __ret = wctype("cntrl");
73 __ret = wctype("upper");
76 __ret = wctype("lower");
79 __ret = wctype("alpha");
82 __ret = wctype("digit");
85 __ret = wctype("punct");
88 __ret = wctype("xdigit");
91 __ret = wctype("alnum");
94 __ret = wctype("graph");
97 __ret = __wmask_type();
103 ctype<wchar_t>::do_toupper(wchar_t __c) const
104 { return towupper(__c); }
107 ctype<wchar_t>::do_toupper(wchar_t* __lo, const wchar_t* __hi) const
111 *__lo = towupper(*__lo);
118 ctype<wchar_t>::do_tolower(wchar_t __c) const
119 { return towlower(__c); }
122 ctype<wchar_t>::do_tolower(wchar_t* __lo, const wchar_t* __hi) const
126 *__lo = towlower(*__lo);
134 do_is(mask __m, char_type __c) const
137 // Generically, 15 (instead of 10) since we don't know the numerical
138 // encoding of the various categories in /usr/include/ctype.h.
139 const size_t __bitmasksize = 15;
140 for (size_t __bitcur = 0; __bitcur <= __bitmasksize; ++__bitcur)
141 if (__m & _M_bit[__bitcur]
142 && iswctype(__c, _M_wmask[__bitcur]))
152 do_is(const wchar_t* __lo, const wchar_t* __hi, mask* __vec) const
154 for (;__lo < __hi; ++__vec, ++__lo)
156 // Generically, 15 (instead of 10) since we don't know the numerical
157 // encoding of the various categories in /usr/include/ctype.h.
158 const size_t __bitmasksize = 15;
160 for (size_t __bitcur = 0; __bitcur <= __bitmasksize; ++__bitcur)
161 if (iswctype(*__lo, _M_wmask[__bitcur]))
162 __m |= _M_bit[__bitcur];
170 do_scan_is(mask __m, const wchar_t* __lo, const wchar_t* __hi) const
172 while (__lo < __hi && !this->do_is(__m, *__lo))
179 do_scan_not(mask __m, const char_type* __lo, const char_type* __hi) const
181 while (__lo < __hi && this->do_is(__m, *__lo) != 0)
188 do_widen(char __c) const
189 { return _M_widen[static_cast<unsigned char>(__c)]; }
193 do_widen(const char* __lo, const char* __hi, wchar_t* __dest) const
197 *__dest = _M_widen[static_cast<unsigned char>(*__lo)];
206 do_narrow(wchar_t __wc, char __dfault) const
208 if (__wc >= 0 && __wc < 128 && _M_narrow_ok)
209 return _M_narrow[__wc];
210 const int __c = wctob(__wc);
211 return (__c == EOF ? __dfault : static_cast<char>(__c));
216 do_narrow(const wchar_t* __lo, const wchar_t* __hi, char __dfault,
222 if (*__lo >= 0 && *__lo < 128)
223 *__dest = _M_narrow[*__lo];
226 const int __c = wctob(*__lo);
227 *__dest = (__c == EOF ? __dfault : static_cast<char>(__c));
235 const int __c = wctob(*__lo);
236 *__dest = (__c == EOF ? __dfault : static_cast<char>(__c));
244 ctype<wchar_t>::_M_initialize_ctype() throw()
247 for (__i = 0; __i < 128; ++__i)
249 const int __c = wctob(__i);
253 _M_narrow[__i] = static_cast<char>(__c);
258 _M_narrow_ok = false;
260 __i < sizeof(_M_widen) / sizeof(wint_t); ++__i)
261 _M_widen[__i] = btowc(__i);
263 for (size_t __i = 0; __i <= 15; ++__i)
265 _M_bit[__i] = static_cast<mask>(1 << __i);
266 _M_wmask[__i] = _M_convert_to_wmask(_M_bit[__i]);
269 #endif // _GLIBCXX_USE_WCHAR_T
271 _GLIBCXX_END_NAMESPACE_VERSION