1 // std::ctype implementation details, generic version -*- C++ -*-
3 // Copyright (C) 2001-2015 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 // For some targets ctype_base::blank == ctype_base::space so check
97 // here to avoid a duplicate case error.
99 __ret = wctype("blank");
101 __ret = __wmask_type();
107 ctype<wchar_t>::do_toupper(wchar_t __c) const
108 { return towupper(__c); }
111 ctype<wchar_t>::do_toupper(wchar_t* __lo, const wchar_t* __hi) const
115 *__lo = towupper(*__lo);
122 ctype<wchar_t>::do_tolower(wchar_t __c) const
123 { return towlower(__c); }
126 ctype<wchar_t>::do_tolower(wchar_t* __lo, const wchar_t* __hi) const
130 *__lo = towlower(*__lo);
138 do_is(mask __m, char_type __c) const
141 // Generically, 15 (instead of 11) since we don't know the numerical
142 // encoding of the various categories in /usr/include/ctype.h.
143 const size_t __bitmasksize = 15;
144 for (size_t __bitcur = 0; __bitcur <= __bitmasksize; ++__bitcur)
145 if (__m & _M_bit[__bitcur]
146 && iswctype(__c, _M_wmask[__bitcur]))
156 do_is(const wchar_t* __lo, const wchar_t* __hi, mask* __vec) const
158 for (;__lo < __hi; ++__vec, ++__lo)
160 // Generically, 15 (instead of 11) since we don't know the numerical
161 // encoding of the various categories in /usr/include/ctype.h.
162 const size_t __bitmasksize = 15;
164 for (size_t __bitcur = 0; __bitcur <= __bitmasksize; ++__bitcur)
165 if (iswctype(*__lo, _M_wmask[__bitcur]))
166 __m |= _M_bit[__bitcur];
174 do_scan_is(mask __m, const wchar_t* __lo, const wchar_t* __hi) const
176 while (__lo < __hi && !this->do_is(__m, *__lo))
183 do_scan_not(mask __m, const char_type* __lo, const char_type* __hi) const
185 while (__lo < __hi && this->do_is(__m, *__lo) != 0)
192 do_widen(char __c) const
193 { return _M_widen[static_cast<unsigned char>(__c)]; }
197 do_widen(const char* __lo, const char* __hi, wchar_t* __dest) const
201 *__dest = _M_widen[static_cast<unsigned char>(*__lo)];
210 do_narrow(wchar_t __wc, char __dfault) const
212 if (__wc >= 0 && __wc < 128 && _M_narrow_ok)
213 return _M_narrow[__wc];
214 const int __c = wctob(__wc);
215 return (__c == EOF ? __dfault : static_cast<char>(__c));
220 do_narrow(const wchar_t* __lo, const wchar_t* __hi, char __dfault,
226 if (*__lo >= 0 && *__lo < 128)
227 *__dest = _M_narrow[*__lo];
230 const int __c = wctob(*__lo);
231 *__dest = (__c == EOF ? __dfault : static_cast<char>(__c));
239 const int __c = wctob(*__lo);
240 *__dest = (__c == EOF ? __dfault : static_cast<char>(__c));
248 ctype<wchar_t>::_M_initialize_ctype() throw()
251 for (__i = 0; __i < 128; ++__i)
253 const int __c = wctob(__i);
257 _M_narrow[__i] = static_cast<char>(__c);
262 _M_narrow_ok = false;
264 __i < sizeof(_M_widen) / sizeof(wint_t); ++__i)
265 _M_widen[__i] = btowc(__i);
267 for (size_t __i = 0; __i <= 15; ++__i)
269 _M_bit[__i] = static_cast<mask>(1 << __i);
270 _M_wmask[__i] = _M_convert_to_wmask(_M_bit[__i]);
273 #endif // _GLIBCXX_USE_WCHAR_T
275 _GLIBCXX_END_NAMESPACE_VERSION