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