]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/config/locale/gnu/ctype_members.cc
c++locale_internal.h: New header.
[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 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 #include "c++locale_internal.h"
38
39 namespace std
40 {
41 // NB: The other ctype<char> specializations are in src/locale.cc and
42 // various /config/os/* files.
43 template<>
44 ctype_byname<char>::ctype_byname(const char* __s, size_t __refs)
45 : ctype<char>(0, false, __refs)
46 {
47 if (_M_c_locale_ctype != _S_c_locale)
48 _S_destroy_c_locale(_M_c_locale_ctype);
49 _S_create_c_locale(_M_c_locale_ctype, __s);
50 _M_toupper = _M_c_locale_ctype->__ctype_toupper;
51 _M_tolower = _M_c_locale_ctype->__ctype_tolower;
52 _M_table = _M_c_locale_ctype->__ctype_b;
53 }
54
55 #ifdef _GLIBCPP_USE_WCHAR_T
56 ctype<wchar_t>::__wmask_type
57 ctype<wchar_t>::_M_convert_to_wmask(const mask __m) const
58 {
59 __wmask_type __ret;
60 switch (__m)
61 {
62 case space:
63 __ret = __wctype_l("space", _M_c_locale_ctype);
64 break;
65 case print:
66 __ret = __wctype_l("print", _M_c_locale_ctype);
67 break;
68 case cntrl:
69 __ret = __wctype_l("cntrl", _M_c_locale_ctype);
70 break;
71 case upper:
72 __ret = __wctype_l("upper", _M_c_locale_ctype);
73 break;
74 case lower:
75 __ret = __wctype_l("lower", _M_c_locale_ctype);
76 break;
77 case alpha:
78 __ret = __wctype_l("alpha", _M_c_locale_ctype);
79 break;
80 case digit:
81 __ret = __wctype_l("digit", _M_c_locale_ctype);
82 break;
83 case punct:
84 __ret = __wctype_l("punct", _M_c_locale_ctype);
85 break;
86 case xdigit:
87 __ret = __wctype_l("xdigit", _M_c_locale_ctype);
88 break;
89 case alnum:
90 __ret = __wctype_l("alnum", _M_c_locale_ctype);
91 break;
92 case graph:
93 __ret = __wctype_l("graph", _M_c_locale_ctype);
94 break;
95 default:
96 __ret = 0;
97 }
98 return __ret;
99 };
100
101 wchar_t
102 ctype<wchar_t>::do_toupper(wchar_t __c) const
103 { return __towupper_l(__c, _M_c_locale_ctype); }
104
105 const wchar_t*
106 ctype<wchar_t>::do_toupper(wchar_t* __lo, const wchar_t* __hi) const
107 {
108 while (__lo < __hi)
109 {
110 *__lo = __towupper_l(*__lo, _M_c_locale_ctype);
111 ++__lo;
112 }
113 return __hi;
114 }
115
116 wchar_t
117 ctype<wchar_t>::do_tolower(wchar_t __c) const
118 { return __towlower_l(__c, _M_c_locale_ctype); }
119
120 const wchar_t*
121 ctype<wchar_t>::do_tolower(wchar_t* __lo, const wchar_t* __hi) const
122 {
123 while (__lo < __hi)
124 {
125 *__lo = __towlower_l(*__lo, _M_c_locale_ctype);
126 ++__lo;
127 }
128 return __hi;
129 }
130
131 bool
132 ctype<wchar_t>::
133 do_is(mask __m, char_type __c) const
134 {
135 return static_cast<bool>(__iswctype_l(__c, _M_convert_to_wmask(__m),
136 _M_c_locale_ctype));
137 }
138
139 const wchar_t*
140 ctype<wchar_t>::
141 do_is(const wchar_t* __lo, const wchar_t* __hi, mask* __m) const
142 {
143 while (__lo < __hi && !this->do_is(*__m, *__lo))
144 ++__lo;
145 return __lo;
146 }
147
148 const wchar_t*
149 ctype<wchar_t>::
150 do_scan_is(mask __m, const wchar_t* __lo, const wchar_t* __hi) const
151 {
152 while (__lo < __hi && !this->do_is(__m, *__lo))
153 ++__lo;
154 return __lo;
155 }
156
157 const wchar_t*
158 ctype<wchar_t>::
159 do_scan_not(mask __m, const char_type* __lo, const char_type* __hi) const
160 {
161 while (__lo < __hi && this->do_is(__m, *__lo) != 0)
162 ++__lo;
163 return __lo;
164 }
165
166 wchar_t
167 ctype<wchar_t>::
168 do_widen(char __c) const
169 { return btowc(__c); }
170
171 const char*
172 ctype<wchar_t>::
173 do_widen(const char* __lo, const char* __hi, wchar_t* __dest) const
174 {
175 mbstate_t __state;
176 memset(static_cast<void*>(&__state), 0, sizeof(mbstate_t));
177 mbsrtowcs(__dest, &__lo, __hi - __lo, &__state);
178 return __hi;
179 }
180
181 char
182 ctype<wchar_t>::
183 do_narrow(wchar_t __wc, char __dfault) const
184 {
185 int __c = wctob(__wc);
186 return (__c == EOF ? __dfault : static_cast<char>(__c));
187 }
188
189 const wchar_t*
190 ctype<wchar_t>::
191 do_narrow(const wchar_t* __lo, const wchar_t* __hi, char __dfault,
192 char* __dest) const
193 {
194 size_t __offset = 0;
195 while (true)
196 {
197 const wchar_t* __start = __lo + __offset;
198 size_t __len = __hi - __start;
199
200 mbstate_t __state;
201 memset(static_cast<void*>(&__state), 0, sizeof(mbstate_t));
202 size_t __con = wcsrtombs(__dest + __offset, &__start, __len, &__state);
203 if (__con != __len && __start != 0)
204 {
205 __offset = __start - __lo;
206 __dest[__offset++] = __dfault;
207 }
208 else
209 break;
210 }
211 return __hi;
212 }
213 #endif // _GLIBCPP_USE_WCHAR_T
214 }