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