xref: /netbsd-src/external/gpl3/gcc.old/dist/libstdc++-v3/include/ext/vstring_util.h (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 // Versatile string utility -*- C++ -*-
2 
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009 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/vstring_util.h
26  *  This file is a GNU extension to the Standard C++ Library.
27  *  This is an internal header file, included by other library headers.
28  *  You should not attempt to use it directly.
29  */
30 
31 #ifndef _VSTRING_UTIL_H
32 #define _VSTRING_UTIL_H 1
33 
34 #pragma GCC system_header
35 
36 #include <ext/vstring_fwd.h>
37 #include <debug/debug.h>
38 #include <bits/stl_function.h>  // For less
39 #include <bits/functexcept.h>
40 #include <bits/localefwd.h>
41 #include <bits/ostream_insert.h>
42 #include <bits/stl_iterator.h>
43 #include <ext/numeric_traits.h>
44 #include <bits/move.h>
45 
46 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
47 
48   template<typename _CharT, typename _Traits, typename _Alloc>
49     struct __vstring_utility
50     {
51       typedef typename _Alloc::template rebind<_CharT>::other _CharT_alloc_type;
52 
53       typedef _Traits					    traits_type;
54       typedef typename _Traits::char_type		    value_type;
55       typedef typename _CharT_alloc_type::size_type	    size_type;
56       typedef typename _CharT_alloc_type::difference_type   difference_type;
57       typedef typename _CharT_alloc_type::pointer	    pointer;
58       typedef typename _CharT_alloc_type::const_pointer	    const_pointer;
59 
60       // For __sso_string.
61       typedef __gnu_cxx::
62       __normal_iterator<pointer, __gnu_cxx::
63 			__versa_string<_CharT, _Traits, _Alloc,
64 				       __sso_string_base> >
65         __sso_iterator;
66       typedef __gnu_cxx::
67       __normal_iterator<const_pointer, __gnu_cxx::
68 			__versa_string<_CharT, _Traits, _Alloc,
69 				       __sso_string_base> >
70         __const_sso_iterator;
71 
72       // For __rc_string.
73       typedef __gnu_cxx::
74       __normal_iterator<pointer, __gnu_cxx::
75 			__versa_string<_CharT, _Traits, _Alloc,
76 				       __rc_string_base> >
77         __rc_iterator;
78       typedef __gnu_cxx::
79       __normal_iterator<const_pointer, __gnu_cxx::
80 			__versa_string<_CharT, _Traits, _Alloc,
81 				       __rc_string_base> >
82         __const_rc_iterator;
83 
84       // NB:  When the allocator is empty, deriving from it saves space
85       // (http://www.cantrip.org/emptyopt.html).
86       template<typename _Alloc1>
87         struct _Alloc_hider
88 	: public _Alloc1
89 	{
90 	  _Alloc_hider(_CharT* __ptr)
91 	  : _Alloc1(), _M_p(__ptr) { }
92 
93 	  _Alloc_hider(const _Alloc1& __a, _CharT* __ptr)
94 	  : _Alloc1(__a), _M_p(__ptr) { }
95 
96 	  _CharT*  _M_p; // The actual data.
97 	};
98 
99       // When __n = 1 way faster than the general multichar
100       // traits_type::copy/move/assign.
101       static void
102       _S_copy(_CharT* __d, const _CharT* __s, size_type __n)
103       {
104 	if (__n == 1)
105 	  traits_type::assign(*__d, *__s);
106 	else
107 	  traits_type::copy(__d, __s, __n);
108       }
109 
110       static void
111       _S_move(_CharT* __d, const _CharT* __s, size_type __n)
112       {
113 	if (__n == 1)
114 	  traits_type::assign(*__d, *__s);
115 	else
116 	  traits_type::move(__d, __s, __n);
117       }
118 
119       static void
120       _S_assign(_CharT* __d, size_type __n, _CharT __c)
121       {
122 	if (__n == 1)
123 	  traits_type::assign(*__d, __c);
124 	else
125 	  traits_type::assign(__d, __n, __c);
126       }
127 
128       // _S_copy_chars is a separate template to permit specialization
129       // to optimize for the common case of pointers as iterators.
130       template<typename _Iterator>
131         static void
132         _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
133         {
134 	  for (; __k1 != __k2; ++__k1, ++__p)
135 	    traits_type::assign(*__p, *__k1); // These types are off.
136 	}
137 
138       static void
139       _S_copy_chars(_CharT* __p, __sso_iterator __k1, __sso_iterator __k2)
140       { _S_copy_chars(__p, __k1.base(), __k2.base()); }
141 
142       static void
143       _S_copy_chars(_CharT* __p, __const_sso_iterator __k1,
144 		    __const_sso_iterator __k2)
145       { _S_copy_chars(__p, __k1.base(), __k2.base()); }
146 
147       static void
148       _S_copy_chars(_CharT* __p, __rc_iterator __k1, __rc_iterator __k2)
149       { _S_copy_chars(__p, __k1.base(), __k2.base()); }
150 
151       static void
152       _S_copy_chars(_CharT* __p, __const_rc_iterator __k1,
153 		    __const_rc_iterator __k2)
154       { _S_copy_chars(__p, __k1.base(), __k2.base()); }
155 
156       static void
157       _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2)
158       { _S_copy(__p, __k1, __k2 - __k1); }
159 
160       static void
161       _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
162       { _S_copy(__p, __k1, __k2 - __k1); }
163 
164       static int
165       _S_compare(size_type __n1, size_type __n2)
166       {
167 	const difference_type __d = difference_type(__n1 - __n2);
168 
169 	if (__d > __numeric_traits_integer<int>::__max)
170 	  return __numeric_traits_integer<int>::__max;
171 	else if (__d < __numeric_traits_integer<int>::__min)
172 	  return __numeric_traits_integer<int>::__min;
173 	else
174 	  return int(__d);
175       }
176     };
177 
178 _GLIBCXX_END_NAMESPACE
179 
180 #endif /* _VSTRING_UTIL_H */
181