]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/config/locale/gnu/ctype_members.cc
Update Copyright years for files modified in 2011 and/or 2012.
[thirdparty/gcc.git] / libstdc++-v3 / config / locale / gnu / ctype_members.cc
1 // std::ctype implementation details, GNU version -*- C++ -*-
2
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
4 // 2011 Free Software Foundation, Inc.
5 //
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)
10 // any later version.
11
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.
16
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.
20
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/>.
25
26 //
27 // ISO C++ 14882: 22.2.1.1.2 ctype virtual functions.
28 //
29
30 // Written by Benjamin Kosnik <bkoz@redhat.com>
31
32 #include <locale>
33 #include <cstdio>
34 #include <bits/c++locale_internal.h>
35
36 namespace std _GLIBCXX_VISIBILITY(default)
37 {
38 _GLIBCXX_BEGIN_NAMESPACE_VERSION
39
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)
44 {
45 if (std::strcmp(__s, "C") != 0 && std::strcmp(__s, "POSIX") != 0)
46 {
47 this->_S_destroy_c_locale(this->_M_c_locale_ctype);
48 this->_S_create_c_locale(this->_M_c_locale_ctype, __s);
49 this->_M_toupper = this->_M_c_locale_ctype->__ctype_toupper;
50 this->_M_tolower = this->_M_c_locale_ctype->__ctype_tolower;
51 this->_M_table = this->_M_c_locale_ctype->__ctype_b;
52 }
53 }
54
55 ctype_byname<char>::~ctype_byname()
56 { }
57
58 #ifdef _GLIBCXX_USE_WCHAR_T
59 ctype<wchar_t>::__wmask_type
60 ctype<wchar_t>::_M_convert_to_wmask(const mask __m) const throw()
61 {
62 __wmask_type __ret;
63 switch (__m)
64 {
65 case space:
66 __ret = __wctype_l("space", _M_c_locale_ctype);
67 break;
68 case print:
69 __ret = __wctype_l("print", _M_c_locale_ctype);
70 break;
71 case cntrl:
72 __ret = __wctype_l("cntrl", _M_c_locale_ctype);
73 break;
74 case upper:
75 __ret = __wctype_l("upper", _M_c_locale_ctype);
76 break;
77 case lower:
78 __ret = __wctype_l("lower", _M_c_locale_ctype);
79 break;
80 case alpha:
81 __ret = __wctype_l("alpha", _M_c_locale_ctype);
82 break;
83 case digit:
84 __ret = __wctype_l("digit", _M_c_locale_ctype);
85 break;
86 case punct:
87 __ret = __wctype_l("punct", _M_c_locale_ctype);
88 break;
89 case xdigit:
90 __ret = __wctype_l("xdigit", _M_c_locale_ctype);
91 break;
92 case alnum:
93 __ret = __wctype_l("alnum", _M_c_locale_ctype);
94 break;
95 case graph:
96 __ret = __wctype_l("graph", _M_c_locale_ctype);
97 break;
98 default:
99 __ret = __wmask_type();
100 }
101 return __ret;
102 }
103
104 wchar_t
105 ctype<wchar_t>::do_toupper(wchar_t __c) const
106 { return __towupper_l(__c, _M_c_locale_ctype); }
107
108 const wchar_t*
109 ctype<wchar_t>::do_toupper(wchar_t* __lo, const wchar_t* __hi) const
110 {
111 while (__lo < __hi)
112 {
113 *__lo = __towupper_l(*__lo, _M_c_locale_ctype);
114 ++__lo;
115 }
116 return __hi;
117 }
118
119 wchar_t
120 ctype<wchar_t>::do_tolower(wchar_t __c) const
121 { return __towlower_l(__c, _M_c_locale_ctype); }
122
123 const wchar_t*
124 ctype<wchar_t>::do_tolower(wchar_t* __lo, const wchar_t* __hi) const
125 {
126 while (__lo < __hi)
127 {
128 *__lo = __towlower_l(*__lo, _M_c_locale_ctype);
129 ++__lo;
130 }
131 return __hi;
132 }
133
134 bool
135 ctype<wchar_t>::
136 do_is(mask __m, wchar_t __c) const
137 {
138 // The case of __m == ctype_base::space is particularly important,
139 // due to its use in many istream functions. Therefore we deal with
140 // it first, exploiting the knowledge that on GNU systems _M_bit[5]
141 // is the mask corresponding to ctype_base::space. NB: an encoding
142 // change would not affect correctness!
143 bool __ret = false;
144 if (__m == _M_bit[5])
145 __ret = __iswctype_l(__c, _M_wmask[5], _M_c_locale_ctype);
146 else
147 {
148 // Highest bitmask in ctype_base == 10, but extra in "C"
149 // library for blank.
150 const size_t __bitmasksize = 11;
151 for (size_t __bitcur = 0; __bitcur <= __bitmasksize; ++__bitcur)
152 if (__m & _M_bit[__bitcur])
153 {
154 if (__iswctype_l(__c, _M_wmask[__bitcur], _M_c_locale_ctype))
155 {
156 __ret = true;
157 break;
158 }
159 else if (__m == _M_bit[__bitcur])
160 break;
161 }
162 }
163 return __ret;
164 }
165
166 const wchar_t*
167 ctype<wchar_t>::
168 do_is(const wchar_t* __lo, const wchar_t* __hi, mask* __vec) const
169 {
170 for (; __lo < __hi; ++__vec, ++__lo)
171 {
172 // Highest bitmask in ctype_base == 10, but extra in "C"
173 // library for blank.
174 const size_t __bitmasksize = 11;
175 mask __m = 0;
176 for (size_t __bitcur = 0; __bitcur <= __bitmasksize; ++__bitcur)
177 if (__iswctype_l(*__lo, _M_wmask[__bitcur], _M_c_locale_ctype))
178 __m |= _M_bit[__bitcur];
179 *__vec = __m;
180 }
181 return __hi;
182 }
183
184 const wchar_t*
185 ctype<wchar_t>::
186 do_scan_is(mask __m, const wchar_t* __lo, const wchar_t* __hi) const
187 {
188 while (__lo < __hi && !this->do_is(__m, *__lo))
189 ++__lo;
190 return __lo;
191 }
192
193 const wchar_t*
194 ctype<wchar_t>::
195 do_scan_not(mask __m, const char_type* __lo, const char_type* __hi) const
196 {
197 while (__lo < __hi && this->do_is(__m, *__lo) != 0)
198 ++__lo;
199 return __lo;
200 }
201
202 wchar_t
203 ctype<wchar_t>::
204 do_widen(char __c) const
205 { return _M_widen[static_cast<unsigned char>(__c)]; }
206
207 const char*
208 ctype<wchar_t>::
209 do_widen(const char* __lo, const char* __hi, wchar_t* __dest) const
210 {
211 while (__lo < __hi)
212 {
213 *__dest = _M_widen[static_cast<unsigned char>(*__lo)];
214 ++__lo;
215 ++__dest;
216 }
217 return __hi;
218 }
219
220 char
221 ctype<wchar_t>::
222 do_narrow(wchar_t __wc, char __dfault) const
223 {
224 if (__wc >= 0 && __wc < 128 && _M_narrow_ok)
225 return _M_narrow[__wc];
226 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
227 __c_locale __old = __uselocale(_M_c_locale_ctype);
228 #endif
229 const int __c = wctob(__wc);
230 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
231 __uselocale(__old);
232 #endif
233 return (__c == EOF ? __dfault : static_cast<char>(__c));
234 }
235
236 const wchar_t*
237 ctype<wchar_t>::
238 do_narrow(const wchar_t* __lo, const wchar_t* __hi, char __dfault,
239 char* __dest) const
240 {
241 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
242 __c_locale __old = __uselocale(_M_c_locale_ctype);
243 #endif
244 if (_M_narrow_ok)
245 while (__lo < __hi)
246 {
247 if (*__lo >= 0 && *__lo < 128)
248 *__dest = _M_narrow[*__lo];
249 else
250 {
251 const int __c = wctob(*__lo);
252 *__dest = (__c == EOF ? __dfault : static_cast<char>(__c));
253 }
254 ++__lo;
255 ++__dest;
256 }
257 else
258 while (__lo < __hi)
259 {
260 const int __c = wctob(*__lo);
261 *__dest = (__c == EOF ? __dfault : static_cast<char>(__c));
262 ++__lo;
263 ++__dest;
264 }
265 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
266 __uselocale(__old);
267 #endif
268 return __hi;
269 }
270
271 void
272 ctype<wchar_t>::_M_initialize_ctype() throw()
273 {
274 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
275 __c_locale __old = __uselocale(_M_c_locale_ctype);
276 #endif
277 wint_t __i;
278 for (__i = 0; __i < 128; ++__i)
279 {
280 const int __c = wctob(__i);
281 if (__c == EOF)
282 break;
283 else
284 _M_narrow[__i] = static_cast<char>(__c);
285 }
286 if (__i == 128)
287 _M_narrow_ok = true;
288 else
289 _M_narrow_ok = false;
290 for (size_t __j = 0;
291 __j < sizeof(_M_widen) / sizeof(wint_t); ++__j)
292 _M_widen[__j] = btowc(__j);
293
294 for (size_t __k = 0; __k <= 11; ++__k)
295 {
296 _M_bit[__k] = static_cast<mask>(_ISbit(__k));
297 _M_wmask[__k] = _M_convert_to_wmask(_M_bit[__k]);
298 }
299 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
300 __uselocale(__old);
301 #endif
302 }
303 #endif // _GLIBCXX_USE_WCHAR_T
304
305 _GLIBCXX_END_NAMESPACE_VERSION
306 } // namespace