libstdc++
|
00001 // POD character, std::char_traits specialization -*- C++ -*- 00002 00003 // Copyright (C) 2002-2015 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 ext/pod_char_traits.h 00026 * This file is a GNU extension to the Standard C++ Library. 00027 */ 00028 00029 // Gabriel Dos Reis <gdr@integrable-solutions.net> 00030 // Benjamin Kosnik <bkoz@redhat.com> 00031 00032 #ifndef _POD_CHAR_TRAITS_H 00033 #define _POD_CHAR_TRAITS_H 1 00034 00035 #pragma GCC system_header 00036 00037 #include <string> 00038 00039 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) 00040 { 00041 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00042 00043 // POD character abstraction. 00044 // NB: The char_type parameter is a subset of int_type, as to allow 00045 // int_type to properly hold the full range of char_type values as 00046 // well as EOF. 00047 /// @brief A POD class that serves as a character abstraction class. 00048 template<typename _Value, typename _Int, typename _St = std::mbstate_t> 00049 struct character 00050 { 00051 typedef _Value value_type; 00052 typedef _Int int_type; 00053 typedef _St state_type; 00054 typedef character<_Value, _Int, _St> char_type; 00055 00056 value_type value; 00057 00058 template<typename V2> 00059 static char_type 00060 from(const V2& v) 00061 { 00062 char_type ret = { static_cast<value_type>(v) }; 00063 return ret; 00064 } 00065 00066 template<typename V2> 00067 static V2 00068 to(const char_type& c) 00069 { 00070 V2 ret = { static_cast<V2>(c.value) }; 00071 return ret; 00072 } 00073 00074 }; 00075 00076 template<typename _Value, typename _Int, typename _St> 00077 inline bool 00078 operator==(const character<_Value, _Int, _St>& lhs, 00079 const character<_Value, _Int, _St>& rhs) 00080 { return lhs.value == rhs.value; } 00081 00082 template<typename _Value, typename _Int, typename _St> 00083 inline bool 00084 operator<(const character<_Value, _Int, _St>& lhs, 00085 const character<_Value, _Int, _St>& rhs) 00086 { return lhs.value < rhs.value; } 00087 00088 _GLIBCXX_END_NAMESPACE_VERSION 00089 } // namespace 00090 00091 namespace std _GLIBCXX_VISIBILITY(default) 00092 { 00093 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00094 00095 /// char_traits<__gnu_cxx::character> specialization. 00096 template<typename _Value, typename _Int, typename _St> 00097 struct char_traits<__gnu_cxx::character<_Value, _Int, _St> > 00098 { 00099 typedef __gnu_cxx::character<_Value, _Int, _St> char_type; 00100 typedef typename char_type::int_type int_type; 00101 typedef typename char_type::state_type state_type; 00102 typedef fpos<state_type> pos_type; 00103 typedef streamoff off_type; 00104 00105 static void 00106 assign(char_type& __c1, const char_type& __c2) 00107 { __c1 = __c2; } 00108 00109 static bool 00110 eq(const char_type& __c1, const char_type& __c2) 00111 { return __c1 == __c2; } 00112 00113 static bool 00114 lt(const char_type& __c1, const char_type& __c2) 00115 { return __c1 < __c2; } 00116 00117 static int 00118 compare(const char_type* __s1, const char_type* __s2, size_t __n) 00119 { 00120 for (size_t __i = 0; __i < __n; ++__i) 00121 if (!eq(__s1[__i], __s2[__i])) 00122 return lt(__s1[__i], __s2[__i]) ? -1 : 1; 00123 return 0; 00124 } 00125 00126 static size_t 00127 length(const char_type* __s) 00128 { 00129 const char_type* __p = __s; 00130 while (__p->value) 00131 ++__p; 00132 return (__p - __s); 00133 } 00134 00135 static const char_type* 00136 find(const char_type* __s, size_t __n, const char_type& __a) 00137 { 00138 for (const char_type* __p = __s; size_t(__p - __s) < __n; ++__p) 00139 if (*__p == __a) 00140 return __p; 00141 return 0; 00142 } 00143 00144 static char_type* 00145 move(char_type* __s1, const char_type* __s2, size_t __n) 00146 { 00147 if (__n == 0) 00148 return __s1; 00149 return static_cast<char_type*> 00150 (__builtin_memmove(__s1, __s2, __n * sizeof(char_type))); 00151 } 00152 00153 static char_type* 00154 copy(char_type* __s1, const char_type* __s2, size_t __n) 00155 { 00156 if (__n == 0) 00157 return __s1; 00158 std::copy(__s2, __s2 + __n, __s1); 00159 return __s1; 00160 } 00161 00162 static char_type* 00163 assign(char_type* __s, size_t __n, char_type __a) 00164 { 00165 std::fill_n(__s, __n, __a); 00166 return __s; 00167 } 00168 00169 static char_type 00170 to_char_type(const int_type& __i) 00171 { return char_type::template from(__i); } 00172 00173 static int_type 00174 to_int_type(const char_type& __c) 00175 { return char_type::template to<int_type>(__c); } 00176 00177 static bool 00178 eq_int_type(const int_type& __c1, const int_type& __c2) 00179 { return __c1 == __c2; } 00180 00181 static int_type 00182 eof() 00183 { 00184 int_type __r = { static_cast<typename __gnu_cxx::__conditional_type 00185 <std::__is_integer<int_type>::__value, 00186 int_type, int>::__type>(-1) }; 00187 return __r; 00188 } 00189 00190 static int_type 00191 not_eof(const int_type& __c) 00192 { return eq_int_type(__c, eof()) ? int_type() : __c; } 00193 }; 00194 00195 _GLIBCXX_END_NAMESPACE_VERSION 00196 } // namespace 00197 00198 #endif