]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/config/locale/generic/ctype_members.cc
acinclude.m4 ([GLIBCXX_CHECK_STDIO_MACROS]): New, checks for common values of EOF...
[thirdparty/gcc.git] / libstdc++-v3 / config / locale / generic / ctype_members.cc
1 // std::ctype implementation details, generic version -*- C++ -*-
2
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 // 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 2, 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 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 // USA.
21
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
30
31 //
32 // ISO C++ 14882: 22.2.1.1.2 ctype virtual functions.
33 //
34
35 // Written by Benjamin Kosnik <bkoz@redhat.com>
36
37 #include <locale>
38 #include <cstdlib>
39 #include <cstring>
40 #include <cstdio>
41
42 _GLIBCXX_BEGIN_NAMESPACE(std)
43
44 // NB: The other ctype<char> specializations are in src/locale.cc and
45 // various /config/os/* files.
46 ctype_byname<char>::ctype_byname(const char* __s, size_t __refs)
47 : ctype<char>(0, false, __refs)
48 {
49 if (std::strcmp(__s, "C") != 0 && std::strcmp(__s, "POSIX") != 0)
50 {
51 this->_S_destroy_c_locale(this->_M_c_locale_ctype);
52 this->_S_create_c_locale(this->_M_c_locale_ctype, __s);
53 }
54 }
55
56 ctype_byname<char>::~ctype_byname()
57 { }
58
59 #ifdef _GLIBCXX_USE_WCHAR_T
60 ctype<wchar_t>::__wmask_type
61 ctype<wchar_t>::_M_convert_to_wmask(const mask __m) const
62 {
63 __wmask_type __ret;
64 switch (__m)
65 {
66 case space:
67 __ret = wctype("space");
68 break;
69 case print:
70 __ret = wctype("print");
71 break;
72 case cntrl:
73 __ret = wctype("cntrl");
74 break;
75 case upper:
76 __ret = wctype("upper");
77 break;
78 case lower:
79 __ret = wctype("lower");
80 break;
81 case alpha:
82 __ret = wctype("alpha");
83 break;
84 case digit:
85 __ret = wctype("digit");
86 break;
87 case punct:
88 __ret = wctype("punct");
89 break;
90 case xdigit:
91 __ret = wctype("xdigit");
92 break;
93 case alnum:
94 __ret = wctype("alnum");
95 break;
96 case graph:
97 __ret = wctype("graph");
98 break;
99 default:
100 __ret = __wmask_type();
101 }
102 return __ret;
103 };
104
105 wchar_t
106 ctype<wchar_t>::do_toupper(wchar_t __c) const
107 { return towupper(__c); }
108
109 const wchar_t*
110 ctype<wchar_t>::do_toupper(wchar_t* __lo, const wchar_t* __hi) const
111 {
112 while (__lo < __hi)
113 {
114 *__lo = towupper(*__lo);
115 ++__lo;
116 }
117 return __hi;
118 }
119
120 wchar_t
121 ctype<wchar_t>::do_tolower(wchar_t __c) const
122 { return towlower(__c); }
123
124 const wchar_t*
125 ctype<wchar_t>::do_tolower(wchar_t* __lo, const wchar_t* __hi) const
126 {
127 while (__lo < __hi)
128 {
129 *__lo = towlower(*__lo);
130 ++__lo;
131 }
132 return __hi;
133 }
134
135 bool
136 ctype<wchar_t>::
137 do_is(mask __m, char_type __c) const
138 {
139 bool __ret = false;
140 // Generically, 15 (instead of 10) since we don't know the numerical
141 // encoding of the various categories in /usr/include/ctype.h.
142 const size_t __bitmasksize = 15;
143 for (size_t __bitcur = 0; __bitcur <= __bitmasksize; ++__bitcur)
144 if (__m & _M_bit[__bitcur]
145 && iswctype(__c, _M_wmask[__bitcur]))
146 {
147 __ret = true;
148 break;
149 }
150 return __ret;
151 }
152
153 const wchar_t*
154 ctype<wchar_t>::
155 do_is(const wchar_t* __lo, const wchar_t* __hi, mask* __vec) const
156 {
157 for (;__lo < __hi; ++__vec, ++__lo)
158 {
159 // Generically, 15 (instead of 10) since we don't know the numerical
160 // encoding of the various categories in /usr/include/ctype.h.
161 const size_t __bitmasksize = 15;
162 mask __m = 0;
163 for (size_t __bitcur = 0; __bitcur <= __bitmasksize; ++__bitcur)
164 if (iswctype(*__lo, _M_wmask[__bitcur]))
165 __m |= _M_bit[__bitcur];
166 *__vec = __m;
167 }
168 return __hi;
169 }
170
171 const wchar_t*
172 ctype<wchar_t>::
173 do_scan_is(mask __m, const wchar_t* __lo, const wchar_t* __hi) const
174 {
175 while (__lo < __hi && !this->do_is(__m, *__lo))
176 ++__lo;
177 return __lo;
178 }
179
180 const wchar_t*
181 ctype<wchar_t>::
182 do_scan_not(mask __m, const char_type* __lo, const char_type* __hi) const
183 {
184 while (__lo < __hi && this->do_is(__m, *__lo) != 0)
185 ++__lo;
186 return __lo;
187 }
188
189 wchar_t
190 ctype<wchar_t>::
191 do_widen(char __c) const
192 { return _M_widen[static_cast<unsigned char>(__c)]; }
193
194 const char*
195 ctype<wchar_t>::
196 do_widen(const char* __lo, const char* __hi, wchar_t* __dest) const
197 {
198 while (__lo < __hi)
199 {
200 *__dest = _M_widen[static_cast<unsigned char>(*__lo)];
201 ++__lo;
202 ++__dest;
203 }
204 return __hi;
205 }
206
207 char
208 ctype<wchar_t>::
209 do_narrow(wchar_t __wc, char __dfault) const
210 {
211 if (__wc >= 0 && __wc < 128 && _M_narrow_ok)
212 return _M_narrow[__wc];
213 const int __c = wctob(__wc);
214 return (__c == EOF ? __dfault : static_cast<char>(__c));
215 }
216
217 const wchar_t*
218 ctype<wchar_t>::
219 do_narrow(const wchar_t* __lo, const wchar_t* __hi, char __dfault,
220 char* __dest) const
221 {
222 if (_M_narrow_ok)
223 while (__lo < __hi)
224 {
225 if (*__lo >= 0 && *__lo < 128)
226 *__dest = _M_narrow[*__lo];
227 else
228 {
229 const int __c = wctob(*__lo);
230 *__dest = (__c == EOF ? __dfault : static_cast<char>(__c));
231 }
232 ++__lo;
233 ++__dest;
234 }
235 else
236 while (__lo < __hi)
237 {
238 const int __c = wctob(*__lo);
239 *__dest = (__c == EOF ? __dfault : static_cast<char>(__c));
240 ++__lo;
241 ++__dest;
242 }
243 return __hi;
244 }
245
246 void
247 ctype<wchar_t>::_M_initialize_ctype()
248 {
249 wint_t __i;
250 for (__i = 0; __i < 128; ++__i)
251 {
252 const int __c = wctob(__i);
253 if (__c == EOF)
254 break;
255 else
256 _M_narrow[__i] = static_cast<char>(__c);
257 }
258 if (__i == 128)
259 _M_narrow_ok = true;
260 else
261 _M_narrow_ok = false;
262 for (size_t __i = 0;
263 __i < sizeof(_M_widen) / sizeof(wint_t); ++__i)
264 _M_widen[__i] = btowc(__i);
265
266 for (size_t __i = 0; __i <= 15; ++__i)
267 {
268 _M_bit[__i] = static_cast<mask>(1 << __i);
269 _M_wmask[__i] = _M_convert_to_wmask(_M_bit[__i]);
270 }
271 }
272 #endif // _GLIBCXX_USE_WCHAR_T
273
274 _GLIBCXX_END_NAMESPACE