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