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