(root)/
gcc-13.2.0/
libstdc++-v3/
include/
ext/
pod_char_traits.h
       1  // POD character, std::char_traits specialization -*- C++ -*-
       2  
       3  // Copyright (C) 2002-2023 Free Software Foundation, Inc.
       4  //
       5  // This file is part of the GNU ISO C++ Library.  This library is free
       6  // software; you can redistribute it and/or modify it under the
       7  // terms of the GNU General Public License as published by the
       8  // Free Software Foundation; either version 3, or (at your option)
       9  // any later version.
      10  
      11  // This library is distributed in the hope that it will be useful,
      12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
      13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14  // GNU General Public License for more details.
      15  
      16  // Under Section 7 of GPL version 3, you are granted additional
      17  // permissions described in the GCC Runtime Library Exception, version
      18  // 3.1, as published by the Free Software Foundation.
      19  
      20  // You should have received a copy of the GNU General Public License and
      21  // a copy of the GCC Runtime Library Exception along with this program;
      22  // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
      23  // <http://www.gnu.org/licenses/>.
      24  
      25  /** @file ext/pod_char_traits.h
      26   *  This file is a GNU extension to the Standard C++ Library.
      27   */
      28  
      29  // Gabriel Dos Reis <gdr@integrable-solutions.net>
      30  // Benjamin Kosnik <bkoz@redhat.com>
      31  
      32  #ifndef _POD_CHAR_TRAITS_H
      33  #define _POD_CHAR_TRAITS_H 1
      34  
      35  #pragma GCC system_header
      36  
      37  #include <bits/requires_hosted.h> // GNU extensions are currently omitted
      38  
      39  #include <string>
      40  
      41  namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
      42  {
      43  _GLIBCXX_BEGIN_NAMESPACE_VERSION
      44  
      45    // POD character abstraction.
      46    // NB: The char_type parameter is a subset of int_type, as to allow
      47    // int_type to properly hold the full range of char_type values as
      48    // well as EOF.
      49    /// @brief A POD class that serves as a character abstraction class.
      50    template<typename _Value, typename _Int, typename _St = std::mbstate_t>
      51      struct character
      52      {
      53        typedef _Value				value_type;
      54        typedef _Int				int_type;
      55        typedef _St				state_type;
      56        typedef character<_Value, _Int, _St>	char_type;
      57  
      58        value_type	value;
      59  
      60        template<typename V2>
      61          static char_type
      62          from(const V2& v)
      63          {
      64  	  char_type ret = { static_cast<value_type>(v) };
      65  	  return ret;
      66  	}
      67  
      68        template<typename V2>
      69          static V2
      70          to(const char_type& c)
      71          {
      72  	  V2 ret = { static_cast<V2>(c.value) };
      73  	  return ret;
      74  	}
      75  
      76      };
      77  
      78    template<typename _Value, typename _Int, typename _St>
      79      inline bool
      80      operator==(const character<_Value, _Int, _St>& lhs,
      81  	       const character<_Value, _Int, _St>& rhs)
      82      { return lhs.value == rhs.value; }
      83  
      84    template<typename _Value, typename _Int, typename _St>
      85      inline bool
      86      operator<(const character<_Value, _Int, _St>& lhs,
      87  	      const character<_Value, _Int, _St>& rhs)
      88      { return lhs.value < rhs.value; }
      89  
      90  _GLIBCXX_END_NAMESPACE_VERSION
      91  } // namespace
      92  
      93  namespace std _GLIBCXX_VISIBILITY(default)
      94  {
      95  _GLIBCXX_BEGIN_NAMESPACE_VERSION
      96  
      97    /// char_traits<__gnu_cxx::character> specialization.
      98    template<typename _Value, typename _Int, typename _St>
      99      struct char_traits<__gnu_cxx::character<_Value, _Int, _St> >
     100      {
     101        typedef __gnu_cxx::character<_Value, _Int, _St>	char_type;
     102        typedef typename char_type::int_type		int_type;
     103        typedef typename char_type::state_type		state_type;
     104        typedef fpos<state_type>				pos_type;
     105        typedef streamoff					off_type;
     106  
     107        static void
     108        assign(char_type& __c1, const char_type& __c2)
     109        { __c1 = __c2; }
     110  
     111        static bool
     112        eq(const char_type& __c1, const char_type& __c2)
     113        { return __c1 == __c2; }
     114  
     115        static bool
     116        lt(const char_type& __c1, const char_type& __c2)
     117        { return __c1 < __c2; }
     118  
     119        static int
     120        compare(const char_type* __s1, const char_type* __s2, size_t __n)
     121        {
     122  	for (size_t __i = 0; __i < __n; ++__i)
     123  	  if (!eq(__s1[__i], __s2[__i]))
     124  	    return lt(__s1[__i], __s2[__i]) ? -1 : 1;
     125  	return 0;
     126        }
     127  
     128        static size_t
     129        length(const char_type* __s)
     130        {
     131  	const char_type* __p = __s;
     132  	while (__p->value)
     133  	  ++__p;
     134  	return (__p - __s);
     135        }
     136  
     137        static const char_type*
     138        find(const char_type* __s, size_t __n, const char_type& __a)
     139        {
     140  	for (const char_type* __p = __s; size_t(__p - __s) < __n; ++__p)
     141  	  if (*__p == __a)
     142  	    return __p;
     143  	return 0;
     144        }
     145  
     146        static char_type*
     147        move(char_type* __s1, const char_type* __s2, size_t __n)
     148        { 
     149  	if (__n == 0)
     150  	  return __s1;
     151  	return static_cast<char_type*>
     152  	  (__builtin_memmove(__s1, __s2, __n * sizeof(char_type)));
     153        }
     154  
     155        static char_type*
     156        copy(char_type* __s1, const char_type* __s2, size_t __n)
     157        {
     158  	if (__n == 0)
     159  	  return __s1;
     160  	std::copy(__s2, __s2 + __n, __s1);
     161  	return __s1;
     162        }
     163  
     164        static char_type*
     165        assign(char_type* __s, size_t __n, char_type __a)
     166        {
     167  	std::fill_n(__s, __n, __a);
     168          return __s;
     169        }
     170  
     171        static char_type
     172        to_char_type(const int_type& __i)
     173        { return char_type::template from(__i); }
     174  
     175        static int_type
     176        to_int_type(const char_type& __c)
     177        { return char_type::template to<int_type>(__c); }
     178  
     179        static bool
     180        eq_int_type(const int_type& __c1, const int_type& __c2)
     181        { return __c1 == __c2; }
     182  
     183        static int_type
     184        eof() 
     185        {
     186  	int_type __r = { static_cast<typename __gnu_cxx::__conditional_type
     187  			 <std::__is_integer<int_type>::__value,
     188  			 int_type, int>::__type>(-1) };
     189  	return __r;
     190        }
     191  
     192        static int_type
     193        not_eof(const int_type& __c)
     194        { return eq_int_type(__c, eof()) ? int_type() : __c; }
     195      };
     196  
     197  _GLIBCXX_END_NAMESPACE_VERSION
     198  } // namespace
     199  
     200  #endif