OSDN Git Service

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