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