1 // Versatile string utility -*- C++ -*-
2 
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library.  This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
10 // any later version.
11 
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16 
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
20 
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24 // <http://www.gnu.org/licenses/>.
25 
26 /** @file ext/vstring_util.h
27  *  This is an internal header file, included by other library headers.
28  *  Do not attempt to use it directly. @headername{ext/vstring.h}
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 #include <bits/range_access.h>
46 
47 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
48 {
49 _GLIBCXX_BEGIN_NAMESPACE_VERSION
50 
51   template<typename _CharT, typename _Traits, typename _Alloc>
52     struct __vstring_utility
53     {
54       typedef typename _Alloc::template rebind<_CharT>::other _CharT_alloc_type;
55 
56       typedef _Traits					    traits_type;
57       typedef typename _Traits::char_type		    value_type;
58       typedef typename _CharT_alloc_type::size_type	    size_type;
59       typedef typename _CharT_alloc_type::difference_type   difference_type;
60       typedef typename _CharT_alloc_type::pointer	    pointer;
61       typedef typename _CharT_alloc_type::const_pointer	    const_pointer;
62 
63       // For __sso_string.
64       typedef __gnu_cxx::
65       __normal_iterator<pointer, __gnu_cxx::
66 			__versa_string<_CharT, _Traits, _Alloc,
67 				       __sso_string_base> >
68 	__sso_iterator;
69       typedef __gnu_cxx::
70       __normal_iterator<const_pointer, __gnu_cxx::
71 			__versa_string<_CharT, _Traits, _Alloc,
72 				       __sso_string_base> >
73 	__const_sso_iterator;
74 
75       // For __rc_string.
76       typedef __gnu_cxx::
77       __normal_iterator<pointer, __gnu_cxx::
78 			__versa_string<_CharT, _Traits, _Alloc,
79 				       __rc_string_base> >
80 	__rc_iterator;
81       typedef __gnu_cxx::
82       __normal_iterator<const_pointer, __gnu_cxx::
83 			__versa_string<_CharT, _Traits, _Alloc,
84 				       __rc_string_base> >
85 	__const_rc_iterator;
86 
87       // NB:  When the allocator is empty, deriving from it saves space
88       // (http://www.cantrip.org/emptyopt.html).
89       template<typename _Alloc1>
90 	struct _Alloc_hider
91 	: public _Alloc1
92 	{
93 	  _Alloc_hider(_CharT* __ptr)
94 	  : _Alloc1(), _M_p(__ptr) { }
95 
96 	  _Alloc_hider(const _Alloc1& __a, _CharT* __ptr)
97 	  : _Alloc1(__a), _M_p(__ptr) { }
98 
99 	  _CharT*  _M_p; // The actual data.
100 	};
101 
102       // When __n = 1 way faster than the general multichar
103       // traits_type::copy/move/assign.
104       static void
105       _S_copy(_CharT* __d, const _CharT* __s, size_type __n)
106       {
107 	if (__n == 1)
108 	  traits_type::assign(*__d, *__s);
109 	else
110 	  traits_type::copy(__d, __s, __n);
111       }
112 
113       static void
114       _S_move(_CharT* __d, const _CharT* __s, size_type __n)
115       {
116 	if (__n == 1)
117 	  traits_type::assign(*__d, *__s);
118 	else
119 	  traits_type::move(__d, __s, __n);
120       }
121 
122       static void
123       _S_assign(_CharT* __d, size_type __n, _CharT __c)
124       {
125 	if (__n == 1)
126 	  traits_type::assign(*__d, __c);
127 	else
128 	  traits_type::assign(__d, __n, __c);
129       }
130 
131       // _S_copy_chars is a separate template to permit specialization
132       // to optimize for the common case of pointers as iterators.
133       template<typename _Iterator>
134 	static void
135 	_S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
136 	{
137 	  for (; __k1 != __k2; ++__k1, ++__p)
138 	    traits_type::assign(*__p, *__k1); // These types are off.
139 	}
140 
141       static void
142       _S_copy_chars(_CharT* __p, __sso_iterator __k1, __sso_iterator __k2)
143       { _S_copy_chars(__p, __k1.base(), __k2.base()); }
144 
145       static void
146       _S_copy_chars(_CharT* __p, __const_sso_iterator __k1,
147 		    __const_sso_iterator __k2)
148       { _S_copy_chars(__p, __k1.base(), __k2.base()); }
149 
150       static void
151       _S_copy_chars(_CharT* __p, __rc_iterator __k1, __rc_iterator __k2)
152       { _S_copy_chars(__p, __k1.base(), __k2.base()); }
153 
154       static void
155       _S_copy_chars(_CharT* __p, __const_rc_iterator __k1,
156 		    __const_rc_iterator __k2)
157       { _S_copy_chars(__p, __k1.base(), __k2.base()); }
158 
159       static void
160       _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2)
161       { _S_copy(__p, __k1, __k2 - __k1); }
162 
163       static void
164       _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
165       { _S_copy(__p, __k1, __k2 - __k1); }
166 
167       static int
168       _S_compare(size_type __n1, size_type __n2)
169       {
170 	const difference_type __d = difference_type(__n1 - __n2);
171 
172 	if (__d > __numeric_traits_integer<int>::__max)
173 	  return __numeric_traits_integer<int>::__max;
174 	else if (__d < __numeric_traits_integer<int>::__min)
175 	  return __numeric_traits_integer<int>::__min;
176 	else
177 	  return int(__d);
178       }
179     };
180 
181 _GLIBCXX_END_NAMESPACE_VERSION
182 } // namespace
183 
184 #endif /* _VSTRING_UTIL_H */
185