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, 2002, 2003, 2004, 2005
00004 // Free Software Foundation, Inc.
00005 //
00006 // This file is part of the GNU ISO C++ Library.  This library is free
00007 // software; you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the
00009 // Free Software Foundation; either version 2, or (at your option)
00010 // any later version.
00011 
00012 // This library is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 
00017 // You should have received a copy of the GNU General Public License along
00018 // with this library; see the file COPYING.  If not, write to the Free
00019 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
00020 // USA.
00021 
00022 // As a special exception, you may use this file as part of a free software
00023 // library without restriction.  Specifically, if other files instantiate
00024 // templates or use macros or inline functions from this file, or you compile
00025 // this file and link it with other files to produce an executable, this
00026 // file does not by itself cause the resulting executable to be covered by
00027 // the GNU General Public License.  This exception does not however
00028 // invalidate any other reasons why the executable file might be covered by
00029 // the GNU General Public License.
00030 
00031 //
00032 // ISO C++ 14882: 21  Strings library
00033 //
00034 
00040 #ifndef _CHAR_TRAITS_H
00041 #define _CHAR_TRAITS_H 1
00042 
00043 #pragma GCC system_header
00044 
00045 #include <cstring>            // For memmove, memset, memchr
00046 #include <bits/stl_algobase.h>// For copy, lexicographical_compare, fill_n
00047 #include <bits/postypes.h>    // For streampos
00048 
00049 namespace __gnu_cxx
00050 {
00061   template <class _CharT>
00062     struct _Char_types
00063     {
00064       typedef unsigned long   int_type;
00065       typedef std::streampos  pos_type;
00066       typedef std::streamoff  off_type;
00067       typedef std::mbstate_t  state_type;
00068     };
00069 
00070 
00086   template<typename _CharT>
00087     struct char_traits
00088     {
00089       typedef _CharT                                    char_type;
00090       typedef typename _Char_types<_CharT>::int_type    int_type;
00091       typedef typename _Char_types<_CharT>::pos_type    pos_type;
00092       typedef typename _Char_types<_CharT>::off_type    off_type;
00093       typedef typename _Char_types<_CharT>::state_type  state_type;
00094 
00095       static void
00096       assign(char_type& __c1, const char_type& __c2)
00097       { __c1 = __c2; }
00098 
00099       static bool
00100       eq(const char_type& __c1, const char_type& __c2)
00101       { return __c1 == __c2; }
00102 
00103       static bool
00104       lt(const char_type& __c1, const char_type& __c2)
00105       { return __c1 < __c2; }
00106 
00107       static int
00108       compare(const char_type* __s1, const char_type* __s2, std::size_t __n);
00109 
00110       static std::size_t
00111       length(const char_type* __s);
00112 
00113       static const char_type*
00114       find(const char_type* __s, std::size_t __n, const char_type& __a);
00115 
00116       static char_type*
00117       move(char_type* __s1, const char_type* __s2, std::size_t __n);
00118 
00119       static char_type*
00120       copy(char_type* __s1, const char_type* __s2, std::size_t __n);
00121 
00122       static char_type*
00123       assign(char_type* __s, std::size_t __n, char_type __a);
00124 
00125       static char_type
00126       to_char_type(const int_type& __c)
00127       { return static_cast<char_type>(__c); }
00128 
00129       static int_type
00130       to_int_type(const char_type& __c)
00131       { return static_cast<int_type>(__c); }
00132 
00133       static bool
00134       eq_int_type(const int_type& __c1, const int_type& __c2)
00135       { return __c1 == __c2; }
00136 
00137       static int_type
00138       eof()
00139       { return static_cast<int_type>(EOF); }
00140 
00141       static int_type
00142       not_eof(const int_type& __c)
00143       { return !eq_int_type(__c, eof()) ? __c : to_int_type(char_type()); }
00144     };
00145 
00146   template<typename _CharT>
00147     int
00148     char_traits<_CharT>::
00149     compare(const char_type* __s1, const char_type* __s2, std::size_t __n)
00150     {
00151       for (size_t __i = 0; __i < __n; ++__i)
00152     if (lt(__s1[__i], __s2[__i]))
00153       return -1;
00154     else if (lt(__s2[__i], __s1[__i]))
00155       return 1;
00156       return 0;
00157     }
00158 
00159   template<typename _CharT>
00160     std::size_t
00161     char_traits<_CharT>::
00162     length(const char_type* __p)
00163     {
00164       std::size_t __i = 0;
00165       while (!eq(__p[__i], char_type()))
00166         ++__i;
00167       return __i;
00168     }
00169 
00170   template<typename _CharT>
00171     const typename char_traits<_CharT>::char_type*
00172     char_traits<_CharT>::
00173     find(const char_type* __s, std::size_t __n, const char_type& __a)
00174     {
00175       for (std::size_t __i = 0; __i < __n; ++__i)
00176         if (eq(__s[__i], __a))
00177           return __s + __i;
00178       return 0;
00179     }
00180 
00181   template<typename _CharT>
00182     typename char_traits<_CharT>::char_type*
00183     char_traits<_CharT>::
00184     move(char_type* __s1, const char_type* __s2, std::size_t __n)
00185     {
00186       return static_cast<_CharT*>(std::memmove(__s1, __s2,
00187                            __n * sizeof(char_type)));
00188     }
00189 
00190   template<typename _CharT>
00191     typename char_traits<_CharT>::char_type*
00192     char_traits<_CharT>::
00193     copy(char_type* __s1, const char_type* __s2, std::size_t __n)
00194     {
00195       std::copy(__s2, __s2 + __n, __s1);
00196       return __s1;
00197     }
00198 
00199   template<typename _CharT>
00200     typename char_traits<_CharT>::char_type*
00201     char_traits<_CharT>::
00202     assign(char_type* __s, std::size_t __n, char_type __a)
00203     {
00204       std::fill_n(__s, __n, __a);
00205       return __s;
00206     }
00207 }
00208 
00209 namespace std
00210 {
00211   // 21.1
00224   template<class _CharT>
00225     struct char_traits : public __gnu_cxx::char_traits<_CharT>
00226     { };
00227 
00228 
00230   template<>
00231     struct char_traits<char>
00232     {
00233       typedef char              char_type;
00234       typedef int               int_type;
00235       typedef streampos         pos_type;
00236       typedef streamoff         off_type;
00237       typedef mbstate_t         state_type;
00238 
00239       static void
00240       assign(char_type& __c1, const char_type& __c2)
00241       { __c1 = __c2; }
00242 
00243       static bool
00244       eq(const char_type& __c1, const char_type& __c2)
00245       { return __c1 == __c2; }
00246 
00247       static bool
00248       lt(const char_type& __c1, const char_type& __c2)
00249       { return __c1 < __c2; }
00250 
00251       static int
00252       compare(const char_type* __s1, const char_type* __s2, size_t __n)
00253       { return memcmp(__s1, __s2, __n); }
00254 
00255       static size_t
00256       length(const char_type* __s)
00257       { return strlen(__s); }
00258 
00259       static const char_type*
00260       find(const char_type* __s, size_t __n, const char_type& __a)
00261       { return static_cast<const char_type*>(memchr(__s, __a, __n)); }
00262 
00263       static char_type*
00264       move(char_type* __s1, const char_type* __s2, size_t __n)
00265       { return static_cast<char_type*>(memmove(__s1, __s2, __n)); }
00266 
00267       static char_type*
00268       copy(char_type* __s1, const char_type* __s2, size_t __n)
00269       { return static_cast<char_type*>(memcpy(__s1, __s2, __n)); }
00270 
00271       static char_type*
00272       assign(char_type* __s, size_t __n, char_type __a)
00273       { return static_cast<char_type*>(memset(__s, __a, __n)); }
00274 
00275       static char_type
00276       to_char_type(const int_type& __c)
00277       { return static_cast<char_type>(__c); }
00278 
00279       // To keep both the byte 0xff and the eof symbol 0xffffffff
00280       // from ending up as 0xffffffff.
00281       static int_type
00282       to_int_type(const char_type& __c)
00283       { return static_cast<int_type>(static_cast<unsigned char>(__c)); }
00284 
00285       static bool
00286       eq_int_type(const int_type& __c1, const int_type& __c2)
00287       { return __c1 == __c2; }
00288 
00289       static int_type
00290       eof() { return static_cast<int_type>(EOF); }
00291 
00292       static int_type
00293       not_eof(const int_type& __c)
00294       { return (__c == eof()) ? 0 : __c; }
00295   };
00296 
00297 
00298 #ifdef _GLIBCXX_USE_WCHAR_T
00300   template<>
00301     struct char_traits<wchar_t>
00302     {
00303       typedef wchar_t           char_type;
00304       typedef wint_t            int_type;
00305       typedef streamoff         off_type;
00306       typedef wstreampos        pos_type;
00307       typedef mbstate_t         state_type;
00308 
00309       static void
00310       assign(char_type& __c1, const char_type& __c2)
00311       { __c1 = __c2; }
00312 
00313       static bool
00314       eq(const char_type& __c1, const char_type& __c2)
00315       { return __c1 == __c2; }
00316 
00317       static bool
00318       lt(const char_type& __c1, const char_type& __c2)
00319       { return __c1 < __c2; }
00320 
00321       static int
00322       compare(const char_type* __s1, const char_type* __s2, size_t __n)
00323       { return wmemcmp(__s1, __s2, __n); }
00324 
00325       static size_t
00326       length(const char_type* __s)
00327       { return wcslen(__s); }
00328 
00329       static const char_type*
00330       find(const char_type* __s, size_t __n, const char_type& __a)
00331       { return wmemchr(__s, __a, __n); }
00332 
00333       static char_type*
00334       move(char_type* __s1, const char_type* __s2, size_t __n)
00335       { return wmemmove(__s1, __s2, __n); }
00336 
00337       static char_type*
00338       copy(char_type* __s1, const char_type* __s2, size_t __n)
00339       { return wmemcpy(__s1, __s2, __n); }
00340 
00341       static char_type*
00342       assign(char_type* __s, size_t __n, char_type __a)
00343       { return wmemset(__s, __a, __n); }
00344 
00345       static char_type
00346       to_char_type(const int_type& __c) { return char_type(__c); }
00347 
00348       static int_type
00349       to_int_type(const char_type& __c) { return int_type(__c); }
00350 
00351       static bool
00352       eq_int_type(const int_type& __c1, const int_type& __c2)
00353       { return __c1 == __c2; }
00354 
00355       static int_type
00356       eof() { return static_cast<int_type>(WEOF); }
00357 
00358       static int_type
00359       not_eof(const int_type& __c)
00360       { return eq_int_type(__c, eof()) ? 0 : __c; }
00361   };
00362 #endif //_GLIBCXX_USE_WCHAR_T
00363 
00364 } // namespace std
00365 
00366 #endif

Generated on Tue Feb 2 16:55:49 2010 for GNU C++ STL by  doxygen 1.4.7