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