1135446Strhodes// Locale support -*- C++ -*-
2262706Serwin
3135446Strhodes// Copyright (C) 2000, 2003 Free Software Foundation, Inc.
4135446Strhodes//
5193149Sdougb// This file is part of the GNU ISO C++ Library.  This library is free
6135446Strhodes// software; you can redistribute it and/or modify it under the
7135446Strhodes// terms of the GNU General Public License as published by the
8135446Strhodes// Free Software Foundation; either version 2, or (at your option)
9135446Strhodes// any later version.
10135446Strhodes
11135446Strhodes// This library is distributed in the hope that it will be useful,
12135446Strhodes// but WITHOUT ANY WARRANTY; without even the implied warranty of
13135446Strhodes// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14135446Strhodes// GNU General Public License for more details.
15135446Strhodes
16135446Strhodes// You should have received a copy of the GNU General Public License along
17135446Strhodes// with this library; see the file COPYING.  If not, write to the Free
18234010Sdougb// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19135446Strhodes// USA.
20170222Sdougb
21135446Strhodes// As a special exception, you may use this file as part of a free software
22170222Sdougb// library without restriction.  Specifically, if other files instantiate
23170222Sdougb// templates or use macros or inline functions from this file, or you compile
24170222Sdougb// this file and link it with other files to produce an executable, this
25170222Sdougb// file does not by itself cause the resulting executable to be covered by
26170222Sdougb// the GNU General Public License.  This exception does not however
27193149Sdougb// invalidate any other reasons why the executable file might be covered by
28170222Sdougb// the GNU General Public License.
29170222Sdougb
30193149Sdougb/** @file ctype_inline.h
31170222Sdougb *  This is an internal header file, included by other library headers.
32170222Sdougb *  You should not attempt to use it directly.
33193149Sdougb */
34170222Sdougb
35170222Sdougb//
36193149Sdougb// ISO C++ 14882: 22.1  Locales
37170222Sdougb//
38193149Sdougb
39170222Sdougb// ctype bits to be inlined go here. Non-inlinable (ie virtual do_*)
40170222Sdougb// functions go in ctype.cc
41170222Sdougb
42193149Sdougb// The following definitions are portable, but insanely slow. If one
43170222Sdougb// cares at all about performance, then specialized ctype
44170222Sdougb// functionality should be added for the native os in question: see
45170222Sdougb// the config/os/bits/ctype_*.h files.
46193149Sdougb
47170222Sdougb// Constructing a synthetic "C" table should be seriously considered...
48193149Sdougb
49170222Sdougb_GLIBCXX_BEGIN_NAMESPACE(std)
50193149Sdougb
51170222Sdougb  bool
52193149Sdougb  ctype<char>::
53170222Sdougb  is(mask __m, char __c) const
54170222Sdougb  {
55170222Sdougb    if (_M_table)
56135446Strhodes      return _M_table[static_cast<unsigned char>(__c)] & __m;
57135446Strhodes    else
58135446Strhodes      {
59135446Strhodes	bool __ret = false;
60135446Strhodes	const size_t __bitmasksize = 15;
61135446Strhodes	size_t __bitcur = 0; // Lowest bitmask in ctype_base == 0
62135446Strhodes	for (; __bitcur <= __bitmasksize; ++__bitcur)
63135446Strhodes	  {
64135446Strhodes	    const mask __bit = static_cast<mask>(1 << __bitcur);
65135446Strhodes	    if (__m & __bit)
66135446Strhodes	      {
67135446Strhodes		bool __testis;
68135446Strhodes		switch (__bit)
69135446Strhodes		  {
70135446Strhodes		  case space:
71135446Strhodes		    __testis = isspace(__c);
72135446Strhodes		    break;
73135446Strhodes		  case print:
74135446Strhodes		    __testis = isprint(__c);
75135446Strhodes		    break;
76135446Strhodes		  case cntrl:
77135446Strhodes		    __testis = iscntrl(__c);
78135446Strhodes		    break;
79135446Strhodes		  case upper:
80135446Strhodes		    __testis = isupper(__c);
81135446Strhodes		    break;
82135446Strhodes		  case lower:
83135446Strhodes		    __testis = islower(__c);
84135446Strhodes		    break;
85135446Strhodes		  case alpha:
86135446Strhodes		    __testis = isalpha(__c);
87135446Strhodes		    break;
88135446Strhodes		  case digit:
89135446Strhodes		    __testis = isdigit(__c);
90135446Strhodes		    break;
91135446Strhodes		  case punct:
92135446Strhodes		    __testis = ispunct(__c);
93135446Strhodes		    break;
94135446Strhodes		  case xdigit:
95135446Strhodes		    __testis = isxdigit(__c);
96135446Strhodes		    break;
97135446Strhodes		  case alnum:
98135446Strhodes		    __testis = isalnum(__c);
99135446Strhodes		    break;
100135446Strhodes		  case graph:
101135446Strhodes		    __testis = isgraph(__c);
102135446Strhodes		    break;
103135446Strhodes		  default:
104135446Strhodes		    __testis = false;
105135446Strhodes		    break;
106135446Strhodes		  }
107135446Strhodes		__ret |= __testis;
108135446Strhodes	      }
109135446Strhodes	  }
110135446Strhodes	return __ret;
111135446Strhodes      }
112135446Strhodes  }
113135446Strhodes
114135446Strhodes  const char*
115135446Strhodes  ctype<char>::
116135446Strhodes  is(const char* __low, const char* __high, mask* __vec) const
117135446Strhodes  {
118135446Strhodes    if (_M_table)
119135446Strhodes      while (__low < __high)
120135446Strhodes	*__vec++ = _M_table[static_cast<unsigned char>(*__low++)];
121135446Strhodes    else
122135446Strhodes      {
123135446Strhodes	// Highest bitmask in ctype_base == 10.
124135446Strhodes	const size_t __bitmasksize = 15;
125135446Strhodes	for (;__low < __high; ++__vec, ++__low)
126135446Strhodes	  {
127135446Strhodes	    mask __m = 0;
128135446Strhodes	    // Lowest bitmask in ctype_base == 0
129170222Sdougb	    size_t __i = 0;
130135446Strhodes	    for (;__i <= __bitmasksize; ++__i)
131135446Strhodes	      {
132135446Strhodes		const mask __bit = static_cast<mask>(1 << __i);
133135446Strhodes		if (this->is(__bit, *__low))
134135446Strhodes		  __m |= __bit;
135135446Strhodes	      }
136135446Strhodes	    *__vec = __m;
137135446Strhodes	  }
138135446Strhodes      }
139135446Strhodes    return __high;
140135446Strhodes  }
141135446Strhodes
142135446Strhodes  const char*
143135446Strhodes  ctype<char>::
144170222Sdougb  scan_is(mask __m, const char* __low, const char* __high) const
145135446Strhodes  {
146135446Strhodes    if (_M_table)
147135446Strhodes      while (__low < __high
148135446Strhodes	     && !(_M_table[static_cast<unsigned char>(*__low)] & __m))
149135446Strhodes	++__low;
150135446Strhodes    else
151135446Strhodes      while (__low < __high && !this->is(__m, *__low))
152135446Strhodes	++__low;
153135446Strhodes    return __low;
154135446Strhodes  }
155135446Strhodes
156135446Strhodes  const char*
157135446Strhodes  ctype<char>::
158135446Strhodes  scan_not(mask __m, const char* __low, const char* __high) const
159135446Strhodes  {
160135446Strhodes    if (_M_table)
161135446Strhodes      while (__low < __high
162135446Strhodes	     && (_M_table[static_cast<unsigned char>(*__low)] & __m) != 0)
163135446Strhodes	++__low;
164170222Sdougb    else
165135446Strhodes      while (__low < __high && this->is(__m, *__low) != 0)
166135446Strhodes	++__low;
167135446Strhodes    return __low;
168135446Strhodes  }
169135446Strhodes
170135446Strhodes_GLIBCXX_END_NAMESPACE
171135446Strhodes