1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_STRING
11#define _LIBCPP_STRING
12
13// clang-format off
14
15/*
16 string synopsis
17
18#include <compare>
19#include <initializer_list>
20
21namespace std
22{
23
24template <class stateT>
25class fpos
26{
27private:
28 stateT st;
29public:
30 fpos(streamoff = streamoff());
31
32 operator streamoff() const;
33
34 stateT state() const;
35 void state(stateT);
36
37 fpos& operator+=(streamoff);
38 fpos operator+ (streamoff) const;
39 fpos& operator-=(streamoff);
40 fpos operator- (streamoff) const;
41};
42
43template <class stateT> streamoff operator-(const fpos<stateT>& x, const fpos<stateT>& y);
44
45template <class stateT> bool operator==(const fpos<stateT>& x, const fpos<stateT>& y);
46template <class stateT> bool operator!=(const fpos<stateT>& x, const fpos<stateT>& y);
47
48template <class charT>
49struct char_traits
50{
51 using char_type = charT;
52 using int_type = ...;
53 using off_type = streamoff;
54 using pos_type = streampos;
55 using state_type = mbstate_t;
56 using comparison_category = strong_ordering; // Since C++20 only for the specializations
57 // char, wchar_t, char8_t, char16_t, and char32_t.
58
59 static void assign(char_type& c1, const char_type& c2) noexcept;
60 static constexpr bool eq(char_type c1, char_type c2) noexcept;
61 static constexpr bool lt(char_type c1, char_type c2) noexcept;
62
63 static int compare(const char_type* s1, const char_type* s2, size_t n);
64 static size_t length(const char_type* s);
65 static const char_type* find(const char_type* s, size_t n, const char_type& a);
66 static char_type* move(char_type* s1, const char_type* s2, size_t n);
67 static char_type* copy(char_type* s1, const char_type* s2, size_t n);
68 static char_type* assign(char_type* s, size_t n, char_type a);
69
70 static constexpr int_type not_eof(int_type c) noexcept;
71 static constexpr char_type to_char_type(int_type c) noexcept;
72 static constexpr int_type to_int_type(char_type c) noexcept;
73 static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept;
74 static constexpr int_type eof() noexcept;
75};
76
77template <> struct char_traits<char>;
78template <> struct char_traits<wchar_t>;
79template <> struct char_traits<char8_t>; // C++20
80template <> struct char_traits<char16_t>;
81template <> struct char_traits<char32_t>;
82
83template<class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
84class basic_string
85{
86public:
87// types:
88 typedef traits traits_type;
89 typedef typename traits_type::char_type value_type;
90 typedef Allocator allocator_type;
91 typedef typename allocator_type::size_type size_type;
92 typedef typename allocator_type::difference_type difference_type;
93 typedef typename allocator_type::reference reference;
94 typedef typename allocator_type::const_reference const_reference;
95 typedef typename allocator_type::pointer pointer;
96 typedef typename allocator_type::const_pointer const_pointer;
97 typedef implementation-defined iterator;
98 typedef implementation-defined const_iterator;
99 typedef std::reverse_iterator<iterator> reverse_iterator;
100 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
101
102 static const size_type npos = -1;
103
104 basic_string()
105 noexcept(is_nothrow_default_constructible<allocator_type>::value); // constexpr since C++20
106 explicit basic_string(const allocator_type& a); // constexpr since C++20
107 basic_string(const basic_string& str); // constexpr since C++20
108 basic_string(basic_string&& str)
109 noexcept(is_nothrow_move_constructible<allocator_type>::value); // constexpr since C++20
110 basic_string(const basic_string& str, size_type pos,
111 const allocator_type& a = allocator_type()); // constexpr since C++20
112 basic_string(const basic_string& str, size_type pos, size_type n,
113 const Allocator& a = Allocator()); // constexpr since C++20
114 constexpr basic_string(
115 basic_string&& str, size_type pos, const Allocator& a = Allocator()); // since C++23
116 constexpr basic_string(
117 basic_string&& str, size_type pos, size_type n, const Allocator& a = Allocator()); // since C++23
118 template<class T>
119 basic_string(const T& t, size_type pos, size_type n, const Allocator& a = Allocator()); // C++17, constexpr since C++20
120 template <class T>
121 explicit basic_string(const T& t, const Allocator& a = Allocator()); // C++17, constexpr since C++20
122 basic_string(const value_type* s, const allocator_type& a = allocator_type()); // constexpr since C++20
123 basic_string(const value_type* s, size_type n, const allocator_type& a = allocator_type()); // constexpr since C++20
124 basic_string(nullptr_t) = delete; // C++23
125 basic_string(size_type n, value_type c, const allocator_type& a = allocator_type()); // constexpr since C++20
126 template<class InputIterator>
127 basic_string(InputIterator begin, InputIterator end,
128 const allocator_type& a = allocator_type()); // constexpr since C++20
129 template<container-compatible-range<charT> R>
130 constexpr basic_string(from_range_t, R&& rg, const Allocator& a = Allocator()); // since C++23
131 basic_string(initializer_list<value_type>, const Allocator& = Allocator()); // constexpr since C++20
132 basic_string(const basic_string&, const Allocator&); // constexpr since C++20
133 basic_string(basic_string&&, const Allocator&); // constexpr since C++20
134
135 ~basic_string(); // constexpr since C++20
136
137 operator basic_string_view<charT, traits>() const noexcept; // constexpr since C++20
138
139 basic_string& operator=(const basic_string& str); // constexpr since C++20
140 template <class T>
141 basic_string& operator=(const T& t); // C++17, constexpr since C++20
142 basic_string& operator=(basic_string&& str)
143 noexcept(
144 allocator_type::propagate_on_container_move_assignment::value ||
145 allocator_type::is_always_equal::value ); // C++17, constexpr since C++20
146 basic_string& operator=(const value_type* s); // constexpr since C++20
147 basic_string& operator=(nullptr_t) = delete; // C++23
148 basic_string& operator=(value_type c); // constexpr since C++20
149 basic_string& operator=(initializer_list<value_type>); // constexpr since C++20
150
151 iterator begin() noexcept; // constexpr since C++20
152 const_iterator begin() const noexcept; // constexpr since C++20
153 iterator end() noexcept; // constexpr since C++20
154 const_iterator end() const noexcept; // constexpr since C++20
155
156 reverse_iterator rbegin() noexcept; // constexpr since C++20
157 const_reverse_iterator rbegin() const noexcept; // constexpr since C++20
158 reverse_iterator rend() noexcept; // constexpr since C++20
159 const_reverse_iterator rend() const noexcept; // constexpr since C++20
160
161 const_iterator cbegin() const noexcept; // constexpr since C++20
162 const_iterator cend() const noexcept; // constexpr since C++20
163 const_reverse_iterator crbegin() const noexcept; // constexpr since C++20
164 const_reverse_iterator crend() const noexcept; // constexpr since C++20
165
166 size_type size() const noexcept; // constexpr since C++20
167 size_type length() const noexcept; // constexpr since C++20
168 size_type max_size() const noexcept; // constexpr since C++20
169 size_type capacity() const noexcept; // constexpr since C++20
170
171 void resize(size_type n, value_type c); // constexpr since C++20
172 void resize(size_type n); // constexpr since C++20
173
174 template<class Operation>
175 constexpr void resize_and_overwrite(size_type n, Operation op); // since C++23
176
177 void reserve(size_type res_arg); // constexpr since C++20
178 void reserve(); // deprecated in C++20, removed in C++26
179 void shrink_to_fit(); // constexpr since C++20
180 void clear() noexcept; // constexpr since C++20
181 bool empty() const noexcept; // constexpr since C++20
182
183 const_reference operator[](size_type pos) const; // constexpr since C++20
184 reference operator[](size_type pos); // constexpr since C++20
185
186 const_reference at(size_type n) const; // constexpr since C++20
187 reference at(size_type n); // constexpr since C++20
188
189 basic_string& operator+=(const basic_string& str); // constexpr since C++20
190 template <class T>
191 basic_string& operator+=(const T& t); // C++17, constexpr since C++20
192 basic_string& operator+=(const value_type* s); // constexpr since C++20
193 basic_string& operator+=(value_type c); // constexpr since C++20
194 basic_string& operator+=(initializer_list<value_type>); // constexpr since C++20
195
196 basic_string& append(const basic_string& str); // constexpr since C++20
197 template <class T>
198 basic_string& append(const T& t); // C++17, constexpr since C++20
199 basic_string& append(const basic_string& str, size_type pos, size_type n=npos); // C++14, constexpr since C++20
200 template <class T>
201 basic_string& append(const T& t, size_type pos, size_type n=npos); // C++17, constexpr since C++20
202 basic_string& append(const value_type* s, size_type n); // constexpr since C++20
203 basic_string& append(const value_type* s); // constexpr since C++20
204 basic_string& append(size_type n, value_type c); // constexpr since C++20
205 template<class InputIterator>
206 basic_string& append(InputIterator first, InputIterator last); // constexpr since C++20
207 template<container-compatible-range<charT> R>
208 constexpr basic_string& append_range(R&& rg); // C++23
209 basic_string& append(initializer_list<value_type>); // constexpr since C++20
210
211 void push_back(value_type c); // constexpr since C++20
212 void pop_back(); // constexpr since C++20
213 reference front(); // constexpr since C++20
214 const_reference front() const; // constexpr since C++20
215 reference back(); // constexpr since C++20
216 const_reference back() const; // constexpr since C++20
217
218 basic_string& assign(const basic_string& str); // constexpr since C++20
219 template <class T>
220 basic_string& assign(const T& t); // C++17, constexpr since C++20
221 basic_string& assign(basic_string&& str); // constexpr since C++20
222 basic_string& assign(const basic_string& str, size_type pos, size_type n=npos); // C++14, constexpr since C++20
223 template <class T>
224 basic_string& assign(const T& t, size_type pos, size_type n=npos); // C++17, constexpr since C++20
225 basic_string& assign(const value_type* s, size_type n); // constexpr since C++20
226 basic_string& assign(const value_type* s); // constexpr since C++20
227 basic_string& assign(size_type n, value_type c); // constexpr since C++20
228 template<class InputIterator>
229 basic_string& assign(InputIterator first, InputIterator last); // constexpr since C++20
230 template<container-compatible-range<charT> R>
231 constexpr basic_string& assign_range(R&& rg); // C++23
232 basic_string& assign(initializer_list<value_type>); // constexpr since C++20
233
234 basic_string& insert(size_type pos1, const basic_string& str); // constexpr since C++20
235 template <class T>
236 basic_string& insert(size_type pos1, const T& t); // constexpr since C++20
237 basic_string& insert(size_type pos1, const basic_string& str,
238 size_type pos2, size_type n2=npos); // constexpr since C++20
239 template <class T>
240 basic_string& insert(size_type pos1, const T& t, size_type pos2, size_type n=npos); // C++17, constexpr since C++20
241 basic_string& insert(size_type pos, const value_type* s, size_type n=npos); // C++14, constexpr since C++20
242 basic_string& insert(size_type pos, const value_type* s); // constexpr since C++20
243 basic_string& insert(size_type pos, size_type n, value_type c); // constexpr since C++20
244 iterator insert(const_iterator p, value_type c); // constexpr since C++20
245 iterator insert(const_iterator p, size_type n, value_type c); // constexpr since C++20
246 template<class InputIterator>
247 iterator insert(const_iterator p, InputIterator first, InputIterator last); // constexpr since C++20
248 template<container-compatible-range<charT> R>
249 constexpr iterator insert_range(const_iterator p, R&& rg); // C++23
250 iterator insert(const_iterator p, initializer_list<value_type>); // constexpr since C++20
251
252 basic_string& erase(size_type pos = 0, size_type n = npos); // constexpr since C++20
253 iterator erase(const_iterator position); // constexpr since C++20
254 iterator erase(const_iterator first, const_iterator last); // constexpr since C++20
255
256 basic_string& replace(size_type pos1, size_type n1, const basic_string& str); // constexpr since C++20
257 template <class T>
258 basic_string& replace(size_type pos1, size_type n1, const T& t); // C++17, constexpr since C++20
259 basic_string& replace(size_type pos1, size_type n1, const basic_string& str,
260 size_type pos2, size_type n2=npos); // C++14, constexpr since C++20
261 template <class T>
262 basic_string& replace(size_type pos1, size_type n1, const T& t,
263 size_type pos2, size_type n2=npos); // C++17, constexpr since C++20
264 basic_string& replace(size_type pos, size_type n1, const value_type* s, size_type n2); // constexpr since C++20
265 basic_string& replace(size_type pos, size_type n1, const value_type* s); // constexpr since C++20
266 basic_string& replace(size_type pos, size_type n1, size_type n2, value_type c); // constexpr since C++20
267 basic_string& replace(const_iterator i1, const_iterator i2, const basic_string& str); // constexpr since C++20
268 template <class T>
269 basic_string& replace(const_iterator i1, const_iterator i2, const T& t); // C++17, constexpr since C++20
270 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s, size_type n); // constexpr since C++20
271 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s); // constexpr since C++20
272 basic_string& replace(const_iterator i1, const_iterator i2, size_type n, value_type c); // constexpr since C++20
273 template<class InputIterator>
274 basic_string& replace(const_iterator i1, const_iterator i2, InputIterator j1, InputIterator j2); // constexpr since C++20
275 template<container-compatible-range<charT> R>
276 constexpr basic_string& replace_with_range(const_iterator i1, const_iterator i2, R&& rg); // C++23
277 basic_string& replace(const_iterator i1, const_iterator i2, initializer_list<value_type>); // constexpr since C++20
278
279 size_type copy(value_type* s, size_type n, size_type pos = 0) const; // constexpr since C++20
280 basic_string substr(size_type pos = 0, size_type n = npos) const; // constexpr in C++20, removed in C++23
281 basic_string substr(size_type pos = 0, size_type n = npos) const&; // since C++23
282 constexpr basic_string substr(size_type pos = 0, size_type n = npos) &&; // since C++23
283 constexpr basic_string_view<charT, traits> subview(size_type pos = 0,
284 size_type n = npos) const; // since C++26
285 void swap(basic_string& str)
286 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
287 allocator_traits<allocator_type>::is_always_equal::value); // C++17, constexpr since C++20
288
289 const value_type* c_str() const noexcept; // constexpr since C++20
290 const value_type* data() const noexcept; // constexpr since C++20
291 value_type* data() noexcept; // C++17, constexpr since C++20
292
293 allocator_type get_allocator() const noexcept; // constexpr since C++20
294
295 size_type find(const basic_string& str, size_type pos = 0) const noexcept; // constexpr since C++20
296 template <class T>
297 size_type find(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension, constexpr since C++20
298 size_type find(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
299 size_type find(const value_type* s, size_type pos = 0) const noexcept; // constexpr since C++20
300 size_type find(value_type c, size_type pos = 0) const noexcept; // constexpr since C++20
301
302 size_type rfind(const basic_string& str, size_type pos = npos) const noexcept; // constexpr since C++20
303 template <class T>
304 size_type rfind(const T& t, size_type pos = npos) const noexcept; // C++17, noexcept as an extension, constexpr since C++20
305 size_type rfind(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
306 size_type rfind(const value_type* s, size_type pos = npos) const noexcept; // constexpr since C++20
307 size_type rfind(value_type c, size_type pos = npos) const noexcept; // constexpr since C++20
308
309 size_type find_first_of(const basic_string& str, size_type pos = 0) const noexcept; // constexpr since C++20
310 template <class T>
311 size_type find_first_of(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension, constexpr since C++20
312 size_type find_first_of(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
313 size_type find_first_of(const value_type* s, size_type pos = 0) const noexcept; // constexpr since C++20
314 size_type find_first_of(value_type c, size_type pos = 0) const noexcept; // constexpr since C++20
315
316 size_type find_last_of(const basic_string& str, size_type pos = npos) const noexcept; // constexpr since C++20
317 template <class T>
318 size_type find_last_of(const T& t, size_type pos = npos) const noexcept noexcept; // C++17, noexcept as an extension, constexpr since C++20
319 size_type find_last_of(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
320 size_type find_last_of(const value_type* s, size_type pos = npos) const noexcept; // constexpr since C++20
321 size_type find_last_of(value_type c, size_type pos = npos) const noexcept; // constexpr since C++20
322
323 size_type find_first_not_of(const basic_string& str, size_type pos = 0) const noexcept; // constexpr since C++20
324 template <class T>
325 size_type find_first_not_of(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension, constexpr since C++20
326 size_type find_first_not_of(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
327 size_type find_first_not_of(const value_type* s, size_type pos = 0) const noexcept; // constexpr since C++20
328 size_type find_first_not_of(value_type c, size_type pos = 0) const noexcept; // constexpr since C++20
329
330 size_type find_last_not_of(const basic_string& str, size_type pos = npos) const noexcept; // constexpr since C++20
331 template <class T>
332 size_type find_last_not_of(const T& t, size_type pos = npos) const noexcept; // C++17, noexcept as an extension, constexpr since C++20
333 size_type find_last_not_of(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
334 size_type find_last_not_of(const value_type* s, size_type pos = npos) const noexcept; // constexpr since C++20
335 size_type find_last_not_of(value_type c, size_type pos = npos) const noexcept; // constexpr since C++20
336
337 int compare(const basic_string& str) const noexcept; // constexpr since C++20
338 template <class T>
339 int compare(const T& t) const noexcept; // C++17, noexcept as an extension, constexpr since C++20
340 int compare(size_type pos1, size_type n1, const basic_string& str) const; // constexpr since C++20
341 template <class T>
342 int compare(size_type pos1, size_type n1, const T& t) const; // C++17, constexpr since C++20
343 int compare(size_type pos1, size_type n1, const basic_string& str,
344 size_type pos2, size_type n2=npos) const; // C++14, constexpr since C++20
345 template <class T>
346 int compare(size_type pos1, size_type n1, const T& t,
347 size_type pos2, size_type n2=npos) const; // C++17, constexpr since C++20
348 int compare(const value_type* s) const noexcept; // constexpr since C++20
349 int compare(size_type pos1, size_type n1, const value_type* s) const; // constexpr since C++20
350 int compare(size_type pos1, size_type n1, const value_type* s, size_type n2) const; // constexpr since C++20
351
352 constexpr bool starts_with(basic_string_view<charT, traits> sv) const noexcept; // C++20
353 constexpr bool starts_with(charT c) const noexcept; // C++20
354 constexpr bool starts_with(const charT* s) const; // C++20
355 constexpr bool ends_with(basic_string_view<charT, traits> sv) const noexcept; // C++20
356 constexpr bool ends_with(charT c) const noexcept; // C++20
357 constexpr bool ends_with(const charT* s) const; // C++20
358
359 constexpr bool contains(basic_string_view<charT, traits> sv) const noexcept; // C++23
360 constexpr bool contains(charT c) const noexcept; // C++23
361 constexpr bool contains(const charT* s) const; // C++23
362};
363
364template<class InputIterator,
365 class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
366basic_string(InputIterator, InputIterator, Allocator = Allocator())
367 -> basic_string<typename iterator_traits<InputIterator>::value_type,
368 char_traits<typename iterator_traits<InputIterator>::value_type>,
369 Allocator>; // C++17
370
371template<ranges::input_range R,
372 class Allocator = allocator<ranges::range_value_t<R>>>
373 basic_string(from_range_t, R&&, Allocator = Allocator())
374 -> basic_string<ranges::range_value_t<R>, char_traits<ranges::range_value_t<R>>,
375 Allocator>; // C++23
376
377template<class charT,
378 class traits,
379 class Allocator = allocator<charT>>
380 explicit basic_string(basic_string_view<charT, traits>, const Allocator& = Allocator())
381 -> basic_string<charT, traits, Allocator>; // C++17
382
383template<class charT,
384 class traits,
385 class Allocator = allocator<charT>>
386 basic_string(basic_string_view<charT, traits>,
387 typename see below::size_type, typename see below::size_type,
388 const Allocator& = Allocator())
389 -> basic_string<charT, traits, Allocator>; // C++17
390
391template<class charT, class traits, class Allocator>
392basic_string<charT, traits, Allocator>
393operator+(const basic_string<charT, traits, Allocator>& lhs,
394 const basic_string<charT, traits, Allocator>& rhs); // constexpr since C++20
395
396template<class charT, class traits, class Allocator>
397basic_string<charT, traits, Allocator>
398operator+(const charT* lhs , const basic_string<charT,traits,Allocator>&rhs); // constexpr since C++20
399
400template<class charT, class traits, class Allocator>
401basic_string<charT, traits, Allocator>
402operator+(charT lhs, const basic_string<charT,traits,Allocator>& rhs); // constexpr since C++20
403
404template<class charT, class traits, class Allocator>
405basic_string<charT, traits, Allocator>
406operator+(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs); // constexpr since C++20
407
408template<class charT, class traits, class Allocator>
409basic_string<charT, traits, Allocator>
410operator+(const basic_string<charT, traits, Allocator>& lhs, charT rhs); // constexpr since C++20
411
412template<class charT, class traits, class Allocator>
413 constexpr basic_string<charT, traits, Allocator>
414 operator+(const basic_string<charT, traits, Allocator>& lhs,
415 type_identity_t<basic_string_view<charT, traits>> rhs); // Since C++26
416template<class charT, class traits, class Allocator>
417 constexpr basic_string<charT, traits, Allocator>
418 operator+(basic_string<charT, traits, Allocator>&& lhs,
419 type_identity_t<basic_string_view<charT, traits>> rhs); // Since C++26
420template<class charT, class traits, class Allocator>
421 constexpr basic_string<charT, traits, Allocator>
422 operator+(type_identity_t<basic_string_view<charT, traits>> lhs,
423 const basic_string<charT, traits, Allocator>& rhs); // Since C++26
424template<class charT, class traits, class Allocator>
425 constexpr basic_string<charT, traits, Allocator>
426 operator+(type_identity_t<basic_string_view<charT, traits>> lhs,
427 basic_string<charT, traits, Allocator>&& rhs); // Since C++26
428
429
430template<class charT, class traits, class Allocator>
431bool operator==(const basic_string<charT, traits, Allocator>& lhs,
432 const basic_string<charT, traits, Allocator>& rhs) noexcept; // constexpr since C++20
433
434template<class charT, class traits, class Allocator>
435bool operator==(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
436
437template<class charT, class traits, class Allocator>
438bool operator==(const basic_string<charT,traits,Allocator>& lhs, const charT* rhs) noexcept; // constexpr since C++20
439
440template<class charT, class traits, class Allocator>
441bool operator!=(const basic_string<charT,traits,Allocator>& lhs,
442 const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
443
444template<class charT, class traits, class Allocator>
445bool operator!=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
446
447template<class charT, class traits, class Allocator>
448bool operator!=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // removed in C++20
449
450template<class charT, class traits, class Allocator>
451bool operator< (const basic_string<charT, traits, Allocator>& lhs,
452 const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
453
454template<class charT, class traits, class Allocator>
455bool operator< (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // removed in C++20
456
457template<class charT, class traits, class Allocator>
458bool operator< (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
459
460template<class charT, class traits, class Allocator>
461bool operator> (const basic_string<charT, traits, Allocator>& lhs,
462 const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
463
464template<class charT, class traits, class Allocator>
465bool operator> (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // removed in C++20
466
467template<class charT, class traits, class Allocator>
468bool operator> (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
469
470template<class charT, class traits, class Allocator>
471bool operator<=(const basic_string<charT, traits, Allocator>& lhs,
472 const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
473
474template<class charT, class traits, class Allocator>
475bool operator<=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // removed in C++20
476
477template<class charT, class traits, class Allocator>
478bool operator<=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
479
480template<class charT, class traits, class Allocator>
481bool operator>=(const basic_string<charT, traits, Allocator>& lhs,
482 const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
483
484template<class charT, class traits, class Allocator>
485bool operator>=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept; // removed in C++20
486
487template<class charT, class traits, class Allocator>
488bool operator>=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept; // removed in C++20
489
490template<class charT, class traits, class Allocator> // since C++20
491constexpr see below operator<=>(const basic_string<charT, traits, Allocator>& lhs,
492 const basic_string<charT, traits, Allocator>& rhs) noexcept;
493
494template<class charT, class traits, class Allocator> // since C++20
495constexpr see below operator<=>(const basic_string<charT, traits, Allocator>& lhs,
496 const charT* rhs) noexcept;
497
498template<class charT, class traits, class Allocator>
499void swap(basic_string<charT, traits, Allocator>& lhs,
500 basic_string<charT, traits, Allocator>& rhs)
501 noexcept(noexcept(lhs.swap(rhs))); // constexpr since C++20
502
503template<class charT, class traits, class Allocator>
504basic_istream<charT, traits>&
505operator>>(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
506
507template<class charT, class traits, class Allocator>
508basic_ostream<charT, traits>&
509operator<<(basic_ostream<charT, traits>& os, const basic_string<charT, traits, Allocator>& str);
510
511template<class charT, class traits, class Allocator>
512basic_istream<charT, traits>&
513getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str,
514 charT delim);
515
516template<class charT, class traits, class Allocator>
517basic_istream<charT, traits>&
518getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
519
520template<class charT, class traits, class Allocator, class U>
521constexpr typename basic_string<charT, traits, Allocator>::size_type
522erase(basic_string<charT, traits, Allocator>& c, const U& value); // C++20
523template<class charT, class traits, class Allocator, class Predicate>
524constexpr typename basic_string<charT, traits, Allocator>::size_type
525erase_if(basic_string<charT, traits, Allocator>& c, Predicate pred); // C++20
526
527typedef basic_string<char> string;
528typedef basic_string<wchar_t> wstring;
529typedef basic_string<char8_t> u8string; // C++20
530typedef basic_string<char16_t> u16string;
531typedef basic_string<char32_t> u32string;
532
533int stoi (const string& str, size_t* idx = nullptr, int base = 10);
534long stol (const string& str, size_t* idx = nullptr, int base = 10);
535unsigned long stoul (const string& str, size_t* idx = nullptr, int base = 10);
536long long stoll (const string& str, size_t* idx = nullptr, int base = 10);
537unsigned long long stoull(const string& str, size_t* idx = nullptr, int base = 10);
538
539float stof (const string& str, size_t* idx = nullptr);
540double stod (const string& str, size_t* idx = nullptr);
541long double stold(const string& str, size_t* idx = nullptr);
542
543string to_string(int val);
544string to_string(unsigned val);
545string to_string(long val);
546string to_string(unsigned long val);
547string to_string(long long val);
548string to_string(unsigned long long val);
549string to_string(float val);
550string to_string(double val);
551string to_string(long double val);
552
553int stoi (const wstring& str, size_t* idx = nullptr, int base = 10);
554long stol (const wstring& str, size_t* idx = nullptr, int base = 10);
555unsigned long stoul (const wstring& str, size_t* idx = nullptr, int base = 10);
556long long stoll (const wstring& str, size_t* idx = nullptr, int base = 10);
557unsigned long long stoull(const wstring& str, size_t* idx = nullptr, int base = 10);
558
559float stof (const wstring& str, size_t* idx = nullptr);
560double stod (const wstring& str, size_t* idx = nullptr);
561long double stold(const wstring& str, size_t* idx = nullptr);
562
563wstring to_wstring(int val);
564wstring to_wstring(unsigned val);
565wstring to_wstring(long val);
566wstring to_wstring(unsigned long val);
567wstring to_wstring(long long val);
568wstring to_wstring(unsigned long long val);
569wstring to_wstring(float val);
570wstring to_wstring(double val);
571wstring to_wstring(long double val);
572
573template <> struct hash<string>;
574template <> struct hash<u8string>; // C++20
575template <> struct hash<u16string>;
576template <> struct hash<u32string>;
577template <> struct hash<wstring>;
578
579basic_string<char> operator""s( const char *str, size_t len ); // C++14, constexpr since C++20
580basic_string<wchar_t> operator""s( const wchar_t *str, size_t len ); // C++14, constexpr since C++20
581constexpr basic_string<char8_t> operator""s( const char8_t *str, size_t len ); // C++20
582basic_string<char16_t> operator""s( const char16_t *str, size_t len ); // C++14, constexpr since C++20
583basic_string<char32_t> operator""s( const char32_t *str, size_t len ); // C++14, constexpr since C++20
584
585} // std
586
587*/
588
589// clang-format on
590
591#if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
592# include <__cxx03/string>
593#else
594# include <__algorithm/max.h>
595# include <__algorithm/min.h>
596# include <__algorithm/remove.h>
597# include <__algorithm/remove_if.h>
598# include <__assert>
599# include <__config>
600# include <__debug_utils/sanitizers.h>
601# include <__format/enable_insertable.h>
602# include <__functional/hash.h>
603# include <__functional/is_transparent.h>
604# include <__functional/unary_function.h>
605# include <__fwd/string.h>
606# include <__iterator/bounded_iter.h>
607# include <__iterator/distance.h>
608# include <__iterator/iterator_traits.h>
609# include <__iterator/reverse_iterator.h>
610# include <__iterator/wrap_iter.h>
611# include <__memory/addressof.h>
612# include <__memory/allocate_at_least.h>
613# include <__memory/allocator.h>
614# include <__memory/allocator_traits.h>
615# include <__memory/compressed_pair.h>
616# include <__memory/construct_at.h>
617# include <__memory/noexcept_move_assign_container.h>
618# include <__memory/pointer_traits.h>
619# include <__memory/swap_allocator.h>
620# include <__memory_resource/polymorphic_allocator.h>
621# include <__ranges/access.h>
622# include <__ranges/concepts.h>
623# include <__ranges/container_compatible_range.h>
624# include <__ranges/from_range.h>
625# include <__string/char_traits.h>
626# include <__string/extern_template_lists.h>
627# include <__type_traits/conditional.h>
628# include <__type_traits/enable_if.h>
629# include <__type_traits/is_allocator.h>
630# include <__type_traits/is_array.h>
631# include <__type_traits/is_convertible.h>
632# include <__type_traits/is_generic_transparent_comparator.h>
633# include <__type_traits/is_nothrow_assignable.h>
634# include <__type_traits/is_nothrow_constructible.h>
635# include <__type_traits/is_same.h>
636# include <__type_traits/is_standard_layout.h>
637# include <__type_traits/is_trivially_constructible.h>
638# include <__type_traits/is_trivially_copyable.h>
639# include <__type_traits/is_trivially_relocatable.h>
640# include <__type_traits/remove_cvref.h>
641# include <__utility/default_three_way_comparator.h>
642# include <__utility/exception_guard.h>
643# include <__utility/forward.h>
644# include <__utility/is_pointer_in_range.h>
645# include <__utility/move.h>
646# include <__utility/no_destroy.h>
647# include <__utility/scope_guard.h>
648# include <__utility/swap.h>
649# include <climits>
650# include <cstdio> // EOF
651# include <cstring>
652# include <limits>
653# include <stdexcept>
654# include <string_view>
655# include <version>
656
657# if _LIBCPP_HAS_WIDE_CHARACTERS
658# include <cwchar>
659# endif
660
661// standard-mandated includes
662
663// [iterator.range]
664# include <__iterator/access.h>
665# include <__iterator/data.h>
666# include <__iterator/empty.h>
667# include <__iterator/reverse_access.h>
668# include <__iterator/size.h>
669
670// [string.syn]
671# include <compare>
672# include <initializer_list>
673
674# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
675# pragma GCC system_header
676# endif
677
678_LIBCPP_PUSH_MACROS
679# include <__undef_macros>
680
681// Since std::string is partially instantiated in the built library, we require that the library
682// has been built with ASAN support in order to enable the container checks in std::string.
683// Within the <string> implementation, use `_LIBCPP_ENABLE_ASAN_CONTAINER_CHECKS_FOR_STRING`
684// to determine whether ASAN container checks should be provided.
685//
686// The _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS macro disables ASAN instrumentation for a specific
687// function, allowing memory accesses that would normally trigger ASan errors to proceed without
688// crashing. This is useful for accessing parts of objects memory, which should not be accessed,
689// such as unused bytes in short strings, that should never be accessed by other parts of the program.
690# if _LIBCPP_ENABLE_ASAN_CONTAINER_CHECKS && _LIBCPP_INSTRUMENTED_WITH_ASAN
691# define _LIBCPP_ENABLE_ASAN_CONTAINER_CHECKS_FOR_STRING 1
692# define _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS __attribute__((__no_sanitize__("address")))
693# else
694# define _LIBCPP_ENABLE_ASAN_CONTAINER_CHECKS_FOR_STRING 0
695# define _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS
696# endif
697
698_LIBCPP_BEGIN_NAMESPACE_STD
699
700// basic_string
701
702template <class _CharT, class _Traits, class _Allocator>
703_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
704__concatenate_strings(const _Allocator& __alloc,
705 __type_identity_t<basic_string_view<_CharT, _Traits> > __str1,
706 __type_identity_t<basic_string_view<_CharT, _Traits> > __str2);
707
708template <class _Iter>
709inline const bool __string_is_trivial_iterator_v = false;
710
711template <class _Tp>
712inline const bool __string_is_trivial_iterator_v<_Tp*> = is_arithmetic<_Tp>::value;
713
714template <class _Iter>
715inline const bool __string_is_trivial_iterator_v<__wrap_iter<_Iter> > = __string_is_trivial_iterator_v<_Iter>;
716
717template <class _CharT, class _Traits, class _Tp>
718inline const bool __can_be_converted_to_string_view_v =
719 is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
720 !is_convertible<const _Tp&, const _CharT*>::value;
721
722struct __uninitialized_size_tag {};
723struct __init_with_sentinel_tag {};
724
725template <size_t _PaddingSize>
726struct __padding {
727 char __padding_[_PaddingSize];
728};
729
730template <>
731struct __padding<0> {};
732
733template <class _CharT, class _Traits, class _Allocator>
734class basic_string {
735public:
736 using __self _LIBCPP_NODEBUG = basic_string;
737 using __self_view _LIBCPP_NODEBUG = basic_string_view<_CharT, _Traits>;
738 using traits_type = _Traits;
739 using value_type = _CharT;
740 using allocator_type = _Allocator;
741 using __alloc_traits _LIBCPP_NODEBUG = allocator_traits<allocator_type>;
742 using size_type = typename __alloc_traits::size_type;
743 using difference_type = typename __alloc_traits::difference_type;
744 using reference = value_type&;
745 using const_reference = const value_type&;
746 using pointer = typename __alloc_traits::pointer;
747 using const_pointer = typename __alloc_traits::const_pointer;
748
749 // A basic_string contains the following members which may be trivially relocatable:
750 // - pointer: is currently assumed to be trivially relocatable, but is still checked in case that changes
751 // - size_type: is always trivially relocatable, since it has to be an integral type
752 // - value_type: is always trivially relocatable, since it has to be trivial
753 // - unsigned char: is a fundamental type, so it's trivially relocatable
754 // - allocator_type: may or may not be trivially relocatable, so it's checked
755 //
756 // This string implementation doesn't contain any references into itself. It only contains a bit that says whether
757 // it is in small or large string mode, so the entire structure is trivially relocatable if its members are.
758# if _LIBCPP_ENABLE_ASAN_CONTAINER_CHECKS_FOR_STRING
759 // When compiling with AddressSanitizer (ASan), basic_string cannot be trivially
760 // relocatable. Because the object's memory might be poisoned when its content
761 // is kept inside objects memory (short string optimization), instead of in allocated
762 // external memory. In such cases, the destructor is responsible for unpoisoning
763 // the memory to avoid triggering false positives.
764 // Therefore it's crucial to ensure the destructor is called.
765 using __trivially_relocatable _LIBCPP_NODEBUG = void;
766# else
767 using __trivially_relocatable _LIBCPP_NODEBUG = __conditional_t<
768 __libcpp_is_trivially_relocatable<allocator_type>::value && __libcpp_is_trivially_relocatable<pointer>::value,
769 basic_string,
770 void>;
771# endif
772
773 static_assert(!is_array<value_type>::value, "Character type of basic_string must not be an array");
774 static_assert(is_standard_layout<value_type>::value, "Character type of basic_string must be standard-layout");
775 static_assert(is_trivially_default_constructible<value_type>::value,
776 "Character type of basic_string must be trivially default constructible");
777 static_assert(is_trivially_copyable<value_type>::value, "Character type of basic_string must be trivially copyable");
778 static_assert(is_same<_CharT, typename traits_type::char_type>::value,
779 "traits_type::char_type must be the same type as CharT");
780 static_assert(is_same<typename allocator_type::value_type, value_type>::value,
781 "Allocator::value_type must be same type as value_type");
782 static_assert(__check_valid_allocator<allocator_type>::value, "");
783
784# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_STRING
785 // Users might provide custom allocators, and prior to C++20 we have no existing way to detect whether the allocator's
786 // pointer type is contiguous (though it has to be by the Standard). Using the wrapper type ensures the iterator is
787 // considered contiguous.
788 using iterator = __bounded_iter<pointer>;
789 using const_iterator = __bounded_iter<const_pointer>;
790# else
791 using iterator = __wrap_iter<pointer>;
792 using const_iterator = __wrap_iter<const_pointer>;
793# endif
794 using reverse_iterator = std::reverse_iterator<iterator>;
795 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
796
797 using __alloc_result _LIBCPP_NODEBUG = __allocation_result<pointer, size_type>;
798
799private:
800 static_assert(CHAR_BIT == 8, "This implementation assumes that one byte contains 8 bits");
801
802# ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
803
804 struct __long {
805 __long() = default;
806
807 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __long(__alloc_result __alloc, size_type __size)
808 : __data_(__alloc.ptr), __size_(__size), __cap_(__alloc.count / __endian_factor), __is_long_(true) {
809 _LIBCPP_ASSERT_INTERNAL(!__fits_in_sso(__alloc.count), "Long capacity should always be larger than the SSO");
810 }
811
812 pointer __data_;
813 size_type __size_;
814 size_type __cap_ : sizeof(size_type) * CHAR_BIT - 1;
815 size_type __is_long_ : 1;
816 };
817
818 enum { __min_cap = (sizeof(__long) - 1) / sizeof(value_type) > 2 ? (sizeof(__long) - 1) / sizeof(value_type) : 2 };
819
820 struct __short {
821 value_type __data_[__min_cap];
822 _LIBCPP_NO_UNIQUE_ADDRESS __padding<sizeof(value_type) - 1> __padding_;
823 unsigned char __size_ : 7;
824 unsigned char __is_long_ : 1;
825 };
826
827 // The __endian_factor is required because the field we use to store the size
828 // has one fewer bit than it would if it were not a bitfield.
829 //
830 // If the LSB is used to store the short-flag in the short string representation,
831 // we have to multiply the size by two when it is stored and divide it by two when
832 // it is loaded to make sure that we always store an even number. In the long string
833 // representation, we can ignore this because we can assume that we always allocate
834 // an even amount of value_types.
835 //
836 // If the MSB is used for the short-flag, the max_size() is numeric_limits<size_type>::max() / 2.
837 // This does not impact the short string representation, since we never need the MSB
838 // for representing the size of a short string anyway.
839
840# ifdef _LIBCPP_BIG_ENDIAN
841 static const size_type __endian_factor = 2;
842# else
843 static const size_type __endian_factor = 1;
844# endif
845
846# else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
847
848# ifdef _LIBCPP_BIG_ENDIAN
849 static const size_type __endian_factor = 1;
850# else
851 static const size_type __endian_factor = 2;
852# endif
853
854 // Attribute 'packed' is used to keep the layout compatible with the
855 // previous definition that did not use bit fields. This is because on
856 // some platforms bit fields have a default size rather than the actual
857 // size used, e.g., it is 4 bytes on AIX. See D128285 for details.
858 struct __long {
859 __long() = default;
860
861 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __long(__alloc_result __alloc, size_type __size)
862 : __is_long_(true), __cap_(__alloc.count / __endian_factor), __size_(__size), __data_(__alloc.ptr) {
863 _LIBCPP_ASSERT_INTERNAL(!__fits_in_sso(__alloc.count), "Long capacity should always be larger than the SSO");
864 }
865
866 struct _LIBCPP_PACKED {
867 size_type __is_long_ : 1;
868 size_type __cap_ : sizeof(size_type) * CHAR_BIT - 1;
869 };
870 size_type __size_;
871 pointer __data_;
872 };
873
874 enum { __min_cap = (sizeof(__long) - 1) / sizeof(value_type) > 2 ? (sizeof(__long) - 1) / sizeof(value_type) : 2 };
875
876 struct __short {
877 struct _LIBCPP_PACKED {
878 unsigned char __is_long_ : 1;
879 unsigned char __size_ : 7;
880 };
881 _LIBCPP_NO_UNIQUE_ADDRESS __padding<sizeof(value_type) - 1> __padding_;
882 value_type __data_[__min_cap];
883 };
884
885# endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
886
887 static_assert(sizeof(__short) == (sizeof(value_type) * (__min_cap + 1)), "__short has an unexpected size.");
888
889 union __rep {
890 __short __s;
891 __long __l;
892
893 __rep() = default;
894 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __rep(__short __r) : __s(__r) {}
895 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __rep(__long __r) : __l(__r) {}
896 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __rep(__uninitialized_tag) {}
897 };
898
899 _LIBCPP_COMPRESSED_PAIR(__rep, __rep_, allocator_type, __alloc_);
900
901 // annotate the string with its size() at scope exit. The string has to be in a valid state at that point.
902 struct __annotate_new_size {
903 basic_string& __str_;
904
905 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __annotate_new_size(basic_string& __str) : __str_(__str) {}
906
907 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void operator()() { __str_.__annotate_new(__str_.size()); }
908 };
909
910 // Construct a string with the given allocator and enough storage to hold `__size` characters, but
911 // don't initialize the characters. The contents of the string, including the null terminator, must be
912 // initialized separately.
913 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit basic_string(
914 __uninitialized_size_tag, size_type __size, const allocator_type& __a)
915 : __alloc_(__a) {
916 __init_internal_buffer(__size);
917 }
918
919 template <class _Iter, class _Sent>
920 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
921 basic_string(__init_with_sentinel_tag, _Iter __first, _Sent __last, const allocator_type& __a)
922 : __alloc_(__a) {
923 __init_with_sentinel(std::move(__first), std::move(__last));
924 }
925
926 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator __make_iterator(pointer __p) {
927# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_STRING
928 // Bound the iterator according to the size (and not the capacity, unlike vector).
929 //
930 // By the Standard, string iterators are generally not guaranteed to stay valid when the container is modified,
931 // regardless of whether reallocation occurs. This allows us to check for out-of-bounds accesses using logical size,
932 // a stricter check, since correct code can never rely on being able to access newly-added elements via an existing
933 // iterator.
934 return std::__make_bounded_iter(__p, __get_pointer(), __get_pointer() + size());
935# else
936 return iterator(__p);
937# endif // _LIBCPP_ABI_BOUNDED_ITERATORS_IN_STRING
938 }
939
940 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_iterator __make_const_iterator(const_pointer __p) const {
941# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_STRING
942 // Bound the iterator according to the size (and not the capacity, unlike vector).
943 return std::__make_bounded_iter(__p, __get_pointer(), __get_pointer() + size());
944# else
945 return const_iterator(__p);
946# endif // _LIBCPP_ABI_BOUNDED_ITERATORS_IN_STRING
947 }
948
949public:
950 _LIBCPP_TEMPLATE_DATA_VIS static const size_type npos = -1;
951
952 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string()
953 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
954# if _LIBCPP_STD_VER >= 20 // TODO(LLVM 23): Remove this condition; this is a workaround for https://llvm.org/PR154567
955 : __rep_(__short())
956# else
957 : __rep_()
958# endif
959 {
960 __annotate_new(current_size: 0);
961 }
962
963 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit basic_string(const allocator_type& __a)
964# if _LIBCPP_STD_VER <= 14
965 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
966# else
967 _NOEXCEPT
968# endif
969# if _LIBCPP_STD_VER >= 20 // TODO(LLVM 23): Remove this condition; this is a workaround for https://llvm.org/PR154567
970 : __rep_(__short()),
971# else
972 : __rep_(),
973# endif
974 __alloc_(__a) {
975 __annotate_new(current_size: 0);
976 }
977
978 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS basic_string(const basic_string& __str)
979 : __alloc_(__alloc_traits::select_on_container_copy_construction(__str.__alloc_)) {
980 if (!__str.__is_long()) {
981 __rep_ = __str.__rep_;
982 __annotate_new(current_size: __get_short_size());
983 } else
984 __init_copy_ctor_external(s: std::__to_address(__str.__get_long_pointer()), sz: __str.__get_long_size());
985 }
986
987 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS
988 basic_string(const basic_string& __str, const allocator_type& __a)
989 : __alloc_(__a) {
990 if (!__str.__is_long()) {
991 __rep_ = __str.__rep_;
992 __annotate_new(current_size: __get_short_size());
993 } else
994 __init_copy_ctor_external(s: std::__to_address(__str.__get_long_pointer()), sz: __str.__get_long_size());
995 }
996
997# ifndef _LIBCPP_CXX03_LANG
998 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(basic_string&& __str)
999# if _LIBCPP_STD_VER <= 14
1000 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
1001# else
1002 _NOEXCEPT
1003# endif
1004 // Turning off ASan instrumentation for variable initialization with _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS
1005 // does not work consistently during initialization of __r_, so we instead unpoison __str's memory manually first.
1006 // __str's memory needs to be unpoisoned only in the case where it's a short string.
1007 : __rep_([](basic_string& __s) -> decltype(__s.__rep_)&& {
1008 if (!__s.__is_long())
1009 __s.__annotate_delete();
1010 return std::move(__s.__rep_);
1011 }(__str)),
1012 __alloc_(std::move(__str.__alloc_)) {
1013 __str.__rep_ = __rep();
1014 __str.__annotate_new(0);
1015 if (!__is_long())
1016 __annotate_new(current_size: size());
1017 }
1018
1019 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(basic_string&& __str, const allocator_type& __a)
1020 : __alloc_(__a) {
1021 if (__str.__is_long() && __a != __str.__alloc_) // copy, not move
1022 __init(std::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
1023 else {
1024 if (__libcpp_is_constant_evaluated())
1025 __rep_ = __rep();
1026 if (!__str.__is_long())
1027 __str.__annotate_delete();
1028 __rep_ = __str.__rep_;
1029 __str.__rep_ = __rep();
1030 __str.__annotate_new(0);
1031 if (!__is_long() && this != std::addressof(__str))
1032 __annotate_new(current_size: size());
1033 }
1034 }
1035# endif // _LIBCPP_CXX03_LANG
1036
1037 template <__enable_if_t<__is_allocator_v<_Allocator>, int> = 0>
1038 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const _CharT* _LIBCPP_DIAGNOSE_NULLPTR __s) {
1039 _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "basic_string(const char*) detected nullptr");
1040 __init(__s, traits_type::length(__s));
1041 }
1042
1043 template <__enable_if_t<__is_allocator_v<_Allocator>, int> = 0>
1044 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
1045 basic_string(const _CharT* _LIBCPP_DIAGNOSE_NULLPTR __s, const _Allocator& __a)
1046 : __alloc_(__a) {
1047 _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
1048 __init(__s, traits_type::length(__s));
1049 }
1050
1051# if _LIBCPP_STD_VER >= 23
1052 basic_string(nullptr_t) = delete;
1053# endif
1054
1055 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const _CharT* __s, size_type __n)
1056 _LIBCPP_DIAGNOSE_NULLPTR_IF(__n != 0 && __s == nullptr, " if n is not zero") {
1057 _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
1058 __init(__s, __n);
1059 }
1060
1061 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
1062 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
1063 _LIBCPP_DIAGNOSE_NULLPTR_IF(__n != 0 && __s == nullptr, " if n is not zero")
1064 : __alloc_(__a) {
1065 _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
1066 __init(__s, __n);
1067 }
1068
1069 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(size_type __n, _CharT __c) { __init(__n, __c); }
1070
1071# if _LIBCPP_STD_VER >= 23
1072 _LIBCPP_HIDE_FROM_ABI constexpr basic_string(
1073 basic_string&& __str, size_type __pos, const _Allocator& __alloc = _Allocator())
1074 : basic_string(std::move(__str), __pos, npos, __alloc) {}
1075
1076 _LIBCPP_HIDE_FROM_ABI constexpr basic_string(
1077 basic_string&& __str, size_type __pos, size_type __n, const _Allocator& __alloc = _Allocator())
1078 : __alloc_(__alloc) {
1079 if (__pos > __str.size())
1080 this->__throw_out_of_range();
1081
1082 auto __len = std::min<size_type>(__n, __str.size() - __pos);
1083 if (__alloc_traits::is_always_equal::value || __alloc == __str.__alloc_) {
1084 __move_assign(std::move(__str), __pos, __len);
1085 } else {
1086 // Perform a copy because the allocators are not compatible.
1087 __init(__str.data() + __pos, __len);
1088 }
1089 }
1090# endif
1091
1092 template <__enable_if_t<__is_allocator_v<_Allocator>, int> = 0>
1093 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(size_type __n, _CharT __c, const _Allocator& __a)
1094 : __alloc_(__a) {
1095 __init(__n, __c);
1096 }
1097
1098 _LIBCPP_CONSTEXPR_SINCE_CXX20
1099 basic_string(const basic_string& __str, size_type __pos, size_type __n, const _Allocator& __a = _Allocator())
1100 : __alloc_(__a) {
1101 size_type __str_sz = __str.size();
1102 if (__pos > __str_sz)
1103 this->__throw_out_of_range();
1104 __init(__str.data() + __pos, std::min(__n, __str_sz - __pos));
1105 }
1106
1107 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
1108 basic_string(const basic_string& __str, size_type __pos, const _Allocator& __a = _Allocator())
1109 : __alloc_(__a) {
1110 size_type __str_sz = __str.size();
1111 if (__pos > __str_sz)
1112 this->__throw_out_of_range();
1113 __init(__str.data() + __pos, __str_sz - __pos);
1114 }
1115
1116 template <class _Tp,
1117 __enable_if_t<__can_be_converted_to_string_view_v<_CharT, _Traits, _Tp> &&
1118 !is_same<__remove_cvref_t<_Tp>, basic_string>::value,
1119 int> = 0>
1120 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
1121 basic_string(const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a = allocator_type())
1122 : __alloc_(__a) {
1123 __self_view __sv0 = __t;
1124 __self_view __sv = __sv0.substr(__pos, __n);
1125 __init(__sv.data(), __sv.size());
1126 }
1127
1128 template <class _Tp,
1129 __enable_if_t<__can_be_converted_to_string_view_v<_CharT, _Traits, _Tp> &&
1130 !is_same<__remove_cvref_t<_Tp>, basic_string>::value,
1131 int> = 0>
1132 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit basic_string(const _Tp& __t) {
1133 __self_view __sv = __t;
1134 __init(__sv.data(), __sv.size());
1135 }
1136
1137 template <class _Tp,
1138 __enable_if_t<__can_be_converted_to_string_view_v<_CharT, _Traits, _Tp> &&
1139 !is_same<__remove_cvref_t<_Tp>, basic_string>::value,
1140 int> = 0>
1141 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit basic_string(const _Tp& __t, const allocator_type& __a)
1142 : __alloc_(__a) {
1143 __self_view __sv = __t;
1144 __init(__sv.data(), __sv.size());
1145 }
1146
1147 template <class _InputIterator, __enable_if_t<__has_input_iterator_category<_InputIterator>::value, int> = 0>
1148 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(_InputIterator __first, _InputIterator __last) {
1149 __init(__first, __last);
1150 }
1151
1152 template <class _InputIterator, __enable_if_t<__has_input_iterator_category<_InputIterator>::value, int> = 0>
1153 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
1154 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a)
1155 : __alloc_(__a) {
1156 __init(__first, __last);
1157 }
1158
1159# if _LIBCPP_STD_VER >= 23
1160 template <_ContainerCompatibleRange<_CharT> _Range>
1161 _LIBCPP_HIDE_FROM_ABI constexpr basic_string(
1162 from_range_t, _Range&& __range, const allocator_type& __a = allocator_type())
1163 : __alloc_(__a) {
1164 if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
1165 __init_with_size(ranges::begin(__range), ranges::end(__range), ranges::distance(__range));
1166 } else {
1167 __init_with_sentinel(ranges::begin(__range), ranges::end(__range));
1168 }
1169 }
1170# endif
1171
1172# ifndef _LIBCPP_CXX03_LANG
1173 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(initializer_list<_CharT> __il) {
1174 __init(__il.begin(), __il.end());
1175 }
1176
1177 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(initializer_list<_CharT> __il, const _Allocator& __a)
1178 : __alloc_(__a) {
1179 __init(__il.begin(), __il.end());
1180 }
1181# endif // _LIBCPP_CXX03_LANG
1182
1183 // TODO(boomanaiden154): Once we mark this in destructors as dead on return,
1184 // we can use a normal call to __reset_internal_buffer and remove the extra
1185 // __rep constructor.
1186 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 ~basic_string() { __reset_internal_buffer(new_rep: __rep(__uninitialized_tag())); }
1187
1188 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 operator __self_view() const _NOEXCEPT {
1189 return __self_view(typename __self_view::__assume_valid(), data(), size());
1190 }
1191
1192 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS basic_string&
1193 operator=(const basic_string& __str);
1194
1195 template <class _Tp,
1196 __enable_if_t<__can_be_converted_to_string_view_v<_CharT, _Traits, _Tp> &&
1197 !is_same<__remove_cvref_t<_Tp>, basic_string>::value,
1198 int> = 0>
1199 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator=(const _Tp& __t) {
1200 __self_view __sv = __t;
1201 return assign(__sv);
1202 }
1203
1204# ifndef _LIBCPP_CXX03_LANG
1205 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1206 operator=(basic_string&& __str) noexcept(__noexcept_move_assign_container<_Allocator, __alloc_traits>::value) {
1207 __move_assign(__str, integral_constant<bool, __alloc_traits::propagate_on_container_move_assignment::value>());
1208 return *this;
1209 }
1210
1211 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator=(initializer_list<value_type> __il) {
1212 return assign(__il.begin(), __il.size());
1213 }
1214# endif
1215 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1216 operator=(const value_type* _LIBCPP_DIAGNOSE_NULLPTR __s) {
1217 return assign(__s);
1218 }
1219# if _LIBCPP_STD_VER >= 23
1220 basic_string& operator=(nullptr_t) = delete;
1221# endif
1222 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator=(value_type __c);
1223
1224 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator begin() _NOEXCEPT {
1225 return __make_iterator(p: __get_pointer());
1226 }
1227 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_iterator begin() const _NOEXCEPT {
1228 return __make_const_iterator(p: __get_pointer());
1229 }
1230 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator end() _NOEXCEPT {
1231 return __make_iterator(p: __get_pointer() + size());
1232 }
1233 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_iterator end() const _NOEXCEPT {
1234 return __make_const_iterator(p: __get_pointer() + size());
1235 }
1236
1237 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reverse_iterator rbegin() _NOEXCEPT {
1238 return reverse_iterator(end());
1239 }
1240 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reverse_iterator
1241 rbegin() const _NOEXCEPT {
1242 return const_reverse_iterator(end());
1243 }
1244 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reverse_iterator rend() _NOEXCEPT {
1245 return reverse_iterator(begin());
1246 }
1247 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reverse_iterator rend() const _NOEXCEPT {
1248 return const_reverse_iterator(begin());
1249 }
1250
1251 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_iterator cbegin() const _NOEXCEPT {
1252 return begin();
1253 }
1254 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_iterator cend() const _NOEXCEPT {
1255 return end();
1256 }
1257 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reverse_iterator
1258 crbegin() const _NOEXCEPT {
1259 return rbegin();
1260 }
1261 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reverse_iterator crend() const _NOEXCEPT {
1262 return rend();
1263 }
1264
1265 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type size() const _NOEXCEPT {
1266 return __is_long() ? __get_long_size() : __get_short_size();
1267 }
1268 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type length() const _NOEXCEPT {
1269 return size();
1270 }
1271
1272 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type max_size() const _NOEXCEPT {
1273 if (size_type __m = __alloc_traits::max_size(__alloc_); __m <= std::numeric_limits<size_type>::max() / 2) {
1274 size_type __res = __m - __alignment;
1275
1276 // When the __endian_factor == 2, our string representation assumes that the capacity
1277 // (including the null terminator) is always even, so we have to make sure the lowest bit isn't set when the
1278 // string grows to max_size()
1279 if (__endian_factor == 2)
1280 __res &= ~size_type(1);
1281
1282 // We have to allocate space for the null terminator, but max_size() doesn't include it.
1283 return __res - 1;
1284 } else {
1285 bool __uses_lsb = __endian_factor == 2;
1286 return __uses_lsb ? __m - __alignment - 1 : (__m / 2) - __alignment - 1;
1287 }
1288 }
1289
1290 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type capacity() const _NOEXCEPT {
1291 return (__is_long() ? __get_long_cap() : static_cast<size_type>(__min_cap)) - 1;
1292 }
1293
1294 _LIBCPP_CONSTEXPR_SINCE_CXX20 void resize(size_type __n, value_type __c);
1295 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void resize(size_type __n) { resize(__n, value_type()); }
1296
1297 _LIBCPP_CONSTEXPR_SINCE_CXX20 void reserve(size_type __requested_capacity);
1298
1299# if _LIBCPP_STD_VER >= 23
1300 template <class _Op>
1301 _LIBCPP_HIDE_FROM_ABI constexpr void resize_and_overwrite(size_type __n, _Op __op) {
1302 using __result_type = decltype(std::move(__op)(data(), auto(__n)));
1303 static_assert(__integer_like<__result_type>, "Operation return type must be integer-like");
1304 size_type __sz = size();
1305 size_type __cap = capacity();
1306 if (__n > __cap)
1307 __grow_by_without_replace(old_cap: __cap, delta_cap: __n - __cap, old_sz: __sz, n_copy: __sz, n_del: 0);
1308 __annotate_delete();
1309 __set_size(s: __n);
1310 __annotate_new(current_size: __n);
1311 __erase_to_end(pos: std::move(__op)(data(), auto(__n)));
1312 }
1313# endif
1314
1315# if _LIBCPP_STD_VER < 26 || defined(_LIBCPP_ENABLE_CXX26_REMOVED_STRING_RESERVE)
1316 _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_HIDE_FROM_ABI void reserve() _NOEXCEPT { shrink_to_fit(); }
1317# endif
1318 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void shrink_to_fit() _NOEXCEPT;
1319 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void clear() _NOEXCEPT;
1320
1321 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool empty() const _NOEXCEPT {
1322 return size() == 0;
1323 }
1324
1325 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference
1326 operator[](size_type __pos) const _NOEXCEPT {
1327 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__pos <= size(), "string index out of bounds");
1328 if (__builtin_constant_p(__pos) && !__fits_in_sso(sz: __pos)) {
1329 return *(__get_long_pointer() + __pos);
1330 }
1331 return *(data() + __pos);
1332 }
1333
1334 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference
1335 operator[](size_type __pos) _NOEXCEPT {
1336 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__pos <= size(), "string index out of bounds");
1337 if (__builtin_constant_p(__pos) && !__fits_in_sso(sz: __pos)) {
1338 return *(__get_long_pointer() + __pos);
1339 }
1340 return *(__get_pointer() + __pos);
1341 }
1342
1343 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference at(size_type __n) const;
1344 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 reference at(size_type __n);
1345
1346 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator+=(const basic_string& __str) {
1347 return append(__str);
1348 }
1349
1350 template <class _Tp,
1351 __enable_if_t<__can_be_converted_to_string_view_v<_CharT, _Traits, _Tp> &&
1352 !is_same<__remove_cvref_t<_Tp>, basic_string >::value,
1353 int> = 0>
1354 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator+=(const _Tp& __t) {
1355 __self_view __sv = __t;
1356 return append(__sv);
1357 }
1358
1359 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1360 operator+=(const value_type* _LIBCPP_DIAGNOSE_NULLPTR __s) {
1361 return append(__s);
1362 }
1363
1364 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator+=(value_type __c) {
1365 push_back(__c);
1366 return *this;
1367 }
1368
1369# ifndef _LIBCPP_CXX03_LANG
1370 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator+=(initializer_list<value_type> __il) {
1371 return append(__il);
1372 }
1373# endif // _LIBCPP_CXX03_LANG
1374
1375 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(const basic_string& __str) {
1376 return append(__str.data(), __str.size());
1377 }
1378
1379 template <class _Tp,
1380 __enable_if_t<__can_be_converted_to_string_view_v<_CharT, _Traits, _Tp> &&
1381 !is_same<__remove_cvref_t<_Tp>, basic_string>::value,
1382 int> = 0>
1383 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(const _Tp& __t) {
1384 __self_view __sv = __t;
1385 return append(__sv.data(), __sv.size());
1386 }
1387
1388 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(const basic_string& __str, size_type __pos, size_type __n = npos);
1389
1390 template <class _Tp,
1391 __enable_if_t<__can_be_converted_to_string_view_v<_CharT, _Traits, _Tp> &&
1392 !is_same<__remove_cvref_t<_Tp>, basic_string>::value,
1393 int> = 0>
1394 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1395 append(const _Tp& __t, size_type __pos, size_type __n = npos) {
1396 __self_view __sv = __t;
1397 size_type __sz = __sv.size();
1398 if (__pos > __sz)
1399 __throw_out_of_range();
1400 return append(__sv.data() + __pos, std::min(__n, __sz - __pos));
1401 }
1402
1403 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(const value_type* __s, size_type __n)
1404 _LIBCPP_DIAGNOSE_NULLPTR_IF(__n != 0 && __s == nullptr, " if n is not zero");
1405 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(const value_type* _LIBCPP_DIAGNOSE_NULLPTR __s);
1406 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(size_type __n, value_type __c);
1407
1408 template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> = 0>
1409 _LIBCPP_HIDE_FROM_ABI _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1410 append(_InputIterator __first, _InputIterator __last) {
1411 const basic_string __temp(__first, __last, __alloc_);
1412 append(__temp.data(), __temp.size());
1413 return *this;
1414 }
1415
1416 template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> = 0>
1417 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1418 append(_ForwardIterator __first, _ForwardIterator __last) {
1419 size_type __sz = size();
1420 size_type __cap = capacity();
1421 size_type __n = static_cast<size_type>(std::distance(__first, __last));
1422 if (__n == 0)
1423 return *this;
1424
1425 if (__string_is_trivial_iterator_v<_ForwardIterator> && !__addr_in_range(*__first)) {
1426 if (__cap - __sz < __n)
1427 __grow_by_without_replace(old_cap: __cap, delta_cap: __sz + __n - __cap, old_sz: __sz, n_copy: __sz, n_del: 0);
1428 __annotate_increase(__n);
1429 auto __end = __copy_non_overlapping_range(__first, __last, __data() + __sz);
1430 traits_type::assign(*__end, value_type());
1431 __set_size(s: __sz + __n);
1432 return *this;
1433 } else {
1434 const basic_string __temp(__first, __last, __alloc_);
1435 return append(__temp.data(), __temp.size());
1436 }
1437 }
1438
1439# if _LIBCPP_STD_VER >= 23
1440 template <_ContainerCompatibleRange<_CharT> _Range>
1441 _LIBCPP_HIDE_FROM_ABI constexpr basic_string& append_range(_Range&& __range) {
1442 insert_range(end(), std::forward<_Range>(__range));
1443 return *this;
1444 }
1445# endif
1446
1447# ifndef _LIBCPP_CXX03_LANG
1448 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(initializer_list<value_type> __il) {
1449 return append(__il.begin(), __il.size());
1450 }
1451# endif // _LIBCPP_CXX03_LANG
1452
1453 _LIBCPP_CONSTEXPR_SINCE_CXX20 void push_back(value_type __c);
1454 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void pop_back();
1455
1456 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference front() _NOEXCEPT {
1457 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "string::front(): string is empty");
1458 return *__get_pointer();
1459 }
1460
1461 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference front() const _NOEXCEPT {
1462 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "string::front(): string is empty");
1463 return *data();
1464 }
1465
1466 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference back() _NOEXCEPT {
1467 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "string::back(): string is empty");
1468 return *(__get_pointer() + size() - 1);
1469 }
1470
1471 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference back() const _NOEXCEPT {
1472 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "string::back(): string is empty");
1473 return *(data() + size() - 1);
1474 }
1475
1476 template <class _Tp, __enable_if_t<__can_be_converted_to_string_view_v<_CharT, _Traits, _Tp>, int> = 0>
1477 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(const _Tp& __t) {
1478 __self_view __sv = __t;
1479 return assign(__sv.data(), __sv.size());
1480 }
1481
1482# if _LIBCPP_STD_VER >= 20
1483 _LIBCPP_HIDE_FROM_ABI constexpr void __move_assign(basic_string&& __str, size_type __pos, size_type __len) {
1484 // Pilfer the allocation from __str.
1485 _LIBCPP_ASSERT_INTERNAL(__alloc_ == __str.__alloc_, "__move_assign called with wrong allocator");
1486 size_type __old_sz = __str.size();
1487 if (!__str.__is_long())
1488 __str.__annotate_delete();
1489 __rep_ = __str.__rep_;
1490 __str.__rep_ = __rep();
1491 __str.__annotate_new(0);
1492
1493 _Traits::move(data(), data() + __pos, __len);
1494 __set_size(s: __len);
1495 _Traits::assign(data()[__len], value_type());
1496
1497 if (!__is_long()) {
1498 __annotate_new(current_size: __len);
1499 } else if (__old_sz > __len) {
1500 __annotate_shrink(old_size: __old_sz);
1501 }
1502 }
1503# endif
1504
1505 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(const basic_string& __str) {
1506 return *this = __str;
1507 }
1508# ifndef _LIBCPP_CXX03_LANG
1509 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1510 assign(basic_string&& __str) noexcept(__noexcept_move_assign_container<_Allocator, __alloc_traits>::value) {
1511 *this = std::move(__str);
1512 return *this;
1513 }
1514# endif
1515 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n = npos);
1516
1517 template <class _Tp,
1518 __enable_if_t<__can_be_converted_to_string_view_v<_CharT, _Traits, _Tp> &&
1519 !is_same<__remove_cvref_t<_Tp>, basic_string>::value,
1520 int> = 0>
1521 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1522 assign(const _Tp& __t, size_type __pos, size_type __n = npos) {
1523 __self_view __sv = __t;
1524 size_type __sz = __sv.size();
1525 if (__pos > __sz)
1526 __throw_out_of_range();
1527 return assign(__sv.data() + __pos, std::min(__n, __sz - __pos));
1528 }
1529
1530 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(const value_type* __s, size_type __n)
1531 _LIBCPP_DIAGNOSE_NULLPTR_IF(__n != 0 && __s == nullptr, " if n is not zero");
1532 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(const value_type* _LIBCPP_DIAGNOSE_NULLPTR __s);
1533 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(size_type __n, value_type __c);
1534
1535 template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> = 0>
1536 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1537 assign(_InputIterator __first, _InputIterator __last) {
1538 __assign_with_sentinel(__first, __last);
1539 return *this;
1540 }
1541
1542 template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> = 0>
1543 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1544 assign(_ForwardIterator __first, _ForwardIterator __last) {
1545 if (__string_is_trivial_iterator_v<_ForwardIterator>) {
1546 size_type __n = static_cast<size_type>(std::distance(__first, __last));
1547 __assign_trivial(__first, __last, __n);
1548 } else {
1549 __assign_with_sentinel(__first, __last);
1550 }
1551
1552 return *this;
1553 }
1554
1555# if _LIBCPP_STD_VER >= 23
1556 template <_ContainerCompatibleRange<_CharT> _Range>
1557 _LIBCPP_HIDE_FROM_ABI constexpr basic_string& assign_range(_Range&& __range) {
1558 if constexpr (__string_is_trivial_iterator_v<ranges::iterator_t<_Range>> &&
1559 (ranges::forward_range<_Range> || ranges::sized_range<_Range>)) {
1560 size_type __n = static_cast<size_type>(ranges::distance(__range));
1561 __assign_trivial(ranges::begin(__range), ranges::end(__range), __n);
1562
1563 } else {
1564 __assign_with_sentinel(ranges::begin(__range), ranges::end(__range));
1565 }
1566
1567 return *this;
1568 }
1569# endif
1570
1571# ifndef _LIBCPP_CXX03_LANG
1572 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(initializer_list<value_type> __il) {
1573 return assign(__il.begin(), __il.size());
1574 }
1575# endif // _LIBCPP_CXX03_LANG
1576
1577 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1578 insert(size_type __pos1, const basic_string& __str) {
1579 return insert(__pos1, __str.data(), __str.size());
1580 }
1581
1582 template <class _Tp, __enable_if_t<__can_be_converted_to_string_view_v<_CharT, _Traits, _Tp>, int> = 0>
1583 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& insert(size_type __pos1, const _Tp& __t) {
1584 __self_view __sv = __t;
1585 return insert(__pos1, __sv.data(), __sv.size());
1586 }
1587
1588 template <class _Tp,
1589 __enable_if_t<__can_be_converted_to_string_view_v<_CharT, _Traits, _Tp> &&
1590 !is_same<__remove_cvref_t<_Tp>, basic_string>::value,
1591 int> = 0>
1592 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1593 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n = npos) {
1594 __self_view __sv = __t;
1595 size_type __str_sz = __sv.size();
1596 if (__pos2 > __str_sz)
1597 __throw_out_of_range();
1598 return insert(__pos1, __sv.data() + __pos2, std::min(__n, __str_sz - __pos2));
1599 }
1600
1601 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1602 insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n = npos);
1603 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& insert(size_type __pos, const value_type* __s, size_type __n)
1604 _LIBCPP_DIAGNOSE_NULLPTR_IF(__n != 0 && __s == nullptr, " if n is not zero");
1605 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& insert(size_type __pos, const value_type* _LIBCPP_DIAGNOSE_NULLPTR __s);
1606 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1607 _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator insert(const_iterator __pos, value_type __c);
1608
1609# if _LIBCPP_STD_VER >= 23
1610 template <_ContainerCompatibleRange<_CharT> _Range>
1611 _LIBCPP_HIDE_FROM_ABI constexpr iterator insert_range(const_iterator __position, _Range&& __range) {
1612 if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
1613 auto __n = static_cast<size_type>(ranges::distance(__range));
1614 return __insert_with_size(__position, ranges::begin(__range), ranges::end(__range), __n);
1615
1616 } else {
1617 basic_string __temp(from_range, std::forward<_Range>(__range), __alloc_);
1618 return insert(__position, __temp.data(), __temp.data() + __temp.size());
1619 }
1620 }
1621# endif
1622
1623 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator
1624 insert(const_iterator __pos, size_type __n, value_type __c) {
1625 difference_type __p = __pos - begin();
1626 insert(static_cast<size_type>(__p), __n, __c);
1627 return begin() + __p;
1628 }
1629
1630 template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> = 0>
1631 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator
1632 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last) {
1633 const basic_string __temp(__first, __last, __alloc_);
1634 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
1635 }
1636
1637 template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> = 0>
1638 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator
1639 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last) {
1640 auto __n = static_cast<size_type>(std::distance(__first, __last));
1641 return __insert_with_size(__pos, __first, __last, __n);
1642 }
1643
1644# ifndef _LIBCPP_CXX03_LANG
1645 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator
1646 insert(const_iterator __pos, initializer_list<value_type> __il) {
1647 return insert(__pos, __il.begin(), __il.end());
1648 }
1649# endif // _LIBCPP_CXX03_LANG
1650
1651 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& erase(size_type __pos = 0, size_type __n = npos);
1652 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator erase(const_iterator __pos);
1653 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator erase(const_iterator __first, const_iterator __last);
1654
1655 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1656 replace(size_type __pos1, size_type __n1, const basic_string& __str) {
1657 return replace(__pos1, __n1, __str.data(), __str.size());
1658 }
1659
1660 template <class _Tp, __enable_if_t<__can_be_converted_to_string_view_v<_CharT, _Traits, _Tp>, int> = 0>
1661 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1662 replace(size_type __pos1, size_type __n1, const _Tp& __t) {
1663 __self_view __sv = __t;
1664 return replace(__pos1, __n1, __sv.data(), __sv.size());
1665 }
1666
1667 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1668 replace(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2 = npos);
1669
1670 template <class _Tp,
1671 __enable_if_t<__can_be_converted_to_string_view_v<_CharT, _Traits, _Tp> &&
1672 !is_same<__remove_cvref_t<_Tp>, basic_string>::value,
1673 int> = 0>
1674 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1675 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2 = npos) {
1676 __self_view __sv = __t;
1677 size_type __str_sz = __sv.size();
1678 if (__pos2 > __str_sz)
1679 __throw_out_of_range();
1680 return replace(__pos1, __n1, __sv.data() + __pos2, std::min(__n2, __str_sz - __pos2));
1681 }
1682
1683 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1684 replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2)
1685 _LIBCPP_DIAGNOSE_NULLPTR_IF(__n2 != 0 && __s == nullptr, " if n2 is not zero");
1686 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1687 replace(size_type __pos, size_type __n1, const value_type* _LIBCPP_DIAGNOSE_NULLPTR __s);
1688 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
1689
1690 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1691 replace(const_iterator __i1, const_iterator __i2, const basic_string& __str) {
1692 return replace(
1693 static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __str.data(), __str.size());
1694 }
1695
1696 template <class _Tp, __enable_if_t<__can_be_converted_to_string_view_v<_CharT, _Traits, _Tp>, int> = 0>
1697 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1698 replace(const_iterator __i1, const_iterator __i2, const _Tp& __t) {
1699 __self_view __sv = __t;
1700 return replace(__i1 - begin(), __i2 - __i1, __sv);
1701 }
1702
1703 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1704 replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n) {
1705 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
1706 }
1707
1708 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1709 replace(const_iterator __i1, const_iterator __i2, const value_type* __s) {
1710 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
1711 }
1712
1713 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1714 replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c) {
1715 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
1716 }
1717
1718 template <class _InputIterator, __enable_if_t<__has_input_iterator_category<_InputIterator>::value, int> = 0>
1719 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1720 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2) {
1721 const basic_string __temp(__j1, __j2, __alloc_);
1722 return replace(__i1, __i2, __temp);
1723 }
1724
1725# if _LIBCPP_STD_VER >= 23
1726 template <_ContainerCompatibleRange<_CharT> _Range>
1727 _LIBCPP_HIDE_FROM_ABI constexpr basic_string&
1728 replace_with_range(const_iterator __i1, const_iterator __i2, _Range&& __range) {
1729 basic_string __temp(from_range, std::forward<_Range>(__range), __alloc_);
1730 return replace(__i1, __i2, __temp);
1731 }
1732# endif
1733
1734# ifndef _LIBCPP_CXX03_LANG
1735 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1736 replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il) {
1737 return replace(__i1, __i2, __il.begin(), __il.end());
1738 }
1739# endif // _LIBCPP_CXX03_LANG
1740
1741 _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
1742
1743# if _LIBCPP_STD_VER <= 20
1744 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI
1745 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string substr(size_type __pos = 0, size_type __n = npos) const {
1746 return basic_string(*this, __pos, __n);
1747 }
1748# else
1749 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr basic_string substr(size_type __pos = 0, size_type __n = npos) const& {
1750 return basic_string(*this, __pos, __n);
1751 }
1752
1753 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr basic_string substr(size_type __pos = 0, size_type __n = npos) && {
1754 return basic_string(std::move(*this), __pos, __n);
1755 }
1756# endif
1757# if _LIBCPP_STD_VER >= 26
1758 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr __self_view subview(size_type __pos = 0, size_type __n = npos) const {
1759 return __self_view(*this).subview(__pos, __n);
1760 }
1761# endif
1762
1763 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void swap(basic_string& __str)
1764# if _LIBCPP_STD_VER >= 14
1765 _NOEXCEPT;
1766# else
1767 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v<allocator_type>);
1768# endif
1769
1770 // [string.ops]
1771 // ------------
1772
1773 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const value_type* c_str() const _NOEXCEPT {
1774 return data();
1775 }
1776
1777 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const value_type* data() const _NOEXCEPT {
1778 return std::__to_address(__get_pointer());
1779 }
1780# if _LIBCPP_STD_VER >= 17
1781 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 value_type* data() _NOEXCEPT {
1782 return std::__to_address(__get_pointer());
1783 }
1784# endif
1785
1786 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const value_type* __data() const _NOEXCEPT {
1787 return std::__to_address(__get_pointer());
1788 }
1789 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 value_type* __data() _NOEXCEPT {
1790 return std::__to_address(__get_pointer());
1791 }
1792
1793 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 allocator_type get_allocator() const _NOEXCEPT {
1794 return __alloc_;
1795 }
1796
1797 // find
1798
1799 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1800 find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT {
1801 return find(__str.data(), __pos, __str.size());
1802 }
1803
1804 template <class _Tp, __enable_if_t<__can_be_converted_to_string_view_v<_CharT, _Traits, _Tp>, int> = 0>
1805 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1806 find(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT {
1807 __self_view __sv = __t;
1808 return find(__sv.data(), __pos, __sv.size());
1809 }
1810
1811 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1812 find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT
1813 _LIBCPP_DIAGNOSE_NULLPTR_IF(__n != 0 && __s == nullptr, " if n is not zero") {
1814 _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string::find(): received nullptr");
1815 return std::__str_find<value_type, size_type, traits_type, npos>(data(), size(), __s, __pos, __n);
1816 }
1817
1818 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1819 find(const value_type* _LIBCPP_DIAGNOSE_NULLPTR __s, size_type __pos = 0) const _NOEXCEPT {
1820 _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::find(): received nullptr");
1821 return find(__s, __pos, traits_type::length(__s));
1822 }
1823
1824 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT {
1825 return std::__str_find<value_type, size_type, traits_type, npos>(data(), size(), __c, __pos);
1826 }
1827
1828 // rfind
1829
1830 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1831 rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT {
1832 return rfind(__str.data(), __pos, __str.size());
1833 }
1834
1835 template <class _Tp, __enable_if_t<__can_be_converted_to_string_view_v<_CharT, _Traits, _Tp>, int> = 0>
1836 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1837 rfind(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT {
1838 __self_view __sv = __t;
1839 return rfind(__sv.data(), __pos, __sv.size());
1840 }
1841
1842 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1843 rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT
1844 _LIBCPP_DIAGNOSE_NULLPTR_IF(__n != 0 && __s == nullptr, " if n is not zero") {
1845 _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
1846 return std::__str_rfind<value_type, size_type, traits_type, npos>(data(), size(), __s, __pos, __n);
1847 }
1848
1849 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1850 rfind(const value_type* _LIBCPP_DIAGNOSE_NULLPTR __s, size_type __pos = npos) const _NOEXCEPT {
1851 _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::rfind(): received nullptr");
1852 return rfind(__s, __pos, traits_type::length(__s));
1853 }
1854
1855 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1856 rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT {
1857 return std::__str_rfind<value_type, size_type, traits_type, npos>(data(), size(), __c, __pos);
1858 }
1859
1860 // find_first_of
1861
1862 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1863 find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT {
1864 return find_first_of(__str.data(), __pos, __str.size());
1865 }
1866
1867 template <class _Tp, __enable_if_t<__can_be_converted_to_string_view_v<_CharT, _Traits, _Tp>, int> = 0>
1868 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1869 find_first_of(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT {
1870 __self_view __sv = __t;
1871 return find_first_of(__sv.data(), __pos, __sv.size());
1872 }
1873
1874 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1875 find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT
1876 _LIBCPP_DIAGNOSE_NULLPTR_IF(__n != 0 && __s == nullptr, " if n is not zero") {
1877 _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
1878 return std::__str_find_first_of<value_type, size_type, traits_type, npos>(data(), size(), __s, __pos, __n);
1879 }
1880
1881 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1882 find_first_of(const value_type* _LIBCPP_DIAGNOSE_NULLPTR __s, size_type __pos = 0) const _NOEXCEPT {
1883 _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::find_first_of(): received nullptr");
1884 return find_first_of(__s, __pos, traits_type::length(__s));
1885 }
1886
1887 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1888 find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT {
1889 return find(__c, __pos);
1890 }
1891
1892 // find_last_of
1893
1894 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1895 find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT {
1896 return find_last_of(__str.data(), __pos, __str.size());
1897 }
1898
1899 template <class _Tp, __enable_if_t<__can_be_converted_to_string_view_v<_CharT, _Traits, _Tp>, int> = 0>
1900 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1901 find_last_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT {
1902 __self_view __sv = __t;
1903 return find_last_of(__sv.data(), __pos, __sv.size());
1904 }
1905
1906 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1907 find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT
1908 _LIBCPP_DIAGNOSE_NULLPTR_IF(__n != 0 && __s == nullptr, " if n is not zero") {
1909 _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
1910 return std::__str_find_last_of<value_type, size_type, traits_type, npos>(data(), size(), __s, __pos, __n);
1911 }
1912
1913 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1914 find_last_of(const value_type* _LIBCPP_DIAGNOSE_NULLPTR __s, size_type __pos = npos) const _NOEXCEPT {
1915 _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::find_last_of(): received nullptr");
1916 return find_last_of(__s, __pos, traits_type::length(__s));
1917 }
1918
1919 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1920 find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT {
1921 return rfind(__c, __pos);
1922 }
1923
1924 // find_first_not_of
1925
1926 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1927 find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT {
1928 return find_first_not_of(__str.data(), __pos, __str.size());
1929 }
1930
1931 template <class _Tp, __enable_if_t<__can_be_converted_to_string_view_v<_CharT, _Traits, _Tp>, int> = 0>
1932 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1933 find_first_not_of(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT {
1934 __self_view __sv = __t;
1935 return find_first_not_of(__sv.data(), __pos, __sv.size());
1936 }
1937
1938 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1939 find_first_not_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT
1940 _LIBCPP_DIAGNOSE_NULLPTR_IF(__n != 0 && __s == nullptr, " if n is not zero") {
1941 _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
1942 return std::__str_find_first_not_of<value_type, size_type, traits_type, npos>(data(), size(), __s, __pos, __n);
1943 }
1944
1945 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1946 find_first_not_of(const value_type* _LIBCPP_DIAGNOSE_NULLPTR __s, size_type __pos = 0) const _NOEXCEPT {
1947 _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::find_first_not_of(): received nullptr");
1948 return find_first_not_of(__s, __pos, traits_type::length(__s));
1949 }
1950
1951 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1952 find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT {
1953 return std::__str_find_first_not_of<value_type, size_type, traits_type, npos>(data(), size(), __c, __pos);
1954 }
1955
1956 // find_last_not_of
1957
1958 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1959 find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT {
1960 return find_last_not_of(__str.data(), __pos, __str.size());
1961 }
1962
1963 template <class _Tp, __enable_if_t<__can_be_converted_to_string_view_v<_CharT, _Traits, _Tp>, int> = 0>
1964 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1965 find_last_not_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT {
1966 __self_view __sv = __t;
1967 return find_last_not_of(__sv.data(), __pos, __sv.size());
1968 }
1969
1970 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1971 find_last_not_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT
1972 _LIBCPP_DIAGNOSE_NULLPTR_IF(__n != 0 && __s == nullptr, " if n is not zero") {
1973 _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
1974 return std::__str_find_last_not_of<value_type, size_type, traits_type, npos>(data(), size(), __s, __pos, __n);
1975 }
1976
1977 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1978 find_last_not_of(const value_type* _LIBCPP_DIAGNOSE_NULLPTR __s, size_type __pos = npos) const _NOEXCEPT {
1979 _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::find_last_not_of(): received nullptr");
1980 return find_last_not_of(__s, __pos, traits_type::length(__s));
1981 }
1982
1983 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1984 find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT {
1985 return std::__str_find_last_not_of<value_type, size_type, traits_type, npos>(data(), size(), __c, __pos);
1986 }
1987
1988 // compare
1989
1990 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 int
1991 compare(const basic_string& __str) const _NOEXCEPT {
1992 return compare(__self_view(__str));
1993 }
1994
1995 template <class _Tp, __enable_if_t<__can_be_converted_to_string_view_v<_CharT, _Traits, _Tp>, int> = 0>
1996 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 int compare(const _Tp& __t) const _NOEXCEPT {
1997 __self_view __sv = __t;
1998 size_t __lhs_sz = size();
1999 size_t __rhs_sz = __sv.size();
2000 int __result = traits_type::compare(data(), __sv.data(), std::min(a: __lhs_sz, b: __rhs_sz));
2001 if (__result != 0)
2002 return __result;
2003 if (__lhs_sz < __rhs_sz)
2004 return -1;
2005 if (__lhs_sz > __rhs_sz)
2006 return 1;
2007 return 0;
2008 }
2009
2010 template <class _Tp, __enable_if_t<__can_be_converted_to_string_view_v<_CharT, _Traits, _Tp>, int> = 0>
2011 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 int
2012 compare(size_type __pos1, size_type __n1, const _Tp& __t) const {
2013 __self_view __sv = __t;
2014 return compare(__pos1, __n1, __sv.data(), __sv.size());
2015 }
2016
2017 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 int
2018 compare(size_type __pos1, size_type __n1, const basic_string& __str) const {
2019 return compare(__pos1, __n1, __str.data(), __str.size());
2020 }
2021
2022 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 int
2023 compare(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2 = npos) const {
2024 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
2025 }
2026
2027 template <class _Tp,
2028 __enable_if_t<__can_be_converted_to_string_view_v<_CharT, _Traits, _Tp> &&
2029 !is_same<__remove_cvref_t<_Tp>, basic_string>::value,
2030 int> = 0>
2031 [[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 int
2032 compare(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2 = npos) const {
2033 __self_view __sv = __t;
2034 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
2035 }
2036
2037 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 int
2038 compare(const value_type* _LIBCPP_DIAGNOSE_NULLPTR __s) const _NOEXCEPT {
2039 _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::compare(): received nullptr");
2040 return compare(0, npos, __s, traits_type::length(__s));
2041 }
2042
2043 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 int
2044 compare(size_type __pos1, size_type __n1, const value_type* _LIBCPP_DIAGNOSE_NULLPTR __s) const {
2045 _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::compare(): received nullptr");
2046 return compare(__pos1, __n1, __s, traits_type::length(__s));
2047 }
2048
2049 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 int
2050 compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const
2051 _LIBCPP_DIAGNOSE_NULLPTR_IF(__n2 != 0 && __s == nullptr, " if n2 is not zero");
2052
2053 // starts_with
2054
2055# if _LIBCPP_STD_VER >= 20
2056 [[__nodiscard__]] constexpr _LIBCPP_HIDE_FROM_ABI bool starts_with(__self_view __sv) const noexcept {
2057 return __self_view(typename __self_view::__assume_valid(), data(), size()).starts_with(__sv);
2058 }
2059
2060 [[__nodiscard__]] constexpr _LIBCPP_HIDE_FROM_ABI bool starts_with(value_type __c) const noexcept {
2061 return !empty() && _Traits::eq(front(), __c);
2062 }
2063
2064 [[__nodiscard__]] constexpr _LIBCPP_HIDE_FROM_ABI bool
2065 starts_with(const value_type* _LIBCPP_DIAGNOSE_NULLPTR __s) const noexcept {
2066 return starts_with(__self_view(__s));
2067 }
2068
2069 // ends_with
2070
2071 [[__nodiscard__]] constexpr _LIBCPP_HIDE_FROM_ABI bool ends_with(__self_view __sv) const noexcept {
2072 return __self_view(typename __self_view::__assume_valid(), data(), size()).ends_with(__sv);
2073 }
2074
2075 [[__nodiscard__]] constexpr _LIBCPP_HIDE_FROM_ABI bool ends_with(value_type __c) const noexcept {
2076 return !empty() && _Traits::eq(back(), __c);
2077 }
2078
2079 [[__nodiscard__]] constexpr _LIBCPP_HIDE_FROM_ABI bool
2080 ends_with(const value_type* _LIBCPP_DIAGNOSE_NULLPTR __s) const noexcept {
2081 return ends_with(__self_view(__s));
2082 }
2083# endif
2084
2085 // contains
2086
2087# if _LIBCPP_STD_VER >= 23
2088 [[__nodiscard__]] constexpr _LIBCPP_HIDE_FROM_ABI bool contains(__self_view __sv) const noexcept {
2089 return __self_view(typename __self_view::__assume_valid(), data(), size()).contains(__sv);
2090 }
2091
2092 [[__nodiscard__]] constexpr _LIBCPP_HIDE_FROM_ABI bool contains(value_type __c) const noexcept {
2093 return __self_view(typename __self_view::__assume_valid(), data(), size()).contains(__c);
2094 }
2095
2096 [[__nodiscard__]] constexpr _LIBCPP_HIDE_FROM_ABI bool
2097 contains(const value_type* _LIBCPP_DIAGNOSE_NULLPTR __s) const {
2098 return __self_view(typename __self_view::__assume_valid(), data(), size()).contains(__s);
2099 }
2100# endif
2101
2102 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __invariants() const;
2103
2104private:
2105 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS bool
2106 __is_long() const _NOEXCEPT {
2107 if (__libcpp_is_constant_evaluated() && __builtin_constant_p(__rep_.__l.__is_long_)) {
2108 return __rep_.__l.__is_long_;
2109 }
2110 return __rep_.__s.__is_long_;
2111 }
2112
2113 _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI static bool __fits_in_sso(size_type __sz) { return __sz < __min_cap; }
2114
2115 template <class _Iterator, class _Sentinel>
2116 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
2117 __assign_trivial(_Iterator __first, _Sentinel __last, size_type __n);
2118
2119 template <class _Iterator, class _Sentinel>
2120 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __assign_with_sentinel(_Iterator __first, _Sentinel __last);
2121
2122 // Copy [__first, __last) into [__dest, __dest + (__last - __first)). Assumes that the ranges don't overlap.
2123 template <class _ForwardIter, class _Sent>
2124 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 static value_type*
2125 __copy_non_overlapping_range(_ForwardIter __first, _Sent __last, value_type* __dest) {
2126# ifndef _LIBCPP_CXX03_LANG
2127 if constexpr (__libcpp_is_contiguous_iterator<_ForwardIter>::value &&
2128 is_same<value_type, __remove_cvref_t<decltype(*__first)>>::value &&
2129 is_same<_ForwardIter, _Sent>::value) {
2130 _LIBCPP_ASSERT_INTERNAL(
2131 !std::__is_overlapping_range(std::__to_address(__first), std::__to_address(__last), __dest),
2132 "__copy_non_overlapping_range called with an overlapping range!");
2133 traits_type::copy(__dest, std::__to_address(__first), __last - __first);
2134 return __dest + (__last - __first);
2135 }
2136# endif
2137
2138 for (; __first != __last; ++__first)
2139 traits_type::assign(*__dest++, *__first);
2140 return __dest;
2141 }
2142
2143 template <class _ForwardIterator, class _Sentinel>
2144 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 iterator
2145 __insert_from_safe_copy(size_type __n, size_type __ip, _ForwardIterator __first, _Sentinel __last) {
2146 size_type __sz = size();
2147 size_type __cap = capacity();
2148 value_type* __p;
2149 if (__cap - __sz >= __n) {
2150 __annotate_increase(__n);
2151 __p = __data();
2152 size_type __n_move = __sz - __ip;
2153 if (__n_move != 0)
2154 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
2155 } else {
2156 __grow_by_without_replace(old_cap: __cap, delta_cap: __sz + __n - __cap, old_sz: __sz, n_copy: __ip, n_del: 0, n_add: __n);
2157 __p = std::__to_address(__get_long_pointer());
2158 }
2159 __sz += __n;
2160 __set_size(s: __sz);
2161 traits_type::assign(__p[__sz], value_type());
2162 __copy_non_overlapping_range(std::move(__first), std::move(__last), __p + __ip);
2163
2164 return begin() + __ip;
2165 }
2166
2167 template <class _Iterator, class _Sentinel>
2168 _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator
2169 __insert_with_size(const_iterator __pos, _Iterator __first, _Sentinel __last, size_type __n);
2170
2171 // internal buffer accessors
2172 // -------------------------
2173
2174 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS void
2175 __set_short_size(size_type __s) _NOEXCEPT {
2176 _LIBCPP_ASSERT_INTERNAL(__s < __min_cap, "__s should never be greater than or equal to the short string capacity");
2177 __rep_.__s.__size_ = __s;
2178 __rep_.__s.__is_long_ = false;
2179 }
2180
2181 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS size_type
2182 __get_short_size() const _NOEXCEPT {
2183 _LIBCPP_ASSERT_INTERNAL(!__rep_.__s.__is_long_, "String has to be short when trying to get the short size");
2184 __annotate_memory_barrier();
2185 return __rep_.__s.__size_;
2186 }
2187
2188 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __set_long_size(size_type __s) _NOEXCEPT {
2189 __rep_.__l.__size_ = __s;
2190 }
2191
2192 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type __get_long_size() const _NOEXCEPT {
2193 _LIBCPP_ASSERT_INTERNAL(__rep_.__l.__is_long_, "String has to be long when trying to get the long size");
2194 __annotate_memory_barrier();
2195 return __rep_.__l.__size_;
2196 }
2197
2198 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __set_size(size_type __s) _NOEXCEPT {
2199 if (__is_long())
2200 __set_long_size(__s);
2201 else
2202 __set_short_size(__s);
2203 }
2204
2205 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type __get_long_cap() const _NOEXCEPT {
2206 _LIBCPP_ASSERT_INTERNAL(__rep_.__l.__is_long_, "String has to be long when trying to get the long capacity");
2207 __annotate_memory_barrier();
2208 return __rep_.__l.__cap_ * __endian_factor;
2209 }
2210
2211 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pointer __get_long_pointer() _NOEXCEPT {
2212 _LIBCPP_ASSERT_INTERNAL(__rep_.__l.__is_long_, "String has to be long when trying to get the long pointer");
2213 __annotate_memory_barrier();
2214 return __rep_.__l.__data_;
2215 }
2216
2217 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_pointer __get_long_pointer() const _NOEXCEPT {
2218 _LIBCPP_ASSERT_INTERNAL(__rep_.__l.__is_long_, "String has to be long when trying to get the long pointer");
2219 __annotate_memory_barrier();
2220 return __rep_.__l.__data_;
2221 }
2222
2223 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS pointer
2224 __get_short_pointer() _NOEXCEPT {
2225 __annotate_memory_barrier();
2226 return pointer_traits<pointer>::pointer_to(__rep_.__s.__data_[0]);
2227 }
2228
2229 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS const_pointer
2230 __get_short_pointer() const _NOEXCEPT {
2231 __annotate_memory_barrier();
2232 return pointer_traits<const_pointer>::pointer_to(__rep_.__s.__data_[0]);
2233 }
2234
2235 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pointer __get_pointer() _NOEXCEPT {
2236 return __is_long() ? __get_long_pointer() : __get_short_pointer();
2237 }
2238 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_pointer __get_pointer() const _NOEXCEPT {
2239 return __is_long() ? __get_long_pointer() : __get_short_pointer();
2240 }
2241
2242 // Internal buffer management
2243 // --------------------------
2244 //
2245 // These functions are only responsible for managing the buffer itself, not the value inside the buffer. As such,
2246 // none of these facilities ensure that there is a null terminator at `data()[size()]`.
2247
2248 // Allocate a buffer of __capacity size with __alloc and return it
2249 _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR_SINCE_CXX20 __long
2250 __allocate_long_buffer(_Allocator& __alloc, size_type __size, size_type __min_capacity) {
2251 _LIBCPP_ASSERT_INTERNAL(
2252 __size <= __min_capacity, "Trying to allocate a long buffer with a smaller capacity than size");
2253 _LIBCPP_ASSERT_INTERNAL(!__fits_in_sso(__min_capacity),
2254 "Trying to allocate long buffer for a capacity what would fit into the small buffer");
2255 auto __buffer = std::__allocate_at_least(__alloc, __align_allocation_size(size: __min_capacity));
2256
2257 if (__libcpp_is_constant_evaluated()) {
2258 for (size_type __i = 0; __i != __buffer.count; ++__i)
2259 std::__construct_at(std::addressof(__buffer.ptr[__i]));
2260 }
2261
2262 return __long(__buffer, __size);
2263 }
2264
2265 _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR_SINCE_CXX20 __long
2266 __allocate_long_buffer(_Allocator& __alloc, size_type __size) {
2267 return __allocate_long_buffer(__alloc, __size, __size);
2268 }
2269
2270 // Replace the current buffer with __new_rep. Deallocate the old long buffer if it exists.
2271 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __reset_internal_buffer(__rep __new_rep = __short()) {
2272 __annotate_delete();
2273 if (__is_long())
2274 __alloc_traits::deallocate(__alloc_, __get_long_pointer(), __get_long_cap());
2275 __rep_ = __new_rep;
2276 }
2277
2278 // Initialize the internal buffer to hold __size elements
2279 // The elements and null terminator have to be set by the caller
2280 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pointer __init_internal_buffer(size_type __size) {
2281 if (__libcpp_is_constant_evaluated())
2282 __rep_ = __rep();
2283
2284 if (__size > max_size())
2285 __throw_length_error();
2286
2287 if (__fits_in_sso(sz: __size)) {
2288 __set_short_size(s: __size);
2289 __annotate_new(current_size: __size);
2290 return __get_short_pointer();
2291 } else {
2292 __rep_.__l = __allocate_long_buffer(__alloc_, __size);
2293 __annotate_new(current_size: __size);
2294 return __get_long_pointer();
2295 }
2296 }
2297
2298 // ASan annotation helpers
2299 // -----------------------
2300
2301 // The following functions are no-ops outside of AddressSanitizer mode.
2302 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
2303 __annotate_contiguous_container(const void* __old_mid, const void* __new_mid) const {
2304 (void)__old_mid;
2305 (void)__new_mid;
2306# if _LIBCPP_ENABLE_ASAN_CONTAINER_CHECKS_FOR_STRING
2307# if defined(__APPLE__)
2308 // TODO: remove after addressing issue #96099 (https://llvm.org/PR96099)
2309 if (!__is_long())
2310 return;
2311# endif
2312 std::__annotate_contiguous_container<_Allocator>(data(), data() + capacity() + 1, __old_mid, __new_mid);
2313# endif
2314 }
2315
2316 // Prevents speculative load before short/long state is determined.
2317 // The following functions are no-ops outside of AddressSanitizer mode.
2318 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __annotate_memory_barrier() const {
2319# if _LIBCPP_ENABLE_ASAN_CONTAINER_CHECKS_FOR_STRING
2320 if (__libcpp_is_constant_evaluated())
2321 return;
2322 asm volatile("" ::: "memory");
2323# endif
2324 }
2325
2326 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __annotate_new(size_type __current_size) const _NOEXCEPT {
2327 __annotate_contiguous_container(old_mid: data() + capacity() + 1, new_mid: data() + __current_size + 1);
2328 }
2329
2330 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __annotate_delete() const _NOEXCEPT {
2331 __annotate_contiguous_container(old_mid: data() + size() + 1, new_mid: data() + capacity() + 1);
2332 }
2333
2334 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __annotate_increase(size_type __n) const _NOEXCEPT {
2335 __annotate_contiguous_container(old_mid: data() + size() + 1, new_mid: data() + size() + 1 + __n);
2336 }
2337
2338 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __annotate_shrink(size_type __old_size) const _NOEXCEPT {
2339 __annotate_contiguous_container(old_mid: data() + __old_size + 1, new_mid: data() + size() + 1);
2340 }
2341
2342 // Disable ASan annotations and enable them again when going out of scope. It is assumed that the string is in a valid
2343 // state at that point, so `size()` can be called safely.
2344 struct [[__nodiscard__]] __annotation_guard {
2345 __annotation_guard(const __annotation_guard&) = delete;
2346 __annotation_guard& operator=(const __annotation_guard&) = delete;
2347
2348 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __annotation_guard(basic_string& __str) : __str_(__str) {
2349 __str_.__annotate_delete();
2350 }
2351
2352 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__annotation_guard() { __str_.__annotate_new(__str_.size()); }
2353
2354 basic_string& __str_;
2355 };
2356
2357 template <size_type __a>
2358 static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type __align_it(size_type __s) _NOEXCEPT {
2359 return (__s + (__a - 1)) & ~(__a - 1);
2360 }
2361 enum { __alignment = 8 };
2362
2363 // This makes sure that we're using a capacity with some extra alignment, since allocators almost always over-align
2364 // the allocations anyways, improving memory usage. More importantly, this ensures that the lowest bit is never set
2365 // if __endian_factor == 2, allowing us to store whether we're in the long string inside the lowest bit.
2366 _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
2367 __align_allocation_size(size_type __size) _NOEXCEPT {
2368 _LIBCPP_ASSERT_INTERNAL(
2369 !__fits_in_sso(__size), "Trying to align allocation of a size which would fit into the SSO");
2370 const size_type __boundary = sizeof(value_type) < __alignment ? __alignment / sizeof(value_type) : __endian_factor;
2371 size_type __guess = __align_it<__boundary>(__size + 1);
2372 if (__guess == __min_cap + 1)
2373 __guess += __endian_factor;
2374
2375 _LIBCPP_ASSERT_INTERNAL(__guess >= __size, "aligned allocation size is below the requested size");
2376 return __guess;
2377 }
2378
2379 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
2380 __get_amortized_growth_capacity(size_type __required_capacity) {
2381 size_type __max_size = max_size();
2382 if (__required_capacity > __max_size)
2383 __throw_length_error();
2384 size_type __current_cap = capacity();
2385 _LIBCPP_ASSERT_INTERNAL(
2386 __current_cap < __required_capacity, "Trying to grow string even though there is enough capacity already?");
2387 if (__current_cap > __max_size / 2 - __alignment)
2388 return __max_size;
2389 return std::max(__required_capacity, 2 * __current_cap);
2390 }
2391
2392 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void __init(const value_type* __s, size_type __sz);
2393 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void __init(size_type __n, value_type __c);
2394
2395 // Slow path for the (inlined) copy constructor for 'long' strings.
2396 // Always externally instantiated and not inlined.
2397 // Requires that __s is zero terminated.
2398 // The main reason for this function to exist is because for unstable, we
2399 // want to allow inlining of the copy constructor. However, we don't want
2400 // to call the __init() functions as those are marked as inline which may
2401 // result in over-aggressive inlining by the compiler, where our aim is
2402 // to only inline the fast path code directly in the ctor.
2403 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE void __init_copy_ctor_external(const value_type* __s, size_type __sz);
2404
2405 template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> = 0>
2406 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void __init(_InputIterator __first, _InputIterator __last);
2407
2408 template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> = 0>
2409 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void __init(_ForwardIterator __first, _ForwardIterator __last);
2410
2411 template <class _InputIterator, class _Sentinel>
2412 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
2413 __init_with_sentinel(_InputIterator __first, _Sentinel __last);
2414 template <class _InputIterator, class _Sentinel>
2415 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
2416 __init_with_size(_InputIterator __first, _Sentinel __last, size_type __sz);
2417
2418 _LIBCPP_CONSTEXPR_SINCE_CXX20
2419# if _LIBCPP_ABI_VERSION >= 2 // We want to use the function in the dylib in ABIv1
2420 _LIBCPP_HIDE_FROM_ABI
2421# endif
2422 _LIBCPP_DEPRECATED_("use __grow_by_without_replace") void __grow_by(
2423 size_type __old_cap,
2424 size_type __delta_cap,
2425 size_type __old_sz,
2426 size_type __n_copy,
2427 size_type __n_del,
2428 size_type __n_add = 0);
2429 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __grow_by_without_replace(
2430 size_type __old_cap,
2431 size_type __delta_cap,
2432 size_type __old_sz,
2433 size_type __n_copy,
2434 size_type __n_del,
2435 size_type __n_add = 0);
2436 _LIBCPP_CONSTEXPR_SINCE_CXX20 void __grow_by_and_replace(
2437 size_type __old_cap,
2438 size_type __delta_cap,
2439 size_type __old_sz,
2440 size_type __n_copy,
2441 size_type __n_del,
2442 size_type __n_add,
2443 const value_type* __p_new_stuff);
2444
2445 // __assign_no_alias is invoked for assignment operations where we
2446 // have proof that the input does not alias the current instance.
2447 // For example, operator=(basic_string) performs a 'self' check.
2448 template <bool __is_short>
2449 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string& __assign_no_alias(const value_type* __s, size_type __n);
2450
2451 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __erase_to_end(size_type __pos) {
2452 _LIBCPP_ASSERT_INTERNAL(__pos <= capacity(), "Trying to erase at position outside the strings capacity!");
2453 __null_terminate_at(p: __data(), newsz: __pos);
2454 }
2455
2456 // __erase_external_with_move is invoked for erase() invocations where
2457 // `n ~= npos`, likely requiring memory moves on the string data.
2458 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE void
2459 __erase_external_with_move(size_type __pos, size_type __n) _NOEXCEPT;
2460
2461 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __copy_assign_alloc(const basic_string& __str) {
2462 __copy_assign_alloc(
2463 __str, integral_constant<bool, __alloc_traits::propagate_on_container_copy_assignment::value>());
2464 }
2465
2466 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __copy_assign_alloc(const basic_string& __str, true_type) {
2467 if (__alloc_ == __str.__alloc_)
2468 __alloc_ = __str.__alloc_;
2469 else {
2470 if (!__str.__is_long()) {
2471 __reset_internal_buffer();
2472 __alloc_ = __str.__alloc_;
2473 } else {
2474 __annotate_delete();
2475 auto __guard = std::__make_scope_guard(__annotate_new_size(*this));
2476 auto __alloc = __str.__alloc_;
2477 __reset_internal_buffer(new_rep: __allocate_long_buffer(__alloc, __str.size()));
2478 __alloc_ = std::move(__alloc);
2479 }
2480 }
2481 }
2482
2483 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
2484 __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT {}
2485
2486# ifndef _LIBCPP_CXX03_LANG
2487 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
2488 __move_assign(basic_string& __str, false_type) noexcept(__alloc_traits::is_always_equal::value);
2489 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS void
2490 __move_assign(basic_string& __str, true_type)
2491# if _LIBCPP_STD_VER >= 17
2492 noexcept;
2493# else
2494 noexcept(is_nothrow_move_assignable<allocator_type>::value);
2495# endif
2496# endif
2497
2498 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __move_assign_alloc(basic_string& __str)
2499 _NOEXCEPT_(!__alloc_traits::propagate_on_container_move_assignment::value ||
2500 is_nothrow_move_assignable<allocator_type>::value) {
2501 __move_assign_alloc(
2502 __str, integral_constant<bool, __alloc_traits::propagate_on_container_move_assignment::value>());
2503 }
2504
2505 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __move_assign_alloc(basic_string& __c, true_type)
2506 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value) {
2507 __alloc_ = std::move(__c.__alloc_);
2508 }
2509
2510 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __move_assign_alloc(basic_string&, false_type) _NOEXCEPT {}
2511
2512 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string& __assign_external(const value_type* __s);
2513 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string& __assign_external(const value_type* __s, size_type __n);
2514
2515 // Assigns the value in __s, guaranteed to be __n < __min_cap in length.
2516 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& __assign_short(const value_type* __s, size_type __n) {
2517 size_type __old_size = size();
2518 if (__n > __old_size)
2519 __annotate_increase(n: __n - __old_size);
2520 pointer __p;
2521 if (__is_long()) {
2522 __set_long_size(s: __n);
2523 __p = __get_long_pointer();
2524 } else {
2525 __set_short_size(s: __n);
2526 __p = __get_short_pointer();
2527 }
2528 traits_type::move(std::__to_address(__p), __s, __n);
2529 traits_type::assign(__p[__n], value_type());
2530 if (__old_size > __n)
2531 __annotate_shrink(__old_size);
2532 return *this;
2533 }
2534
2535 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
2536 __null_terminate_at(value_type* __p, size_type __newsz) {
2537 size_type __old_size = size();
2538 if (__newsz > __old_size)
2539 __annotate_increase(n: __newsz - __old_size);
2540 __set_size(s: __newsz);
2541 traits_type::assign(__p[__newsz], value_type());
2542 if (__old_size > __newsz)
2543 __annotate_shrink(__old_size);
2544 return *this;
2545 }
2546
2547 template <class _Tp>
2548 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __addr_in_range(const _Tp& __v) const {
2549 return std::__is_pointer_in_range(data(), data() + size() + 1, std::addressof(__v));
2550 }
2551
2552 [[__noreturn__]] _LIBCPP_HIDE_FROM_ABI static void __throw_length_error() {
2553 std::__throw_length_error(msg: "basic_string");
2554 }
2555
2556 [[__noreturn__]] _LIBCPP_HIDE_FROM_ABI static void __throw_out_of_range() {
2557 std::__throw_out_of_range(msg: "basic_string");
2558 }
2559
2560 friend _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string
2561 __concatenate_strings<>(const _Allocator&, __type_identity_t<__self_view>, __type_identity_t<__self_view>);
2562
2563 template <class _CharT2, class _Traits2, class _Allocator2>
2564 friend inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool
2565 operator==(const basic_string<_CharT2, _Traits2, _Allocator2>&, const _CharT2*) _NOEXCEPT;
2566
2567 // These functions aren't used anymore but are part of out ABI, so we need to provide them in the dylib for backwards
2568 // compatibility
2569# ifdef _LIBCPP_BUILDING_LIBRARY
2570 void __init(const value_type* __s, size_type __sz, size_type __reserve);
2571# endif
2572};
2573
2574// These declarations must appear before any functions are implicitly used
2575// so that they have the correct visibility specifier.
2576# define _LIBCPP_DECLARE(...) extern template _LIBCPP_EXPORTED_FROM_ABI __VA_ARGS__;
2577# ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
2578_LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, char)
2579# if _LIBCPP_HAS_WIDE_CHARACTERS
2580_LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, wchar_t)
2581# endif
2582# else
2583_LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, char)
2584# if _LIBCPP_HAS_WIDE_CHARACTERS
2585_LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, wchar_t)
2586# endif
2587# endif
2588# undef _LIBCPP_DECLARE
2589
2590# if _LIBCPP_STD_VER <= 17 || !__has_builtin(__builtin_lt_synthesizes_from_spaceship)
2591template <class _CharT, class _Traits, class _Alloc>
2592struct __default_three_way_comparator<basic_string<_CharT, _Traits, _Alloc>, basic_string<_CharT, _Traits, _Alloc> > {
2593 using __string_t _LIBCPP_NODEBUG = basic_string<_CharT, _Traits, _Alloc>;
2594
2595 _LIBCPP_HIDE_FROM_ABI static int operator()(const __string_t& __lhs, const __string_t& __rhs) {
2596 auto __min_len = std::min(__lhs.size(), __rhs.size());
2597 auto __ret = _Traits::compare(__lhs.data(), __rhs.data(), __min_len);
2598 if (__ret == 0)
2599 return __lhs.size() == __rhs.size() ? 0 : __lhs.size() < __rhs.size() ? -1 : 1;
2600 return __ret;
2601 }
2602};
2603# endif
2604
2605template <class _Comparator, class _CharT, class _Traits, class _Alloc>
2606inline const bool __is_transparently_comparable_v<_Comparator,
2607 basic_string<_CharT, _Traits, _Alloc>,
2608 const _CharT*,
2609 __enable_if_t<__is_generic_transparent_comparator_v<_Comparator> > > =
2610 true;
2611
2612template <class _Comparator, class _CharT, class _Traits, class _Alloc, size_t _Np>
2613inline const bool __is_transparently_comparable_v<_Comparator,
2614 basic_string<_CharT, _Traits, _Alloc>,
2615 _CharT[_Np],
2616 __enable_if_t<__is_generic_transparent_comparator_v<_Comparator> > > =
2617 true;
2618
2619# if _LIBCPP_STD_VER >= 17
2620template <class _InputIterator,
2621 class _CharT = __iterator_value_type<_InputIterator>,
2622 class _Allocator = allocator<_CharT>,
2623 class = enable_if_t<__has_input_iterator_category<_InputIterator>::value>,
2624 class = enable_if_t<__is_allocator_v<_Allocator>>>
2625basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
2626 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
2627
2628template <class _CharT,
2629 class _Traits,
2630 class _Allocator = allocator<_CharT>,
2631 class = enable_if_t<__is_allocator_v<_Allocator>>>
2632explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
2633 -> basic_string<_CharT, _Traits, _Allocator>;
2634
2635template <class _CharT,
2636 class _Traits,
2637 class _Allocator = allocator<_CharT>,
2638 class = enable_if_t<__is_allocator_v<_Allocator>>,
2639 class _Sz = typename allocator_traits<_Allocator>::size_type >
2640basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
2641 -> basic_string<_CharT, _Traits, _Allocator>;
2642# endif
2643
2644# if _LIBCPP_STD_VER >= 23
2645template <ranges::input_range _Range,
2646 class _Allocator = allocator<ranges::range_value_t<_Range>>,
2647 class = enable_if_t<__is_allocator_v<_Allocator>>>
2648basic_string(from_range_t, _Range&&, _Allocator = _Allocator())
2649 -> basic_string<ranges::range_value_t<_Range>, char_traits<ranges::range_value_t<_Range>>, _Allocator>;
2650# endif
2651
2652template <class _CharT, class _Traits, class _Allocator>
2653_LIBCPP_CONSTEXPR_SINCE_CXX20 void
2654basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz) {
2655 pointer __p = __init_internal_buffer(size: __sz);
2656 traits_type::copy(std::__to_address(__p), __s, __sz);
2657 traits_type::assign(__p[__sz], value_type());
2658}
2659
2660template <class _CharT, class _Traits, class _Allocator>
2661_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE void
2662basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(const value_type* __s, size_type __sz) {
2663 pointer __p = __init_internal_buffer(size: __sz);
2664 traits_type::copy(std::__to_address(__p), __s, __sz + 1);
2665}
2666
2667template <class _CharT, class _Traits, class _Allocator>
2668_LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c) {
2669 pointer __p = __init_internal_buffer(size: __n);
2670 traits_type::assign(std::__to_address(__p), __n, __c);
2671 traits_type::assign(__p[__n], value_type());
2672}
2673
2674template <class _CharT, class _Traits, class _Allocator>
2675template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> >
2676_LIBCPP_CONSTEXPR_SINCE_CXX20 void
2677basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last) {
2678 __init_with_sentinel(std::move(__first), std::move(__last));
2679}
2680
2681template <class _CharT, class _Traits, class _Allocator>
2682template <class _InputIterator, class _Sentinel>
2683_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
2684basic_string<_CharT, _Traits, _Allocator>::__init_with_sentinel(_InputIterator __first, _Sentinel __last) {
2685 __rep_ = __rep();
2686 __annotate_new(current_size: 0);
2687
2688 auto __guard = std::__make_exception_guard([this] { __reset_internal_buffer(); });
2689 for (; __first != __last; ++__first)
2690 push_back(c: *__first);
2691 __guard.__complete();
2692}
2693
2694template <class _CharT, class _Traits, class _Allocator>
2695template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> >
2696_LIBCPP_CONSTEXPR_SINCE_CXX20 void
2697basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last) {
2698 size_type __sz = static_cast<size_type>(std::distance(__first, __last));
2699 __init_with_size(__first, __last, __sz);
2700}
2701
2702template <class _CharT, class _Traits, class _Allocator>
2703template <class _InputIterator, class _Sentinel>
2704_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
2705basic_string<_CharT, _Traits, _Allocator>::__init_with_size(_InputIterator __first, _Sentinel __last, size_type __sz) {
2706 pointer __p = __init_internal_buffer(size: __sz);
2707
2708 auto __guard = std::__make_exception_guard([this] { __reset_internal_buffer(); });
2709 auto __end = __copy_non_overlapping_range(std::move(__first), std::move(__last), std::__to_address(__p));
2710 traits_type::assign(*__end, value_type());
2711 __guard.__complete();
2712}
2713
2714template <class _CharT, class _Traits, class _Allocator>
2715_LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace(
2716 size_type __old_cap,
2717 size_type __delta_cap,
2718 size_type __old_sz,
2719 size_type __n_copy,
2720 size_type __n_del,
2721 size_type __n_add,
2722 const value_type* __p_new_stuff) {
2723 __long __buffer = __allocate_long_buffer(__alloc_, 0, __get_amortized_growth_capacity(required_capacity: __old_cap + __delta_cap));
2724 value_type* __old_p = __data();
2725 __annotate_delete();
2726 auto __guard = std::__make_scope_guard(__annotate_new_size(*this));
2727 if (__n_copy != 0)
2728 traits_type::copy(std::__to_address(__buffer.__data_), __old_p, __n_copy);
2729 if (__n_add != 0)
2730 traits_type::copy(std::__to_address(__buffer.__data_) + __n_copy, __p_new_stuff, __n_add);
2731 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2732 if (__sec_cp_sz != 0)
2733 traits_type::copy(
2734 std::__to_address(__buffer.__data_) + __n_copy + __n_add, __old_p + __n_copy + __n_del, __sec_cp_sz);
2735 __buffer.__size_ = __n_copy + __n_add + __sec_cp_sz;
2736 traits_type::assign(__buffer.__data_[__buffer.__size_], value_type());
2737 __reset_internal_buffer(new_rep: __buffer);
2738}
2739
2740// __grow_by is deprecated because it does not set the size. It may not update the size when the size is changed, and it
2741// may also not set the size at all when the string was short initially. This leads to unpredictable size value. It is
2742// not removed or changed to avoid breaking the ABI.
2743template <class _CharT, class _Traits, class _Allocator>
2744void _LIBCPP_CONSTEXPR_SINCE_CXX20
2745# if _LIBCPP_ABI_VERSION >= 2 // We want to use the function in the dylib in ABIv1
2746_LIBCPP_HIDE_FROM_ABI
2747# endif
2748_LIBCPP_DEPRECATED_("use __grow_by_without_replace") basic_string<_CharT, _Traits, _Allocator>::__grow_by(
2749 size_type __old_cap,
2750 size_type __delta_cap,
2751 size_type __old_sz,
2752 size_type __n_copy,
2753 size_type __n_del,
2754 size_type __n_add) {
2755 __long __buffer = __allocate_long_buffer(__alloc_, 0, __get_amortized_growth_capacity(required_capacity: __old_cap + __delta_cap));
2756 value_type* __old_p = __data();
2757 if (__n_copy != 0)
2758 traits_type::copy(std::__to_address(__buffer.__data_), __old_p, __n_copy);
2759 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2760 if (__sec_cp_sz != 0)
2761 traits_type::copy(
2762 std::__to_address(__buffer.__data_) + __n_copy + __n_add, __old_p + __n_copy + __n_del, __sec_cp_sz);
2763 // This is -1 to make sure the caller sets the size properly, since old versions of this function didn't set the size
2764 // at all.
2765 __buffer.__size_ = -1;
2766 __reset_internal_buffer(new_rep: __buffer);
2767}
2768
2769template <class _CharT, class _Traits, class _Allocator>
2770void _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
2771basic_string<_CharT, _Traits, _Allocator>::__grow_by_without_replace(
2772 size_type __old_cap,
2773 size_type __delta_cap,
2774 size_type __old_sz,
2775 size_type __n_copy,
2776 size_type __n_del,
2777 size_type __n_add) {
2778 __annotate_delete();
2779 auto __guard = std::__make_scope_guard(__annotate_new_size(*this));
2780 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
2781 __grow_by(__old_cap, __delta_cap, __old_sz, __n_copy, __n_del, __n_add);
2782 _LIBCPP_SUPPRESS_DEPRECATED_POP
2783 // Due to the ABI of __grow_by we have to set the size after calling it.
2784 __set_long_size(s: __old_sz - __n_del + __n_add);
2785}
2786
2787// assign
2788
2789template <class _CharT, class _Traits, class _Allocator>
2790template <bool __is_short>
2791_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string<_CharT, _Traits, _Allocator>&
2792basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(const value_type* __s, size_type __n) {
2793 const auto __cap = __is_short ? static_cast<size_type>(__min_cap) : __get_long_cap();
2794 const auto __size = __is_short ? __get_short_size() : __get_long_size();
2795 if (__n >= __cap) {
2796 __grow_by_and_replace(old_cap: __cap - 1, delta_cap: __n - __cap + 1, old_sz: __size, n_copy: 0, n_del: __size, n_add: __n, p_new_stuff: __s);
2797 return *this;
2798 }
2799
2800 __annotate_delete();
2801 auto __guard = std::__make_scope_guard(__annotate_new_size(*this));
2802 pointer __p;
2803 if (__is_short) {
2804 __p = __get_short_pointer();
2805 __set_short_size(s: __n);
2806 } else {
2807 __p = __get_long_pointer();
2808 __set_long_size(s: __n);
2809 }
2810 traits_type::copy(std::__to_address(__p), __s, __n);
2811 traits_type::assign(__p[__n], value_type());
2812 return *this;
2813}
2814
2815template <class _CharT, class _Traits, class _Allocator>
2816_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string<_CharT, _Traits, _Allocator>&
2817basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s, size_type __n) {
2818 const auto __cap = capacity();
2819 const auto __size = size();
2820 if (__cap >= __n) {
2821 if (__n > __size)
2822 __annotate_increase(n: __n - __size);
2823 value_type* __p = __data();
2824 traits_type::move(__p, __s, __n);
2825 return __null_terminate_at(__p, newsz: __n);
2826 } else {
2827 __grow_by_and_replace(old_cap: __cap, delta_cap: __n - __cap, old_sz: __size, n_copy: 0, n_del: __size, n_add: __n, p_new_stuff: __s);
2828 return *this;
2829 }
2830}
2831
2832template <class _CharT, class _Traits, class _Allocator>
2833_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
2834basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n) {
2835 _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string::assign received nullptr");
2836 return (__builtin_constant_p(__n) && __fits_in_sso(sz: __n)) ? __assign_short(__s, __n) : __assign_external(__s, __n);
2837}
2838
2839template <class _CharT, class _Traits, class _Allocator>
2840_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
2841basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c) {
2842 size_type __cap = capacity();
2843 size_type __old_size = size();
2844 if (__cap < __n) {
2845 __grow_by_without_replace(old_cap: __cap, delta_cap: __n - __cap, old_sz: __old_size, n_copy: 0, n_del: __old_size);
2846 __annotate_increase(__n);
2847 } else if (__n > __old_size)
2848 __annotate_increase(n: __n - __old_size);
2849 value_type* __p = __data();
2850 traits_type::assign(__p, __n, __c);
2851 return __null_terminate_at(__p, newsz: __n);
2852}
2853
2854template <class _CharT, class _Traits, class _Allocator>
2855_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
2856basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c) {
2857 size_type __old_size = size();
2858 if (__old_size == 0)
2859 __annotate_increase(n: 1);
2860 pointer __p;
2861 if (__is_long()) {
2862 __p = __get_long_pointer();
2863 __set_long_size(s: 1);
2864 } else {
2865 __p = __get_short_pointer();
2866 __set_short_size(s: 1);
2867 }
2868 traits_type::assign(*__p, __c);
2869 traits_type::assign(*++__p, value_type());
2870 if (__old_size > 1)
2871 __annotate_shrink(__old_size);
2872 return *this;
2873}
2874
2875template <class _CharT, class _Traits, class _Allocator>
2876_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS basic_string<_CharT, _Traits, _Allocator>&
2877basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str) {
2878 if (this == std::addressof(__str))
2879 return *this;
2880
2881 __copy_assign_alloc(__str);
2882
2883 if (__is_long())
2884 return __assign_no_alias<false>(__str.data(), __str.size());
2885
2886 if (__str.__is_long())
2887 return __assign_no_alias<true>(__str.data(), __str.size());
2888
2889 __annotate_delete();
2890 auto __guard = std::__make_scope_guard(__annotate_new_size(*this));
2891 __rep_ = __str.__rep_;
2892
2893 return *this;
2894}
2895
2896# ifndef _LIBCPP_CXX03_LANG
2897
2898template <class _CharT, class _Traits, class _Allocator>
2899inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::__move_assign(
2900 basic_string& __str, false_type) noexcept(__alloc_traits::is_always_equal::value) {
2901 if (__alloc_ != __str.__alloc_)
2902 assign(__str);
2903 else
2904 __move_assign(__str, true_type());
2905}
2906
2907template <class _CharT, class _Traits, class _Allocator>
2908inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS void
2909basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
2910# if _LIBCPP_STD_VER >= 17
2911 noexcept
2912# else
2913 noexcept(is_nothrow_move_assignable<allocator_type>::value)
2914# endif
2915{
2916 __annotate_delete();
2917 if (__is_long()) {
2918 __reset_internal_buffer();
2919# if _LIBCPP_STD_VER <= 14
2920 if (!is_nothrow_move_assignable<allocator_type>::value) {
2921 __set_short_size(0);
2922 traits_type::assign(__get_short_pointer()[0], value_type());
2923 __annotate_new(0);
2924 }
2925# endif
2926 }
2927 size_type __str_old_size = __str.size();
2928 bool __str_was_short = !__str.__is_long();
2929
2930 __move_assign_alloc(__str);
2931 __rep_ = __str.__rep_;
2932 __str.__set_short_size(0);
2933 traits_type::assign(__str.__get_short_pointer()[0], value_type());
2934
2935 if (__str_was_short && this != std::addressof(__str))
2936 __str.__annotate_shrink(__str_old_size);
2937 else
2938 // ASan annotations: was long, so object memory is unpoisoned as new.
2939 // Or is same as *this, and __annotate_delete() was called.
2940 __str.__annotate_new(0);
2941
2942 // ASan annotations: Guard against `std::string s; s = std::move(s);`
2943 // You can find more here: https://en.cppreference.com/w/cpp/utility/move
2944 // Quote: "Unless otherwise specified, all standard library objects that have been moved
2945 // from are placed in a "valid but unspecified state", meaning the object's class
2946 // invariants hold (so functions without preconditions, such as the assignment operator,
2947 // can be safely used on the object after it was moved from):"
2948 // Quote: "v = std::move(v); // the value of v is unspecified"
2949 if (!__is_long() && std::addressof(__str) != this)
2950 // If it is long string, delete was never called on original __str's buffer.
2951 __annotate_new(current_size: __get_short_size());
2952}
2953
2954# endif
2955
2956template <class _CharT, class _Traits, class _Allocator>
2957template <class _InputIterator, class _Sentinel>
2958_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
2959basic_string<_CharT, _Traits, _Allocator>::__assign_with_sentinel(_InputIterator __first, _Sentinel __last) {
2960 const basic_string __temp(__init_with_sentinel_tag(), std::move(__first), std::move(__last), __alloc_);
2961 assign(__temp.data(), __temp.size());
2962}
2963
2964template <class _CharT, class _Traits, class _Allocator>
2965template <class _Iterator, class _Sentinel>
2966_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
2967basic_string<_CharT, _Traits, _Allocator>::__assign_trivial(_Iterator __first, _Sentinel __last, size_type __n) {
2968 _LIBCPP_ASSERT_INTERNAL(
2969 __string_is_trivial_iterator_v<_Iterator>, "The iterator type given to `__assign_trivial` must be trivial");
2970
2971 size_type __old_size = size();
2972 size_type __cap = capacity();
2973 if (__cap < __n) {
2974 // Unlike `append` functions, if the input range points into the string itself, there is no case that the input
2975 // range could get invalidated by reallocation:
2976 // 1. If the input range is a subset of the string itself, its size cannot exceed the capacity of the string,
2977 // thus no reallocation would happen.
2978 // 2. In the exotic case where the input range is the byte representation of the string itself, the string
2979 // object itself stays valid even if reallocation happens.
2980 size_type __sz = size();
2981 __grow_by_without_replace(old_cap: __cap, delta_cap: __n - __cap, old_sz: __sz, n_copy: 0, n_del: __sz);
2982 __annotate_increase(__n);
2983 } else if (__n > __old_size)
2984 __annotate_increase(n: __n - __old_size);
2985 pointer __p = __get_pointer();
2986 for (; __first != __last; ++__p, (void)++__first)
2987 traits_type::assign(*__p, *__first);
2988 traits_type::assign(*__p, value_type());
2989 __set_size(s: __n);
2990 if (__n < __old_size)
2991 __annotate_shrink(__old_size);
2992}
2993
2994template <class _CharT, class _Traits, class _Allocator>
2995_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
2996basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n) {
2997 size_type __sz = __str.size();
2998 if (__pos > __sz)
2999 this->__throw_out_of_range();
3000 return assign(__str.data() + __pos, std::min(__n, __sz - __pos));
3001}
3002
3003template <class _CharT, class _Traits, class _Allocator>
3004_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string<_CharT, _Traits, _Allocator>&
3005basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s) {
3006 return __assign_external(__s, traits_type::length(__s));
3007}
3008
3009template <class _CharT, class _Traits, class _Allocator>
3010_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
3011basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s) {
3012 _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::assign received nullptr");
3013 if (auto __len = traits_type::length(__s); __builtin_constant_p(__len) && __fits_in_sso(sz: __len))
3014 return __assign_short(__s, n: __len);
3015 return __assign_external(__s);
3016}
3017// append
3018
3019template <class _CharT, class _Traits, class _Allocator>
3020_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
3021basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n) {
3022 _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string::append received nullptr");
3023 size_type __cap = capacity();
3024 size_type __sz = size();
3025 if (__cap - __sz < __n) {
3026 __grow_by_and_replace(old_cap: __cap, delta_cap: __sz + __n - __cap, old_sz: __sz, n_copy: __sz, n_del: 0, n_add: __n, p_new_stuff: __s);
3027 return *this;
3028 }
3029
3030 if (__n == 0)
3031 return *this;
3032
3033 __annotate_increase(__n);
3034 value_type* __p = __data();
3035 traits_type::copy(__p + __sz, __s, __n);
3036 __sz += __n;
3037 __set_size(s: __sz);
3038 traits_type::assign(__p[__sz], value_type());
3039 return *this;
3040}
3041
3042template <class _CharT, class _Traits, class _Allocator>
3043_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
3044basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c) {
3045 if (__n == 0)
3046 return *this;
3047
3048 size_type __cap = capacity();
3049 size_type __sz = size();
3050 if (__cap - __sz < __n)
3051 __grow_by_without_replace(old_cap: __cap, delta_cap: __sz + __n - __cap, old_sz: __sz, n_copy: __sz, n_del: 0);
3052 __annotate_increase(__n);
3053 value_type* __p = __data();
3054 traits_type::assign(std::__to_address(__p) + __sz, __n, __c);
3055 __sz += __n;
3056 __set_size(s: __sz);
3057 traits_type::assign(__p[__sz], value_type());
3058 return *this;
3059}
3060
3061template <class _CharT, class _Traits, class _Allocator>
3062_LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c) {
3063 bool __is_short = !__is_long();
3064 size_type __cap;
3065 size_type __sz;
3066 if (__is_short) {
3067 __cap = __min_cap - 1;
3068 __sz = __get_short_size();
3069 } else {
3070 __cap = __get_long_cap() - 1;
3071 __sz = __get_long_size();
3072 }
3073 if (__sz == __cap) {
3074 __grow_by_without_replace(old_cap: __cap, delta_cap: 1, old_sz: __sz, n_copy: __sz, n_del: 0);
3075 __is_short = false; // the string is always long after __grow_by
3076 }
3077 __annotate_increase(n: 1);
3078 pointer __p;
3079 if (__is_short) {
3080 __p = __get_short_pointer() + __sz;
3081 __set_short_size(s: __sz + 1);
3082 } else {
3083 __p = __get_long_pointer() + __sz;
3084 __set_long_size(s: __sz + 1);
3085 }
3086 traits_type::assign(*__p, __c);
3087 traits_type::assign(*++__p, value_type());
3088}
3089
3090template <class _CharT, class _Traits, class _Allocator>
3091_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
3092basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n) {
3093 size_type __sz = __str.size();
3094 if (__pos > __sz)
3095 this->__throw_out_of_range();
3096 return append(__str.data() + __pos, std::min(__n, __sz - __pos));
3097}
3098
3099template <class _CharT, class _Traits, class _Allocator>
3100_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
3101basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s) {
3102 _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::append received nullptr");
3103 return append(__s, traits_type::length(__s));
3104}
3105
3106// insert
3107
3108template <class _CharT, class _Traits, class _Allocator>
3109_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
3110basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n) {
3111 _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string::insert received nullptr");
3112 size_type __sz = size();
3113 if (__pos > __sz)
3114 this->__throw_out_of_range();
3115 size_type __cap = capacity();
3116
3117 if (__cap - __sz < __n) {
3118 __grow_by_and_replace(old_cap: __cap, delta_cap: __sz + __n - __cap, old_sz: __sz, n_copy: __pos, n_del: 0, n_add: __n, p_new_stuff: __s);
3119 return *this;
3120 }
3121
3122 if (__n == 0)
3123 return *this;
3124
3125 __annotate_increase(__n);
3126 value_type* __p = __data();
3127 size_type __n_move = __sz - __pos;
3128 if (__n_move != 0) {
3129 if (std::__is_pointer_in_range(__p + __pos, __p + __sz, __s))
3130 __s += __n;
3131 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
3132 }
3133 traits_type::move(__p + __pos, __s, __n);
3134 __sz += __n;
3135 __set_size(s: __sz);
3136 traits_type::assign(__p[__sz], value_type());
3137 return *this;
3138}
3139
3140template <class _CharT, class _Traits, class _Allocator>
3141_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
3142basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c) {
3143 size_type __sz = size();
3144 if (__pos > __sz)
3145 this->__throw_out_of_range();
3146
3147 if (__n == 0)
3148 return *this;
3149
3150 size_type __cap = capacity();
3151 value_type* __p;
3152 if (__cap - __sz >= __n) {
3153 __annotate_increase(__n);
3154 __p = __data();
3155 size_type __n_move = __sz - __pos;
3156 if (__n_move != 0)
3157 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
3158 } else {
3159 __grow_by_without_replace(old_cap: __cap, delta_cap: __sz + __n - __cap, old_sz: __sz, n_copy: __pos, n_del: 0, n_add: __n);
3160 __p = std::__to_address(__get_long_pointer());
3161 }
3162 traits_type::assign(__p + __pos, __n, __c);
3163 __sz += __n;
3164 __set_size(s: __sz);
3165 traits_type::assign(__p[__sz], value_type());
3166 return *this;
3167}
3168
3169template <class _CharT, class _Traits, class _Allocator>
3170template <class _Iterator, class _Sentinel>
3171_LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::iterator
3172basic_string<_CharT, _Traits, _Allocator>::__insert_with_size(
3173 const_iterator __pos, _Iterator __first, _Sentinel __last, size_type __n) {
3174 size_type __ip = static_cast<size_type>(__pos - begin());
3175 if (__n == 0)
3176 return begin() + __ip;
3177
3178 if (__string_is_trivial_iterator_v<_Iterator> && !__addr_in_range(*__first)) {
3179 return __insert_from_safe_copy(__n, __ip, std::move(__first), std::move(__last));
3180 } else {
3181 const basic_string __temp(__init_with_sentinel_tag(), std::move(__first), std::move(__last), __alloc_);
3182 return __insert_from_safe_copy(__n, __ip, __temp.begin(), __temp.end());
3183 }
3184}
3185
3186template <class _CharT, class _Traits, class _Allocator>
3187_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
3188basic_string<_CharT, _Traits, _Allocator>::insert(
3189 size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n) {
3190 size_type __str_sz = __str.size();
3191 if (__pos2 > __str_sz)
3192 this->__throw_out_of_range();
3193 return insert(__pos1, __str.data() + __pos2, std::min(__n, __str_sz - __pos2));
3194}
3195
3196template <class _CharT, class _Traits, class _Allocator>
3197_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
3198basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s) {
3199 _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::insert received nullptr");
3200 return insert(__pos, __s, traits_type::length(__s));
3201}
3202
3203template <class _CharT, class _Traits, class _Allocator>
3204_LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::iterator
3205basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c) {
3206 size_type __ip = static_cast<size_type>(__pos - begin());
3207 size_type __sz = size();
3208 size_type __cap = capacity();
3209 value_type* __p;
3210 if (__cap == __sz) {
3211 __grow_by_without_replace(old_cap: __cap, delta_cap: 1, old_sz: __sz, n_copy: __ip, n_del: 0, n_add: 1);
3212 __p = std::__to_address(__get_long_pointer());
3213 } else {
3214 __annotate_increase(n: 1);
3215 __p = __data();
3216 size_type __n_move = __sz - __ip;
3217 if (__n_move != 0)
3218 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
3219 }
3220 traits_type::assign(__p[__ip], __c);
3221 traits_type::assign(__p[++__sz], value_type());
3222 __set_size(s: __sz);
3223 return begin() + static_cast<difference_type>(__ip);
3224}
3225
3226// replace
3227
3228template <class _CharT, class _Traits, class _Allocator>
3229_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
3230basic_string<_CharT, _Traits, _Allocator>::replace(
3231 size_type __pos, size_type __n1, const value_type* __s, size_type __n2)
3232 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK {
3233 _LIBCPP_ASSERT_NON_NULL(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
3234 size_type __sz = size();
3235 if (__pos > __sz)
3236 this->__throw_out_of_range();
3237 __n1 = std::min(__n1, __sz - __pos);
3238 size_type __cap = capacity();
3239 if (__cap - __sz + __n1 < __n2) {
3240 __grow_by_and_replace(old_cap: __cap, delta_cap: __sz - __n1 + __n2 - __cap, old_sz: __sz, n_copy: __pos, n_del: __n1, n_add: __n2, p_new_stuff: __s);
3241 return *this;
3242 }
3243
3244 value_type* __p = __data();
3245 if (__n1 != __n2) {
3246 if (__n2 > __n1)
3247 __annotate_increase(n: __n2 - __n1);
3248 size_type __n_move = __sz - __pos - __n1;
3249 if (__n_move != 0) {
3250 if (__n1 > __n2) {
3251 traits_type::move(__p + __pos, __s, __n2);
3252 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3253 return __null_terminate_at(__p, newsz: __sz + (__n2 - __n1));
3254 }
3255 if (std::__is_pointer_in_range(__p + __pos + 1, __p + __sz, __s)) {
3256 if (__p + __pos + __n1 <= __s) {
3257 __s += __n2 - __n1;
3258 } else { // __p + __pos < __s < __p + __pos + __n1
3259 traits_type::move(__p + __pos, __s, __n1);
3260 __pos += __n1;
3261 __s += __n2;
3262 __n2 -= __n1;
3263 __n1 = 0;
3264 }
3265 }
3266 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3267 }
3268 }
3269 traits_type::move(__p + __pos, __s, __n2);
3270 return __null_terminate_at(__p, newsz: __sz + (__n2 - __n1));
3271}
3272
3273template <class _CharT, class _Traits, class _Allocator>
3274_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
3275basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c) {
3276 size_type __sz = size();
3277 if (__pos > __sz)
3278 this->__throw_out_of_range();
3279 __n1 = std::min(__n1, __sz - __pos);
3280 size_type __cap = capacity();
3281 value_type* __p;
3282 if (__cap - __sz + __n1 >= __n2) {
3283 __p = __data();
3284 if (__n1 != __n2) {
3285 if (__n2 > __n1)
3286 __annotate_increase(n: __n2 - __n1);
3287 size_type __n_move = __sz - __pos - __n1;
3288 if (__n_move != 0)
3289 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3290 }
3291 } else {
3292 __grow_by_without_replace(old_cap: __cap, delta_cap: __sz - __n1 + __n2 - __cap, old_sz: __sz, n_copy: __pos, n_del: __n1, n_add: __n2);
3293 __p = std::__to_address(__get_long_pointer());
3294 }
3295 traits_type::assign(__p + __pos, __n2, __c);
3296 return __null_terminate_at(__p, newsz: __sz - (__n1 - __n2));
3297}
3298
3299template <class _CharT, class _Traits, class _Allocator>
3300_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
3301basic_string<_CharT, _Traits, _Allocator>::replace(
3302 size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2) {
3303 size_type __str_sz = __str.size();
3304 if (__pos2 > __str_sz)
3305 this->__throw_out_of_range();
3306 return replace(__pos1, __n1, __str.data() + __pos2, std::min(__n2, __str_sz - __pos2));
3307}
3308
3309template <class _CharT, class _Traits, class _Allocator>
3310_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
3311basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s) {
3312 _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::replace received nullptr");
3313 return replace(__pos, __n1, __s, traits_type::length(__s));
3314}
3315
3316// erase
3317
3318// 'externally instantiated' erase() implementation, called when __n != npos.
3319// Does not check __pos against size()
3320template <class _CharT, class _Traits, class _Allocator>
3321_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE void
3322basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(size_type __pos, size_type __n) _NOEXCEPT {
3323 if (__n == 0)
3324 return;
3325
3326 size_type __sz = size();
3327 value_type* __p = __data();
3328 __n = std::min(__n, __sz - __pos);
3329 size_type __n_move = __sz - __pos - __n;
3330 if (__n_move != 0)
3331 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
3332 __null_terminate_at(__p, newsz: __sz - __n);
3333}
3334
3335template <class _CharT, class _Traits, class _Allocator>
3336_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
3337basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos, size_type __n) {
3338 if (__pos > size())
3339 this->__throw_out_of_range();
3340 if (__n == npos) {
3341 __erase_to_end(__pos);
3342 } else {
3343 __erase_external_with_move(__pos, __n);
3344 }
3345 return *this;
3346}
3347
3348template <class _CharT, class _Traits, class _Allocator>
3349inline _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::iterator
3350basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos) {
3351 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
3352 __pos != end(), "string::erase(iterator) called with a non-dereferenceable iterator");
3353 iterator __b = begin();
3354 size_type __r = static_cast<size_type>(__pos - __b);
3355 erase(__r, 1);
3356 return __b + static_cast<difference_type>(__r);
3357}
3358
3359template <class _CharT, class _Traits, class _Allocator>
3360inline _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::iterator
3361basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last) {
3362 _LIBCPP_ASSERT_VALID_INPUT_RANGE(__first <= __last, "string::erase(first, last) called with invalid range");
3363 iterator __b = begin();
3364 size_type __r = static_cast<size_type>(__first - __b);
3365 erase(__r, static_cast<size_type>(__last - __first));
3366 return __b + static_cast<difference_type>(__r);
3367}
3368
3369template <class _CharT, class _Traits, class _Allocator>
3370inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::pop_back() {
3371 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "string::pop_back(): string is already empty");
3372 __erase_to_end(pos: size() - 1);
3373}
3374
3375template <class _CharT, class _Traits, class _Allocator>
3376inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT {
3377 size_type __old_size;
3378 if (__is_long()) {
3379 __old_size = __get_long_size();
3380 traits_type::assign(*__get_long_pointer(), value_type());
3381 __set_long_size(s: 0);
3382 } else {
3383 __old_size = __get_short_size();
3384 traits_type::assign(*__get_short_pointer(), value_type());
3385 __set_short_size(s: 0);
3386 }
3387 __annotate_shrink(__old_size);
3388}
3389
3390template <class _CharT, class _Traits, class _Allocator>
3391_LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c) {
3392 size_type __sz = size();
3393 if (__n > __sz)
3394 append(__n - __sz, __c);
3395 else
3396 __erase_to_end(pos: __n);
3397}
3398
3399template <class _CharT, class _Traits, class _Allocator>
3400_LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __requested_capacity) {
3401 if (__requested_capacity > max_size())
3402 this->__throw_length_error();
3403
3404 // Make sure reserve(n) never shrinks. This is technically only required in C++20
3405 // and later (since P0966R1), however we provide consistent behavior in all Standard
3406 // modes because this function is instantiated in the shared library.
3407 if (__requested_capacity <= capacity())
3408 return;
3409
3410 __annotation_guard __g(*this);
3411 __long __buffer = __allocate_long_buffer(__alloc_, size(), __requested_capacity);
3412 traits_type::copy(std::__to_address(__buffer.__data_), data(), __buffer.__size_ + 1);
3413 __reset_internal_buffer(new_rep: __buffer);
3414}
3415
3416template <class _CharT, class _Traits, class _Allocator>
3417inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT {
3418 if (!__is_long())
3419 return;
3420
3421 const auto __ptr = __get_long_pointer();
3422 const auto __size = __get_long_size();
3423 const auto __cap = __get_long_cap();
3424
3425 // We're a long string and we're shrinking into the small buffer.
3426 if (__fits_in_sso(sz: __size)) {
3427 __annotation_guard __g(*this);
3428 __set_short_size(s: __size);
3429 traits_type::copy(std::__to_address(__get_short_pointer()), std::__to_address(__ptr), __size + 1);
3430 __alloc_traits::deallocate(__alloc_, __ptr, __cap);
3431 return;
3432 }
3433
3434 if (__align_allocation_size(__size) == __cap)
3435 return;
3436
3437# if _LIBCPP_HAS_EXCEPTIONS
3438 try {
3439# endif // _LIBCPP_HAS_EXCEPTIONS
3440 __annotation_guard __g(*this);
3441 __long __buffer = __allocate_long_buffer(__alloc_, __size);
3442
3443 // The Standard mandates shrink_to_fit() does not increase the capacity.
3444 // With equal capacity keep the existing buffer. This avoids extra work
3445 // due to swapping the elements.
3446 if (__buffer.__cap_ * __endian_factor - 1 >= capacity()) {
3447 __alloc_traits::deallocate(__alloc_, __buffer.__data_, __buffer.__cap_ * __endian_factor);
3448 return;
3449 }
3450
3451 traits_type::copy(std::__to_address(__buffer.__data_), std::__to_address(__get_long_pointer()), __size + 1);
3452 __reset_internal_buffer(new_rep: __buffer);
3453# if _LIBCPP_HAS_EXCEPTIONS
3454 } catch (...) {
3455 return;
3456 }
3457# endif // _LIBCPP_HAS_EXCEPTIONS
3458}
3459
3460template <class _CharT, class _Traits, class _Allocator>
3461_LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3462basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const {
3463 if (__n >= size())
3464 this->__throw_out_of_range();
3465 return (*this)[__n];
3466}
3467
3468template <class _CharT, class _Traits, class _Allocator>
3469_LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::reference
3470basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) {
3471 if (__n >= size())
3472 this->__throw_out_of_range();
3473 return (*this)[__n];
3474}
3475
3476template <class _CharT, class _Traits, class _Allocator>
3477_LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
3478basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const {
3479 size_type __sz = size();
3480 if (__pos > __sz)
3481 this->__throw_out_of_range();
3482 size_type __rlen = std::min(__n, __sz - __pos);
3483 traits_type::copy(__s, data() + __pos, __rlen);
3484 return __rlen;
3485}
3486
3487template <class _CharT, class _Traits, class _Allocator>
3488inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
3489# if _LIBCPP_STD_VER >= 14
3490 _NOEXCEPT
3491# else
3492 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v<allocator_type>)
3493# endif
3494{
3495 _LIBCPP_ASSERT_COMPATIBLE_ALLOCATOR(
3496 __alloc_traits::propagate_on_container_swap::value || __alloc_traits::is_always_equal::value ||
3497 __alloc_ == __str.__alloc_,
3498 "swapping non-equal allocators");
3499 if (!__is_long())
3500 __annotate_delete();
3501 if (this != std::addressof(__str) && !__str.__is_long())
3502 __str.__annotate_delete();
3503 std::swap(__rep_, __str.__rep_);
3504 std::__swap_allocator(__alloc_, __str.__alloc_);
3505 if (!__is_long())
3506 __annotate_new(current_size: __get_short_size());
3507 if (this != std::addressof(__str) && !__str.__is_long())
3508 __str.__annotate_new(__str.__get_short_size());
3509}
3510
3511// compare
3512
3513template <class _CharT, class _Traits, class _Allocator>
3514inline _LIBCPP_CONSTEXPR_SINCE_CXX20 int basic_string<_CharT, _Traits, _Allocator>::compare(
3515 size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const {
3516 _LIBCPP_ASSERT_NON_NULL(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
3517 size_type __sz = size();
3518 if (__pos1 > __sz || __n2 == npos)
3519 this->__throw_out_of_range();
3520 size_type __rlen = std::min(__n1, __sz - __pos1);
3521 int __r = traits_type::compare(data() + __pos1, __s, std::min(__rlen, __n2));
3522 if (__r == 0) {
3523 if (__rlen < __n2)
3524 __r = -1;
3525 else if (__rlen > __n2)
3526 __r = 1;
3527 }
3528 return __r;
3529}
3530
3531// __invariants
3532
3533template <class _CharT, class _Traits, class _Allocator>
3534inline _LIBCPP_CONSTEXPR_SINCE_CXX20 bool basic_string<_CharT, _Traits, _Allocator>::__invariants() const {
3535 if (size() > capacity())
3536 return false;
3537 if (capacity() < __min_cap - 1)
3538 return false;
3539 if (data() == nullptr)
3540 return false;
3541 if (!_Traits::eq(data()[size()], value_type()))
3542 return false;
3543 return true;
3544}
3545
3546// operator==
3547
3548template <class _CharT, class _Traits, class _Allocator>
3549inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool
3550operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3551 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {
3552 size_t __lhs_sz = __lhs.size();
3553 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(), __rhs.data(), __lhs_sz) == 0;
3554}
3555
3556template <class _CharT, class _Traits, class _Allocator>
3557inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool
3558operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3559 const _CharT* _LIBCPP_DIAGNOSE_NULLPTR __rhs) _NOEXCEPT {
3560 _LIBCPP_ASSERT_NON_NULL(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
3561
3562 using _String = basic_string<_CharT, _Traits, _Allocator>;
3563
3564 size_t __rhs_len = _Traits::length(__rhs);
3565 if (__builtin_constant_p(__rhs_len) && !_String::__fits_in_sso(__rhs_len)) {
3566 if (!__lhs.__is_long())
3567 return false;
3568 }
3569 if (__rhs_len != __lhs.size())
3570 return false;
3571 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
3572}
3573
3574# if _LIBCPP_STD_VER <= 17
3575template <class _CharT, class _Traits, class _Allocator>
3576inline _LIBCPP_HIDE_FROM_ABI bool
3577operator==(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {
3578 return __rhs == __lhs;
3579}
3580# endif // _LIBCPP_STD_VER <= 17
3581
3582# if _LIBCPP_STD_VER >= 20
3583
3584template <class _CharT, class _Traits, class _Allocator>
3585_LIBCPP_HIDE_FROM_ABI constexpr auto operator<=>(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3586 const basic_string<_CharT, _Traits, _Allocator>& __rhs) noexcept {
3587 return basic_string_view<_CharT, _Traits>(__lhs) <=> basic_string_view<_CharT, _Traits>(__rhs);
3588}
3589
3590template <class _CharT, class _Traits, class _Allocator>
3591_LIBCPP_HIDE_FROM_ABI constexpr auto
3592operator<=>(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) {
3593 return basic_string_view<_CharT, _Traits>(__lhs) <=> basic_string_view<_CharT, _Traits>(__rhs);
3594}
3595
3596# else // _LIBCPP_STD_VER >= 20
3597
3598template <class _CharT, class _Traits, class _Allocator>
3599inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3600 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {
3601 return !(__lhs == __rhs);
3602}
3603
3604template <class _CharT, class _Traits, class _Allocator>
3605inline _LIBCPP_HIDE_FROM_ABI bool
3606operator!=(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {
3607 return !(__lhs == __rhs);
3608}
3609
3610template <class _CharT, class _Traits, class _Allocator>
3611inline _LIBCPP_HIDE_FROM_ABI bool
3612operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) _NOEXCEPT {
3613 return !(__lhs == __rhs);
3614}
3615
3616// operator<
3617
3618template <class _CharT, class _Traits, class _Allocator>
3619inline _LIBCPP_HIDE_FROM_ABI bool operator<(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3620 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {
3621 return __lhs.compare(__rhs) < 0;
3622}
3623
3624template <class _CharT, class _Traits, class _Allocator>
3625inline _LIBCPP_HIDE_FROM_ABI bool
3626operator<(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) _NOEXCEPT {
3627 return __lhs.compare(__rhs) < 0;
3628}
3629
3630template <class _CharT, class _Traits, class _Allocator>
3631inline _LIBCPP_HIDE_FROM_ABI bool
3632operator<(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {
3633 return __rhs.compare(__lhs) > 0;
3634}
3635
3636// operator>
3637
3638template <class _CharT, class _Traits, class _Allocator>
3639inline _LIBCPP_HIDE_FROM_ABI bool operator>(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3640 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {
3641 return __rhs < __lhs;
3642}
3643
3644template <class _CharT, class _Traits, class _Allocator>
3645inline _LIBCPP_HIDE_FROM_ABI bool
3646operator>(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) _NOEXCEPT {
3647 return __rhs < __lhs;
3648}
3649
3650template <class _CharT, class _Traits, class _Allocator>
3651inline _LIBCPP_HIDE_FROM_ABI bool
3652operator>(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {
3653 return __rhs < __lhs;
3654}
3655
3656// operator<=
3657
3658template <class _CharT, class _Traits, class _Allocator>
3659inline _LIBCPP_HIDE_FROM_ABI bool operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3660 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {
3661 return !(__rhs < __lhs);
3662}
3663
3664template <class _CharT, class _Traits, class _Allocator>
3665inline _LIBCPP_HIDE_FROM_ABI bool
3666operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) _NOEXCEPT {
3667 return !(__rhs < __lhs);
3668}
3669
3670template <class _CharT, class _Traits, class _Allocator>
3671inline _LIBCPP_HIDE_FROM_ABI bool
3672operator<=(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {
3673 return !(__rhs < __lhs);
3674}
3675
3676// operator>=
3677
3678template <class _CharT, class _Traits, class _Allocator>
3679inline _LIBCPP_HIDE_FROM_ABI bool operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3680 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {
3681 return !(__lhs < __rhs);
3682}
3683
3684template <class _CharT, class _Traits, class _Allocator>
3685inline _LIBCPP_HIDE_FROM_ABI bool
3686operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) _NOEXCEPT {
3687 return !(__lhs < __rhs);
3688}
3689
3690template <class _CharT, class _Traits, class _Allocator>
3691inline _LIBCPP_HIDE_FROM_ABI bool
3692operator>=(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {
3693 return !(__lhs < __rhs);
3694}
3695# endif // _LIBCPP_STD_VER >= 20
3696
3697// operator +
3698
3699template <class _CharT, class _Traits, class _Allocator>
3700_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
3701__concatenate_strings(const _Allocator& __alloc,
3702 __type_identity_t<basic_string_view<_CharT, _Traits> > __str1,
3703 __type_identity_t<basic_string_view<_CharT, _Traits> > __str2) {
3704 using _String = basic_string<_CharT, _Traits, _Allocator>;
3705 _String __r(__uninitialized_size_tag(),
3706 __str1.size() + __str2.size(),
3707 _String::__alloc_traits::select_on_container_copy_construction(__alloc));
3708 auto __ptr = __r.__data();
3709 _Traits::copy(__ptr, __str1.data(), __str1.size());
3710 _Traits::copy(__ptr + __str1.size(), __str2.data(), __str2.size());
3711 _Traits::assign(__ptr[__str1.size() + __str2.size()], _CharT());
3712 return __r;
3713}
3714
3715template <class _CharT, class _Traits, class _Allocator>
3716_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
3717operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3718 const basic_string<_CharT, _Traits, _Allocator>& __rhs) {
3719 return std::__concatenate_strings<_CharT, _Traits>(__lhs.get_allocator(), __lhs, __rhs);
3720}
3721
3722template <class _CharT, class _Traits, class _Allocator>
3723_LIBCPP_HIDDEN _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
3724operator+(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) {
3725 return std::__concatenate_strings<_CharT, _Traits>(__rhs.get_allocator(), __lhs, __rhs);
3726}
3727
3728extern template _LIBCPP_EXPORTED_FROM_ABI string operator+
3729 <char, char_traits<char>, allocator<char> >(char const*, string const&);
3730
3731template <class _CharT, class _Traits, class _Allocator>
3732_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
3733operator+(_CharT __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) {
3734 return std::__concatenate_strings<_CharT, _Traits>(
3735 __rhs.get_allocator(), basic_string_view<_CharT, _Traits>(std::addressof(__lhs), 1), __rhs);
3736}
3737
3738template <class _CharT, class _Traits, class _Allocator>
3739_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
3740operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) {
3741 return std::__concatenate_strings<_CharT, _Traits>(__lhs.get_allocator(), __lhs, __rhs);
3742}
3743
3744template <class _CharT, class _Traits, class _Allocator>
3745_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
3746operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs) {
3747 return std::__concatenate_strings<_CharT, _Traits>(
3748 __lhs.get_allocator(), __lhs, basic_string_view<_CharT, _Traits>(std::addressof(__rhs), 1));
3749}
3750# if _LIBCPP_STD_VER >= 26
3751
3752template <class _CharT, class _Traits, class _Allocator>
3753_LIBCPP_HIDE_FROM_ABI constexpr basic_string<_CharT, _Traits, _Allocator>
3754operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3755 type_identity_t<basic_string_view<_CharT, _Traits>> __rhs) {
3756 return std::__concatenate_strings<_CharT, _Traits>(__lhs.get_allocator(), __lhs, __rhs);
3757}
3758
3759template <class _CharT, class _Traits, class _Allocator>
3760_LIBCPP_HIDE_FROM_ABI constexpr basic_string<_CharT, _Traits, _Allocator>
3761operator+(type_identity_t<basic_string_view<_CharT, _Traits>> __lhs,
3762 const basic_string<_CharT, _Traits, _Allocator>& __rhs) {
3763 return std::__concatenate_strings<_CharT, _Traits>(__rhs.get_allocator(), __lhs, __rhs);
3764}
3765
3766# endif // _LIBCPP_STD_VER >= 26
3767
3768# ifndef _LIBCPP_CXX03_LANG
3769
3770template <class _CharT, class _Traits, class _Allocator>
3771inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
3772operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) {
3773 return std::move(__lhs.append(__rhs));
3774}
3775
3776template <class _CharT, class _Traits, class _Allocator>
3777inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
3778operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs) {
3779 return std::move(__rhs.insert(0, __lhs));
3780}
3781
3782template <class _CharT, class _Traits, class _Allocator>
3783inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
3784operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs) {
3785 return std::move(__lhs.append(__rhs));
3786}
3787
3788template <class _CharT, class _Traits, class _Allocator>
3789inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
3790operator+(const _CharT* __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs) {
3791 return std::move(__rhs.insert(0, __lhs));
3792}
3793
3794template <class _CharT, class _Traits, class _Allocator>
3795inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
3796operator+(_CharT __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs) {
3797 __rhs.insert(__rhs.begin(), __lhs);
3798 return std::move(__rhs);
3799}
3800
3801template <class _CharT, class _Traits, class _Allocator>
3802inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
3803operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs) {
3804 return std::move(__lhs.append(__rhs));
3805}
3806
3807template <class _CharT, class _Traits, class _Allocator>
3808inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
3809operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs) {
3810 __lhs.push_back(__rhs);
3811 return std::move(__lhs);
3812}
3813
3814# endif // _LIBCPP_CXX03_LANG
3815
3816# if _LIBCPP_STD_VER >= 26
3817
3818template <class _CharT, class _Traits, class _Allocator>
3819_LIBCPP_HIDE_FROM_ABI constexpr basic_string<_CharT, _Traits, _Allocator>
3820operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs,
3821 type_identity_t<basic_string_view<_CharT, _Traits>> __rhs) {
3822 return std::move(__lhs.append(__rhs));
3823}
3824
3825template <class _CharT, class _Traits, class _Allocator>
3826_LIBCPP_HIDE_FROM_ABI constexpr basic_string<_CharT, _Traits, _Allocator>
3827operator+(type_identity_t<basic_string_view<_CharT, _Traits>> __lhs,
3828 basic_string<_CharT, _Traits, _Allocator>&& __rhs) {
3829 return std::move(__rhs.insert(0, __lhs));
3830}
3831
3832# endif // _LIBCPP_STD_VER >= 26
3833
3834// swap
3835
3836template <class _CharT, class _Traits, class _Allocator>
3837inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
3838swap(basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>& __rhs)
3839 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs))) {
3840 __lhs.swap(__rhs);
3841}
3842
3843[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI int stoi(const string& __str, size_t* __idx = nullptr, int __base = 10);
3844[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI unsigned long
3845stoul(const string& __str, size_t* __idx = nullptr, int __base = 10);
3846[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI long stol(const string& __str, size_t* __idx = nullptr, int __base = 10);
3847[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI long long
3848stoll(const string& __str, size_t* __idx = nullptr, int __base = 10);
3849[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI unsigned long long
3850stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);
3851
3852[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI float stof(const string& __str, size_t* __idx = nullptr);
3853[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI double stod(const string& __str, size_t* __idx = nullptr);
3854[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI long double stold(const string& __str, size_t* __idx = nullptr);
3855
3856[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI string to_string(int __val);
3857[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI string to_string(unsigned __val);
3858[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI string to_string(long __val);
3859[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI string to_string(unsigned long __val);
3860[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI string to_string(long long __val);
3861[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI string to_string(unsigned long long __val);
3862[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI string to_string(float __val);
3863[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI string to_string(double __val);
3864[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI string to_string(long double __val);
3865
3866# if _LIBCPP_HAS_WIDE_CHARACTERS
3867[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI int stoi(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
3868[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI long stol(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
3869[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI unsigned long
3870stoul(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
3871[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI long long
3872stoll(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
3873[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI unsigned long long
3874stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
3875
3876[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI float stof(const wstring& __str, size_t* __idx = nullptr);
3877[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI double stod(const wstring& __str, size_t* __idx = nullptr);
3878[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI long double stold(const wstring& __str, size_t* __idx = nullptr);
3879
3880[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(int __val);
3881[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(unsigned __val);
3882[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(long __val);
3883[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(unsigned long __val);
3884[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(long long __val);
3885[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(unsigned long long __val);
3886[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(float __val);
3887[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(double __val);
3888[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(long double __val);
3889# endif // _LIBCPP_HAS_WIDE_CHARACTERS
3890
3891template <class _CharT, class _Traits, class _Allocator>
3892_LIBCPP_TEMPLATE_DATA_VIS const typename basic_string<_CharT, _Traits, _Allocator>::size_type
3893 basic_string<_CharT, _Traits, _Allocator>::npos;
3894
3895template <class _CharT, class _Allocator>
3896struct __string_hash : public __unary_function<basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t> {
3897 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI size_t
3898 operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT {
3899 return std::__do_string_hash(__val.data(), __val.data() + __val.size());
3900 }
3901};
3902
3903template <class _Allocator>
3904struct hash<basic_string<char, char_traits<char>, _Allocator> > : __string_hash<char, _Allocator> {};
3905
3906# if _LIBCPP_HAS_CHAR8_T
3907template <class _Allocator>
3908struct hash<basic_string<char8_t, char_traits<char8_t>, _Allocator> > : __string_hash<char8_t, _Allocator> {};
3909# endif
3910
3911template <class _Allocator>
3912struct hash<basic_string<char16_t, char_traits<char16_t>, _Allocator> > : __string_hash<char16_t, _Allocator> {};
3913
3914template <class _Allocator>
3915struct hash<basic_string<char32_t, char_traits<char32_t>, _Allocator> > : __string_hash<char32_t, _Allocator> {};
3916
3917# if _LIBCPP_HAS_WIDE_CHARACTERS
3918template <class _Allocator>
3919struct hash<basic_string<wchar_t, char_traits<wchar_t>, _Allocator> > : __string_hash<wchar_t, _Allocator> {};
3920# endif
3921
3922template <class _CharT, class _Traits, class _Allocator>
3923_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
3924operator<<(basic_ostream<_CharT, _Traits>& __os, const basic_string<_CharT, _Traits, _Allocator>& __str);
3925
3926template <class _CharT, class _Traits, class _Allocator>
3927_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
3928operator>>(basic_istream<_CharT, _Traits>& __is, basic_string<_CharT, _Traits, _Allocator>& __str);
3929
3930template <class _CharT, class _Traits, class _Allocator>
3931_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
3932getline(basic_istream<_CharT, _Traits>& __is, basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
3933
3934template <class _CharT, class _Traits, class _Allocator>
3935inline _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
3936getline(basic_istream<_CharT, _Traits>& __is, basic_string<_CharT, _Traits, _Allocator>& __str);
3937
3938template <class _CharT, class _Traits, class _Allocator>
3939inline _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
3940getline(basic_istream<_CharT, _Traits>&& __is, basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
3941
3942template <class _CharT, class _Traits, class _Allocator>
3943inline _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
3944getline(basic_istream<_CharT, _Traits>&& __is, basic_string<_CharT, _Traits, _Allocator>& __str);
3945
3946# if _LIBCPP_STD_VER >= 20
3947template <class _CharT, class _Traits, class _Allocator, class _Up>
3948inline _LIBCPP_HIDE_FROM_ABI constexpr typename basic_string<_CharT, _Traits, _Allocator>::size_type
3949erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
3950 auto __old_size = __str.size();
3951 __str.erase(std::remove(__str.begin(), __str.end(), __v), __str.end());
3952 return __old_size - __str.size();
3953}
3954
3955template <class _CharT, class _Traits, class _Allocator, class _Predicate>
3956inline _LIBCPP_HIDE_FROM_ABI constexpr typename basic_string<_CharT, _Traits, _Allocator>::size_type
3957erase_if(basic_string<_CharT, _Traits, _Allocator>& __str, _Predicate __pred) {
3958 auto __old_size = __str.size();
3959 __str.erase(std::remove_if(__str.begin(), __str.end(), __pred), __str.end());
3960 return __old_size - __str.size();
3961}
3962# endif
3963
3964# if _LIBCPP_STD_VER >= 14
3965// Literal suffixes for basic_string [basic.string.literals]
3966inline namespace literals {
3967inline namespace string_literals {
3968[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<char>
3969operator""s(const char* __str, size_t __len) {
3970 return basic_string<char>(__str, __len);
3971}
3972
3973# if _LIBCPP_HAS_WIDE_CHARACTERS
3974[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI
3975_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<wchar_t> operator""s(const wchar_t* __str, size_t __len) {
3976 return basic_string<wchar_t>(__str, __len);
3977}
3978# endif
3979
3980# if _LIBCPP_HAS_CHAR8_T
3981[[__nodiscard__]] inline
3982 _LIBCPP_HIDE_FROM_ABI constexpr basic_string<char8_t> operator""s(const char8_t* __str, size_t __len) {
3983 return basic_string<char8_t>(__str, __len);
3984}
3985# endif
3986
3987[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI
3988_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<char16_t> operator""s(const char16_t* __str, size_t __len) {
3989 return basic_string<char16_t>(__str, __len);
3990}
3991
3992[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<char32_t>
3993operator""s(const char32_t* __str, size_t __len) {
3994 return basic_string<char32_t>(__str, __len);
3995}
3996} // namespace string_literals
3997} // namespace literals
3998
3999# if _LIBCPP_STD_VER >= 20
4000template <>
4001inline constexpr bool __format::__enable_insertable<std::basic_string<char>> = true;
4002# if _LIBCPP_HAS_WIDE_CHARACTERS
4003template <>
4004inline constexpr bool __format::__enable_insertable<std::basic_string<wchar_t>> = true;
4005# endif
4006# endif
4007
4008# endif
4009
4010_LIBCPP_END_NAMESPACE_STD
4011
4012_LIBCPP_POP_MACROS
4013
4014# if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
4015# include <algorithm>
4016# include <concepts>
4017# include <cstdlib>
4018# include <iterator>
4019# include <new>
4020# include <optional>
4021# include <type_traits>
4022# include <typeinfo>
4023# include <utility>
4024# endif
4025#endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
4026
4027#endif // _LIBCPP_STRING
4028