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