1 // std::ctype implementation details, generic version -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
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 _GLIBCXX_BEGIN_NAMESPACE(std)
39 // NB: The other ctype<char> specializations are in src/locale.cc and
40 // various /config/os/* files.
41 ctype_byname<char>::ctype_byname(const char* __s, size_t __refs)
42 : ctype<char>(0, false, __refs)
44 if (std::strcmp(__s, "C") != 0 && std::strcmp(__s, "POSIX") != 0)
46 this->_S_destroy_c_locale(this->_M_c_locale_ctype);
47 this->_S_create_c_locale(this->_M_c_locale_ctype, __s);
51 ctype_byname<char>::~ctype_byname()
54 #ifdef _GLIBCXX_USE_WCHAR_T
55 ctype<wchar_t>::__wmask_type
56 ctype<wchar_t>::_M_convert_to_wmask(const mask __m) const
62 __ret = wctype("space");
65 __ret = wctype("print");
68 __ret = wctype("cntrl");
71 __ret = wctype("upper");
74 __ret = wctype("lower");
77 __ret = wctype("alpha");
80 __ret = wctype("digit");
83 __ret = wctype("punct");
86 __ret = wctype("xdigit");
89 __ret = wctype("alnum");
92 __ret = wctype("graph");
95 __ret = __wmask_type();
101 ctype<wchar_t>::do_toupper(wchar_t __c) const
102 { return towupper(__c); }
105 ctype<wchar_t>::do_toupper(wchar_t* __lo, const wchar_t* __hi) const
109 *__lo = towupper(*__lo);
116 ctype<wchar_t>::do_tolower(wchar_t __c) const
117 { return towlower(__c); }
120 ctype<wchar_t>::do_tolower(wchar_t* __lo, const wchar_t* __hi) const
124 *__lo = towlower(*__lo);
132 do_is(mask __m, char_type __c) const
135 // Generically, 15 (instead of 10) since we don't know the numerical
136 // encoding of the various categories in /usr/include/ctype.h.
137 const size_t __bitmasksize = 15;
138 for (size_t __bitcur = 0; __bitcur <= __bitmasksize; ++__bitcur)
139 if (__m & _M_bit[__bitcur]
140 && iswctype(__c, _M_wmask[__bitcur]))
150 do_is(const wchar_t* __lo, const wchar_t* __hi, mask* __vec) const
152 for (;__lo < __hi; ++__vec, ++__lo)
154 // Generically, 15 (instead of 10) since we don't know the numerical
155 // encoding of the various categories in /usr/include/ctype.h.
156 const size_t __bitmasksize = 15;
158 for (size_t __bitcur = 0; __bitcur <= __bitmasksize; ++__bitcur)
159 if (iswctype(*__lo, _M_wmask[__bitcur]))
160 __m |= _M_bit[__bitcur];
168 do_scan_is(mask __m, const wchar_t* __lo, const wchar_t* __hi) const
170 while (__lo < __hi && !this->do_is(__m, *__lo))
177 do_scan_not(mask __m, const char_type* __lo, const char_type* __hi) const
179 while (__lo < __hi && this->do_is(__m, *__lo) != 0)
186 do_widen(char __c) const
187 { return _M_widen[static_cast<unsigned char>(__c)]; }
191 do_widen(const char* __lo, const char* __hi, wchar_t* __dest) const
195 *__dest = _M_widen[static_cast<unsigned char>(*__lo)];
204 do_narrow(wchar_t __wc, char __dfault) const
206 if (__wc >= 0 && __wc < 128 && _M_narrow_ok)
207 return _M_narrow[__wc];
208 const int __c = wctob(__wc);
209 return (__c == EOF ? __dfault : static_cast<char>(__c));
214 do_narrow(const wchar_t* __lo, const wchar_t* __hi, char __dfault,
220 if (*__lo >= 0 && *__lo < 128)
221 *__dest = _M_narrow[*__lo];
224 const int __c = wctob(*__lo);
225 *__dest = (__c == EOF ? __dfault : static_cast<char>(__c));
233 const int __c = wctob(*__lo);
234 *__dest = (__c == EOF ? __dfault : static_cast<char>(__c));
242 ctype<wchar_t>::_M_initialize_ctype()
245 for (__i = 0; __i < 128; ++__i)
247 const int __c = wctob(__i);
251 _M_narrow[__i] = static_cast<char>(__c);
256 _M_narrow_ok = false;
258 __i < sizeof(_M_widen) / sizeof(wint_t); ++__i)
259 _M_widen[__i] = btowc(__i);
261 for (size_t __i = 0; __i <= 15; ++__i)
263 _M_bit[__i] = static_cast<mask>(1 << __i);
264 _M_wmask[__i] = _M_convert_to_wmask(_M_bit[__i]);
267 #endif // _GLIBCXX_USE_WCHAR_T
269 _GLIBCXX_END_NAMESPACE