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