]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/libstdc++-v3/contrib/libstdc++-v3-4.3.3/config/locale/generic/ctype_members.cc
update
[l4.git] / l4 / pkg / libstdc++-v3 / contrib / libstdc++-v3-4.3.3 / 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
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
41 _GLIBCXX_BEGIN_NAMESPACE(std)
42
43   // NB: The other ctype<char> specializations are in src/locale.cc and
44   // various /config/os/* files.
45   ctype_byname<char>::ctype_byname(const char* __s, size_t __refs)
46   : ctype<char>(0, false, __refs) 
47   {     
48     if (std::strcmp(__s, "C") != 0 && std::strcmp(__s, "POSIX") != 0)
49       {
50         this->_S_destroy_c_locale(this->_M_c_locale_ctype);
51         this->_S_create_c_locale(this->_M_c_locale_ctype, __s); 
52       }
53   }
54
55   ctype_byname<char>::~ctype_byname()
56   { }
57
58 #ifdef _GLIBCXX_USE_WCHAR_T  
59   ctype<wchar_t>::__wmask_type
60   ctype<wchar_t>::_M_convert_to_wmask(const mask __m) const
61   {
62     __wmask_type __ret;
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       }
101     return __ret;
102   };
103   
104   wchar_t
105   ctype<wchar_t>::do_toupper(wchar_t __c) const
106   { return towupper(__c); }
107
108   const wchar_t*
109   ctype<wchar_t>::do_toupper(wchar_t* __lo, const wchar_t* __hi) const
110   {
111     while (__lo < __hi)
112       {
113         *__lo = towupper(*__lo);
114         ++__lo;
115       }
116     return __hi;
117   }
118   
119   wchar_t
120   ctype<wchar_t>::do_tolower(wchar_t __c) const
121   { return towlower(__c); }
122   
123   const wchar_t*
124   ctype<wchar_t>::do_tolower(wchar_t* __lo, const wchar_t* __hi) const
125   {
126     while (__lo < __hi)
127       {
128         *__lo = towlower(*__lo);
129         ++__lo;
130       }
131     return __hi;
132   }
133
134   bool
135   ctype<wchar_t>::
136   do_is(mask __m, char_type __c) const
137   { 
138     bool __ret = false;
139     // Generically, 15 (instead of 10) since we don't know the numerical
140     // encoding of the various categories in /usr/include/ctype.h.
141     const size_t __bitmasksize = 15; 
142     for (size_t __bitcur = 0; __bitcur <= __bitmasksize; ++__bitcur)
143       if (__m & _M_bit[__bitcur]
144           && iswctype(__c, _M_wmask[__bitcur]))
145         {
146           __ret = true;
147           break;
148         }
149     return __ret;    
150   }
151   
152   const wchar_t* 
153   ctype<wchar_t>::
154   do_is(const wchar_t* __lo, const wchar_t* __hi, mask* __vec) const
155   {
156     for (;__lo < __hi; ++__vec, ++__lo)
157       {
158         // Generically, 15 (instead of 10) since we don't know the numerical
159         // encoding of the various categories in /usr/include/ctype.h.
160         const size_t __bitmasksize = 15; 
161         mask __m = 0;
162         for (size_t __bitcur = 0; __bitcur <= __bitmasksize; ++__bitcur)
163           if (iswctype(*__lo, _M_wmask[__bitcur]))
164             __m |= _M_bit[__bitcur];
165         *__vec = __m;
166       }
167     return __hi;
168   }
169   
170   const wchar_t* 
171   ctype<wchar_t>::
172   do_scan_is(mask __m, const wchar_t* __lo, const wchar_t* __hi) const
173   {
174     while (__lo < __hi && !this->do_is(__m, *__lo))
175       ++__lo;
176     return __lo;
177   }
178
179   const wchar_t*
180   ctype<wchar_t>::
181   do_scan_not(mask __m, const char_type* __lo, const char_type* __hi) const
182   {
183     while (__lo < __hi && this->do_is(__m, *__lo) != 0)
184       ++__lo;
185     return __lo;
186   }
187
188   wchar_t
189   ctype<wchar_t>::
190   do_widen(char __c) const
191   { return _M_widen[static_cast<unsigned char>(__c)]; }
192   
193   const char* 
194   ctype<wchar_t>::
195   do_widen(const char* __lo, const char* __hi, wchar_t* __dest) const
196   {
197     while (__lo < __hi)
198       {
199         *__dest = _M_widen[static_cast<unsigned char>(*__lo)];
200         ++__lo;
201         ++__dest;
202       }
203     return __hi;
204   }
205
206   char
207   ctype<wchar_t>::
208   do_narrow(wchar_t __wc, char __dfault) const
209   { 
210     if (__wc >= 0 && __wc < 128 && _M_narrow_ok)
211       return _M_narrow[__wc];
212     const int __c = wctob(__wc);
213     return (__c == EOF ? __dfault : static_cast<char>(__c)); 
214   }
215
216   const wchar_t*
217   ctype<wchar_t>::
218   do_narrow(const wchar_t* __lo, const wchar_t* __hi, char __dfault, 
219             char* __dest) const
220   {
221     if (_M_narrow_ok)
222       while (__lo < __hi)
223         {
224           if (*__lo >= 0 && *__lo < 128)
225             *__dest = _M_narrow[*__lo];
226           else
227             {
228               const int __c = wctob(*__lo);
229               *__dest = (__c == EOF ? __dfault : static_cast<char>(__c));
230             }
231           ++__lo;
232           ++__dest;
233         }
234     else
235       while (__lo < __hi)
236         {
237           const int __c = wctob(*__lo);
238           *__dest = (__c == EOF ? __dfault : static_cast<char>(__c));
239           ++__lo;
240           ++__dest;
241         }
242     return __hi;
243   }
244
245   void
246   ctype<wchar_t>::_M_initialize_ctype()
247   {
248     wint_t __i;
249     for (__i = 0; __i < 128; ++__i)
250       {
251         const int __c = wctob(__i);
252         if (__c == EOF)
253           break;
254         else
255           _M_narrow[__i] = static_cast<char>(__c);
256       }
257     if (__i == 128)
258       _M_narrow_ok = true;
259     else
260       _M_narrow_ok = false;
261     for (size_t __i = 0;
262          __i < sizeof(_M_widen) / sizeof(wint_t); ++__i)
263       _M_widen[__i] = btowc(__i);
264
265     for (size_t __i = 0; __i <= 15; ++__i)
266       { 
267         _M_bit[__i] = static_cast<mask>(1 << __i);
268         _M_wmask[__i] = _M_convert_to_wmask(_M_bit[__i]);
269       }  
270   }
271 #endif //  _GLIBCXX_USE_WCHAR_T
272
273 _GLIBCXX_END_NAMESPACE