xref: /llvm-project-15.0.7/libcxx/include/string (revision 08f68dfe)
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_all_iterators();
1791    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 void __invalidate_iterators_past(size_type);
1792
1793    template<class _Tp>
1794    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
1795    bool __addr_in_range(_Tp&& __t) const {
1796        // assume that the ranges overlap, because we can't check during constant evaluation
1797        if (__libcpp_is_constant_evaluated())
1798          return true;
1799        const volatile void *__p = std::addressof(__t);
1800        return data() <= __p && __p <= data() + size();
1801    }
1802
1803    _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1804    void __throw_length_error() const {
1805        std::__throw_length_error("basic_string");
1806    }
1807
1808    _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1809    void __throw_out_of_range() const {
1810        std::__throw_out_of_range("basic_string");
1811    }
1812
1813    friend _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string operator+<>(const basic_string&, const basic_string&);
1814    friend _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string operator+<>(const value_type*, const basic_string&);
1815    friend _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string operator+<>(value_type, const basic_string&);
1816    friend _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string operator+<>(const basic_string&, const value_type*);
1817    friend _LIBCPP_CONSTEXPR_AFTER_CXX17 basic_string operator+<>(const basic_string&, value_type);
1818};
1819
1820// These declarations must appear before any functions are implicitly used
1821// so that they have the correct visibility specifier.
1822#ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
1823    _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1824#   ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1825        _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1826#   endif
1827#else
1828    _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1829#   ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1830        _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1831#   endif
1832#endif
1833
1834
1835#if _LIBCPP_STD_VER >= 17
1836template<class _InputIterator,
1837         class _CharT = __iter_value_type<_InputIterator>,
1838         class _Allocator = allocator<_CharT>,
1839         class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
1840         class = enable_if_t<__is_allocator<_Allocator>::value>
1841         >
1842basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
1843  -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
1844
1845template<class _CharT,
1846         class _Traits,
1847         class _Allocator = allocator<_CharT>,
1848         class = enable_if_t<__is_allocator<_Allocator>::value>
1849         >
1850explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
1851  -> basic_string<_CharT, _Traits, _Allocator>;
1852
1853template<class _CharT,
1854         class _Traits,
1855         class _Allocator = allocator<_CharT>,
1856         class = enable_if_t<__is_allocator<_Allocator>::value>,
1857         class _Sz = typename allocator_traits<_Allocator>::size_type
1858         >
1859basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
1860  -> basic_string<_CharT, _Traits, _Allocator>;
1861#endif
1862
1863template <class _CharT, class _Traits, class _Allocator>
1864inline _LIBCPP_CONSTEXPR_AFTER_CXX17
1865void
1866basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
1867{
1868    std::__debug_db_invalidate_all(this);
1869}
1870
1871template <class _CharT, class _Traits, class _Allocator>
1872inline _LIBCPP_CONSTEXPR_AFTER_CXX17
1873void
1874basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type __pos)
1875{
1876#if _LIBCPP_DEBUG_LEVEL == 2
1877    if (!__libcpp_is_constant_evaluated()) {
1878        __c_node* __c = __get_db()->__find_c_and_lock(this);
1879        if (__c)
1880        {
1881            const_pointer __new_last = __get_pointer() + __pos;
1882            for (__i_node** __p = __c->end_; __p != __c->beg_; )
1883            {
1884                --__p;
1885                const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1886                if (__i->base() > __new_last)
1887                {
1888                    (*__p)->__c_ = nullptr;
1889                    if (--__c->end_ != __p)
1890                        std::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1891                }
1892            }
1893            __get_db()->unlock();
1894        }
1895    }
1896#else
1897    (void)__pos;
1898#endif // _LIBCPP_DEBUG_LEVEL == 2
1899}
1900
1901template <class _CharT, class _Traits, class _Allocator>
1902inline _LIBCPP_CONSTEXPR_AFTER_CXX17
1903basic_string<_CharT, _Traits, _Allocator>::basic_string()
1904    _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
1905     : __r_(__default_init_tag(), __default_init_tag())
1906{
1907    std::__debug_db_insert_c(this);
1908    __default_init();
1909}
1910
1911template <class _CharT, class _Traits, class _Allocator>
1912inline _LIBCPP_CONSTEXPR_AFTER_CXX17
1913basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
1914#if _LIBCPP_STD_VER <= 14
1915        _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1916#else
1917        _NOEXCEPT
1918#endif
1919: __r_(__default_init_tag(), __a)
1920{
1921    std::__debug_db_insert_c(this);
1922    __default_init();
1923}
1924
1925template <class _CharT, class _Traits, class _Allocator>
1926_LIBCPP_CONSTEXPR_AFTER_CXX17
1927void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1928                                                       size_type __sz,
1929                                                       size_type __reserve)
1930{
1931    if (__libcpp_is_constant_evaluated())
1932        __zero();
1933    if (__reserve > max_size())
1934        __throw_length_error();
1935    pointer __p;
1936    if (__fits_in_sso(__reserve))
1937    {
1938        __set_short_size(__sz);
1939        __p = __get_short_pointer();
1940    }
1941    else
1942    {
1943        auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__reserve) + 1);
1944        __p = __allocation.ptr;
1945        __begin_lifetime(__p, __allocation.count);
1946        __set_long_pointer(__p);
1947        __set_long_cap(__allocation.count);
1948        __set_long_size(__sz);
1949    }
1950    traits_type::copy(std::__to_address(__p), __s, __sz);
1951    traits_type::assign(__p[__sz], value_type());
1952}
1953
1954template <class _CharT, class _Traits, class _Allocator>
1955_LIBCPP_CONSTEXPR_AFTER_CXX17
1956void
1957basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
1958{
1959    if (__libcpp_is_constant_evaluated())
1960        __zero();
1961    if (__sz > max_size())
1962        __throw_length_error();
1963    pointer __p;
1964    if (__fits_in_sso(__sz))
1965    {
1966        __set_short_size(__sz);
1967        __p = __get_short_pointer();
1968    }
1969    else
1970    {
1971        auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
1972        __p = __allocation.ptr;
1973        __begin_lifetime(__p, __allocation.count);
1974        __set_long_pointer(__p);
1975        __set_long_cap(__allocation.count);
1976        __set_long_size(__sz);
1977    }
1978    traits_type::copy(std::__to_address(__p), __s, __sz);
1979    traits_type::assign(__p[__sz], value_type());
1980}
1981
1982template <class _CharT, class _Traits, class _Allocator>
1983template <class>
1984_LIBCPP_CONSTEXPR_AFTER_CXX17
1985basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
1986    : __r_(__default_init_tag(), __a)
1987{
1988    _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
1989    __init(__s, traits_type::length(__s));
1990    std::__debug_db_insert_c(this);
1991}
1992
1993template <class _CharT, class _Traits, class _Allocator>
1994inline _LIBCPP_CONSTEXPR_AFTER_CXX17
1995basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
1996     : __r_(__default_init_tag(), __default_init_tag())
1997{
1998    _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
1999    __init(__s, __n);
2000    std::__debug_db_insert_c(this);
2001}
2002
2003template <class _CharT, class _Traits, class _Allocator>
2004inline _LIBCPP_CONSTEXPR_AFTER_CXX17
2005basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
2006    : __r_(__default_init_tag(), __a)
2007{
2008    _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
2009    __init(__s, __n);
2010    std::__debug_db_insert_c(this);
2011}
2012
2013template <class _CharT, class _Traits, class _Allocator>
2014_LIBCPP_CONSTEXPR_AFTER_CXX17
2015basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
2016    : __r_(__default_init_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
2017{
2018    if (!__str.__is_long())
2019        __r_.first().__r = __str.__r_.first().__r;
2020    else
2021        __init_copy_ctor_external(std::__to_address(__str.__get_long_pointer()),
2022                                  __str.__get_long_size());
2023    std::__debug_db_insert_c(this);
2024}
2025
2026template <class _CharT, class _Traits, class _Allocator>
2027_LIBCPP_CONSTEXPR_AFTER_CXX17
2028basic_string<_CharT, _Traits, _Allocator>::basic_string(
2029    const basic_string& __str, const allocator_type& __a)
2030    : __r_(__default_init_tag(), __a)
2031{
2032    if (!__str.__is_long())
2033        __r_.first().__r = __str.__r_.first().__r;
2034    else
2035        __init_copy_ctor_external(std::__to_address(__str.__get_long_pointer()),
2036                                  __str.__get_long_size());
2037    std::__debug_db_insert_c(this);
2038}
2039
2040template <class _CharT, class _Traits, class _Allocator>
2041_LIBCPP_CONSTEXPR_AFTER_CXX17
2042void basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(
2043    const value_type* __s, size_type __sz) {
2044  if (__libcpp_is_constant_evaluated())
2045    __zero();
2046  pointer __p;
2047  if (__fits_in_sso(__sz)) {
2048    __p = __get_short_pointer();
2049    __set_short_size(__sz);
2050  } else {
2051    if (__sz > max_size())
2052      __throw_length_error();
2053    auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
2054    __p = __allocation.ptr;
2055    __begin_lifetime(__p, __allocation.count);
2056    __set_long_pointer(__p);
2057    __set_long_cap(__allocation.count);
2058    __set_long_size(__sz);
2059  }
2060  traits_type::copy(std::__to_address(__p), __s, __sz + 1);
2061}
2062
2063#ifndef _LIBCPP_CXX03_LANG
2064
2065template <class _CharT, class _Traits, class _Allocator>
2066inline _LIBCPP_CONSTEXPR_AFTER_CXX17
2067basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
2068#if _LIBCPP_STD_VER <= 14
2069        _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
2070#else
2071        _NOEXCEPT
2072#endif
2073    : __r_(std::move(__str.__r_))
2074{
2075    __str.__default_init();
2076    std::__debug_db_insert_c(this);
2077    if (__is_long())
2078        std::__debug_db_swap(this, &__str);
2079}
2080
2081template <class _CharT, class _Traits, class _Allocator>
2082inline _LIBCPP_CONSTEXPR_AFTER_CXX17
2083basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
2084    : __r_(__default_init_tag(), __a)
2085{
2086    if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
2087        __init(std::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
2088    else
2089    {
2090        if (__libcpp_is_constant_evaluated()) {
2091            __zero();
2092            __r_.first().__l = __str.__r_.first().__l;
2093        } else {
2094            __r_.first().__r = __str.__r_.first().__r;
2095        }
2096        __str.__default_init();
2097    }
2098    std::__debug_db_insert_c(this);
2099    if (__is_long())
2100        std::__debug_db_swap(this, &__str);
2101}
2102
2103#endif // _LIBCPP_CXX03_LANG
2104
2105template <class _CharT, class _Traits, class _Allocator>
2106_LIBCPP_CONSTEXPR_AFTER_CXX17
2107void
2108basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
2109{
2110    if (__libcpp_is_constant_evaluated())
2111        __zero();
2112    if (__n > max_size())
2113        __throw_length_error();
2114    pointer __p;
2115    if (__fits_in_sso(__n))
2116    {
2117        __set_short_size(__n);
2118        __p = __get_short_pointer();
2119    }
2120    else
2121    {
2122        auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__n) + 1);
2123        __p = __allocation.ptr;
2124        __begin_lifetime(__p, __allocation.count);
2125        __set_long_pointer(__p);
2126        __set_long_cap(__allocation.count);
2127        __set_long_size(__n);
2128    }
2129    traits_type::assign(std::__to_address(__p), __n, __c);
2130    traits_type::assign(__p[__n], value_type());
2131}
2132
2133template <class _CharT, class _Traits, class _Allocator>
2134inline _LIBCPP_CONSTEXPR_AFTER_CXX17
2135basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
2136     : __r_(__default_init_tag(), __default_init_tag())
2137{
2138    __init(__n, __c);
2139    std::__debug_db_insert_c(this);
2140}
2141
2142template <class _CharT, class _Traits, class _Allocator>
2143template <class>
2144_LIBCPP_CONSTEXPR_AFTER_CXX17
2145basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
2146    : __r_(__default_init_tag(), __a)
2147{
2148    __init(__n, __c);
2149    std::__debug_db_insert_c(this);
2150}
2151
2152template <class _CharT, class _Traits, class _Allocator>
2153_LIBCPP_CONSTEXPR_AFTER_CXX17
2154basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
2155                                                        size_type __pos, size_type __n,
2156                                                        const _Allocator& __a)
2157    : __r_(__default_init_tag(), __a)
2158{
2159    size_type __str_sz = __str.size();
2160    if (__pos > __str_sz)
2161        __throw_out_of_range();
2162    __init(__str.data() + __pos, std::min(__n, __str_sz - __pos));
2163    std::__debug_db_insert_c(this);
2164}
2165
2166template <class _CharT, class _Traits, class _Allocator>
2167inline _LIBCPP_CONSTEXPR_AFTER_CXX17
2168basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
2169                                                        const _Allocator& __a)
2170    : __r_(__default_init_tag(), __a)
2171{
2172    size_type __str_sz = __str.size();
2173    if (__pos > __str_sz)
2174        __throw_out_of_range();
2175    __init(__str.data() + __pos, __str_sz - __pos);
2176    std::__debug_db_insert_c(this);
2177}
2178
2179template <class _CharT, class _Traits, class _Allocator>
2180template <class _Tp, class>
2181_LIBCPP_CONSTEXPR_AFTER_CXX17
2182basic_string<_CharT, _Traits, _Allocator>::basic_string(
2183             const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a)
2184    : __r_(__default_init_tag(), __a)
2185{
2186    __self_view __sv0 = __t;
2187    __self_view __sv = __sv0.substr(__pos, __n);
2188    __init(__sv.data(), __sv.size());
2189    std::__debug_db_insert_c(this);
2190}
2191
2192template <class _CharT, class _Traits, class _Allocator>
2193template <class _Tp, class>
2194_LIBCPP_CONSTEXPR_AFTER_CXX17
2195basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t)
2196     : __r_(__default_init_tag(), __default_init_tag())
2197{
2198    __self_view __sv = __t;
2199    __init(__sv.data(), __sv.size());
2200    std::__debug_db_insert_c(this);
2201}
2202
2203template <class _CharT, class _Traits, class _Allocator>
2204template <class _Tp, class>
2205_LIBCPP_CONSTEXPR_AFTER_CXX17
2206basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t, const _Allocator& __a)
2207    : __r_(__default_init_tag(), __a)
2208{
2209    __self_view __sv = __t;
2210    __init(__sv.data(), __sv.size());
2211    std::__debug_db_insert_c(this);
2212}
2213
2214template <class _CharT, class _Traits, class _Allocator>
2215template <class _InputIterator>
2216_LIBCPP_CONSTEXPR_AFTER_CXX17
2217__enable_if_t
2218<
2219    __is_exactly_cpp17_input_iterator<_InputIterator>::value
2220>
2221basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
2222{
2223    __default_init();
2224#ifndef _LIBCPP_NO_EXCEPTIONS
2225    try
2226    {
2227#endif // _LIBCPP_NO_EXCEPTIONS
2228    for (; __first != __last; ++__first)
2229        push_back(*__first);
2230#ifndef _LIBCPP_NO_EXCEPTIONS
2231    }
2232    catch (...)
2233    {
2234        if (__is_long())
2235            __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
2236        throw;
2237    }
2238#endif // _LIBCPP_NO_EXCEPTIONS
2239}
2240
2241template <class _CharT, class _Traits, class _Allocator>
2242template <class _ForwardIterator>
2243_LIBCPP_CONSTEXPR_AFTER_CXX17
2244__enable_if_t
2245<
2246    __is_cpp17_forward_iterator<_ForwardIterator>::value
2247>
2248basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
2249{
2250    if (__libcpp_is_constant_evaluated())
2251        __zero();
2252    size_type __sz = static_cast<size_type>(std::distance(__first, __last));
2253    if (__sz > max_size())
2254        __throw_length_error();
2255    pointer __p;
2256    if (__fits_in_sso(__sz))
2257    {
2258        __set_short_size(__sz);
2259        __p = __get_short_pointer();
2260    }
2261    else
2262    {
2263        auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
2264        __p = __allocation.ptr;
2265        __begin_lifetime(__p, __allocation.count);
2266        __set_long_pointer(__p);
2267        __set_long_cap(__allocation.count);
2268        __set_long_size(__sz);
2269    }
2270
2271#ifndef _LIBCPP_NO_EXCEPTIONS
2272    try
2273    {
2274#endif  // _LIBCPP_NO_EXCEPTIONS
2275    for (; __first != __last; ++__first, (void) ++__p)
2276        traits_type::assign(*__p, *__first);
2277    traits_type::assign(*__p, value_type());
2278#ifndef _LIBCPP_NO_EXCEPTIONS
2279    }
2280    catch (...)
2281    {
2282        if (__is_long())
2283            __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
2284        throw;
2285    }
2286#endif  // _LIBCPP_NO_EXCEPTIONS
2287}
2288
2289template <class _CharT, class _Traits, class _Allocator>
2290template<class _InputIterator, class>
2291inline _LIBCPP_CONSTEXPR_AFTER_CXX17
2292basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
2293     : __r_(__default_init_tag(), __default_init_tag())
2294{
2295    __init(__first, __last);
2296    std::__debug_db_insert_c(this);
2297}
2298
2299template <class _CharT, class _Traits, class _Allocator>
2300template<class _InputIterator, class>
2301inline _LIBCPP_CONSTEXPR_AFTER_CXX17
2302basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
2303                                                        const allocator_type& __a)
2304    : __r_(__default_init_tag(), __a)
2305{
2306    __init(__first, __last);
2307    std::__debug_db_insert_c(this);
2308}
2309
2310#ifndef _LIBCPP_CXX03_LANG
2311
2312template <class _CharT, class _Traits, class _Allocator>
2313inline _LIBCPP_CONSTEXPR_AFTER_CXX17
2314basic_string<_CharT, _Traits, _Allocator>::basic_string(
2315    initializer_list<_CharT> __il)
2316     : __r_(__default_init_tag(), __default_init_tag())
2317{
2318    __init(__il.begin(), __il.end());
2319    std::__debug_db_insert_c(this);
2320}
2321
2322template <class _CharT, class _Traits, class _Allocator>
2323inline _LIBCPP_CONSTEXPR_AFTER_CXX17
2324basic_string<_CharT, _Traits, _Allocator>::basic_string(
2325    initializer_list<_CharT> __il, const _Allocator& __a)
2326    : __r_(__default_init_tag(), __a)
2327{
2328    __init(__il.begin(), __il.end());
2329    std::__debug_db_insert_c(this);
2330}
2331
2332#endif // _LIBCPP_CXX03_LANG
2333
2334template <class _CharT, class _Traits, class _Allocator>
2335_LIBCPP_CONSTEXPR_AFTER_CXX17
2336basic_string<_CharT, _Traits, _Allocator>::~basic_string()
2337{
2338    std::__debug_db_erase_c(this);
2339    if (__is_long())
2340        __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
2341}
2342
2343template <class _CharT, class _Traits, class _Allocator>
2344_LIBCPP_CONSTEXPR_AFTER_CXX17
2345void
2346basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
2347    (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2348     size_type __n_copy,  size_type __n_del,     size_type __n_add, const value_type* __p_new_stuff)
2349{
2350    size_type __ms = max_size();
2351    if (__delta_cap > __ms - __old_cap - 1)
2352        __throw_length_error();
2353    pointer __old_p = __get_pointer();
2354    size_type __cap = __old_cap < __ms / 2 - __alignment ?
2355                          __recommend(std::max(__old_cap + __delta_cap, 2 * __old_cap)) :
2356                          __ms - 1;
2357    auto __allocation = std::__allocate_at_least(__alloc(), __cap + 1);
2358    pointer __p = __allocation.ptr;
2359    __begin_lifetime(__p, __allocation.count);
2360    __invalidate_all_iterators();
2361    if (__n_copy != 0)
2362        traits_type::copy(std::__to_address(__p),
2363                          std::__to_address(__old_p), __n_copy);
2364    if (__n_add != 0)
2365        traits_type::copy(std::__to_address(__p) + __n_copy, __p_new_stuff, __n_add);
2366    size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2367    if (__sec_cp_sz != 0)
2368        traits_type::copy(std::__to_address(__p) + __n_copy + __n_add,
2369                          std::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
2370    if (__old_cap+1 != __min_cap || __libcpp_is_constant_evaluated())
2371        __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
2372    __set_long_pointer(__p);
2373    __set_long_cap(__allocation.count);
2374    __old_sz = __n_copy + __n_add + __sec_cp_sz;
2375    __set_long_size(__old_sz);
2376    traits_type::assign(__p[__old_sz], value_type());
2377}
2378
2379template <class _CharT, class _Traits, class _Allocator>
2380void
2381_LIBCPP_CONSTEXPR_AFTER_CXX17
2382basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2383                                                     size_type __n_copy,  size_type __n_del,     size_type __n_add)
2384{
2385    size_type __ms = max_size();
2386    if (__delta_cap > __ms - __old_cap)
2387        __throw_length_error();
2388    pointer __old_p = __get_pointer();
2389    size_type __cap = __old_cap < __ms / 2 - __alignment ?
2390                          __recommend(std::max(__old_cap + __delta_cap, 2 * __old_cap)) :
2391                          __ms - 1;
2392    auto __allocation = std::__allocate_at_least(__alloc(), __cap + 1);
2393    pointer __p = __allocation.ptr;
2394    __begin_lifetime(__p, __allocation.count);
2395    __invalidate_all_iterators();
2396    if (__n_copy != 0)
2397        traits_type::copy(std::__to_address(__p),
2398                          std::__to_address(__old_p), __n_copy);
2399    size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2400    if (__sec_cp_sz != 0)
2401        traits_type::copy(std::__to_address(__p) + __n_copy + __n_add,
2402                          std::__to_address(__old_p) + __n_copy + __n_del,
2403                          __sec_cp_sz);
2404    if (__libcpp_is_constant_evaluated() || __old_cap + 1 != __min_cap)
2405        __alloc_traits::deallocate(__alloc(), __old_p, __old_cap + 1);
2406    __set_long_pointer(__p);
2407    __set_long_cap(__allocation.count);
2408}
2409
2410// assign
2411
2412template <class _CharT, class _Traits, class _Allocator>
2413template <bool __is_short>
2414_LIBCPP_CONSTEXPR_AFTER_CXX17
2415basic_string<_CharT, _Traits, _Allocator>&
2416basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(
2417    const value_type* __s, size_type __n) {
2418  size_type __cap = __is_short ? static_cast<size_type>(__min_cap) : __get_long_cap();
2419  if (__n < __cap) {
2420    pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
2421    __is_short ? __set_short_size(__n) : __set_long_size(__n);
2422    traits_type::copy(std::__to_address(__p), __s, __n);
2423    traits_type::assign(__p[__n], value_type());
2424    __invalidate_iterators_past(__n);
2425  } else {
2426    size_type __sz = __is_short ? __get_short_size() : __get_long_size();
2427    __grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
2428  }
2429  return *this;
2430}
2431
2432template <class _CharT, class _Traits, class _Allocator>
2433_LIBCPP_CONSTEXPR_AFTER_CXX17
2434basic_string<_CharT, _Traits, _Allocator>&
2435basic_string<_CharT, _Traits, _Allocator>::__assign_external(
2436    const value_type* __s, size_type __n) {
2437  size_type __cap = capacity();
2438  if (__cap >= __n) {
2439    value_type* __p = std::__to_address(__get_pointer());
2440    traits_type::move(__p, __s, __n);
2441    return __null_terminate_at(__p, __n);
2442  } else {
2443    size_type __sz = size();
2444    __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
2445    return *this;
2446  }
2447}
2448
2449template <class _CharT, class _Traits, class _Allocator>
2450_LIBCPP_CONSTEXPR_AFTER_CXX17
2451basic_string<_CharT, _Traits, _Allocator>&
2452basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
2453{
2454    _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
2455    return (__builtin_constant_p(__n) && __fits_in_sso(__n))
2456               ? __assign_short(__s, __n)
2457               : __assign_external(__s, __n);
2458}
2459
2460template <class _CharT, class _Traits, class _Allocator>
2461_LIBCPP_CONSTEXPR_AFTER_CXX17
2462basic_string<_CharT, _Traits, _Allocator>&
2463basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2464{
2465    size_type __cap = capacity();
2466    if (__cap < __n)
2467    {
2468        size_type __sz = size();
2469        __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2470    }
2471    value_type* __p = std::__to_address(__get_pointer());
2472    traits_type::assign(__p, __n, __c);
2473    return __null_terminate_at(__p, __n);
2474}
2475
2476template <class _CharT, class _Traits, class _Allocator>
2477_LIBCPP_CONSTEXPR_AFTER_CXX17
2478basic_string<_CharT, _Traits, _Allocator>&
2479basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2480{
2481    pointer __p;
2482    if (__is_long())
2483    {
2484        __p = __get_long_pointer();
2485        __set_long_size(1);
2486    }
2487    else
2488    {
2489        __p = __get_short_pointer();
2490        __set_short_size(1);
2491    }
2492    traits_type::assign(*__p, __c);
2493    traits_type::assign(*++__p, value_type());
2494    __invalidate_iterators_past(1);
2495    return *this;
2496}
2497
2498template <class _CharT, class _Traits, class _Allocator>
2499_LIBCPP_CONSTEXPR_AFTER_CXX17
2500basic_string<_CharT, _Traits, _Allocator>&
2501basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2502{
2503  if (this != &__str) {
2504    __copy_assign_alloc(__str);
2505    if (!__is_long()) {
2506      if (!__str.__is_long()) {
2507        __r_.first().__r = __str.__r_.first().__r;
2508      } else {
2509        return __assign_no_alias<true>(__str.data(), __str.size());
2510      }
2511    } else {
2512      return __assign_no_alias<false>(__str.data(), __str.size());
2513    }
2514  }
2515  return *this;
2516}
2517
2518#ifndef _LIBCPP_CXX03_LANG
2519
2520template <class _CharT, class _Traits, class _Allocator>
2521inline _LIBCPP_CONSTEXPR_AFTER_CXX17
2522void
2523basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
2524    _NOEXCEPT_(__alloc_traits::is_always_equal::value)
2525{
2526    if (__alloc() != __str.__alloc())
2527        assign(__str);
2528    else
2529        __move_assign(__str, true_type());
2530}
2531
2532template <class _CharT, class _Traits, class _Allocator>
2533inline _LIBCPP_CONSTEXPR_AFTER_CXX17
2534void
2535basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
2536#if _LIBCPP_STD_VER > 14
2537    _NOEXCEPT
2538#else
2539    _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
2540#endif
2541{
2542  if (__is_long()) {
2543    __alloc_traits::deallocate(__alloc(), __get_long_pointer(),
2544                               __get_long_cap());
2545#if _LIBCPP_STD_VER <= 14
2546    if (!is_nothrow_move_assignable<allocator_type>::value) {
2547      __set_short_size(0);
2548      traits_type::assign(__get_short_pointer()[0], value_type());
2549    }
2550#endif
2551  }
2552  __move_assign_alloc(__str);
2553  __r_.first() = __str.__r_.first();
2554  if (__libcpp_is_constant_evaluated()) {
2555    __str.__default_init();
2556  } else {
2557    __str.__set_short_size(0);
2558    traits_type::assign(__str.__get_short_pointer()[0], value_type());
2559  }
2560}
2561
2562template <class _CharT, class _Traits, class _Allocator>
2563inline _LIBCPP_CONSTEXPR_AFTER_CXX17
2564basic_string<_CharT, _Traits, _Allocator>&
2565basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
2566    _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
2567{
2568    __move_assign(__str, integral_constant<bool,
2569          __alloc_traits::propagate_on_container_move_assignment::value>());
2570    return *this;
2571}
2572
2573#endif
2574
2575template <class _CharT, class _Traits, class _Allocator>
2576template<class _InputIterator>
2577_LIBCPP_CONSTEXPR_AFTER_CXX17
2578__enable_if_t
2579<
2580     __is_exactly_cpp17_input_iterator<_InputIterator>::value,
2581    basic_string<_CharT, _Traits, _Allocator>&
2582>
2583basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2584{
2585    const basic_string __temp(__first, __last, __alloc());
2586    assign(__temp.data(), __temp.size());
2587    return *this;
2588}
2589
2590template <class _CharT, class _Traits, class _Allocator>
2591template<class _ForwardIterator>
2592_LIBCPP_CONSTEXPR_AFTER_CXX17
2593__enable_if_t
2594<
2595    __is_cpp17_forward_iterator<_ForwardIterator>::value,
2596    basic_string<_CharT, _Traits, _Allocator>&
2597>
2598basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2599{
2600    size_type __cap = capacity();
2601    size_type __n = __string_is_trivial_iterator<_ForwardIterator>::value ?
2602        static_cast<size_type>(std::distance(__first, __last)) : 0;
2603
2604    if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2605        (__cap >= __n || !__addr_in_range(*__first)))
2606    {
2607        if (__cap < __n)
2608        {
2609            size_type __sz = size();
2610            __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2611        }
2612        pointer __p = __get_pointer();
2613        for (; __first != __last; ++__p, (void) ++__first)
2614            traits_type::assign(*__p, *__first);
2615        traits_type::assign(*__p, value_type());
2616        __set_size(__n);
2617        __invalidate_iterators_past(__n);
2618    }
2619    else
2620    {
2621        const basic_string __temp(__first, __last, __alloc());
2622        assign(__temp.data(), __temp.size());
2623    }
2624    return *this;
2625}
2626
2627template <class _CharT, class _Traits, class _Allocator>
2628_LIBCPP_CONSTEXPR_AFTER_CXX17
2629basic_string<_CharT, _Traits, _Allocator>&
2630basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2631{
2632    size_type __sz = __str.size();
2633    if (__pos > __sz)
2634        __throw_out_of_range();
2635    return assign(__str.data() + __pos, std::min(__n, __sz - __pos));
2636}
2637
2638template <class _CharT, class _Traits, class _Allocator>
2639template <class _Tp>
2640_LIBCPP_CONSTEXPR_AFTER_CXX17
2641__enable_if_t
2642<
2643    __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
2644    && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
2645    basic_string<_CharT, _Traits, _Allocator>&
2646>
2647basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
2648{
2649    __self_view __sv = __t;
2650    size_type __sz = __sv.size();
2651    if (__pos > __sz)
2652        __throw_out_of_range();
2653    return assign(__sv.data() + __pos, std::min(__n, __sz - __pos));
2654}
2655
2656
2657template <class _CharT, class _Traits, class _Allocator>
2658_LIBCPP_CONSTEXPR_AFTER_CXX17
2659basic_string<_CharT, _Traits, _Allocator>&
2660basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s) {
2661  return __assign_external(__s, traits_type::length(__s));
2662}
2663
2664template <class _CharT, class _Traits, class _Allocator>
2665_LIBCPP_CONSTEXPR_AFTER_CXX17
2666basic_string<_CharT, _Traits, _Allocator>&
2667basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
2668{
2669    _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
2670    return __builtin_constant_p(*__s)
2671               ? (__fits_in_sso(traits_type::length(__s))
2672                      ? __assign_short(__s, traits_type::length(__s))
2673                      : __assign_external(__s, traits_type::length(__s)))
2674               : __assign_external(__s);
2675}
2676// append
2677
2678template <class _CharT, class _Traits, class _Allocator>
2679_LIBCPP_CONSTEXPR_AFTER_CXX17
2680basic_string<_CharT, _Traits, _Allocator>&
2681basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
2682{
2683    _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
2684    size_type __cap = capacity();
2685    size_type __sz = size();
2686    if (__cap - __sz >= __n)
2687    {
2688        if (__n)
2689        {
2690            value_type* __p = std::__to_address(__get_pointer());
2691            traits_type::copy(__p + __sz, __s, __n);
2692            __sz += __n;
2693            __set_size(__sz);
2694            traits_type::assign(__p[__sz], value_type());
2695        }
2696    }
2697    else
2698        __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2699    return *this;
2700}
2701
2702template <class _CharT, class _Traits, class _Allocator>
2703_LIBCPP_CONSTEXPR_AFTER_CXX17
2704basic_string<_CharT, _Traits, _Allocator>&
2705basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2706{
2707    if (__n)
2708    {
2709        size_type __cap = capacity();
2710        size_type __sz = size();
2711        if (__cap - __sz < __n)
2712            __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2713        pointer __p = __get_pointer();
2714        traits_type::assign(std::__to_address(__p) + __sz, __n, __c);
2715        __sz += __n;
2716        __set_size(__sz);
2717        traits_type::assign(__p[__sz], value_type());
2718    }
2719    return *this;
2720}
2721
2722template <class _CharT, class _Traits, class _Allocator>
2723_LIBCPP_CONSTEXPR_AFTER_CXX17 inline void
2724basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n)
2725{
2726    if (__n)
2727    {
2728        size_type __cap = capacity();
2729        size_type __sz = size();
2730        if (__cap - __sz < __n)
2731            __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2732        pointer __p = __get_pointer();
2733        __sz += __n;
2734        __set_size(__sz);
2735        traits_type::assign(__p[__sz], value_type());
2736    }
2737}
2738
2739template <class _CharT, class _Traits, class _Allocator>
2740_LIBCPP_CONSTEXPR_AFTER_CXX17
2741void
2742basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2743{
2744    bool __is_short = !__is_long();
2745    size_type __cap;
2746    size_type __sz;
2747    if (__is_short)
2748    {
2749        __cap = __min_cap - 1;
2750        __sz = __get_short_size();
2751    }
2752    else
2753    {
2754        __cap = __get_long_cap() - 1;
2755        __sz = __get_long_size();
2756    }
2757    if (__sz == __cap)
2758    {
2759        __grow_by(__cap, 1, __sz, __sz, 0);
2760        __is_short = false; // the string is always long after __grow_by
2761    }
2762    pointer __p = __get_pointer();
2763    if (__is_short)
2764    {
2765        __p = __get_short_pointer() + __sz;
2766        __set_short_size(__sz+1);
2767    }
2768    else
2769    {
2770        __p = __get_long_pointer() + __sz;
2771        __set_long_size(__sz+1);
2772    }
2773    traits_type::assign(*__p, __c);
2774    traits_type::assign(*++__p, value_type());
2775}
2776
2777template <class _CharT, class _Traits, class _Allocator>
2778template<class _ForwardIterator>
2779_LIBCPP_CONSTEXPR_AFTER_CXX17
2780__enable_if_t
2781<
2782    __is_cpp17_forward_iterator<_ForwardIterator>::value,
2783    basic_string<_CharT, _Traits, _Allocator>&
2784>
2785basic_string<_CharT, _Traits, _Allocator>::append(
2786    _ForwardIterator __first, _ForwardIterator __last)
2787{
2788    size_type __sz = size();
2789    size_type __cap = capacity();
2790    size_type __n = static_cast<size_type>(std::distance(__first, __last));
2791    if (__n)
2792    {
2793        if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2794            !__addr_in_range(*__first))
2795        {
2796            if (__cap - __sz < __n)
2797                __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2798            pointer __p = __get_pointer() + __sz;
2799            for (; __first != __last; ++__p, (void) ++__first)
2800                traits_type::assign(*__p, *__first);
2801            traits_type::assign(*__p, value_type());
2802            __set_size(__sz + __n);
2803        }
2804        else
2805        {
2806            const basic_string __temp(__first, __last, __alloc());
2807            append(__temp.data(), __temp.size());
2808        }
2809    }
2810    return *this;
2811}
2812
2813template <class _CharT, class _Traits, class _Allocator>
2814inline _LIBCPP_CONSTEXPR_AFTER_CXX17
2815basic_string<_CharT, _Traits, _Allocator>&
2816basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2817{
2818    return append(__str.data(), __str.size());
2819}
2820
2821template <class _CharT, class _Traits, class _Allocator>
2822_LIBCPP_CONSTEXPR_AFTER_CXX17
2823basic_string<_CharT, _Traits, _Allocator>&
2824basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2825{
2826    size_type __sz = __str.size();
2827    if (__pos > __sz)
2828        __throw_out_of_range();
2829    return append(__str.data() + __pos, std::min(__n, __sz - __pos));
2830}
2831
2832template <class _CharT, class _Traits, class _Allocator>
2833template <class _Tp>
2834_LIBCPP_CONSTEXPR_AFTER_CXX17
2835    __enable_if_t
2836    <
2837        __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value  && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
2838        basic_string<_CharT, _Traits, _Allocator>&
2839    >
2840basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
2841{
2842    __self_view __sv = __t;
2843    size_type __sz = __sv.size();
2844    if (__pos > __sz)
2845        __throw_out_of_range();
2846    return append(__sv.data() + __pos, std::min(__n, __sz - __pos));
2847}
2848
2849template <class _CharT, class _Traits, class _Allocator>
2850_LIBCPP_CONSTEXPR_AFTER_CXX17
2851basic_string<_CharT, _Traits, _Allocator>&
2852basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
2853{
2854    _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
2855    return append(__s, traits_type::length(__s));
2856}
2857
2858// insert
2859
2860template <class _CharT, class _Traits, class _Allocator>
2861_LIBCPP_CONSTEXPR_AFTER_CXX17
2862basic_string<_CharT, _Traits, _Allocator>&
2863basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
2864{
2865    _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
2866    size_type __sz = size();
2867    if (__pos > __sz)
2868        __throw_out_of_range();
2869    size_type __cap = capacity();
2870    if (__libcpp_is_constant_evaluated()) {
2871        if (__cap - __sz >= __n)
2872            __grow_by_and_replace(__cap, 0, __sz, __pos, 0, __n, __s);
2873        else
2874            __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2875        return *this;
2876    }
2877    if (__cap - __sz >= __n)
2878    {
2879        if (__n)
2880        {
2881            value_type* __p = std::__to_address(__get_pointer());
2882            size_type __n_move = __sz - __pos;
2883            if (__n_move != 0)
2884            {
2885                if (__p + __pos <= __s && __s < __p + __sz)
2886                    __s += __n;
2887                traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2888            }
2889            traits_type::move(__p + __pos, __s, __n);
2890            __sz += __n;
2891            __set_size(__sz);
2892            traits_type::assign(__p[__sz], value_type());
2893        }
2894    }
2895    else
2896        __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2897    return *this;
2898}
2899
2900template <class _CharT, class _Traits, class _Allocator>
2901_LIBCPP_CONSTEXPR_AFTER_CXX17
2902basic_string<_CharT, _Traits, _Allocator>&
2903basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2904{
2905    size_type __sz = size();
2906    if (__pos > __sz)
2907        __throw_out_of_range();
2908    if (__n)
2909    {
2910        size_type __cap = capacity();
2911        value_type* __p;
2912        if (__cap - __sz >= __n)
2913        {
2914            __p = std::__to_address(__get_pointer());
2915            size_type __n_move = __sz - __pos;
2916            if (__n_move != 0)
2917                traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2918        }
2919        else
2920        {
2921            __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
2922            __p = std::__to_address(__get_long_pointer());
2923        }
2924        traits_type::assign(__p + __pos, __n, __c);
2925        __sz += __n;
2926        __set_size(__sz);
2927        traits_type::assign(__p[__sz], value_type());
2928    }
2929    return *this;
2930}
2931
2932template <class _CharT, class _Traits, class _Allocator>
2933template<class _InputIterator>
2934_LIBCPP_CONSTEXPR_AFTER_CXX17
2935__enable_if_t
2936<
2937   __is_exactly_cpp17_input_iterator<_InputIterator>::value,
2938   typename basic_string<_CharT, _Traits, _Allocator>::iterator
2939>
2940basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2941{
2942  _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2943                       "string::insert(iterator, range) called with an iterator not"
2944                       " referring to this string");
2945  const basic_string __temp(__first, __last, __alloc());
2946  return insert(__pos, __temp.data(), __temp.data() + __temp.size());
2947}
2948
2949template <class _CharT, class _Traits, class _Allocator>
2950template<class _ForwardIterator>
2951_LIBCPP_CONSTEXPR_AFTER_CXX17
2952__enable_if_t
2953<
2954    __is_cpp17_forward_iterator<_ForwardIterator>::value,
2955    typename basic_string<_CharT, _Traits, _Allocator>::iterator
2956>
2957basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2958{
2959    _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2960                         "string::insert(iterator, range) called with an iterator not referring to this string");
2961
2962    size_type __ip = static_cast<size_type>(__pos - begin());
2963    size_type __n = static_cast<size_type>(std::distance(__first, __last));
2964    if (__n == 0)
2965        return begin() + __ip;
2966
2967    if (__string_is_trivial_iterator<_ForwardIterator>::value && !__addr_in_range(*__first))
2968    {
2969        return __insert_from_safe_copy(__n, __ip, __first, __last);
2970    }
2971    else
2972    {
2973        const basic_string __temp(__first, __last, __alloc());
2974        return __insert_from_safe_copy(__n, __ip, __temp.begin(), __temp.end());
2975    }
2976}
2977
2978template <class _CharT, class _Traits, class _Allocator>
2979inline _LIBCPP_CONSTEXPR_AFTER_CXX17
2980basic_string<_CharT, _Traits, _Allocator>&
2981basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2982{
2983    return insert(__pos1, __str.data(), __str.size());
2984}
2985
2986template <class _CharT, class _Traits, class _Allocator>
2987_LIBCPP_CONSTEXPR_AFTER_CXX17
2988basic_string<_CharT, _Traits, _Allocator>&
2989basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2990                                                  size_type __pos2, size_type __n)
2991{
2992    size_type __str_sz = __str.size();
2993    if (__pos2 > __str_sz)
2994        __throw_out_of_range();
2995    return insert(__pos1, __str.data() + __pos2, std::min(__n, __str_sz - __pos2));
2996}
2997
2998template <class _CharT, class _Traits, class _Allocator>
2999template <class _Tp>
3000_LIBCPP_CONSTEXPR_AFTER_CXX17
3001__enable_if_t
3002<
3003    __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value  && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
3004    basic_string<_CharT, _Traits, _Allocator>&
3005>
3006basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
3007                                                  size_type __pos2, size_type __n)
3008{
3009    __self_view __sv = __t;
3010    size_type __str_sz = __sv.size();
3011    if (__pos2 > __str_sz)
3012        __throw_out_of_range();
3013    return insert(__pos1, __sv.data() + __pos2, std::min(__n, __str_sz - __pos2));
3014}
3015
3016template <class _CharT, class _Traits, class _Allocator>
3017_LIBCPP_CONSTEXPR_AFTER_CXX17
3018basic_string<_CharT, _Traits, _Allocator>&
3019basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
3020{
3021    _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
3022    return insert(__pos, __s, traits_type::length(__s));
3023}
3024
3025template <class _CharT, class _Traits, class _Allocator>
3026_LIBCPP_CONSTEXPR_AFTER_CXX17
3027typename basic_string<_CharT, _Traits, _Allocator>::iterator
3028basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
3029{
3030    _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3031                         "string::insert(iterator, character) called with an iterator not"
3032                         " referring to this string");
3033
3034    size_type __ip = static_cast<size_type>(__pos - begin());
3035    size_type __sz = size();
3036    size_type __cap = capacity();
3037    value_type* __p;
3038    if (__cap == __sz)
3039    {
3040        __grow_by(__cap, 1, __sz, __ip, 0, 1);
3041        __p = std::__to_address(__get_long_pointer());
3042    }
3043    else
3044    {
3045        __p = std::__to_address(__get_pointer());
3046        size_type __n_move = __sz - __ip;
3047        if (__n_move != 0)
3048            traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
3049    }
3050    traits_type::assign(__p[__ip], __c);
3051    traits_type::assign(__p[++__sz], value_type());
3052    __set_size(__sz);
3053    return begin() + static_cast<difference_type>(__ip);
3054}
3055
3056template <class _CharT, class _Traits, class _Allocator>
3057inline _LIBCPP_CONSTEXPR_AFTER_CXX17
3058typename basic_string<_CharT, _Traits, _Allocator>::iterator
3059basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
3060{
3061  _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3062                       "string::insert(iterator, n, value) called with an iterator not"
3063                       " referring to this string");
3064  difference_type __p = __pos - begin();
3065  insert(static_cast<size_type>(__p), __n, __c);
3066  return begin() + __p;
3067}
3068
3069// replace
3070
3071template <class _CharT, class _Traits, class _Allocator>
3072_LIBCPP_CONSTEXPR_AFTER_CXX17
3073basic_string<_CharT, _Traits, _Allocator>&
3074basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2)
3075    _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
3076{
3077    _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
3078    size_type __sz = size();
3079    if (__pos > __sz)
3080        __throw_out_of_range();
3081    __n1 = std::min(__n1, __sz - __pos);
3082    size_type __cap = capacity();
3083    if (__cap - __sz + __n1 >= __n2)
3084    {
3085        if (__libcpp_is_constant_evaluated()) {
3086            __grow_by_and_replace(__cap, 0, __sz, __pos, __n1, __n2, __s);
3087            return *this;
3088        }
3089        value_type* __p = std::__to_address(__get_pointer());
3090        if (__n1 != __n2)
3091        {
3092            size_type __n_move = __sz - __pos - __n1;
3093            if (__n_move != 0)
3094            {
3095                if (__n1 > __n2)
3096                {
3097                    traits_type::move(__p + __pos, __s, __n2);
3098                    traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3099                    return __null_terminate_at(__p, __sz + (__n2 - __n1));
3100                }
3101                if (__p + __pos < __s && __s < __p + __sz)
3102                {
3103                    if (__p + __pos + __n1 <= __s)
3104                        __s += __n2 - __n1;
3105                    else // __p + __pos < __s < __p + __pos + __n1
3106                    {
3107                        traits_type::move(__p + __pos, __s, __n1);
3108                        __pos += __n1;
3109                        __s += __n2;
3110                        __n2 -= __n1;
3111                        __n1 = 0;
3112                    }
3113                }
3114                traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3115            }
3116        }
3117        traits_type::move(__p + __pos, __s, __n2);
3118        return __null_terminate_at(__p, __sz + (__n2 - __n1));
3119    }
3120    else
3121        __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
3122    return *this;
3123}
3124
3125template <class _CharT, class _Traits, class _Allocator>
3126_LIBCPP_CONSTEXPR_AFTER_CXX17
3127basic_string<_CharT, _Traits, _Allocator>&
3128basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
3129{
3130    size_type __sz = size();
3131    if (__pos > __sz)
3132        __throw_out_of_range();
3133    __n1 = std::min(__n1, __sz - __pos);
3134    size_type __cap = capacity();
3135    value_type* __p;
3136    if (__cap - __sz + __n1 >= __n2)
3137    {
3138        __p = std::__to_address(__get_pointer());
3139        if (__n1 != __n2)
3140        {
3141            size_type __n_move = __sz - __pos - __n1;
3142            if (__n_move != 0)
3143                traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3144        }
3145    }
3146    else
3147    {
3148        __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
3149        __p = std::__to_address(__get_long_pointer());
3150    }
3151    traits_type::assign(__p + __pos, __n2, __c);
3152    return __null_terminate_at(__p, __sz - (__n1 - __n2));
3153}
3154
3155template <class _CharT, class _Traits, class _Allocator>
3156template<class _InputIterator>
3157_LIBCPP_CONSTEXPR_AFTER_CXX17
3158__enable_if_t
3159<
3160    __is_cpp17_input_iterator<_InputIterator>::value,
3161    basic_string<_CharT, _Traits, _Allocator>&
3162>
3163basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
3164                                                   _InputIterator __j1, _InputIterator __j2)
3165{
3166    const basic_string __temp(__j1, __j2, __alloc());
3167    return replace(__i1, __i2, __temp);
3168}
3169
3170template <class _CharT, class _Traits, class _Allocator>
3171inline _LIBCPP_CONSTEXPR_AFTER_CXX17
3172basic_string<_CharT, _Traits, _Allocator>&
3173basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
3174{
3175    return replace(__pos1, __n1, __str.data(), __str.size());
3176}
3177
3178template <class _CharT, class _Traits, class _Allocator>
3179_LIBCPP_CONSTEXPR_AFTER_CXX17
3180basic_string<_CharT, _Traits, _Allocator>&
3181basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
3182                                                   size_type __pos2, size_type __n2)
3183{
3184    size_type __str_sz = __str.size();
3185    if (__pos2 > __str_sz)
3186        __throw_out_of_range();
3187    return replace(__pos1, __n1, __str.data() + __pos2, std::min(__n2, __str_sz - __pos2));
3188}
3189
3190template <class _CharT, class _Traits, class _Allocator>
3191template <class _Tp>
3192_LIBCPP_CONSTEXPR_AFTER_CXX17
3193__enable_if_t
3194<
3195    __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
3196    basic_string<_CharT, _Traits, _Allocator>&
3197>
3198basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
3199                                                   size_type __pos2, size_type __n2)
3200{
3201    __self_view __sv = __t;
3202    size_type __str_sz = __sv.size();
3203    if (__pos2 > __str_sz)
3204        __throw_out_of_range();
3205    return replace(__pos1, __n1, __sv.data() + __pos2, std::min(__n2, __str_sz - __pos2));
3206}
3207
3208template <class _CharT, class _Traits, class _Allocator>
3209_LIBCPP_CONSTEXPR_AFTER_CXX17
3210basic_string<_CharT, _Traits, _Allocator>&
3211basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
3212{
3213    _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
3214    return replace(__pos, __n1, __s, traits_type::length(__s));
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 basic_string& __str)
3221{
3222    return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
3223                   __str.data(), __str.size());
3224}
3225
3226template <class _CharT, class _Traits, class _Allocator>
3227inline _LIBCPP_CONSTEXPR_AFTER_CXX17
3228basic_string<_CharT, _Traits, _Allocator>&
3229basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n)
3230{
3231    return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
3232}
3233
3234template <class _CharT, class _Traits, class _Allocator>
3235inline _LIBCPP_CONSTEXPR_AFTER_CXX17
3236basic_string<_CharT, _Traits, _Allocator>&
3237basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
3238{
3239    return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
3240}
3241
3242template <class _CharT, class _Traits, class _Allocator>
3243inline _LIBCPP_CONSTEXPR_AFTER_CXX17
3244basic_string<_CharT, _Traits, _Allocator>&
3245basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
3246{
3247    return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
3248}
3249
3250// erase
3251
3252// 'externally instantiated' erase() implementation, called when __n != npos.
3253// Does not check __pos against size()
3254template <class _CharT, class _Traits, class _Allocator>
3255_LIBCPP_CONSTEXPR_AFTER_CXX17
3256void
3257basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(
3258    size_type __pos, size_type __n)
3259{
3260    if (__n)
3261    {
3262        size_type __sz = size();
3263        value_type* __p = std::__to_address(__get_pointer());
3264        __n = std::min(__n, __sz - __pos);
3265        size_type __n_move = __sz - __pos - __n;
3266        if (__n_move != 0)
3267            traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
3268        __null_terminate_at(__p, __sz - __n);
3269    }
3270}
3271
3272template <class _CharT, class _Traits, class _Allocator>
3273_LIBCPP_CONSTEXPR_AFTER_CXX17
3274basic_string<_CharT, _Traits, _Allocator>&
3275basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos,
3276                                                 size_type __n) {
3277  if (__pos > size())
3278    __throw_out_of_range();
3279  if (__n == npos) {
3280    __erase_to_end(__pos);
3281  } else {
3282    __erase_external_with_move(__pos, __n);
3283  }
3284  return *this;
3285}
3286
3287template <class _CharT, class _Traits, class _Allocator>
3288inline _LIBCPP_CONSTEXPR_AFTER_CXX17
3289typename basic_string<_CharT, _Traits, _Allocator>::iterator
3290basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
3291{
3292  _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3293                       "string::erase(iterator) called with an iterator not"
3294                       " referring to this string");
3295
3296  _LIBCPP_ASSERT(__pos != end(), "string::erase(iterator) called with a non-dereferenceable iterator");
3297  iterator __b = begin();
3298  size_type __r = static_cast<size_type>(__pos - __b);
3299  erase(__r, 1);
3300  return __b + static_cast<difference_type>(__r);
3301}
3302
3303template <class _CharT, class _Traits, class _Allocator>
3304inline _LIBCPP_CONSTEXPR_AFTER_CXX17
3305typename basic_string<_CharT, _Traits, _Allocator>::iterator
3306basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
3307{
3308  _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
3309                       "string::erase(iterator,  iterator) called with an iterator not"
3310                       " referring to this string");
3311
3312  _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
3313  iterator __b = begin();
3314  size_type __r = static_cast<size_type>(__first - __b);
3315  erase(__r, static_cast<size_type>(__last - __first));
3316  return __b + static_cast<difference_type>(__r);
3317}
3318
3319template <class _CharT, class _Traits, class _Allocator>
3320inline _LIBCPP_CONSTEXPR_AFTER_CXX17
3321void
3322basic_string<_CharT, _Traits, _Allocator>::pop_back()
3323{
3324    _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
3325    __erase_to_end(size() - 1);
3326}
3327
3328template <class _CharT, class _Traits, class _Allocator>
3329inline _LIBCPP_CONSTEXPR_AFTER_CXX17
3330void
3331basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
3332{
3333    __invalidate_all_iterators();
3334    if (__is_long())
3335    {
3336        traits_type::assign(*__get_long_pointer(), value_type());
3337        __set_long_size(0);
3338    }
3339    else
3340    {
3341        traits_type::assign(*__get_short_pointer(), value_type());
3342        __set_short_size(0);
3343    }
3344}
3345
3346template <class _CharT, class _Traits, class _Allocator>
3347inline _LIBCPP_CONSTEXPR_AFTER_CXX17
3348void
3349basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
3350{
3351  __null_terminate_at(std::__to_address(__get_pointer()), __pos);
3352}
3353
3354template <class _CharT, class _Traits, class _Allocator>
3355_LIBCPP_CONSTEXPR_AFTER_CXX17
3356void
3357basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
3358{
3359    size_type __sz = size();
3360    if (__n > __sz)
3361        append(__n - __sz, __c);
3362    else
3363        __erase_to_end(__n);
3364}
3365
3366template <class _CharT, class _Traits, class _Allocator>
3367_LIBCPP_CONSTEXPR_AFTER_CXX17 inline void
3368basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n)
3369{
3370    size_type __sz = size();
3371    if (__n > __sz) {
3372       __append_default_init(__n - __sz);
3373    } else
3374        __erase_to_end(__n);
3375}
3376
3377template <class _CharT, class _Traits, class _Allocator>
3378inline _LIBCPP_CONSTEXPR_AFTER_CXX17
3379typename basic_string<_CharT, _Traits, _Allocator>::size_type
3380basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
3381{
3382    size_type __m = __alloc_traits::max_size(__alloc());
3383    if (__m <= std::numeric_limits<size_type>::max() / 2) {
3384        return __m - __alignment;
3385    } else {
3386        bool __uses_lsb = __endian_factor == 2;
3387        return __uses_lsb ? __m - __alignment : (__m / 2) - __alignment;
3388    }
3389}
3390
3391template <class _CharT, class _Traits, class _Allocator>
3392_LIBCPP_CONSTEXPR_AFTER_CXX17
3393void
3394basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __requested_capacity)
3395{
3396    if (__requested_capacity > max_size())
3397        __throw_length_error();
3398
3399    // Make sure reserve(n) never shrinks. This is technically only required in C++20
3400    // and later (since P0966R1), however we provide consistent behavior in all Standard
3401    // modes because this function is instantiated in the shared library.
3402    if (__requested_capacity <= capacity())
3403        return;
3404
3405    size_type __target_capacity = std::max(__requested_capacity, size());
3406    __target_capacity = __recommend(__target_capacity);
3407    if (__target_capacity == capacity()) return;
3408
3409    __shrink_or_extend(__target_capacity);
3410}
3411
3412template <class _CharT, class _Traits, class _Allocator>
3413inline _LIBCPP_CONSTEXPR_AFTER_CXX17
3414void
3415basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT
3416{
3417    size_type __target_capacity = __recommend(size());
3418    if (__target_capacity == capacity()) return;
3419
3420    __shrink_or_extend(__target_capacity);
3421}
3422
3423template <class _CharT, class _Traits, class _Allocator>
3424inline _LIBCPP_CONSTEXPR_AFTER_CXX17
3425void
3426basic_string<_CharT, _Traits, _Allocator>::__shrink_or_extend(size_type __target_capacity)
3427{
3428    size_type __cap = capacity();
3429    size_type __sz = size();
3430
3431    pointer __new_data, __p;
3432    bool __was_long, __now_long;
3433    if (__fits_in_sso(__target_capacity))
3434    {
3435        __was_long = true;
3436        __now_long = false;
3437        __new_data = __get_short_pointer();
3438        __p = __get_long_pointer();
3439    }
3440    else
3441    {
3442        if (__target_capacity > __cap) {
3443            auto __allocation = std::__allocate_at_least(__alloc(), __target_capacity + 1);
3444            __new_data = __allocation.ptr;
3445            __target_capacity = __allocation.count - 1;
3446        }
3447        else
3448        {
3449        #ifndef _LIBCPP_NO_EXCEPTIONS
3450            try
3451            {
3452        #endif // _LIBCPP_NO_EXCEPTIONS
3453                auto __allocation = std::__allocate_at_least(__alloc(), __target_capacity + 1);
3454                __new_data = __allocation.ptr;
3455                __target_capacity = __allocation.count - 1;
3456        #ifndef _LIBCPP_NO_EXCEPTIONS
3457            }
3458            catch (...)
3459            {
3460                return;
3461            }
3462        #else  // _LIBCPP_NO_EXCEPTIONS
3463            if (__new_data == nullptr)
3464                return;
3465        #endif // _LIBCPP_NO_EXCEPTIONS
3466        }
3467        __begin_lifetime(__new_data, __target_capacity + 1);
3468        __now_long = true;
3469        __was_long = __is_long();
3470        __p = __get_pointer();
3471    }
3472    traits_type::copy(std::__to_address(__new_data),
3473                      std::__to_address(__p), size()+1);
3474    if (__was_long)
3475        __alloc_traits::deallocate(__alloc(), __p, __cap+1);
3476    if (__now_long)
3477    {
3478        __set_long_cap(__target_capacity+1);
3479        __set_long_size(__sz);
3480        __set_long_pointer(__new_data);
3481    }
3482    else
3483        __set_short_size(__sz);
3484    __invalidate_all_iterators();
3485}
3486
3487template <class _CharT, class _Traits, class _Allocator>
3488inline _LIBCPP_CONSTEXPR_AFTER_CXX17
3489typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3490basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
3491{
3492    _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
3493    return *(data() + __pos);
3494}
3495
3496template <class _CharT, class _Traits, class _Allocator>
3497inline _LIBCPP_CONSTEXPR_AFTER_CXX17
3498typename basic_string<_CharT, _Traits, _Allocator>::reference
3499basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
3500{
3501    _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
3502    return *(__get_pointer() + __pos);
3503}
3504
3505template <class _CharT, class _Traits, class _Allocator>
3506_LIBCPP_CONSTEXPR_AFTER_CXX17
3507typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3508basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3509{
3510    if (__n >= size())
3511        __throw_out_of_range();
3512    return (*this)[__n];
3513}
3514
3515template <class _CharT, class _Traits, class _Allocator>
3516_LIBCPP_CONSTEXPR_AFTER_CXX17
3517typename basic_string<_CharT, _Traits, _Allocator>::reference
3518basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
3519{
3520    if (__n >= size())
3521        __throw_out_of_range();
3522    return (*this)[__n];
3523}
3524
3525template <class _CharT, class _Traits, class _Allocator>
3526inline _LIBCPP_CONSTEXPR_AFTER_CXX17
3527typename basic_string<_CharT, _Traits, _Allocator>::reference
3528basic_string<_CharT, _Traits, _Allocator>::front() _NOEXCEPT
3529{
3530    _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
3531    return *__get_pointer();
3532}
3533
3534template <class _CharT, class _Traits, class _Allocator>
3535inline _LIBCPP_CONSTEXPR_AFTER_CXX17
3536typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3537basic_string<_CharT, _Traits, _Allocator>::front() const _NOEXCEPT
3538{
3539    _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
3540    return *data();
3541}
3542
3543template <class _CharT, class _Traits, class _Allocator>
3544inline _LIBCPP_CONSTEXPR_AFTER_CXX17
3545typename basic_string<_CharT, _Traits, _Allocator>::reference
3546basic_string<_CharT, _Traits, _Allocator>::back() _NOEXCEPT
3547{
3548    _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
3549    return *(__get_pointer() + size() - 1);
3550}
3551
3552template <class _CharT, class _Traits, class _Allocator>
3553inline _LIBCPP_CONSTEXPR_AFTER_CXX17
3554typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3555basic_string<_CharT, _Traits, _Allocator>::back() const _NOEXCEPT
3556{
3557    _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
3558    return *(data() + size() - 1);
3559}
3560
3561template <class _CharT, class _Traits, class _Allocator>
3562_LIBCPP_CONSTEXPR_AFTER_CXX17
3563typename basic_string<_CharT, _Traits, _Allocator>::size_type
3564basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
3565{
3566    size_type __sz = size();
3567    if (__pos > __sz)
3568        __throw_out_of_range();
3569    size_type __rlen = std::min(__n, __sz - __pos);
3570    traits_type::copy(__s, data() + __pos, __rlen);
3571    return __rlen;
3572}
3573
3574template <class _CharT, class _Traits, class _Allocator>
3575inline _LIBCPP_CONSTEXPR_AFTER_CXX17
3576basic_string<_CharT, _Traits, _Allocator>
3577basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3578{
3579    return basic_string(*this, __pos, __n, __alloc());
3580}
3581
3582template <class _CharT, class _Traits, class _Allocator>
3583inline _LIBCPP_CONSTEXPR_AFTER_CXX17
3584void
3585basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
3586#if _LIBCPP_STD_VER >= 14
3587        _NOEXCEPT
3588#else
3589        _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
3590                    __is_nothrow_swappable<allocator_type>::value)
3591#endif
3592{
3593    if (!__is_long())
3594        std::__debug_db_invalidate_all(this);
3595    if (!__str.__is_long())
3596        std::__debug_db_invalidate_all(&__str);
3597    std::__debug_db_swap(this, &__str);
3598
3599    _LIBCPP_ASSERT(
3600        __alloc_traits::propagate_on_container_swap::value ||
3601        __alloc_traits::is_always_equal::value ||
3602        __alloc() == __str.__alloc(), "swapping non-equal allocators");
3603    std::swap(__r_.first(), __str.__r_.first());
3604    std::__swap_allocator(__alloc(), __str.__alloc());
3605}
3606
3607// find
3608
3609template <class _Traits>
3610struct _LIBCPP_HIDDEN __traits_eq
3611{
3612    typedef typename _Traits::char_type char_type;
3613    _LIBCPP_HIDE_FROM_ABI
3614    bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3615        {return _Traits::eq(__x, __y);}
3616};
3617
3618template<class _CharT, class _Traits, class _Allocator>
3619_LIBCPP_CONSTEXPR_AFTER_CXX17
3620typename basic_string<_CharT, _Traits, _Allocator>::size_type
3621basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
3622                                                size_type __pos,
3623                                                size_type __n) const _NOEXCEPT
3624{
3625    _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
3626    return __str_find<value_type, size_type, traits_type, npos>
3627        (data(), size(), __s, __pos, __n);
3628}
3629
3630template<class _CharT, class _Traits, class _Allocator>
3631inline _LIBCPP_CONSTEXPR_AFTER_CXX17
3632typename basic_string<_CharT, _Traits, _Allocator>::size_type
3633basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3634                                                size_type __pos) const _NOEXCEPT
3635{
3636    return __str_find<value_type, size_type, traits_type, npos>
3637        (data(), size(), __str.data(), __pos, __str.size());
3638}
3639
3640template<class _CharT, class _Traits, class _Allocator>
3641template <class _Tp>
3642_LIBCPP_CONSTEXPR_AFTER_CXX17
3643__enable_if_t
3644<
3645    __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3646    typename basic_string<_CharT, _Traits, _Allocator>::size_type
3647>
3648basic_string<_CharT, _Traits, _Allocator>::find(const _Tp &__t,
3649                                                size_type __pos) const _NOEXCEPT
3650{
3651    __self_view __sv = __t;
3652    return __str_find<value_type, size_type, traits_type, npos>
3653        (data(), size(), __sv.data(), __pos, __sv.size());
3654}
3655
3656template<class _CharT, class _Traits, class _Allocator>
3657inline _LIBCPP_CONSTEXPR_AFTER_CXX17
3658typename basic_string<_CharT, _Traits, _Allocator>::size_type
3659basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
3660                                                size_type __pos) const _NOEXCEPT
3661{
3662    _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
3663    return __str_find<value_type, size_type, traits_type, npos>
3664        (data(), size(), __s, __pos, traits_type::length(__s));
3665}
3666
3667template<class _CharT, class _Traits, class _Allocator>
3668_LIBCPP_CONSTEXPR_AFTER_CXX17
3669typename basic_string<_CharT, _Traits, _Allocator>::size_type
3670basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3671                                                size_type __pos) const _NOEXCEPT
3672{
3673    return __str_find<value_type, size_type, traits_type, npos>
3674        (data(), size(), __c, __pos);
3675}
3676
3677// rfind
3678
3679template<class _CharT, class _Traits, class _Allocator>
3680_LIBCPP_CONSTEXPR_AFTER_CXX17
3681typename basic_string<_CharT, _Traits, _Allocator>::size_type
3682basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
3683                                                 size_type __pos,
3684                                                 size_type __n) const _NOEXCEPT
3685{
3686    _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
3687    return __str_rfind<value_type, size_type, traits_type, npos>
3688        (data(), size(), __s, __pos, __n);
3689}
3690
3691template<class _CharT, class _Traits, class _Allocator>
3692inline _LIBCPP_CONSTEXPR_AFTER_CXX17
3693typename basic_string<_CharT, _Traits, _Allocator>::size_type
3694basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3695                                                 size_type __pos) const _NOEXCEPT
3696{
3697    return __str_rfind<value_type, size_type, traits_type, npos>
3698        (data(), size(), __str.data(), __pos, __str.size());
3699}
3700
3701template<class _CharT, class _Traits, class _Allocator>
3702template <class _Tp>
3703_LIBCPP_CONSTEXPR_AFTER_CXX17
3704__enable_if_t
3705<
3706    __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3707    typename basic_string<_CharT, _Traits, _Allocator>::size_type
3708>
3709basic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t,
3710                                                size_type __pos) const _NOEXCEPT
3711{
3712    __self_view __sv = __t;
3713    return __str_rfind<value_type, size_type, traits_type, npos>
3714        (data(), size(), __sv.data(), __pos, __sv.size());
3715}
3716
3717template<class _CharT, class _Traits, class _Allocator>
3718inline _LIBCPP_CONSTEXPR_AFTER_CXX17
3719typename basic_string<_CharT, _Traits, _Allocator>::size_type
3720basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
3721                                                 size_type __pos) const _NOEXCEPT
3722{
3723    _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
3724    return __str_rfind<value_type, size_type, traits_type, npos>
3725        (data(), size(), __s, __pos, traits_type::length(__s));
3726}
3727
3728template<class _CharT, class _Traits, class _Allocator>
3729_LIBCPP_CONSTEXPR_AFTER_CXX17
3730typename basic_string<_CharT, _Traits, _Allocator>::size_type
3731basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3732                                                 size_type __pos) const _NOEXCEPT
3733{
3734    return __str_rfind<value_type, size_type, traits_type, npos>
3735        (data(), size(), __c, __pos);
3736}
3737
3738// find_first_of
3739
3740template<class _CharT, class _Traits, class _Allocator>
3741_LIBCPP_CONSTEXPR_AFTER_CXX17
3742typename basic_string<_CharT, _Traits, _Allocator>::size_type
3743basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
3744                                                         size_type __pos,
3745                                                         size_type __n) const _NOEXCEPT
3746{
3747    _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
3748    return __str_find_first_of<value_type, size_type, traits_type, npos>
3749        (data(), size(), __s, __pos, __n);
3750}
3751
3752template<class _CharT, class _Traits, class _Allocator>
3753inline _LIBCPP_CONSTEXPR_AFTER_CXX17
3754typename basic_string<_CharT, _Traits, _Allocator>::size_type
3755basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3756                                                         size_type __pos) const _NOEXCEPT
3757{
3758    return __str_find_first_of<value_type, size_type, traits_type, npos>
3759        (data(), size(), __str.data(), __pos, __str.size());
3760}
3761
3762template<class _CharT, class _Traits, class _Allocator>
3763template <class _Tp>
3764_LIBCPP_CONSTEXPR_AFTER_CXX17
3765__enable_if_t
3766<
3767    __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3768    typename basic_string<_CharT, _Traits, _Allocator>::size_type
3769>
3770basic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t,
3771                                                size_type __pos) const _NOEXCEPT
3772{
3773    __self_view __sv = __t;
3774    return __str_find_first_of<value_type, size_type, traits_type, npos>
3775        (data(), size(), __sv.data(), __pos, __sv.size());
3776}
3777
3778template<class _CharT, class _Traits, class _Allocator>
3779inline _LIBCPP_CONSTEXPR_AFTER_CXX17
3780typename basic_string<_CharT, _Traits, _Allocator>::size_type
3781basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
3782                                                         size_type __pos) const _NOEXCEPT
3783{
3784    _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
3785    return __str_find_first_of<value_type, size_type, traits_type, npos>
3786        (data(), size(), __s, __pos, traits_type::length(__s));
3787}
3788
3789template<class _CharT, class _Traits, class _Allocator>
3790inline _LIBCPP_CONSTEXPR_AFTER_CXX17
3791typename basic_string<_CharT, _Traits, _Allocator>::size_type
3792basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3793                                                         size_type __pos) const _NOEXCEPT
3794{
3795    return find(__c, __pos);
3796}
3797
3798// find_last_of
3799
3800template<class _CharT, class _Traits, class _Allocator>
3801inline _LIBCPP_CONSTEXPR_AFTER_CXX17
3802typename basic_string<_CharT, _Traits, _Allocator>::size_type
3803basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
3804                                                        size_type __pos,
3805                                                        size_type __n) const _NOEXCEPT
3806{
3807    _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
3808    return __str_find_last_of<value_type, size_type, traits_type, npos>
3809        (data(), size(), __s, __pos, __n);
3810}
3811
3812template<class _CharT, class _Traits, class _Allocator>
3813inline _LIBCPP_CONSTEXPR_AFTER_CXX17
3814typename basic_string<_CharT, _Traits, _Allocator>::size_type
3815basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3816                                                        size_type __pos) const _NOEXCEPT
3817{
3818    return __str_find_last_of<value_type, size_type, traits_type, npos>
3819        (data(), size(), __str.data(), __pos, __str.size());
3820}
3821
3822template<class _CharT, class _Traits, class _Allocator>
3823template <class _Tp>
3824_LIBCPP_CONSTEXPR_AFTER_CXX17
3825__enable_if_t
3826<
3827    __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3828    typename basic_string<_CharT, _Traits, _Allocator>::size_type
3829>
3830basic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t,
3831                                                size_type __pos) const _NOEXCEPT
3832{
3833    __self_view __sv = __t;
3834    return __str_find_last_of<value_type, size_type, traits_type, npos>
3835        (data(), size(), __sv.data(), __pos, __sv.size());
3836}
3837
3838template<class _CharT, class _Traits, class _Allocator>
3839inline _LIBCPP_CONSTEXPR_AFTER_CXX17
3840typename basic_string<_CharT, _Traits, _Allocator>::size_type
3841basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
3842                                                        size_type __pos) const _NOEXCEPT
3843{
3844    _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
3845    return __str_find_last_of<value_type, size_type, traits_type, npos>
3846        (data(), size(), __s, __pos, traits_type::length(__s));
3847}
3848
3849template<class _CharT, class _Traits, class _Allocator>
3850inline _LIBCPP_CONSTEXPR_AFTER_CXX17
3851typename basic_string<_CharT, _Traits, _Allocator>::size_type
3852basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3853                                                        size_type __pos) const _NOEXCEPT
3854{
3855    return rfind(__c, __pos);
3856}
3857
3858// find_first_not_of
3859
3860template<class _CharT, class _Traits, class _Allocator>
3861_LIBCPP_CONSTEXPR_AFTER_CXX17
3862typename basic_string<_CharT, _Traits, _Allocator>::size_type
3863basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
3864                                                             size_type __pos,
3865                                                             size_type __n) const _NOEXCEPT
3866{
3867    _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
3868    return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3869        (data(), size(), __s, __pos, __n);
3870}
3871
3872template<class _CharT, class _Traits, class _Allocator>
3873inline _LIBCPP_CONSTEXPR_AFTER_CXX17
3874typename basic_string<_CharT, _Traits, _Allocator>::size_type
3875basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3876                                                             size_type __pos) const _NOEXCEPT
3877{
3878    return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3879        (data(), size(), __str.data(), __pos, __str.size());
3880}
3881
3882template<class _CharT, class _Traits, class _Allocator>
3883template <class _Tp>
3884_LIBCPP_CONSTEXPR_AFTER_CXX17
3885__enable_if_t
3886<
3887    __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3888    typename basic_string<_CharT, _Traits, _Allocator>::size_type
3889>
3890basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t,
3891                                                size_type __pos) const _NOEXCEPT
3892{
3893    __self_view __sv = __t;
3894    return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3895        (data(), size(), __sv.data(), __pos, __sv.size());
3896}
3897
3898template<class _CharT, class _Traits, class _Allocator>
3899inline _LIBCPP_CONSTEXPR_AFTER_CXX17
3900typename basic_string<_CharT, _Traits, _Allocator>::size_type
3901basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
3902                                                             size_type __pos) const _NOEXCEPT
3903{
3904    _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
3905    return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3906        (data(), size(), __s, __pos, traits_type::length(__s));
3907}
3908
3909template<class _CharT, class _Traits, class _Allocator>
3910inline _LIBCPP_CONSTEXPR_AFTER_CXX17
3911typename basic_string<_CharT, _Traits, _Allocator>::size_type
3912basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3913                                                             size_type __pos) const _NOEXCEPT
3914{
3915    return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3916        (data(), size(), __c, __pos);
3917}
3918
3919// find_last_not_of
3920
3921template<class _CharT, class _Traits, class _Allocator>
3922_LIBCPP_CONSTEXPR_AFTER_CXX17
3923typename basic_string<_CharT, _Traits, _Allocator>::size_type
3924basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
3925                                                            size_type __pos,
3926                                                            size_type __n) const _NOEXCEPT
3927{
3928    _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
3929    return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3930        (data(), size(), __s, __pos, __n);
3931}
3932
3933template<class _CharT, class _Traits, class _Allocator>
3934inline _LIBCPP_CONSTEXPR_AFTER_CXX17
3935typename basic_string<_CharT, _Traits, _Allocator>::size_type
3936basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3937                                                            size_type __pos) const _NOEXCEPT
3938{
3939    return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3940        (data(), size(), __str.data(), __pos, __str.size());
3941}
3942
3943template<class _CharT, class _Traits, class _Allocator>
3944template <class _Tp>
3945_LIBCPP_CONSTEXPR_AFTER_CXX17
3946__enable_if_t
3947<
3948    __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3949    typename basic_string<_CharT, _Traits, _Allocator>::size_type
3950>
3951basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t,
3952                                                size_type __pos) const _NOEXCEPT
3953{
3954    __self_view __sv = __t;
3955    return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3956        (data(), size(), __sv.data(), __pos, __sv.size());
3957}
3958
3959template<class _CharT, class _Traits, class _Allocator>
3960inline _LIBCPP_CONSTEXPR_AFTER_CXX17
3961typename basic_string<_CharT, _Traits, _Allocator>::size_type
3962basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
3963                                                            size_type __pos) const _NOEXCEPT
3964{
3965    _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
3966    return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3967        (data(), size(), __s, __pos, traits_type::length(__s));
3968}
3969
3970template<class _CharT, class _Traits, class _Allocator>
3971inline _LIBCPP_CONSTEXPR_AFTER_CXX17
3972typename basic_string<_CharT, _Traits, _Allocator>::size_type
3973basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3974                                                            size_type __pos) const _NOEXCEPT
3975{
3976    return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3977        (data(), size(), __c, __pos);
3978}
3979
3980// compare
3981
3982template <class _CharT, class _Traits, class _Allocator>
3983template <class _Tp>
3984_LIBCPP_CONSTEXPR_AFTER_CXX17
3985__enable_if_t
3986<
3987    __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3988    int
3989>
3990basic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const _NOEXCEPT
3991{
3992    __self_view __sv = __t;
3993    size_t __lhs_sz = size();
3994    size_t __rhs_sz = __sv.size();
3995    int __result = traits_type::compare(data(), __sv.data(),
3996                                        std::min(__lhs_sz, __rhs_sz));
3997    if (__result != 0)
3998        return __result;
3999    if (__lhs_sz < __rhs_sz)
4000        return -1;
4001    if (__lhs_sz > __rhs_sz)
4002        return 1;
4003    return 0;
4004}
4005
4006template <class _CharT, class _Traits, class _Allocator>
4007inline _LIBCPP_CONSTEXPR_AFTER_CXX17
4008int
4009basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
4010{
4011    return compare(__self_view(__str));
4012}
4013
4014template <class _CharT, class _Traits, class _Allocator>
4015inline _LIBCPP_CONSTEXPR_AFTER_CXX17
4016int
4017basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4018                                                   size_type __n1,
4019                                                   const value_type* __s,
4020                                                   size_type __n2) const
4021{
4022    _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
4023    size_type __sz = size();
4024    if (__pos1 > __sz || __n2 == npos)
4025        __throw_out_of_range();
4026    size_type __rlen = std::min(__n1, __sz - __pos1);
4027    int __r = traits_type::compare(data() + __pos1, __s, std::min(__rlen, __n2));
4028    if (__r == 0)
4029    {
4030        if (__rlen < __n2)
4031            __r = -1;
4032        else if (__rlen > __n2)
4033            __r = 1;
4034    }
4035    return __r;
4036}
4037
4038template <class _CharT, class _Traits, class _Allocator>
4039template <class _Tp>
4040_LIBCPP_CONSTEXPR_AFTER_CXX17
4041__enable_if_t
4042<
4043    __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
4044    int
4045>
4046basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4047                                                   size_type __n1,
4048                                                   const _Tp& __t) const
4049{
4050    __self_view __sv = __t;
4051    return compare(__pos1, __n1, __sv.data(), __sv.size());
4052}
4053
4054template <class _CharT, class _Traits, class _Allocator>
4055inline _LIBCPP_CONSTEXPR_AFTER_CXX17
4056int
4057basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4058                                                   size_type __n1,
4059                                                   const basic_string& __str) const
4060{
4061    return compare(__pos1, __n1, __str.data(), __str.size());
4062}
4063
4064template <class _CharT, class _Traits, class _Allocator>
4065template <class _Tp>
4066_LIBCPP_CONSTEXPR_AFTER_CXX17
4067__enable_if_t
4068<
4069    __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
4070    && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
4071    int
4072>
4073basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4074                                                   size_type __n1,
4075                                                   const _Tp& __t,
4076                                                   size_type __pos2,
4077                                                   size_type __n2) const
4078{
4079    __self_view __sv = __t;
4080    return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
4081}
4082
4083template <class _CharT, class _Traits, class _Allocator>
4084_LIBCPP_CONSTEXPR_AFTER_CXX17
4085int
4086basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4087                                                   size_type __n1,
4088                                                   const basic_string& __str,
4089                                                   size_type __pos2,
4090                                                   size_type __n2) const
4091{
4092    return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
4093}
4094
4095template <class _CharT, class _Traits, class _Allocator>
4096_LIBCPP_CONSTEXPR_AFTER_CXX17
4097int
4098basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
4099{
4100    _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
4101    return compare(0, npos, __s, traits_type::length(__s));
4102}
4103
4104template <class _CharT, class _Traits, class _Allocator>
4105_LIBCPP_CONSTEXPR_AFTER_CXX17
4106int
4107basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4108                                                   size_type __n1,
4109                                                   const value_type* __s) const
4110{
4111    _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
4112    return compare(__pos1, __n1, __s, traits_type::length(__s));
4113}
4114
4115// __invariants
4116
4117template<class _CharT, class _Traits, class _Allocator>
4118inline _LIBCPP_CONSTEXPR_AFTER_CXX17
4119bool
4120basic_string<_CharT, _Traits, _Allocator>::__invariants() const
4121{
4122    if (size() > capacity())
4123        return false;
4124    if (capacity() < __min_cap - 1)
4125        return false;
4126    if (data() == nullptr)
4127        return false;
4128    if (data()[size()] != value_type())
4129        return false;
4130    return true;
4131}
4132
4133// __clear_and_shrink
4134
4135template<class _CharT, class _Traits, class _Allocator>
4136inline _LIBCPP_CONSTEXPR_AFTER_CXX17
4137void
4138basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
4139{
4140    clear();
4141    if(__is_long())
4142    {
4143        __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
4144        __set_long_cap(0);
4145        __set_short_size(0);
4146        traits_type::assign(*__get_short_pointer(), value_type());
4147    }
4148}
4149
4150// operator==
4151
4152template<class _CharT, class _Traits, class _Allocator>
4153inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
4154bool
4155operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4156           const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
4157{
4158    size_t __lhs_sz = __lhs.size();
4159    return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
4160                                                        __rhs.data(),
4161                                                        __lhs_sz) == 0;
4162}
4163
4164template<class _Allocator>
4165inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
4166bool
4167operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
4168           const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
4169{
4170    size_t __lhs_sz = __lhs.size();
4171    if (__lhs_sz != __rhs.size())
4172        return false;
4173    const char* __lp = __lhs.data();
4174    const char* __rp = __rhs.data();
4175    if (__lhs.__is_long())
4176        return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
4177    for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
4178        if (*__lp != *__rp)
4179            return false;
4180    return true;
4181}
4182
4183template<class _CharT, class _Traits, class _Allocator>
4184inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
4185bool
4186operator==(const _CharT* __lhs,
4187           const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
4188{
4189    typedef basic_string<_CharT, _Traits, _Allocator> _String;
4190    _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
4191    size_t __lhs_len = _Traits::length(__lhs);
4192    if (__lhs_len != __rhs.size()) return false;
4193    return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
4194}
4195
4196template<class _CharT, class _Traits, class _Allocator>
4197inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
4198bool
4199operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
4200           const _CharT* __rhs) _NOEXCEPT
4201{
4202    typedef basic_string<_CharT, _Traits, _Allocator> _String;
4203    _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
4204    size_t __rhs_len = _Traits::length(__rhs);
4205    if (__rhs_len != __lhs.size()) return false;
4206    return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
4207}
4208
4209template<class _CharT, class _Traits, class _Allocator>
4210inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
4211bool
4212operator!=(const basic_string<_CharT,_Traits,_Allocator>& __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 _CharT* __lhs,
4222           const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
4223{
4224    return !(__lhs == __rhs);
4225}
4226
4227template<class _CharT, class _Traits, class _Allocator>
4228inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
4229bool
4230operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4231           const _CharT* __rhs) _NOEXCEPT
4232{
4233    return !(__lhs == __rhs);
4234}
4235
4236// operator<
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 basic_string<_CharT, _Traits, _Allocator>& __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 basic_string<_CharT, _Traits, _Allocator>& __lhs,
4251           const _CharT* __rhs) _NOEXCEPT
4252{
4253    return __lhs.compare(__rhs) < 0;
4254}
4255
4256template<class _CharT, class _Traits, class _Allocator>
4257inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
4258bool
4259operator< (const _CharT* __lhs,
4260           const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
4261{
4262    return __rhs.compare(__lhs) > 0;
4263}
4264
4265// operator>
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 basic_string<_CharT, _Traits, _Allocator>& __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 basic_string<_CharT, _Traits, _Allocator>& __lhs,
4280           const _CharT* __rhs) _NOEXCEPT
4281{
4282    return __rhs < __lhs;
4283}
4284
4285template<class _CharT, class _Traits, class _Allocator>
4286inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
4287bool
4288operator> (const _CharT* __lhs,
4289           const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
4290{
4291    return __rhs < __lhs;
4292}
4293
4294// operator<=
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 basic_string<_CharT, _Traits, _Allocator>& __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 basic_string<_CharT, _Traits, _Allocator>& __lhs,
4309           const _CharT* __rhs) _NOEXCEPT
4310{
4311    return !(__rhs < __lhs);
4312}
4313
4314template<class _CharT, class _Traits, class _Allocator>
4315inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
4316bool
4317operator<=(const _CharT* __lhs,
4318           const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
4319{
4320    return !(__rhs < __lhs);
4321}
4322
4323// operator>=
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 basic_string<_CharT, _Traits, _Allocator>& __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 basic_string<_CharT, _Traits, _Allocator>& __lhs,
4338           const _CharT* __rhs) _NOEXCEPT
4339{
4340    return !(__lhs < __rhs);
4341}
4342
4343template<class _CharT, class _Traits, class _Allocator>
4344inline _LIBCPP_CONSTEXPR_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
4345bool
4346operator>=(const _CharT* __lhs,
4347           const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
4348{
4349    return !(__lhs < __rhs);
4350}
4351
4352// operator +
4353
4354template<class _CharT, class _Traits, class _Allocator>
4355_LIBCPP_CONSTEXPR_AFTER_CXX17
4356basic_string<_CharT, _Traits, _Allocator>
4357operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4358          const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4359{
4360    using _String = basic_string<_CharT, _Traits, _Allocator>;
4361    auto __lhs_sz = __lhs.size();
4362    auto __rhs_sz = __rhs.size();
4363    _String __r(__uninitialized_size_tag(),
4364                __lhs_sz + __rhs_sz,
4365                _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4366    auto __ptr = std::__to_address(__r.__get_pointer());
4367    _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
4368    _Traits::copy(__ptr + __lhs_sz, __rhs.data(), __rhs_sz);
4369    _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
4370    return __r;
4371}
4372
4373template<class _CharT, class _Traits, class _Allocator>
4374_LIBCPP_CONSTEXPR_AFTER_CXX17
4375basic_string<_CharT, _Traits, _Allocator>
4376operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4377{
4378    using _String = basic_string<_CharT, _Traits, _Allocator>;
4379    auto __lhs_sz = _Traits::length(__lhs);
4380    auto __rhs_sz = __rhs.size();
4381    _String __r(__uninitialized_size_tag(),
4382                __lhs_sz + __rhs_sz,
4383                _String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator()));
4384    auto __ptr = std::__to_address(__r.__get_pointer());
4385    _Traits::copy(__ptr, __lhs, __lhs_sz);
4386    _Traits::copy(__ptr + __lhs_sz, __rhs.data(), __rhs_sz);
4387    _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
4388    return __r;
4389}
4390
4391template<class _CharT, class _Traits, class _Allocator>
4392_LIBCPP_CONSTEXPR_AFTER_CXX17
4393basic_string<_CharT, _Traits, _Allocator>
4394operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4395{
4396    using _String = basic_string<_CharT, _Traits, _Allocator>;
4397    typename _String::size_type __rhs_sz = __rhs.size();
4398    _String __r(__uninitialized_size_tag(),
4399                __rhs_sz + 1,
4400                _String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator()));
4401    auto __ptr = std::__to_address(__r.__get_pointer());
4402    _Traits::assign(__ptr, 1, __lhs);
4403    _Traits::copy(__ptr + 1, __rhs.data(), __rhs_sz);
4404    _Traits::assign(__ptr + 1 + __rhs_sz, 1, _CharT());
4405    return __r;
4406}
4407
4408template<class _CharT, class _Traits, class _Allocator>
4409inline _LIBCPP_CONSTEXPR_AFTER_CXX17
4410basic_string<_CharT, _Traits, _Allocator>
4411operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
4412{
4413    using _String = basic_string<_CharT, _Traits, _Allocator>;
4414    typename _String::size_type __lhs_sz = __lhs.size();
4415    typename _String::size_type __rhs_sz = _Traits::length(__rhs);
4416    _String __r(__uninitialized_size_tag(),
4417                __lhs_sz + __rhs_sz,
4418                _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4419    auto __ptr = std::__to_address(__r.__get_pointer());
4420    _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
4421    _Traits::copy(__ptr + __lhs_sz, __rhs, __rhs_sz);
4422    _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
4423    return __r;
4424}
4425
4426template<class _CharT, class _Traits, class _Allocator>
4427_LIBCPP_CONSTEXPR_AFTER_CXX17
4428basic_string<_CharT, _Traits, _Allocator>
4429operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
4430{
4431    using _String = basic_string<_CharT, _Traits, _Allocator>;
4432    typename _String::size_type __lhs_sz = __lhs.size();
4433    _String __r(__uninitialized_size_tag(),
4434                __lhs_sz + 1,
4435                _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4436    auto __ptr = std::__to_address(__r.__get_pointer());
4437    _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
4438    _Traits::assign(__ptr + __lhs_sz, 1, __rhs);
4439    _Traits::assign(__ptr + 1 + __lhs_sz, 1, _CharT());
4440    return __r;
4441}
4442
4443#ifndef _LIBCPP_CXX03_LANG
4444
4445template<class _CharT, class _Traits, class _Allocator>
4446inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
4447basic_string<_CharT, _Traits, _Allocator>
4448operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4449{
4450    return std::move(__lhs.append(__rhs));
4451}
4452
4453template<class _CharT, class _Traits, class _Allocator>
4454inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
4455basic_string<_CharT, _Traits, _Allocator>
4456operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4457{
4458    return std::move(__rhs.insert(0, __lhs));
4459}
4460
4461template<class _CharT, class _Traits, class _Allocator>
4462inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
4463basic_string<_CharT, _Traits, _Allocator>
4464operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4465{
4466    return std::move(__lhs.append(__rhs));
4467}
4468
4469template<class _CharT, class _Traits, class _Allocator>
4470inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
4471basic_string<_CharT, _Traits, _Allocator>
4472operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4473{
4474    return std::move(__rhs.insert(0, __lhs));
4475}
4476
4477template<class _CharT, class _Traits, class _Allocator>
4478inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
4479basic_string<_CharT, _Traits, _Allocator>
4480operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4481{
4482    __rhs.insert(__rhs.begin(), __lhs);
4483    return std::move(__rhs);
4484}
4485
4486template<class _CharT, class _Traits, class _Allocator>
4487inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
4488basic_string<_CharT, _Traits, _Allocator>
4489operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
4490{
4491    return std::move(__lhs.append(__rhs));
4492}
4493
4494template<class _CharT, class _Traits, class _Allocator>
4495inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
4496basic_string<_CharT, _Traits, _Allocator>
4497operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
4498{
4499    __lhs.push_back(__rhs);
4500    return std::move(__lhs);
4501}
4502
4503#endif // _LIBCPP_CXX03_LANG
4504
4505// swap
4506
4507template<class _CharT, class _Traits, class _Allocator>
4508inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
4509void
4510swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
4511     basic_string<_CharT, _Traits, _Allocator>& __rhs)
4512     _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
4513{
4514    __lhs.swap(__rhs);
4515}
4516
4517_LIBCPP_FUNC_VIS int                stoi  (const string& __str, size_t* __idx = nullptr, int __base = 10);
4518_LIBCPP_FUNC_VIS long               stol  (const string& __str, size_t* __idx = nullptr, int __base = 10);
4519_LIBCPP_FUNC_VIS unsigned long      stoul (const string& __str, size_t* __idx = nullptr, int __base = 10);
4520_LIBCPP_FUNC_VIS long long          stoll (const string& __str, size_t* __idx = nullptr, int __base = 10);
4521_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);
4522
4523_LIBCPP_FUNC_VIS float       stof (const string& __str, size_t* __idx = nullptr);
4524_LIBCPP_FUNC_VIS double      stod (const string& __str, size_t* __idx = nullptr);
4525_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = nullptr);
4526
4527_LIBCPP_FUNC_VIS string to_string(int __val);
4528_LIBCPP_FUNC_VIS string to_string(unsigned __val);
4529_LIBCPP_FUNC_VIS string to_string(long __val);
4530_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
4531_LIBCPP_FUNC_VIS string to_string(long long __val);
4532_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
4533_LIBCPP_FUNC_VIS string to_string(float __val);
4534_LIBCPP_FUNC_VIS string to_string(double __val);
4535_LIBCPP_FUNC_VIS string to_string(long double __val);
4536
4537#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
4538_LIBCPP_FUNC_VIS int                stoi  (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4539_LIBCPP_FUNC_VIS long               stol  (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4540_LIBCPP_FUNC_VIS unsigned long      stoul (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4541_LIBCPP_FUNC_VIS long long          stoll (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4542_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4543
4544_LIBCPP_FUNC_VIS float       stof (const wstring& __str, size_t* __idx = nullptr);
4545_LIBCPP_FUNC_VIS double      stod (const wstring& __str, size_t* __idx = nullptr);
4546_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = nullptr);
4547
4548_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
4549_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
4550_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
4551_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
4552_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
4553_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
4554_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
4555_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
4556_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
4557#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
4558
4559template<class _CharT, class _Traits, class _Allocator>
4560_LIBCPP_TEMPLATE_DATA_VIS
4561const typename basic_string<_CharT, _Traits, _Allocator>::size_type
4562               basic_string<_CharT, _Traits, _Allocator>::npos;
4563
4564template <class _CharT, class _Allocator>
4565struct _LIBCPP_TEMPLATE_VIS
4566    hash<basic_string<_CharT, char_traits<_CharT>, _Allocator> >
4567    : public unary_function<
4568          basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t>
4569{
4570    size_t
4571    operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT
4572    { return __do_string_hash(__val.data(), __val.data() + __val.size()); }
4573};
4574
4575
4576template<class _CharT, class _Traits, class _Allocator>
4577basic_ostream<_CharT, _Traits>&
4578operator<<(basic_ostream<_CharT, _Traits>& __os,
4579           const basic_string<_CharT, _Traits, _Allocator>& __str);
4580
4581template<class _CharT, class _Traits, class _Allocator>
4582basic_istream<_CharT, _Traits>&
4583operator>>(basic_istream<_CharT, _Traits>& __is,
4584           basic_string<_CharT, _Traits, _Allocator>& __str);
4585
4586template<class _CharT, class _Traits, class _Allocator>
4587basic_istream<_CharT, _Traits>&
4588getline(basic_istream<_CharT, _Traits>& __is,
4589        basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4590
4591template<class _CharT, class _Traits, class _Allocator>
4592inline _LIBCPP_HIDE_FROM_ABI
4593basic_istream<_CharT, _Traits>&
4594getline(basic_istream<_CharT, _Traits>& __is,
4595        basic_string<_CharT, _Traits, _Allocator>& __str);
4596
4597template<class _CharT, class _Traits, class _Allocator>
4598inline _LIBCPP_HIDE_FROM_ABI
4599basic_istream<_CharT, _Traits>&
4600getline(basic_istream<_CharT, _Traits>&& __is,
4601        basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4602
4603template<class _CharT, class _Traits, class _Allocator>
4604inline _LIBCPP_HIDE_FROM_ABI
4605basic_istream<_CharT, _Traits>&
4606getline(basic_istream<_CharT, _Traits>&& __is,
4607        basic_string<_CharT, _Traits, _Allocator>& __str);
4608
4609#if _LIBCPP_STD_VER > 17
4610template <class _CharT, class _Traits, class _Allocator, class _Up>
4611inline _LIBCPP_HIDE_FROM_ABI
4612    typename basic_string<_CharT, _Traits, _Allocator>::size_type
4613    erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
4614  auto __old_size = __str.size();
4615  __str.erase(std::remove(__str.begin(), __str.end(), __v), __str.end());
4616  return __old_size - __str.size();
4617}
4618
4619template <class _CharT, class _Traits, class _Allocator, class _Predicate>
4620inline _LIBCPP_HIDE_FROM_ABI
4621    typename basic_string<_CharT, _Traits, _Allocator>::size_type
4622    erase_if(basic_string<_CharT, _Traits, _Allocator>& __str,
4623             _Predicate __pred) {
4624  auto __old_size = __str.size();
4625  __str.erase(std::remove_if(__str.begin(), __str.end(), __pred),
4626              __str.end());
4627  return __old_size - __str.size();
4628}
4629#endif
4630
4631#if _LIBCPP_DEBUG_LEVEL == 2
4632
4633template<class _CharT, class _Traits, class _Allocator>
4634bool
4635basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4636{
4637    return data() <= std::__to_address(__i->base()) &&
4638           std::__to_address(__i->base()) < data() + size();
4639}
4640
4641template<class _CharT, class _Traits, class _Allocator>
4642bool
4643basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4644{
4645    return data() < std::__to_address(__i->base()) &&
4646           std::__to_address(__i->base()) <= data() + size();
4647}
4648
4649template<class _CharT, class _Traits, class _Allocator>
4650bool
4651basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4652{
4653    const value_type* __p = std::__to_address(__i->base()) + __n;
4654    return data() <= __p && __p <= data() + size();
4655}
4656
4657template<class _CharT, class _Traits, class _Allocator>
4658bool
4659basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4660{
4661    const value_type* __p = std::__to_address(__i->base()) + __n;
4662    return data() <= __p && __p < data() + size();
4663}
4664
4665#endif // _LIBCPP_DEBUG_LEVEL == 2
4666
4667#if _LIBCPP_STD_VER > 11
4668// Literal suffixes for basic_string [basic.string.literals]
4669inline namespace literals
4670{
4671  inline namespace string_literals
4672  {
4673    inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
4674    basic_string<char> operator "" s( const char *__str, size_t __len )
4675    {
4676        return basic_string<char> (__str, __len);
4677    }
4678
4679#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
4680    inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
4681    basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4682    {
4683        return basic_string<wchar_t> (__str, __len);
4684    }
4685#endif
4686
4687#ifndef _LIBCPP_HAS_NO_CHAR8_T
4688    inline _LIBCPP_HIDE_FROM_ABI constexpr
4689    basic_string<char8_t> operator "" s(const char8_t *__str, size_t __len) _NOEXCEPT
4690    {
4691        return basic_string<char8_t> (__str, __len);
4692    }
4693#endif
4694
4695    inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
4696    basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4697    {
4698        return basic_string<char16_t> (__str, __len);
4699    }
4700
4701    inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17
4702    basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4703    {
4704        return basic_string<char32_t> (__str, __len);
4705    }
4706  } // namespace string_literals
4707} // namespace literals
4708
4709#if _LIBCPP_STD_VER > 17
4710template <>
4711inline constexpr bool __format::__enable_insertable<std::basic_string<char>> = true;
4712#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
4713template <>
4714inline constexpr bool __format::__enable_insertable<std::basic_string<wchar_t>> = true;
4715#endif
4716#endif
4717
4718#endif
4719
4720_LIBCPP_END_NAMESPACE_STD
4721
4722_LIBCPP_POP_MACROS
4723
4724#endif // _LIBCPP_STRING
4725