libstdc++
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-2014 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /** @file bits/char_traits.h
00026  *  This is an internal header file, included by other library headers.
00027  *  Do not attempt to use it directly. @headername{string}
00028  */
00029 
00030 //
00031 // ISO C++ 14882: 21  Strings library
00032 //
00033 
00034 #ifndef _CHAR_TRAITS_H
00035 #define _CHAR_TRAITS_H 1
00036 
00037 #pragma GCC system_header
00038 
00039 #include <bits/stl_algobase.h>  // std::copy, std::fill_n
00040 #include <bits/postypes.h>      // For streampos
00041 #include <cwchar>               // For WEOF, wmemmove, wmemset, etc.
00042 
00043 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
00044 {
00045 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00046 
00047   /**
00048    *  @brief  Mapping from character type to associated types.
00049    *
00050    *  @note This is an implementation class for the generic version
00051    *  of char_traits.  It defines int_type, off_type, pos_type, and
00052    *  state_type.  By default these are unsigned long, streamoff,
00053    *  streampos, and mbstate_t.  Users who need a different set of
00054    *  types, but who don't need to change the definitions of any function
00055    *  defined in char_traits, can specialize __gnu_cxx::_Char_types
00056    *  while leaving __gnu_cxx::char_traits alone. */
00057   template<typename _CharT>
00058     struct _Char_types
00059     {
00060       typedef unsigned long   int_type;
00061       typedef std::streampos  pos_type;
00062       typedef std::streamoff  off_type;
00063       typedef std::mbstate_t  state_type;
00064     };
00065 
00066 
00067   /**
00068    *  @brief  Base class used to implement std::char_traits.
00069    *
00070    *  @note For any given actual character type, this definition is
00071    *  probably wrong.  (Most of the member functions are likely to be
00072    *  right, but the int_type and state_type typedefs, and the eof()
00073    *  member function, are likely to be wrong.)  The reason this class
00074    *  exists is so users can specialize it.  Classes in namespace std
00075    *  may not be specialized for fundamental types, but classes in
00076    *  namespace __gnu_cxx may be.
00077    *
00078    *  See http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt05ch13s03.html
00079    *  for advice on how to make use of this class for @a unusual character
00080    *  types. Also, check out include/ext/pod_char_traits.h.  
00081    */
00082   template<typename _CharT>
00083     struct char_traits
00084     {
00085       typedef _CharT                                    char_type;
00086       typedef typename _Char_types<_CharT>::int_type    int_type;
00087       typedef typename _Char_types<_CharT>::pos_type    pos_type;
00088       typedef typename _Char_types<_CharT>::off_type    off_type;
00089       typedef typename _Char_types<_CharT>::state_type  state_type;
00090 
00091       static void
00092       assign(char_type& __c1, const char_type& __c2)
00093       { __c1 = __c2; }
00094 
00095       static _GLIBCXX_CONSTEXPR bool
00096       eq(const char_type& __c1, const char_type& __c2)
00097       { return __c1 == __c2; }
00098 
00099       static _GLIBCXX_CONSTEXPR bool
00100       lt(const char_type& __c1, const char_type& __c2)
00101       { return __c1 < __c2; }
00102 
00103       static int
00104       compare(const char_type* __s1, const char_type* __s2, std::size_t __n);
00105 
00106       static std::size_t
00107       length(const char_type* __s);
00108 
00109       static const char_type*
00110       find(const char_type* __s, std::size_t __n, const char_type& __a);
00111 
00112       static char_type*
00113       move(char_type* __s1, const char_type* __s2, std::size_t __n);
00114 
00115       static char_type*
00116       copy(char_type* __s1, const char_type* __s2, std::size_t __n);
00117 
00118       static char_type*
00119       assign(char_type* __s, std::size_t __n, char_type __a);
00120 
00121       static _GLIBCXX_CONSTEXPR char_type
00122       to_char_type(const int_type& __c)
00123       { return static_cast<char_type>(__c); }
00124 
00125       static _GLIBCXX_CONSTEXPR int_type
00126       to_int_type(const char_type& __c)
00127       { return static_cast<int_type>(__c); }
00128 
00129       static _GLIBCXX_CONSTEXPR bool
00130       eq_int_type(const int_type& __c1, const int_type& __c2)
00131       { return __c1 == __c2; }
00132 
00133       static _GLIBCXX_CONSTEXPR int_type
00134       eof()
00135       { return static_cast<int_type>(_GLIBCXX_STDIO_EOF); }
00136 
00137       static _GLIBCXX_CONSTEXPR int_type
00138       not_eof(const int_type& __c)
00139       { return !eq_int_type(__c, eof()) ? __c : to_int_type(char_type()); }
00140     };
00141 
00142   template<typename _CharT>
00143     int
00144     char_traits<_CharT>::
00145     compare(const char_type* __s1, const char_type* __s2, std::size_t __n)
00146     {
00147       for (std::size_t __i = 0; __i < __n; ++__i)
00148     if (lt(__s1[__i], __s2[__i]))
00149       return -1;
00150     else if (lt(__s2[__i], __s1[__i]))
00151       return 1;
00152       return 0;
00153     }
00154 
00155   template<typename _CharT>
00156     std::size_t
00157     char_traits<_CharT>::
00158     length(const char_type* __p)
00159     {
00160       std::size_t __i = 0;
00161       while (!eq(__p[__i], char_type()))
00162         ++__i;
00163       return __i;
00164     }
00165 
00166   template<typename _CharT>
00167     const typename char_traits<_CharT>::char_type*
00168     char_traits<_CharT>::
00169     find(const char_type* __s, std::size_t __n, const char_type& __a)
00170     {
00171       for (std::size_t __i = 0; __i < __n; ++__i)
00172         if (eq(__s[__i], __a))
00173           return __s + __i;
00174       return 0;
00175     }
00176 
00177   template<typename _CharT>
00178     typename char_traits<_CharT>::char_type*
00179     char_traits<_CharT>::
00180     move(char_type* __s1, const char_type* __s2, std::size_t __n)
00181     {
00182       return static_cast<_CharT*>(__builtin_memmove(__s1, __s2,
00183                             __n * sizeof(char_type)));
00184     }
00185 
00186   template<typename _CharT>
00187     typename char_traits<_CharT>::char_type*
00188     char_traits<_CharT>::
00189     copy(char_type* __s1, const char_type* __s2, std::size_t __n)
00190     {
00191       // NB: Inline std::copy so no recursive dependencies.
00192       std::copy(__s2, __s2 + __n, __s1);
00193       return __s1;
00194     }
00195 
00196   template<typename _CharT>
00197     typename char_traits<_CharT>::char_type*
00198     char_traits<_CharT>::
00199     assign(char_type* __s, std::size_t __n, char_type __a)
00200     {
00201       // NB: Inline std::fill_n so no recursive dependencies.
00202       std::fill_n(__s, __n, __a);
00203       return __s;
00204     }
00205 
00206 _GLIBCXX_END_NAMESPACE_VERSION
00207 } // namespace
00208 
00209 namespace std _GLIBCXX_VISIBILITY(default)
00210 {
00211 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00212 
00213   // 21.1
00214   /**
00215    *  @brief  Basis for explicit traits specializations.
00216    *
00217    *  @note  For any given actual character type, this definition is
00218    *  probably wrong.  Since this is just a thin wrapper around
00219    *  __gnu_cxx::char_traits, it is possible to achieve a more
00220    *  appropriate definition by specializing __gnu_cxx::char_traits.
00221    *
00222    *  See http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt05ch13s03.html
00223    *  for advice on how to make use of this class for @a unusual character
00224    *  types. Also, check out include/ext/pod_char_traits.h.
00225   */
00226   template<class _CharT>
00227     struct char_traits : public __gnu_cxx::char_traits<_CharT>
00228     { };
00229 
00230 
00231   /// 21.1.3.1  char_traits specializations
00232   template<>
00233     struct char_traits<char>
00234     {
00235       typedef char              char_type;
00236       typedef int               int_type;
00237       typedef streampos         pos_type;
00238       typedef streamoff         off_type;
00239       typedef mbstate_t         state_type;
00240 
00241       static void
00242       assign(char_type& __c1, const char_type& __c2) _GLIBCXX_NOEXCEPT
00243       { __c1 = __c2; }
00244 
00245       static _GLIBCXX_CONSTEXPR bool
00246       eq(const char_type& __c1, const char_type& __c2) _GLIBCXX_NOEXCEPT
00247       { return __c1 == __c2; }
00248 
00249       static _GLIBCXX_CONSTEXPR bool
00250       lt(const char_type& __c1, const char_type& __c2) _GLIBCXX_NOEXCEPT
00251       {
00252     // LWG 467.
00253     return (static_cast<unsigned char>(__c1)
00254         < static_cast<unsigned char>(__c2));
00255       }
00256 
00257       static int
00258       compare(const char_type* __s1, const char_type* __s2, size_t __n)
00259       { return __builtin_memcmp(__s1, __s2, __n); }
00260 
00261       static size_t
00262       length(const char_type* __s)
00263       { return __builtin_strlen(__s); }
00264 
00265       static const char_type*
00266       find(const char_type* __s, size_t __n, const char_type& __a)
00267       { return static_cast<const char_type*>(__builtin_memchr(__s, __a, __n)); }
00268 
00269       static char_type*
00270       move(char_type* __s1, const char_type* __s2, size_t __n)
00271       { return static_cast<char_type*>(__builtin_memmove(__s1, __s2, __n)); }
00272 
00273       static char_type*
00274       copy(char_type* __s1, const char_type* __s2, size_t __n)
00275       { return static_cast<char_type*>(__builtin_memcpy(__s1, __s2, __n)); }
00276 
00277       static char_type*
00278       assign(char_type* __s, size_t __n, char_type __a)
00279       { return static_cast<char_type*>(__builtin_memset(__s, __a, __n)); }
00280 
00281       static _GLIBCXX_CONSTEXPR char_type
00282       to_char_type(const int_type& __c) _GLIBCXX_NOEXCEPT
00283       { return static_cast<char_type>(__c); }
00284 
00285       // To keep both the byte 0xff and the eof symbol 0xffffffff
00286       // from ending up as 0xffffffff.
00287       static _GLIBCXX_CONSTEXPR int_type
00288       to_int_type(const char_type& __c) _GLIBCXX_NOEXCEPT
00289       { return static_cast<int_type>(static_cast<unsigned char>(__c)); }
00290 
00291       static _GLIBCXX_CONSTEXPR bool
00292       eq_int_type(const int_type& __c1, const int_type& __c2) _GLIBCXX_NOEXCEPT
00293       { return __c1 == __c2; }
00294 
00295       static _GLIBCXX_CONSTEXPR int_type
00296       eof() _GLIBCXX_NOEXCEPT
00297       { return static_cast<int_type>(_GLIBCXX_STDIO_EOF); }
00298 
00299       static _GLIBCXX_CONSTEXPR int_type
00300       not_eof(const int_type& __c) _GLIBCXX_NOEXCEPT
00301       { return (__c == eof()) ? 0 : __c; }
00302   };
00303 
00304 
00305 #ifdef _GLIBCXX_USE_WCHAR_T
00306   /// 21.1.3.2  char_traits specializations
00307   template<>
00308     struct char_traits<wchar_t>
00309     {
00310       typedef wchar_t           char_type;
00311       typedef wint_t            int_type;
00312       typedef streamoff         off_type;
00313       typedef wstreampos        pos_type;
00314       typedef mbstate_t         state_type;
00315 
00316       static void
00317       assign(char_type& __c1, const char_type& __c2) _GLIBCXX_NOEXCEPT
00318       { __c1 = __c2; }
00319 
00320       static _GLIBCXX_CONSTEXPR bool
00321       eq(const char_type& __c1, const char_type& __c2) _GLIBCXX_NOEXCEPT
00322       { return __c1 == __c2; }
00323 
00324       static _GLIBCXX_CONSTEXPR bool
00325       lt(const char_type& __c1, const char_type& __c2) _GLIBCXX_NOEXCEPT
00326       { return __c1 < __c2; }
00327 
00328       static int
00329       compare(const char_type* __s1, const char_type* __s2, size_t __n)
00330       { return wmemcmp(__s1, __s2, __n); }
00331 
00332       static size_t
00333       length(const char_type* __s)
00334       { return wcslen(__s); }
00335 
00336       static const char_type*
00337       find(const char_type* __s, size_t __n, const char_type& __a)
00338       { return wmemchr(__s, __a, __n); }
00339 
00340       static char_type*
00341       move(char_type* __s1, const char_type* __s2, size_t __n)
00342       { return wmemmove(__s1, __s2, __n); }
00343 
00344       static char_type*
00345       copy(char_type* __s1, const char_type* __s2, size_t __n)
00346       { return wmemcpy(__s1, __s2, __n); }
00347 
00348       static char_type*
00349       assign(char_type* __s, size_t __n, char_type __a)
00350       { return wmemset(__s, __a, __n); }
00351 
00352       static _GLIBCXX_CONSTEXPR char_type
00353       to_char_type(const int_type& __c) _GLIBCXX_NOEXCEPT
00354       { return char_type(__c); }
00355 
00356       static _GLIBCXX_CONSTEXPR int_type
00357       to_int_type(const char_type& __c) _GLIBCXX_NOEXCEPT
00358       { return int_type(__c); }
00359 
00360       static _GLIBCXX_CONSTEXPR bool
00361       eq_int_type(const int_type& __c1, const int_type& __c2) _GLIBCXX_NOEXCEPT
00362       { return __c1 == __c2; }
00363 
00364       static _GLIBCXX_CONSTEXPR int_type
00365       eof() _GLIBCXX_NOEXCEPT
00366       { return static_cast<int_type>(WEOF); }
00367 
00368       static _GLIBCXX_CONSTEXPR int_type
00369       not_eof(const int_type& __c) _GLIBCXX_NOEXCEPT
00370       { return eq_int_type(__c, eof()) ? 0 : __c; }
00371   };
00372 #endif //_GLIBCXX_USE_WCHAR_T
00373 
00374 _GLIBCXX_END_NAMESPACE_VERSION
00375 } // namespace
00376 
00377 #if ((__cplusplus >= 201103L) \
00378      && defined(_GLIBCXX_USE_C99_STDINT_TR1))
00379 
00380 #include <cstdint>
00381 
00382 namespace std _GLIBCXX_VISIBILITY(default)
00383 {
00384 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00385 
00386   template<>
00387     struct char_traits<char16_t>
00388     {
00389       typedef char16_t          char_type;
00390       typedef uint_least16_t    int_type;
00391       typedef streamoff         off_type;
00392       typedef u16streampos      pos_type;
00393       typedef mbstate_t         state_type;
00394 
00395       static void
00396       assign(char_type& __c1, const char_type& __c2) noexcept
00397       { __c1 = __c2; }
00398 
00399       static constexpr bool
00400       eq(const char_type& __c1, const char_type& __c2) noexcept
00401       { return __c1 == __c2; }
00402 
00403       static constexpr bool
00404       lt(const char_type& __c1, const char_type& __c2) noexcept
00405       { return __c1 < __c2; }
00406 
00407       static int
00408       compare(const char_type* __s1, const char_type* __s2, size_t __n)
00409       {
00410     for (size_t __i = 0; __i < __n; ++__i)
00411       if (lt(__s1[__i], __s2[__i]))
00412         return -1;
00413       else if (lt(__s2[__i], __s1[__i]))
00414         return 1;
00415     return 0;
00416       }
00417 
00418       static size_t
00419       length(const char_type* __s)
00420       {
00421     size_t __i = 0;
00422     while (!eq(__s[__i], char_type()))
00423       ++__i;
00424     return __i;
00425       }
00426 
00427       static const char_type*
00428       find(const char_type* __s, size_t __n, const char_type& __a)
00429       {
00430     for (size_t __i = 0; __i < __n; ++__i)
00431       if (eq(__s[__i], __a))
00432         return __s + __i;
00433     return 0;
00434       }
00435 
00436       static char_type*
00437       move(char_type* __s1, const char_type* __s2, size_t __n)
00438       {
00439     return (static_cast<char_type*>
00440         (__builtin_memmove(__s1, __s2, __n * sizeof(char_type))));
00441       }
00442 
00443       static char_type*
00444       copy(char_type* __s1, const char_type* __s2, size_t __n)
00445       {
00446     return (static_cast<char_type*>
00447         (__builtin_memcpy(__s1, __s2, __n * sizeof(char_type))));
00448       }
00449 
00450       static char_type*
00451       assign(char_type* __s, size_t __n, char_type __a)
00452       {
00453     for (size_t __i = 0; __i < __n; ++__i)
00454       assign(__s[__i], __a);
00455     return __s;
00456       }
00457 
00458       static constexpr char_type
00459       to_char_type(const int_type& __c) noexcept
00460       { return char_type(__c); }
00461 
00462       static constexpr int_type
00463       to_int_type(const char_type& __c) noexcept
00464       { return int_type(__c); }
00465 
00466       static constexpr bool
00467       eq_int_type(const int_type& __c1, const int_type& __c2) noexcept
00468       { return __c1 == __c2; }
00469 
00470       static constexpr int_type
00471       eof() noexcept
00472       { return static_cast<int_type>(-1); }
00473 
00474       static constexpr int_type
00475       not_eof(const int_type& __c) noexcept
00476       { return eq_int_type(__c, eof()) ? 0 : __c; }
00477     };
00478 
00479   template<>
00480     struct char_traits<char32_t>
00481     {
00482       typedef char32_t          char_type;
00483       typedef uint_least32_t    int_type;
00484       typedef streamoff         off_type;
00485       typedef u32streampos      pos_type;
00486       typedef mbstate_t         state_type;
00487 
00488       static void
00489       assign(char_type& __c1, const char_type& __c2) noexcept
00490       { __c1 = __c2; }
00491 
00492       static constexpr bool
00493       eq(const char_type& __c1, const char_type& __c2) noexcept
00494       { return __c1 == __c2; }
00495 
00496       static constexpr bool
00497       lt(const char_type& __c1, const char_type& __c2) noexcept
00498       { return __c1 < __c2; }
00499 
00500       static int
00501       compare(const char_type* __s1, const char_type* __s2, size_t __n)
00502       {
00503     for (size_t __i = 0; __i < __n; ++__i)
00504       if (lt(__s1[__i], __s2[__i]))
00505         return -1;
00506       else if (lt(__s2[__i], __s1[__i]))
00507         return 1;
00508     return 0;
00509       }
00510 
00511       static size_t
00512       length(const char_type* __s)
00513       {
00514     size_t __i = 0;
00515     while (!eq(__s[__i], char_type()))
00516       ++__i;
00517     return __i;
00518       }
00519 
00520       static const char_type*
00521       find(const char_type* __s, size_t __n, const char_type& __a)
00522       {
00523     for (size_t __i = 0; __i < __n; ++__i)
00524       if (eq(__s[__i], __a))
00525         return __s + __i;
00526     return 0;
00527       }
00528 
00529       static char_type*
00530       move(char_type* __s1, const char_type* __s2, size_t __n)
00531       {
00532     return (static_cast<char_type*>
00533         (__builtin_memmove(__s1, __s2, __n * sizeof(char_type))));
00534       }
00535 
00536       static char_type*
00537       copy(char_type* __s1, const char_type* __s2, size_t __n)
00538       { 
00539     return (static_cast<char_type*>
00540         (__builtin_memcpy(__s1, __s2, __n * sizeof(char_type))));
00541       }
00542 
00543       static char_type*
00544       assign(char_type* __s, size_t __n, char_type __a)
00545       {
00546     for (size_t __i = 0; __i < __n; ++__i)
00547       assign(__s[__i], __a);
00548     return __s;
00549       }
00550 
00551       static constexpr char_type
00552       to_char_type(const int_type& __c) noexcept
00553       { return char_type(__c); }
00554 
00555       static constexpr int_type
00556       to_int_type(const char_type& __c) noexcept
00557       { return int_type(__c); }
00558 
00559       static constexpr bool
00560       eq_int_type(const int_type& __c1, const int_type& __c2) noexcept
00561       { return __c1 == __c2; }
00562 
00563       static constexpr int_type
00564       eof() noexcept
00565       { return static_cast<int_type>(-1); }
00566 
00567       static constexpr int_type
00568       not_eof(const int_type& __c) noexcept
00569       { return eq_int_type(__c, eof()) ? 0 : __c; }
00570     };
00571 
00572 _GLIBCXX_END_NAMESPACE_VERSION
00573 } // namespace
00574 
00575 #endif 
00576 
00577 #endif // _CHAR_TRAITS_H