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