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