1 // std::ctype implementation details, generic version -*- C++ -*-
3 // Copyright (C) 2001-2013 Free Software Foundation, Inc.
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)
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.
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.
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/>.
26 // ISO C++ 14882: 22.2.1.1.2 ctype virtual functions.
29 // Written by Benjamin Kosnik <bkoz@redhat.com>
36 namespace std _GLIBCXX_VISIBILITY(default)
38 _GLIBCXX_BEGIN_NAMESPACE_VERSION
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)
45 if (std::strcmp(__s, "C") != 0 && std::strcmp(__s, "POSIX") != 0)
47 this->_S_destroy_c_locale(this->_M_c_locale_ctype);
48 this->_S_create_c_locale(this->_M_c_locale_ctype, __s);
52 ctype_byname<char>::~ctype_byname()
55 #ifdef _GLIBCXX_USE_WCHAR_T
56 ctype<wchar_t>::__wmask_type
57 ctype<wchar_t>::_M_convert_to_wmask(const mask __m) const throw()
63 __ret = wctype("space");
66 __ret = wctype("print");
69 __ret = wctype("cntrl");
72 __ret = wctype("upper");
75 __ret = wctype("lower");
78 __ret = wctype("alpha");
81 __ret = wctype("digit");
84 __ret = wctype("punct");
87 __ret = wctype("xdigit");
90 __ret = wctype("alnum");
93 __ret = wctype("graph");
96 __ret = __wmask_type();
102 ctype<wchar_t>::do_toupper(wchar_t __c) const
103 { return towupper(__c); }
106 ctype<wchar_t>::do_toupper(wchar_t* __lo, const wchar_t* __hi) const
110 *__lo = towupper(*__lo);
117 ctype<wchar_t>::do_tolower(wchar_t __c) const
118 { return towlower(__c); }
121 ctype<wchar_t>::do_tolower(wchar_t* __lo, const wchar_t* __hi) const
125 *__lo = towlower(*__lo);
133 do_is(mask __m, char_type __c) const
136 // Generically, 15 (instead of 10) since we don't know the numerical
137 // encoding of the various categories in /usr/include/ctype.h.
138 const size_t __bitmasksize = 15;
139 for (size_t __bitcur = 0; __bitcur <= __bitmasksize; ++__bitcur)
140 if (__m & _M_bit[__bitcur]
141 && iswctype(__c, _M_wmask[__bitcur]))
151 do_is(const wchar_t* __lo, const wchar_t* __hi, mask* __vec) const
153 for (;__lo < __hi; ++__vec, ++__lo)
155 // Generically, 15 (instead of 10) since we don't know the numerical
156 // encoding of the various categories in /usr/include/ctype.h.
157 const size_t __bitmasksize = 15;
159 for (size_t __bitcur = 0; __bitcur <= __bitmasksize; ++__bitcur)
160 if (iswctype(*__lo, _M_wmask[__bitcur]))
161 __m |= _M_bit[__bitcur];
169 do_scan_is(mask __m, const wchar_t* __lo, const wchar_t* __hi) const
171 while (__lo < __hi && !this->do_is(__m, *__lo))
178 do_scan_not(mask __m, const char_type* __lo, const char_type* __hi) const
180 while (__lo < __hi && this->do_is(__m, *__lo) != 0)
187 do_widen(char __c) const
188 { return _M_widen[static_cast<unsigned char>(__c)]; }
192 do_widen(const char* __lo, const char* __hi, wchar_t* __dest) const
196 *__dest = _M_widen[static_cast<unsigned char>(*__lo)];
205 do_narrow(wchar_t __wc, char __dfault) const
207 if (__wc >= 0 && __wc < 128 && _M_narrow_ok)
208 return _M_narrow[__wc];
209 const int __c = wctob(__wc);
210 return (__c == EOF ? __dfault : static_cast<char>(__c));
215 do_narrow(const wchar_t* __lo, const wchar_t* __hi, char __dfault,
221 if (*__lo >= 0 && *__lo < 128)
222 *__dest = _M_narrow[*__lo];
225 const int __c = wctob(*__lo);
226 *__dest = (__c == EOF ? __dfault : static_cast<char>(__c));
234 const int __c = wctob(*__lo);
235 *__dest = (__c == EOF ? __dfault : static_cast<char>(__c));
243 ctype<wchar_t>::_M_initialize_ctype() throw()
246 for (__i = 0; __i < 128; ++__i)
248 const int __c = wctob(__i);
252 _M_narrow[__i] = static_cast<char>(__c);
257 _M_narrow_ok = false;
259 __i < sizeof(_M_widen) / sizeof(wint_t); ++__i)
260 _M_widen[__i] = btowc(__i);
262 for (size_t __i = 0; __i <= 15; ++__i)
264 _M_bit[__i] = static_cast<mask>(1 << __i);
265 _M_wmask[__i] = _M_convert_to_wmask(_M_bit[__i]);
268 #endif // _GLIBCXX_USE_WCHAR_T
270 _GLIBCXX_END_NAMESPACE_VERSION