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 template <bool __is_short>
2377 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string& __assign_no_alias(const value_type* __s, size_type __n);
2378
2379 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __erase_to_end(size_type __pos) {
2380 _LIBCPP_ASSERT_INTERNAL(__pos <= capacity(), "Trying to erase at position outside the strings capacity!");
2381 __null_terminate_at(p: __data(), newsz: __pos);
2382 }
2383
2384 // __erase_external_with_move is invoked for erase() invocations where
2385 // `n ~= npos`, likely requiring memory moves on the string data.
2386 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE void
2387 __erase_external_with_move(size_type __pos, size_type __n) _NOEXCEPT;
2388
2389 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __copy_assign_alloc(const basic_string& __str) {
2390 __copy_assign_alloc(
2391 __str, integral_constant<bool, __alloc_traits::propagate_on_container_copy_assignment::value>());
2392 }
2393
2394 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __copy_assign_alloc(const basic_string& __str, true_type) {
2395 if (__alloc_ == __str.__alloc_)
2396 __alloc_ = __str.__alloc_;
2397 else {
2398 if (!__str.__is_long()) {
2399 __reset_internal_buffer();
2400 __alloc_ = __str.__alloc_;
2401 } else {
2402 __annotate_delete();
2403 auto __guard = std::__make_scope_guard(__annotate_new_size(*this));
2404 auto __alloc = __str.__alloc_;
2405 __reset_internal_buffer(new_rep: __allocate_long_buffer(__alloc, __str.size()));
2406 __alloc_ = std::move(__alloc);
2407 }
2408 }
2409 }
2410
2411 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
2412 __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT {}
2413
2414# ifndef _LIBCPP_CXX03_LANG
2415 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
2416 __move_assign(basic_string& __str, false_type) noexcept(__alloc_traits::is_always_equal::value);
2417 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __move_assign(basic_string& __str, true_type)
2418# if _LIBCPP_STD_VER >= 17
2419 noexcept;
2420# else
2421 noexcept(is_nothrow_move_assignable<allocator_type>::value);
2422# endif
2423# endif
2424
2425 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __move_assign_alloc(basic_string& __str)
2426 _NOEXCEPT_(!__alloc_traits::propagate_on_container_move_assignment::value ||
2427 is_nothrow_move_assignable<allocator_type>::value) {
2428 __move_assign_alloc(
2429 __str, integral_constant<bool, __alloc_traits::propagate_on_container_move_assignment::value>());
2430 }
2431
2432 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __move_assign_alloc(basic_string& __c, true_type)
2433 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value) {
2434 __alloc_ = std::move(__c.__alloc_);
2435 }
2436
2437 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __move_assign_alloc(basic_string&, false_type) _NOEXCEPT {}
2438
2439 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string& __assign_external(const value_type* __s);
2440 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string& __assign_external(const value_type* __s, size_type __n);
2441
2442 // Assigns the value in __s, guaranteed to be __n < __min_cap in length.
2443 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& __assign_short(const value_type* __s, size_type __n) {
2444 size_type __old_size = size();
2445 if (__n > __old_size)
2446 __annotate_increase(n: __n - __old_size);
2447 pointer __p;
2448 if (__is_long()) {
2449 __set_long_size(s: __n);
2450 __p = __get_long_pointer();
2451 } else {
2452 __set_short_size(s: __n);
2453 __p = __get_short_pointer();
2454 }
2455 traits_type::move(std::__to_address(__p), __s, __n);
2456 traits_type::assign(__p[__n], value_type());
2457 if (__old_size > __n)
2458 __annotate_shrink(__old_size);
2459 return *this;
2460 }
2461
2462 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
2463 __null_terminate_at(value_type* __p, size_type __newsz) {
2464 size_type __old_size = size();
2465 if (__newsz > __old_size)
2466 __annotate_increase(n: __newsz - __old_size);
2467 __set_size(s: __newsz);
2468 traits_type::assign(__p[__newsz], value_type());
2469 if (__old_size > __newsz)
2470 __annotate_shrink(__old_size);
2471 return *this;
2472 }
2473
2474 template <class _Tp>
2475 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __addr_in_range(const _Tp& __v) const {
2476 return std::__is_pointer_in_range(data(), data() + size() + 1, std::addressof(__v));
2477 }
2478
2479 [[__noreturn__]] _LIBCPP_HIDE_FROM_ABI static void __throw_length_error() {
2480 std::__throw_length_error(msg: "basic_string");
2481 }
2482
2483 [[__noreturn__]] _LIBCPP_HIDE_FROM_ABI static void __throw_out_of_range() {
2484 std::__throw_out_of_range(msg: "basic_string");
2485 }
2486
2487 friend _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string
2488 __concatenate_strings<>(const _Allocator&, __type_identity_t<__self_view>, __type_identity_t<__self_view>);
2489
2490 template <class _CharT2, class _Traits2, class _Allocator2>
2491 friend inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool
2492 operator==(const basic_string<_CharT2, _Traits2, _Allocator2>&, const _CharT2*) _NOEXCEPT;
2493
2494 // These functions aren't used anymore but are part of out ABI, so we need to provide them in the dylib for backwards
2495 // compatibility
2496# ifdef _LIBCPP_BUILDING_LIBRARY
2497 void __init(const value_type* __s, size_type __sz, size_type __reserve);
2498# endif
2499};
2500
2501// These declarations must appear before any functions are implicitly used
2502// so that they have the correct visibility specifier.
2503# define _LIBCPP_DECLARE(...) extern template _LIBCPP_EXPORTED_FROM_ABI __VA_ARGS__;
2504# ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
2505_LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, char)
2506# if _LIBCPP_HAS_WIDE_CHARACTERS
2507_LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, wchar_t)
2508# endif
2509# else
2510_LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, char)
2511# if _LIBCPP_HAS_WIDE_CHARACTERS
2512_LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, wchar_t)
2513# endif
2514# endif
2515# undef _LIBCPP_DECLARE
2516
2517# if _LIBCPP_STD_VER <= 17 || !__has_builtin(__builtin_lt_synthesizes_from_spaceship)
2518template <class _CharT, class _Traits, class _Alloc>
2519struct __default_three_way_comparator<basic_string<_CharT, _Traits, _Alloc>, basic_string<_CharT, _Traits, _Alloc> > {
2520 using __string_t _LIBCPP_NODEBUG = basic_string<_CharT, _Traits, _Alloc>;
2521
2522 _LIBCPP_HIDE_FROM_ABI static int operator()(const __string_t& __lhs, const __string_t& __rhs) {
2523 auto __min_len = std::min(__lhs.size(), __rhs.size());
2524 auto __ret = _Traits::compare(__lhs.data(), __rhs.data(), __min_len);
2525 if (__ret == 0)
2526 return __lhs.size() == __rhs.size() ? 0 : __lhs.size() < __rhs.size() ? -1 : 1;
2527 return __ret;
2528 }
2529};
2530# endif
2531
2532template <class _Comparator, class _CharT2, class _CharT, class _Traits, class _Alloc>
2533inline const bool __is_transparently_comparable_v<_Comparator, basic_string<_CharT, _Traits, _Alloc>, _CharT2*> =
2534 is_same<_CharT, __remove_cv_t<_CharT2> >::value &&
2535 (__desugars_to_v<__less_tag,
2536 _Comparator,
2537 basic_string<_CharT, _Traits, _Alloc>,
2538 basic_string<_CharT, _Traits, _Alloc> > ||
2539 __desugars_to_v<__greater_tag,
2540 _Comparator,
2541 basic_string<_CharT, _Traits, _Alloc>,
2542 basic_string<_CharT, _Traits, _Alloc> >);
2543
2544template <class _Comparator, class _CharT2, class _CharT, class _Traits, class _Alloc>
2545inline const bool __is_transparently_comparable_v<_Comparator, _CharT2*, basic_string<_CharT, _Traits, _Alloc> > =
2546 __is_transparently_comparable_v<_Comparator, basic_string<_CharT, _Traits, _Alloc>, _CharT2*>;
2547
2548template <class _Comparator, class _CharT, class _Traits, class _Alloc, size_t _Np>
2549inline const bool __is_transparently_comparable_v<_Comparator, basic_string<_CharT, _Traits, _Alloc>, _CharT[_Np]> =
2550 __is_transparently_comparable_v<_Comparator, basic_string<_CharT, _Traits, _Alloc>, const _CharT*>;
2551
2552template <class _Comparator, class _CharT, class _Traits, class _Alloc, size_t _Np>
2553inline const bool __is_transparently_comparable_v<_Comparator, _CharT[_Np], basic_string<_CharT, _Traits, _Alloc> > =
2554 __is_transparently_comparable_v<_Comparator, basic_string<_CharT, _Traits, _Alloc>, const _CharT*>;
2555
2556# if _LIBCPP_STD_VER >= 17
2557template <class _InputIterator,
2558 class _CharT = __iterator_value_type<_InputIterator>,
2559 class _Allocator = allocator<_CharT>,
2560 class = enable_if_t<__has_input_iterator_category<_InputIterator>::value>,
2561 class = enable_if_t<__is_allocator_v<_Allocator>>>
2562basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
2563 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
2564
2565template <class _CharT,
2566 class _Traits,
2567 class _Allocator = allocator<_CharT>,
2568 class = enable_if_t<__is_allocator_v<_Allocator>>>
2569explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
2570 -> basic_string<_CharT, _Traits, _Allocator>;
2571
2572template <class _CharT,
2573 class _Traits,
2574 class _Allocator = allocator<_CharT>,
2575 class = enable_if_t<__is_allocator_v<_Allocator>>,
2576 class _Sz = typename allocator_traits<_Allocator>::size_type >
2577basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
2578 -> basic_string<_CharT, _Traits, _Allocator>;
2579# endif
2580
2581# if _LIBCPP_STD_VER >= 23
2582template <ranges::input_range _Range,
2583 class _Allocator = allocator<ranges::range_value_t<_Range>>,
2584 class = enable_if_t<__is_allocator_v<_Allocator>>>
2585basic_string(from_range_t, _Range&&, _Allocator = _Allocator())
2586 -> basic_string<ranges::range_value_t<_Range>, char_traits<ranges::range_value_t<_Range>>, _Allocator>;
2587# endif
2588
2589template <class _CharT, class _Traits, class _Allocator>
2590_LIBCPP_CONSTEXPR_SINCE_CXX20 void
2591basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz) {
2592 pointer __p = __init_internal_buffer(size: __sz);
2593 traits_type::copy(std::__to_address(__p), __s, __sz);
2594 traits_type::assign(__p[__sz], value_type());
2595}
2596
2597template <class _CharT, class _Traits, class _Allocator>
2598_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE void
2599basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(const value_type* __s, size_type __sz) {
2600 if _LIBCPP_CONSTEXPR (__alloc_traits::is_always_equal::value)
2601 std::__assume(cond: __sz <= max_size());
2602 pointer __p = __init_internal_buffer(size: __sz);
2603 traits_type::copy(std::__to_address(__p), __s, __sz + 1);
2604}
2605
2606template <class _CharT, class _Traits, class _Allocator>
2607_LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c) {
2608 pointer __p = __init_internal_buffer(size: __n);
2609 traits_type::assign(std::__to_address(__p), __n, __c);
2610 traits_type::assign(__p[__n], value_type());
2611}
2612
2613template <class _CharT, class _Traits, class _Allocator>
2614template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> >
2615_LIBCPP_CONSTEXPR_SINCE_CXX20 void
2616basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last) {
2617 __init_with_sentinel(std::move(__first), std::move(__last));
2618}
2619
2620template <class _CharT, class _Traits, class _Allocator>
2621template <class _InputIterator, class _Sentinel>
2622_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
2623basic_string<_CharT, _Traits, _Allocator>::__init_with_sentinel(_InputIterator __first, _Sentinel __last) {
2624 __rep_ = __rep();
2625 __annotate_new(current_size: 0);
2626
2627 auto __guard = std::__make_exception_guard([this] { __reset_internal_buffer(); });
2628 for (; __first != __last; ++__first)
2629 push_back(c: *__first);
2630 __guard.__complete();
2631}
2632
2633template <class _CharT, class _Traits, class _Allocator>
2634template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> >
2635_LIBCPP_CONSTEXPR_SINCE_CXX20 void
2636basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last) {
2637 size_type __sz = static_cast<size_type>(std::distance(__first, __last));
2638 __init_with_size(__first, __last, __sz);
2639}
2640
2641template <class _CharT, class _Traits, class _Allocator>
2642template <class _InputIterator, class _Sentinel>
2643_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
2644basic_string<_CharT, _Traits, _Allocator>::__init_with_size(_InputIterator __first, _Sentinel __last, size_type __sz) {
2645 pointer __p = __init_internal_buffer(size: __sz);
2646
2647 auto __guard = std::__make_exception_guard([this] { __reset_internal_buffer(); });
2648 auto __end = __copy_non_overlapping_range(std::move(__first), std::move(__last), std::__to_address(__p));
2649 traits_type::assign(*__end, value_type());
2650 __guard.__complete();
2651}
2652
2653template <class _CharT, class _Traits, class _Allocator>
2654_LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace(
2655 size_type __old_cap,
2656 size_type __delta_cap,
2657 size_type __old_sz,
2658 size_type __n_copy,
2659 size_type __n_del,
2660 size_type __n_add,
2661 const value_type* __p_new_stuff) {
2662 __long __buffer = __allocate_long_buffer(__alloc_, 0, __get_amortized_growth_capacity(required_capacity: __old_cap + __delta_cap));
2663 value_type* __old_p = __data();
2664 __annotate_delete();
2665 auto __guard = std::__make_scope_guard(__annotate_new_size(*this));
2666 if (__n_copy != 0)
2667 traits_type::copy(std::__to_address(__buffer.__data_), __old_p, __n_copy);
2668 if (__n_add != 0)
2669 traits_type::copy(std::__to_address(__buffer.__data_) + __n_copy, __p_new_stuff, __n_add);
2670 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2671 if (__sec_cp_sz != 0)
2672 traits_type::copy(
2673 std::__to_address(__buffer.__data_) + __n_copy + __n_add, __old_p + __n_copy + __n_del, __sec_cp_sz);
2674 __buffer.__size_ = __n_copy + __n_add + __sec_cp_sz;
2675 traits_type::assign(__buffer.__data_[__buffer.__size_], value_type());
2676 __reset_internal_buffer(new_rep: __buffer);
2677}
2678
2679// __grow_by is deprecated because it does not set the size. It may not update the size when the size is changed, and it
2680// may also not set the size at all when the string was short initially. This leads to unpredictable size value. It is
2681// not removed or changed to avoid breaking the ABI.
2682template <class _CharT, class _Traits, class _Allocator>
2683void _LIBCPP_CONSTEXPR_SINCE_CXX20
2684# if _LIBCPP_ABI_VERSION >= 2 // We want to use the function in the dylib in ABIv1
2685_LIBCPP_HIDE_FROM_ABI
2686# endif
2687basic_string<_CharT, _Traits, _Allocator>::__grow_by(
2688 size_type __old_cap,
2689 size_type __delta_cap,
2690 size_type __old_sz,
2691 size_type __n_copy,
2692 size_type __n_del,
2693 size_type __n_add) {
2694 __long __buffer = __allocate_long_buffer(__alloc_, 0, __get_amortized_growth_capacity(required_capacity: __old_cap + __delta_cap));
2695 value_type* __old_p = __data();
2696 if (__n_copy != 0)
2697 traits_type::copy(std::__to_address(__buffer.__data_), __old_p, __n_copy);
2698 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2699 if (__sec_cp_sz != 0)
2700 traits_type::copy(
2701 std::__to_address(__buffer.__data_) + __n_copy + __n_add, __old_p + __n_copy + __n_del, __sec_cp_sz);
2702 // This is -1 to make sure the caller sets the size properly, since old versions of this function didn't set the size
2703 // at all.
2704 __buffer.__size_ = -1;
2705 __reset_internal_buffer(new_rep: __buffer);
2706}
2707
2708template <class _CharT, class _Traits, class _Allocator>
2709void _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
2710basic_string<_CharT, _Traits, _Allocator>::__grow_by_without_replace(
2711 size_type __old_cap,
2712 size_type __delta_cap,
2713 size_type __old_sz,
2714 size_type __n_copy,
2715 size_type __n_del,
2716 size_type __n_add) {
2717 __annotate_delete();
2718 auto __guard = std::__make_scope_guard(__annotate_new_size(*this));
2719 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
2720 __grow_by(__old_cap, __delta_cap, __old_sz, __n_copy, __n_del, __n_add);
2721 _LIBCPP_SUPPRESS_DEPRECATED_POP
2722 // Due to the ABI of __grow_by we have to set the size after calling it.
2723 __set_long_size(s: __old_sz - __n_del + __n_add);
2724}
2725
2726// assign
2727
2728template <class _CharT, class _Traits, class _Allocator>
2729template <bool __is_short>
2730_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string<_CharT, _Traits, _Allocator>&
2731basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(const value_type* __s, size_type __n) {
2732 const auto __cap = __is_short ? static_cast<size_type>(__min_cap) : __get_long_cap();
2733 const auto __size = __is_short ? __get_short_size() : __get_long_size();
2734 if (__n >= __cap) {
2735 __grow_by_and_replace(old_cap: __cap - 1, delta_cap: __n - __cap + 1, old_sz: __size, n_copy: 0, n_del: __size, n_add: __n, p_new_stuff: __s);
2736 return *this;
2737 }
2738
2739 __annotate_delete();
2740 auto __guard = std::__make_scope_guard(__annotate_new_size(*this));
2741 pointer __p;
2742 if (__is_short) {
2743 __p = __get_short_pointer();
2744 __set_short_size(s: __n);
2745 } else {
2746 __p = __get_long_pointer();
2747 __set_long_size(s: __n);
2748 }
2749 traits_type::copy(std::__to_address(__p), __s, __n);
2750 traits_type::assign(__p[__n], value_type());
2751 return *this;
2752}
2753
2754template <class _CharT, class _Traits, class _Allocator>
2755_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string<_CharT, _Traits, _Allocator>&
2756basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s, size_type __n) {
2757 const auto __cap = capacity();
2758 const auto __size = size();
2759 if (__cap >= __n) {
2760 if (__n > __size)
2761 __annotate_increase(n: __n - __size);
2762 value_type* __p = __data();
2763 traits_type::move(__p, __s, __n);
2764 return __null_terminate_at(__p, newsz: __n);
2765 } else {
2766 __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);
2767 return *this;
2768 }
2769}
2770
2771template <class _CharT, class _Traits, class _Allocator>
2772_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
2773basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n) {
2774 _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string::assign received nullptr");
2775 return (__builtin_constant_p(__n) && __fits_in_sso(sz: __n)) ? __assign_short(__s, __n) : __assign_external(__s, __n);
2776}
2777
2778template <class _CharT, class _Traits, class _Allocator>
2779_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
2780basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __pos, size_type __n) {
2781 _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string::assign received nullptr");
2782 return assign(__self_view(__s).substr(__pos, __n));
2783}
2784
2785template <class _CharT, class _Traits, class _Allocator>
2786_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
2787basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c) {
2788 size_type __cap = capacity();
2789 size_type __old_size = size();
2790 if (__cap < __n) {
2791 __grow_by_without_replace(old_cap: __cap, delta_cap: __n - __cap, old_sz: __old_size, n_copy: 0, n_del: __old_size);
2792 __annotate_increase(__n);
2793 } else if (__n > __old_size)
2794 __annotate_increase(n: __n - __old_size);
2795 value_type* __p = __data();
2796 traits_type::assign(__p, __n, __c);
2797 return __null_terminate_at(__p, newsz: __n);
2798}
2799
2800template <class _CharT, class _Traits, class _Allocator>
2801_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
2802basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c) {
2803 size_type __old_size = size();
2804 if (__old_size == 0)
2805 __annotate_increase(n: 1);
2806 pointer __p;
2807 if (__is_long()) {
2808 __p = __get_long_pointer();
2809 __set_long_size(s: 1);
2810 } else {
2811 __p = __get_short_pointer();
2812 __set_short_size(s: 1);
2813 }
2814 traits_type::assign(*__p, __c);
2815 traits_type::assign(*++__p, value_type());
2816 if (__old_size > 1)
2817 __annotate_shrink(__old_size);
2818 return *this;
2819}
2820
2821template <class _CharT, class _Traits, class _Allocator>
2822_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
2823basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str) {
2824 if (this == std::addressof(__str))
2825 return *this;
2826
2827 __copy_assign_alloc(__str);
2828
2829 if (__is_long())
2830 return __assign_no_alias<false>(__str.data(), __str.size());
2831
2832 if (__str.__is_long())
2833 return __assign_no_alias<true>(__str.data(), __str.size());
2834
2835 __annotate_delete();
2836 auto __guard = std::__make_scope_guard(__annotate_new_size(*this));
2837 __rep_ = __str.__rep_;
2838
2839 return *this;
2840}
2841
2842# ifndef _LIBCPP_CXX03_LANG
2843
2844template <class _CharT, class _Traits, class _Allocator>
2845inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::__move_assign(
2846 basic_string& __str, false_type) noexcept(__alloc_traits::is_always_equal::value) {
2847 if (__alloc_ != __str.__alloc_)
2848 assign(__str);
2849 else
2850 __move_assign(__str, true_type());
2851}
2852
2853template <class _CharT, class _Traits, class _Allocator>
2854inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void
2855basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
2856# if _LIBCPP_STD_VER >= 17
2857 noexcept
2858# else
2859 noexcept(is_nothrow_move_assignable<allocator_type>::value)
2860# endif
2861{
2862 __annotate_delete();
2863 if (__is_long()) {
2864 __reset_internal_buffer();
2865# if _LIBCPP_STD_VER <= 14
2866 if (!is_nothrow_move_assignable<allocator_type>::value) {
2867 __set_short_size(0);
2868 traits_type::assign(__get_short_pointer()[0], value_type());
2869 __annotate_new(0);
2870 }
2871# endif
2872 }
2873 size_type __str_old_size = __str.size();
2874 bool __str_was_short = !__str.__is_long();
2875
2876 __move_assign_alloc(__str);
2877 __rep_ = __str.__rep_;
2878 __str.__set_short_size(s: 0);
2879 traits_type::assign(__str.__get_short_pointer()[0], value_type());
2880
2881 if (__str_was_short && this != std::addressof(__str))
2882 __str.__annotate_shrink(old_size: __str_old_size);
2883 else
2884 // ASan annotations: was long, so object memory is unpoisoned as new.
2885 // Or is same as *this, and __annotate_delete() was called.
2886 __str.__annotate_new(current_size: 0);
2887
2888 // ASan annotations: Guard against `std::string s; s = std::move(s);`
2889 // You can find more here: https://en.cppreference.com/w/cpp/utility/move
2890 // Quote: "Unless otherwise specified, all standard library objects that have been moved
2891 // from are placed in a "valid but unspecified state", meaning the object's class
2892 // invariants hold (so functions without preconditions, such as the assignment operator,
2893 // can be safely used on the object after it was moved from):"
2894 // Quote: "v = std::move(v); // the value of v is unspecified"
2895 if (!__is_long() && std::addressof(__str) != this)
2896 // If it is long string, delete was never called on original __str's buffer.
2897 __annotate_new(current_size: __get_short_size());
2898}
2899
2900# endif
2901
2902template <class _CharT, class _Traits, class _Allocator>
2903template <class _InputIterator, class _Sentinel>
2904_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
2905basic_string<_CharT, _Traits, _Allocator>::__assign_with_sentinel(_InputIterator __first, _Sentinel __last) {
2906 const basic_string __temp(__init_with_sentinel_tag(), std::move(__first), std::move(__last), __alloc_);
2907 assign(__temp.data(), __temp.size());
2908}
2909
2910template <class _CharT, class _Traits, class _Allocator>
2911template <class _Iterator, class _Sentinel>
2912_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
2913basic_string<_CharT, _Traits, _Allocator>::__assign_trivial(_Iterator __first, _Sentinel __last, size_type __n) {
2914 _LIBCPP_ASSERT_INTERNAL(
2915 __string_is_trivial_iterator_v<_Iterator>, "The iterator type given to `__assign_trivial` must be trivial");
2916
2917 size_type __old_size = size();
2918 size_type __cap = capacity();
2919 if (__cap < __n) {
2920 // Unlike `append` functions, if the input range points into the string itself, there is no case that the input
2921 // range could get invalidated by reallocation:
2922 // 1. If the input range is a subset of the string itself, its size cannot exceed the capacity of the string,
2923 // thus no reallocation would happen.
2924 // 2. In the exotic case where the input range is the byte representation of the string itself, the string
2925 // object itself stays valid even if reallocation happens.
2926 size_type __sz = size();
2927 __grow_by_without_replace(old_cap: __cap, delta_cap: __n - __cap, old_sz: __sz, n_copy: 0, n_del: __sz);
2928 __annotate_increase(__n);
2929 } else if (__n > __old_size)
2930 __annotate_increase(n: __n - __old_size);
2931 pointer __p = __get_pointer();
2932 for (; __first != __last; ++__p, (void)++__first)
2933 traits_type::assign(*__p, *__first);
2934 traits_type::assign(*__p, value_type());
2935 __set_size(s: __n);
2936 if (__n < __old_size)
2937 __annotate_shrink(__old_size);
2938}
2939
2940template <class _CharT, class _Traits, class _Allocator>
2941_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
2942basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n) {
2943 size_type __sz = __str.size();
2944 if (__pos > __sz)
2945 this->__throw_out_of_range();
2946 return assign(__str.data() + __pos, std::min(__n, __sz - __pos));
2947}
2948
2949template <class _CharT, class _Traits, class _Allocator>
2950_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string<_CharT, _Traits, _Allocator>&
2951basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s) {
2952 return __assign_external(__s, traits_type::length(__s));
2953}
2954
2955template <class _CharT, class _Traits, class _Allocator>
2956_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
2957basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s) {
2958 _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::assign received nullptr");
2959 if (auto __len = traits_type::length(__s); __builtin_constant_p(__len) && __fits_in_sso(sz: __len))
2960 return __assign_short(__s, n: __len);
2961 return __assign_external(__s);
2962}
2963// append
2964
2965template <class _CharT, class _Traits, class _Allocator>
2966_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
2967basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n) {
2968 _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string::append received nullptr");
2969 size_type __cap = capacity();
2970 size_type __sz = size();
2971 if (__cap - __sz < __n) {
2972 __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);
2973 return *this;
2974 }
2975
2976 if (__n == 0)
2977 return *this;
2978
2979 __annotate_increase(__n);
2980 value_type* __p = __data();
2981 traits_type::copy(__p + __sz, __s, __n);
2982 __sz += __n;
2983 __set_size(s: __sz);
2984 traits_type::assign(__p[__sz], value_type());
2985 return *this;
2986}
2987
2988template <class _CharT, class _Traits, class _Allocator>
2989_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
2990basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c) {
2991 if (__n == 0)
2992 return *this;
2993
2994 size_type __cap = capacity();
2995 size_type __sz = size();
2996 if (__cap - __sz < __n)
2997 __grow_by_without_replace(old_cap: __cap, delta_cap: __sz + __n - __cap, old_sz: __sz, n_copy: __sz, n_del: 0);
2998 __annotate_increase(__n);
2999 value_type* __p = __data();
3000 traits_type::assign(std::__to_address(__p) + __sz, __n, __c);
3001 __sz += __n;
3002 __set_size(s: __sz);
3003 traits_type::assign(__p[__sz], value_type());
3004 return *this;
3005}
3006
3007template <class _CharT, class _Traits, class _Allocator>
3008_LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c) {
3009 bool __is_short = !__is_long();
3010 size_type __cap;
3011 size_type __sz;
3012 if (__is_short) {
3013 __cap = __min_cap - 1;
3014 __sz = __get_short_size();
3015 } else {
3016 __cap = __get_long_cap() - 1;
3017 __sz = __get_long_size();
3018 }
3019 if (__sz == __cap) {
3020 __grow_by_without_replace(old_cap: __cap, delta_cap: 1, old_sz: __sz, n_copy: __sz, n_del: 0);
3021 __is_short = false; // the string is always long after __grow_by
3022 }
3023 __annotate_increase(n: 1);
3024 pointer __p;
3025 if (__is_short) {
3026 __p = __get_short_pointer() + __sz;
3027 __set_short_size(s: __sz + 1);
3028 } else {
3029 __p = __get_long_pointer() + __sz;
3030 __set_long_size(s: __sz + 1);
3031 }
3032 traits_type::assign(*__p, __c);
3033 traits_type::assign(*++__p, value_type());
3034}
3035
3036template <class _CharT, class _Traits, class _Allocator>
3037_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
3038basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n) {
3039 size_type __sz = __str.size();
3040 if (__pos > __sz)
3041 this->__throw_out_of_range();
3042 return append(__str.data() + __pos, std::min(__n, __sz - __pos));
3043}
3044
3045template <class _CharT, class _Traits, class _Allocator>
3046_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
3047basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s) {
3048 _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::append received nullptr");
3049 return append(__s, traits_type::length(__s));
3050}
3051
3052template <class _CharT, class _Traits, class _Allocator>
3053_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
3054basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __pos, size_type __n) {
3055 _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string::append received nullptr");
3056 return append(__self_view(__s).substr(__pos, __n));
3057}
3058
3059// insert
3060
3061template <class _CharT, class _Traits, class _Allocator>
3062_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
3063basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n) {
3064 _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string::insert received nullptr");
3065 size_type __sz = size();
3066 if (__pos > __sz)
3067 this->__throw_out_of_range();
3068 size_type __cap = capacity();
3069
3070 if (__cap - __sz < __n) {
3071 __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);
3072 return *this;
3073 }
3074
3075 if (__n == 0)
3076 return *this;
3077
3078 __annotate_increase(__n);
3079 value_type* __p = __data();
3080 size_type __n_move = __sz - __pos;
3081 if (__n_move != 0) {
3082 if (std::__is_pointer_in_range(__p + __pos, __p + __sz, __s))
3083 __s += __n;
3084 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
3085 }
3086 traits_type::move(__p + __pos, __s, __n);
3087 __sz += __n;
3088 __set_size(s: __sz);
3089 traits_type::assign(__p[__sz], value_type());
3090 return *this;
3091}
3092
3093template <class _CharT, class _Traits, class _Allocator>
3094_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
3095basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c) {
3096 size_type __sz = size();
3097 if (__pos > __sz)
3098 this->__throw_out_of_range();
3099
3100 if (__n == 0)
3101 return *this;
3102
3103 size_type __cap = capacity();
3104 value_type* __p;
3105 if (__cap - __sz >= __n) {
3106 __annotate_increase(__n);
3107 __p = __data();
3108 size_type __n_move = __sz - __pos;
3109 if (__n_move != 0)
3110 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
3111 } else {
3112 __grow_by_without_replace(old_cap: __cap, delta_cap: __sz + __n - __cap, old_sz: __sz, n_copy: __pos, n_del: 0, n_add: __n);
3113 __p = std::__to_address(__get_long_pointer());
3114 }
3115 traits_type::assign(__p + __pos, __n, __c);
3116 __sz += __n;
3117 __set_size(s: __sz);
3118 traits_type::assign(__p[__sz], value_type());
3119 return *this;
3120}
3121
3122template <class _CharT, class _Traits, class _Allocator>
3123template <class _Iterator, class _Sentinel>
3124_LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::iterator
3125basic_string<_CharT, _Traits, _Allocator>::__insert_with_size(
3126 const_iterator __pos, _Iterator __first, _Sentinel __last, size_type __n) {
3127 size_type __ip = static_cast<size_type>(__pos - begin());
3128 if (__n == 0)
3129 return begin() + __ip;
3130
3131 if (__string_is_trivial_iterator_v<_Iterator> && !__addr_in_range(*__first)) {
3132 return __insert_from_safe_copy(__n, __ip, std::move(__first), std::move(__last));
3133 } else {
3134 const basic_string __temp(__init_with_sentinel_tag(), std::move(__first), std::move(__last), __alloc_);
3135 return __insert_from_safe_copy(__n, __ip, __temp.begin(), __temp.end());
3136 }
3137}
3138
3139template <class _CharT, class _Traits, class _Allocator>
3140_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
3141basic_string<_CharT, _Traits, _Allocator>::insert(
3142 size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n) {
3143 size_type __str_sz = __str.size();
3144 if (__pos2 > __str_sz)
3145 this->__throw_out_of_range();
3146 return insert(__pos1, __str.data() + __pos2, std::min(__n, __str_sz - __pos2));
3147}
3148
3149template <class _CharT, class _Traits, class _Allocator>
3150_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
3151basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s) {
3152 _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::insert received nullptr");
3153 return insert(__pos, __s, traits_type::length(__s));
3154}
3155
3156template <class _CharT, class _Traits, class _Allocator>
3157_LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::iterator
3158basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c) {
3159 size_type __ip = static_cast<size_type>(__pos - begin());
3160 size_type __sz = size();
3161 size_type __cap = capacity();
3162 value_type* __p;
3163 if (__cap == __sz) {
3164 __grow_by_without_replace(old_cap: __cap, delta_cap: 1, old_sz: __sz, n_copy: __ip, n_del: 0, n_add: 1);
3165 __p = std::__to_address(__get_long_pointer());
3166 } else {
3167 __annotate_increase(n: 1);
3168 __p = __data();
3169 size_type __n_move = __sz - __ip;
3170 if (__n_move != 0)
3171 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
3172 }
3173 traits_type::assign(__p[__ip], __c);
3174 traits_type::assign(__p[++__sz], value_type());
3175 __set_size(s: __sz);
3176 return begin() + static_cast<difference_type>(__ip);
3177}
3178
3179// replace
3180
3181template <class _CharT, class _Traits, class _Allocator>
3182_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
3183basic_string<_CharT, _Traits, _Allocator>::replace(
3184 size_type __pos, size_type __n1, const value_type* __s, size_type __n2)
3185 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK {
3186 _LIBCPP_ASSERT_NON_NULL(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
3187 size_type __sz = size();
3188 if (__pos > __sz)
3189 this->__throw_out_of_range();
3190 __n1 = std::min(__n1, __sz - __pos);
3191 size_type __cap = capacity();
3192 if (__cap - __sz + __n1 < __n2) {
3193 __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);
3194 return *this;
3195 }
3196
3197 value_type* __p = __data();
3198 if (__n1 != __n2) {
3199 if (__n2 > __n1)
3200 __annotate_increase(n: __n2 - __n1);
3201 size_type __n_move = __sz - __pos - __n1;
3202 if (__n_move != 0) {
3203 if (__n1 > __n2) {
3204 traits_type::move(__p + __pos, __s, __n2);
3205 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3206 return __null_terminate_at(__p, newsz: __sz + (__n2 - __n1));
3207 }
3208 if (std::__is_pointer_in_range(__p + __pos + 1, __p + __sz, __s)) {
3209 if (__p + __pos + __n1 <= __s) {
3210 __s += __n2 - __n1;
3211 } else { // __p + __pos < __s < __p + __pos + __n1
3212 traits_type::move(__p + __pos, __s, __n1);
3213 __pos += __n1;
3214 __s += __n2;
3215 __n2 -= __n1;
3216 __n1 = 0;
3217 }
3218 }
3219 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3220 }
3221 }
3222 traits_type::move(__p + __pos, __s, __n2);
3223 return __null_terminate_at(__p, newsz: __sz + (__n2 - __n1));
3224}
3225
3226template <class _CharT, class _Traits, class _Allocator>
3227_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
3228basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c) {
3229 size_type __sz = size();
3230 if (__pos > __sz)
3231 this->__throw_out_of_range();
3232 __n1 = std::min(__n1, __sz - __pos);
3233 size_type __cap = capacity();
3234 value_type* __p;
3235 if (__cap - __sz + __n1 >= __n2) {
3236 __p = __data();
3237 if (__n1 != __n2) {
3238 if (__n2 > __n1)
3239 __annotate_increase(n: __n2 - __n1);
3240 size_type __n_move = __sz - __pos - __n1;
3241 if (__n_move != 0)
3242 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3243 }
3244 } else {
3245 __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);
3246 __p = std::__to_address(__get_long_pointer());
3247 }
3248 traits_type::assign(__p + __pos, __n2, __c);
3249 return __null_terminate_at(__p, newsz: __sz - (__n1 - __n2));
3250}
3251
3252template <class _CharT, class _Traits, class _Allocator>
3253_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
3254basic_string<_CharT, _Traits, _Allocator>::replace(
3255 size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2) {
3256 size_type __str_sz = __str.size();
3257 if (__pos2 > __str_sz)
3258 this->__throw_out_of_range();
3259 return replace(__pos1, __n1, __str.data() + __pos2, std::min(__n2, __str_sz - __pos2));
3260}
3261
3262template <class _CharT, class _Traits, class _Allocator>
3263_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
3264basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s) {
3265 _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::replace received nullptr");
3266 return replace(__pos, __n1, __s, traits_type::length(__s));
3267}
3268
3269// erase
3270
3271// 'externally instantiated' erase() implementation, called when __n != npos.
3272// Does not check __pos against size()
3273template <class _CharT, class _Traits, class _Allocator>
3274_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE void
3275basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(size_type __pos, size_type __n) _NOEXCEPT {
3276 if (__n == 0)
3277 return;
3278
3279 size_type __size = size();
3280 value_type* __p = __data();
3281 __n = std::min(__n, __size - __pos);
3282 __set_size(s: __size - __n);
3283 traits_type::move(__p + __pos, __p + __pos + __n, __size - __pos - __n + 1);
3284 __annotate_shrink(old_size: __size);
3285}
3286
3287template <class _CharT, class _Traits, class _Allocator>
3288_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
3289basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos, size_type __n) {
3290 if (__pos > size())
3291 this->__throw_out_of_range();
3292 if (__n == npos) {
3293 __erase_to_end(__pos);
3294 } else {
3295 __erase_external_with_move(__pos, __n);
3296 }
3297 return *this;
3298}
3299
3300template <class _CharT, class _Traits, class _Allocator>
3301inline _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::iterator
3302basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos) {
3303 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
3304 __pos != end(), "string::erase(iterator) called with a non-dereferenceable iterator");
3305 iterator __b = begin();
3306 size_type __r = static_cast<size_type>(__pos - __b);
3307 erase(__r, 1);
3308 return __b + static_cast<difference_type>(__r);
3309}
3310
3311template <class _CharT, class _Traits, class _Allocator>
3312inline _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::iterator
3313basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last) {
3314 _LIBCPP_ASSERT_VALID_INPUT_RANGE(__first <= __last, "string::erase(first, last) called with invalid range");
3315 iterator __b = begin();
3316 size_type __r = static_cast<size_type>(__first - __b);
3317 erase(__r, static_cast<size_type>(__last - __first));
3318 return __b + static_cast<difference_type>(__r);
3319}
3320
3321template <class _CharT, class _Traits, class _Allocator>
3322inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::pop_back() {
3323 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "string::pop_back(): string is already empty");
3324 __erase_to_end(pos: size() - 1);
3325}
3326
3327template <class _CharT, class _Traits, class _Allocator>
3328inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT {
3329 size_type __old_size;
3330 if (__is_long()) {
3331 __old_size = __get_long_size();
3332 traits_type::assign(*__get_long_pointer(), value_type());
3333 __set_long_size(s: 0);
3334 } else {
3335 __old_size = __get_short_size();
3336 traits_type::assign(*__get_short_pointer(), value_type());
3337 __set_short_size(s: 0);
3338 }
3339 __annotate_shrink(__old_size);
3340}
3341
3342template <class _CharT, class _Traits, class _Allocator>
3343_LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c) {
3344 size_type __sz = size();
3345 if (__n > __sz)
3346 append(__n - __sz, __c);
3347 else
3348 __erase_to_end(pos: __n);
3349}
3350
3351template <class _CharT, class _Traits, class _Allocator>
3352_LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __requested_capacity) {
3353 if (__requested_capacity > max_size())
3354 this->__throw_length_error();
3355
3356 // Make sure reserve(n) never shrinks. This is technically only required in C++20
3357 // and later (since P0966R1), however we provide consistent behavior in all Standard
3358 // modes because this function is instantiated in the shared library.
3359 if (__requested_capacity <= capacity())
3360 return;
3361
3362 __annotation_guard __g(*this);
3363 __long __buffer = __allocate_long_buffer(__alloc_, size(), __requested_capacity);
3364 traits_type::copy(std::__to_address(__buffer.__data_), data(), __buffer.__size_ + 1);
3365 __reset_internal_buffer(new_rep: __buffer);
3366}
3367
3368template <class _CharT, class _Traits, class _Allocator>
3369inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT {
3370 if (!__is_long())
3371 return;
3372
3373 const auto __ptr = __get_long_pointer();
3374 const auto __size = __get_long_size();
3375 const auto __cap = __get_long_cap();
3376
3377 // We're a long string and we're shrinking into the small buffer.
3378 if (__fits_in_sso(sz: __size)) {
3379 __annotation_guard __g(*this);
3380 __set_short_size(s: __size);
3381 traits_type::copy(std::__to_address(__get_short_pointer()), std::__to_address(__ptr), __size + 1);
3382 __alloc_traits::deallocate(__alloc_, __ptr, __cap);
3383 return;
3384 }
3385
3386 if (__align_allocation_size(__size) == __cap)
3387 return;
3388
3389# if _LIBCPP_HAS_EXCEPTIONS
3390 try {
3391# endif // _LIBCPP_HAS_EXCEPTIONS
3392 __annotation_guard __g(*this);
3393 __long __buffer = __allocate_long_buffer(__alloc_, __size);
3394
3395 // The Standard mandates shrink_to_fit() does not increase the capacity.
3396 // With equal capacity keep the existing buffer. This avoids extra work
3397 // due to swapping the elements.
3398 if (__buffer.__cap_ * __endian_factor - 1 >= capacity()) {
3399 __alloc_traits::deallocate(__alloc_, __buffer.__data_, __buffer.__cap_ * __endian_factor);
3400 return;
3401 }
3402
3403 traits_type::copy(std::__to_address(__buffer.__data_), std::__to_address(__get_long_pointer()), __size + 1);
3404 __reset_internal_buffer(new_rep: __buffer);
3405# if _LIBCPP_HAS_EXCEPTIONS
3406 } catch (...) {
3407 return;
3408 }
3409# endif // _LIBCPP_HAS_EXCEPTIONS
3410}
3411
3412template <class _CharT, class _Traits, class _Allocator>
3413_LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3414basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const {
3415 if (__n >= size())
3416 this->__throw_out_of_range();
3417 return (*this)[__n];
3418}
3419
3420template <class _CharT, class _Traits, class _Allocator>
3421_LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::reference
3422basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) {
3423 if (__n >= size())
3424 this->__throw_out_of_range();
3425 return (*this)[__n];
3426}
3427
3428template <class _CharT, class _Traits, class _Allocator>
3429_LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
3430basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const {
3431 size_type __sz = size();
3432 if (__pos > __sz)
3433 this->__throw_out_of_range();
3434 size_type __rlen = std::min(__n, __sz - __pos);
3435 traits_type::copy(__s, data() + __pos, __rlen);
3436 return __rlen;
3437}
3438
3439template <class _CharT, class _Traits, class _Allocator>
3440inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
3441# if _LIBCPP_STD_VER >= 14
3442 _NOEXCEPT
3443# else
3444 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v<allocator_type>)
3445# endif
3446{
3447 _LIBCPP_ASSERT_COMPATIBLE_ALLOCATOR(
3448 __alloc_traits::propagate_on_container_swap::value || __alloc_traits::is_always_equal::value ||
3449 __alloc_ == __str.__alloc_,
3450 "swapping non-equal allocators");
3451 if (!__is_long())
3452 __annotate_delete();
3453 if (this != std::addressof(__str) && !__str.__is_long())
3454 __str.__annotate_delete();
3455 std::swap(__rep_, __str.__rep_);
3456 std::__swap_allocator(__alloc_, __str.__alloc_);
3457 if (!__is_long())
3458 __annotate_new(current_size: __get_short_size());
3459 if (this != std::addressof(__str) && !__str.__is_long())
3460 __str.__annotate_new(current_size: __str.__get_short_size());
3461}
3462
3463// compare
3464
3465template <class _CharT, class _Traits, class _Allocator>
3466inline _LIBCPP_CONSTEXPR_SINCE_CXX20 int basic_string<_CharT, _Traits, _Allocator>::compare(
3467 size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const {
3468 _LIBCPP_ASSERT_NON_NULL(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
3469 size_type __sz = size();
3470 if (__pos1 > __sz || __n2 == npos)
3471 this->__throw_out_of_range();
3472 size_type __rlen = std::min(__n1, __sz - __pos1);
3473 int __r = traits_type::compare(data() + __pos1, __s, std::min(__rlen, __n2));
3474 if (__r == 0) {
3475 if (__rlen < __n2)
3476 __r = -1;
3477 else if (__rlen > __n2)
3478 __r = 1;
3479 }
3480 return __r;
3481}
3482
3483// __invariants
3484
3485template <class _CharT, class _Traits, class _Allocator>
3486inline _LIBCPP_CONSTEXPR_SINCE_CXX20 bool basic_string<_CharT, _Traits, _Allocator>::__invariants() const {
3487 if (size() > capacity())
3488 return false;
3489 if (capacity() < __min_cap - 1)
3490 return false;
3491 if (data() == nullptr)
3492 return false;
3493 if (!_Traits::eq(data()[size()], value_type()))
3494 return false;
3495 return true;
3496}
3497
3498// operator==
3499
3500template <class _CharT, class _Traits, class _Allocator>
3501inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool
3502operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3503 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {
3504 size_t __lhs_sz = __lhs.size();
3505 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(), __rhs.data(), __lhs_sz) == 0;
3506}
3507
3508template <class _CharT, class _Traits, class _Allocator>
3509inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool
3510operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3511 const _CharT* _LIBCPP_DIAGNOSE_NULLPTR __rhs) _NOEXCEPT {
3512 _LIBCPP_ASSERT_NON_NULL(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
3513
3514 using _String = basic_string<_CharT, _Traits, _Allocator>;
3515
3516 size_t __rhs_len = _Traits::length(__rhs);
3517 if (__builtin_constant_p(__rhs_len) && !_String::__fits_in_sso(__rhs_len)) {
3518 if (!__lhs.__is_long())
3519 return false;
3520 }
3521 if (__rhs_len != __lhs.size())
3522 return false;
3523 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
3524}
3525
3526# if _LIBCPP_STD_VER <= 17
3527template <class _CharT, class _Traits, class _Allocator>
3528inline _LIBCPP_HIDE_FROM_ABI bool
3529operator==(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {
3530 return __rhs == __lhs;
3531}
3532# endif // _LIBCPP_STD_VER <= 17
3533
3534# if _LIBCPP_STD_VER >= 20
3535
3536template <class _CharT, class _Traits, class _Allocator>
3537_LIBCPP_HIDE_FROM_ABI constexpr auto operator<=>(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3538 const basic_string<_CharT, _Traits, _Allocator>& __rhs) noexcept {
3539 return basic_string_view<_CharT, _Traits>(__lhs) <=> basic_string_view<_CharT, _Traits>(__rhs);
3540}
3541
3542template <class _CharT, class _Traits, class _Allocator>
3543_LIBCPP_HIDE_FROM_ABI constexpr auto
3544operator<=>(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) {
3545 return basic_string_view<_CharT, _Traits>(__lhs) <=> basic_string_view<_CharT, _Traits>(__rhs);
3546}
3547
3548# else // _LIBCPP_STD_VER >= 20
3549
3550template <class _CharT, class _Traits, class _Allocator>
3551inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3552 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {
3553 return !(__lhs == __rhs);
3554}
3555
3556template <class _CharT, class _Traits, class _Allocator>
3557inline _LIBCPP_HIDE_FROM_ABI bool
3558operator!=(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {
3559 return !(__lhs == __rhs);
3560}
3561
3562template <class _CharT, class _Traits, class _Allocator>
3563inline _LIBCPP_HIDE_FROM_ABI bool
3564operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) _NOEXCEPT {
3565 return !(__lhs == __rhs);
3566}
3567
3568// operator<
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.compare(__rhs) < 0;
3574}
3575
3576template <class _CharT, class _Traits, class _Allocator>
3577inline _LIBCPP_HIDE_FROM_ABI bool
3578operator<(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) _NOEXCEPT {
3579 return __lhs.compare(__rhs) < 0;
3580}
3581
3582template <class _CharT, class _Traits, class _Allocator>
3583inline _LIBCPP_HIDE_FROM_ABI bool
3584operator<(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {
3585 return __rhs.compare(__lhs) > 0;
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 __rhs < __lhs;
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 __rhs < __lhs;
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 < __lhs;
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 !(__lhs < __rhs);
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 !(__lhs < __rhs);
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 !(__lhs < __rhs);
3646}
3647# endif // _LIBCPP_STD_VER >= 20
3648
3649// operator +
3650
3651template <class _CharT, class _Traits, class _Allocator>
3652_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
3653__concatenate_strings(const _Allocator& __alloc,
3654 __type_identity_t<basic_string_view<_CharT, _Traits> > __str1,
3655 __type_identity_t<basic_string_view<_CharT, _Traits> > __str2) {
3656 using _String = basic_string<_CharT, _Traits, _Allocator>;
3657 _String __r(__uninitialized_size_tag(),
3658 __str1.size() + __str2.size(),
3659 _String::__alloc_traits::select_on_container_copy_construction(__alloc));
3660 auto __ptr = __r.__data();
3661 _Traits::copy(__ptr, __str1.data(), __str1.size());
3662 _Traits::copy(__ptr + __str1.size(), __str2.data(), __str2.size());
3663 _Traits::assign(__ptr[__str1.size() + __str2.size()], _CharT());
3664 return __r;
3665}
3666
3667template <class _CharT, class _Traits, class _Allocator>
3668_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
3669operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3670 const basic_string<_CharT, _Traits, _Allocator>& __rhs) {
3671 return std::__concatenate_strings<_CharT, _Traits>(__lhs.get_allocator(), __lhs, __rhs);
3672}
3673
3674template <class _CharT, class _Traits, class _Allocator>
3675_LIBCPP_HIDDEN _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
3676operator+(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) {
3677 return std::__concatenate_strings<_CharT, _Traits>(__rhs.get_allocator(), __lhs, __rhs);
3678}
3679
3680extern template _LIBCPP_EXPORTED_FROM_ABI string operator+
3681 <char, char_traits<char>, allocator<char> >(char const*, string const&);
3682
3683template <class _CharT, class _Traits, class _Allocator>
3684_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
3685operator+(_CharT __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) {
3686 return std::__concatenate_strings<_CharT, _Traits>(
3687 __rhs.get_allocator(), basic_string_view<_CharT, _Traits>(std::addressof(__lhs), 1), __rhs);
3688}
3689
3690template <class _CharT, class _Traits, class _Allocator>
3691_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
3692operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) {
3693 return std::__concatenate_strings<_CharT, _Traits>(__lhs.get_allocator(), __lhs, __rhs);
3694}
3695
3696template <class _CharT, class _Traits, class _Allocator>
3697_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
3698operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs) {
3699 return std::__concatenate_strings<_CharT, _Traits>(
3700 __lhs.get_allocator(), __lhs, basic_string_view<_CharT, _Traits>(std::addressof(__rhs), 1));
3701}
3702# if _LIBCPP_STD_VER >= 26
3703
3704template <class _CharT, class _Traits, class _Allocator>
3705_LIBCPP_HIDE_FROM_ABI constexpr basic_string<_CharT, _Traits, _Allocator>
3706operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3707 type_identity_t<basic_string_view<_CharT, _Traits>> __rhs) {
3708 return std::__concatenate_strings<_CharT, _Traits>(__lhs.get_allocator(), __lhs, __rhs);
3709}
3710
3711template <class _CharT, class _Traits, class _Allocator>
3712_LIBCPP_HIDE_FROM_ABI constexpr basic_string<_CharT, _Traits, _Allocator>
3713operator+(type_identity_t<basic_string_view<_CharT, _Traits>> __lhs,
3714 const basic_string<_CharT, _Traits, _Allocator>& __rhs) {
3715 return std::__concatenate_strings<_CharT, _Traits>(__rhs.get_allocator(), __lhs, __rhs);
3716}
3717
3718# endif // _LIBCPP_STD_VER >= 26
3719
3720# ifndef _LIBCPP_CXX03_LANG
3721
3722template <class _CharT, class _Traits, class _Allocator>
3723inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
3724operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) {
3725 return std::move(__lhs.append(__rhs));
3726}
3727
3728template <class _CharT, class _Traits, class _Allocator>
3729inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
3730operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs) {
3731 return std::move(__rhs.insert(0, __lhs));
3732}
3733
3734template <class _CharT, class _Traits, class _Allocator>
3735inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
3736operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs) {
3737 return std::move(__lhs.append(__rhs));
3738}
3739
3740template <class _CharT, class _Traits, class _Allocator>
3741inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
3742operator+(const _CharT* __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs) {
3743 return std::move(__rhs.insert(0, __lhs));
3744}
3745
3746template <class _CharT, class _Traits, class _Allocator>
3747inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
3748operator+(_CharT __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs) {
3749 __rhs.insert(__rhs.begin(), __lhs);
3750 return std::move(__rhs);
3751}
3752
3753template <class _CharT, class _Traits, class _Allocator>
3754inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
3755operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs) {
3756 return std::move(__lhs.append(__rhs));
3757}
3758
3759template <class _CharT, class _Traits, class _Allocator>
3760inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
3761operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs) {
3762 __lhs.push_back(__rhs);
3763 return std::move(__lhs);
3764}
3765
3766# endif // _LIBCPP_CXX03_LANG
3767
3768# if _LIBCPP_STD_VER >= 26
3769
3770template <class _CharT, class _Traits, class _Allocator>
3771_LIBCPP_HIDE_FROM_ABI constexpr basic_string<_CharT, _Traits, _Allocator>
3772operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs,
3773 type_identity_t<basic_string_view<_CharT, _Traits>> __rhs) {
3774 return std::move(__lhs.append(__rhs));
3775}
3776
3777template <class _CharT, class _Traits, class _Allocator>
3778_LIBCPP_HIDE_FROM_ABI constexpr basic_string<_CharT, _Traits, _Allocator>
3779operator+(type_identity_t<basic_string_view<_CharT, _Traits>> __lhs,
3780 basic_string<_CharT, _Traits, _Allocator>&& __rhs) {
3781 return std::move(__rhs.insert(0, __lhs));
3782}
3783
3784# endif // _LIBCPP_STD_VER >= 26
3785
3786// swap
3787
3788template <class _CharT, class _Traits, class _Allocator>
3789inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
3790swap(basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>& __rhs)
3791 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs))) {
3792 __lhs.swap(__rhs);
3793}
3794
3795[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI int stoi(const string& __str, size_t* __idx = nullptr, int __base = 10);
3796[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI unsigned long
3797stoul(const string& __str, size_t* __idx = nullptr, int __base = 10);
3798[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI long stol(const string& __str, size_t* __idx = nullptr, int __base = 10);
3799[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI long long
3800stoll(const string& __str, size_t* __idx = nullptr, int __base = 10);
3801[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI unsigned long long
3802stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);
3803
3804[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI float stof(const string& __str, size_t* __idx = nullptr);
3805[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI double stod(const string& __str, size_t* __idx = nullptr);
3806[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI long double stold(const string& __str, size_t* __idx = nullptr);
3807
3808[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI string to_string(int __val);
3809[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI string to_string(unsigned __val);
3810[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI string to_string(long __val);
3811[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI string to_string(unsigned long __val);
3812[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI string to_string(long long __val);
3813[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI string to_string(unsigned long long __val);
3814[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI string to_string(float __val);
3815[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI string to_string(double __val);
3816[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI string to_string(long double __val);
3817
3818# if _LIBCPP_HAS_WIDE_CHARACTERS
3819[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI int stoi(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
3820[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI long stol(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
3821[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI unsigned long
3822stoul(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
3823[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI long long
3824stoll(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
3825[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI unsigned long long
3826stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
3827
3828[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI float stof(const wstring& __str, size_t* __idx = nullptr);
3829[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI double stod(const wstring& __str, size_t* __idx = nullptr);
3830[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI long double stold(const wstring& __str, size_t* __idx = nullptr);
3831
3832[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(int __val);
3833[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(unsigned __val);
3834[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(long __val);
3835[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(unsigned long __val);
3836[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(long long __val);
3837[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(unsigned long long __val);
3838[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(float __val);
3839[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(double __val);
3840[[__nodiscard__]] _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(long double __val);
3841# endif // _LIBCPP_HAS_WIDE_CHARACTERS
3842
3843template <class _CharT, class _Traits, class _Allocator>
3844_LIBCPP_TEMPLATE_DATA_VIS const typename basic_string<_CharT, _Traits, _Allocator>::size_type
3845 basic_string<_CharT, _Traits, _Allocator>::npos;
3846
3847template <class _CharT, class _Allocator>
3848struct __string_hash : public __unary_function<basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t> {
3849 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI size_t
3850 operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT {
3851 return std::__do_string_hash(__val.data(), __val.data() + __val.size());
3852 }
3853};
3854
3855template <class _Allocator>
3856struct hash<basic_string<char, char_traits<char>, _Allocator> > : __string_hash<char, _Allocator> {};
3857
3858# if _LIBCPP_HAS_CHAR8_T
3859template <class _Allocator>
3860struct hash<basic_string<char8_t, char_traits<char8_t>, _Allocator> > : __string_hash<char8_t, _Allocator> {};
3861# endif
3862
3863template <class _Allocator>
3864struct hash<basic_string<char16_t, char_traits<char16_t>, _Allocator> > : __string_hash<char16_t, _Allocator> {};
3865
3866template <class _Allocator>
3867struct hash<basic_string<char32_t, char_traits<char32_t>, _Allocator> > : __string_hash<char32_t, _Allocator> {};
3868
3869# if _LIBCPP_HAS_WIDE_CHARACTERS
3870template <class _Allocator>
3871struct hash<basic_string<wchar_t, char_traits<wchar_t>, _Allocator> > : __string_hash<wchar_t, _Allocator> {};
3872# endif
3873
3874template <class _CharT, class _Traits, class _Allocator>
3875_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
3876operator<<(basic_ostream<_CharT, _Traits>& __os, const basic_string<_CharT, _Traits, _Allocator>& __str);
3877
3878template <class _CharT, class _Traits, class _Allocator>
3879_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
3880operator>>(basic_istream<_CharT, _Traits>& __is, basic_string<_CharT, _Traits, _Allocator>& __str);
3881
3882template <class _CharT, class _Traits, class _Allocator>
3883_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
3884getline(basic_istream<_CharT, _Traits>& __is, basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
3885
3886template <class _CharT, class _Traits, class _Allocator>
3887inline _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
3888getline(basic_istream<_CharT, _Traits>& __is, basic_string<_CharT, _Traits, _Allocator>& __str);
3889
3890template <class _CharT, class _Traits, class _Allocator>
3891inline _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
3892getline(basic_istream<_CharT, _Traits>&& __is, basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
3893
3894template <class _CharT, class _Traits, class _Allocator>
3895inline _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
3896getline(basic_istream<_CharT, _Traits>&& __is, basic_string<_CharT, _Traits, _Allocator>& __str);
3897
3898# if _LIBCPP_STD_VER >= 20
3899template <class _CharT, class _Traits, class _Allocator, class _Up>
3900inline _LIBCPP_HIDE_FROM_ABI constexpr typename basic_string<_CharT, _Traits, _Allocator>::size_type
3901erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
3902 auto __old_size = __str.size();
3903 __str.erase(std::remove(__str.begin(), __str.end(), __v), __str.end());
3904 return __old_size - __str.size();
3905}
3906
3907template <class _CharT, class _Traits, class _Allocator, class _Predicate>
3908inline _LIBCPP_HIDE_FROM_ABI constexpr typename basic_string<_CharT, _Traits, _Allocator>::size_type
3909erase_if(basic_string<_CharT, _Traits, _Allocator>& __str, _Predicate __pred) {
3910 auto __old_size = __str.size();
3911 __str.erase(std::remove_if(__str.begin(), __str.end(), __pred), __str.end());
3912 return __old_size - __str.size();
3913}
3914# endif
3915
3916# if _LIBCPP_STD_VER >= 14
3917// Literal suffixes for basic_string [basic.string.literals]
3918inline namespace literals {
3919inline namespace string_literals {
3920[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<char>
3921operator""s(const char* __str, size_t __len) {
3922 return basic_string<char>(__str, __len);
3923}
3924
3925# if _LIBCPP_HAS_WIDE_CHARACTERS
3926[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI
3927_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<wchar_t> operator""s(const wchar_t* __str, size_t __len) {
3928 return basic_string<wchar_t>(__str, __len);
3929}
3930# endif
3931
3932# if _LIBCPP_HAS_CHAR8_T
3933[[__nodiscard__]] inline
3934 _LIBCPP_HIDE_FROM_ABI constexpr basic_string<char8_t> operator""s(const char8_t* __str, size_t __len) {
3935 return basic_string<char8_t>(__str, __len);
3936}
3937# endif
3938
3939[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI
3940_LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<char16_t> operator""s(const char16_t* __str, size_t __len) {
3941 return basic_string<char16_t>(__str, __len);
3942}
3943
3944[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<char32_t>
3945operator""s(const char32_t* __str, size_t __len) {
3946 return basic_string<char32_t>(__str, __len);
3947}
3948} // namespace string_literals
3949} // namespace literals
3950
3951# if _LIBCPP_STD_VER >= 20
3952template <>
3953inline constexpr bool __format::__enable_insertable<std::basic_string<char>> = true;
3954# if _LIBCPP_HAS_WIDE_CHARACTERS
3955template <>
3956inline constexpr bool __format::__enable_insertable<std::basic_string<wchar_t>> = true;
3957# endif
3958# endif
3959
3960# endif
3961
3962_LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS
3963_LIBCPP_END_NAMESPACE_STD
3964
3965_LIBCPP_POP_MACROS
3966
3967#endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
3968
3969#endif // _LIBCPP_STRING
3970