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