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