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