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