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