]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/config/locale/generic/ctype_members.cc
re PR libstdc++/13341 (ctype<wchar_t>::do_narrow(wchar_t, char) is slow)
[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 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
10
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.
15
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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
20
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.
29
30 //
31 // ISO C++ 14882: 22.2.1.1.2 ctype virtual functions.
32 //
33
34 // Written by Benjamin Kosnik <bkoz@redhat.com>
35
36 #include <locale>
37
38 namespace std
39 {
40 // NB: The other ctype<char> specializations are in src/locale.cc and
41 // various /config/os/* files.
42 template<>
43 ctype_byname<char>::ctype_byname(const char* __s, size_t __refs)
44 : ctype<char>(0, false, __refs)
45 {
46 if (std::strcmp(__s, "C") != 0 && std::strcmp(__s, "POSIX") != 0)
47 {
48 _S_destroy_c_locale(_M_c_locale_ctype);
49 _S_create_c_locale(_M_c_locale_ctype, __s);
50 }
51 }
52
53 #ifdef _GLIBCXX_USE_WCHAR_T
54 ctype<wchar_t>::__wmask_type
55 ctype<wchar_t>::_M_convert_to_wmask(const mask __m) const
56 {
57 __wmask_type __ret;
58 switch (__m)
59 {
60 case space:
61 __ret = wctype("space");
62 break;
63 case print:
64 __ret = wctype("print");
65 break;
66 case cntrl:
67 __ret = wctype("cntrl");
68 break;
69 case upper:
70 __ret = wctype("upper");
71 break;
72 case lower:
73 __ret = wctype("lower");
74 break;
75 case alpha:
76 __ret = wctype("alpha");
77 break;
78 case digit:
79 __ret = wctype("digit");
80 break;
81 case punct:
82 __ret = wctype("punct");
83 break;
84 case xdigit:
85 __ret = wctype("xdigit");
86 break;
87 case alnum:
88 __ret = wctype("alnum");
89 break;
90 case graph:
91 __ret = wctype("graph");
92 break;
93 default:
94 __ret = 0;
95 }
96 return __ret;
97 };
98
99 wchar_t
100 ctype<wchar_t>::do_toupper(wchar_t __c) const
101 { return towupper(__c); }
102
103 const wchar_t*
104 ctype<wchar_t>::do_toupper(wchar_t* __lo, const wchar_t* __hi) const
105 {
106 while (__lo < __hi)
107 {
108 *__lo = towupper(*__lo);
109 ++__lo;
110 }
111 return __hi;
112 }
113
114 wchar_t
115 ctype<wchar_t>::do_tolower(wchar_t __c) const
116 { return towlower(__c); }
117
118 const wchar_t*
119 ctype<wchar_t>::do_tolower(wchar_t* __lo, const wchar_t* __hi) const
120 {
121 while (__lo < __hi)
122 {
123 *__lo = towlower(*__lo);
124 ++__lo;
125 }
126 return __hi;
127 }
128
129 bool
130 ctype<wchar_t>::
131 do_is(mask __m, char_type __c) const
132 {
133 bool __ret = false;
134 // Generically, 15 (instead of 10) since we don't know the numerical
135 // encoding of the various categories in /usr/include/ctype.h.
136 const size_t __bitmasksize = 15;
137 for (size_t __bitcur = 0; __bitcur <= __bitmasksize; ++__bitcur)
138 {
139 const mask __bit = static_cast<mask>(1 << __bitcur);
140 if (__m & __bit)
141 __ret |= iswctype(__c, _M_convert_to_wmask(__bit));
142 }
143 return __ret;
144 }
145
146 const wchar_t*
147 ctype<wchar_t>::
148 do_is(const wchar_t* __lo, const wchar_t* __hi, mask* __vec) const
149 {
150 for (;__lo < __hi; ++__vec, ++__lo)
151 {
152 // Generically, 15 (instead of 10) since we don't know the numerical
153 // encoding of the various categories in /usr/include/ctype.h.
154 const size_t __bitmasksize = 15;
155 mask __m = 0;
156 for (size_t __bitcur = 0; __bitcur <= __bitmasksize; ++__bitcur)
157 {
158 const mask __bit = static_cast<mask>(1 << __bitcur);
159 if (iswctype(*__lo, _M_convert_to_wmask(__bit)))
160 __m |= __bit;
161 }
162 *__vec = __m;
163 }
164 return __hi;
165 }
166
167 const wchar_t*
168 ctype<wchar_t>::
169 do_scan_is(mask __m, const wchar_t* __lo, const wchar_t* __hi) const
170 {
171 while (__lo < __hi && !this->do_is(__m, *__lo))
172 ++__lo;
173 return __lo;
174 }
175
176 const wchar_t*
177 ctype<wchar_t>::
178 do_scan_not(mask __m, const char_type* __lo, const char_type* __hi) const
179 {
180 while (__lo < __hi && this->do_is(__m, *__lo) != 0)
181 ++__lo;
182 return __lo;
183 }
184
185 wchar_t
186 ctype<wchar_t>::
187 do_widen(char __c) const
188 {
189 const unsigned char __uc = static_cast<unsigned char>(__c);
190 if (__uc < 128)
191 return _M_widen[__uc];
192 return btowc(__uc);
193 }
194
195 const char*
196 ctype<wchar_t>::
197 do_widen(const char* __lo, const char* __hi, wchar_t* __dest) const
198 {
199 while (__lo < __hi)
200 {
201 const unsigned char __uc = static_cast<unsigned char>(*__lo);
202 if (__uc < 128)
203 *__dest = _M_widen[__uc];
204 else
205 *__dest = btowc(__uc);
206 ++__lo;
207 ++__dest;
208 }
209 return __hi;
210 }
211
212 char
213 ctype<wchar_t>::
214 do_narrow(wchar_t __wc, char __dfault) const
215 {
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));
220 }
221
222 const wchar_t*
223 ctype<wchar_t>::
224 do_narrow(const wchar_t* __lo, const wchar_t* __hi, char __dfault,
225 char* __dest) const
226 {
227 if (_M_narrow_ok)
228 while (__lo < __hi)
229 {
230 if (*__lo >= 0 && *__lo < 128)
231 *__dest = _M_narrow[*__lo];
232 else
233 {
234 const int __c = wctob(*__lo);
235 *__dest = (__c == EOF ? __dfault : static_cast<char>(__c));
236 }
237 ++__lo;
238 ++__dest;
239 }
240 else
241 while (__lo < __hi)
242 {
243 const int __c = wctob(*__lo);
244 *__dest = (__c == EOF ? __dfault : static_cast<char>(__c));
245 ++__lo;
246 ++__dest;
247 }
248 return __hi;
249 }
250
251 void
252 ctype<wchar_t>::_M_initialize_ctype()
253 {
254 wint_t __i;
255 for (__i = 0; __i < 128; ++__i)
256 {
257 const int __c = wctob(__i);
258 if (__c == EOF)
259 break;
260 else
261 _M_narrow[__i] = static_cast<char>(__c);
262 }
263 if (__i == 128)
264 _M_narrow_ok = true;
265 else
266 _M_narrow_ok = false;
267 for (int __i = 0; __i < 128; ++__i)
268 _M_widen[__i] = btowc(__i);
269 }
270 #endif // _GLIBCXX_USE_WCHAR_T
271 }