Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4680 right-hear 1
// Character Traits for use by standard string and iostream -*- C++ -*-
2
 
3
// Copyright (C) 1997, 1998, 1999, 2000, 2001 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: 21  Strings library
32
//
33
 
34
#ifndef _CPP_BITS_CHAR_TRAITS_H
35
#define _CPP_BITS_CHAR_TRAITS_H 1
36
 
37
#pragma GCC system_header
38
 
39
#include  	// For memmove, memset, memchr
40
#include  		// For streampos
41
 
42
namespace std
43
{
44
  // 21.1.2 Basis for explicit _Traits specialization
45
  // NB: That for any given actual character type this definition is
46
  // probably wrong.
47
  template
48
    struct char_traits
49
    {
50
      typedef _CharT 		char_type;
51
      // Unsigned as wint_t in unsigned.
52
      typedef unsigned long  	int_type;
53
      typedef streampos 	pos_type;
54
      typedef streamoff 	off_type;
55
      typedef mbstate_t 	state_type;
56
 
57
      static void
58
      assign(char_type& __c1, const char_type& __c2)
59
      { __c1 = __c2; }
60
 
61
      static bool
62
      eq(const char_type& __c1, const char_type& __c2)
63
      { return __c1 == __c2; }
64
 
65
      static bool
66
      lt(const char_type& __c1, const char_type& __c2)
67
      { return __c1 < __c2; }
68
 
69
      static int
70
      compare(const char_type* __s1, const char_type* __s2, size_t __n)
71
      {
72
	for (size_t __i = 0; __i < __n; ++__i)
73
	  if (!eq(__s1[__i], __s2[__i]))
74
	    return lt(__s1[__i], __s2[__i]) ? -1 : 1;
75
	return 0;
76
      }
77
 
78
      static size_t
79
      length(const char_type* __s)
80
      {
81
	const char_type* __p = __s;
82
	while (*__p) ++__p;
83
	return (__p - __s);
84
      }
85
 
86
      static const char_type*
87
      find(const char_type* __s, size_t __n, const char_type& __a)
88
      {
89
	for (const char_type* __p = __s; size_t(__p - __s) < __n; ++__p)
90
	  if (*__p == __a) return __p;
91
	return 0;
92
      }
93
 
94
      static char_type*
95
      move(char_type* __s1, const char_type* __s2, size_t __n)
96
      { return (char_type*) memmove(__s1, __s2, __n * sizeof(char_type)); }
97
 
98
      static char_type*
99
      copy(char_type* __s1, const char_type* __s2, size_t __n)
100
      { return (char_type*) memcpy(__s1, __s2, __n * sizeof(char_type)); }
101
 
102
      static char_type*
103
      assign(char_type* __s, size_t __n, char_type __a)
104
      {
105
	for (char_type* __p = __s; __p < __s + __n; ++__p)
106
	  assign(*__p, __a);
107
        return __s;
108
      }
109
 
110
      static char_type
111
      to_char_type(const int_type& __c)
112
      { return char_type(__c); }
113
 
114
      static int_type
115
      to_int_type(const char_type& __c) { return int_type(__c); }
116
 
117
      static bool
118
      eq_int_type(const int_type& __c1, const int_type& __c2)
119
      { return __c1 == __c2; }
120
 
121
      static int_type
122
      eof() { return static_cast(-1); }
123
 
124
      static int_type
125
      not_eof(const int_type& __c)
126
      { return eq_int_type(__c, eof()) ? int_type(0) : __c; }
127
    };
128
 
129
 
130
  // 21.1.4  char_traits specializations
131
  template<>
132
    struct char_traits
133
    {
134
      typedef char 		char_type;
135
      typedef int 	        int_type;
136
      typedef streampos 	pos_type;
137
      typedef streamoff 	off_type;
138
      typedef mbstate_t 	state_type;
139
 
140
      static void
141
      assign(char_type& __c1, const char_type& __c2)
142
      { __c1 = __c2; }
143
 
144
      static bool
145
      eq(const char_type& __c1, const char_type& __c2)
146
      { return __c1 == __c2; }
147
 
148
      static bool
149
      lt(const char_type& __c1, const char_type& __c2)
150
      { return __c1 < __c2; }
151
 
152
      static int
153
      compare(const char_type* __s1, const char_type* __s2, size_t __n)
154
      { return memcmp(__s1, __s2, __n); }
155
 
156
      static size_t
157
      length(const char_type* __s)
158
      { return strlen(__s); }
159
 
160
      static const char_type*
161
      find(const char_type* __s, size_t __n, const char_type& __a)
162
      { return static_cast(memchr(__s, __a, __n)); }
163
 
164
      static char_type*
165
      move(char_type* __s1, const char_type* __s2, size_t __n)
166
      { return static_cast(memmove(__s1, __s2, __n)); }
167
 
168
      static char_type*
169
      copy(char_type* __s1, const char_type* __s2, size_t __n)
170
      {  return static_cast(memcpy(__s1, __s2, __n)); }
171
 
172
      static char_type*
173
      assign(char_type* __s, size_t __n, char_type __a)
174
      { return static_cast(memset(__s, __a, __n)); }
175
 
176
      static char_type
177
      to_char_type(const int_type& __c)
178
      { return static_cast(__c); }
179
 
180
      // To keep both the byte 0xff and the eof symbol 0xffffffff
181
      // from ending up as 0xffffffff.
182
      static int_type
183
      to_int_type(const char_type& __c)
184
      { return static_cast(static_cast(__c)); }
185
 
186
      static bool
187
      eq_int_type(const int_type& __c1, const int_type& __c2)
188
      { return __c1 == __c2; }
189
 
190
      static int_type
191
      eof() { return static_cast(EOF); }
192
 
193
      static int_type
194
      not_eof(const int_type& __c)
195
      { return (__c == eof()) ? 0 : __c; }
196
  };
197
 
198
 
199
#ifdef _GLIBCPP_USE_WCHAR_T
200
  template<>
201
    struct char_traits
202
    {
203
      typedef wchar_t 		char_type;
204
      typedef wint_t 		int_type;
205
      typedef streamoff 	off_type;
206
      typedef wstreampos 	pos_type;
207
      typedef mbstate_t 	state_type;
208
 
209
      static void
210
      assign(char_type& __c1, const char_type& __c2)
211
      { __c1 = __c2; }
212
 
213
      static bool
214
      eq(const char_type& __c1, const char_type& __c2)
215
      { return __c1 == __c2; }
216
 
217
      static bool
218
      lt(const char_type& __c1, const char_type& __c2)
219
      { return __c1 < __c2; }
220
 
221
      static int
222
      compare(const char_type* __s1, const char_type* __s2, size_t __n)
223
      { return wmemcmp(__s1, __s2, __n); }
224
 
225
      static size_t
226
      length(const char_type* __s)
227
      { return wcslen(__s); }
228
 
229
      static const char_type*
230
      find(const char_type* __s, size_t __n, const char_type& __a)
231
      { return wmemchr(__s, __a, __n); }
232
 
233
      static char_type*
234
      move(char_type* __s1, const char_type* __s2, int_type __n)
235
      { return wmemmove(__s1, __s2, __n); }
236
 
237
      static char_type*
238
      copy(char_type* __s1, const char_type* __s2, size_t __n)
239
      { return wmemcpy(__s1, __s2, __n); }
240
 
241
      static char_type*
242
      assign(char_type* __s, size_t __n, char_type __a)
243
      { return wmemset(__s, __a, __n); }
244
 
245
      static char_type
246
      to_char_type(const int_type& __c) { return char_type(__c); }
247
 
248
      static int_type
249
      to_int_type(const char_type& __c) { return int_type(__c); }
250
 
251
      static bool
252
      eq_int_type(const int_type& __c1, const int_type& __c2)
253
      { return __c1 == __c2; }
254
 
255
      static int_type
256
      eof() { return static_cast(WEOF); }
257
 
258
      static int_type
259
      not_eof(const int_type& __c)
260
      { return eq_int_type(__c, eof()) ? 0 : __c; }
261
  };
262
#endif //_GLIBCPP_USE_WCHAR_T
263
 
264
  template
265
    struct _Char_traits_match
266
    {
267
      _CharT _M_c;
268
      _Char_traits_match(_CharT const& __c) : _M_c(__c) { }
269
 
270
      bool
271
      operator()(_CharT const& __a) { return _Traits::eq(_M_c, __a); }
272
    };
273
} // namespace std
274
 
275
#endif