# pod_char_traits.h

Go to the documentation of this file.

    00001 // POD character, std::char_traits specialization -*- C++ -*-
    00002
    00003 // Copyright (C) 2002, 2003, 2004, 2005, 2007, 2009 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 #include <string>
    00036
    00037 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
    00038
    00039   // POD character abstraction.
    00040   // NB: The char_type parameter is a subset of int_type, as to allow
    00041   // int_type to properly hold the full range of char_type values as
    00042   // well as EOF.
    00043   /// @brief A POD class that serves as a character abstraction class.
    00044   template<typename V, typename I, typename S = std::mbstate_t>
    00045     struct character
    00046     {
    00047       typedef V             value_type;
    00048       typedef I             int_type;
    00049       typedef S             state_type;
    00050       typedef character<V, I, S>    char_type;
    00051
    00052       value_type    value;
    00053
    00054       template<typename V2>
    00055         static char_type
    00056         from(const V2& v)
    00057         {
    00058       char_type ret = { static_cast<value_type>(v) };
    00059       return ret;
    00060     }
    00061
    00062       template<typename V2>
    00063         static V2
    00064         to(const char_type& c)
    00065         {
    00066       V2 ret = { static_cast<V2>(c.value) };
    00067       return ret;
    00068     }
    00069
    00070     };
    00071
    00072   template<typename V, typename I, typename S>
    00073     inline bool
    00074     operator==(const character<V, I, S>& lhs, const character<V, I, S>& rhs)
    00075     { return lhs.value == rhs.value; }
    00076
    00077   template<typename V, typename I, typename S>
    00078     inline bool
    00079     operator<(const character<V, I, S>& lhs, const character<V, I, S>& rhs)
    00080     { return lhs.value < rhs.value; }
    00081
    00082 _GLIBCXX_END_NAMESPACE
    00083
    00084 _GLIBCXX_BEGIN_NAMESPACE(std)
    00085
    00086   /// char_traits<__gnu_cxx::character> specialization.
    00087   template<typename V, typename I, typename S>
    00088     struct char_traits<__gnu_cxx::character<V, I, S> >
    00089     {
    00090       typedef __gnu_cxx::character<V, I, S> char_type;
    00091       typedef typename char_type::int_type  int_type;
    00092       typedef typename char_type::state_type    state_type;
    00093       typedef fpos<state_type>          pos_type;
    00094       typedef streamoff             off_type;
    00095
    00096       static void
    00097       assign(char_type& __c1, const char_type& __c2)
    00098       { __c1 = __c2; }
    00099
    00100       static bool
    00101       eq(const char_type& __c1, const char_type& __c2)
    00102       { return __c1 == __c2; }
    00103
    00104       static bool
    00105       lt(const char_type& __c1, const char_type& __c2)
    00106       { return __c1 < __c2; }
    00107
    00108       static int
    00109       compare(const char_type* __s1, const char_type* __s2, size_t __n)
    00110       {
    00111     for (size_t __i = 0; __i < __n; ++__i)
    00112       if (!eq(__s1[__i], __s2[__i]))
    00113         return lt(__s1[__i], __s2[__i]) ? -1 : 1;
    00114     return 0;
    00115       }
    00116
    00117       static size_t
    00118       length(const char_type* __s)
    00119       {
    00120     const char_type* __p = __s;
    00121     while (__p->value)
    00122       ++__p;
    00123     return (__p - __s);
    00124       }
    00125
    00126       static const char_type*
    00127       find(const char_type* __s, size_t __n, const char_type& __a)
    00128       {
    00129     for (const char_type* __p = __s; size_t(__p - __s) < __n; ++__p)
    00130       if (*__p == __a)
    00131         return __p;
    00132     return 0;
    00133       }
    00134
    00135       static char_type*
    00136       move(char_type* __s1, const char_type* __s2, size_t __n)
    00137       {
    00138     return static_cast<char_type*>
    00139       (__builtin_memmove(__s1, __s2, __n * sizeof(char_type)));
    00140       }
    00141
    00142       static char_type*
    00143       copy(char_type* __s1, const char_type* __s2, size_t __n)
    00144       {
    00145     std::copy(__s2, __s2 + __n, __s1);
    00146     return __s1;
    00147       }
    00148
    00149       static char_type*
    00150       assign(char_type* __s, size_t __n, char_type __a)
    00151       {
    00152     std::fill_n(__s, __n, __a);
    00153         return __s;
    00154       }
    00155
    00156       static char_type
    00157       to_char_type(const int_type& __i)
    00158       { return char_type::template from(__i); }
    00159
    00160       static int_type
    00161       to_int_type(const char_type& __c)
    00162       { return char_type::template to<int_type>(__c); }
    00163
    00164       static bool
    00165       eq_int_type(const int_type& __c1, const int_type& __c2)
    00166       { return __c1 == __c2; }
    00167
    00168       static int_type
    00169       eof()
    00170       {
    00171     int_type __r = { -1 };
    00172     return __r;
    00173       }
    00174
    00175       static int_type
    00176       not_eof(const int_type& __c)
    00177       { return eq_int_type(__c, eof()) ? int_type() : __c; }
    00178     };
    00179
    00180 _GLIBCXX_END_NAMESPACE
    00181
    00182 #endif