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