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