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