STLdoc
STLdocumentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Functions
char_traits.h File Reference
#include <bits/stl_algobase.h>
#include <bits/postypes.h>
#include <cwchar>

Go to the source code of this file.

Functions

namespace __gnu_cxx _GLIBCXX_VISIBILITY (default)
 

Detailed Description

This is an internal header file, included by other library headers. Do not attempt to use it directly. {string}

Function Documentation

namespace __gnu_cxx _GLIBCXX_VISIBILITY ( default  )

Mapping from character type to associated types.

Note
This is an implementation class for the generic version of char_traits. It defines int_type, off_type, pos_type, and state_type. By default these are unsigned long, streamoff, streampos, and mbstate_t. Users who need a different set of types, but who don't need to change the definitions of any function defined in char_traits, can specialize __gnu_cxx::_Char_types while leaving __gnu_cxx::char_traits alone.

Base class used to implement std::char_traits.

Note
For any given actual character type, this definition is probably wrong. (Most of the member functions are likely to be right, but the int_type and state_type typedefs, and the eof() member function, are likely to be wrong.) The reason this class exists is so users can specialize it. Classes in namespace std may not be specialized for fundamental types, but classes in namespace __gnu_cxx may be.

See http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt05ch13s03.html for advice on how to make use of this class for unusual character types. Also, check out include/ext/pod_char_traits.h.

Basis for explicit traits specializations.

Note
For any given actual character type, this definition is probably wrong. Since this is just a thin wrapper around __gnu_cxx::char_traits, it is possible to achieve a more appropriate definition by specializing __gnu_cxx::char_traits.

See http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt05ch13s03.html for advice on how to make use of this class for unusual character types. Also, check out include/ext/pod_char_traits.h.

21.1.3.1 char_traits specializations

44 {
45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
46 
57  template<typename _CharT>
58  struct _Char_types
59  {
60  typedef unsigned long int_type;
61  typedef std::streampos pos_type;
62  typedef std::streamoff off_type;
63  typedef std::mbstate_t state_type;
64  };
65 
66 
82  template<typename _CharT>
83  struct char_traits
84  {
85  typedef _CharT char_type;
86  typedef typename _Char_types<_CharT>::int_type int_type;
87  typedef typename _Char_types<_CharT>::pos_type pos_type;
88  typedef typename _Char_types<_CharT>::off_type off_type;
89  typedef typename _Char_types<_CharT>::state_type state_type;
90 
91  static void
92  assign(char_type& __c1, const char_type& __c2)
93  { __c1 = __c2; }
94 
95  static _GLIBCXX_CONSTEXPR bool
96  eq(const char_type& __c1, const char_type& __c2)
97  { return __c1 == __c2; }
98 
99  static _GLIBCXX_CONSTEXPR bool
100  lt(const char_type& __c1, const char_type& __c2)
101  { return __c1 < __c2; }
102 
103  static int
104  compare(const char_type* __s1, const char_type* __s2, std::size_t __n);
105 
106  static std::size_t
107  length(const char_type* __s);
108 
109  static const char_type*
110  find(const char_type* __s, std::size_t __n, const char_type& __a);
111 
112  static char_type*
113  move(char_type* __s1, const char_type* __s2, std::size_t __n);
114 
115  static char_type*
116  copy(char_type* __s1, const char_type* __s2, std::size_t __n);
117 
118  static char_type*
119  assign(char_type* __s, std::size_t __n, char_type __a);
120 
121  static _GLIBCXX_CONSTEXPR char_type
122  to_char_type(const int_type& __c)
123  { return static_cast<char_type>(__c); }
124 
125  static _GLIBCXX_CONSTEXPR int_type
126  to_int_type(const char_type& __c)
127  { return static_cast<int_type>(__c); }
128 
129  static _GLIBCXX_CONSTEXPR bool
130  eq_int_type(const int_type& __c1, const int_type& __c2)
131  { return __c1 == __c2; }
132 
133  static _GLIBCXX_CONSTEXPR int_type
134  eof()
135  { return static_cast<int_type>(_GLIBCXX_STDIO_EOF); }
136 
137  static _GLIBCXX_CONSTEXPR int_type
138  not_eof(const int_type& __c)
139  { return !eq_int_type(__c, eof()) ? __c : to_int_type(char_type()); }
140  };
141 
142  template<typename _CharT>
143  int
144  char_traits<_CharT>::
145  compare(const char_type* __s1, const char_type* __s2, std::size_t __n)
146  {
147  for (std::size_t __i = 0; __i < __n; ++__i)
148  if (lt(__s1[__i], __s2[__i]))
149  return -1;
150  else if (lt(__s2[__i], __s1[__i]))
151  return 1;
152  return 0;
153  }
154 
155  template<typename _CharT>
157  char_traits<_CharT>::
158  length(const char_type* __p)
159  {
160  std::size_t __i = 0;
161  while (!eq(__p[__i], char_type()))
162  ++__i;
163  return __i;
164  }
165 
166  template<typename _CharT>
167  const typename char_traits<_CharT>::char_type*
168  char_traits<_CharT>::
169  find(const char_type* __s, std::size_t __n, const char_type& __a)
170  {
171  for (std::size_t __i = 0; __i < __n; ++__i)
172  if (eq(__s[__i], __a))
173  return __s + __i;
174  return 0;
175  }
176 
177  template<typename _CharT>
178  typename char_traits<_CharT>::char_type*
179  char_traits<_CharT>::
180  move(char_type* __s1, const char_type* __s2, std::size_t __n)
181  {
182  return static_cast<_CharT*>(__builtin_memmove(__s1, __s2,
183  __n * sizeof(char_type)));
184  }
185 
186  template<typename _CharT>
187  typename char_traits<_CharT>::char_type*
188  char_traits<_CharT>::
189  copy(char_type* __s1, const char_type* __s2, std::size_t __n)
190  {
191  // NB: Inline std::copy so no recursive dependencies.
192  std::copy(__s2, __s2 + __n, __s1);
193  return __s1;
194  }
195 
196  template<typename _CharT>
197  typename char_traits<_CharT>::char_type*
198  char_traits<_CharT>::
199  assign(char_type* __s, std::size_t __n, char_type __a)
200  {
201  // NB: Inline std::fill_n so no recursive dependencies.
202  std::fill_n(__s, __n, __a);
203  return __s;
204  }
205 
206 _GLIBCXX_END_NAMESPACE_VERSION
207 } // namespace
__SIZE_TYPE__ size_t
Definition: stddef.h:212