1 // std::ctype implementation details, newlib version -*- C++ -*-
3 // Copyright (C) 2011-2014 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.
34 namespace std _GLIBCXX_VISIBILITY(default)
36 _GLIBCXX_BEGIN_NAMESPACE_VERSION
38 // NB: The other ctype<char> specializations are in src/locale.cc and
39 // various /config/os/* files.
40 ctype_byname<char>::ctype_byname(const char* __s, size_t __refs)
41 : ctype<char>(0, false, __refs)
43 if (std::strcmp(__s, "C") != 0 && std::strcmp(__s, "POSIX") != 0)
45 this->_S_destroy_c_locale(this->_M_c_locale_ctype);
46 this->_S_create_c_locale(this->_M_c_locale_ctype, __s);
50 ctype_byname<char>::~ctype_byname()
53 #ifdef _GLIBCXX_USE_WCHAR_T
54 ctype<wchar_t>::__wmask_type
55 ctype<wchar_t>::_M_convert_to_wmask(const mask __m) const throw()
61 __ret = wctype("space");
64 __ret = wctype("print");
67 __ret = wctype("cntrl");
70 __ret = wctype("upper");
73 __ret = wctype("lower");
76 __ret = wctype("alpha");
79 __ret = wctype("digit");
82 __ret = wctype("punct");
85 __ret = wctype("xdigit");
88 __ret = wctype("alnum");
91 __ret = wctype("graph");
94 // Different from the generic version, xdigit and print in
95 // newlib are defined as bitwise-OR result of bitmasks:
97 // print = _P | _U | _L | _N | _B;
98 // in which _X and _B don't correspond to any ctype mask.
99 // In order to get the wmask correctly converted when __m is
100 // equal to _X or _B, the two cases are specifically handled
103 __ret = wctype("xdigit");
104 else if (__m & print)
105 __ret = wctype("print");
107 __ret = __wmask_type();
113 ctype<wchar_t>::do_toupper(wchar_t __c) const
114 { return towupper(__c); }
117 ctype<wchar_t>::do_toupper(wchar_t* __lo, const wchar_t* __hi) const
121 *__lo = towupper(*__lo);
128 ctype<wchar_t>::do_tolower(wchar_t __c) const
129 { return towlower(__c); }
132 ctype<wchar_t>::do_tolower(wchar_t* __lo, const wchar_t* __hi) const
136 *__lo = towlower(*__lo);
144 do_is(mask __m, wchar_t __c) const
147 // Newlib C library has a compact encoding that uses 8 bits only.
148 const size_t __bitmasksize = 7;
149 for (size_t __bitcur = 0; __bitcur <= __bitmasksize; ++__bitcur)
150 if (__m & _M_bit[__bitcur]
151 && iswctype(__c, _M_wmask[__bitcur]))
161 do_is(const wchar_t* __lo, const wchar_t* __hi, mask* __vec) const
163 for (; __lo < __hi; ++__vec, ++__lo)
165 // Newlib C library has a compact encoding that uses 8 bits only.
166 const size_t __bitmasksize = 7;
168 for (size_t __bitcur = 0; __bitcur <= __bitmasksize; ++__bitcur)
169 if (iswctype(*__lo, _M_wmask[__bitcur]))
170 __m |= _M_bit[__bitcur];
178 do_scan_is(mask __m, const wchar_t* __lo, const wchar_t* __hi) const
180 while (__lo < __hi && !this->do_is(__m, *__lo))
187 do_scan_not(mask __m, const char_type* __lo, const char_type* __hi) const
189 while (__lo < __hi && this->do_is(__m, *__lo) != 0)
196 do_widen(char __c) const
197 { return _M_widen[static_cast<unsigned char>(__c)]; }
201 do_widen(const char* __lo, const char* __hi, wchar_t* __dest) const
205 *__dest = _M_widen[static_cast<unsigned char>(*__lo)];
214 do_narrow(wchar_t __wc, char __dfault) const
216 if (__wc >= 0 && __wc < 128 && _M_narrow_ok)
217 return _M_narrow[__wc];
218 const int __c = wctob(__wc);
219 return (__c == EOF ? __dfault : static_cast<char>(__c));
224 do_narrow(const wchar_t* __lo, const wchar_t* __hi, char __dfault,
230 if (*__lo >= 0 && *__lo < 128)
231 *__dest = _M_narrow[*__lo];
234 const int __c = wctob(*__lo);
235 *__dest = (__c == EOF ? __dfault : static_cast<char>(__c));
243 const int __c = wctob(*__lo);
244 *__dest = (__c == EOF ? __dfault : static_cast<char>(__c));
252 ctype<wchar_t>::_M_initialize_ctype() throw()
255 for (__i = 0; __i < 128; ++__i)
257 const int __c = wctob(__i);
261 _M_narrow[__i] = static_cast<char>(__c);
266 _M_narrow_ok = false;
268 __i < sizeof(_M_widen) / sizeof(wint_t); ++__i)
269 _M_widen[__i] = btowc(__i);
271 for (size_t __i = 0; __i <= 7; ++__i)
273 _M_bit[__i] = static_cast<mask>(1 << __i);
274 _M_wmask[__i] = _M_convert_to_wmask(_M_bit[__i]);
277 #endif // _GLIBCXX_USE_WCHAR_T
279 _GLIBCXX_END_NAMESPACE_VERSION