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