]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/config/locale/vxworks/ctype_members.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / config / locale / vxworks / ctype_members.cc
1 // std::ctype implementation details, vxworks specific version -*- C++ -*-
2
3 // Copyright (C) 2001-2022 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 3, 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 // 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.
19
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/>.
24
25 //
26 // ISO C++ 14882: 22.2.1.1.2 ctype virtual functions.
27 //
28
29 // Originally written by Benjamin Kosnik <bkoz@redhat.com>.
30 // Ported to vxworks by Corentin Gay <gay@adacore.com>.
31
32 #include <locale>
33 #include <cstdlib>
34 #include <cstring>
35 #include <cstdio>
36 #include <iostream>
37
38 namespace std _GLIBCXX_VISIBILITY(default)
39 {
40 _GLIBCXX_BEGIN_NAMESPACE_VERSION
41
42 // NB: The other ctype<char> specializations are in src/locale.cc and
43 // various /config/os/* files.
44 ctype_byname<char>::ctype_byname(const char* __s, size_t __refs)
45 : ctype<char>(0, false, __refs)
46 {
47 if (std::strcmp(__s, "C") != 0 && std::strcmp(__s, "POSIX") != 0)
48 {
49 this->_S_destroy_c_locale(this->_M_c_locale_ctype);
50 this->_S_create_c_locale(this->_M_c_locale_ctype, __s);
51 }
52 }
53
54 ctype_byname<char>::~ctype_byname()
55 { }
56
57 #ifdef _GLIBCXX_USE_WCHAR_T
58 ctype<wchar_t>::__wmask_type
59 ctype<wchar_t>::_M_convert_to_wmask(const mask __m) const throw()
60 {
61 __wmask_type __ret;
62
63 switch (__m)
64 {
65 case space:
66 __ret = wctype("space");
67 break;
68 case print:
69 __ret = wctype("print");
70 break;
71 case cntrl:
72 __ret = wctype("cntrl");
73 break;
74 case upper:
75 __ret = wctype("upper");
76 break;
77 case lower:
78 __ret = wctype("lower");
79 break;
80 case alpha:
81 __ret = wctype("alpha");
82 break;
83 case digit:
84 __ret = wctype("digit");
85 break;
86 case punct:
87 __ret = wctype("punct");
88 break;
89 case xdigit:
90 __ret = wctype("xdigit");
91 break;
92 case alnum:
93 __ret = wctype("alnum");
94 break;
95 case graph:
96 __ret = wctype("graph");
97 break;
98 default:
99 __ret = __wmask_type();
100 break;
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 // In VxWorks, a ctype is a short int, thus if we go up to the 15th index,
141 // we will overflow.
142 const size_t __bitmasksize = 14;
143
144 // VxWorks does not consider spaces to be blank, however, the testsuite
145 // and more generally the libstdc++ rely on it, we explicitly handle
146 // that case here.
147 if ((__m & blank) && isspace(__c))
148 {
149 __ret = true;
150 }
151 else
152 {
153 for (size_t __bitcur = 0; __bitcur <= __bitmasksize; ++__bitcur) {
154 if (__m & _M_bit[__bitcur]
155 && iswctype(__c, _M_wmask[__bitcur]))
156 {
157 __ret = true;
158 break;
159 }
160 }
161 }
162 return __ret;
163 }
164
165 const wchar_t*
166 ctype<wchar_t>::
167 do_is(const wchar_t* __lo, const wchar_t* __hi, mask* __vec) const
168 {
169 for (;__lo < __hi; ++__vec, ++__lo)
170 {
171 const size_t __bitmasksize = 14;
172 // In VxWorks, a ctype is a short int, thus if we go up to the 15th index,
173 // we will overflow.
174 mask __m = 0;
175 // VxWorks does not consider space as blank, so let's add an explicit
176 // check.
177 if (isspace(*__lo))
178 __m |= blank;
179 for (size_t __bitcur = 0; __bitcur <= __bitmasksize; ++__bitcur)
180 if (iswctype(*__lo, _M_wmask[__bitcur]))
181 __m |= _M_bit[__bitcur];
182 *__vec = __m;
183 }
184 return __hi;
185 }
186
187 const wchar_t*
188 ctype<wchar_t>::
189 do_scan_is(mask __m, const wchar_t* __lo, const wchar_t* __hi) const
190 {
191 while (__lo < __hi && !this->do_is(__m, *__lo))
192 ++__lo;
193 return __lo;
194 }
195
196 const wchar_t*
197 ctype<wchar_t>::
198 do_scan_not(mask __m, const char_type* __lo, const char_type* __hi) const
199 {
200 while (__lo < __hi && this->do_is(__m, *__lo) != 0)
201 ++__lo;
202 return __lo;
203 }
204
205 wchar_t
206 ctype<wchar_t>::
207 do_widen(char __c) const
208 { return _M_widen[static_cast<unsigned char>(__c)]; }
209
210 const char*
211 ctype<wchar_t>::
212 do_widen(const char* __lo, const char* __hi, wchar_t* __dest) const
213 {
214 while (__lo < __hi)
215 {
216 *__dest = _M_widen[static_cast<unsigned char>(*__lo)];
217 ++__lo;
218 ++__dest;
219 }
220 return __hi;
221 }
222
223 char
224 ctype<wchar_t>::
225 do_narrow(wchar_t __wc, char __dfault) const
226 {
227 if (__wc >= 0 && __wc < 128 && _M_narrow_ok)
228 return _M_narrow[__wc];
229 const int __c = wctob(__wc);
230 return (__c == EOF ? __dfault : static_cast<char>(__c));
231 }
232
233 const wchar_t*
234 ctype<wchar_t>::
235 do_narrow(const wchar_t* __lo, const wchar_t* __hi, char __dfault,
236 char* __dest) const
237 {
238 if (_M_narrow_ok)
239 while (__lo < __hi)
240 {
241 if (*__lo >= 0 && *__lo < 128)
242 *__dest = _M_narrow[*__lo];
243 else
244 {
245 const int __c = wctob(*__lo);
246 *__dest = (__c == EOF ? __dfault : static_cast<char>(__c));
247 }
248 ++__lo;
249 ++__dest;
250 }
251 else
252 while (__lo < __hi)
253 {
254 const int __c = wctob(*__lo);
255 *__dest = (__c == EOF ? __dfault : static_cast<char>(__c));
256 ++__lo;
257 ++__dest;
258 }
259 return __hi;
260 }
261
262 void
263 ctype<wchar_t>::_M_initialize_ctype() throw()
264 {
265 wint_t __i;
266 for (__i = 0; __i < 128; ++__i)
267 {
268 const int __c = wctob(__i);
269 if (__c == EOF)
270 break;
271 else
272 _M_narrow[__i] = static_cast<char>(__c);
273 }
274 if (__i == 128)
275 _M_narrow_ok = true;
276 else
277 _M_narrow_ok = false;
278 for (size_t __i = 0;
279 __i < sizeof(_M_widen) / sizeof(wint_t); ++__i)
280 _M_widen[__i] = btowc(__i);
281
282 // In VxWorks, a ctype is a short int, thus if we go up to the 15th index,
283 // we will overflow.
284 for (size_t __i = 0; __i <= 14; ++__i)
285 {
286 _M_bit[__i] = static_cast<mask>(1 << __i);
287 _M_wmask[__i] = _M_convert_to_wmask(_M_bit[__i]);
288 }
289 }
290 #endif // _GLIBCXX_USE_WCHAR_T
291 _GLIBCXX_END_NAMESPACE_VERSION
292 } // namespace