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