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