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