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