1 // Versatile string utility -*- C++ -*-
3 // Copyright (C) 2005 Free Software Foundation, Inc.
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 2, or (at your option)
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.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 /** @file ext/vstring_util.h
31 * This file is a GNU extension to the Standard C++ Library.
32 * This is an internal header file, included by other library headers.
33 * You should not attempt to use it directly.
36 #ifndef _VSTRING_UTIL_H
37 #define _VSTRING_UTIL_H 1
39 #pragma GCC system_header
41 #include <ext/vstring_fwd.h>
42 #include <debug/debug.h>
43 #include <bits/stl_function.h> // For less
44 #include <bits/functexcept.h>
46 #include <algorithm> // For std::distance, srd::search.
48 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
50 template<typename _CharT, typename _Traits, typename _Alloc>
51 struct __vstring_utility
53 typedef typename _Alloc::template rebind<_CharT>::other _CharT_alloc_type;
55 typedef _Traits traits_type;
56 typedef typename _Traits::char_type value_type;
57 typedef typename _CharT_alloc_type::size_type size_type;
58 typedef typename _CharT_alloc_type::pointer pointer;
59 typedef typename _CharT_alloc_type::const_pointer const_pointer;
63 __normal_iterator<pointer, __gnu_cxx::
64 __versa_string<_CharT, _Traits, _Alloc,
68 __normal_iterator<const_pointer, __gnu_cxx::
69 __versa_string<_CharT, _Traits, _Alloc,
75 __normal_iterator<pointer, __gnu_cxx::
76 __versa_string<_CharT, _Traits, _Alloc,
80 __normal_iterator<const_pointer, __gnu_cxx::
81 __versa_string<_CharT, _Traits, _Alloc,
85 // NB: When the allocator is empty, deriving from it saves space
86 // (http://www.cantrip.org/emptyopt.html). We do that anyway for
88 template<typename _Alloc1, bool = std::__is_empty<_Alloc1>::__value>
92 _Alloc_hider(const _Alloc1& __a, _CharT* __ptr)
93 : _Alloc1(__a), _M_p(__ptr) { }
96 _M_alloc_swap(_Alloc_hider& __ah)
98 // Implement Option 3 of DR 431 (see N1599).
99 // Precondition: swappable allocators.
100 _Alloc1& __this = static_cast<_Alloc1&>(*this);
101 _Alloc1& __that = static_cast<_Alloc1&>(__ah);
102 if (__this != __that)
103 swap(__this, __that);
106 _CharT* _M_p; // The actual data.
109 template<typename _Alloc1>
110 struct _Alloc_hider<_Alloc1, true>
113 _Alloc_hider(const _Alloc1& __a, _CharT* __ptr)
114 : _Alloc1(__a), _M_p(__ptr) { }
116 void _M_alloc_swap(_Alloc_hider&) { }
118 _CharT* _M_p; // The actual data.
121 // For use in _M_construct (_S_construct) forward_iterator_tag.
122 template<typename _Type>
124 _S_is_null_pointer(_Type* __ptr)
125 { return __ptr == 0; }
127 template<typename _Type>
129 _S_is_null_pointer(_Type)
132 // When __n = 1 way faster than the general multichar
133 // traits_type::copy/move/assign.
135 _S_copy(_CharT* __d, const _CharT* __s, size_type __n)
138 traits_type::assign(*__d, *__s);
140 traits_type::copy(__d, __s, __n);
144 _S_move(_CharT* __d, const _CharT* __s, size_type __n)
147 traits_type::assign(*__d, *__s);
149 traits_type::move(__d, __s, __n);
153 _S_assign(_CharT* __d, size_type __n, _CharT __c)
156 traits_type::assign(*__d, __c);
158 traits_type::assign(__d, __n, __c);
161 // _S_copy_chars is a separate template to permit specialization
162 // to optimize for the common case of pointers as iterators.
163 template<typename _Iterator>
165 _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
167 for (; __k1 != __k2; ++__k1, ++__p)
168 traits_type::assign(*__p, *__k1); // These types are off.
172 _S_copy_chars(_CharT* __p, __sso_iterator __k1, __sso_iterator __k2)
173 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
176 _S_copy_chars(_CharT* __p, __const_sso_iterator __k1,
177 __const_sso_iterator __k2)
178 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
181 _S_copy_chars(_CharT* __p, __rc_iterator __k1, __rc_iterator __k2)
182 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
185 _S_copy_chars(_CharT* __p, __const_rc_iterator __k1,
186 __const_rc_iterator __k2)
187 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
190 _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2)
191 { _S_copy(__p, __k1, __k2 - __k1); }
194 _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
195 { _S_copy(__p, __k1, __k2 - __k1); }
198 _GLIBCXX_END_NAMESPACE
200 #endif /* _VSTRING_UTIL_H */