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