Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/c++/v1/string is written in an unsupported language. File is not indexed.

0001 // -*- C++ -*-
0002 //===----------------------------------------------------------------------===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0007 //
0008 //===----------------------------------------------------------------------===//
0009 
0010 #ifndef _LIBCPP_STRING
0011 #define _LIBCPP_STRING
0012 
0013 // clang-format off
0014 
0015 /*
0016     string synopsis
0017 
0018 #include <compare>
0019 #include <initializer_list>
0020 
0021 namespace std
0022 {
0023 
0024 template <class stateT>
0025 class fpos
0026 {
0027 private:
0028     stateT st;
0029 public:
0030     fpos(streamoff = streamoff());
0031 
0032     operator streamoff() const;
0033 
0034     stateT state() const;
0035     void state(stateT);
0036 
0037     fpos& operator+=(streamoff);
0038     fpos  operator+ (streamoff) const;
0039     fpos& operator-=(streamoff);
0040     fpos  operator- (streamoff) const;
0041 };
0042 
0043 template <class stateT> streamoff operator-(const fpos<stateT>& x, const fpos<stateT>& y);
0044 
0045 template <class stateT> bool operator==(const fpos<stateT>& x, const fpos<stateT>& y);
0046 template <class stateT> bool operator!=(const fpos<stateT>& x, const fpos<stateT>& y);
0047 
0048 template <class charT>
0049 struct char_traits
0050 {
0051     using char_type           = charT;
0052     using int_type            = ...;
0053     using off_type            = streamoff;
0054     using pos_type            = streampos;
0055     using state_type          = mbstate_t;
0056     using comparison_category = strong_ordering; // Since C++20 only for the specializations
0057                                                  // char, wchar_t, char8_t, char16_t, and char32_t.
0058 
0059     static void assign(char_type& c1, const char_type& c2) noexcept;
0060     static constexpr bool eq(char_type c1, char_type c2) noexcept;
0061     static constexpr bool lt(char_type c1, char_type c2) noexcept;
0062 
0063     static int              compare(const char_type* s1, const char_type* s2, size_t n);
0064     static size_t           length(const char_type* s);
0065     static const char_type* find(const char_type* s, size_t n, const char_type& a);
0066     static char_type*       move(char_type* s1, const char_type* s2, size_t n);
0067     static char_type*       copy(char_type* s1, const char_type* s2, size_t n);
0068     static char_type*       assign(char_type* s, size_t n, char_type a);
0069 
0070     static constexpr int_type  not_eof(int_type c) noexcept;
0071     static constexpr char_type to_char_type(int_type c) noexcept;
0072     static constexpr int_type  to_int_type(char_type c) noexcept;
0073     static constexpr bool      eq_int_type(int_type c1, int_type c2) noexcept;
0074     static constexpr int_type  eof() noexcept;
0075 };
0076 
0077 template <> struct char_traits<char>;
0078 template <> struct char_traits<wchar_t>;
0079 template <> struct char_traits<char8_t>;  // C++20
0080 template <> struct char_traits<char16_t>;
0081 template <> struct char_traits<char32_t>;
0082 
0083 template<class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
0084 class basic_string
0085 {
0086 public:
0087 // types:
0088     typedef traits traits_type;
0089     typedef typename traits_type::char_type value_type;
0090     typedef Allocator allocator_type;
0091     typedef typename allocator_type::size_type size_type;
0092     typedef typename allocator_type::difference_type difference_type;
0093     typedef typename allocator_type::reference reference;
0094     typedef typename allocator_type::const_reference const_reference;
0095     typedef typename allocator_type::pointer pointer;
0096     typedef typename allocator_type::const_pointer const_pointer;
0097     typedef implementation-defined iterator;
0098     typedef implementation-defined const_iterator;
0099     typedef std::reverse_iterator<iterator> reverse_iterator;
0100     typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
0101 
0102     static const size_type npos = -1;
0103 
0104     basic_string()
0105         noexcept(is_nothrow_default_constructible<allocator_type>::value);                      // constexpr since C++20
0106     explicit basic_string(const allocator_type& a);                                             // constexpr since C++20
0107     basic_string(const basic_string& str);                                                      // constexpr since C++20
0108     basic_string(basic_string&& str)
0109         noexcept(is_nothrow_move_constructible<allocator_type>::value);                         // constexpr since C++20
0110     basic_string(const basic_string& str, size_type pos,
0111                  const allocator_type& a = allocator_type());                                   // constexpr since C++20
0112     basic_string(const basic_string& str, size_type pos, size_type n,
0113                  const Allocator& a = Allocator());                                             // constexpr since C++20
0114     constexpr basic_string(
0115         basic_string&& str, size_type pos, const Allocator& a = Allocator());                   // since C++23
0116     constexpr basic_string(
0117         basic_string&& str, size_type pos, size_type n, const Allocator& a = Allocator());      // since C++23
0118     template<class T>
0119         basic_string(const T& t, size_type pos, size_type n, const Allocator& a = Allocator()); // C++17, constexpr since C++20
0120     template <class T>
0121         explicit basic_string(const T& t, const Allocator& a = Allocator());                    // C++17, constexpr since C++20
0122     basic_string(const value_type* s, const allocator_type& a = allocator_type());              // constexpr since C++20
0123     basic_string(const value_type* s, size_type n, const allocator_type& a = allocator_type()); // constexpr since C++20
0124     basic_string(nullptr_t) = delete; // C++23
0125     basic_string(size_type n, value_type c, const allocator_type& a = allocator_type());        // constexpr since C++20
0126     template<class InputIterator>
0127         basic_string(InputIterator begin, InputIterator end,
0128                      const allocator_type& a = allocator_type());                               // constexpr since C++20
0129     template<container-compatible-range<charT> R>
0130       constexpr basic_string(from_range_t, R&& rg, const Allocator& a = Allocator());           // since C++23
0131     basic_string(initializer_list<value_type>, const Allocator& = Allocator());                 // constexpr since C++20
0132     basic_string(const basic_string&, const Allocator&);                                        // constexpr since C++20
0133     basic_string(basic_string&&, const Allocator&);                                             // constexpr since C++20
0134 
0135     ~basic_string();                                                                            // constexpr since C++20
0136 
0137     operator basic_string_view<charT, traits>() const noexcept;                                 // constexpr since C++20
0138 
0139     basic_string& operator=(const basic_string& str);                                           // constexpr since C++20
0140     template <class T>
0141         basic_string& operator=(const T& t);                                                    // C++17, constexpr since C++20
0142     basic_string& operator=(basic_string&& str)
0143         noexcept(
0144              allocator_type::propagate_on_container_move_assignment::value ||
0145              allocator_type::is_always_equal::value );                                          // C++17, constexpr since C++20
0146     basic_string& operator=(const value_type* s);                                               // constexpr since C++20
0147     basic_string& operator=(nullptr_t) = delete; // C++23
0148     basic_string& operator=(value_type c);                                                      // constexpr since C++20
0149     basic_string& operator=(initializer_list<value_type>);                                      // constexpr since C++20
0150 
0151     iterator       begin() noexcept;                                                            // constexpr since C++20
0152     const_iterator begin() const noexcept;                                                      // constexpr since C++20
0153     iterator       end() noexcept;                                                              // constexpr since C++20
0154     const_iterator end() const noexcept;                                                        // constexpr since C++20
0155 
0156     reverse_iterator       rbegin() noexcept;                                                   // constexpr since C++20
0157     const_reverse_iterator rbegin() const noexcept;                                             // constexpr since C++20
0158     reverse_iterator       rend() noexcept;                                                     // constexpr since C++20
0159     const_reverse_iterator rend() const noexcept;                                               // constexpr since C++20
0160 
0161     const_iterator         cbegin() const noexcept;                                             // constexpr since C++20
0162     const_iterator         cend() const noexcept;                                               // constexpr since C++20
0163     const_reverse_iterator crbegin() const noexcept;                                            // constexpr since C++20
0164     const_reverse_iterator crend() const noexcept;                                              // constexpr since C++20
0165 
0166     size_type size() const noexcept;                                                            // constexpr since C++20
0167     size_type length() const noexcept;                                                          // constexpr since C++20
0168     size_type max_size() const noexcept;                                                        // constexpr since C++20
0169     size_type capacity() const noexcept;                                                        // constexpr since C++20
0170 
0171     void resize(size_type n, value_type c);                                                     // constexpr since C++20
0172     void resize(size_type n);                                                                   // constexpr since C++20
0173 
0174     template<class Operation>
0175     constexpr void resize_and_overwrite(size_type n, Operation op); // since C++23
0176 
0177     void reserve(size_type res_arg);                                                            // constexpr since C++20
0178     void reserve();                                                                             // deprecated in C++20, removed in C++26
0179     void shrink_to_fit();                                                                       // constexpr since C++20
0180     void clear() noexcept;                                                                      // constexpr since C++20
0181     bool empty() const noexcept;                                                                // constexpr since C++20
0182 
0183     const_reference operator[](size_type pos) const;                                            // constexpr since C++20
0184     reference       operator[](size_type pos);                                                  // constexpr since C++20
0185 
0186     const_reference at(size_type n) const;                                                      // constexpr since C++20
0187     reference       at(size_type n);                                                            // constexpr since C++20
0188 
0189     basic_string& operator+=(const basic_string& str);                                          // constexpr since C++20
0190     template <class T>
0191         basic_string& operator+=(const T& t);                                                   // C++17, constexpr since C++20
0192     basic_string& operator+=(const value_type* s);                                              // constexpr since C++20
0193     basic_string& operator+=(value_type c);                                                     // constexpr since C++20
0194     basic_string& operator+=(initializer_list<value_type>);                                     // constexpr since C++20
0195 
0196     basic_string& append(const basic_string& str);                                              // constexpr since C++20
0197     template <class T>
0198         basic_string& append(const T& t);                                                       // C++17, constexpr since C++20
0199     basic_string& append(const basic_string& str, size_type pos, size_type n=npos);             // C++14, constexpr since C++20
0200     template <class T>
0201         basic_string& append(const T& t, size_type pos, size_type n=npos);                      // C++17, constexpr since C++20
0202     basic_string& append(const value_type* s, size_type n);                                     // constexpr since C++20
0203     basic_string& append(const value_type* s);                                                  // constexpr since C++20
0204     basic_string& append(size_type n, value_type c);                                            // constexpr since C++20
0205     template<class InputIterator>
0206         basic_string& append(InputIterator first, InputIterator last);                          // constexpr since C++20
0207     template<container-compatible-range<charT> R>
0208       constexpr basic_string& append_range(R&& rg);                                             // C++23
0209     basic_string& append(initializer_list<value_type>);                                         // constexpr since C++20
0210 
0211     void push_back(value_type c);                                                               // constexpr since C++20
0212     void pop_back();                                                                            // constexpr since C++20
0213     reference       front();                                                                    // constexpr since C++20
0214     const_reference front() const;                                                              // constexpr since C++20
0215     reference       back();                                                                     // constexpr since C++20
0216     const_reference back() const;                                                               // constexpr since C++20
0217 
0218     basic_string& assign(const basic_string& str);                                              // constexpr since C++20
0219     template <class T>
0220         basic_string& assign(const T& t);                                                       // C++17, constexpr since C++20
0221     basic_string& assign(basic_string&& str);                                                   // constexpr since C++20
0222     basic_string& assign(const basic_string& str, size_type pos, size_type n=npos);             // C++14, constexpr since C++20
0223     template <class T>
0224         basic_string& assign(const T& t, size_type pos, size_type n=npos);                      // C++17, constexpr since C++20
0225     basic_string& assign(const value_type* s, size_type n);                                     // constexpr since C++20
0226     basic_string& assign(const value_type* s);                                                  // constexpr since C++20
0227     basic_string& assign(size_type n, value_type c);                                            // constexpr since C++20
0228     template<class InputIterator>
0229         basic_string& assign(InputIterator first, InputIterator last);                          // constexpr since C++20
0230     template<container-compatible-range<charT> R>
0231       constexpr basic_string& assign_range(R&& rg);                                             // C++23
0232     basic_string& assign(initializer_list<value_type>);                                         // constexpr since C++20
0233 
0234     basic_string& insert(size_type pos1, const basic_string& str);                              // constexpr since C++20
0235     template <class T>
0236         basic_string& insert(size_type pos1, const T& t);                                       // constexpr since C++20
0237     basic_string& insert(size_type pos1, const basic_string& str,
0238                          size_type pos2, size_type n);                                          // constexpr since C++20
0239     template <class T>
0240         basic_string& insert(size_type pos1, const T& t, size_type pos2, size_type n);          // C++17, constexpr since C++20
0241     basic_string& insert(size_type pos, const value_type* s, size_type n=npos);                 // C++14, constexpr since C++20
0242     basic_string& insert(size_type pos, const value_type* s);                                   // constexpr since C++20
0243     basic_string& insert(size_type pos, size_type n, value_type c);                             // constexpr since C++20
0244     iterator      insert(const_iterator p, value_type c);                                       // constexpr since C++20
0245     iterator      insert(const_iterator p, size_type n, value_type c);                          // constexpr since C++20
0246     template<class InputIterator>
0247         iterator insert(const_iterator p, InputIterator first, InputIterator last);             // constexpr since C++20
0248     template<container-compatible-range<charT> R>
0249       constexpr iterator insert_range(const_iterator p, R&& rg);                                // C++23
0250     iterator      insert(const_iterator p, initializer_list<value_type>);                       // constexpr since C++20
0251 
0252     basic_string& erase(size_type pos = 0, size_type n = npos);                                 // constexpr since C++20
0253     iterator      erase(const_iterator position);                                               // constexpr since C++20
0254     iterator      erase(const_iterator first, const_iterator last);                             // constexpr since C++20
0255 
0256     basic_string& replace(size_type pos1, size_type n1, const basic_string& str);               // constexpr since C++20
0257     template <class T>
0258     basic_string& replace(size_type pos1, size_type n1, const T& t);                            // C++17, constexpr since C++20
0259     basic_string& replace(size_type pos1, size_type n1, const basic_string& str,
0260                           size_type pos2, size_type n2=npos);                                   // C++14, constexpr since C++20
0261     template <class T>
0262         basic_string& replace(size_type pos1, size_type n1, const T& t,
0263                               size_type pos2, size_type n);                                     // C++17, constexpr since C++20
0264     basic_string& replace(size_type pos, size_type n1, const value_type* s, size_type n2);      // constexpr since C++20
0265     basic_string& replace(size_type pos, size_type n1, const value_type* s);                    // constexpr since C++20
0266     basic_string& replace(size_type pos, size_type n1, size_type n2, value_type c);             // constexpr since C++20
0267     basic_string& replace(const_iterator i1, const_iterator i2, const basic_string& str);       // constexpr since C++20
0268     template <class T>
0269         basic_string& replace(const_iterator i1, const_iterator i2, const T& t);                // C++17, constexpr since C++20
0270     basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s, size_type n); // constexpr since C++20
0271     basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s);           // constexpr since C++20
0272     basic_string& replace(const_iterator i1, const_iterator i2, size_type n, value_type c);     // constexpr since C++20
0273     template<class InputIterator>
0274         basic_string& replace(const_iterator i1, const_iterator i2, InputIterator j1, InputIterator j2); // constexpr since C++20
0275     template<container-compatible-range<charT> R>
0276       constexpr basic_string& replace_with_range(const_iterator i1, const_iterator i2, R&& rg); // C++23
0277     basic_string& replace(const_iterator i1, const_iterator i2, initializer_list<value_type>);  // constexpr since C++20
0278 
0279     size_type copy(value_type* s, size_type n, size_type pos = 0) const;                        // constexpr since C++20
0280     basic_string substr(size_type pos = 0, size_type n = npos) const;                           // constexpr in C++20, removed in C++23
0281     basic_string substr(size_type pos = 0, size_type n = npos) const&;                          // since C++23
0282     constexpr basic_string substr(size_type pos = 0, size_type n = npos) &&;                    // since C++23
0283     void swap(basic_string& str)
0284         noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
0285                  allocator_traits<allocator_type>::is_always_equal::value);                     // C++17, constexpr since C++20
0286 
0287     const value_type* c_str() const noexcept;                                                   // constexpr since C++20
0288     const value_type* data() const noexcept;                                                    // constexpr since C++20
0289           value_type* data()       noexcept;                                                    // C++17, constexpr since C++20
0290 
0291     allocator_type get_allocator() const noexcept;                                              // constexpr since C++20
0292 
0293     size_type find(const basic_string& str, size_type pos = 0) const noexcept;                  // constexpr since C++20
0294     template <class T>
0295         size_type find(const T& t, size_type pos = 0) const noexcept;                           // C++17, noexcept as an extension, constexpr since C++20
0296     size_type find(const value_type* s, size_type pos, size_type n) const noexcept;             // constexpr since C++20
0297     size_type find(const value_type* s, size_type pos = 0) const noexcept;                      // constexpr since C++20
0298     size_type find(value_type c, size_type pos = 0) const noexcept;                             // constexpr since C++20
0299 
0300     size_type rfind(const basic_string& str, size_type pos = npos) const noexcept;              // constexpr since C++20
0301     template <class T>
0302         size_type rfind(const T& t, size_type pos = npos) const noexcept;                       // C++17, noexcept as an extension, constexpr since C++20
0303     size_type rfind(const value_type* s, size_type pos, size_type n) const noexcept;            // constexpr since C++20
0304     size_type rfind(const value_type* s, size_type pos = npos) const noexcept;                  // constexpr since C++20
0305     size_type rfind(value_type c, size_type pos = npos) const noexcept;                         // constexpr since C++20
0306 
0307     size_type find_first_of(const basic_string& str, size_type pos = 0) const noexcept;         // constexpr since C++20
0308     template <class T>
0309         size_type find_first_of(const T& t, size_type pos = 0) const noexcept;                  // C++17, noexcept as an extension, constexpr since C++20
0310     size_type find_first_of(const value_type* s, size_type pos, size_type n) const noexcept;    // constexpr since C++20
0311     size_type find_first_of(const value_type* s, size_type pos = 0) const noexcept;             // constexpr since C++20
0312     size_type find_first_of(value_type c, size_type pos = 0) const noexcept;                    // constexpr since C++20
0313 
0314     size_type find_last_of(const basic_string& str, size_type pos = npos) const noexcept;       // constexpr since C++20
0315     template <class T>
0316         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
0317     size_type find_last_of(const value_type* s, size_type pos, size_type n) const noexcept;     // constexpr since C++20
0318     size_type find_last_of(const value_type* s, size_type pos = npos) const noexcept;           // constexpr since C++20
0319     size_type find_last_of(value_type c, size_type pos = npos) const noexcept;                  // constexpr since C++20
0320 
0321     size_type find_first_not_of(const basic_string& str, size_type pos = 0) const noexcept;     // constexpr since C++20
0322     template <class T>
0323         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
0324     size_type find_first_not_of(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
0325     size_type find_first_not_of(const value_type* s, size_type pos = 0) const noexcept;         // constexpr since C++20
0326     size_type find_first_not_of(value_type c, size_type pos = 0) const noexcept;                // constexpr since C++20
0327 
0328     size_type find_last_not_of(const basic_string& str, size_type pos = npos) const noexcept;   // constexpr since C++20
0329     template <class T>
0330         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
0331     size_type find_last_not_of(const value_type* s, size_type pos, size_type n) const noexcept; // constexpr since C++20
0332     size_type find_last_not_of(const value_type* s, size_type pos = npos) const noexcept;       // constexpr since C++20
0333     size_type find_last_not_of(value_type c, size_type pos = npos) const noexcept;              // constexpr since C++20
0334 
0335     int compare(const basic_string& str) const noexcept;                                        // constexpr since C++20
0336     template <class T>
0337         int compare(const T& t) const noexcept;                                                 // C++17, noexcept as an extension, constexpr since C++20
0338     int compare(size_type pos1, size_type n1, const basic_string& str) const;                   // constexpr since C++20
0339     template <class T>
0340         int compare(size_type pos1, size_type n1, const T& t) const;                            // C++17, constexpr since C++20
0341     int compare(size_type pos1, size_type n1, const basic_string& str,
0342                 size_type pos2, size_type n2=npos) const;                                       // C++14, constexpr since C++20
0343     template <class T>
0344         int compare(size_type pos1, size_type n1, const T& t,
0345                     size_type pos2, size_type n2=npos) const;                                   // C++17, constexpr since C++20
0346     int compare(const value_type* s) const noexcept;                                            // constexpr since C++20
0347     int compare(size_type pos1, size_type n1, const value_type* s) const;                       // constexpr since C++20
0348     int compare(size_type pos1, size_type n1, const value_type* s, size_type n2) const;         // constexpr since C++20
0349 
0350     constexpr bool starts_with(basic_string_view<charT, traits> sv) const noexcept;             // C++20
0351     constexpr bool starts_with(charT c) const noexcept;                                         // C++20
0352     constexpr bool starts_with(const charT* s) const;                                           // C++20
0353     constexpr bool ends_with(basic_string_view<charT, traits> sv) const noexcept;               // C++20
0354     constexpr bool ends_with(charT c) const noexcept;                                           // C++20
0355     constexpr bool ends_with(const charT* s) const;                                             // C++20
0356 
0357     constexpr bool contains(basic_string_view<charT, traits> sv) const noexcept;                // C++23
0358     constexpr bool contains(charT c) const noexcept;                                            // C++23
0359     constexpr bool contains(const charT* s) const;                                              // C++23
0360 };
0361 
0362 template<class InputIterator,
0363          class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
0364 basic_string(InputIterator, InputIterator, Allocator = Allocator())
0365    -> basic_string<typename iterator_traits<InputIterator>::value_type,
0366                   char_traits<typename iterator_traits<InputIterator>::value_type>,
0367                   Allocator>;   // C++17
0368 
0369 template<ranges::input_range R,
0370          class Allocator = allocator<ranges::range_value_t<R>>>
0371   basic_string(from_range_t, R&&, Allocator = Allocator())
0372     -> basic_string<ranges::range_value_t<R>, char_traits<ranges::range_value_t<R>>,
0373                     Allocator>; // C++23
0374 
0375 template<class charT,
0376          class traits,
0377          class Allocator = allocator<charT>>
0378   explicit basic_string(basic_string_view<charT, traits>, const Allocator& = Allocator())
0379     -> basic_string<charT, traits, Allocator>; // C++17
0380 
0381 template<class charT,
0382          class traits,
0383          class Allocator = allocator<charT>>
0384   basic_string(basic_string_view<charT, traits>,
0385                 typename see below::size_type, typename see below::size_type,
0386                 const Allocator& = Allocator())
0387     -> basic_string<charT, traits, Allocator>; // C++17
0388 
0389 template<class charT, class traits, class Allocator>
0390 basic_string<charT, traits, Allocator>
0391 operator+(const basic_string<charT, traits, Allocator>& lhs,
0392           const basic_string<charT, traits, Allocator>& rhs);                                   // constexpr since C++20
0393 
0394 template<class charT, class traits, class Allocator>
0395 basic_string<charT, traits, Allocator>
0396 operator+(const charT* lhs , const basic_string<charT,traits,Allocator>&rhs);                   // constexpr since C++20
0397 
0398 template<class charT, class traits, class Allocator>
0399 basic_string<charT, traits, Allocator>
0400 operator+(charT lhs, const basic_string<charT,traits,Allocator>& rhs);                          // constexpr since C++20
0401 
0402 template<class charT, class traits, class Allocator>
0403 basic_string<charT, traits, Allocator>
0404 operator+(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs);                 // constexpr since C++20
0405 
0406 template<class charT, class traits, class Allocator>
0407 basic_string<charT, traits, Allocator>
0408 operator+(const basic_string<charT, traits, Allocator>& lhs, charT rhs);                        // constexpr since C++20
0409 
0410 template<class charT, class traits, class Allocator>
0411   constexpr basic_string<charT, traits, Allocator>
0412     operator+(const basic_string<charT, traits, Allocator>& lhs,
0413               type_identity_t<basic_string_view<charT, traits>> rhs);                           // Since C++26
0414 template<class charT, class traits, class Allocator>
0415   constexpr basic_string<charT, traits, Allocator>
0416     operator+(basic_string<charT, traits, Allocator>&& lhs,
0417               type_identity_t<basic_string_view<charT, traits>> rhs);                           // Since C++26
0418 template<class charT, class traits, class Allocator>
0419   constexpr basic_string<charT, traits, Allocator>
0420     operator+(type_identity_t<basic_string_view<charT, traits>> lhs,
0421               const basic_string<charT, traits, Allocator>& rhs);                               // Since C++26
0422 template<class charT, class traits, class Allocator>
0423   constexpr basic_string<charT, traits, Allocator>
0424     operator+(type_identity_t<basic_string_view<charT, traits>> lhs,
0425               basic_string<charT, traits, Allocator>&& rhs);                                    // Since C++26
0426 
0427 
0428 template<class charT, class traits, class Allocator>
0429 bool operator==(const basic_string<charT, traits, Allocator>& lhs,
0430                 const basic_string<charT, traits, Allocator>& rhs) noexcept;                    // constexpr since C++20
0431 
0432 template<class charT, class traits, class Allocator>
0433 bool operator==(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;  // removed in C++20
0434 
0435 template<class charT, class traits, class Allocator>
0436 bool operator==(const basic_string<charT,traits,Allocator>& lhs, const charT* rhs) noexcept;    // constexpr since C++20
0437 
0438 template<class charT, class traits, class Allocator>
0439 bool operator!=(const basic_string<charT,traits,Allocator>& lhs,
0440                 const basic_string<charT, traits, Allocator>& rhs) noexcept;                    // removed in C++20
0441 
0442 template<class charT, class traits, class Allocator>
0443 bool operator!=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;  // removed in C++20
0444 
0445 template<class charT, class traits, class Allocator>
0446 bool operator!=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;  // removed in C++20
0447 
0448 template<class charT, class traits, class Allocator>
0449 bool operator< (const basic_string<charT, traits, Allocator>& lhs,
0450                 const basic_string<charT, traits, Allocator>& rhs) noexcept;                    // removed in C++20
0451 
0452 template<class charT, class traits, class Allocator>
0453 bool operator< (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;  // removed in C++20
0454 
0455 template<class charT, class traits, class Allocator>
0456 bool operator< (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;  // removed in C++20
0457 
0458 template<class charT, class traits, class Allocator>
0459 bool operator> (const basic_string<charT, traits, Allocator>& lhs,
0460                 const basic_string<charT, traits, Allocator>& rhs) noexcept;                    // removed in C++20
0461 
0462 template<class charT, class traits, class Allocator>
0463 bool operator> (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;  // removed in C++20
0464 
0465 template<class charT, class traits, class Allocator>
0466 bool operator> (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;  // removed in C++20
0467 
0468 template<class charT, class traits, class Allocator>
0469 bool operator<=(const basic_string<charT, traits, Allocator>& lhs,
0470                 const basic_string<charT, traits, Allocator>& rhs) noexcept;                    // removed in C++20
0471 
0472 template<class charT, class traits, class Allocator>
0473 bool operator<=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;  // removed in C++20
0474 
0475 template<class charT, class traits, class Allocator>
0476 bool operator<=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;  // removed in C++20
0477 
0478 template<class charT, class traits, class Allocator>
0479 bool operator>=(const basic_string<charT, traits, Allocator>& lhs,
0480                 const basic_string<charT, traits, Allocator>& rhs) noexcept;                    // removed in C++20
0481 
0482 template<class charT, class traits, class Allocator>
0483 bool operator>=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;  // removed in C++20
0484 
0485 template<class charT, class traits, class Allocator>
0486 bool operator>=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;  // removed in C++20
0487 
0488 template<class charT, class traits, class Allocator>                                            // since C++20
0489 constexpr see below operator<=>(const basic_string<charT, traits, Allocator>& lhs,
0490                                 const basic_string<charT, traits, Allocator>& rhs) noexcept;
0491 
0492 template<class charT, class traits, class Allocator>                                            // since C++20
0493 constexpr see below operator<=>(const basic_string<charT, traits, Allocator>& lhs,
0494                                 const charT* rhs) noexcept;
0495 
0496 template<class charT, class traits, class Allocator>
0497 void swap(basic_string<charT, traits, Allocator>& lhs,
0498           basic_string<charT, traits, Allocator>& rhs)
0499             noexcept(noexcept(lhs.swap(rhs)));                                                  // constexpr since C++20
0500 
0501 template<class charT, class traits, class Allocator>
0502 basic_istream<charT, traits>&
0503 operator>>(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
0504 
0505 template<class charT, class traits, class Allocator>
0506 basic_ostream<charT, traits>&
0507 operator<<(basic_ostream<charT, traits>& os, const basic_string<charT, traits, Allocator>& str);
0508 
0509 template<class charT, class traits, class Allocator>
0510 basic_istream<charT, traits>&
0511 getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str,
0512         charT delim);
0513 
0514 template<class charT, class traits, class Allocator>
0515 basic_istream<charT, traits>&
0516 getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
0517 
0518 template<class charT, class traits, class Allocator, class U>
0519 typename basic_string<charT, traits, Allocator>::size_type
0520 erase(basic_string<charT, traits, Allocator>& c, const U& value);    // C++20
0521 template<class charT, class traits, class Allocator, class Predicate>
0522 typename basic_string<charT, traits, Allocator>::size_type
0523 erase_if(basic_string<charT, traits, Allocator>& c, Predicate pred); // C++20
0524 
0525 typedef basic_string<char>    string;
0526 typedef basic_string<wchar_t> wstring;
0527 typedef basic_string<char8_t> u8string; // C++20
0528 typedef basic_string<char16_t> u16string;
0529 typedef basic_string<char32_t> u32string;
0530 
0531 int                stoi  (const string& str, size_t* idx = nullptr, int base = 10);
0532 long               stol  (const string& str, size_t* idx = nullptr, int base = 10);
0533 unsigned long      stoul (const string& str, size_t* idx = nullptr, int base = 10);
0534 long long          stoll (const string& str, size_t* idx = nullptr, int base = 10);
0535 unsigned long long stoull(const string& str, size_t* idx = nullptr, int base = 10);
0536 
0537 float       stof (const string& str, size_t* idx = nullptr);
0538 double      stod (const string& str, size_t* idx = nullptr);
0539 long double stold(const string& str, size_t* idx = nullptr);
0540 
0541 string to_string(int val);
0542 string to_string(unsigned val);
0543 string to_string(long val);
0544 string to_string(unsigned long val);
0545 string to_string(long long val);
0546 string to_string(unsigned long long val);
0547 string to_string(float val);
0548 string to_string(double val);
0549 string to_string(long double val);
0550 
0551 int                stoi  (const wstring& str, size_t* idx = nullptr, int base = 10);
0552 long               stol  (const wstring& str, size_t* idx = nullptr, int base = 10);
0553 unsigned long      stoul (const wstring& str, size_t* idx = nullptr, int base = 10);
0554 long long          stoll (const wstring& str, size_t* idx = nullptr, int base = 10);
0555 unsigned long long stoull(const wstring& str, size_t* idx = nullptr, int base = 10);
0556 
0557 float       stof (const wstring& str, size_t* idx = nullptr);
0558 double      stod (const wstring& str, size_t* idx = nullptr);
0559 long double stold(const wstring& str, size_t* idx = nullptr);
0560 
0561 wstring to_wstring(int val);
0562 wstring to_wstring(unsigned val);
0563 wstring to_wstring(long val);
0564 wstring to_wstring(unsigned long val);
0565 wstring to_wstring(long long val);
0566 wstring to_wstring(unsigned long long val);
0567 wstring to_wstring(float val);
0568 wstring to_wstring(double val);
0569 wstring to_wstring(long double val);
0570 
0571 template <> struct hash<string>;
0572 template <> struct hash<u8string>; // C++20
0573 template <> struct hash<u16string>;
0574 template <> struct hash<u32string>;
0575 template <> struct hash<wstring>;
0576 
0577 basic_string<char>     operator""s( const char *str,     size_t len );           // C++14, constexpr since C++20
0578 basic_string<wchar_t>  operator""s( const wchar_t *str,  size_t len );           // C++14, constexpr since C++20
0579 constexpr basic_string<char8_t>  operator""s( const char8_t *str,  size_t len ); // C++20
0580 basic_string<char16_t> operator""s( const char16_t *str, size_t len );           // C++14, constexpr since C++20
0581 basic_string<char32_t> operator""s( const char32_t *str, size_t len );           // C++14, constexpr since C++20
0582 
0583 }  // std
0584 
0585 */
0586 
0587 // clang-format on
0588 
0589 #if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
0590 #  include <__cxx03/string>
0591 #else
0592 #  include <__algorithm/max.h>
0593 #  include <__algorithm/min.h>
0594 #  include <__algorithm/remove.h>
0595 #  include <__algorithm/remove_if.h>
0596 #  include <__assert>
0597 #  include <__config>
0598 #  include <__debug_utils/sanitizers.h>
0599 #  include <__format/enable_insertable.h>
0600 #  include <__functional/hash.h>
0601 #  include <__functional/unary_function.h>
0602 #  include <__fwd/string.h>
0603 #  include <__ios/fpos.h>
0604 #  include <__iterator/bounded_iter.h>
0605 #  include <__iterator/distance.h>
0606 #  include <__iterator/iterator_traits.h>
0607 #  include <__iterator/reverse_iterator.h>
0608 #  include <__iterator/wrap_iter.h>
0609 #  include <__memory/addressof.h>
0610 #  include <__memory/allocate_at_least.h>
0611 #  include <__memory/allocator.h>
0612 #  include <__memory/allocator_traits.h>
0613 #  include <__memory/compressed_pair.h>
0614 #  include <__memory/construct_at.h>
0615 #  include <__memory/noexcept_move_assign_container.h>
0616 #  include <__memory/pointer_traits.h>
0617 #  include <__memory/swap_allocator.h>
0618 #  include <__memory_resource/polymorphic_allocator.h>
0619 #  include <__ranges/access.h>
0620 #  include <__ranges/concepts.h>
0621 #  include <__ranges/container_compatible_range.h>
0622 #  include <__ranges/from_range.h>
0623 #  include <__ranges/size.h>
0624 #  include <__string/char_traits.h>
0625 #  include <__string/extern_template_lists.h>
0626 #  include <__type_traits/conditional.h>
0627 #  include <__type_traits/enable_if.h>
0628 #  include <__type_traits/is_allocator.h>
0629 #  include <__type_traits/is_array.h>
0630 #  include <__type_traits/is_convertible.h>
0631 #  include <__type_traits/is_nothrow_assignable.h>
0632 #  include <__type_traits/is_nothrow_constructible.h>
0633 #  include <__type_traits/is_same.h>
0634 #  include <__type_traits/is_standard_layout.h>
0635 #  include <__type_traits/is_trivial.h>
0636 #  include <__type_traits/is_trivially_relocatable.h>
0637 #  include <__type_traits/remove_cvref.h>
0638 #  include <__type_traits/void_t.h>
0639 #  include <__utility/auto_cast.h>
0640 #  include <__utility/declval.h>
0641 #  include <__utility/forward.h>
0642 #  include <__utility/is_pointer_in_range.h>
0643 #  include <__utility/move.h>
0644 #  include <__utility/scope_guard.h>
0645 #  include <__utility/swap.h>
0646 #  include <__utility/unreachable.h>
0647 #  include <climits>
0648 #  include <cstdio> // EOF
0649 #  include <cstring>
0650 #  include <limits>
0651 #  include <stdexcept>
0652 #  include <string_view>
0653 #  include <version>
0654 
0655 #  if _LIBCPP_HAS_WIDE_CHARACTERS
0656 #    include <cwchar>
0657 #  endif
0658 
0659 // standard-mandated includes
0660 
0661 // [iterator.range]
0662 #  include <__iterator/access.h>
0663 #  include <__iterator/data.h>
0664 #  include <__iterator/empty.h>
0665 #  include <__iterator/reverse_access.h>
0666 #  include <__iterator/size.h>
0667 
0668 // [string.syn]
0669 #  include <compare>
0670 #  include <initializer_list>
0671 
0672 #  if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0673 #    pragma GCC system_header
0674 #  endif
0675 
0676 _LIBCPP_PUSH_MACROS
0677 #  include <__undef_macros>
0678 
0679 #  if _LIBCPP_HAS_ASAN && _LIBCPP_INSTRUMENTED_WITH_ASAN
0680 #    define _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS __attribute__((__no_sanitize__("address")))
0681 // This macro disables AddressSanitizer (ASan) instrumentation for a specific function,
0682 // allowing memory accesses that would normally trigger ASan errors to proceed without crashing.
0683 // This is useful for accessing parts of objects memory, which should not be accessed,
0684 // such as unused bytes in short strings, that should never be accessed
0685 // by other parts of the program.
0686 #  else
0687 #    define _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS
0688 #  endif
0689 
0690 _LIBCPP_BEGIN_NAMESPACE_STD
0691 
0692 // basic_string
0693 
0694 template <class _CharT, class _Traits, class _Allocator>
0695 basic_string<_CharT, _Traits, _Allocator> _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
0696 operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const basic_string<_CharT, _Traits, _Allocator>& __y);
0697 
0698 template <class _CharT, class _Traits, class _Allocator>
0699 _LIBCPP_HIDDEN _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
0700 operator+(const _CharT* __x, const basic_string<_CharT, _Traits, _Allocator>& __y);
0701 
0702 template <class _CharT, class _Traits, class _Allocator>
0703 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
0704 operator+(_CharT __x, const basic_string<_CharT, _Traits, _Allocator>& __y);
0705 
0706 template <class _CharT, class _Traits, class _Allocator>
0707 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
0708 operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
0709 
0710 template <class _CharT, class _Traits, class _Allocator>
0711 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
0712 operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
0713 
0714 #  if _LIBCPP_STD_VER >= 26
0715 
0716 template <class _CharT, class _Traits, class _Allocator>
0717 _LIBCPP_HIDE_FROM_ABI constexpr basic_string<_CharT, _Traits, _Allocator>
0718 operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
0719           type_identity_t<basic_string_view<_CharT, _Traits>> __rhs);
0720 
0721 template <class _CharT, class _Traits, class _Allocator>
0722 _LIBCPP_HIDE_FROM_ABI constexpr basic_string<_CharT, _Traits, _Allocator>
0723 operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, type_identity_t<basic_string_view<_CharT, _Traits>> __rhs);
0724 
0725 template <class _CharT, class _Traits, class _Allocator>
0726 _LIBCPP_HIDE_FROM_ABI constexpr basic_string<_CharT, _Traits, _Allocator>
0727 operator+(type_identity_t<basic_string_view<_CharT, _Traits>> __lhs,
0728           const basic_string<_CharT, _Traits, _Allocator>& __rhs);
0729 
0730 template <class _CharT, class _Traits, class _Allocator>
0731 _LIBCPP_HIDE_FROM_ABI constexpr basic_string<_CharT, _Traits, _Allocator>
0732 operator+(type_identity_t<basic_string_view<_CharT, _Traits>> __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs);
0733 
0734 #  endif
0735 
0736 extern template _LIBCPP_EXPORTED_FROM_ABI string operator+
0737     <char, char_traits<char>, allocator<char> >(char const*, string const&);
0738 
0739 template <class _Iter>
0740 struct __string_is_trivial_iterator : public false_type {};
0741 
0742 template <class _Tp>
0743 struct __string_is_trivial_iterator<_Tp*> : public is_arithmetic<_Tp> {};
0744 
0745 template <class _Iter>
0746 struct __string_is_trivial_iterator<__wrap_iter<_Iter> > : public __string_is_trivial_iterator<_Iter> {};
0747 
0748 template <class _CharT, class _Traits, class _Tp>
0749 struct __can_be_converted_to_string_view
0750     : public _BoolConstant< is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
0751                             !is_convertible<const _Tp&, const _CharT*>::value > {};
0752 
0753 struct __uninitialized_size_tag {};
0754 struct __init_with_sentinel_tag {};
0755 
0756 template <size_t _PaddingSize>
0757 struct __padding {
0758   char __padding_[_PaddingSize];
0759 };
0760 
0761 template <>
0762 struct __padding<0> {};
0763 
0764 template <class _CharT, class _Traits, class _Allocator>
0765 class basic_string {
0766 private:
0767   using __default_allocator_type _LIBCPP_NODEBUG = allocator<_CharT>;
0768 
0769 public:
0770   typedef basic_string __self;
0771   typedef basic_string_view<_CharT, _Traits> __self_view;
0772   typedef _Traits traits_type;
0773   typedef _CharT value_type;
0774   typedef _Allocator allocator_type;
0775   typedef allocator_traits<allocator_type> __alloc_traits;
0776   typedef typename __alloc_traits::size_type size_type;
0777   typedef typename __alloc_traits::difference_type difference_type;
0778   typedef value_type& reference;
0779   typedef const value_type& const_reference;
0780   typedef typename __alloc_traits::pointer pointer;
0781   typedef typename __alloc_traits::const_pointer const_pointer;
0782 
0783   // A basic_string contains the following members which may be trivially relocatable:
0784   // - pointer: is currently assumed to be trivially relocatable, but is still checked in case that changes
0785   // - size_type: is always trivially relocatable, since it has to be an integral type
0786   // - value_type: is always trivially relocatable, since it has to be trivial
0787   // - unsigned char: is a fundamental type, so it's trivially relocatable
0788   // - allocator_type: may or may not be trivially relocatable, so it's checked
0789   //
0790   // This string implementation doesn't contain any references into itself. It only contains a bit that says whether
0791   // it is in small or large string mode, so the entire structure is trivially relocatable if its members are.
0792 #  if _LIBCPP_HAS_ASAN && _LIBCPP_INSTRUMENTED_WITH_ASAN
0793   // When compiling with AddressSanitizer (ASan), basic_string cannot be trivially
0794   // relocatable. Because the object's memory might be poisoned when its content
0795   // is kept inside objects memory (short string optimization), instead of in allocated
0796   // external memory. In such cases, the destructor is responsible for unpoisoning
0797   // the memory to avoid triggering false positives.
0798   // Therefore it's crucial to ensure the destructor is called.
0799   using __trivially_relocatable = void;
0800 #  else
0801   using __trivially_relocatable _LIBCPP_NODEBUG = __conditional_t<
0802       __libcpp_is_trivially_relocatable<allocator_type>::value && __libcpp_is_trivially_relocatable<pointer>::value,
0803       basic_string,
0804       void>;
0805 #  endif
0806 
0807 #  if _LIBCPP_HAS_ASAN && _LIBCPP_INSTRUMENTED_WITH_ASAN
0808   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pointer __asan_volatile_wrapper(pointer const& __ptr) const {
0809     if (__libcpp_is_constant_evaluated())
0810       return __ptr;
0811 
0812     pointer volatile __copy_ptr = __ptr;
0813 
0814     return const_cast<pointer&>(__copy_ptr);
0815   }
0816 
0817   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_pointer
0818   __asan_volatile_wrapper(const_pointer const& __ptr) const {
0819     if (__libcpp_is_constant_evaluated())
0820       return __ptr;
0821 
0822     const_pointer volatile __copy_ptr = __ptr;
0823 
0824     return const_cast<const_pointer&>(__copy_ptr);
0825   }
0826 #    define _LIBCPP_ASAN_VOLATILE_WRAPPER(PTR) __asan_volatile_wrapper(PTR)
0827 #  else
0828 #    define _LIBCPP_ASAN_VOLATILE_WRAPPER(PTR) PTR
0829 #  endif
0830 
0831   static_assert(!is_array<value_type>::value, "Character type of basic_string must not be an array");
0832   static_assert(is_standard_layout<value_type>::value, "Character type of basic_string must be standard-layout");
0833   static_assert(is_trivial<value_type>::value, "Character type of basic_string must be trivial");
0834   static_assert(is_same<_CharT, typename traits_type::char_type>::value,
0835                 "traits_type::char_type must be the same type as CharT");
0836   static_assert(is_same<typename allocator_type::value_type, value_type>::value,
0837                 "Allocator::value_type must be same type as value_type");
0838   static_assert(__check_valid_allocator<allocator_type>::value, "");
0839 
0840 #  ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_STRING
0841   // Users might provide custom allocators, and prior to C++20 we have no existing way to detect whether the allocator's
0842   // pointer type is contiguous (though it has to be by the Standard). Using the wrapper type ensures the iterator is
0843   // considered contiguous.
0844   typedef __bounded_iter<__wrap_iter<pointer> > iterator;
0845   typedef __bounded_iter<__wrap_iter<const_pointer> > const_iterator;
0846 #  else
0847   typedef __wrap_iter<pointer> iterator;
0848   typedef __wrap_iter<const_pointer> const_iterator;
0849 #  endif
0850   typedef std::reverse_iterator<iterator> reverse_iterator;
0851   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
0852 
0853 private:
0854   static_assert(CHAR_BIT == 8, "This implementation assumes that one byte contains 8 bits");
0855 
0856 #  ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
0857 
0858   struct __long {
0859     pointer __data_;
0860     size_type __size_;
0861     size_type __cap_ : sizeof(size_type) * CHAR_BIT - 1;
0862     size_type __is_long_ : 1;
0863   };
0864 
0865   enum { __min_cap = (sizeof(__long) - 1) / sizeof(value_type) > 2 ? (sizeof(__long) - 1) / sizeof(value_type) : 2 };
0866 
0867   struct __short {
0868     value_type __data_[__min_cap];
0869     _LIBCPP_NO_UNIQUE_ADDRESS __padding<sizeof(value_type) - 1> __padding_;
0870     unsigned char __size_    : 7;
0871     unsigned char __is_long_ : 1;
0872   };
0873 
0874   // The __endian_factor is required because the field we use to store the size
0875   // has one fewer bit than it would if it were not a bitfield.
0876   //
0877   // If the LSB is used to store the short-flag in the short string representation,
0878   // we have to multiply the size by two when it is stored and divide it by two when
0879   // it is loaded to make sure that we always store an even number. In the long string
0880   // representation, we can ignore this because we can assume that we always allocate
0881   // an even amount of value_types.
0882   //
0883   // If the MSB is used for the short-flag, the max_size() is numeric_limits<size_type>::max() / 2.
0884   // This does not impact the short string representation, since we never need the MSB
0885   // for representing the size of a short string anyway.
0886 
0887 #    ifdef _LIBCPP_BIG_ENDIAN
0888   static const size_type __endian_factor = 2;
0889 #    else
0890   static const size_type __endian_factor = 1;
0891 #    endif
0892 
0893 #  else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
0894 
0895 #    ifdef _LIBCPP_BIG_ENDIAN
0896   static const size_type __endian_factor = 1;
0897 #    else
0898   static const size_type __endian_factor = 2;
0899 #    endif
0900 
0901   // Attribute 'packed' is used to keep the layout compatible with the
0902   // previous definition that did not use bit fields. This is because on
0903   // some platforms bit fields have a default size rather than the actual
0904   // size used, e.g., it is 4 bytes on AIX. See D128285 for details.
0905   struct __long {
0906     struct _LIBCPP_PACKED {
0907       size_type __is_long_ : 1;
0908       size_type __cap_ : sizeof(size_type) * CHAR_BIT - 1;
0909     };
0910     size_type __size_;
0911     pointer __data_;
0912   };
0913 
0914   enum { __min_cap = (sizeof(__long) - 1) / sizeof(value_type) > 2 ? (sizeof(__long) - 1) / sizeof(value_type) : 2 };
0915 
0916   struct __short {
0917     struct _LIBCPP_PACKED {
0918       unsigned char __is_long_ : 1;
0919       unsigned char __size_    : 7;
0920     };
0921     _LIBCPP_NO_UNIQUE_ADDRESS __padding<sizeof(value_type) - 1> __padding_;
0922     value_type __data_[__min_cap];
0923   };
0924 
0925 #  endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
0926 
0927   static_assert(sizeof(__short) == (sizeof(value_type) * (__min_cap + 1)), "__short has an unexpected size.");
0928 
0929   union __rep {
0930     __short __s;
0931     __long __l;
0932   };
0933 
0934   _LIBCPP_COMPRESSED_PAIR(__rep, __rep_, allocator_type, __alloc_);
0935 
0936   // annotate the string with its size() at scope exit. The string has to be in a valid state at that point.
0937   struct __annotate_new_size {
0938     basic_string& __str_;
0939 
0940     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __annotate_new_size(basic_string& __str) : __str_(__str) {}
0941 
0942     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void operator()() { __str_.__annotate_new(__str_.size()); }
0943   };
0944 
0945   // Construct a string with the given allocator and enough storage to hold `__size` characters, but
0946   // don't initialize the characters. The contents of the string, including the null terminator, must be
0947   // initialized separately.
0948   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit basic_string(
0949       __uninitialized_size_tag, size_type __size, const allocator_type& __a)
0950       : __alloc_(__a) {
0951     if (__size > max_size())
0952       __throw_length_error();
0953     if (__fits_in_sso(__size)) {
0954       __rep_ = __rep();
0955       __set_short_size(__size);
0956     } else {
0957       auto __capacity   = __recommend(__size) + 1;
0958       auto __allocation = __alloc_traits::allocate(__alloc_, __capacity);
0959       __begin_lifetime(__allocation, __capacity);
0960       __set_long_cap(__capacity);
0961       __set_long_pointer(__allocation);
0962       __set_long_size(__size);
0963     }
0964     __annotate_new(__size);
0965   }
0966 
0967   template <class _Iter, class _Sent>
0968   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
0969   basic_string(__init_with_sentinel_tag, _Iter __first, _Sent __last, const allocator_type& __a)
0970       : __alloc_(__a) {
0971     __init_with_sentinel(std::move(__first), std::move(__last));
0972   }
0973 
0974   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator __make_iterator(pointer __p) {
0975 #  ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_STRING
0976     // Bound the iterator according to the size (and not the capacity, unlike vector).
0977     //
0978     // By the Standard, string iterators are generally not guaranteed to stay valid when the container is modified,
0979     // regardless of whether reallocation occurs. This allows us to check for out-of-bounds accesses using logical size,
0980     // a stricter check, since correct code can never rely on being able to access newly-added elements via an existing
0981     // iterator.
0982     return std::__make_bounded_iter(
0983         std::__wrap_iter<pointer>(__p),
0984         std::__wrap_iter<pointer>(__get_pointer()),
0985         std::__wrap_iter<pointer>(__get_pointer() + size()));
0986 #  else
0987     return iterator(__p);
0988 #  endif // _LIBCPP_ABI_BOUNDED_ITERATORS_IN_STRING
0989   }
0990 
0991   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_iterator __make_const_iterator(const_pointer __p) const {
0992 #  ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_STRING
0993     // Bound the iterator according to the size (and not the capacity, unlike vector).
0994     return std::__make_bounded_iter(
0995         std::__wrap_iter<const_pointer>(__p),
0996         std::__wrap_iter<const_pointer>(__get_pointer()),
0997         std::__wrap_iter<const_pointer>(__get_pointer() + size()));
0998 #  else
0999     return const_iterator(__p);
1000 #  endif // _LIBCPP_ABI_BOUNDED_ITERATORS_IN_STRING
1001   }
1002 
1003 public:
1004   _LIBCPP_TEMPLATE_DATA_VIS static const size_type npos = -1;
1005 
1006   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string()
1007       _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
1008       : __rep_() {
1009     __annotate_new(0);
1010   }
1011 
1012   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit basic_string(const allocator_type& __a)
1013 #  if _LIBCPP_STD_VER <= 14
1014       _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1015 #  else
1016       _NOEXCEPT
1017 #  endif
1018       : __rep_(), __alloc_(__a) {
1019     __annotate_new(0);
1020   }
1021 
1022   _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS basic_string(const basic_string& __str)
1023       : __alloc_(__alloc_traits::select_on_container_copy_construction(__str.__alloc_)) {
1024     if (!__str.__is_long()) {
1025       __rep_ = __str.__rep_;
1026       __annotate_new(__get_short_size());
1027     } else
1028       __init_copy_ctor_external(std::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
1029   }
1030 
1031   _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS
1032   basic_string(const basic_string& __str, const allocator_type& __a)
1033       : __alloc_(__a) {
1034     if (!__str.__is_long()) {
1035       __rep_ = __str.__rep_;
1036       __annotate_new(__get_short_size());
1037     } else
1038       __init_copy_ctor_external(std::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
1039   }
1040 
1041 #  ifndef _LIBCPP_CXX03_LANG
1042   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(basic_string&& __str)
1043 #    if _LIBCPP_STD_VER <= 14
1044       _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
1045 #    else
1046       _NOEXCEPT
1047 #    endif
1048       // Turning off ASan instrumentation for variable initialization with _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS
1049       // does not work consistently during initialization of __r_, so we instead unpoison __str's memory manually first.
1050       // __str's memory needs to be unpoisoned only in the case where it's a short string.
1051       : __rep_([](basic_string& __s) -> decltype(__s.__rep_)&& {
1052           if (!__s.__is_long())
1053             __s.__annotate_delete();
1054           return std::move(__s.__rep_);
1055         }(__str)),
1056         __alloc_(std::move(__str.__alloc_)) {
1057     __str.__rep_ = __rep();
1058     __str.__annotate_new(0);
1059     if (!__is_long())
1060       __annotate_new(size());
1061   }
1062 
1063   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(basic_string&& __str, const allocator_type& __a)
1064       : __alloc_(__a) {
1065     if (__str.__is_long() && __a != __str.__alloc_) // copy, not move
1066       __init(std::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
1067     else {
1068       if (__libcpp_is_constant_evaluated())
1069         __rep_ = __rep();
1070       if (!__str.__is_long())
1071         __str.__annotate_delete();
1072       __rep_       = __str.__rep_;
1073       __str.__rep_ = __rep();
1074       __str.__annotate_new(0);
1075       if (!__is_long() && this != std::addressof(__str))
1076         __annotate_new(size());
1077     }
1078   }
1079 #  endif // _LIBCPP_CXX03_LANG
1080 
1081   template <__enable_if_t<__is_allocator<_Allocator>::value, int> = 0>
1082   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const _CharT* __s) {
1083     _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "basic_string(const char*) detected nullptr");
1084     __init(__s, traits_type::length(__s));
1085   }
1086 
1087   template <__enable_if_t<__is_allocator<_Allocator>::value, int> = 0>
1088   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const _CharT* __s, const _Allocator& __a)
1089       : __alloc_(__a) {
1090     _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
1091     __init(__s, traits_type::length(__s));
1092   }
1093 
1094 #  if _LIBCPP_STD_VER >= 23
1095   basic_string(nullptr_t) = delete;
1096 #  endif
1097 
1098   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const _CharT* __s, size_type __n) {
1099     _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
1100     __init(__s, __n);
1101   }
1102 
1103   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
1104   basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
1105       : __alloc_(__a) {
1106     _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
1107     __init(__s, __n);
1108   }
1109 
1110   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(size_type __n, _CharT __c) { __init(__n, __c); }
1111 
1112 #  if _LIBCPP_STD_VER >= 23
1113   _LIBCPP_HIDE_FROM_ABI constexpr basic_string(
1114       basic_string&& __str, size_type __pos, const _Allocator& __alloc = _Allocator())
1115       : basic_string(std::move(__str), __pos, npos, __alloc) {}
1116 
1117   _LIBCPP_HIDE_FROM_ABI constexpr basic_string(
1118       basic_string&& __str, size_type __pos, size_type __n, const _Allocator& __alloc = _Allocator())
1119       : __alloc_(__alloc) {
1120     if (__pos > __str.size())
1121       __throw_out_of_range();
1122 
1123     auto __len = std::min<size_type>(__n, __str.size() - __pos);
1124     if (__alloc_traits::is_always_equal::value || __alloc == __str.__alloc_) {
1125       __move_assign(std::move(__str), __pos, __len);
1126     } else {
1127       // Perform a copy because the allocators are not compatible.
1128       __init(__str.data() + __pos, __len);
1129     }
1130   }
1131 #  endif
1132 
1133   template <__enable_if_t<__is_allocator<_Allocator>::value, int> = 0>
1134   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(size_type __n, _CharT __c, const _Allocator& __a)
1135       : __alloc_(__a) {
1136     __init(__n, __c);
1137   }
1138 
1139   _LIBCPP_CONSTEXPR_SINCE_CXX20
1140   basic_string(const basic_string& __str, size_type __pos, size_type __n, const _Allocator& __a = _Allocator())
1141       : __alloc_(__a) {
1142     size_type __str_sz = __str.size();
1143     if (__pos > __str_sz)
1144       __throw_out_of_range();
1145     __init(__str.data() + __pos, std::min(__n, __str_sz - __pos));
1146   }
1147 
1148   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
1149   basic_string(const basic_string& __str, size_type __pos, const _Allocator& __a = _Allocator())
1150       : __alloc_(__a) {
1151     size_type __str_sz = __str.size();
1152     if (__pos > __str_sz)
1153       __throw_out_of_range();
1154     __init(__str.data() + __pos, __str_sz - __pos);
1155   }
1156 
1157   template <class _Tp,
1158             __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
1159                               !__is_same_uncvref<_Tp, basic_string>::value,
1160                           int> = 0>
1161   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
1162   basic_string(const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a = allocator_type())
1163       : __alloc_(__a) {
1164     __self_view __sv0 = __t;
1165     __self_view __sv  = __sv0.substr(__pos, __n);
1166     __init(__sv.data(), __sv.size());
1167   }
1168 
1169   template <class _Tp,
1170             __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
1171                               !__is_same_uncvref<_Tp, basic_string>::value,
1172                           int> = 0>
1173   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1174   _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit basic_string(const _Tp& __t) {
1175     __self_view __sv = __t;
1176     __init(__sv.data(), __sv.size());
1177   }
1178 
1179   template <class _Tp,
1180             __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
1181                               !__is_same_uncvref<_Tp, basic_string>::value,
1182                           int> = 0>
1183   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1184   _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit basic_string(const _Tp& __t, const allocator_type& __a)
1185       : __alloc_(__a) {
1186     __self_view __sv = __t;
1187     __init(__sv.data(), __sv.size());
1188   }
1189 
1190   template <class _InputIterator, __enable_if_t<__has_input_iterator_category<_InputIterator>::value, int> = 0>
1191   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(_InputIterator __first, _InputIterator __last) {
1192     __init(__first, __last);
1193   }
1194 
1195   template <class _InputIterator, __enable_if_t<__has_input_iterator_category<_InputIterator>::value, int> = 0>
1196   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
1197   basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a)
1198       : __alloc_(__a) {
1199     __init(__first, __last);
1200   }
1201 
1202 #  if _LIBCPP_STD_VER >= 23
1203   template <_ContainerCompatibleRange<_CharT> _Range>
1204   _LIBCPP_HIDE_FROM_ABI constexpr basic_string(
1205       from_range_t, _Range&& __range, const allocator_type& __a = allocator_type())
1206       : __alloc_(__a) {
1207     if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
1208       __init_with_size(ranges::begin(__range), ranges::end(__range), ranges::distance(__range));
1209     } else {
1210       __init_with_sentinel(ranges::begin(__range), ranges::end(__range));
1211     }
1212   }
1213 #  endif
1214 
1215 #  ifndef _LIBCPP_CXX03_LANG
1216   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(initializer_list<_CharT> __il) {
1217     __init(__il.begin(), __il.end());
1218   }
1219 
1220   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(initializer_list<_CharT> __il, const _Allocator& __a)
1221       : __alloc_(__a) {
1222     __init(__il.begin(), __il.end());
1223   }
1224 #  endif // _LIBCPP_CXX03_LANG
1225 
1226   inline _LIBCPP_CONSTEXPR_SINCE_CXX20 ~basic_string() {
1227     __annotate_delete();
1228     if (__is_long())
1229       __alloc_traits::deallocate(__alloc_, __get_long_pointer(), __get_long_cap());
1230   }
1231 
1232   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 operator __self_view() const _NOEXCEPT {
1233     return __self_view(typename __self_view::__assume_valid(), data(), size());
1234   }
1235 
1236   _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS basic_string&
1237   operator=(const basic_string& __str);
1238 
1239   template <class _Tp,
1240             __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
1241                               !__is_same_uncvref<_Tp, basic_string>::value,
1242                           int> = 0>
1243   _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator=(const _Tp& __t) {
1244     __self_view __sv = __t;
1245     return assign(__sv);
1246   }
1247 
1248 #  ifndef _LIBCPP_CXX03_LANG
1249   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1250   operator=(basic_string&& __str) noexcept(__noexcept_move_assign_container<_Allocator, __alloc_traits>::value) {
1251     __move_assign(__str, integral_constant<bool, __alloc_traits::propagate_on_container_move_assignment::value>());
1252     return *this;
1253   }
1254 
1255   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator=(initializer_list<value_type> __il) {
1256     return assign(__il.begin(), __il.size());
1257   }
1258 #  endif
1259   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator=(const value_type* __s) {
1260     return assign(__s);
1261   }
1262 #  if _LIBCPP_STD_VER >= 23
1263   basic_string& operator=(nullptr_t) = delete;
1264 #  endif
1265   _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator=(value_type __c);
1266 
1267   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator begin() _NOEXCEPT {
1268     return __make_iterator(__get_pointer());
1269   }
1270   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_iterator begin() const _NOEXCEPT {
1271     return __make_const_iterator(__get_pointer());
1272   }
1273   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator end() _NOEXCEPT {
1274     return __make_iterator(__get_pointer() + size());
1275   }
1276   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_iterator end() const _NOEXCEPT {
1277     return __make_const_iterator(__get_pointer() + size());
1278   }
1279 
1280   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reverse_iterator rbegin() _NOEXCEPT {
1281     return reverse_iterator(end());
1282   }
1283   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reverse_iterator rbegin() const _NOEXCEPT {
1284     return const_reverse_iterator(end());
1285   }
1286   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reverse_iterator rend() _NOEXCEPT {
1287     return reverse_iterator(begin());
1288   }
1289   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reverse_iterator rend() const _NOEXCEPT {
1290     return const_reverse_iterator(begin());
1291   }
1292 
1293   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_iterator cbegin() const _NOEXCEPT { return begin(); }
1294   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_iterator cend() const _NOEXCEPT { return end(); }
1295   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reverse_iterator crbegin() const _NOEXCEPT {
1296     return rbegin();
1297   }
1298   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reverse_iterator crend() const _NOEXCEPT { return rend(); }
1299 
1300   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type size() const _NOEXCEPT {
1301     return __is_long() ? __get_long_size() : __get_short_size();
1302   }
1303   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type length() const _NOEXCEPT { return size(); }
1304 
1305   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type max_size() const _NOEXCEPT {
1306     size_type __m = __alloc_traits::max_size(__alloc_);
1307     if (__m <= std::numeric_limits<size_type>::max() / 2) {
1308       return __m - __alignment;
1309     } else {
1310       bool __uses_lsb = __endian_factor == 2;
1311       return __uses_lsb ? __m - __alignment : (__m / 2) - __alignment;
1312     }
1313   }
1314 
1315   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type capacity() const _NOEXCEPT {
1316     return (__is_long() ? __get_long_cap() : static_cast<size_type>(__min_cap)) - 1;
1317   }
1318 
1319   _LIBCPP_CONSTEXPR_SINCE_CXX20 void resize(size_type __n, value_type __c);
1320   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void resize(size_type __n) { resize(__n, value_type()); }
1321 
1322   _LIBCPP_CONSTEXPR_SINCE_CXX20 void reserve(size_type __requested_capacity);
1323 
1324 #  if _LIBCPP_STD_VER >= 23
1325   template <class _Op>
1326   _LIBCPP_HIDE_FROM_ABI constexpr void resize_and_overwrite(size_type __n, _Op __op) {
1327     __resize_default_init(__n);
1328     __erase_to_end(std::move(__op)(data(), _LIBCPP_AUTO_CAST(__n)));
1329   }
1330 #  endif
1331 
1332   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __resize_default_init(size_type __n);
1333 
1334 #  if _LIBCPP_STD_VER < 26 || defined(_LIBCPP_ENABLE_CXX26_REMOVED_STRING_RESERVE)
1335   _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_HIDE_FROM_ABI void reserve() _NOEXCEPT { shrink_to_fit(); }
1336 #  endif
1337   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void shrink_to_fit() _NOEXCEPT;
1338   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void clear() _NOEXCEPT;
1339 
1340   [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool empty() const _NOEXCEPT {
1341     return size() == 0;
1342   }
1343 
1344   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference operator[](size_type __pos) const _NOEXCEPT {
1345     _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__pos <= size(), "string index out of bounds");
1346     if (__builtin_constant_p(__pos) && !__fits_in_sso(__pos)) {
1347       return *(__get_long_pointer() + __pos);
1348     }
1349     return *(data() + __pos);
1350   }
1351 
1352   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference operator[](size_type __pos) _NOEXCEPT {
1353     _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__pos <= size(), "string index out of bounds");
1354     if (__builtin_constant_p(__pos) && !__fits_in_sso(__pos)) {
1355       return *(__get_long_pointer() + __pos);
1356     }
1357     return *(__get_pointer() + __pos);
1358   }
1359 
1360   _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference at(size_type __n) const;
1361   _LIBCPP_CONSTEXPR_SINCE_CXX20 reference at(size_type __n);
1362 
1363   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator+=(const basic_string& __str) {
1364     return append(__str);
1365   }
1366 
1367   template <class _Tp,
1368             __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
1369                               !__is_same_uncvref<_Tp, basic_string >::value,
1370                           int> = 0>
1371   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1372   operator+=(const _Tp& __t) {
1373     __self_view __sv = __t;
1374     return append(__sv);
1375   }
1376 
1377   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator+=(const value_type* __s) {
1378     return append(__s);
1379   }
1380 
1381   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator+=(value_type __c) {
1382     push_back(__c);
1383     return *this;
1384   }
1385 
1386 #  ifndef _LIBCPP_CXX03_LANG
1387   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& operator+=(initializer_list<value_type> __il) {
1388     return append(__il);
1389   }
1390 #  endif // _LIBCPP_CXX03_LANG
1391 
1392   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(const basic_string& __str) {
1393     return append(__str.data(), __str.size());
1394   }
1395 
1396   template <class _Tp,
1397             __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
1398                               !__is_same_uncvref<_Tp, basic_string>::value,
1399                           int> = 0>
1400   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1401   append(const _Tp& __t) {
1402     __self_view __sv = __t;
1403     return append(__sv.data(), __sv.size());
1404   }
1405 
1406   _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(const basic_string& __str, size_type __pos, size_type __n = npos);
1407 
1408   template <class _Tp,
1409             __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
1410                               !__is_same_uncvref<_Tp, basic_string>::value,
1411                           int> = 0>
1412   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
1413 
1414   basic_string&
1415   append(const _Tp& __t, size_type __pos, size_type __n = npos);
1416 
1417   _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(const value_type* __s, size_type __n);
1418   _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(const value_type* __s);
1419   _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(size_type __n, value_type __c);
1420 
1421   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __append_default_init(size_type __n);
1422 
1423   template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> = 0>
1424   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1425   append(_InputIterator __first, _InputIterator __last) {
1426     const basic_string __temp(__first, __last, __alloc_);
1427     append(__temp.data(), __temp.size());
1428     return *this;
1429   }
1430 
1431   template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> = 0>
1432   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1433   append(_ForwardIterator __first, _ForwardIterator __last);
1434 
1435 #  if _LIBCPP_STD_VER >= 23
1436   template <_ContainerCompatibleRange<_CharT> _Range>
1437   _LIBCPP_HIDE_FROM_ABI constexpr basic_string& append_range(_Range&& __range) {
1438     insert_range(end(), std::forward<_Range>(__range));
1439     return *this;
1440   }
1441 #  endif
1442 
1443 #  ifndef _LIBCPP_CXX03_LANG
1444   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& append(initializer_list<value_type> __il) {
1445     return append(__il.begin(), __il.size());
1446   }
1447 #  endif // _LIBCPP_CXX03_LANG
1448 
1449   _LIBCPP_CONSTEXPR_SINCE_CXX20 void push_back(value_type __c);
1450   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void pop_back();
1451 
1452   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference front() _NOEXCEPT {
1453     _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "string::front(): string is empty");
1454     return *__get_pointer();
1455   }
1456 
1457   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference front() const _NOEXCEPT {
1458     _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "string::front(): string is empty");
1459     return *data();
1460   }
1461 
1462   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference back() _NOEXCEPT {
1463     _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "string::back(): string is empty");
1464     return *(__get_pointer() + size() - 1);
1465   }
1466 
1467   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference back() const _NOEXCEPT {
1468     _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "string::back(): string is empty");
1469     return *(data() + size() - 1);
1470   }
1471 
1472   template <class _Tp, __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, int> = 0>
1473   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1474   assign(const _Tp& __t) {
1475     __self_view __sv = __t;
1476     return assign(__sv.data(), __sv.size());
1477   }
1478 
1479 #  if _LIBCPP_STD_VER >= 20
1480   _LIBCPP_HIDE_FROM_ABI constexpr void __move_assign(basic_string&& __str, size_type __pos, size_type __len) {
1481     // Pilfer the allocation from __str.
1482     _LIBCPP_ASSERT_INTERNAL(__alloc_ == __str.__alloc_, "__move_assign called with wrong allocator");
1483     size_type __old_sz = __str.size();
1484     if (!__str.__is_long())
1485       __str.__annotate_delete();
1486     __rep_       = __str.__rep_;
1487     __str.__rep_ = __rep();
1488     __str.__annotate_new(0);
1489 
1490     _Traits::move(data(), data() + __pos, __len);
1491     __set_size(__len);
1492     _Traits::assign(data()[__len], value_type());
1493 
1494     if (!__is_long()) {
1495       __annotate_new(__len);
1496     } else if (__old_sz > __len) {
1497       __annotate_shrink(__old_sz);
1498     }
1499   }
1500 #  endif
1501 
1502   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(const basic_string& __str) {
1503     return *this = __str;
1504   }
1505 #  ifndef _LIBCPP_CXX03_LANG
1506   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1507   assign(basic_string&& __str) noexcept(__noexcept_move_assign_container<_Allocator, __alloc_traits>::value) {
1508     *this = std::move(__str);
1509     return *this;
1510   }
1511 #  endif
1512   _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n = npos);
1513 
1514   template <class _Tp,
1515             __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
1516                               !__is_same_uncvref<_Tp, basic_string>::value,
1517                           int> = 0>
1518   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1519   assign(const _Tp& __t, size_type __pos, size_type __n = npos);
1520 
1521   _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(const value_type* __s, size_type __n);
1522   _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(const value_type* __s);
1523   _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(size_type __n, value_type __c);
1524   template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> = 0>
1525   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1526   assign(_InputIterator __first, _InputIterator __last);
1527 
1528   template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> = 0>
1529   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1530   assign(_ForwardIterator __first, _ForwardIterator __last);
1531 
1532 #  if _LIBCPP_STD_VER >= 23
1533   template <_ContainerCompatibleRange<_CharT> _Range>
1534   _LIBCPP_HIDE_FROM_ABI constexpr basic_string& assign_range(_Range&& __range) {
1535     if constexpr (__string_is_trivial_iterator<ranges::iterator_t<_Range>>::value &&
1536                   (ranges::forward_range<_Range> || ranges::sized_range<_Range>)) {
1537       size_type __n = static_cast<size_type>(ranges::distance(__range));
1538       __assign_trivial(ranges::begin(__range), ranges::end(__range), __n);
1539 
1540     } else {
1541       __assign_with_sentinel(ranges::begin(__range), ranges::end(__range));
1542     }
1543 
1544     return *this;
1545   }
1546 #  endif
1547 
1548 #  ifndef _LIBCPP_CXX03_LANG
1549   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& assign(initializer_list<value_type> __il) {
1550     return assign(__il.begin(), __il.size());
1551   }
1552 #  endif // _LIBCPP_CXX03_LANG
1553 
1554   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1555   insert(size_type __pos1, const basic_string& __str) {
1556     return insert(__pos1, __str.data(), __str.size());
1557   }
1558 
1559   template <class _Tp, __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, int> = 0>
1560   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1561   insert(size_type __pos1, const _Tp& __t) {
1562     __self_view __sv = __t;
1563     return insert(__pos1, __sv.data(), __sv.size());
1564   }
1565 
1566   template <class _Tp,
1567             __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
1568                               !__is_same_uncvref<_Tp, basic_string>::value,
1569                           int> = 0>
1570   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1571   insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n = npos);
1572 
1573   _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1574   insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n = npos);
1575   _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1576   _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& insert(size_type __pos, const value_type* __s);
1577   _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1578   _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator insert(const_iterator __pos, value_type __c);
1579 
1580 #  if _LIBCPP_STD_VER >= 23
1581   template <_ContainerCompatibleRange<_CharT> _Range>
1582   _LIBCPP_HIDE_FROM_ABI constexpr iterator insert_range(const_iterator __position, _Range&& __range) {
1583     if constexpr (ranges::forward_range<_Range> || ranges::sized_range<_Range>) {
1584       auto __n = static_cast<size_type>(ranges::distance(__range));
1585       return __insert_with_size(__position, ranges::begin(__range), ranges::end(__range), __n);
1586 
1587     } else {
1588       basic_string __temp(from_range, std::forward<_Range>(__range), __alloc_);
1589       return insert(__position, __temp.data(), __temp.data() + __temp.size());
1590     }
1591   }
1592 #  endif
1593 
1594   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator
1595   insert(const_iterator __pos, size_type __n, value_type __c) {
1596     difference_type __p = __pos - begin();
1597     insert(static_cast<size_type>(__p), __n, __c);
1598     return begin() + __p;
1599   }
1600 
1601   template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> = 0>
1602   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator
1603   insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1604 
1605   template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> = 0>
1606   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator
1607   insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
1608 
1609 #  ifndef _LIBCPP_CXX03_LANG
1610   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator
1611   insert(const_iterator __pos, initializer_list<value_type> __il) {
1612     return insert(__pos, __il.begin(), __il.end());
1613   }
1614 #  endif // _LIBCPP_CXX03_LANG
1615 
1616   _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& erase(size_type __pos = 0, size_type __n = npos);
1617   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator erase(const_iterator __pos);
1618   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator erase(const_iterator __first, const_iterator __last);
1619 
1620   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1621   replace(size_type __pos1, size_type __n1, const basic_string& __str) {
1622     return replace(__pos1, __n1, __str.data(), __str.size());
1623   }
1624 
1625   template <class _Tp, __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, int> = 0>
1626   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1627   replace(size_type __pos1, size_type __n1, const _Tp& __t) {
1628     __self_view __sv = __t;
1629     return replace(__pos1, __n1, __sv.data(), __sv.size());
1630   }
1631 
1632   _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1633   replace(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2 = npos);
1634 
1635   template <class _Tp,
1636             __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
1637                               !__is_same_uncvref<_Tp, basic_string>::value,
1638                           int> = 0>
1639   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1640   replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2 = npos);
1641 
1642   _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1643   replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
1644   _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
1645   _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
1646 
1647   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1648   replace(const_iterator __i1, const_iterator __i2, const basic_string& __str) {
1649     return replace(
1650         static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __str.data(), __str.size());
1651   }
1652 
1653   template <class _Tp, __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, int> = 0>
1654   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1655   replace(const_iterator __i1, const_iterator __i2, const _Tp& __t) {
1656     __self_view __sv = __t;
1657     return replace(__i1 - begin(), __i2 - __i1, __sv);
1658   }
1659 
1660   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1661   replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n) {
1662     return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
1663   }
1664 
1665   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1666   replace(const_iterator __i1, const_iterator __i2, const value_type* __s) {
1667     return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
1668   }
1669 
1670   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1671   replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c) {
1672     return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
1673   }
1674 
1675   template <class _InputIterator, __enable_if_t<__has_input_iterator_category<_InputIterator>::value, int> = 0>
1676   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1677   replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
1678 
1679 #  if _LIBCPP_STD_VER >= 23
1680   template <_ContainerCompatibleRange<_CharT> _Range>
1681   _LIBCPP_HIDE_FROM_ABI constexpr basic_string&
1682   replace_with_range(const_iterator __i1, const_iterator __i2, _Range&& __range) {
1683     basic_string __temp(from_range, std::forward<_Range>(__range), __alloc_);
1684     return replace(__i1, __i2, __temp);
1685   }
1686 #  endif
1687 
1688 #  ifndef _LIBCPP_CXX03_LANG
1689   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
1690   replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il) {
1691     return replace(__i1, __i2, __il.begin(), __il.end());
1692   }
1693 #  endif // _LIBCPP_CXX03_LANG
1694 
1695   _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
1696 
1697 #  if _LIBCPP_STD_VER <= 20
1698   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string
1699   substr(size_type __pos = 0, size_type __n = npos) const {
1700     return basic_string(*this, __pos, __n);
1701   }
1702 #  else
1703   _LIBCPP_HIDE_FROM_ABI constexpr basic_string substr(size_type __pos = 0, size_type __n = npos) const& {
1704     return basic_string(*this, __pos, __n);
1705   }
1706 
1707   _LIBCPP_HIDE_FROM_ABI constexpr basic_string substr(size_type __pos = 0, size_type __n = npos) && {
1708     return basic_string(std::move(*this), __pos, __n);
1709   }
1710 #  endif
1711 
1712   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void swap(basic_string& __str)
1713 #  if _LIBCPP_STD_VER >= 14
1714       _NOEXCEPT;
1715 #  else
1716       _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v<allocator_type>);
1717 #  endif
1718 
1719   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const value_type* c_str() const _NOEXCEPT { return data(); }
1720   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const value_type* data() const _NOEXCEPT {
1721     return std::__to_address(__get_pointer());
1722   }
1723 #  if _LIBCPP_STD_VER >= 17
1724   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 value_type* data() _NOEXCEPT {
1725     return std::__to_address(__get_pointer());
1726   }
1727 #  endif
1728 
1729   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 allocator_type get_allocator() const _NOEXCEPT {
1730     return __alloc_;
1731   }
1732 
1733   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1734   find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
1735 
1736   template <class _Tp, __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, int> = 0>
1737   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1738   find(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
1739 
1740   _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
1741   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1742   find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
1743   _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1744 
1745   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1746   rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
1747 
1748   template <class _Tp, __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, int> = 0>
1749   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1750   rfind(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
1751 
1752   _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
1753   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1754   rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
1755   _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1756 
1757   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1758   find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
1759 
1760   template <class _Tp, __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, int> = 0>
1761   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1762   find_first_of(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
1763 
1764   _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1765   find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
1766   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1767   find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
1768   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1769   find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1770 
1771   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1772   find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
1773 
1774   template <class _Tp, __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, int> = 0>
1775   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1776   find_last_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
1777 
1778   _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1779   find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
1780   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1781   find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
1782   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1783   find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1784 
1785   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1786   find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
1787 
1788   template <class _Tp, __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, int> = 0>
1789   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1790   find_first_not_of(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
1791 
1792   _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1793   find_first_not_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
1794   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1795   find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
1796   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1797   find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1798 
1799   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1800   find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
1801 
1802   template <class _Tp, __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, int> = 0>
1803   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1804   find_last_not_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
1805 
1806   _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1807   find_last_not_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
1808   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1809   find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
1810   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
1811   find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1812 
1813   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 int compare(const basic_string& __str) const _NOEXCEPT;
1814 
1815   template <class _Tp, __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, int> = 0>
1816   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 int
1817   compare(const _Tp& __t) const _NOEXCEPT;
1818 
1819   template <class _Tp, __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, int> = 0>
1820   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 int
1821   compare(size_type __pos1, size_type __n1, const _Tp& __t) const;
1822 
1823   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 int
1824   compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
1825   _LIBCPP_CONSTEXPR_SINCE_CXX20 int
1826   compare(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2 = npos) const;
1827 
1828   template <class _Tp,
1829             __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
1830                               !__is_same_uncvref<_Tp, basic_string>::value,
1831                           int> = 0>
1832   inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 int
1833   compare(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2 = npos) const;
1834 
1835   _LIBCPP_CONSTEXPR_SINCE_CXX20 int compare(const value_type* __s) const _NOEXCEPT;
1836   _LIBCPP_CONSTEXPR_SINCE_CXX20 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1837   _LIBCPP_CONSTEXPR_SINCE_CXX20 int
1838   compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
1839 
1840 #  if _LIBCPP_STD_VER >= 20
1841   constexpr _LIBCPP_HIDE_FROM_ABI bool starts_with(__self_view __sv) const noexcept {
1842     return __self_view(typename __self_view::__assume_valid(), data(), size()).starts_with(__sv);
1843   }
1844 
1845   constexpr _LIBCPP_HIDE_FROM_ABI bool starts_with(value_type __c) const noexcept {
1846     return !empty() && _Traits::eq(front(), __c);
1847   }
1848 
1849   constexpr _LIBCPP_HIDE_FROM_ABI bool starts_with(const value_type* __s) const noexcept {
1850     return starts_with(__self_view(__s));
1851   }
1852 
1853   constexpr _LIBCPP_HIDE_FROM_ABI bool ends_with(__self_view __sv) const noexcept {
1854     return __self_view(typename __self_view::__assume_valid(), data(), size()).ends_with(__sv);
1855   }
1856 
1857   constexpr _LIBCPP_HIDE_FROM_ABI bool ends_with(value_type __c) const noexcept {
1858     return !empty() && _Traits::eq(back(), __c);
1859   }
1860 
1861   constexpr _LIBCPP_HIDE_FROM_ABI bool ends_with(const value_type* __s) const noexcept {
1862     return ends_with(__self_view(__s));
1863   }
1864 #  endif
1865 
1866 #  if _LIBCPP_STD_VER >= 23
1867   constexpr _LIBCPP_HIDE_FROM_ABI bool contains(__self_view __sv) const noexcept {
1868     return __self_view(typename __self_view::__assume_valid(), data(), size()).contains(__sv);
1869   }
1870 
1871   constexpr _LIBCPP_HIDE_FROM_ABI bool contains(value_type __c) const noexcept {
1872     return __self_view(typename __self_view::__assume_valid(), data(), size()).contains(__c);
1873   }
1874 
1875   constexpr _LIBCPP_HIDE_FROM_ABI bool contains(const value_type* __s) const {
1876     return __self_view(typename __self_view::__assume_valid(), data(), size()).contains(__s);
1877   }
1878 #  endif
1879 
1880   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __invariants() const;
1881 
1882   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __clear_and_shrink() _NOEXCEPT;
1883 
1884 private:
1885   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __shrink_or_extend(size_type __target_capacity);
1886 
1887   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS bool
1888   __is_long() const _NOEXCEPT {
1889     if (__libcpp_is_constant_evaluated() && __builtin_constant_p(__rep_.__l.__is_long_)) {
1890       return __rep_.__l.__is_long_;
1891     }
1892     return __rep_.__s.__is_long_;
1893   }
1894 
1895   static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __begin_lifetime(pointer __begin, size_type __n) {
1896 #  if _LIBCPP_STD_VER >= 20
1897     if (__libcpp_is_constant_evaluated()) {
1898       for (size_type __i = 0; __i != __n; ++__i)
1899         std::construct_at(std::addressof(__begin[__i]));
1900     }
1901 #  else
1902     (void)__begin;
1903     (void)__n;
1904 #  endif // _LIBCPP_STD_VER >= 20
1905   }
1906 
1907   _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI static bool __fits_in_sso(size_type __sz) { return __sz < __min_cap; }
1908 
1909   template <class _Iterator, class _Sentinel>
1910   _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
1911   __assign_trivial(_Iterator __first, _Sentinel __last, size_type __n);
1912 
1913   template <class _Iterator, class _Sentinel>
1914   _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __assign_with_sentinel(_Iterator __first, _Sentinel __last);
1915 
1916   // Copy [__first, __last) into [__dest, __dest + (__last - __first)). Assumes that the ranges don't overlap.
1917   template <class _ForwardIter, class _Sent>
1918   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 static value_type*
1919   __copy_non_overlapping_range(_ForwardIter __first, _Sent __last, value_type* __dest) {
1920 #  ifndef _LIBCPP_CXX03_LANG
1921     if constexpr (__libcpp_is_contiguous_iterator<_ForwardIter>::value &&
1922                   is_same<value_type, __remove_cvref_t<decltype(*__first)>>::value &&
1923                   is_same<_ForwardIter, _Sent>::value) {
1924       _LIBCPP_ASSERT_INTERNAL(
1925           !std::__is_overlapping_range(std::__to_address(__first), std::__to_address(__last), __dest),
1926           "__copy_non_overlapping_range called with an overlapping range!");
1927       traits_type::copy(__dest, std::__to_address(__first), __last - __first);
1928       return __dest + (__last - __first);
1929     }
1930 #  endif
1931 
1932     for (; __first != __last; ++__first)
1933       traits_type::assign(*__dest++, *__first);
1934     return __dest;
1935   }
1936 
1937   template <class _ForwardIterator, class _Sentinel>
1938   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 iterator
1939   __insert_from_safe_copy(size_type __n, size_type __ip, _ForwardIterator __first, _Sentinel __last) {
1940     size_type __sz  = size();
1941     size_type __cap = capacity();
1942     value_type* __p;
1943     if (__cap - __sz >= __n) {
1944       __annotate_increase(__n);
1945       __p                = std::__to_address(__get_pointer());
1946       size_type __n_move = __sz - __ip;
1947       if (__n_move != 0)
1948         traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
1949     } else {
1950       __grow_by_without_replace(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
1951       __p = std::__to_address(__get_long_pointer());
1952     }
1953     __sz += __n;
1954     __set_size(__sz);
1955     traits_type::assign(__p[__sz], value_type());
1956     __copy_non_overlapping_range(std::move(__first), std::move(__last), __p + __ip);
1957 
1958     return begin() + __ip;
1959   }
1960 
1961   template <class _Iterator, class _Sentinel>
1962   _LIBCPP_CONSTEXPR_SINCE_CXX20 iterator
1963   __insert_with_size(const_iterator __pos, _Iterator __first, _Sentinel __last, size_type __n);
1964 
1965   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS void
1966   __set_short_size(size_type __s) _NOEXCEPT {
1967     _LIBCPP_ASSERT_INTERNAL(__s < __min_cap, "__s should never be greater than or equal to the short string capacity");
1968     __rep_.__s.__size_    = __s;
1969     __rep_.__s.__is_long_ = false;
1970   }
1971 
1972   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS size_type
1973   __get_short_size() const _NOEXCEPT {
1974     _LIBCPP_ASSERT_INTERNAL(!__rep_.__s.__is_long_, "String has to be short when trying to get the short size");
1975     return __rep_.__s.__size_;
1976   }
1977 
1978   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __set_long_size(size_type __s) _NOEXCEPT {
1979     __rep_.__l.__size_ = __s;
1980   }
1981 
1982   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type __get_long_size() const _NOEXCEPT {
1983     _LIBCPP_ASSERT_INTERNAL(__rep_.__l.__is_long_, "String has to be long when trying to get the long size");
1984     return __rep_.__l.__size_;
1985   }
1986 
1987   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __set_size(size_type __s) _NOEXCEPT {
1988     if (__is_long())
1989       __set_long_size(__s);
1990     else
1991       __set_short_size(__s);
1992   }
1993 
1994   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __set_long_cap(size_type __s) _NOEXCEPT {
1995     _LIBCPP_ASSERT_INTERNAL(!__fits_in_sso(__s), "Long capacity should always be larger than the SSO");
1996     __rep_.__l.__cap_     = __s / __endian_factor;
1997     __rep_.__l.__is_long_ = true;
1998   }
1999 
2000   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type __get_long_cap() const _NOEXCEPT {
2001     _LIBCPP_ASSERT_INTERNAL(__rep_.__l.__is_long_, "String has to be long when trying to get the long capacity");
2002     return __rep_.__l.__cap_ * __endian_factor;
2003   }
2004 
2005   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __set_long_pointer(pointer __p) _NOEXCEPT {
2006     __rep_.__l.__data_ = __p;
2007   }
2008 
2009   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pointer __get_long_pointer() _NOEXCEPT {
2010     _LIBCPP_ASSERT_INTERNAL(__rep_.__l.__is_long_, "String has to be long when trying to get the long pointer");
2011     return _LIBCPP_ASAN_VOLATILE_WRAPPER(__rep_.__l.__data_);
2012   }
2013 
2014   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_pointer __get_long_pointer() const _NOEXCEPT {
2015     _LIBCPP_ASSERT_INTERNAL(__rep_.__l.__is_long_, "String has to be long when trying to get the long pointer");
2016     return _LIBCPP_ASAN_VOLATILE_WRAPPER(__rep_.__l.__data_);
2017   }
2018 
2019   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS pointer
2020   __get_short_pointer() _NOEXCEPT {
2021     return _LIBCPP_ASAN_VOLATILE_WRAPPER(pointer_traits<pointer>::pointer_to(__rep_.__s.__data_[0]));
2022   }
2023 
2024   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS const_pointer
2025   __get_short_pointer() const _NOEXCEPT {
2026     return _LIBCPP_ASAN_VOLATILE_WRAPPER(pointer_traits<const_pointer>::pointer_to(__rep_.__s.__data_[0]));
2027   }
2028 
2029   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pointer __get_pointer() _NOEXCEPT {
2030     return __is_long() ? __get_long_pointer() : __get_short_pointer();
2031   }
2032   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_pointer __get_pointer() const _NOEXCEPT {
2033     return __is_long() ? __get_long_pointer() : __get_short_pointer();
2034   }
2035 
2036   // The following functions are no-ops outside of AddressSanitizer mode.
2037   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
2038   __annotate_contiguous_container(const void* __old_mid, const void* __new_mid) const {
2039     (void)__old_mid;
2040     (void)__new_mid;
2041 #  if _LIBCPP_HAS_ASAN && _LIBCPP_INSTRUMENTED_WITH_ASAN
2042 #    if defined(__APPLE__)
2043     // TODO: remove after addressing issue #96099 (https://github.com/llvm/llvm-project/issues/96099)
2044     if (!__is_long())
2045       return;
2046 #    endif
2047     std::__annotate_contiguous_container<_Allocator>(data(), data() + capacity() + 1, __old_mid, __new_mid);
2048 #  endif
2049   }
2050 
2051   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __annotate_new(size_type __current_size) const _NOEXCEPT {
2052     (void)__current_size;
2053 #  if _LIBCPP_HAS_ASAN && _LIBCPP_INSTRUMENTED_WITH_ASAN
2054     if (!__libcpp_is_constant_evaluated())
2055       __annotate_contiguous_container(data() + capacity() + 1, data() + __current_size + 1);
2056 #  endif
2057   }
2058 
2059   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __annotate_delete() const _NOEXCEPT {
2060 #  if _LIBCPP_HAS_ASAN && _LIBCPP_INSTRUMENTED_WITH_ASAN
2061     if (!__libcpp_is_constant_evaluated())
2062       __annotate_contiguous_container(data() + size() + 1, data() + capacity() + 1);
2063 #  endif
2064   }
2065 
2066   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __annotate_increase(size_type __n) const _NOEXCEPT {
2067     (void)__n;
2068 #  if _LIBCPP_HAS_ASAN && _LIBCPP_INSTRUMENTED_WITH_ASAN
2069     if (!__libcpp_is_constant_evaluated())
2070       __annotate_contiguous_container(data() + size() + 1, data() + size() + 1 + __n);
2071 #  endif
2072   }
2073 
2074   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __annotate_shrink(size_type __old_size) const _NOEXCEPT {
2075     (void)__old_size;
2076 #  if _LIBCPP_HAS_ASAN && _LIBCPP_INSTRUMENTED_WITH_ASAN
2077     if (!__libcpp_is_constant_evaluated())
2078       __annotate_contiguous_container(data() + __old_size + 1, data() + size() + 1);
2079 #  endif
2080   }
2081 
2082   template <size_type __a>
2083   static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type __align_it(size_type __s) _NOEXCEPT {
2084     return (__s + (__a - 1)) & ~(__a - 1);
2085   }
2086   enum { __alignment = 8 };
2087   static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type __recommend(size_type __s) _NOEXCEPT {
2088     if (__s < __min_cap) {
2089       return static_cast<size_type>(__min_cap) - 1;
2090     }
2091     const size_type __boundary = sizeof(value_type) < __alignment ? __alignment / sizeof(value_type) : __endian_factor;
2092     size_type __guess          = __align_it<__boundary>(__s + 1) - 1;
2093     if (__guess == __min_cap)
2094       __guess += __endian_factor;
2095 
2096     _LIBCPP_ASSERT_INTERNAL(__guess >= __s, "recommendation is below the requested size");
2097     return __guess;
2098   }
2099 
2100   inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void __init(const value_type* __s, size_type __sz, size_type __reserve);
2101   inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void __init(const value_type* __s, size_type __sz);
2102   inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void __init(size_type __n, value_type __c);
2103 
2104   // Slow path for the (inlined) copy constructor for 'long' strings.
2105   // Always externally instantiated and not inlined.
2106   // Requires that __s is zero terminated.
2107   // The main reason for this function to exist is because for unstable, we
2108   // want to allow inlining of the copy constructor. However, we don't want
2109   // to call the __init() functions as those are marked as inline which may
2110   // result in over-aggressive inlining by the compiler, where our aim is
2111   // to only inline the fast path code directly in the ctor.
2112   _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE void __init_copy_ctor_external(const value_type* __s, size_type __sz);
2113 
2114   template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> = 0>
2115   inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void __init(_InputIterator __first, _InputIterator __last);
2116 
2117   template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> = 0>
2118   inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void __init(_ForwardIterator __first, _ForwardIterator __last);
2119 
2120   template <class _InputIterator, class _Sentinel>
2121   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
2122   __init_with_sentinel(_InputIterator __first, _Sentinel __last);
2123   template <class _InputIterator, class _Sentinel>
2124   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
2125   __init_with_size(_InputIterator __first, _Sentinel __last, size_type __sz);
2126 
2127   _LIBCPP_CONSTEXPR_SINCE_CXX20
2128 #  if _LIBCPP_ABI_VERSION >= 2 //  We want to use the function in the dylib in ABIv1
2129   _LIBCPP_HIDE_FROM_ABI
2130 #  endif
2131   _LIBCPP_DEPRECATED_("use __grow_by_without_replace") void __grow_by(
2132       size_type __old_cap,
2133       size_type __delta_cap,
2134       size_type __old_sz,
2135       size_type __n_copy,
2136       size_type __n_del,
2137       size_type __n_add = 0);
2138   _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __grow_by_without_replace(
2139       size_type __old_cap,
2140       size_type __delta_cap,
2141       size_type __old_sz,
2142       size_type __n_copy,
2143       size_type __n_del,
2144       size_type __n_add = 0);
2145   _LIBCPP_CONSTEXPR_SINCE_CXX20 void __grow_by_and_replace(
2146       size_type __old_cap,
2147       size_type __delta_cap,
2148       size_type __old_sz,
2149       size_type __n_copy,
2150       size_type __n_del,
2151       size_type __n_add,
2152       const value_type* __p_new_stuff);
2153 
2154   // __assign_no_alias is invoked for assignment operations where we
2155   // have proof that the input does not alias the current instance.
2156   // For example, operator=(basic_string) performs a 'self' check.
2157   template <bool __is_short>
2158   _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string& __assign_no_alias(const value_type* __s, size_type __n);
2159 
2160   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __erase_to_end(size_type __pos) {
2161     _LIBCPP_ASSERT_INTERNAL(__pos <= capacity(), "Trying to erase at position outside the strings capacity!");
2162     __null_terminate_at(std::__to_address(__get_pointer()), __pos);
2163   }
2164 
2165   // __erase_external_with_move is invoked for erase() invocations where
2166   // `n ~= npos`, likely requiring memory moves on the string data.
2167   _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE void __erase_external_with_move(size_type __pos, size_type __n);
2168 
2169   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __copy_assign_alloc(const basic_string& __str) {
2170     __copy_assign_alloc(
2171         __str, integral_constant<bool, __alloc_traits::propagate_on_container_copy_assignment::value>());
2172   }
2173 
2174   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __copy_assign_alloc(const basic_string& __str, true_type) {
2175     if (__alloc_ == __str.__alloc_)
2176       __alloc_ = __str.__alloc_;
2177     else {
2178       if (!__str.__is_long()) {
2179         __clear_and_shrink();
2180         __alloc_ = __str.__alloc_;
2181       } else {
2182         __annotate_delete();
2183         auto __guard       = std::__make_scope_guard(__annotate_new_size(*this));
2184         allocator_type __a = __str.__alloc_;
2185         auto __allocation  = std::__allocate_at_least(__a, __str.__get_long_cap());
2186         __begin_lifetime(__allocation.ptr, __allocation.count);
2187         if (__is_long())
2188           __alloc_traits::deallocate(__alloc_, __get_long_pointer(), __get_long_cap());
2189         __alloc_ = std::move(__a);
2190         __set_long_pointer(__allocation.ptr);
2191         __set_long_cap(__allocation.count);
2192         __set_long_size(__str.size());
2193       }
2194     }
2195   }
2196 
2197   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
2198   __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT {}
2199 
2200 #  ifndef _LIBCPP_CXX03_LANG
2201   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
2202   __move_assign(basic_string& __str, false_type) noexcept(__alloc_traits::is_always_equal::value);
2203   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS void
2204   __move_assign(basic_string& __str, true_type)
2205 #    if _LIBCPP_STD_VER >= 17
2206       noexcept;
2207 #    else
2208       noexcept(is_nothrow_move_assignable<allocator_type>::value);
2209 #    endif
2210 #  endif
2211 
2212   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __move_assign_alloc(basic_string& __str)
2213       _NOEXCEPT_(!__alloc_traits::propagate_on_container_move_assignment::value ||
2214                  is_nothrow_move_assignable<allocator_type>::value) {
2215     __move_assign_alloc(
2216         __str, integral_constant<bool, __alloc_traits::propagate_on_container_move_assignment::value>());
2217   }
2218 
2219   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __move_assign_alloc(basic_string& __c, true_type)
2220       _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value) {
2221     __alloc_ = std::move(__c.__alloc_);
2222   }
2223 
2224   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __move_assign_alloc(basic_string&, false_type) _NOEXCEPT {}
2225 
2226   _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string& __assign_external(const value_type* __s);
2227   _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string& __assign_external(const value_type* __s, size_type __n);
2228 
2229   // Assigns the value in __s, guaranteed to be __n < __min_cap in length.
2230   inline _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string& __assign_short(const value_type* __s, size_type __n) {
2231     size_type __old_size = size();
2232     if (__n > __old_size)
2233       __annotate_increase(__n - __old_size);
2234     pointer __p =
2235         __is_long() ? (__set_long_size(__n), __get_long_pointer()) : (__set_short_size(__n), __get_short_pointer());
2236     traits_type::move(std::__to_address(__p), __s, __n);
2237     traits_type::assign(__p[__n], value_type());
2238     if (__old_size > __n)
2239       __annotate_shrink(__old_size);
2240     return *this;
2241   }
2242 
2243   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string&
2244   __null_terminate_at(value_type* __p, size_type __newsz) {
2245     size_type __old_size = size();
2246     if (__newsz > __old_size)
2247       __annotate_increase(__newsz - __old_size);
2248     __set_size(__newsz);
2249     traits_type::assign(__p[__newsz], value_type());
2250     if (__old_size > __newsz)
2251       __annotate_shrink(__old_size);
2252     return *this;
2253   }
2254 
2255   template <class _Tp>
2256   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __addr_in_range(const _Tp& __v) const {
2257     return std::__is_pointer_in_range(data(), data() + size() + 1, std::addressof(__v));
2258   }
2259 
2260   [[__noreturn__]] _LIBCPP_HIDE_FROM_ABI static void __throw_length_error() {
2261     std::__throw_length_error("basic_string");
2262   }
2263 
2264   [[__noreturn__]] _LIBCPP_HIDE_FROM_ABI static void __throw_out_of_range() {
2265     std::__throw_out_of_range("basic_string");
2266   }
2267 
2268   friend _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string operator+ <>(const basic_string&, const basic_string&);
2269   friend _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string operator+ <>(const value_type*, const basic_string&);
2270   friend _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string operator+ <>(value_type, const basic_string&);
2271   friend _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string operator+ <>(const basic_string&, const value_type*);
2272   friend _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string operator+ <>(const basic_string&, value_type);
2273 #  if _LIBCPP_STD_VER >= 26
2274   friend constexpr basic_string operator+ <>(const basic_string&, type_identity_t<__self_view>);
2275   friend constexpr basic_string operator+ <>(type_identity_t<__self_view>, const basic_string&);
2276 #  endif
2277 
2278   template <class _CharT2, class _Traits2, class _Allocator2>
2279   friend inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool
2280   operator==(const basic_string<_CharT2, _Traits2, _Allocator2>&, const _CharT2*) _NOEXCEPT;
2281 };
2282 
2283 // These declarations must appear before any functions are implicitly used
2284 // so that they have the correct visibility specifier.
2285 #  define _LIBCPP_DECLARE(...) extern template __VA_ARGS__;
2286 #  ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
2287 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, char)
2288 #    if _LIBCPP_HAS_WIDE_CHARACTERS
2289 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, wchar_t)
2290 #    endif
2291 #  else
2292 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, char)
2293 #    if _LIBCPP_HAS_WIDE_CHARACTERS
2294 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, wchar_t)
2295 #    endif
2296 #  endif
2297 #  undef _LIBCPP_DECLARE
2298 
2299 #  if _LIBCPP_STD_VER >= 17
2300 template <class _InputIterator,
2301           class _CharT     = __iter_value_type<_InputIterator>,
2302           class _Allocator = allocator<_CharT>,
2303           class            = enable_if_t<__has_input_iterator_category<_InputIterator>::value>,
2304           class            = enable_if_t<__is_allocator<_Allocator>::value> >
2305 basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
2306     -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
2307 
2308 template <class _CharT,
2309           class _Traits,
2310           class _Allocator = allocator<_CharT>,
2311           class            = enable_if_t<__is_allocator<_Allocator>::value> >
2312 explicit basic_string(basic_string_view<_CharT, _Traits>,
2313                       const _Allocator& = _Allocator()) -> basic_string<_CharT, _Traits, _Allocator>;
2314 
2315 template <class _CharT,
2316           class _Traits,
2317           class _Allocator = allocator<_CharT>,
2318           class            = enable_if_t<__is_allocator<_Allocator>::value>,
2319           class _Sz        = typename allocator_traits<_Allocator>::size_type >
2320 basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
2321     -> basic_string<_CharT, _Traits, _Allocator>;
2322 #  endif
2323 
2324 #  if _LIBCPP_STD_VER >= 23
2325 template <ranges::input_range _Range,
2326           class _Allocator = allocator<ranges::range_value_t<_Range>>,
2327           class            = enable_if_t<__is_allocator<_Allocator>::value> >
2328 basic_string(from_range_t, _Range&&, _Allocator = _Allocator())
2329     -> basic_string<ranges::range_value_t<_Range>, char_traits<ranges::range_value_t<_Range>>, _Allocator>;
2330 #  endif
2331 
2332 template <class _CharT, class _Traits, class _Allocator>
2333 _LIBCPP_CONSTEXPR_SINCE_CXX20 void
2334 basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz, size_type __reserve) {
2335   if (__libcpp_is_constant_evaluated())
2336     __rep_ = __rep();
2337   if (__reserve > max_size())
2338     __throw_length_error();
2339   pointer __p;
2340   if (__fits_in_sso(__reserve)) {
2341     __set_short_size(__sz);
2342     __p = __get_short_pointer();
2343   } else {
2344     auto __allocation = std::__allocate_at_least(__alloc_, __recommend(__reserve) + 1);
2345     __p               = __allocation.ptr;
2346     __begin_lifetime(__p, __allocation.count);
2347     __set_long_pointer(__p);
2348     __set_long_cap(__allocation.count);
2349     __set_long_size(__sz);
2350   }
2351   traits_type::copy(std::__to_address(__p), __s, __sz);
2352   traits_type::assign(__p[__sz], value_type());
2353   __annotate_new(__sz);
2354 }
2355 
2356 template <class _CharT, class _Traits, class _Allocator>
2357 _LIBCPP_CONSTEXPR_SINCE_CXX20 void
2358 basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz) {
2359   if (__libcpp_is_constant_evaluated())
2360     __rep_ = __rep();
2361   if (__sz > max_size())
2362     __throw_length_error();
2363   pointer __p;
2364   if (__fits_in_sso(__sz)) {
2365     __set_short_size(__sz);
2366     __p = __get_short_pointer();
2367   } else {
2368     auto __allocation = std::__allocate_at_least(__alloc_, __recommend(__sz) + 1);
2369     __p               = __allocation.ptr;
2370     __begin_lifetime(__p, __allocation.count);
2371     __set_long_pointer(__p);
2372     __set_long_cap(__allocation.count);
2373     __set_long_size(__sz);
2374   }
2375   traits_type::copy(std::__to_address(__p), __s, __sz);
2376   traits_type::assign(__p[__sz], value_type());
2377   __annotate_new(__sz);
2378 }
2379 
2380 template <class _CharT, class _Traits, class _Allocator>
2381 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE void
2382 basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(const value_type* __s, size_type __sz) {
2383   if (__libcpp_is_constant_evaluated())
2384     __rep_ = __rep();
2385 
2386   pointer __p;
2387   if (__fits_in_sso(__sz)) {
2388     __p = __get_short_pointer();
2389     __set_short_size(__sz);
2390   } else {
2391     if (__sz > max_size())
2392       __throw_length_error();
2393     auto __allocation = std::__allocate_at_least(__alloc_, __recommend(__sz) + 1);
2394     __p               = __allocation.ptr;
2395     __begin_lifetime(__p, __allocation.count);
2396     __set_long_pointer(__p);
2397     __set_long_cap(__allocation.count);
2398     __set_long_size(__sz);
2399   }
2400   traits_type::copy(std::__to_address(__p), __s, __sz + 1);
2401   __annotate_new(__sz);
2402 }
2403 
2404 template <class _CharT, class _Traits, class _Allocator>
2405 _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c) {
2406   if (__libcpp_is_constant_evaluated())
2407     __rep_ = __rep();
2408 
2409   if (__n > max_size())
2410     __throw_length_error();
2411   pointer __p;
2412   if (__fits_in_sso(__n)) {
2413     __set_short_size(__n);
2414     __p = __get_short_pointer();
2415   } else {
2416     auto __allocation = std::__allocate_at_least(__alloc_, __recommend(__n) + 1);
2417     __p               = __allocation.ptr;
2418     __begin_lifetime(__p, __allocation.count);
2419     __set_long_pointer(__p);
2420     __set_long_cap(__allocation.count);
2421     __set_long_size(__n);
2422   }
2423   traits_type::assign(std::__to_address(__p), __n, __c);
2424   traits_type::assign(__p[__n], value_type());
2425   __annotate_new(__n);
2426 }
2427 
2428 template <class _CharT, class _Traits, class _Allocator>
2429 template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> >
2430 _LIBCPP_CONSTEXPR_SINCE_CXX20 void
2431 basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last) {
2432   __init_with_sentinel(std::move(__first), std::move(__last));
2433 }
2434 
2435 template <class _CharT, class _Traits, class _Allocator>
2436 template <class _InputIterator, class _Sentinel>
2437 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
2438 basic_string<_CharT, _Traits, _Allocator>::__init_with_sentinel(_InputIterator __first, _Sentinel __last) {
2439   __rep_ = __rep();
2440   __annotate_new(0);
2441 
2442 #  if _LIBCPP_HAS_EXCEPTIONS
2443   try {
2444 #  endif // _LIBCPP_HAS_EXCEPTIONS
2445     for (; __first != __last; ++__first)
2446       push_back(*__first);
2447 #  if _LIBCPP_HAS_EXCEPTIONS
2448   } catch (...) {
2449     __annotate_delete();
2450     if (__is_long())
2451       __alloc_traits::deallocate(__alloc_, __get_long_pointer(), __get_long_cap());
2452     throw;
2453   }
2454 #  endif // _LIBCPP_HAS_EXCEPTIONS
2455 }
2456 
2457 template <class _CharT, class _Traits, class _Allocator>
2458 template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> >
2459 _LIBCPP_CONSTEXPR_SINCE_CXX20 void
2460 basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last) {
2461   size_type __sz = static_cast<size_type>(std::distance(__first, __last));
2462   __init_with_size(__first, __last, __sz);
2463 }
2464 
2465 template <class _CharT, class _Traits, class _Allocator>
2466 template <class _InputIterator, class _Sentinel>
2467 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
2468 basic_string<_CharT, _Traits, _Allocator>::__init_with_size(_InputIterator __first, _Sentinel __last, size_type __sz) {
2469   if (__libcpp_is_constant_evaluated())
2470     __rep_ = __rep();
2471 
2472   if (__sz > max_size())
2473     __throw_length_error();
2474 
2475   pointer __p;
2476   if (__fits_in_sso(__sz)) {
2477     __set_short_size(__sz);
2478     __p = __get_short_pointer();
2479 
2480   } else {
2481     auto __allocation = std::__allocate_at_least(__alloc_, __recommend(__sz) + 1);
2482     __p               = __allocation.ptr;
2483     __begin_lifetime(__p, __allocation.count);
2484     __set_long_pointer(__p);
2485     __set_long_cap(__allocation.count);
2486     __set_long_size(__sz);
2487   }
2488 
2489 #  if _LIBCPP_HAS_EXCEPTIONS
2490   try {
2491 #  endif // _LIBCPP_HAS_EXCEPTIONS
2492     auto __end = __copy_non_overlapping_range(std::move(__first), std::move(__last), std::__to_address(__p));
2493     traits_type::assign(*__end, value_type());
2494 #  if _LIBCPP_HAS_EXCEPTIONS
2495   } catch (...) {
2496     if (__is_long())
2497       __alloc_traits::deallocate(__alloc_, __get_long_pointer(), __get_long_cap());
2498     throw;
2499   }
2500 #  endif                       // _LIBCPP_HAS_EXCEPTIONS
2501   __annotate_new(__sz);
2502 }
2503 
2504 template <class _CharT, class _Traits, class _Allocator>
2505 _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace(
2506     size_type __old_cap,
2507     size_type __delta_cap,
2508     size_type __old_sz,
2509     size_type __n_copy,
2510     size_type __n_del,
2511     size_type __n_add,
2512     const value_type* __p_new_stuff) {
2513   size_type __ms = max_size();
2514   if (__delta_cap > __ms - __old_cap - 1)
2515     __throw_length_error();
2516   pointer __old_p = __get_pointer();
2517   size_type __cap =
2518       __old_cap < __ms / 2 - __alignment ? __recommend(std::max(__old_cap + __delta_cap, 2 * __old_cap)) : __ms - 1;
2519   __annotate_delete();
2520   auto __guard      = std::__make_scope_guard(__annotate_new_size(*this));
2521   auto __allocation = std::__allocate_at_least(__alloc_, __cap + 1);
2522   pointer __p       = __allocation.ptr;
2523   __begin_lifetime(__p, __allocation.count);
2524   if (__n_copy != 0)
2525     traits_type::copy(std::__to_address(__p), std::__to_address(__old_p), __n_copy);
2526   if (__n_add != 0)
2527     traits_type::copy(std::__to_address(__p) + __n_copy, __p_new_stuff, __n_add);
2528   size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2529   if (__sec_cp_sz != 0)
2530     traits_type::copy(
2531         std::__to_address(__p) + __n_copy + __n_add, std::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
2532   if (__old_cap + 1 != __min_cap)
2533     __alloc_traits::deallocate(__alloc_, __old_p, __old_cap + 1);
2534   __set_long_pointer(__p);
2535   __set_long_cap(__allocation.count);
2536   __old_sz = __n_copy + __n_add + __sec_cp_sz;
2537   __set_long_size(__old_sz);
2538   traits_type::assign(__p[__old_sz], value_type());
2539 }
2540 
2541 // __grow_by is deprecated because it does not set the size. It may not update the size when the size is changed, and it
2542 // may also not set the size at all when the string was short initially. This leads to unpredictable size value. It is
2543 // not removed or changed to avoid breaking the ABI.
2544 template <class _CharT, class _Traits, class _Allocator>
2545 void _LIBCPP_CONSTEXPR_SINCE_CXX20
2546 #  if _LIBCPP_ABI_VERSION >= 2 // We want to use the function in the dylib in ABIv1
2547 _LIBCPP_HIDE_FROM_ABI
2548 #  endif
2549 _LIBCPP_DEPRECATED_("use __grow_by_without_replace") basic_string<_CharT, _Traits, _Allocator>::__grow_by(
2550     size_type __old_cap,
2551     size_type __delta_cap,
2552     size_type __old_sz,
2553     size_type __n_copy,
2554     size_type __n_del,
2555     size_type __n_add) {
2556   size_type __ms = max_size();
2557   if (__delta_cap > __ms - __old_cap)
2558     __throw_length_error();
2559   pointer __old_p = __get_pointer();
2560   size_type __cap =
2561       __old_cap < __ms / 2 - __alignment ? __recommend(std::max(__old_cap + __delta_cap, 2 * __old_cap)) : __ms - 1;
2562   auto __allocation = std::__allocate_at_least(__alloc_, __cap + 1);
2563   pointer __p       = __allocation.ptr;
2564   __begin_lifetime(__p, __allocation.count);
2565   if (__n_copy != 0)
2566     traits_type::copy(std::__to_address(__p), std::__to_address(__old_p), __n_copy);
2567   size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2568   if (__sec_cp_sz != 0)
2569     traits_type::copy(
2570         std::__to_address(__p) + __n_copy + __n_add, std::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
2571   if (__old_cap + 1 != __min_cap)
2572     __alloc_traits::deallocate(__alloc_, __old_p, __old_cap + 1);
2573   __set_long_pointer(__p);
2574   __set_long_cap(__allocation.count);
2575 }
2576 
2577 template <class _CharT, class _Traits, class _Allocator>
2578 void _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
2579 basic_string<_CharT, _Traits, _Allocator>::__grow_by_without_replace(
2580     size_type __old_cap,
2581     size_type __delta_cap,
2582     size_type __old_sz,
2583     size_type __n_copy,
2584     size_type __n_del,
2585     size_type __n_add) {
2586   __annotate_delete();
2587   auto __guard = std::__make_scope_guard(__annotate_new_size(*this));
2588   _LIBCPP_SUPPRESS_DEPRECATED_PUSH
2589   __grow_by(__old_cap, __delta_cap, __old_sz, __n_copy, __n_del, __n_add);
2590   _LIBCPP_SUPPRESS_DEPRECATED_POP
2591   __set_long_size(__old_sz - __n_del + __n_add);
2592 }
2593 
2594 // assign
2595 
2596 template <class _CharT, class _Traits, class _Allocator>
2597 template <bool __is_short>
2598 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string<_CharT, _Traits, _Allocator>&
2599 basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(const value_type* __s, size_type __n) {
2600   size_type __cap = __is_short ? static_cast<size_type>(__min_cap) : __get_long_cap();
2601   if (__n < __cap) {
2602     size_type __old_size = __is_short ? __get_short_size() : __get_long_size();
2603     if (__n > __old_size)
2604       __annotate_increase(__n - __old_size);
2605     pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
2606     __is_short ? __set_short_size(__n) : __set_long_size(__n);
2607     traits_type::copy(std::__to_address(__p), __s, __n);
2608     traits_type::assign(__p[__n], value_type());
2609     if (__old_size > __n)
2610       __annotate_shrink(__old_size);
2611   } else {
2612     size_type __sz = __is_short ? __get_short_size() : __get_long_size();
2613     __grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
2614   }
2615   return *this;
2616 }
2617 
2618 template <class _CharT, class _Traits, class _Allocator>
2619 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string<_CharT, _Traits, _Allocator>&
2620 basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s, size_type __n) {
2621   size_type __cap = capacity();
2622   if (__cap >= __n) {
2623     size_type __old_size = size();
2624     if (__n > __old_size)
2625       __annotate_increase(__n - __old_size);
2626     value_type* __p = std::__to_address(__get_pointer());
2627     traits_type::move(__p, __s, __n);
2628     return __null_terminate_at(__p, __n);
2629   } else {
2630     size_type __sz = size();
2631     __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
2632     return *this;
2633   }
2634 }
2635 
2636 template <class _CharT, class _Traits, class _Allocator>
2637 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
2638 basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n) {
2639   _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string::assign received nullptr");
2640   return (__builtin_constant_p(__n) && __fits_in_sso(__n)) ? __assign_short(__s, __n) : __assign_external(__s, __n);
2641 }
2642 
2643 template <class _CharT, class _Traits, class _Allocator>
2644 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
2645 basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c) {
2646   size_type __cap      = capacity();
2647   size_type __old_size = size();
2648   if (__cap < __n) {
2649     size_type __sz = size();
2650     __grow_by_without_replace(__cap, __n - __cap, __sz, 0, __sz);
2651     __annotate_increase(__n);
2652   } else if (__n > __old_size)
2653     __annotate_increase(__n - __old_size);
2654   value_type* __p = std::__to_address(__get_pointer());
2655   traits_type::assign(__p, __n, __c);
2656   return __null_terminate_at(__p, __n);
2657 }
2658 
2659 template <class _CharT, class _Traits, class _Allocator>
2660 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
2661 basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c) {
2662   pointer __p;
2663   size_type __old_size = size();
2664   if (__old_size == 0)
2665     __annotate_increase(1);
2666   if (__is_long()) {
2667     __p = __get_long_pointer();
2668     __set_long_size(1);
2669   } else {
2670     __p = __get_short_pointer();
2671     __set_short_size(1);
2672   }
2673   traits_type::assign(*__p, __c);
2674   traits_type::assign(*++__p, value_type());
2675   if (__old_size > 1)
2676     __annotate_shrink(__old_size);
2677   return *this;
2678 }
2679 
2680 template <class _CharT, class _Traits, class _Allocator>
2681 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS basic_string<_CharT, _Traits, _Allocator>&
2682 basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str) {
2683   if (this != std::addressof(__str)) {
2684     __copy_assign_alloc(__str);
2685     if (!__is_long()) {
2686       if (!__str.__is_long()) {
2687         size_type __old_size = __get_short_size();
2688         if (__get_short_size() < __str.__get_short_size())
2689           __annotate_increase(__str.__get_short_size() - __get_short_size());
2690         __rep_ = __str.__rep_;
2691         if (__old_size > __get_short_size())
2692           __annotate_shrink(__old_size);
2693       } else {
2694         return __assign_no_alias<true>(__str.data(), __str.size());
2695       }
2696     } else {
2697       return __assign_no_alias<false>(__str.data(), __str.size());
2698     }
2699   }
2700   return *this;
2701 }
2702 
2703 #  ifndef _LIBCPP_CXX03_LANG
2704 
2705 template <class _CharT, class _Traits, class _Allocator>
2706 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::__move_assign(
2707     basic_string& __str, false_type) noexcept(__alloc_traits::is_always_equal::value) {
2708   if (__alloc_ != __str.__alloc_)
2709     assign(__str);
2710   else
2711     __move_assign(__str, true_type());
2712 }
2713 
2714 template <class _CharT, class _Traits, class _Allocator>
2715 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS void
2716 basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
2717 #    if _LIBCPP_STD_VER >= 17
2718     noexcept
2719 #    else
2720     noexcept(is_nothrow_move_assignable<allocator_type>::value)
2721 #    endif
2722 {
2723   __annotate_delete();
2724   if (__is_long()) {
2725     __alloc_traits::deallocate(__alloc_, __get_long_pointer(), __get_long_cap());
2726 #    if _LIBCPP_STD_VER <= 14
2727     if (!is_nothrow_move_assignable<allocator_type>::value) {
2728       __set_short_size(0);
2729       traits_type::assign(__get_short_pointer()[0], value_type());
2730       __annotate_new(0);
2731     }
2732 #    endif
2733   }
2734   size_type __str_old_size = __str.size();
2735   bool __str_was_short     = !__str.__is_long();
2736 
2737   __move_assign_alloc(__str);
2738   __rep_ = __str.__rep_;
2739   __str.__set_short_size(0);
2740   traits_type::assign(__str.__get_short_pointer()[0], value_type());
2741 
2742   if (__str_was_short && this != std::addressof(__str))
2743     __str.__annotate_shrink(__str_old_size);
2744   else
2745     // ASan annotations: was long, so object memory is unpoisoned as new.
2746     // Or is same as *this, and __annotate_delete() was called.
2747     __str.__annotate_new(0);
2748 
2749   // ASan annotations: Guard against `std::string s; s = std::move(s);`
2750   // You can find more here: https://en.cppreference.com/w/cpp/utility/move
2751   // Quote: "Unless otherwise specified, all standard library objects that have been moved
2752   // from are placed in a "valid but unspecified state", meaning the object's class
2753   // invariants hold (so functions without preconditions, such as the assignment operator,
2754   // can be safely used on the object after it was moved from):"
2755   // Quote: "v = std::move(v); // the value of v is unspecified"
2756   if (!__is_long() && std::addressof(__str) != this)
2757     // If it is long string, delete was never called on original __str's buffer.
2758     __annotate_new(__get_short_size());
2759 }
2760 
2761 #  endif
2762 
2763 template <class _CharT, class _Traits, class _Allocator>
2764 template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> >
2765 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
2766 basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last) {
2767   __assign_with_sentinel(__first, __last);
2768   return *this;
2769 }
2770 
2771 template <class _CharT, class _Traits, class _Allocator>
2772 template <class _InputIterator, class _Sentinel>
2773 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
2774 basic_string<_CharT, _Traits, _Allocator>::__assign_with_sentinel(_InputIterator __first, _Sentinel __last) {
2775   const basic_string __temp(__init_with_sentinel_tag(), std::move(__first), std::move(__last), __alloc_);
2776   assign(__temp.data(), __temp.size());
2777 }
2778 
2779 template <class _CharT, class _Traits, class _Allocator>
2780 template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> >
2781 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
2782 basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last) {
2783   if (__string_is_trivial_iterator<_ForwardIterator>::value) {
2784     size_type __n = static_cast<size_type>(std::distance(__first, __last));
2785     __assign_trivial(__first, __last, __n);
2786   } else {
2787     __assign_with_sentinel(__first, __last);
2788   }
2789 
2790   return *this;
2791 }
2792 
2793 template <class _CharT, class _Traits, class _Allocator>
2794 template <class _Iterator, class _Sentinel>
2795 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
2796 basic_string<_CharT, _Traits, _Allocator>::__assign_trivial(_Iterator __first, _Sentinel __last, size_type __n) {
2797   _LIBCPP_ASSERT_INTERNAL(
2798       __string_is_trivial_iterator<_Iterator>::value, "The iterator type given to `__assign_trivial` must be trivial");
2799 
2800   size_type __old_size = size();
2801   size_type __cap      = capacity();
2802   if (__cap < __n) {
2803     // Unlike `append` functions, if the input range points into the string itself, there is no case that the input
2804     // range could get invalidated by reallocation:
2805     // 1. If the input range is a subset of the string itself, its size cannot exceed the capacity of the string,
2806     //    thus no reallocation would happen.
2807     // 2. In the exotic case where the input range is the byte representation of the string itself, the string
2808     //    object itself stays valid even if reallocation happens.
2809     size_type __sz = size();
2810     __grow_by_without_replace(__cap, __n - __cap, __sz, 0, __sz);
2811     __annotate_increase(__n);
2812   } else if (__n > __old_size)
2813     __annotate_increase(__n - __old_size);
2814   pointer __p = __get_pointer();
2815   for (; __first != __last; ++__p, (void)++__first)
2816     traits_type::assign(*__p, *__first);
2817   traits_type::assign(*__p, value_type());
2818   __set_size(__n);
2819   if (__n < __old_size)
2820     __annotate_shrink(__old_size);
2821 }
2822 
2823 template <class _CharT, class _Traits, class _Allocator>
2824 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
2825 basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n) {
2826   size_type __sz = __str.size();
2827   if (__pos > __sz)
2828     __throw_out_of_range();
2829   return assign(__str.data() + __pos, std::min(__n, __sz - __pos));
2830 }
2831 
2832 template <class _CharT, class _Traits, class _Allocator>
2833 template <class _Tp,
2834           __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
2835                             !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
2836                         int> >
2837 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
2838 basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp& __t, size_type __pos, size_type __n) {
2839   __self_view __sv = __t;
2840   size_type __sz   = __sv.size();
2841   if (__pos > __sz)
2842     __throw_out_of_range();
2843   return assign(__sv.data() + __pos, std::min(__n, __sz - __pos));
2844 }
2845 
2846 template <class _CharT, class _Traits, class _Allocator>
2847 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE basic_string<_CharT, _Traits, _Allocator>&
2848 basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s) {
2849   return __assign_external(__s, traits_type::length(__s));
2850 }
2851 
2852 template <class _CharT, class _Traits, class _Allocator>
2853 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
2854 basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s) {
2855   _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::assign received nullptr");
2856   return __builtin_constant_p(*__s)
2857            ? (__fits_in_sso(traits_type::length(__s)) ? __assign_short(__s, traits_type::length(__s))
2858                                                       : __assign_external(__s, traits_type::length(__s)))
2859            : __assign_external(__s);
2860 }
2861 // append
2862 
2863 template <class _CharT, class _Traits, class _Allocator>
2864 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
2865 basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n) {
2866   _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string::append received nullptr");
2867   size_type __cap = capacity();
2868   size_type __sz  = size();
2869   if (__cap - __sz >= __n) {
2870     if (__n) {
2871       __annotate_increase(__n);
2872       value_type* __p = std::__to_address(__get_pointer());
2873       traits_type::copy(__p + __sz, __s, __n);
2874       __sz += __n;
2875       __set_size(__sz);
2876       traits_type::assign(__p[__sz], value_type());
2877     }
2878   } else
2879     __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2880   return *this;
2881 }
2882 
2883 template <class _CharT, class _Traits, class _Allocator>
2884 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
2885 basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c) {
2886   if (__n) {
2887     size_type __cap = capacity();
2888     size_type __sz  = size();
2889     if (__cap - __sz < __n)
2890       __grow_by_without_replace(__cap, __sz + __n - __cap, __sz, __sz, 0);
2891     __annotate_increase(__n);
2892     pointer __p = __get_pointer();
2893     traits_type::assign(std::__to_address(__p) + __sz, __n, __c);
2894     __sz += __n;
2895     __set_size(__sz);
2896     traits_type::assign(__p[__sz], value_type());
2897   }
2898   return *this;
2899 }
2900 
2901 template <class _CharT, class _Traits, class _Allocator>
2902 _LIBCPP_CONSTEXPR_SINCE_CXX20 inline void
2903 basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n) {
2904   if (__n) {
2905     size_type __cap = capacity();
2906     size_type __sz  = size();
2907     if (__cap - __sz < __n)
2908       __grow_by_without_replace(__cap, __sz + __n - __cap, __sz, __sz, 0);
2909     __annotate_increase(__n);
2910     pointer __p = __get_pointer();
2911     __sz += __n;
2912     __set_size(__sz);
2913     traits_type::assign(__p[__sz], value_type());
2914   }
2915 }
2916 
2917 template <class _CharT, class _Traits, class _Allocator>
2918 _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c) {
2919   bool __is_short = !__is_long();
2920   size_type __cap;
2921   size_type __sz;
2922   if (__is_short) {
2923     __cap = __min_cap - 1;
2924     __sz  = __get_short_size();
2925   } else {
2926     __cap = __get_long_cap() - 1;
2927     __sz  = __get_long_size();
2928   }
2929   if (__sz == __cap) {
2930     __grow_by_without_replace(__cap, 1, __sz, __sz, 0);
2931     __annotate_increase(1);
2932     __is_short = false; // the string is always long after __grow_by
2933   } else
2934     __annotate_increase(1);
2935   pointer __p = __get_pointer();
2936   if (__is_short) {
2937     __p = __get_short_pointer() + __sz;
2938     __set_short_size(__sz + 1);
2939   } else {
2940     __p = __get_long_pointer() + __sz;
2941     __set_long_size(__sz + 1);
2942   }
2943   traits_type::assign(*__p, __c);
2944   traits_type::assign(*++__p, value_type());
2945 }
2946 
2947 template <class _CharT, class _Traits, class _Allocator>
2948 template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> >
2949 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
2950 basic_string<_CharT, _Traits, _Allocator>::append(_ForwardIterator __first, _ForwardIterator __last) {
2951   size_type __sz  = size();
2952   size_type __cap = capacity();
2953   size_type __n   = static_cast<size_type>(std::distance(__first, __last));
2954   if (__n) {
2955     if (__string_is_trivial_iterator<_ForwardIterator>::value && !__addr_in_range(*__first)) {
2956       if (__cap - __sz < __n)
2957         __grow_by_without_replace(__cap, __sz + __n - __cap, __sz, __sz, 0);
2958       __annotate_increase(__n);
2959       auto __end = __copy_non_overlapping_range(__first, __last, std::__to_address(__get_pointer() + __sz));
2960       traits_type::assign(*__end, value_type());
2961       __set_size(__sz + __n);
2962     } else {
2963       const basic_string __temp(__first, __last, __alloc_);
2964       append(__temp.data(), __temp.size());
2965     }
2966   }
2967   return *this;
2968 }
2969 
2970 template <class _CharT, class _Traits, class _Allocator>
2971 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
2972 basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n) {
2973   size_type __sz = __str.size();
2974   if (__pos > __sz)
2975     __throw_out_of_range();
2976   return append(__str.data() + __pos, std::min(__n, __sz - __pos));
2977 }
2978 
2979 template <class _CharT, class _Traits, class _Allocator>
2980 template <class _Tp,
2981           __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
2982                             !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
2983                         int> >
2984 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
2985 basic_string<_CharT, _Traits, _Allocator>::append(const _Tp& __t, size_type __pos, size_type __n) {
2986   __self_view __sv = __t;
2987   size_type __sz   = __sv.size();
2988   if (__pos > __sz)
2989     __throw_out_of_range();
2990   return append(__sv.data() + __pos, std::min(__n, __sz - __pos));
2991 }
2992 
2993 template <class _CharT, class _Traits, class _Allocator>
2994 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
2995 basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s) {
2996   _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::append received nullptr");
2997   return append(__s, traits_type::length(__s));
2998 }
2999 
3000 // insert
3001 
3002 template <class _CharT, class _Traits, class _Allocator>
3003 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
3004 basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n) {
3005   _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string::insert received nullptr");
3006   size_type __sz = size();
3007   if (__pos > __sz)
3008     __throw_out_of_range();
3009   size_type __cap = capacity();
3010   if (__cap - __sz >= __n) {
3011     if (__n) {
3012       __annotate_increase(__n);
3013       value_type* __p    = std::__to_address(__get_pointer());
3014       size_type __n_move = __sz - __pos;
3015       if (__n_move != 0) {
3016         if (std::__is_pointer_in_range(__p + __pos, __p + __sz, __s))
3017           __s += __n;
3018         traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
3019       }
3020       traits_type::move(__p + __pos, __s, __n);
3021       __sz += __n;
3022       __set_size(__sz);
3023       traits_type::assign(__p[__sz], value_type());
3024     }
3025   } else
3026     __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
3027   return *this;
3028 }
3029 
3030 template <class _CharT, class _Traits, class _Allocator>
3031 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
3032 basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c) {
3033   size_type __sz = size();
3034   if (__pos > __sz)
3035     __throw_out_of_range();
3036   if (__n) {
3037     size_type __cap = capacity();
3038     value_type* __p;
3039     if (__cap - __sz >= __n) {
3040       __annotate_increase(__n);
3041       __p                = std::__to_address(__get_pointer());
3042       size_type __n_move = __sz - __pos;
3043       if (__n_move != 0)
3044         traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
3045     } else {
3046       __grow_by_without_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
3047       __p = std::__to_address(__get_long_pointer());
3048     }
3049     traits_type::assign(__p + __pos, __n, __c);
3050     __sz += __n;
3051     __set_size(__sz);
3052     traits_type::assign(__p[__sz], value_type());
3053   }
3054   return *this;
3055 }
3056 
3057 template <class _CharT, class _Traits, class _Allocator>
3058 template <class _InputIterator, __enable_if_t<__has_exactly_input_iterator_category<_InputIterator>::value, int> >
3059 _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::iterator
3060 basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last) {
3061   const basic_string __temp(__first, __last, __alloc_);
3062   return insert(__pos, __temp.data(), __temp.data() + __temp.size());
3063 }
3064 
3065 template <class _CharT, class _Traits, class _Allocator>
3066 template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> >
3067 _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::iterator
3068 basic_string<_CharT, _Traits, _Allocator>::insert(
3069     const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last) {
3070   auto __n = static_cast<size_type>(std::distance(__first, __last));
3071   return __insert_with_size(__pos, __first, __last, __n);
3072 }
3073 
3074 template <class _CharT, class _Traits, class _Allocator>
3075 template <class _Iterator, class _Sentinel>
3076 _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::iterator
3077 basic_string<_CharT, _Traits, _Allocator>::__insert_with_size(
3078     const_iterator __pos, _Iterator __first, _Sentinel __last, size_type __n) {
3079   size_type __ip = static_cast<size_type>(__pos - begin());
3080   if (__n == 0)
3081     return begin() + __ip;
3082 
3083   if (__string_is_trivial_iterator<_Iterator>::value && !__addr_in_range(*__first)) {
3084     return __insert_from_safe_copy(__n, __ip, std::move(__first), std::move(__last));
3085   } else {
3086     const basic_string __temp(__init_with_sentinel_tag(), std::move(__first), std::move(__last), __alloc_);
3087     return __insert_from_safe_copy(__n, __ip, __temp.begin(), __temp.end());
3088   }
3089 }
3090 
3091 template <class _CharT, class _Traits, class _Allocator>
3092 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
3093 basic_string<_CharT, _Traits, _Allocator>::insert(
3094     size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n) {
3095   size_type __str_sz = __str.size();
3096   if (__pos2 > __str_sz)
3097     __throw_out_of_range();
3098   return insert(__pos1, __str.data() + __pos2, std::min(__n, __str_sz - __pos2));
3099 }
3100 
3101 template <class _CharT, class _Traits, class _Allocator>
3102 template <class _Tp,
3103           __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
3104                             !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
3105                         int> >
3106 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
3107 basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n) {
3108   __self_view __sv   = __t;
3109   size_type __str_sz = __sv.size();
3110   if (__pos2 > __str_sz)
3111     __throw_out_of_range();
3112   return insert(__pos1, __sv.data() + __pos2, std::min(__n, __str_sz - __pos2));
3113 }
3114 
3115 template <class _CharT, class _Traits, class _Allocator>
3116 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
3117 basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s) {
3118   _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::insert received nullptr");
3119   return insert(__pos, __s, traits_type::length(__s));
3120 }
3121 
3122 template <class _CharT, class _Traits, class _Allocator>
3123 _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::iterator
3124 basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c) {
3125   size_type __ip  = static_cast<size_type>(__pos - begin());
3126   size_type __sz  = size();
3127   size_type __cap = capacity();
3128   value_type* __p;
3129   if (__cap == __sz) {
3130     __grow_by_without_replace(__cap, 1, __sz, __ip, 0, 1);
3131     __p = std::__to_address(__get_long_pointer());
3132   } else {
3133     __annotate_increase(1);
3134     __p                = std::__to_address(__get_pointer());
3135     size_type __n_move = __sz - __ip;
3136     if (__n_move != 0)
3137       traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
3138   }
3139   traits_type::assign(__p[__ip], __c);
3140   traits_type::assign(__p[++__sz], value_type());
3141   __set_size(__sz);
3142   return begin() + static_cast<difference_type>(__ip);
3143 }
3144 
3145 // replace
3146 
3147 template <class _CharT, class _Traits, class _Allocator>
3148 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
3149 basic_string<_CharT, _Traits, _Allocator>::replace(
3150     size_type __pos, size_type __n1, const value_type* __s, size_type __n2)
3151     _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK {
3152   _LIBCPP_ASSERT_NON_NULL(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
3153   size_type __sz = size();
3154   if (__pos > __sz)
3155     __throw_out_of_range();
3156   __n1            = std::min(__n1, __sz - __pos);
3157   size_type __cap = capacity();
3158   if (__cap - __sz + __n1 >= __n2) {
3159     value_type* __p = std::__to_address(__get_pointer());
3160     if (__n1 != __n2) {
3161       if (__n2 > __n1)
3162         __annotate_increase(__n2 - __n1);
3163       size_type __n_move = __sz - __pos - __n1;
3164       if (__n_move != 0) {
3165         if (__n1 > __n2) {
3166           traits_type::move(__p + __pos, __s, __n2);
3167           traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3168           return __null_terminate_at(__p, __sz + (__n2 - __n1));
3169         }
3170         if (std::__is_pointer_in_range(__p + __pos + 1, __p + __sz, __s)) {
3171           if (__p + __pos + __n1 <= __s)
3172             __s += __n2 - __n1;
3173           else // __p + __pos < __s < __p + __pos + __n1
3174           {
3175             traits_type::move(__p + __pos, __s, __n1);
3176             __pos += __n1;
3177             __s += __n2;
3178             __n2 -= __n1;
3179             __n1 = 0;
3180           }
3181         }
3182         traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3183       }
3184     }
3185     traits_type::move(__p + __pos, __s, __n2);
3186     return __null_terminate_at(__p, __sz + (__n2 - __n1));
3187   } else
3188     __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
3189   return *this;
3190 }
3191 
3192 template <class _CharT, class _Traits, class _Allocator>
3193 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
3194 basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c) {
3195   size_type __sz = size();
3196   if (__pos > __sz)
3197     __throw_out_of_range();
3198   __n1            = std::min(__n1, __sz - __pos);
3199   size_type __cap = capacity();
3200   value_type* __p;
3201   if (__cap - __sz + __n1 >= __n2) {
3202     __p = std::__to_address(__get_pointer());
3203     if (__n1 != __n2) {
3204       if (__n2 > __n1)
3205         __annotate_increase(__n2 - __n1);
3206       size_type __n_move = __sz - __pos - __n1;
3207       if (__n_move != 0)
3208         traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3209     }
3210   } else {
3211     __grow_by_without_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
3212     __p = std::__to_address(__get_long_pointer());
3213   }
3214   traits_type::assign(__p + __pos, __n2, __c);
3215   return __null_terminate_at(__p, __sz - (__n1 - __n2));
3216 }
3217 
3218 template <class _CharT, class _Traits, class _Allocator>
3219 template <class _InputIterator, __enable_if_t<__has_input_iterator_category<_InputIterator>::value, int> >
3220 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
3221 basic_string<_CharT, _Traits, _Allocator>::replace(
3222     const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2) {
3223   const basic_string __temp(__j1, __j2, __alloc_);
3224   return replace(__i1, __i2, __temp);
3225 }
3226 
3227 template <class _CharT, class _Traits, class _Allocator>
3228 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
3229 basic_string<_CharT, _Traits, _Allocator>::replace(
3230     size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2) {
3231   size_type __str_sz = __str.size();
3232   if (__pos2 > __str_sz)
3233     __throw_out_of_range();
3234   return replace(__pos1, __n1, __str.data() + __pos2, std::min(__n2, __str_sz - __pos2));
3235 }
3236 
3237 template <class _CharT, class _Traits, class _Allocator>
3238 template <class _Tp,
3239           __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
3240                             !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
3241                         int> >
3242 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
3243 basic_string<_CharT, _Traits, _Allocator>::replace(
3244     size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2) {
3245   __self_view __sv   = __t;
3246   size_type __str_sz = __sv.size();
3247   if (__pos2 > __str_sz)
3248     __throw_out_of_range();
3249   return replace(__pos1, __n1, __sv.data() + __pos2, std::min(__n2, __str_sz - __pos2));
3250 }
3251 
3252 template <class _CharT, class _Traits, class _Allocator>
3253 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
3254 basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s) {
3255   _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::replace received nullptr");
3256   return replace(__pos, __n1, __s, traits_type::length(__s));
3257 }
3258 
3259 // erase
3260 
3261 // 'externally instantiated' erase() implementation, called when __n != npos.
3262 // Does not check __pos against size()
3263 template <class _CharT, class _Traits, class _Allocator>
3264 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NOINLINE void
3265 basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(size_type __pos, size_type __n) {
3266   if (__n) {
3267     size_type __sz     = size();
3268     value_type* __p    = std::__to_address(__get_pointer());
3269     __n                = std::min(__n, __sz - __pos);
3270     size_type __n_move = __sz - __pos - __n;
3271     if (__n_move != 0)
3272       traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
3273     __null_terminate_at(__p, __sz - __n);
3274   }
3275 }
3276 
3277 template <class _CharT, class _Traits, class _Allocator>
3278 _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>&
3279 basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos, size_type __n) {
3280   if (__pos > size())
3281     __throw_out_of_range();
3282   if (__n == npos) {
3283     __erase_to_end(__pos);
3284   } else {
3285     __erase_external_with_move(__pos, __n);
3286   }
3287   return *this;
3288 }
3289 
3290 template <class _CharT, class _Traits, class _Allocator>
3291 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::iterator
3292 basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos) {
3293   _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
3294       __pos != end(), "string::erase(iterator) called with a non-dereferenceable iterator");
3295   iterator __b  = begin();
3296   size_type __r = static_cast<size_type>(__pos - __b);
3297   erase(__r, 1);
3298   return __b + static_cast<difference_type>(__r);
3299 }
3300 
3301 template <class _CharT, class _Traits, class _Allocator>
3302 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::iterator
3303 basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last) {
3304   _LIBCPP_ASSERT_VALID_INPUT_RANGE(__first <= __last, "string::erase(first, last) called with invalid range");
3305   iterator __b  = begin();
3306   size_type __r = static_cast<size_type>(__first - __b);
3307   erase(__r, static_cast<size_type>(__last - __first));
3308   return __b + static_cast<difference_type>(__r);
3309 }
3310 
3311 template <class _CharT, class _Traits, class _Allocator>
3312 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::pop_back() {
3313   _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "string::pop_back(): string is already empty");
3314   __erase_to_end(size() - 1);
3315 }
3316 
3317 template <class _CharT, class _Traits, class _Allocator>
3318 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT {
3319   size_type __old_size = size();
3320   if (__is_long()) {
3321     traits_type::assign(*__get_long_pointer(), value_type());
3322     __set_long_size(0);
3323   } else {
3324     traits_type::assign(*__get_short_pointer(), value_type());
3325     __set_short_size(0);
3326   }
3327   __annotate_shrink(__old_size);
3328 }
3329 
3330 template <class _CharT, class _Traits, class _Allocator>
3331 _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c) {
3332   size_type __sz = size();
3333   if (__n > __sz)
3334     append(__n - __sz, __c);
3335   else
3336     __erase_to_end(__n);
3337 }
3338 
3339 template <class _CharT, class _Traits, class _Allocator>
3340 _LIBCPP_CONSTEXPR_SINCE_CXX20 inline void
3341 basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n) {
3342   size_type __sz = size();
3343   if (__n > __sz) {
3344     __append_default_init(__n - __sz);
3345   } else
3346     __erase_to_end(__n);
3347 }
3348 
3349 template <class _CharT, class _Traits, class _Allocator>
3350 _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __requested_capacity) {
3351   if (__requested_capacity > max_size())
3352     __throw_length_error();
3353 
3354   // Make sure reserve(n) never shrinks. This is technically only required in C++20
3355   // and later (since P0966R1), however we provide consistent behavior in all Standard
3356   // modes because this function is instantiated in the shared library.
3357   if (__requested_capacity <= capacity())
3358     return;
3359 
3360   __shrink_or_extend(__recommend(__requested_capacity));
3361 }
3362 
3363 template <class _CharT, class _Traits, class _Allocator>
3364 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT {
3365   size_type __target_capacity = __recommend(size());
3366   if (__target_capacity == capacity())
3367     return;
3368 
3369   __shrink_or_extend(__target_capacity);
3370 }
3371 
3372 template <class _CharT, class _Traits, class _Allocator>
3373 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void
3374 basic_string<_CharT, _Traits, _Allocator>::__shrink_or_extend(size_type __target_capacity) {
3375   __annotate_delete();
3376   auto __guard    = std::__make_scope_guard(__annotate_new_size(*this));
3377   size_type __cap = capacity();
3378   size_type __sz  = size();
3379 
3380   pointer __new_data, __p;
3381   bool __was_long, __now_long;
3382   if (__fits_in_sso(__target_capacity)) {
3383     __was_long = true;
3384     __now_long = false;
3385     __new_data = __get_short_pointer();
3386     __p        = __get_long_pointer();
3387   } else {
3388     if (__target_capacity > __cap) {
3389       // Extend
3390       // - called from reserve should propagate the exception thrown.
3391       auto __allocation = std::__allocate_at_least(__alloc_, __target_capacity + 1);
3392       __new_data        = __allocation.ptr;
3393       __target_capacity = __allocation.count - 1;
3394     } else {
3395       // Shrink
3396       // - called from shrink_to_fit should not throw.
3397       // - called from reserve may throw but is not required to.
3398 #  if _LIBCPP_HAS_EXCEPTIONS
3399       try {
3400 #  endif // _LIBCPP_HAS_EXCEPTIONS
3401         auto __allocation = std::__allocate_at_least(__alloc_, __target_capacity + 1);
3402 
3403         // The Standard mandates shrink_to_fit() does not increase the capacity.
3404         // With equal capacity keep the existing buffer. This avoids extra work
3405         // due to swapping the elements.
3406         if (__allocation.count - 1 > capacity()) {
3407           __alloc_traits::deallocate(__alloc_, __allocation.ptr, __allocation.count);
3408           return;
3409         }
3410         __new_data        = __allocation.ptr;
3411         __target_capacity = __allocation.count - 1;
3412 #  if _LIBCPP_HAS_EXCEPTIONS
3413       } catch (...) {
3414         return;
3415       }
3416 #  endif // _LIBCPP_HAS_EXCEPTIONS
3417     }
3418     __begin_lifetime(__new_data, __target_capacity + 1);
3419     __now_long = true;
3420     __was_long = __is_long();
3421     __p        = __get_pointer();
3422   }
3423   traits_type::copy(std::__to_address(__new_data), std::__to_address(__p), size() + 1);
3424   if (__was_long)
3425     __alloc_traits::deallocate(__alloc_, __p, __cap + 1);
3426   if (__now_long) {
3427     __set_long_cap(__target_capacity + 1);
3428     __set_long_size(__sz);
3429     __set_long_pointer(__new_data);
3430   } else
3431     __set_short_size(__sz);
3432 }
3433 
3434 template <class _CharT, class _Traits, class _Allocator>
3435 _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3436 basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const {
3437   if (__n >= size())
3438     __throw_out_of_range();
3439   return (*this)[__n];
3440 }
3441 
3442 template <class _CharT, class _Traits, class _Allocator>
3443 _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::reference
3444 basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) {
3445   if (__n >= size())
3446     __throw_out_of_range();
3447   return (*this)[__n];
3448 }
3449 
3450 template <class _CharT, class _Traits, class _Allocator>
3451 _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
3452 basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const {
3453   size_type __sz = size();
3454   if (__pos > __sz)
3455     __throw_out_of_range();
3456   size_type __rlen = std::min(__n, __sz - __pos);
3457   traits_type::copy(__s, data() + __pos, __rlen);
3458   return __rlen;
3459 }
3460 
3461 template <class _CharT, class _Traits, class _Allocator>
3462 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
3463 #  if _LIBCPP_STD_VER >= 14
3464     _NOEXCEPT
3465 #  else
3466     _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v<allocator_type>)
3467 #  endif
3468 {
3469   _LIBCPP_ASSERT_COMPATIBLE_ALLOCATOR(
3470       __alloc_traits::propagate_on_container_swap::value || __alloc_traits::is_always_equal::value ||
3471           __alloc_ == __str.__alloc_,
3472       "swapping non-equal allocators");
3473   if (!__is_long())
3474     __annotate_delete();
3475   if (this != std::addressof(__str) && !__str.__is_long())
3476     __str.__annotate_delete();
3477   std::swap(__rep_, __str.__rep_);
3478   std::__swap_allocator(__alloc_, __str.__alloc_);
3479   if (!__is_long())
3480     __annotate_new(__get_short_size());
3481   if (this != std::addressof(__str) && !__str.__is_long())
3482     __str.__annotate_new(__str.__get_short_size());
3483 }
3484 
3485 // find
3486 
3487 template <class _CharT, class _Traits, class _Allocator>
3488 _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
3489 basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT {
3490   _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string::find(): received nullptr");
3491   return std::__str_find<value_type, size_type, traits_type, npos>(data(), size(), __s, __pos, __n);
3492 }
3493 
3494 template <class _CharT, class _Traits, class _Allocator>
3495 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
3496 basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str, size_type __pos) const _NOEXCEPT {
3497   return std::__str_find<value_type, size_type, traits_type, npos>(data(), size(), __str.data(), __pos, __str.size());
3498 }
3499 
3500 template <class _CharT, class _Traits, class _Allocator>
3501 template <class _Tp, __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, int> >
3502 _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
3503 basic_string<_CharT, _Traits, _Allocator>::find(const _Tp& __t, size_type __pos) const _NOEXCEPT {
3504   __self_view __sv = __t;
3505   return std::__str_find<value_type, size_type, traits_type, npos>(data(), size(), __sv.data(), __pos, __sv.size());
3506 }
3507 
3508 template <class _CharT, class _Traits, class _Allocator>
3509 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
3510 basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s, size_type __pos) const _NOEXCEPT {
3511   _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::find(): received nullptr");
3512   return std::__str_find<value_type, size_type, traits_type, npos>(
3513       data(), size(), __s, __pos, traits_type::length(__s));
3514 }
3515 
3516 template <class _CharT, class _Traits, class _Allocator>
3517 _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
3518 basic_string<_CharT, _Traits, _Allocator>::find(value_type __c, size_type __pos) const _NOEXCEPT {
3519   return std::__str_find<value_type, size_type, traits_type, npos>(data(), size(), __c, __pos);
3520 }
3521 
3522 // rfind
3523 
3524 template <class _CharT, class _Traits, class _Allocator>
3525 _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
3526 basic_string<_CharT, _Traits, _Allocator>::rfind(
3527     const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT {
3528   _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
3529   return std::__str_rfind<value_type, size_type, traits_type, npos>(data(), size(), __s, __pos, __n);
3530 }
3531 
3532 template <class _CharT, class _Traits, class _Allocator>
3533 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
3534 basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str, size_type __pos) const _NOEXCEPT {
3535   return std::__str_rfind<value_type, size_type, traits_type, npos>(data(), size(), __str.data(), __pos, __str.size());
3536 }
3537 
3538 template <class _CharT, class _Traits, class _Allocator>
3539 template <class _Tp, __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, int> >
3540 _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
3541 basic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t, size_type __pos) const _NOEXCEPT {
3542   __self_view __sv = __t;
3543   return std::__str_rfind<value_type, size_type, traits_type, npos>(data(), size(), __sv.data(), __pos, __sv.size());
3544 }
3545 
3546 template <class _CharT, class _Traits, class _Allocator>
3547 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
3548 basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s, size_type __pos) const _NOEXCEPT {
3549   _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::rfind(): received nullptr");
3550   return std::__str_rfind<value_type, size_type, traits_type, npos>(
3551       data(), size(), __s, __pos, traits_type::length(__s));
3552 }
3553 
3554 template <class _CharT, class _Traits, class _Allocator>
3555 _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
3556 basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c, size_type __pos) const _NOEXCEPT {
3557   return std::__str_rfind<value_type, size_type, traits_type, npos>(data(), size(), __c, __pos);
3558 }
3559 
3560 // find_first_of
3561 
3562 template <class _CharT, class _Traits, class _Allocator>
3563 _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
3564 basic_string<_CharT, _Traits, _Allocator>::find_first_of(
3565     const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT {
3566   _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
3567   return std::__str_find_first_of<value_type, size_type, traits_type, npos>(data(), size(), __s, __pos, __n);
3568 }
3569 
3570 template <class _CharT, class _Traits, class _Allocator>
3571 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
3572 basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str, size_type __pos) const _NOEXCEPT {
3573   return std::__str_find_first_of<value_type, size_type, traits_type, npos>(
3574       data(), size(), __str.data(), __pos, __str.size());
3575 }
3576 
3577 template <class _CharT, class _Traits, class _Allocator>
3578 template <class _Tp, __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, int> >
3579 _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
3580 basic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t, size_type __pos) const _NOEXCEPT {
3581   __self_view __sv = __t;
3582   return std::__str_find_first_of<value_type, size_type, traits_type, npos>(
3583       data(), size(), __sv.data(), __pos, __sv.size());
3584 }
3585 
3586 template <class _CharT, class _Traits, class _Allocator>
3587 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
3588 basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s, size_type __pos) const _NOEXCEPT {
3589   _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::find_first_of(): received nullptr");
3590   return std::__str_find_first_of<value_type, size_type, traits_type, npos>(
3591       data(), size(), __s, __pos, traits_type::length(__s));
3592 }
3593 
3594 template <class _CharT, class _Traits, class _Allocator>
3595 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
3596 basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c, size_type __pos) const _NOEXCEPT {
3597   return find(__c, __pos);
3598 }
3599 
3600 // find_last_of
3601 
3602 template <class _CharT, class _Traits, class _Allocator>
3603 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
3604 basic_string<_CharT, _Traits, _Allocator>::find_last_of(
3605     const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT {
3606   _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
3607   return std::__str_find_last_of<value_type, size_type, traits_type, npos>(data(), size(), __s, __pos, __n);
3608 }
3609 
3610 template <class _CharT, class _Traits, class _Allocator>
3611 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
3612 basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str, size_type __pos) const _NOEXCEPT {
3613   return std::__str_find_last_of<value_type, size_type, traits_type, npos>(
3614       data(), size(), __str.data(), __pos, __str.size());
3615 }
3616 
3617 template <class _CharT, class _Traits, class _Allocator>
3618 template <class _Tp, __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, int> >
3619 _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
3620 basic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t, size_type __pos) const _NOEXCEPT {
3621   __self_view __sv = __t;
3622   return std::__str_find_last_of<value_type, size_type, traits_type, npos>(
3623       data(), size(), __sv.data(), __pos, __sv.size());
3624 }
3625 
3626 template <class _CharT, class _Traits, class _Allocator>
3627 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
3628 basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s, size_type __pos) const _NOEXCEPT {
3629   _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::find_last_of(): received nullptr");
3630   return std::__str_find_last_of<value_type, size_type, traits_type, npos>(
3631       data(), size(), __s, __pos, traits_type::length(__s));
3632 }
3633 
3634 template <class _CharT, class _Traits, class _Allocator>
3635 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
3636 basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c, size_type __pos) const _NOEXCEPT {
3637   return rfind(__c, __pos);
3638 }
3639 
3640 // find_first_not_of
3641 
3642 template <class _CharT, class _Traits, class _Allocator>
3643 _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
3644 basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(
3645     const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT {
3646   _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
3647   return std::__str_find_first_not_of<value_type, size_type, traits_type, npos>(data(), size(), __s, __pos, __n);
3648 }
3649 
3650 template <class _CharT, class _Traits, class _Allocator>
3651 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
3652 basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(
3653     const basic_string& __str, size_type __pos) const _NOEXCEPT {
3654   return std::__str_find_first_not_of<value_type, size_type, traits_type, npos>(
3655       data(), size(), __str.data(), __pos, __str.size());
3656 }
3657 
3658 template <class _CharT, class _Traits, class _Allocator>
3659 template <class _Tp, __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, int> >
3660 _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
3661 basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t, size_type __pos) const _NOEXCEPT {
3662   __self_view __sv = __t;
3663   return std::__str_find_first_not_of<value_type, size_type, traits_type, npos>(
3664       data(), size(), __sv.data(), __pos, __sv.size());
3665 }
3666 
3667 template <class _CharT, class _Traits, class _Allocator>
3668 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
3669 basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s, size_type __pos) const _NOEXCEPT {
3670   _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::find_first_not_of(): received nullptr");
3671   return std::__str_find_first_not_of<value_type, size_type, traits_type, npos>(
3672       data(), size(), __s, __pos, traits_type::length(__s));
3673 }
3674 
3675 template <class _CharT, class _Traits, class _Allocator>
3676 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
3677 basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c, size_type __pos) const _NOEXCEPT {
3678   return std::__str_find_first_not_of<value_type, size_type, traits_type, npos>(data(), size(), __c, __pos);
3679 }
3680 
3681 // find_last_not_of
3682 
3683 template <class _CharT, class _Traits, class _Allocator>
3684 _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
3685 basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(
3686     const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT {
3687   _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
3688   return std::__str_find_last_not_of<value_type, size_type, traits_type, npos>(data(), size(), __s, __pos, __n);
3689 }
3690 
3691 template <class _CharT, class _Traits, class _Allocator>
3692 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
3693 basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(
3694     const basic_string& __str, size_type __pos) const _NOEXCEPT {
3695   return std::__str_find_last_not_of<value_type, size_type, traits_type, npos>(
3696       data(), size(), __str.data(), __pos, __str.size());
3697 }
3698 
3699 template <class _CharT, class _Traits, class _Allocator>
3700 template <class _Tp, __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, int> >
3701 _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
3702 basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t, size_type __pos) const _NOEXCEPT {
3703   __self_view __sv = __t;
3704   return std::__str_find_last_not_of<value_type, size_type, traits_type, npos>(
3705       data(), size(), __sv.data(), __pos, __sv.size());
3706 }
3707 
3708 template <class _CharT, class _Traits, class _Allocator>
3709 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
3710 basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s, size_type __pos) const _NOEXCEPT {
3711   _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::find_last_not_of(): received nullptr");
3712   return std::__str_find_last_not_of<value_type, size_type, traits_type, npos>(
3713       data(), size(), __s, __pos, traits_type::length(__s));
3714 }
3715 
3716 template <class _CharT, class _Traits, class _Allocator>
3717 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 typename basic_string<_CharT, _Traits, _Allocator>::size_type
3718 basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c, size_type __pos) const _NOEXCEPT {
3719   return std::__str_find_last_not_of<value_type, size_type, traits_type, npos>(data(), size(), __c, __pos);
3720 }
3721 
3722 // compare
3723 
3724 template <class _CharT, class _Traits, class _Allocator>
3725 template <class _Tp, __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, int> >
3726 _LIBCPP_CONSTEXPR_SINCE_CXX20 int basic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const _NOEXCEPT {
3727   __self_view __sv = __t;
3728   size_t __lhs_sz  = size();
3729   size_t __rhs_sz  = __sv.size();
3730   int __result     = traits_type::compare(data(), __sv.data(), std::min(__lhs_sz, __rhs_sz));
3731   if (__result != 0)
3732     return __result;
3733   if (__lhs_sz < __rhs_sz)
3734     return -1;
3735   if (__lhs_sz > __rhs_sz)
3736     return 1;
3737   return 0;
3738 }
3739 
3740 template <class _CharT, class _Traits, class _Allocator>
3741 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 int
3742 basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT {
3743   return compare(__self_view(__str));
3744 }
3745 
3746 template <class _CharT, class _Traits, class _Allocator>
3747 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 int basic_string<_CharT, _Traits, _Allocator>::compare(
3748     size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const {
3749   _LIBCPP_ASSERT_NON_NULL(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
3750   size_type __sz = size();
3751   if (__pos1 > __sz || __n2 == npos)
3752     __throw_out_of_range();
3753   size_type __rlen = std::min(__n1, __sz - __pos1);
3754   int __r          = traits_type::compare(data() + __pos1, __s, std::min(__rlen, __n2));
3755   if (__r == 0) {
3756     if (__rlen < __n2)
3757       __r = -1;
3758     else if (__rlen > __n2)
3759       __r = 1;
3760   }
3761   return __r;
3762 }
3763 
3764 template <class _CharT, class _Traits, class _Allocator>
3765 template <class _Tp, __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, int> >
3766 _LIBCPP_CONSTEXPR_SINCE_CXX20 int
3767 basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1, size_type __n1, const _Tp& __t) const {
3768   __self_view __sv = __t;
3769   return compare(__pos1, __n1, __sv.data(), __sv.size());
3770 }
3771 
3772 template <class _CharT, class _Traits, class _Allocator>
3773 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 int
3774 basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1, size_type __n1, const basic_string& __str) const {
3775   return compare(__pos1, __n1, __str.data(), __str.size());
3776 }
3777 
3778 template <class _CharT, class _Traits, class _Allocator>
3779 template <class _Tp,
3780           __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
3781                             !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
3782                         int> >
3783 _LIBCPP_CONSTEXPR_SINCE_CXX20 int basic_string<_CharT, _Traits, _Allocator>::compare(
3784     size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2) const {
3785   __self_view __sv = __t;
3786   return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3787 }
3788 
3789 template <class _CharT, class _Traits, class _Allocator>
3790 _LIBCPP_CONSTEXPR_SINCE_CXX20 int basic_string<_CharT, _Traits, _Allocator>::compare(
3791     size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2) const {
3792   return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
3793 }
3794 
3795 template <class _CharT, class _Traits, class _Allocator>
3796 _LIBCPP_CONSTEXPR_SINCE_CXX20 int
3797 basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT {
3798   _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::compare(): received nullptr");
3799   return compare(0, npos, __s, traits_type::length(__s));
3800 }
3801 
3802 template <class _CharT, class _Traits, class _Allocator>
3803 _LIBCPP_CONSTEXPR_SINCE_CXX20 int
3804 basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1, size_type __n1, const value_type* __s) const {
3805   _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string::compare(): received nullptr");
3806   return compare(__pos1, __n1, __s, traits_type::length(__s));
3807 }
3808 
3809 // __invariants
3810 
3811 template <class _CharT, class _Traits, class _Allocator>
3812 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 bool basic_string<_CharT, _Traits, _Allocator>::__invariants() const {
3813   if (size() > capacity())
3814     return false;
3815   if (capacity() < __min_cap - 1)
3816     return false;
3817   if (data() == nullptr)
3818     return false;
3819   if (!_Traits::eq(data()[size()], value_type()))
3820     return false;
3821   return true;
3822 }
3823 
3824 // __clear_and_shrink
3825 
3826 template <class _CharT, class _Traits, class _Allocator>
3827 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT {
3828   clear();
3829   if (__is_long()) {
3830     __annotate_delete();
3831     __alloc_traits::deallocate(__alloc_, __get_long_pointer(), capacity() + 1);
3832     __rep_ = __rep();
3833   }
3834 }
3835 
3836 // operator==
3837 
3838 template <class _CharT, class _Traits, class _Allocator>
3839 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool
3840 operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3841            const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {
3842   size_t __lhs_sz = __lhs.size();
3843   return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(), __rhs.data(), __lhs_sz) == 0;
3844 }
3845 
3846 template <class _CharT, class _Traits, class _Allocator>
3847 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool
3848 operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) _NOEXCEPT {
3849   _LIBCPP_ASSERT_NON_NULL(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
3850 
3851   using _String = basic_string<_CharT, _Traits, _Allocator>;
3852 
3853   size_t __rhs_len = _Traits::length(__rhs);
3854   if (__builtin_constant_p(__rhs_len) && !_String::__fits_in_sso(__rhs_len)) {
3855     if (!__lhs.__is_long())
3856       return false;
3857   }
3858   if (__rhs_len != __lhs.size())
3859     return false;
3860   return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
3861 }
3862 
3863 #  if _LIBCPP_STD_VER <= 17
3864 template <class _CharT, class _Traits, class _Allocator>
3865 inline _LIBCPP_HIDE_FROM_ABI bool
3866 operator==(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {
3867   return __rhs == __lhs;
3868 }
3869 #  endif // _LIBCPP_STD_VER <= 17
3870 
3871 #  if _LIBCPP_STD_VER >= 20
3872 
3873 template <class _CharT, class _Traits, class _Allocator>
3874 _LIBCPP_HIDE_FROM_ABI constexpr auto operator<=>(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3875                                                  const basic_string<_CharT, _Traits, _Allocator>& __rhs) noexcept {
3876   return basic_string_view<_CharT, _Traits>(__lhs) <=> basic_string_view<_CharT, _Traits>(__rhs);
3877 }
3878 
3879 template <class _CharT, class _Traits, class _Allocator>
3880 _LIBCPP_HIDE_FROM_ABI constexpr auto
3881 operator<=>(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) {
3882   return basic_string_view<_CharT, _Traits>(__lhs) <=> basic_string_view<_CharT, _Traits>(__rhs);
3883 }
3884 
3885 #  else  // _LIBCPP_STD_VER >= 20
3886 
3887 template <class _CharT, class _Traits, class _Allocator>
3888 inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3889                                              const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {
3890   return !(__lhs == __rhs);
3891 }
3892 
3893 template <class _CharT, class _Traits, class _Allocator>
3894 inline _LIBCPP_HIDE_FROM_ABI bool
3895 operator!=(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {
3896   return !(__lhs == __rhs);
3897 }
3898 
3899 template <class _CharT, class _Traits, class _Allocator>
3900 inline _LIBCPP_HIDE_FROM_ABI bool
3901 operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) _NOEXCEPT {
3902   return !(__lhs == __rhs);
3903 }
3904 
3905 // operator<
3906 
3907 template <class _CharT, class _Traits, class _Allocator>
3908 inline _LIBCPP_HIDE_FROM_ABI bool operator<(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3909                                             const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {
3910   return __lhs.compare(__rhs) < 0;
3911 }
3912 
3913 template <class _CharT, class _Traits, class _Allocator>
3914 inline _LIBCPP_HIDE_FROM_ABI bool
3915 operator<(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) _NOEXCEPT {
3916   return __lhs.compare(__rhs) < 0;
3917 }
3918 
3919 template <class _CharT, class _Traits, class _Allocator>
3920 inline _LIBCPP_HIDE_FROM_ABI bool
3921 operator<(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {
3922   return __rhs.compare(__lhs) > 0;
3923 }
3924 
3925 // operator>
3926 
3927 template <class _CharT, class _Traits, class _Allocator>
3928 inline _LIBCPP_HIDE_FROM_ABI bool operator>(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3929                                             const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {
3930   return __rhs < __lhs;
3931 }
3932 
3933 template <class _CharT, class _Traits, class _Allocator>
3934 inline _LIBCPP_HIDE_FROM_ABI bool
3935 operator>(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) _NOEXCEPT {
3936   return __rhs < __lhs;
3937 }
3938 
3939 template <class _CharT, class _Traits, class _Allocator>
3940 inline _LIBCPP_HIDE_FROM_ABI bool
3941 operator>(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {
3942   return __rhs < __lhs;
3943 }
3944 
3945 // operator<=
3946 
3947 template <class _CharT, class _Traits, class _Allocator>
3948 inline _LIBCPP_HIDE_FROM_ABI bool operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3949                                              const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {
3950   return !(__rhs < __lhs);
3951 }
3952 
3953 template <class _CharT, class _Traits, class _Allocator>
3954 inline _LIBCPP_HIDE_FROM_ABI bool
3955 operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) _NOEXCEPT {
3956   return !(__rhs < __lhs);
3957 }
3958 
3959 template <class _CharT, class _Traits, class _Allocator>
3960 inline _LIBCPP_HIDE_FROM_ABI bool
3961 operator<=(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {
3962   return !(__rhs < __lhs);
3963 }
3964 
3965 // operator>=
3966 
3967 template <class _CharT, class _Traits, class _Allocator>
3968 inline _LIBCPP_HIDE_FROM_ABI bool operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3969                                              const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {
3970   return !(__lhs < __rhs);
3971 }
3972 
3973 template <class _CharT, class _Traits, class _Allocator>
3974 inline _LIBCPP_HIDE_FROM_ABI bool
3975 operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) _NOEXCEPT {
3976   return !(__lhs < __rhs);
3977 }
3978 
3979 template <class _CharT, class _Traits, class _Allocator>
3980 inline _LIBCPP_HIDE_FROM_ABI bool
3981 operator>=(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT {
3982   return !(__lhs < __rhs);
3983 }
3984 #  endif // _LIBCPP_STD_VER >= 20
3985 
3986 // operator +
3987 
3988 template <class _CharT, class _Traits, class _Allocator>
3989 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
3990 operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3991           const basic_string<_CharT, _Traits, _Allocator>& __rhs) {
3992   using _String = basic_string<_CharT, _Traits, _Allocator>;
3993   auto __lhs_sz = __lhs.size();
3994   auto __rhs_sz = __rhs.size();
3995   _String __r(__uninitialized_size_tag(),
3996               __lhs_sz + __rhs_sz,
3997               _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
3998   auto __ptr = std::__to_address(__r.__get_pointer());
3999   _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
4000   _Traits::copy(__ptr + __lhs_sz, __rhs.data(), __rhs_sz);
4001   _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
4002   return __r;
4003 }
4004 
4005 template <class _CharT, class _Traits, class _Allocator>
4006 _LIBCPP_HIDDEN _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
4007 operator+(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) {
4008   using _String = basic_string<_CharT, _Traits, _Allocator>;
4009   auto __lhs_sz = _Traits::length(__lhs);
4010   auto __rhs_sz = __rhs.size();
4011   _String __r(__uninitialized_size_tag(),
4012               __lhs_sz + __rhs_sz,
4013               _String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator()));
4014   auto __ptr = std::__to_address(__r.__get_pointer());
4015   _Traits::copy(__ptr, __lhs, __lhs_sz);
4016   _Traits::copy(__ptr + __lhs_sz, __rhs.data(), __rhs_sz);
4017   _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
4018   return __r;
4019 }
4020 
4021 template <class _CharT, class _Traits, class _Allocator>
4022 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
4023 operator+(_CharT __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) {
4024   using _String                        = basic_string<_CharT, _Traits, _Allocator>;
4025   typename _String::size_type __rhs_sz = __rhs.size();
4026   _String __r(__uninitialized_size_tag(),
4027               __rhs_sz + 1,
4028               _String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator()));
4029   auto __ptr = std::__to_address(__r.__get_pointer());
4030   _Traits::assign(__ptr, 1, __lhs);
4031   _Traits::copy(__ptr + 1, __rhs.data(), __rhs_sz);
4032   _Traits::assign(__ptr + 1 + __rhs_sz, 1, _CharT());
4033   return __r;
4034 }
4035 
4036 template <class _CharT, class _Traits, class _Allocator>
4037 inline _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
4038 operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs) {
4039   using _String                        = basic_string<_CharT, _Traits, _Allocator>;
4040   typename _String::size_type __lhs_sz = __lhs.size();
4041   typename _String::size_type __rhs_sz = _Traits::length(__rhs);
4042   _String __r(__uninitialized_size_tag(),
4043               __lhs_sz + __rhs_sz,
4044               _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4045   auto __ptr = std::__to_address(__r.__get_pointer());
4046   _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
4047   _Traits::copy(__ptr + __lhs_sz, __rhs, __rhs_sz);
4048   _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
4049   return __r;
4050 }
4051 
4052 template <class _CharT, class _Traits, class _Allocator>
4053 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
4054 operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs) {
4055   using _String                        = basic_string<_CharT, _Traits, _Allocator>;
4056   typename _String::size_type __lhs_sz = __lhs.size();
4057   _String __r(__uninitialized_size_tag(),
4058               __lhs_sz + 1,
4059               _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4060   auto __ptr = std::__to_address(__r.__get_pointer());
4061   _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
4062   _Traits::assign(__ptr + __lhs_sz, 1, __rhs);
4063   _Traits::assign(__ptr + 1 + __lhs_sz, 1, _CharT());
4064   return __r;
4065 }
4066 
4067 #  ifndef _LIBCPP_CXX03_LANG
4068 
4069 template <class _CharT, class _Traits, class _Allocator>
4070 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
4071 operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs) {
4072   return std::move(__lhs.append(__rhs));
4073 }
4074 
4075 template <class _CharT, class _Traits, class _Allocator>
4076 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
4077 operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs) {
4078   return std::move(__rhs.insert(0, __lhs));
4079 }
4080 
4081 template <class _CharT, class _Traits, class _Allocator>
4082 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
4083 operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs) {
4084   return std::move(__lhs.append(__rhs));
4085 }
4086 
4087 template <class _CharT, class _Traits, class _Allocator>
4088 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
4089 operator+(const _CharT* __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs) {
4090   return std::move(__rhs.insert(0, __lhs));
4091 }
4092 
4093 template <class _CharT, class _Traits, class _Allocator>
4094 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
4095 operator+(_CharT __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs) {
4096   __rhs.insert(__rhs.begin(), __lhs);
4097   return std::move(__rhs);
4098 }
4099 
4100 template <class _CharT, class _Traits, class _Allocator>
4101 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
4102 operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs) {
4103   return std::move(__lhs.append(__rhs));
4104 }
4105 
4106 template <class _CharT, class _Traits, class _Allocator>
4107 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<_CharT, _Traits, _Allocator>
4108 operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs) {
4109   __lhs.push_back(__rhs);
4110   return std::move(__lhs);
4111 }
4112 
4113 #  endif // _LIBCPP_CXX03_LANG
4114 
4115 #  if _LIBCPP_STD_VER >= 26
4116 
4117 template <class _CharT, class _Traits, class _Allocator>
4118 _LIBCPP_HIDE_FROM_ABI constexpr basic_string<_CharT, _Traits, _Allocator>
4119 operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4120           type_identity_t<basic_string_view<_CharT, _Traits>> __rhs) {
4121   using _String                        = basic_string<_CharT, _Traits, _Allocator>;
4122   typename _String::size_type __lhs_sz = __lhs.size();
4123   typename _String::size_type __rhs_sz = __rhs.size();
4124   _String __r(__uninitialized_size_tag(),
4125               __lhs_sz + __rhs_sz,
4126               _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4127   auto __ptr = std::__to_address(__r.__get_pointer());
4128   _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
4129   _Traits::copy(__ptr + __lhs_sz, __rhs.data(), __rhs_sz);
4130   _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
4131   return __r;
4132 }
4133 
4134 template <class _CharT, class _Traits, class _Allocator>
4135 _LIBCPP_HIDE_FROM_ABI constexpr basic_string<_CharT, _Traits, _Allocator>
4136 operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs,
4137           type_identity_t<basic_string_view<_CharT, _Traits>> __rhs) {
4138   __lhs.append(__rhs);
4139   return std::move(__lhs);
4140 }
4141 
4142 template <class _CharT, class _Traits, class _Allocator>
4143 _LIBCPP_HIDE_FROM_ABI constexpr basic_string<_CharT, _Traits, _Allocator>
4144 operator+(type_identity_t<basic_string_view<_CharT, _Traits>> __lhs,
4145           const basic_string<_CharT, _Traits, _Allocator>& __rhs) {
4146   using _String                        = basic_string<_CharT, _Traits, _Allocator>;
4147   typename _String::size_type __lhs_sz = __lhs.size();
4148   typename _String::size_type __rhs_sz = __rhs.size();
4149   _String __r(__uninitialized_size_tag(),
4150               __lhs_sz + __rhs_sz,
4151               _String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator()));
4152   auto __ptr = std::__to_address(__r.__get_pointer());
4153   _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
4154   _Traits::copy(__ptr + __lhs_sz, __rhs.data(), __rhs_sz);
4155   _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
4156   return __r;
4157 }
4158 
4159 template <class _CharT, class _Traits, class _Allocator>
4160 _LIBCPP_HIDE_FROM_ABI constexpr basic_string<_CharT, _Traits, _Allocator>
4161 operator+(type_identity_t<basic_string_view<_CharT, _Traits>> __lhs,
4162           basic_string<_CharT, _Traits, _Allocator>&& __rhs) {
4163   __rhs.insert(0, __lhs);
4164   return std::move(__rhs);
4165 }
4166 
4167 #  endif // _LIBCPP_STD_VER >= 26
4168 
4169 // swap
4170 
4171 template <class _CharT, class _Traits, class _Allocator>
4172 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
4173 swap(basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>& __rhs)
4174     _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs))) {
4175   __lhs.swap(__rhs);
4176 }
4177 
4178 _LIBCPP_EXPORTED_FROM_ABI int stoi(const string& __str, size_t* __idx = nullptr, int __base = 10);
4179 _LIBCPP_EXPORTED_FROM_ABI long stol(const string& __str, size_t* __idx = nullptr, int __base = 10);
4180 _LIBCPP_EXPORTED_FROM_ABI unsigned long stoul(const string& __str, size_t* __idx = nullptr, int __base = 10);
4181 _LIBCPP_EXPORTED_FROM_ABI long long stoll(const string& __str, size_t* __idx = nullptr, int __base = 10);
4182 _LIBCPP_EXPORTED_FROM_ABI unsigned long long stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);
4183 
4184 _LIBCPP_EXPORTED_FROM_ABI float stof(const string& __str, size_t* __idx = nullptr);
4185 _LIBCPP_EXPORTED_FROM_ABI double stod(const string& __str, size_t* __idx = nullptr);
4186 _LIBCPP_EXPORTED_FROM_ABI long double stold(const string& __str, size_t* __idx = nullptr);
4187 
4188 _LIBCPP_EXPORTED_FROM_ABI string to_string(int __val);
4189 _LIBCPP_EXPORTED_FROM_ABI string to_string(unsigned __val);
4190 _LIBCPP_EXPORTED_FROM_ABI string to_string(long __val);
4191 _LIBCPP_EXPORTED_FROM_ABI string to_string(unsigned long __val);
4192 _LIBCPP_EXPORTED_FROM_ABI string to_string(long long __val);
4193 _LIBCPP_EXPORTED_FROM_ABI string to_string(unsigned long long __val);
4194 _LIBCPP_EXPORTED_FROM_ABI string to_string(float __val);
4195 _LIBCPP_EXPORTED_FROM_ABI string to_string(double __val);
4196 _LIBCPP_EXPORTED_FROM_ABI string to_string(long double __val);
4197 
4198 #  if _LIBCPP_HAS_WIDE_CHARACTERS
4199 _LIBCPP_EXPORTED_FROM_ABI int stoi(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4200 _LIBCPP_EXPORTED_FROM_ABI long stol(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4201 _LIBCPP_EXPORTED_FROM_ABI unsigned long stoul(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4202 _LIBCPP_EXPORTED_FROM_ABI long long stoll(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4203 _LIBCPP_EXPORTED_FROM_ABI unsigned long long stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4204 
4205 _LIBCPP_EXPORTED_FROM_ABI float stof(const wstring& __str, size_t* __idx = nullptr);
4206 _LIBCPP_EXPORTED_FROM_ABI double stod(const wstring& __str, size_t* __idx = nullptr);
4207 _LIBCPP_EXPORTED_FROM_ABI long double stold(const wstring& __str, size_t* __idx = nullptr);
4208 
4209 _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(int __val);
4210 _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(unsigned __val);
4211 _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(long __val);
4212 _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(unsigned long __val);
4213 _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(long long __val);
4214 _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(unsigned long long __val);
4215 _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(float __val);
4216 _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(double __val);
4217 _LIBCPP_EXPORTED_FROM_ABI wstring to_wstring(long double __val);
4218 #  endif // _LIBCPP_HAS_WIDE_CHARACTERS
4219 
4220 template <class _CharT, class _Traits, class _Allocator>
4221 _LIBCPP_TEMPLATE_DATA_VIS const typename basic_string<_CharT, _Traits, _Allocator>::size_type
4222     basic_string<_CharT, _Traits, _Allocator>::npos;
4223 
4224 template <class _CharT, class _Allocator>
4225 struct __string_hash : public __unary_function<basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t> {
4226   _LIBCPP_HIDE_FROM_ABI size_t
4227   operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT {
4228     return std::__do_string_hash(__val.data(), __val.data() + __val.size());
4229   }
4230 };
4231 
4232 template <class _Allocator>
4233 struct hash<basic_string<char, char_traits<char>, _Allocator> > : __string_hash<char, _Allocator> {};
4234 
4235 #  if _LIBCPP_HAS_CHAR8_T
4236 template <class _Allocator>
4237 struct hash<basic_string<char8_t, char_traits<char8_t>, _Allocator> > : __string_hash<char8_t, _Allocator> {};
4238 #  endif
4239 
4240 template <class _Allocator>
4241 struct hash<basic_string<char16_t, char_traits<char16_t>, _Allocator> > : __string_hash<char16_t, _Allocator> {};
4242 
4243 template <class _Allocator>
4244 struct hash<basic_string<char32_t, char_traits<char32_t>, _Allocator> > : __string_hash<char32_t, _Allocator> {};
4245 
4246 #  if _LIBCPP_HAS_WIDE_CHARACTERS
4247 template <class _Allocator>
4248 struct hash<basic_string<wchar_t, char_traits<wchar_t>, _Allocator> > : __string_hash<wchar_t, _Allocator> {};
4249 #  endif
4250 
4251 template <class _CharT, class _Traits, class _Allocator>
4252 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
4253 operator<<(basic_ostream<_CharT, _Traits>& __os, const basic_string<_CharT, _Traits, _Allocator>& __str);
4254 
4255 template <class _CharT, class _Traits, class _Allocator>
4256 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
4257 operator>>(basic_istream<_CharT, _Traits>& __is, basic_string<_CharT, _Traits, _Allocator>& __str);
4258 
4259 template <class _CharT, class _Traits, class _Allocator>
4260 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
4261 getline(basic_istream<_CharT, _Traits>& __is, basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4262 
4263 template <class _CharT, class _Traits, class _Allocator>
4264 inline _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
4265 getline(basic_istream<_CharT, _Traits>& __is, basic_string<_CharT, _Traits, _Allocator>& __str);
4266 
4267 template <class _CharT, class _Traits, class _Allocator>
4268 inline _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
4269 getline(basic_istream<_CharT, _Traits>&& __is, basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4270 
4271 template <class _CharT, class _Traits, class _Allocator>
4272 inline _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
4273 getline(basic_istream<_CharT, _Traits>&& __is, basic_string<_CharT, _Traits, _Allocator>& __str);
4274 
4275 #  if _LIBCPP_STD_VER >= 20
4276 template <class _CharT, class _Traits, class _Allocator, class _Up>
4277 inline _LIBCPP_HIDE_FROM_ABI typename basic_string<_CharT, _Traits, _Allocator>::size_type
4278 erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
4279   auto __old_size = __str.size();
4280   __str.erase(std::remove(__str.begin(), __str.end(), __v), __str.end());
4281   return __old_size - __str.size();
4282 }
4283 
4284 template <class _CharT, class _Traits, class _Allocator, class _Predicate>
4285 inline _LIBCPP_HIDE_FROM_ABI typename basic_string<_CharT, _Traits, _Allocator>::size_type
4286 erase_if(basic_string<_CharT, _Traits, _Allocator>& __str, _Predicate __pred) {
4287   auto __old_size = __str.size();
4288   __str.erase(std::remove_if(__str.begin(), __str.end(), __pred), __str.end());
4289   return __old_size - __str.size();
4290 }
4291 #  endif
4292 
4293 #  if _LIBCPP_STD_VER >= 14
4294 // Literal suffixes for basic_string [basic.string.literals]
4295 inline namespace literals {
4296 inline namespace string_literals {
4297 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<char>
4298 operator""s(const char* __str, size_t __len) {
4299   return basic_string<char>(__str, __len);
4300 }
4301 
4302 #    if _LIBCPP_HAS_WIDE_CHARACTERS
4303 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<wchar_t>
4304 operator""s(const wchar_t* __str, size_t __len) {
4305   return basic_string<wchar_t>(__str, __len);
4306 }
4307 #    endif
4308 
4309 #    if _LIBCPP_HAS_CHAR8_T
4310 inline _LIBCPP_HIDE_FROM_ABI constexpr basic_string<char8_t> operator""s(const char8_t* __str, size_t __len) {
4311   return basic_string<char8_t>(__str, __len);
4312 }
4313 #    endif
4314 
4315 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<char16_t>
4316 operator""s(const char16_t* __str, size_t __len) {
4317   return basic_string<char16_t>(__str, __len);
4318 }
4319 
4320 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string<char32_t>
4321 operator""s(const char32_t* __str, size_t __len) {
4322   return basic_string<char32_t>(__str, __len);
4323 }
4324 } // namespace string_literals
4325 } // namespace literals
4326 
4327 #    if _LIBCPP_STD_VER >= 20
4328 template <>
4329 inline constexpr bool __format::__enable_insertable<std::basic_string<char>> = true;
4330 #      if _LIBCPP_HAS_WIDE_CHARACTERS
4331 template <>
4332 inline constexpr bool __format::__enable_insertable<std::basic_string<wchar_t>> = true;
4333 #      endif
4334 #    endif
4335 
4336 #  endif
4337 
4338 _LIBCPP_END_NAMESPACE_STD
4339 
4340 _LIBCPP_POP_MACROS
4341 
4342 #  if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
4343 #    include <algorithm>
4344 #    include <concepts>
4345 #    include <cstdlib>
4346 #    include <iterator>
4347 #    include <new>
4348 #    include <type_traits>
4349 #    include <typeinfo>
4350 #    include <utility>
4351 #  endif
4352 #endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
4353 
4354 #endif // _LIBCPP_STRING