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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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
39namespace 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	}
52    }
53
54#ifdef _GLIBCXX_USE_WCHAR_T
55  ctype<wchar_t>::__wmask_type
56  ctype<wchar_t>::_M_convert_to_wmask(const mask __m) const
57  {
58    // Darwin uses the same codes for 'char' as 'wchar_t', so this routine
59    // never gets called.
60    return __m;
61  };
62
63  wchar_t
64  ctype<wchar_t>::do_toupper(wchar_t __c) const
65  { return towupper(__c); }
66
67  const wchar_t*
68  ctype<wchar_t>::do_toupper(wchar_t* __lo, const wchar_t* __hi) const
69  {
70    while (__lo < __hi)
71      {
72        *__lo = towupper(*__lo);
73        ++__lo;
74      }
75    return __hi;
76  }
77
78  wchar_t
79  ctype<wchar_t>::do_tolower(wchar_t __c) const
80  { return towlower(__c); }
81
82  const wchar_t*
83  ctype<wchar_t>::do_tolower(wchar_t* __lo, const wchar_t* __hi) const
84  {
85    while (__lo < __hi)
86      {
87        *__lo = towlower(*__lo);
88        ++__lo;
89      }
90    return __hi;
91  }
92
93  wchar_t
94  ctype<wchar_t>::
95  do_widen(char __c) const
96  { return _M_widen[static_cast<unsigned char>(__c)]; }
97
98  const char*
99  ctype<wchar_t>::
100  do_widen(const char* __lo, const char* __hi, wchar_t* __dest) const
101  {
102    while (__lo < __hi)
103      {
104	*__dest = _M_widen[static_cast<unsigned char>(*__lo)];
105	++__lo;
106	++__dest;
107      }
108    return __hi;
109  }
110
111  char
112  ctype<wchar_t>::
113  do_narrow(wchar_t __wc, char __dfault) const
114  {
115    if (__wc >= 0 && __wc < 128 && _M_narrow_ok)
116      return _M_narrow[__wc];
117    const int __c = wctob(__wc);
118    return (__c == EOF ? __dfault : static_cast<char>(__c));
119  }
120
121  const wchar_t*
122  ctype<wchar_t>::
123  do_narrow(const wchar_t* __lo, const wchar_t* __hi, char __dfault,
124	    char* __dest) const
125  {
126    if (_M_narrow_ok)
127      while (__lo < __hi)
128	{
129	  if (*__lo >= 0 && *__lo < 128)
130	    *__dest = _M_narrow[*__lo];
131	  else
132	    {
133	      const int __c = wctob(*__lo);
134	      *__dest = (__c == EOF ? __dfault : static_cast<char>(__c));
135	    }
136	  ++__lo;
137	  ++__dest;
138	}
139    else
140      while (__lo < __hi)
141	{
142	  const int __c = wctob(*__lo);
143	  *__dest = (__c == EOF ? __dfault : static_cast<char>(__c));
144	  ++__lo;
145	  ++__dest;
146	}
147    return __hi;
148  }
149
150  void
151  ctype<wchar_t>::_M_initialize_ctype()
152  {
153    wint_t __i;
154    for (__i = 0; __i < 128; ++__i)
155      {
156	const int __c = wctob(__i);
157	if (__c == EOF)
158	  break;
159	else
160	  _M_narrow[__i] = static_cast<char>(__c);
161      }
162    if (__i == 128)
163      _M_narrow_ok = true;
164    else
165      _M_narrow_ok = false;
166    for (size_t __i = 0;
167	 __i < sizeof(_M_widen) / sizeof(wint_t); ++__i)
168      _M_widen[__i] = btowc(__i);
169  }
170#endif //  _GLIBCXX_USE_WCHAR_T
171}
172