OSDN Git Service

2004-05-13 Simon Marshall <simon.marshall@misys.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / config / locale / gnu / ctype_members.cc
1 // std::ctype implementation details, GNU version -*- C++ -*-
2
3 // Copyright (C) 2001, 2002, 2003, 2004 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 <bits/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 (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 #ifdef _GLIBCXX_USE_WCHAR_T  
58   ctype<wchar_t>::__wmask_type
59   ctype<wchar_t>::_M_convert_to_wmask(const mask __m) const
60   {
61     __wmask_type __ret;
62     switch (__m)
63       {
64       case space:
65         __ret = __wctype_l("space", _M_c_locale_ctype);
66         break;
67       case print:
68         __ret = __wctype_l("print", _M_c_locale_ctype);
69         break;
70       case cntrl:
71         __ret = __wctype_l("cntrl", _M_c_locale_ctype);
72         break;
73       case upper:
74         __ret = __wctype_l("upper", _M_c_locale_ctype);
75         break;
76       case lower:
77         __ret = __wctype_l("lower", _M_c_locale_ctype);
78         break;
79       case alpha:
80         __ret = __wctype_l("alpha", _M_c_locale_ctype);
81         break;
82       case digit:
83         __ret = __wctype_l("digit", _M_c_locale_ctype);
84         break;
85       case punct:
86         __ret = __wctype_l("punct", _M_c_locale_ctype);
87         break;
88       case xdigit:
89         __ret = __wctype_l("xdigit", _M_c_locale_ctype);
90         break;
91       case alnum:
92         __ret = __wctype_l("alnum", _M_c_locale_ctype);
93         break;
94       case graph:
95         __ret = __wctype_l("graph", _M_c_locale_ctype);
96         break;
97       default:
98         __ret = 0;
99       }
100     return __ret;
101   }
102   
103   wchar_t
104   ctype<wchar_t>::do_toupper(wchar_t __c) const
105   { return __towupper_l(__c, _M_c_locale_ctype); }
106
107   const wchar_t*
108   ctype<wchar_t>::do_toupper(wchar_t* __lo, const wchar_t* __hi) const
109   {
110     while (__lo < __hi)
111       {
112         *__lo = __towupper_l(*__lo, _M_c_locale_ctype);
113         ++__lo;
114       }
115     return __hi;
116   }
117   
118   wchar_t
119   ctype<wchar_t>::do_tolower(wchar_t __c) const
120   { return __towlower_l(__c, _M_c_locale_ctype); }
121   
122   const wchar_t*
123   ctype<wchar_t>::do_tolower(wchar_t* __lo, const wchar_t* __hi) const
124   {
125     while (__lo < __hi)
126       {
127         *__lo = __towlower_l(*__lo, _M_c_locale_ctype);
128         ++__lo;
129       }
130     return __hi;
131   }
132
133   bool
134   ctype<wchar_t>::
135   do_is(mask __m, wchar_t __c) const
136   { 
137     // Highest bitmask in ctype_base == 10, but extra in "C"
138     // library for blank.
139     bool __ret = false;
140     const size_t __bitmasksize = 11; 
141     for (size_t __bitcur = 0; __bitcur <= __bitmasksize; ++__bitcur)
142       if (__m & _M_bit[__bitcur]
143           && __iswctype_l(__c, _M_wmask[__bitcur], _M_c_locale_ctype))
144         {
145           __ret = true;
146           break;
147         }
148     return __ret;    
149   }
150   
151   const wchar_t* 
152   ctype<wchar_t>::
153   do_is(const wchar_t* __lo, const wchar_t* __hi, mask* __vec) const
154   {
155     for (; __lo < __hi; ++__vec, ++__lo)
156       {
157         // Highest bitmask in ctype_base == 10, but extra in "C"
158         // library for blank.
159         const size_t __bitmasksize = 11; 
160         mask __m = 0;
161         for (size_t __bitcur = 0; __bitcur <= __bitmasksize; ++__bitcur)
162           if (__iswctype_l(*__lo, _M_wmask[__bitcur], _M_c_locale_ctype))
163             __m |= _M_bit[__bitcur];
164         *__vec = __m;
165       }
166     return __hi;
167   }
168   
169   const wchar_t* 
170   ctype<wchar_t>::
171   do_scan_is(mask __m, const wchar_t* __lo, const wchar_t* __hi) const
172   {
173     while (__lo < __hi && !this->do_is(__m, *__lo))
174       ++__lo;
175     return __lo;
176   }
177
178   const wchar_t*
179   ctype<wchar_t>::
180   do_scan_not(mask __m, const char_type* __lo, const char_type* __hi) const
181   {
182     while (__lo < __hi && this->do_is(__m, *__lo) != 0)
183       ++__lo;
184     return __lo;
185   }
186
187   wchar_t
188   ctype<wchar_t>::
189   do_widen(char __c) const
190   { return _M_widen[static_cast<unsigned char>(__c)]; }
191
192   const char* 
193   ctype<wchar_t>::
194   do_widen(const char* __lo, const char* __hi, wchar_t* __dest) const
195   {
196     while (__lo < __hi)
197       {
198         *__dest = _M_widen[static_cast<unsigned char>(*__lo)];
199         ++__lo;
200         ++__dest;
201       }
202     return __hi;
203   }
204
205   char
206   ctype<wchar_t>::
207   do_narrow(wchar_t __wc, char __dfault) const
208   {
209     if (__wc >= 0 && __wc < 128 && _M_narrow_ok)
210       return _M_narrow[__wc];
211 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
212     __c_locale __old = __uselocale(_M_c_locale_ctype);
213 #endif
214     const int __c = wctob(__wc);
215 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
216     __uselocale(__old);
217 #endif
218     return (__c == EOF ? __dfault : static_cast<char>(__c)); 
219   }
220
221   const wchar_t*
222   ctype<wchar_t>::
223   do_narrow(const wchar_t* __lo, const wchar_t* __hi, char __dfault, 
224             char* __dest) const
225   {
226 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
227     __c_locale __old = __uselocale(_M_c_locale_ctype);
228 #endif
229     if (_M_narrow_ok)
230       while (__lo < __hi)
231         {
232           if (*__lo >= 0 && *__lo < 128)
233             *__dest = _M_narrow[*__lo];
234           else
235             {
236               const int __c = wctob(*__lo);
237               *__dest = (__c == EOF ? __dfault : static_cast<char>(__c));
238             }
239           ++__lo;
240           ++__dest;
241         }
242     else
243       while (__lo < __hi)
244         {
245           const int __c = wctob(*__lo);
246           *__dest = (__c == EOF ? __dfault : static_cast<char>(__c));
247           ++__lo;
248           ++__dest;
249         }
250 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
251     __uselocale(__old);
252 #endif
253     return __hi;
254   }
255
256   void
257   ctype<wchar_t>::_M_initialize_ctype()
258   {
259 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
260     __c_locale __old = __uselocale(_M_c_locale_ctype);
261 #endif
262     wint_t __i;
263     for (__i = 0; __i < 128; ++__i)
264       {
265         const int __c = wctob(__i);
266         if (__c == EOF)
267           break;
268         else
269           _M_narrow[__i] = static_cast<char>(__c);
270       }
271     if (__i == 128)
272       _M_narrow_ok = true;
273     else
274       _M_narrow_ok = false;
275     for (size_t __j = 0;
276          __j < sizeof(_M_widen) / sizeof(wint_t); ++__j)
277       _M_widen[__j] = btowc(__j);
278
279     for (size_t __k = 0; __k <= 11; ++__k)
280       { 
281         _M_bit[__k] = static_cast<mask>(_ISbit(__k));
282         _M_wmask[__k] = _M_convert_to_wmask(_M_bit[__k]);
283       }
284 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
285     __uselocale(__old);
286 #endif
287   }
288 #endif //  _GLIBCXX_USE_WCHAR_T
289 }