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