1*404b540aSrobert // POD character, std::char_traits specialization -*- C++ -*- 2*404b540aSrobert 3*404b540aSrobert // Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc. 4*404b540aSrobert // 5*404b540aSrobert // This file is part of the GNU ISO C++ Library. This library is free 6*404b540aSrobert // software; you can redistribute it and/or modify it under the 7*404b540aSrobert // terms of the GNU General Public License as published by the 8*404b540aSrobert // Free Software Foundation; either version 2, or (at your option) 9*404b540aSrobert // any later version. 10*404b540aSrobert 11*404b540aSrobert // This library is distributed in the hope that it will be useful, 12*404b540aSrobert // but WITHOUT ANY WARRANTY; without even the implied warranty of 13*404b540aSrobert // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14*404b540aSrobert // GNU General Public License for more details. 15*404b540aSrobert 16*404b540aSrobert // You should have received a copy of the GNU General Public License along 17*404b540aSrobert // with this library; see the file COPYING. If not, write to the Free 18*404b540aSrobert // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 19*404b540aSrobert // USA. 20*404b540aSrobert 21*404b540aSrobert // As a special exception, you may use this file as part of a free software 22*404b540aSrobert // library without restriction. Specifically, if other files instantiate 23*404b540aSrobert // templates or use macros or inline functions from this file, or you compile 24*404b540aSrobert // this file and link it with other files to produce an executable, this 25*404b540aSrobert // file does not by itself cause the resulting executable to be covered by 26*404b540aSrobert // the GNU General Public License. This exception does not however 27*404b540aSrobert // invalidate any other reasons why the executable file might be covered by 28*404b540aSrobert // the GNU General Public License. 29*404b540aSrobert 30*404b540aSrobert /** @file ext/pod_char_traits.h 31*404b540aSrobert * This file is a GNU extension to the Standard C++ Library. 32*404b540aSrobert */ 33*404b540aSrobert 34*404b540aSrobert // Gabriel Dos Reis <gdr@integrable-solutions.net> 35*404b540aSrobert // Benjamin Kosnik <bkoz@redhat.com> 36*404b540aSrobert 37*404b540aSrobert #ifndef _POD_CHAR_TRAITS_H 38*404b540aSrobert #define _POD_CHAR_TRAITS_H 1 39*404b540aSrobert 40*404b540aSrobert #include <string> 41*404b540aSrobert 42*404b540aSrobert _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx) 43*404b540aSrobert 44*404b540aSrobert // POD character abstraction. 45*404b540aSrobert // NB: The char_type parameter is a subset of int_type, as to allow 46*404b540aSrobert // int_type to properly hold the full range of char_type values as 47*404b540aSrobert // well as EOF. 48*404b540aSrobert /// @brief A POD class that serves as a character abstraction class. 49*404b540aSrobert template<typename V, typename I, typename S = mbstate_t> 50*404b540aSrobert struct character 51*404b540aSrobert { 52*404b540aSrobert typedef V value_type; 53*404b540aSrobert typedef I int_type; 54*404b540aSrobert typedef S state_type; 55*404b540aSrobert typedef character<V, I, S> char_type; 56*404b540aSrobert 57*404b540aSrobert value_type value; 58*404b540aSrobert 59*404b540aSrobert template<typename V2> 60*404b540aSrobert static char_type fromcharacter61*404b540aSrobert from(const V2& v) 62*404b540aSrobert { 63*404b540aSrobert char_type ret = { static_cast<value_type>(v) }; 64*404b540aSrobert return ret; 65*404b540aSrobert } 66*404b540aSrobert 67*404b540aSrobert template<typename V2> 68*404b540aSrobert static V2 tocharacter69*404b540aSrobert to(const char_type& c) 70*404b540aSrobert { 71*404b540aSrobert V2 ret = { static_cast<V2>(c.value) }; 72*404b540aSrobert return ret; 73*404b540aSrobert } 74*404b540aSrobert 75*404b540aSrobert }; 76*404b540aSrobert 77*404b540aSrobert template<typename V, typename I, typename S> 78*404b540aSrobert inline bool 79*404b540aSrobert operator==(const character<V, I, S>& lhs, const character<V, I, S>& rhs) 80*404b540aSrobert { return lhs.value == rhs.value; } 81*404b540aSrobert 82*404b540aSrobert template<typename V, typename I, typename S> 83*404b540aSrobert inline bool 84*404b540aSrobert operator<(const character<V, I, S>& lhs, const character<V, I, S>& rhs) 85*404b540aSrobert { return lhs.value < rhs.value; } 86*404b540aSrobert 87*404b540aSrobert _GLIBCXX_END_NAMESPACE 88*404b540aSrobert 89*404b540aSrobert _GLIBCXX_BEGIN_NAMESPACE(std) 90*404b540aSrobert 91*404b540aSrobert /// char_traits<__gnu_cxx::character> specialization. 92*404b540aSrobert template<typename V, typename I, typename S> 93*404b540aSrobert struct char_traits<__gnu_cxx::character<V, I, S> > 94*404b540aSrobert { 95*404b540aSrobert typedef __gnu_cxx::character<V, I, S> char_type; 96*404b540aSrobert typedef typename char_type::int_type int_type; 97*404b540aSrobert typedef typename char_type::state_type state_type; 98*404b540aSrobert typedef fpos<state_type> pos_type; 99*404b540aSrobert typedef streamoff off_type; 100*404b540aSrobert 101*404b540aSrobert static void 102*404b540aSrobert assign(char_type& __c1, const char_type& __c2) 103*404b540aSrobert { __c1 = __c2; } 104*404b540aSrobert 105*404b540aSrobert static bool 106*404b540aSrobert eq(const char_type& __c1, const char_type& __c2) 107*404b540aSrobert { return __c1 == __c2; } 108*404b540aSrobert 109*404b540aSrobert static bool 110*404b540aSrobert lt(const char_type& __c1, const char_type& __c2) 111*404b540aSrobert { return __c1 < __c2; } 112*404b540aSrobert 113*404b540aSrobert static int 114*404b540aSrobert compare(const char_type* __s1, const char_type* __s2, size_t __n) 115*404b540aSrobert { 116*404b540aSrobert for (size_t __i = 0; __i < __n; ++__i) 117*404b540aSrobert if (!eq(__s1[__i], __s2[__i])) 118*404b540aSrobert return lt(__s1[__i], __s2[__i]) ? -1 : 1; 119*404b540aSrobert return 0; 120*404b540aSrobert } 121*404b540aSrobert 122*404b540aSrobert static size_t 123*404b540aSrobert length(const char_type* __s) 124*404b540aSrobert { 125*404b540aSrobert const char_type* __p = __s; 126*404b540aSrobert while (__p->value) 127*404b540aSrobert ++__p; 128*404b540aSrobert return (__p - __s); 129*404b540aSrobert } 130*404b540aSrobert 131*404b540aSrobert static const char_type* 132*404b540aSrobert find(const char_type* __s, size_t __n, const char_type& __a) 133*404b540aSrobert { 134*404b540aSrobert for (const char_type* __p = __s; size_t(__p - __s) < __n; ++__p) 135*404b540aSrobert if (*__p == __a) 136*404b540aSrobert return __p; 137*404b540aSrobert return 0; 138*404b540aSrobert } 139*404b540aSrobert 140*404b540aSrobert static char_type* 141*404b540aSrobert move(char_type* __s1, const char_type* __s2, size_t __n) 142*404b540aSrobert { 143*404b540aSrobert return static_cast<char_type*>(std::memmove(__s1, __s2, 144*404b540aSrobert __n * sizeof(char_type))); 145*404b540aSrobert } 146*404b540aSrobert 147*404b540aSrobert static char_type* 148*404b540aSrobert copy(char_type* __s1, const char_type* __s2, size_t __n) 149*404b540aSrobert { 150*404b540aSrobert std::copy(__s2, __s2 + __n, __s1); 151*404b540aSrobert return __s1; 152*404b540aSrobert } 153*404b540aSrobert 154*404b540aSrobert static char_type* 155*404b540aSrobert assign(char_type* __s, size_t __n, char_type __a) 156*404b540aSrobert { 157*404b540aSrobert std::fill_n(__s, __n, __a); 158*404b540aSrobert return __s; 159*404b540aSrobert } 160*404b540aSrobert 161*404b540aSrobert static char_type 162*404b540aSrobert to_char_type(const int_type& __i) 163*404b540aSrobert { return char_type::template from(__i); } 164*404b540aSrobert 165*404b540aSrobert static int_type 166*404b540aSrobert to_int_type(const char_type& __c) 167*404b540aSrobert { return char_type::template to<int_type>(__c); } 168*404b540aSrobert 169*404b540aSrobert static bool 170*404b540aSrobert eq_int_type(const int_type& __c1, const int_type& __c2) 171*404b540aSrobert { return __c1 == __c2; } 172*404b540aSrobert 173*404b540aSrobert static int_type 174*404b540aSrobert eof() 175*404b540aSrobert { 176*404b540aSrobert int_type __r = { -1 }; 177*404b540aSrobert return __r; 178*404b540aSrobert } 179*404b540aSrobert 180*404b540aSrobert static int_type 181*404b540aSrobert not_eof(const int_type& __c) 182*404b540aSrobert { return eq_int_type(__c, eof()) ? int_type() : __c; } 183*404b540aSrobert }; 184*404b540aSrobert 185*404b540aSrobert _GLIBCXX_END_NAMESPACE 186*404b540aSrobert 187*404b540aSrobert #endif 188