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