# 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
    00039 #ifndef _CPP_BITS_CHAR_TRAITS_H
    00040 #define _CPP_BITS_CHAR_TRAITS_H 1
    00041
    00042 #pragma GCC system_header
    00043
    00044 #include <cstring>  // For memmove, memset, memchr
    00045 #include <bits/fpos.h>      // For streampos
    00046
    00047 namespace std
    00048 {
    00052   template<class _CharT>
    00053     struct char_traits
    00054     {
    00055       typedef _CharT        char_type;
    00056       // Unsigned as wint_t in unsigned.
    00057       typedef unsigned long     int_type;
    00058       typedef streampos     pos_type;
    00059       typedef streamoff     off_type;
    00060       typedef mbstate_t     state_type;
    00061
    00062       static void
    00063       assign(char_type& __c1, const char_type& __c2)
    00064       { __c1 = __c2; }
    00065
    00066       static bool
    00067       eq(const char_type& __c1, const char_type& __c2)
    00068       { return __c1 == __c2; }
    00069
    00070       static bool
    00071       lt(const char_type& __c1, const char_type& __c2)
    00072       { return __c1 < __c2; }
    00073
    00074       static int
    00075       compare(const char_type* __s1, const char_type* __s2, size_t __n)
    00076       {
    00077     for (size_t __i = 0; __i < __n; ++__i)
    00078       if (!eq(__s1[__i], __s2[__i]))
    00079         return lt(__s1[__i], __s2[__i]) ? -1 : 1;
    00080     return 0;
    00081       }
    00082
    00083       static size_t
    00084       length(const char_type* __s)
    00085       {
    00086     const char_type* __p = __s;
    00087     while (*__p) ++__p;
    00088     return (__p - __s);
    00089       }
    00090
    00091       static const char_type*
    00092       find(const char_type* __s, size_t __n, const char_type& __a)
    00093       {
    00094     for (const char_type* __p = __s; size_t(__p - __s) < __n; ++__p)
    00095       if (*__p == __a) return __p;
    00096     return 0;
    00097       }
    00098
    00099       static char_type*
    00100       move(char_type* __s1, const char_type* __s2, size_t __n)
    00101       { return (char_type*) memmove(__s1, __s2, __n * sizeof(char_type)); }
    00102
    00103       static char_type*
    00104       copy(char_type* __s1, const char_type* __s2, size_t __n)
    00105       { return (char_type*) memcpy(__s1, __s2, __n * sizeof(char_type)); }
    00106
    00107       static char_type*
    00108       assign(char_type* __s, size_t __n, char_type __a)
    00109       {
    00110     for (char_type* __p = __s; __p < __s + __n; ++__p)
    00111       assign(*__p, __a);
    00112         return __s;
    00113       }
    00114
    00115       static char_type
    00116       to_char_type(const int_type& __c)
    00117       { return char_type(__c); }
    00118
    00119       static int_type
    00120       to_int_type(const char_type& __c) { return int_type(__c); }
    00121
    00122       static bool
    00123       eq_int_type(const int_type& __c1, const int_type& __c2)
    00124       { return __c1 == __c2; }
    00125
    00126       static int_type
    00127       eof() { return static_cast<int_type>(-1); }
    00128
    00129       static int_type
    00130       not_eof(const int_type& __c)
    00131       { return eq_int_type(__c, eof()) ? int_type(0) : __c; }
    00132     };
    00133
    00134
    00136   template<>
    00137     struct char_traits<char>
    00138     {
    00139       typedef char      char_type;
    00140       typedef int           int_type;
    00141       typedef streampos     pos_type;
    00142       typedef streamoff     off_type;
    00143       typedef mbstate_t     state_type;
    00144
    00145       static void
    00146       assign(char_type& __c1, const char_type& __c2)
    00147       { __c1 = __c2; }
    00148
    00149       static bool
    00150       eq(const char_type& __c1, const char_type& __c2)
    00151       { return __c1 == __c2; }
    00152
    00153       static bool
    00154       lt(const char_type& __c1, const char_type& __c2)
    00155       { return __c1 < __c2; }
    00156
    00157       static int
    00158       compare(const char_type* __s1, const char_type* __s2, size_t __n)
    00159       { return memcmp(__s1, __s2, __n); }
    00160
    00161       static size_t
    00162       length(const char_type* __s)
    00163       { return strlen(__s); }
    00164
    00165       static const char_type*
    00166       find(const char_type* __s, size_t __n, const char_type& __a)
    00167       { return static_cast<const char_type*>(memchr(__s, __a, __n)); }
    00168
    00169       static char_type*
    00170       move(char_type* __s1, const char_type* __s2, size_t __n)
    00171       { return static_cast<char_type*>(memmove(__s1, __s2, __n)); }
    00172
    00173       static char_type*
    00174       copy(char_type* __s1, const char_type* __s2, size_t __n)
    00175       {  return static_cast<char_type*>(memcpy(__s1, __s2, __n)); }
    00176
    00177       static char_type*
    00178       assign(char_type* __s, size_t __n, char_type __a)
    00179       { return static_cast<char_type*>(memset(__s, __a, __n)); }
    00180
    00181       static char_type
    00182       to_char_type(const int_type& __c)
    00183       { return static_cast<char_type>(__c); }
    00184
    00185       // To keep both the byte 0xff and the eof symbol 0xffffffff
    00186       // from ending up as 0xffffffff.
    00187       static int_type
    00188       to_int_type(const char_type& __c)
    00189       { return static_cast<int_type>(static_cast<unsigned char>(__c)); }
    00190
    00191       static bool
    00192       eq_int_type(const int_type& __c1, const int_type& __c2)
    00193       { return __c1 == __c2; }
    00194
    00195       static int_type
    00196       eof() { return static_cast<int_type>(EOF); }
    00197
    00198       static int_type
    00199       not_eof(const int_type& __c)
    00200       { return (__c == eof()) ? 0 : __c; }
    00201   };
    00202
    00203
    00204 #ifdef _GLIBCPP_USE_WCHAR_T
    00205   template<>
    00206     struct char_traits<wchar_t>
    00207     {
    00208       typedef wchar_t       char_type;
    00209       typedef wint_t        int_type;
    00210       typedef streamoff     off_type;
    00211       typedef wstreampos    pos_type;
    00212       typedef mbstate_t     state_type;
    00213
    00214       static void
    00215       assign(char_type& __c1, const char_type& __c2)
    00216       { __c1 = __c2; }
    00217
    00218       static bool
    00219       eq(const char_type& __c1, const char_type& __c2)
    00220       { return __c1 == __c2; }
    00221
    00222       static bool
    00223       lt(const char_type& __c1, const char_type& __c2)
    00224       { return __c1 < __c2; }
    00225
    00226       static int
    00227       compare(const char_type* __s1, const char_type* __s2, size_t __n)
    00228       { return wmemcmp(__s1, __s2, __n); }
    00229
    00230       static size_t
    00231       length(const char_type* __s)
    00232       { return wcslen(__s); }
    00233
    00234       static const char_type*
    00235       find(const char_type* __s, size_t __n, const char_type& __a)
    00236       { return wmemchr(__s, __a, __n); }
    00237
    00238       static char_type*
    00239       move(char_type* __s1, const char_type* __s2, int_type __n)
    00240       { return wmemmove(__s1, __s2, __n); }
    00241
    00242       static char_type*
    00243       copy(char_type* __s1, const char_type* __s2, size_t __n)
    00244       { return wmemcpy(__s1, __s2, __n); }
    00245
    00246       static char_type*
    00247       assign(char_type* __s, size_t __n, char_type __a)
    00248       { return wmemset(__s, __a, __n); }
    00249
    00250       static char_type
    00251       to_char_type(const int_type& __c) { return char_type(__c); }
    00252
    00253       static int_type
    00254       to_int_type(const char_type& __c) { return int_type(__c); }
    00255
    00256       static bool
    00257       eq_int_type(const int_type& __c1, const int_type& __c2)
    00258       { return __c1 == __c2; }
    00259
    00260       static int_type
    00261       eof() { return static_cast<int_type>(WEOF); }
    00262
    00263       static int_type
    00264       not_eof(const int_type& __c)
    00265       { return eq_int_type(__c, eof()) ? 0 : __c; }
    00266   };
    00267 #endif //_GLIBCPP_USE_WCHAR_T
    00268
    00269   template<typename _CharT, typename _Traits>
    00270     struct _Char_traits_match
    00271     {
    00272       _CharT _M_c;
    00273       _Char_traits_match(_CharT const& __c) : _M_c(__c) { }
    00274
    00275       bool
    00276       operator()(_CharT const& __a) { return _Traits::eq(_M_c, __a); }
    00277     };
    00278 } // namespace std
    00279
    00280 #endif