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