1 // std::ctype implementation details, GNU version -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005 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 2, 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 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
31 // ISO C++ 14882: 22.2.1.1.2 ctype virtual functions.
34 // Written by Benjamin Kosnik <bkoz@redhat.com>
37 #include <bits/c++locale_internal.h>
39 _GLIBCXX_BEGIN_NAMESPACE(std)
41 // NB: The other ctype<char> specializations are in src/locale.cc and
42 // various /config/os/* files.
44 ctype_byname<char>::ctype_byname(const char* __s, size_t __refs)
45 : ctype<char>(0, false, __refs)
47 if (std::strcmp(__s, "C") != 0 && std::strcmp(__s, "POSIX") != 0)
49 this->_S_destroy_c_locale(this->_M_c_locale_ctype);
50 this->_S_create_c_locale(this->_M_c_locale_ctype, __s);
51 this->_M_toupper = this->_M_c_locale_ctype->__ctype_toupper;
52 this->_M_tolower = this->_M_c_locale_ctype->__ctype_tolower;
53 this->_M_table = this->_M_c_locale_ctype->__ctype_b;
57 #ifdef _GLIBCXX_USE_WCHAR_T
58 ctype<wchar_t>::__wmask_type
59 ctype<wchar_t>::_M_convert_to_wmask(const mask __m) const
65 __ret = __wctype_l("space", _M_c_locale_ctype);
68 __ret = __wctype_l("print", _M_c_locale_ctype);
71 __ret = __wctype_l("cntrl", _M_c_locale_ctype);
74 __ret = __wctype_l("upper", _M_c_locale_ctype);
77 __ret = __wctype_l("lower", _M_c_locale_ctype);
80 __ret = __wctype_l("alpha", _M_c_locale_ctype);
83 __ret = __wctype_l("digit", _M_c_locale_ctype);
86 __ret = __wctype_l("punct", _M_c_locale_ctype);
89 __ret = __wctype_l("xdigit", _M_c_locale_ctype);
92 __ret = __wctype_l("alnum", _M_c_locale_ctype);
95 __ret = __wctype_l("graph", _M_c_locale_ctype);
98 __ret = __wmask_type();
104 ctype<wchar_t>::do_toupper(wchar_t __c) const
105 { return __towupper_l(__c, _M_c_locale_ctype); }
108 ctype<wchar_t>::do_toupper(wchar_t* __lo, const wchar_t* __hi) const
112 *__lo = __towupper_l(*__lo, _M_c_locale_ctype);
119 ctype<wchar_t>::do_tolower(wchar_t __c) const
120 { return __towlower_l(__c, _M_c_locale_ctype); }
123 ctype<wchar_t>::do_tolower(wchar_t* __lo, const wchar_t* __hi) const
127 *__lo = __towlower_l(*__lo, _M_c_locale_ctype);
135 do_is(mask __m, wchar_t __c) const
137 // The case of __m == ctype_base::space is particularly important,
138 // due to its use in many istream functions. Therefore we deal with
139 // it first, exploiting the knowledge that on GNU systems _M_bit[5]
140 // is the mask corresponding to ctype_base::space. NB: an encoding
141 // change would not affect correctness!
143 if (__m == _M_bit[5])
144 __ret = __iswctype_l(__c, _M_wmask[5], _M_c_locale_ctype);
147 // Highest bitmask in ctype_base == 10, but extra in "C"
148 // library for blank.
149 const size_t __bitmasksize = 11;
150 for (size_t __bitcur = 0; __bitcur <= __bitmasksize; ++__bitcur)
151 if (__m & _M_bit[__bitcur])
153 if (__iswctype_l(__c, _M_wmask[__bitcur], _M_c_locale_ctype))
158 else if (__m == _M_bit[__bitcur])
167 do_is(const wchar_t* __lo, const wchar_t* __hi, mask* __vec) const
169 for (; __lo < __hi; ++__vec, ++__lo)
171 // Highest bitmask in ctype_base == 10, but extra in "C"
172 // library for blank.
173 const size_t __bitmasksize = 11;
175 for (size_t __bitcur = 0; __bitcur <= __bitmasksize; ++__bitcur)
176 if (__iswctype_l(*__lo, _M_wmask[__bitcur], _M_c_locale_ctype))
177 __m |= _M_bit[__bitcur];
185 do_scan_is(mask __m, const wchar_t* __lo, const wchar_t* __hi) const
187 while (__lo < __hi && !this->do_is(__m, *__lo))
194 do_scan_not(mask __m, const char_type* __lo, const char_type* __hi) const
196 while (__lo < __hi && this->do_is(__m, *__lo) != 0)
203 do_widen(char __c) const
204 { return _M_widen[static_cast<unsigned char>(__c)]; }
208 do_widen(const char* __lo, const char* __hi, wchar_t* __dest) const
212 *__dest = _M_widen[static_cast<unsigned char>(*__lo)];
221 do_narrow(wchar_t __wc, char __dfault) const
223 if (__wc >= 0 && __wc < 128 && _M_narrow_ok)
224 return _M_narrow[__wc];
225 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
226 __c_locale __old = __uselocale(_M_c_locale_ctype);
228 const int __c = wctob(__wc);
229 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
232 return (__c == EOF ? __dfault : static_cast<char>(__c));
237 do_narrow(const wchar_t* __lo, const wchar_t* __hi, char __dfault,
240 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
241 __c_locale __old = __uselocale(_M_c_locale_ctype);
246 if (*__lo >= 0 && *__lo < 128)
247 *__dest = _M_narrow[*__lo];
250 const int __c = wctob(*__lo);
251 *__dest = (__c == EOF ? __dfault : static_cast<char>(__c));
259 const int __c = wctob(*__lo);
260 *__dest = (__c == EOF ? __dfault : static_cast<char>(__c));
264 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
271 ctype<wchar_t>::_M_initialize_ctype()
273 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
274 __c_locale __old = __uselocale(_M_c_locale_ctype);
277 for (__i = 0; __i < 128; ++__i)
279 const int __c = wctob(__i);
283 _M_narrow[__i] = static_cast<char>(__c);
288 _M_narrow_ok = false;
290 __j < sizeof(_M_widen) / sizeof(wint_t); ++__j)
291 _M_widen[__j] = btowc(__j);
293 for (size_t __k = 0; __k <= 11; ++__k)
295 _M_bit[__k] = static_cast<mask>(_ISbit(__k));
296 _M_wmask[__k] = _M_convert_to_wmask(_M_bit[__k]);
298 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
302 #endif // _GLIBCXX_USE_WCHAR_T
304 _GLIBCXX_END_NAMESPACE