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