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