Whole document tree
    

Whole document tree

char_traits.h Source File
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

char_traits.h

Go to the documentation of this file.
00001 // Character Traits for use by standard string and iostream -*- C++ -*-
00002 
00003 // Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 2, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // You should have received a copy of the GNU General Public License along
00017 // with this library; see the file COPYING.  If not, write to the Free
00018 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
00019 // USA.
00020 
00021 // As a special exception, you may use this file as part of a free software
00022 // library without restriction.  Specifically, if other files instantiate
00023 // templates or use macros or inline functions from this file, or you compile
00024 // this file and link it with other files to produce an executable, this
00025 // file does not by itself cause the resulting executable to be covered by
00026 // the GNU General Public License.  This exception does not however
00027 // invalidate any other reasons why the executable file might be covered by
00028 // the GNU General Public License.
00029 
00030 //
00031 // ISO C++ 14882: 21  Strings library
00032 //
00033 
00034 #ifndef _CPP_BITS_CHAR_TRAITS_H
00035 #define _CPP_BITS_CHAR_TRAITS_H 1
00036 
00037 #pragma GCC system_header
00038 
00039 #include <bits/std_cstring.h>   // For memmove, memset, memchr
00040 #include <bits/fpos.h>      // For streampos
00041 
00042 namespace std 
00043 {
00047   template<class _CharT>
00048     struct char_traits
00049     {
00050       typedef _CharT        char_type;
00051       // Unsigned as wint_t in unsigned.
00052       typedef unsigned long     int_type;
00053       typedef streampos     pos_type;
00054       typedef streamoff     off_type;
00055       typedef mbstate_t     state_type;
00056       
00057       static void 
00058       assign(char_type& __c1, const char_type& __c2)
00059       { __c1 = __c2; }
00060 
00061       static bool 
00062       eq(const char_type& __c1, const char_type& __c2)
00063       { return __c1 == __c2; }
00064 
00065       static bool 
00066       lt(const char_type& __c1, const char_type& __c2)
00067       { return __c1 < __c2; }
00068 
00069       static int 
00070       compare(const char_type* __s1, const char_type* __s2, size_t __n)
00071       { 
00072     for (size_t __i = 0; __i < __n; ++__i)
00073       if (!eq(__s1[__i], __s2[__i]))
00074         return lt(__s1[__i], __s2[__i]) ? -1 : 1;
00075     return 0; 
00076       }
00077 
00078       static size_t
00079       length(const char_type* __s)
00080       { 
00081     const char_type* __p = __s; 
00082     while (*__p) ++__p; 
00083     return (__p - __s); 
00084       }
00085 
00086       static const char_type* 
00087       find(const char_type* __s, size_t __n, const char_type& __a)
00088       { 
00089     for (const char_type* __p = __s; size_t(__p - __s) < __n; ++__p)
00090       if (*__p == __a) return __p;
00091     return 0;
00092       }
00093 
00094       static char_type* 
00095       move(char_type* __s1, const char_type* __s2, size_t __n)
00096       { return (char_type*) memmove(__s1, __s2, __n * sizeof(char_type)); }
00097 
00098       static char_type* 
00099       copy(char_type* __s1, const char_type* __s2, size_t __n)
00100       { return (char_type*) memcpy(__s1, __s2, __n * sizeof(char_type)); }
00101 
00102       static char_type* 
00103       assign(char_type* __s, size_t __n, char_type __a)
00104       { 
00105     for (char_type* __p = __s; __p < __s + __n; ++__p) 
00106       assign(*__p, __a);
00107         return __s; 
00108       }
00109 
00110       static char_type 
00111       to_char_type(const int_type& __c)
00112       { return char_type(__c); }
00113 
00114       static int_type 
00115       to_int_type(const char_type& __c) { return int_type(__c); }
00116 
00117       static bool 
00118       eq_int_type(const int_type& __c1, const int_type& __c2)
00119       { return __c1 == __c2; }
00120 
00121       static int_type 
00122       eof() { return static_cast<int_type>(-1); }
00123 
00124       static int_type 
00125       not_eof(const int_type& __c)
00126       { return eq_int_type(__c, eof()) ? int_type(0) : __c; }
00127     };
00128 
00129 
00131   template<>
00132     struct char_traits<char>
00133     {
00134       typedef char      char_type;
00135       typedef int           int_type;
00136       typedef streampos     pos_type;
00137       typedef streamoff     off_type;
00138       typedef mbstate_t     state_type;
00139 
00140       static void 
00141       assign(char_type& __c1, const char_type& __c2)
00142       { __c1 = __c2; }
00143 
00144       static bool 
00145       eq(const char_type& __c1, const char_type& __c2)
00146       { return __c1 == __c2; }
00147 
00148       static bool 
00149       lt(const char_type& __c1, const char_type& __c2)
00150       { return __c1 < __c2; }
00151 
00152       static int 
00153       compare(const char_type* __s1, const char_type* __s2, size_t __n)
00154       { return memcmp(__s1, __s2, __n); }
00155 
00156       static size_t
00157       length(const char_type* __s)
00158       { return strlen(__s); }
00159 
00160       static const char_type* 
00161       find(const char_type* __s, size_t __n, const char_type& __a)
00162       { return static_cast<const char_type*>(memchr(__s, __a, __n)); }
00163 
00164       static char_type* 
00165       move(char_type* __s1, const char_type* __s2, size_t __n)
00166       { return static_cast<char_type*>(memmove(__s1, __s2, __n)); }
00167 
00168       static char_type* 
00169       copy(char_type* __s1, const char_type* __s2, size_t __n)
00170       {  return static_cast<char_type*>(memcpy(__s1, __s2, __n)); }
00171 
00172       static char_type* 
00173       assign(char_type* __s, size_t __n, char_type __a)
00174       { return static_cast<char_type*>(memset(__s, __a, __n)); }
00175 
00176       static char_type 
00177       to_char_type(const int_type& __c)
00178       { return static_cast<char_type>(__c); }
00179 
00180       // To keep both the byte 0xff and the eof symbol 0xffffffff
00181       // from ending up as 0xffffffff.
00182       static int_type 
00183       to_int_type(const char_type& __c)
00184       { return static_cast<int_type>(static_cast<unsigned char>(__c)); }
00185 
00186       static bool 
00187       eq_int_type(const int_type& __c1, const int_type& __c2)
00188       { return __c1 == __c2; }
00189 
00190       static int_type 
00191       eof() { return static_cast<int_type>(EOF); }
00192 
00193       static int_type 
00194       not_eof(const int_type& __c)
00195       { return (__c == eof()) ? 0 : __c; }
00196   };
00197 
00198 
00199 #ifdef _GLIBCPP_USE_WCHAR_T
00200   template<>
00201     struct char_traits<wchar_t>
00202     {
00203       typedef wchar_t       char_type;
00204       typedef wint_t        int_type;
00205       typedef streamoff     off_type;
00206       typedef wstreampos    pos_type;
00207       typedef mbstate_t     state_type;
00208       
00209       static void 
00210       assign(char_type& __c1, const char_type& __c2)
00211       { __c1 = __c2; }
00212 
00213       static bool 
00214       eq(const char_type& __c1, const char_type& __c2)
00215       { return __c1 == __c2; }
00216 
00217       static bool 
00218       lt(const char_type& __c1, const char_type& __c2)
00219       { return __c1 < __c2; }
00220 
00221       static int 
00222       compare(const char_type* __s1, const char_type* __s2, size_t __n)
00223       { return wmemcmp(__s1, __s2, __n); }
00224 
00225       static size_t
00226       length(const char_type* __s)
00227       { return wcslen(__s); }
00228 
00229       static const char_type* 
00230       find(const char_type* __s, size_t __n, const char_type& __a)
00231       { return wmemchr(__s, __a, __n); }
00232 
00233       static char_type* 
00234       move(char_type* __s1, const char_type* __s2, int_type __n)
00235       { return wmemmove(__s1, __s2, __n); }
00236 
00237       static char_type* 
00238       copy(char_type* __s1, const char_type* __s2, size_t __n)
00239       { return wmemcpy(__s1, __s2, __n); }
00240 
00241       static char_type* 
00242       assign(char_type* __s, size_t __n, char_type __a)
00243       { return wmemset(__s, __a, __n); }
00244 
00245       static char_type 
00246       to_char_type(const int_type& __c) { return char_type(__c); }
00247 
00248       static int_type 
00249       to_int_type(const char_type& __c) { return int_type(__c); }
00250 
00251       static bool 
00252       eq_int_type(const int_type& __c1, const int_type& __c2)
00253       { return __c1 == __c2; }
00254 
00255       static int_type 
00256       eof() { return static_cast<int_type>(WEOF); }
00257 
00258       static int_type 
00259       not_eof(const int_type& __c)
00260       { return eq_int_type(__c, eof()) ? 0 : __c; }
00261   };
00262 #endif //_GLIBCPP_USE_WCHAR_T
00263 
00264   template<typename _CharT, typename _Traits>
00265     struct _Char_traits_match
00266     {
00267       _CharT _M_c;
00268       _Char_traits_match(_CharT const& __c) : _M_c(__c) { }
00269 
00270       bool 
00271       operator()(_CharT const& __a) { return _Traits::eq(_M_c, __a); }
00272     };
00273 } // namespace std
00274 
00275 #endif

Generated on Mon Apr 8 03:11:23 2002 for libstdc++-v3 Source by doxygen1.2.15