blob: fe42bbf1d998554588640ccc67a510d8f62b26d3 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===--------------------------- string -----------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
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
Howard Hinnanta6119a82011-05-29 19:57:12 +000053 static void assign(char_type& c1, const char_type& c2) noexcept;
Howard Hinnant03d71812012-07-20 19:09:12 +000054 static constexpr bool eq(char_type c1, char_type c2) noexcept;
55 static constexpr bool lt(char_type c1, char_type c2) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000056
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
Howard Hinnant03d71812012-07-20 19:09:12 +000064 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;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000069};
70
71template <> struct char_traits<char>;
72template <> struct char_traits<wchar_t>;
73
Howard Hinnant324bb032010-08-22 00:02:43 +000074template<class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000075class basic_string
76{
Howard Hinnant324bb032010-08-22 00:02:43 +000077public:
78// types:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000079 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
Howard Hinnant53f7d4c2011-06-03 18:40:47 +000095 basic_string()
96 noexcept(is_nothrow_default_constructible<allocator_type>::value);
97 explicit basic_string(const allocator_type& a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000098 basic_string(const basic_string& str);
Howard Hinnant53f7d4c2011-06-03 18:40:47 +000099 basic_string(basic_string&& str)
100 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000101 basic_string(const basic_string& str, size_type pos, size_type n = npos,
102 const allocator_type& a = allocator_type());
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000103 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());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000105 basic_string(size_type n, value_type c, const allocator_type& a = allocator_type());
106 template<class InputIterator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000107 basic_string(InputIterator begin, InputIterator end,
108 const allocator_type& a = allocator_type());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000109 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);
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000116 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);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000120 basic_string& operator=(const value_type* s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000121 basic_string& operator=(value_type c);
122 basic_string& operator=(initializer_list<value_type>);
123
Howard Hinnanta6119a82011-05-29 19:57:12 +0000124 iterator begin() noexcept;
125 const_iterator begin() const noexcept;
126 iterator end() noexcept;
127 const_iterator end() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000128
Howard Hinnanta6119a82011-05-29 19:57:12 +0000129 reverse_iterator rbegin() noexcept;
130 const_reverse_iterator rbegin() const noexcept;
131 reverse_iterator rend() noexcept;
132 const_reverse_iterator rend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000133
Howard Hinnanta6119a82011-05-29 19:57:12 +0000134 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;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000138
Howard Hinnanta6119a82011-05-29 19:57:12 +0000139 size_type size() const noexcept;
140 size_type length() const noexcept;
141 size_type max_size() const noexcept;
142 size_type capacity() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000143
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();
Howard Hinnanta6119a82011-05-29 19:57:12 +0000149 void clear() noexcept;
150 bool empty() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000151
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);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000159 basic_string& operator+=(const value_type* s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000160 basic_string& operator+=(value_type c);
161 basic_string& operator+=(initializer_list<value_type>);
162
163 basic_string& append(const basic_string& str);
Marshall Clowa93b5e22014-03-04 19:17:19 +0000164 basic_string& append(const basic_string& str, size_type pos, size_type n=npos); //C++14
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000165 basic_string& append(const value_type* s, size_type n);
166 basic_string& append(const value_type* s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000167 basic_string& append(size_type n, value_type c);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000168 template<class InputIterator>
169 basic_string& append(InputIterator first, InputIterator last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000170 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);
Howard Hinnanta6119a82011-05-29 19:57:12 +0000180 basic_string& assign(basic_string&& str);
Marshall Clowa93b5e22014-03-04 19:17:19 +0000181 basic_string& assign(const basic_string& str, size_type pos, size_type n=npos); // C++14
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000182 basic_string& assign(const value_type* s, size_type n);
183 basic_string& assign(const value_type* s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000184 basic_string& assign(size_type n, value_type c);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000185 template<class InputIterator>
186 basic_string& assign(InputIterator first, InputIterator last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000187 basic_string& assign(initializer_list<value_type>);
188
189 basic_string& insert(size_type pos1, const basic_string& str);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000190 basic_string& insert(size_type pos1, const basic_string& str,
191 size_type pos2, size_type n);
Marshall Clowa93b5e22014-03-04 19:17:19 +0000192 basic_string& insert(size_type pos, const value_type* s, size_type n=npos); //C++14
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000193 basic_string& insert(size_type pos, const value_type* s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000194 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);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000197 template<class InputIterator>
198 iterator insert(const_iterator p, InputIterator first, InputIterator last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000199 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);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000206 basic_string& replace(size_type pos1, size_type n1, const basic_string& str,
Marshall Clowa93b5e22014-03-04 19:17:19 +0000207 size_type pos2, size_type n2=npos); // C++14
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000208 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);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000210 basic_string& replace(size_type pos, size_type n1, size_type n2, value_type c);
Howard Hinnant7b2cb482010-11-17 21:11:40 +0000211 basic_string& replace(const_iterator i1, const_iterator i2, const basic_string& str);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000212 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);
Howard Hinnant7b2cb482010-11-17 21:11:40 +0000214 basic_string& replace(const_iterator i1, const_iterator i2, size_type n, value_type c);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000215 template<class InputIterator>
Howard Hinnant7b2cb482010-11-17 21:11:40 +0000216 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>);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000218
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000219 size_type copy(value_type* s, size_type n, size_type pos = 0) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000220 basic_string substr(size_type pos = 0, size_type n = npos) const;
221
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000222 void swap(basic_string& str)
223 noexcept(!allocator_type::propagate_on_container_swap::value ||
224 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000225
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000226 const value_type* c_str() const noexcept;
227 const value_type* data() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000228
Howard Hinnanta6119a82011-05-29 19:57:12 +0000229 allocator_type get_allocator() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000230
Howard Hinnanta6119a82011-05-29 19:57:12 +0000231 size_type find(const basic_string& str, size_type pos = 0) const noexcept;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000232 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;
Howard Hinnanta6119a82011-05-29 19:57:12 +0000234 size_type find(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000235
Howard Hinnanta6119a82011-05-29 19:57:12 +0000236 size_type rfind(const basic_string& str, size_type pos = npos) const noexcept;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000237 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;
Howard Hinnanta6119a82011-05-29 19:57:12 +0000239 size_type rfind(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000240
Howard Hinnanta6119a82011-05-29 19:57:12 +0000241 size_type find_first_of(const basic_string& str, size_type pos = 0) const noexcept;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000242 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;
Howard Hinnanta6119a82011-05-29 19:57:12 +0000244 size_type find_first_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000245
Howard Hinnanta6119a82011-05-29 19:57:12 +0000246 size_type find_last_of(const basic_string& str, size_type pos = npos) const noexcept;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000247 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;
Howard Hinnanta6119a82011-05-29 19:57:12 +0000249 size_type find_last_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000250
Howard Hinnanta6119a82011-05-29 19:57:12 +0000251 size_type find_first_not_of(const basic_string& str, size_type pos = 0) const noexcept;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000252 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;
Howard Hinnanta6119a82011-05-29 19:57:12 +0000254 size_type find_first_not_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000255
Howard Hinnanta6119a82011-05-29 19:57:12 +0000256 size_type find_last_not_of(const basic_string& str, size_type pos = npos) const noexcept;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000257 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;
Howard Hinnanta6119a82011-05-29 19:57:12 +0000259 size_type find_last_not_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000260
Howard Hinnanta6119a82011-05-29 19:57:12 +0000261 int compare(const basic_string& str) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000262 int compare(size_type pos1, size_type n1, const basic_string& str) const;
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000263 int compare(size_type pos1, size_type n1, const basic_string& str,
Marshall Clowa93b5e22014-03-04 19:17:19 +0000264 size_type pos2, size_type n2=npos) const; // C++14
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000265 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;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000268
269 bool __invariants() const;
270};
271
272template<class charT, class traits, class Allocator>
273basic_string<charT, traits, Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000274operator+(const basic_string<charT, traits, Allocator>& lhs,
275 const basic_string<charT, traits, Allocator>& rhs);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000276
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>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000294bool operator==(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +0000295 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000296
297template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000298bool operator==(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000299
300template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000301bool operator==(const basic_string<charT,traits,Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000302
Howard Hinnant324bb032010-08-22 00:02:43 +0000303template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000304bool operator!=(const basic_string<charT,traits,Allocator>& lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +0000305 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000306
307template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000308bool operator!=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000309
310template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000311bool operator!=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000312
313template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000314bool operator< (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +0000315 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000316
317template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000318bool operator< (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000319
320template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000321bool operator< (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000322
323template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000324bool operator> (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +0000325 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000326
327template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000328bool operator> (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000329
330template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000331bool operator> (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000332
333template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000334bool operator<=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +0000335 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000336
337template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000338bool operator<=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000339
340template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000341bool operator<=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000342
343template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000344bool operator>=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +0000345 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000346
347template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000348bool operator>=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000349
350template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000351bool operator>=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000352
353template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000354void swap(basic_string<charT, traits, Allocator>& lhs,
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000355 basic_string<charT, traits, Allocator>& rhs)
356 noexcept(noexcept(lhs.swap(rhs)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000357
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>
Howard Hinnant324bb032010-08-22 00:02:43 +0000367basic_istream<charT, traits>&
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000368getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str,
369 charT delim);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000370
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;
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000377typedef 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);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000419
420template <> struct hash<string>;
421template <> struct hash<u16string>;
422template <> struct hash<u32string>;
423template <> struct hash<wstring>;
424
Marshall Clow15234322013-07-23 17:05:24 +0000425basic_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
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000430} // std
431
432*/
433
434#include <__config>
435#include <iosfwd>
436#include <cstring>
Howard Hinnantadff4892010-05-24 17:49:41 +0000437#include <cstdio> // For EOF.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000438#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
Howard Hinnant499cea12013-08-23 17:37:05 +0000450#if defined(_LIBCPP_NO_EXCEPTIONS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000451#include <cassert>
452#endif
453
Howard Hinnant66c6f972011-11-29 16:45:27 +0000454#include <__undef_min_max>
455
Eric Fiselierb9536102014-08-10 23:53:08 +0000456#include <__debug>
457
Howard Hinnant08e17472011-10-17 20:05:10 +0000458#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000459#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000460#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000461
462_LIBCPP_BEGIN_NAMESPACE_STD
463
464// fpos
465
466template <class _StateT>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000467class _LIBCPP_TYPE_VIS_ONLY fpos
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000468{
469private:
470 _StateT __st_;
471 streamoff __off_;
472public:
473 _LIBCPP_INLINE_VISIBILITY fpos(streamoff __off = streamoff()) : __st_(), __off_(__off) {}
474
475 _LIBCPP_INLINE_VISIBILITY operator streamoff() const {return __off_;}
476
477 _LIBCPP_INLINE_VISIBILITY _StateT state() const {return __st_;}
478 _LIBCPP_INLINE_VISIBILITY void state(_StateT __st) {__st_ = __st;}
479
480 _LIBCPP_INLINE_VISIBILITY fpos& operator+=(streamoff __off) {__off_ += __off; return *this;}
481 _LIBCPP_INLINE_VISIBILITY fpos operator+ (streamoff __off) const {fpos __t(*this); __t += __off; return __t;}
482 _LIBCPP_INLINE_VISIBILITY fpos& operator-=(streamoff __off) {__off_ -= __off; return *this;}
483 _LIBCPP_INLINE_VISIBILITY fpos operator- (streamoff __off) const {fpos __t(*this); __t -= __off; return __t;}
484};
485
486template <class _StateT>
487inline _LIBCPP_INLINE_VISIBILITY
488streamoff operator-(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
489 {return streamoff(__x) - streamoff(__y);}
490
491template <class _StateT>
492inline _LIBCPP_INLINE_VISIBILITY
493bool operator==(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
494 {return streamoff(__x) == streamoff(__y);}
495
496template <class _StateT>
497inline _LIBCPP_INLINE_VISIBILITY
498bool operator!=(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
499 {return streamoff(__x) != streamoff(__y);}
500
501// char_traits
502
503template <class _CharT>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000504struct _LIBCPP_TYPE_VIS_ONLY char_traits
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000505{
506 typedef _CharT char_type;
507 typedef int int_type;
508 typedef streamoff off_type;
509 typedef streampos pos_type;
510 typedef mbstate_t state_type;
511
Dan Albert6d9505a2014-09-17 16:34:29 +0000512 static inline void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT
Howard Hinnanta6119a82011-05-29 19:57:12 +0000513 {__c1 = __c2;}
Dan Albert6d9505a2014-09-17 16:34:29 +0000514 static inline _LIBCPP_CONSTEXPR bool eq(char_type __c1, char_type __c2) _NOEXCEPT
Howard Hinnanta6119a82011-05-29 19:57:12 +0000515 {return __c1 == __c2;}
Dan Albert6d9505a2014-09-17 16:34:29 +0000516 static inline _LIBCPP_CONSTEXPR bool lt(char_type __c1, char_type __c2) _NOEXCEPT
Howard Hinnanta6119a82011-05-29 19:57:12 +0000517 {return __c1 < __c2;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000518
519 static int compare(const char_type* __s1, const char_type* __s2, size_t __n);
520 static size_t length(const char_type* __s);
521 static const char_type* find(const char_type* __s, size_t __n, const char_type& __a);
522 static char_type* move(char_type* __s1, const char_type* __s2, size_t __n);
523 static char_type* copy(char_type* __s1, const char_type* __s2, size_t __n);
524 static char_type* assign(char_type* __s, size_t __n, char_type __a);
525
Dan Albert6d9505a2014-09-17 16:34:29 +0000526 static inline _LIBCPP_CONSTEXPR int_type not_eof(int_type __c) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000527 {return eq_int_type(__c, eof()) ? ~eof() : __c;}
Dan Albert6d9505a2014-09-17 16:34:29 +0000528 static inline _LIBCPP_CONSTEXPR char_type to_char_type(int_type __c) _NOEXCEPT
Howard Hinnanta6119a82011-05-29 19:57:12 +0000529 {return char_type(__c);}
Dan Albert6d9505a2014-09-17 16:34:29 +0000530 static inline _LIBCPP_CONSTEXPR int_type to_int_type(char_type __c) _NOEXCEPT
Howard Hinnanta6119a82011-05-29 19:57:12 +0000531 {return int_type(__c);}
Dan Albert6d9505a2014-09-17 16:34:29 +0000532 static inline _LIBCPP_CONSTEXPR bool eq_int_type(int_type __c1, int_type __c2) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000533 {return __c1 == __c2;}
Dan Albert6d9505a2014-09-17 16:34:29 +0000534 static inline _LIBCPP_CONSTEXPR int_type eof() _NOEXCEPT
Howard Hinnanta6119a82011-05-29 19:57:12 +0000535 {return int_type(EOF);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000536};
537
538template <class _CharT>
539int
540char_traits<_CharT>::compare(const char_type* __s1, const char_type* __s2, size_t __n)
541{
542 for (; __n; --__n, ++__s1, ++__s2)
543 {
544 if (lt(*__s1, *__s2))
545 return -1;
546 if (lt(*__s2, *__s1))
547 return 1;
548 }
549 return 0;
550}
551
552template <class _CharT>
553inline _LIBCPP_INLINE_VISIBILITY
554size_t
555char_traits<_CharT>::length(const char_type* __s)
556{
557 size_t __len = 0;
558 for (; !eq(*__s, char_type(0)); ++__s)
559 ++__len;
560 return __len;
561}
562
563template <class _CharT>
564inline _LIBCPP_INLINE_VISIBILITY
565const _CharT*
566char_traits<_CharT>::find(const char_type* __s, size_t __n, const char_type& __a)
567{
568 for (; __n; --__n)
569 {
570 if (eq(*__s, __a))
571 return __s;
572 ++__s;
573 }
574 return 0;
575}
576
577template <class _CharT>
578_CharT*
579char_traits<_CharT>::move(char_type* __s1, const char_type* __s2, size_t __n)
580{
581 char_type* __r = __s1;
582 if (__s1 < __s2)
583 {
584 for (; __n; --__n, ++__s1, ++__s2)
585 assign(*__s1, *__s2);
586 }
587 else if (__s2 < __s1)
588 {
589 __s1 += __n;
590 __s2 += __n;
591 for (; __n; --__n)
592 assign(*--__s1, *--__s2);
593 }
594 return __r;
595}
596
597template <class _CharT>
598inline _LIBCPP_INLINE_VISIBILITY
599_CharT*
600char_traits<_CharT>::copy(char_type* __s1, const char_type* __s2, size_t __n)
601{
Howard Hinnant499cea12013-08-23 17:37:05 +0000602 _LIBCPP_ASSERT(__s2 < __s1 || __s2 >= __s1+__n, "char_traits::copy overlapped range");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000603 char_type* __r = __s1;
604 for (; __n; --__n, ++__s1, ++__s2)
605 assign(*__s1, *__s2);
606 return __r;
607}
608
609template <class _CharT>
610inline _LIBCPP_INLINE_VISIBILITY
611_CharT*
612char_traits<_CharT>::assign(char_type* __s, size_t __n, char_type __a)
613{
614 char_type* __r = __s;
615 for (; __n; --__n, ++__s)
616 assign(*__s, __a);
617 return __r;
618}
619
620// char_traits<char>
621
622template <>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000623struct _LIBCPP_TYPE_VIS_ONLY char_traits<char>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000624{
625 typedef char char_type;
626 typedef int int_type;
627 typedef streamoff off_type;
628 typedef streampos pos_type;
629 typedef mbstate_t state_type;
630
Dan Albert6d9505a2014-09-17 16:34:29 +0000631 static inline void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT
Howard Hinnanta6119a82011-05-29 19:57:12 +0000632 {__c1 = __c2;}
Dan Albert6d9505a2014-09-17 16:34:29 +0000633 static inline _LIBCPP_CONSTEXPR bool eq(char_type __c1, char_type __c2) _NOEXCEPT
Howard Hinnanta6119a82011-05-29 19:57:12 +0000634 {return __c1 == __c2;}
Dan Albert6d9505a2014-09-17 16:34:29 +0000635 static inline _LIBCPP_CONSTEXPR bool lt(char_type __c1, char_type __c2) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000636 {return (unsigned char)__c1 < (unsigned char)__c2;}
637
Dan Albert6d9505a2014-09-17 16:34:29 +0000638 static inline int compare(const char_type* __s1, const char_type* __s2, size_t __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000639 {return memcmp(__s1, __s2, __n);}
Dan Albert6d9505a2014-09-17 16:34:29 +0000640 static inline size_t length(const char_type* __s) {return strlen(__s);}
641 static inline const char_type* find(const char_type* __s, size_t __n, const char_type& __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000642 {return (const char_type*)memchr(__s, to_int_type(__a), __n);}
Dan Albert6d9505a2014-09-17 16:34:29 +0000643 static inline char_type* move(char_type* __s1, const char_type* __s2, size_t __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000644 {return (char_type*)memmove(__s1, __s2, __n);}
Dan Albert6d9505a2014-09-17 16:34:29 +0000645 static inline char_type* copy(char_type* __s1, const char_type* __s2, size_t __n)
Howard Hinnant499cea12013-08-23 17:37:05 +0000646 {
647 _LIBCPP_ASSERT(__s2 < __s1 || __s2 >= __s1+__n, "char_traits::copy overlapped range");
648 return (char_type*)memcpy(__s1, __s2, __n);
649 }
Dan Albert6d9505a2014-09-17 16:34:29 +0000650 static inline char_type* assign(char_type* __s, size_t __n, char_type __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000651 {return (char_type*)memset(__s, to_int_type(__a), __n);}
652
Dan Albert6d9505a2014-09-17 16:34:29 +0000653 static inline _LIBCPP_CONSTEXPR int_type not_eof(int_type __c) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000654 {return eq_int_type(__c, eof()) ? ~eof() : __c;}
Dan Albert6d9505a2014-09-17 16:34:29 +0000655 static inline _LIBCPP_CONSTEXPR char_type to_char_type(int_type __c) _NOEXCEPT
Howard Hinnanta6119a82011-05-29 19:57:12 +0000656 {return char_type(__c);}
Dan Albert6d9505a2014-09-17 16:34:29 +0000657 static inline _LIBCPP_CONSTEXPR int_type to_int_type(char_type __c) _NOEXCEPT
Howard Hinnanta6119a82011-05-29 19:57:12 +0000658 {return int_type((unsigned char)__c);}
Dan Albert6d9505a2014-09-17 16:34:29 +0000659 static inline _LIBCPP_CONSTEXPR bool eq_int_type(int_type __c1, int_type __c2) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000660 {return __c1 == __c2;}
Dan Albert6d9505a2014-09-17 16:34:29 +0000661 static inline _LIBCPP_CONSTEXPR int_type eof() _NOEXCEPT
Howard Hinnanta6119a82011-05-29 19:57:12 +0000662 {return int_type(EOF);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000663};
664
665// char_traits<wchar_t>
666
667template <>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000668struct _LIBCPP_TYPE_VIS_ONLY char_traits<wchar_t>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000669{
670 typedef wchar_t char_type;
671 typedef wint_t int_type;
672 typedef streamoff off_type;
673 typedef streampos pos_type;
674 typedef mbstate_t state_type;
675
Dan Albert6d9505a2014-09-17 16:34:29 +0000676 static inline void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT
Howard Hinnanta6119a82011-05-29 19:57:12 +0000677 {__c1 = __c2;}
Dan Albert6d9505a2014-09-17 16:34:29 +0000678 static inline _LIBCPP_CONSTEXPR bool eq(char_type __c1, char_type __c2) _NOEXCEPT
Howard Hinnanta6119a82011-05-29 19:57:12 +0000679 {return __c1 == __c2;}
Dan Albert6d9505a2014-09-17 16:34:29 +0000680 static inline _LIBCPP_CONSTEXPR bool lt(char_type __c1, char_type __c2) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000681 {return __c1 < __c2;}
682
Dan Albert6d9505a2014-09-17 16:34:29 +0000683 static inline int compare(const char_type* __s1, const char_type* __s2, size_t __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000684 {return wmemcmp(__s1, __s2, __n);}
Dan Albert6d9505a2014-09-17 16:34:29 +0000685 static inline size_t length(const char_type* __s)
Howard Hinnanta6119a82011-05-29 19:57:12 +0000686 {return wcslen(__s);}
Dan Albert6d9505a2014-09-17 16:34:29 +0000687 static inline const char_type* find(const char_type* __s, size_t __n, const char_type& __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000688 {return (const char_type*)wmemchr(__s, __a, __n);}
Dan Albert6d9505a2014-09-17 16:34:29 +0000689 static inline char_type* move(char_type* __s1, const char_type* __s2, size_t __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000690 {return (char_type*)wmemmove(__s1, __s2, __n);}
Dan Albert6d9505a2014-09-17 16:34:29 +0000691 static inline char_type* copy(char_type* __s1, const char_type* __s2, size_t __n)
Howard Hinnant499cea12013-08-23 17:37:05 +0000692 {
693 _LIBCPP_ASSERT(__s2 < __s1 || __s2 >= __s1+__n, "char_traits::copy overlapped range");
694 return (char_type*)wmemcpy(__s1, __s2, __n);
695 }
Dan Albert6d9505a2014-09-17 16:34:29 +0000696 static inline char_type* assign(char_type* __s, size_t __n, char_type __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000697 {return (char_type*)wmemset(__s, __a, __n);}
698
Dan Albert6d9505a2014-09-17 16:34:29 +0000699 static inline _LIBCPP_CONSTEXPR int_type not_eof(int_type __c) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000700 {return eq_int_type(__c, eof()) ? ~eof() : __c;}
Dan Albert6d9505a2014-09-17 16:34:29 +0000701 static inline _LIBCPP_CONSTEXPR char_type to_char_type(int_type __c) _NOEXCEPT
Howard Hinnanta6119a82011-05-29 19:57:12 +0000702 {return char_type(__c);}
Dan Albert6d9505a2014-09-17 16:34:29 +0000703 static inline _LIBCPP_CONSTEXPR int_type to_int_type(char_type __c) _NOEXCEPT
Howard Hinnanta6119a82011-05-29 19:57:12 +0000704 {return int_type(__c);}
Dan Albert6d9505a2014-09-17 16:34:29 +0000705 static inline _LIBCPP_CONSTEXPR bool eq_int_type(int_type __c1, int_type __c2) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000706 {return __c1 == __c2;}
Dan Albert6d9505a2014-09-17 16:34:29 +0000707 static inline _LIBCPP_CONSTEXPR int_type eof() _NOEXCEPT
Howard Hinnanta6119a82011-05-29 19:57:12 +0000708 {return int_type(WEOF);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000709};
710
711#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
712
713template <>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000714struct _LIBCPP_TYPE_VIS_ONLY char_traits<char16_t>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000715{
716 typedef char16_t char_type;
717 typedef uint_least16_t int_type;
718 typedef streamoff off_type;
719 typedef u16streampos pos_type;
720 typedef mbstate_t state_type;
721
Dan Albert6d9505a2014-09-17 16:34:29 +0000722 static inline void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT
Howard Hinnanta6119a82011-05-29 19:57:12 +0000723 {__c1 = __c2;}
Dan Albert6d9505a2014-09-17 16:34:29 +0000724 static inline _LIBCPP_CONSTEXPR bool eq(char_type __c1, char_type __c2) _NOEXCEPT
Howard Hinnanta6119a82011-05-29 19:57:12 +0000725 {return __c1 == __c2;}
Dan Albert6d9505a2014-09-17 16:34:29 +0000726 static inline _LIBCPP_CONSTEXPR bool lt(char_type __c1, char_type __c2) _NOEXCEPT
Howard Hinnanta6119a82011-05-29 19:57:12 +0000727 {return __c1 < __c2;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000728
729 static int compare(const char_type* __s1, const char_type* __s2, size_t __n);
730 static size_t length(const char_type* __s);
731 static const char_type* find(const char_type* __s, size_t __n, const char_type& __a);
732 static char_type* move(char_type* __s1, const char_type* __s2, size_t __n);
733 static char_type* copy(char_type* __s1, const char_type* __s2, size_t __n);
734 static char_type* assign(char_type* __s, size_t __n, char_type __a);
735
Dan Albert6d9505a2014-09-17 16:34:29 +0000736 static inline _LIBCPP_CONSTEXPR int_type not_eof(int_type __c) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000737 {return eq_int_type(__c, eof()) ? ~eof() : __c;}
Dan Albert6d9505a2014-09-17 16:34:29 +0000738 static inline _LIBCPP_CONSTEXPR char_type to_char_type(int_type __c) _NOEXCEPT
Howard Hinnanta6119a82011-05-29 19:57:12 +0000739 {return char_type(__c);}
Dan Albert6d9505a2014-09-17 16:34:29 +0000740 static inline _LIBCPP_CONSTEXPR int_type to_int_type(char_type __c) _NOEXCEPT
Howard Hinnanta6119a82011-05-29 19:57:12 +0000741 {return int_type(__c);}
Dan Albert6d9505a2014-09-17 16:34:29 +0000742 static inline _LIBCPP_CONSTEXPR bool eq_int_type(int_type __c1, int_type __c2) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000743 {return __c1 == __c2;}
Dan Albert6d9505a2014-09-17 16:34:29 +0000744 static inline _LIBCPP_CONSTEXPR int_type eof() _NOEXCEPT
Howard Hinnanta6119a82011-05-29 19:57:12 +0000745 {return int_type(0xDFFF);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000746};
747
748inline _LIBCPP_INLINE_VISIBILITY
749int
750char_traits<char16_t>::compare(const char_type* __s1, const char_type* __s2, size_t __n)
751{
752 for (; __n; --__n, ++__s1, ++__s2)
753 {
754 if (lt(*__s1, *__s2))
755 return -1;
756 if (lt(*__s2, *__s1))
757 return 1;
758 }
759 return 0;
760}
761
762inline _LIBCPP_INLINE_VISIBILITY
763size_t
764char_traits<char16_t>::length(const char_type* __s)
765{
766 size_t __len = 0;
767 for (; !eq(*__s, char_type(0)); ++__s)
768 ++__len;
769 return __len;
770}
771
772inline _LIBCPP_INLINE_VISIBILITY
773const char16_t*
774char_traits<char16_t>::find(const char_type* __s, size_t __n, const char_type& __a)
775{
776 for (; __n; --__n)
777 {
778 if (eq(*__s, __a))
779 return __s;
780 ++__s;
781 }
782 return 0;
783}
784
785inline _LIBCPP_INLINE_VISIBILITY
786char16_t*
787char_traits<char16_t>::move(char_type* __s1, const char_type* __s2, size_t __n)
788{
789 char_type* __r = __s1;
790 if (__s1 < __s2)
791 {
792 for (; __n; --__n, ++__s1, ++__s2)
793 assign(*__s1, *__s2);
794 }
795 else if (__s2 < __s1)
796 {
797 __s1 += __n;
798 __s2 += __n;
799 for (; __n; --__n)
800 assign(*--__s1, *--__s2);
801 }
802 return __r;
803}
804
805inline _LIBCPP_INLINE_VISIBILITY
806char16_t*
807char_traits<char16_t>::copy(char_type* __s1, const char_type* __s2, size_t __n)
808{
Howard Hinnant499cea12013-08-23 17:37:05 +0000809 _LIBCPP_ASSERT(__s2 < __s1 || __s2 >= __s1+__n, "char_traits::copy overlapped range");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000810 char_type* __r = __s1;
811 for (; __n; --__n, ++__s1, ++__s2)
812 assign(*__s1, *__s2);
813 return __r;
814}
815
816inline _LIBCPP_INLINE_VISIBILITY
817char16_t*
818char_traits<char16_t>::assign(char_type* __s, size_t __n, char_type __a)
819{
820 char_type* __r = __s;
821 for (; __n; --__n, ++__s)
822 assign(*__s, __a);
823 return __r;
824}
825
826template <>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000827struct _LIBCPP_TYPE_VIS_ONLY char_traits<char32_t>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000828{
829 typedef char32_t char_type;
830 typedef uint_least32_t int_type;
831 typedef streamoff off_type;
832 typedef u32streampos pos_type;
833 typedef mbstate_t state_type;
834
Dan Albert6d9505a2014-09-17 16:34:29 +0000835 static inline void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT
Howard Hinnanta6119a82011-05-29 19:57:12 +0000836 {__c1 = __c2;}
Dan Albert6d9505a2014-09-17 16:34:29 +0000837 static inline _LIBCPP_CONSTEXPR bool eq(char_type __c1, char_type __c2) _NOEXCEPT
Howard Hinnanta6119a82011-05-29 19:57:12 +0000838 {return __c1 == __c2;}
Dan Albert6d9505a2014-09-17 16:34:29 +0000839 static inline _LIBCPP_CONSTEXPR bool lt(char_type __c1, char_type __c2) _NOEXCEPT
Howard Hinnanta6119a82011-05-29 19:57:12 +0000840 {return __c1 < __c2;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000841
842 static int compare(const char_type* __s1, const char_type* __s2, size_t __n);
843 static size_t length(const char_type* __s);
844 static const char_type* find(const char_type* __s, size_t __n, const char_type& __a);
845 static char_type* move(char_type* __s1, const char_type* __s2, size_t __n);
846 static char_type* copy(char_type* __s1, const char_type* __s2, size_t __n);
847 static char_type* assign(char_type* __s, size_t __n, char_type __a);
848
Dan Albert6d9505a2014-09-17 16:34:29 +0000849 static inline _LIBCPP_CONSTEXPR int_type not_eof(int_type __c) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000850 {return eq_int_type(__c, eof()) ? ~eof() : __c;}
Dan Albert6d9505a2014-09-17 16:34:29 +0000851 static inline _LIBCPP_CONSTEXPR char_type to_char_type(int_type __c) _NOEXCEPT
Howard Hinnanta6119a82011-05-29 19:57:12 +0000852 {return char_type(__c);}
Dan Albert6d9505a2014-09-17 16:34:29 +0000853 static inline _LIBCPP_CONSTEXPR int_type to_int_type(char_type __c) _NOEXCEPT
Howard Hinnanta6119a82011-05-29 19:57:12 +0000854 {return int_type(__c);}
Dan Albert6d9505a2014-09-17 16:34:29 +0000855 static inline _LIBCPP_CONSTEXPR bool eq_int_type(int_type __c1, int_type __c2) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000856 {return __c1 == __c2;}
Dan Albert6d9505a2014-09-17 16:34:29 +0000857 static inline _LIBCPP_CONSTEXPR int_type eof() _NOEXCEPT
Howard Hinnanta6119a82011-05-29 19:57:12 +0000858 {return int_type(0xFFFFFFFF);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000859};
860
861inline _LIBCPP_INLINE_VISIBILITY
862int
863char_traits<char32_t>::compare(const char_type* __s1, const char_type* __s2, size_t __n)
864{
865 for (; __n; --__n, ++__s1, ++__s2)
866 {
867 if (lt(*__s1, *__s2))
868 return -1;
869 if (lt(*__s2, *__s1))
870 return 1;
871 }
872 return 0;
873}
874
875inline _LIBCPP_INLINE_VISIBILITY
876size_t
877char_traits<char32_t>::length(const char_type* __s)
878{
879 size_t __len = 0;
880 for (; !eq(*__s, char_type(0)); ++__s)
881 ++__len;
882 return __len;
883}
884
885inline _LIBCPP_INLINE_VISIBILITY
886const char32_t*
887char_traits<char32_t>::find(const char_type* __s, size_t __n, const char_type& __a)
888{
889 for (; __n; --__n)
890 {
891 if (eq(*__s, __a))
892 return __s;
893 ++__s;
894 }
895 return 0;
896}
897
898inline _LIBCPP_INLINE_VISIBILITY
899char32_t*
900char_traits<char32_t>::move(char_type* __s1, const char_type* __s2, size_t __n)
901{
902 char_type* __r = __s1;
903 if (__s1 < __s2)
904 {
905 for (; __n; --__n, ++__s1, ++__s2)
906 assign(*__s1, *__s2);
907 }
908 else if (__s2 < __s1)
909 {
910 __s1 += __n;
911 __s2 += __n;
912 for (; __n; --__n)
913 assign(*--__s1, *--__s2);
914 }
915 return __r;
916}
917
918inline _LIBCPP_INLINE_VISIBILITY
919char32_t*
920char_traits<char32_t>::copy(char_type* __s1, const char_type* __s2, size_t __n)
921{
Howard Hinnant499cea12013-08-23 17:37:05 +0000922 _LIBCPP_ASSERT(__s2 < __s1 || __s2 >= __s1+__n, "char_traits::copy overlapped range");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000923 char_type* __r = __s1;
924 for (; __n; --__n, ++__s1, ++__s2)
925 assign(*__s1, *__s2);
926 return __r;
927}
928
929inline _LIBCPP_INLINE_VISIBILITY
930char32_t*
931char_traits<char32_t>::assign(char_type* __s, size_t __n, char_type __a)
932{
933 char_type* __r = __s;
934 for (; __n; --__n, ++__s)
935 assign(*__s, __a);
936 return __r;
937}
938
Howard Hinnant324bb032010-08-22 00:02:43 +0000939#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000940
Marshall Clowb671fc92013-12-09 16:00:28 +0000941// helper fns for basic_string
942
Marshall Clow37025e12014-06-10 18:51:55 +0000943// __str_find
Marshall Clowb671fc92013-12-09 16:00:28 +0000944template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
Marshall Clow360f3192014-06-02 02:22:49 +0000945_SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow37025e12014-06-10 18:51:55 +0000946__str_find(const _CharT *__p, _SizeT __sz,
Marshall Clow360f3192014-06-02 02:22:49 +0000947 _CharT __c, _SizeT __pos) _NOEXCEPT
948{
949 if (__pos >= __sz)
950 return __npos;
951 const _CharT* __r = _Traits::find(__p + __pos, __sz - __pos, __c);
952 if (__r == 0)
953 return __npos;
954 return static_cast<_SizeT>(__r - __p);
955}
956
957template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
958_SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow37025e12014-06-10 18:51:55 +0000959__str_find(const _CharT *__p, _SizeT __sz,
Marshall Clow360f3192014-06-02 02:22:49 +0000960 const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
961{
962 if (__pos > __sz || __sz - __pos < __n)
963 return __npos;
964 if (__n == 0)
965 return __pos;
Marshall Clow360f3192014-06-02 02:22:49 +0000966 const _CharT* __r =
Marshall Clow37025e12014-06-10 18:51:55 +0000967 _VSTD::__search(__p + __pos, __p + __sz,
968 __s, __s + __n, _Traits::eq,
969 random_access_iterator_tag(), random_access_iterator_tag());
Marshall Clow360f3192014-06-02 02:22:49 +0000970 if (__r == __p + __sz)
971 return __npos;
972 return static_cast<_SizeT>(__r - __p);
973}
974
975
Marshall Clow37025e12014-06-10 18:51:55 +0000976// __str_rfind
Marshall Clow360f3192014-06-02 02:22:49 +0000977
978template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
979_SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow37025e12014-06-10 18:51:55 +0000980__str_rfind(const _CharT *__p, _SizeT __sz,
Marshall Clow360f3192014-06-02 02:22:49 +0000981 _CharT __c, _SizeT __pos) _NOEXCEPT
982{
983 if (__sz < 1)
Marshall Clowd5549cc2014-07-17 15:32:20 +0000984 return __npos;
985 if (__pos < __sz)
986 ++__pos;
987 else
988 __pos = __sz;
989 for (const _CharT* __ps = __p + __pos; __ps != __p;)
990 {
991 if (_Traits::eq(*--__ps, __c))
992 return static_cast<_SizeT>(__ps - __p);
993 }
Marshall Clow360f3192014-06-02 02:22:49 +0000994 return __npos;
995}
996
997template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
998_SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow37025e12014-06-10 18:51:55 +0000999__str_rfind(const _CharT *__p, _SizeT __sz,
Marshall Clow360f3192014-06-02 02:22:49 +00001000 const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
1001{
1002 __pos = _VSTD::min(__pos, __sz);
1003 if (__n < __sz - __pos)
1004 __pos += __n;
1005 else
1006 __pos = __sz;
Marshall Clow37025e12014-06-10 18:51:55 +00001007 const _CharT* __r = _VSTD::__find_end(
1008 __p, __p + __pos, __s, __s + __n, _Traits::eq,
1009 random_access_iterator_tag(), random_access_iterator_tag());
Marshall Clow360f3192014-06-02 02:22:49 +00001010 if (__n > 0 && __r == __p + __pos)
1011 return __npos;
1012 return static_cast<_SizeT>(__r - __p);
1013}
1014
Marshall Clow37025e12014-06-10 18:51:55 +00001015// __str_find_first_of
Marshall Clow360f3192014-06-02 02:22:49 +00001016template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
1017_SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow37025e12014-06-10 18:51:55 +00001018__str_find_first_of(const _CharT *__p, _SizeT __sz,
Marshall Clow360f3192014-06-02 02:22:49 +00001019 const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
Marshall Clowb671fc92013-12-09 16:00:28 +00001020{
1021 if (__pos >= __sz || __n == 0)
1022 return __npos;
Marshall Clow37025e12014-06-10 18:51:55 +00001023 const _CharT* __r = _VSTD::__find_first_of_ce
Marshall Clowb671fc92013-12-09 16:00:28 +00001024 (__p + __pos, __p + __sz, __s, __s + __n, _Traits::eq );
1025 if (__r == __p + __sz)
1026 return __npos;
1027 return static_cast<_SizeT>(__r - __p);
1028}
1029
Marshall Clow360f3192014-06-02 02:22:49 +00001030
Marshall Clow37025e12014-06-10 18:51:55 +00001031// __str_find_last_of
Marshall Clowb671fc92013-12-09 16:00:28 +00001032template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
Marshall Clow360f3192014-06-02 02:22:49 +00001033_SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow37025e12014-06-10 18:51:55 +00001034__str_find_last_of(const _CharT *__p, _SizeT __sz,
Marshall Clow360f3192014-06-02 02:22:49 +00001035 const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
Marshall Clowb671fc92013-12-09 16:00:28 +00001036 {
1037 if (__n != 0)
1038 {
1039 if (__pos < __sz)
1040 ++__pos;
1041 else
1042 __pos = __sz;
1043 for (const _CharT* __ps = __p + __pos; __ps != __p;)
1044 {
1045 const _CharT* __r = _Traits::find(__s, __n, *--__ps);
1046 if (__r)
1047 return static_cast<_SizeT>(__ps - __p);
1048 }
1049 }
1050 return __npos;
1051}
1052
1053
Marshall Clow37025e12014-06-10 18:51:55 +00001054// __str_find_first_not_of
Marshall Clowb671fc92013-12-09 16:00:28 +00001055template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
Marshall Clow360f3192014-06-02 02:22:49 +00001056_SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow37025e12014-06-10 18:51:55 +00001057__str_find_first_not_of(const _CharT *__p, _SizeT __sz,
Marshall Clow360f3192014-06-02 02:22:49 +00001058 const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
Marshall Clowb671fc92013-12-09 16:00:28 +00001059{
1060 if (__pos < __sz)
1061 {
1062 const _CharT* __pe = __p + __sz;
1063 for (const _CharT* __ps = __p + __pos; __ps != __pe; ++__ps)
1064 if (_Traits::find(__s, __n, *__ps) == 0)
1065 return static_cast<_SizeT>(__ps - __p);
1066 }
1067 return __npos;
1068}
1069
1070
1071template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
Marshall Clow360f3192014-06-02 02:22:49 +00001072_SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow37025e12014-06-10 18:51:55 +00001073__str_find_first_not_of(const _CharT *__p, _SizeT __sz,
Marshall Clow360f3192014-06-02 02:22:49 +00001074 _CharT __c, _SizeT __pos) _NOEXCEPT
Marshall Clowb671fc92013-12-09 16:00:28 +00001075{
1076 if (__pos < __sz)
1077 {
1078 const _CharT* __pe = __p + __sz;
1079 for (const _CharT* __ps = __p + __pos; __ps != __pe; ++__ps)
1080 if (!_Traits::eq(*__ps, __c))
1081 return static_cast<_SizeT>(__ps - __p);
1082 }
1083 return __npos;
1084}
1085
1086
Marshall Clow37025e12014-06-10 18:51:55 +00001087// __str_find_last_not_of
Marshall Clowb671fc92013-12-09 16:00:28 +00001088template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
Marshall Clow360f3192014-06-02 02:22:49 +00001089_SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow37025e12014-06-10 18:51:55 +00001090__str_find_last_not_of(const _CharT *__p, _SizeT __sz,
Marshall Clow360f3192014-06-02 02:22:49 +00001091 const _CharT* __s, _SizeT __pos, _SizeT __n) _NOEXCEPT
Marshall Clowb671fc92013-12-09 16:00:28 +00001092{
1093 if (__pos < __sz)
1094 ++__pos;
1095 else
1096 __pos = __sz;
1097 for (const _CharT* __ps = __p + __pos; __ps != __p;)
1098 if (_Traits::find(__s, __n, *--__ps) == 0)
1099 return static_cast<_SizeT>(__ps - __p);
1100 return __npos;
1101}
1102
1103
1104template<class _CharT, class _SizeT, class _Traits, _SizeT __npos>
Marshall Clow360f3192014-06-02 02:22:49 +00001105_SizeT _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow37025e12014-06-10 18:51:55 +00001106__str_find_last_not_of(const _CharT *__p, _SizeT __sz,
Marshall Clow360f3192014-06-02 02:22:49 +00001107 _CharT __c, _SizeT __pos) _NOEXCEPT
Marshall Clowb671fc92013-12-09 16:00:28 +00001108{
1109 if (__pos < __sz)
1110 ++__pos;
1111 else
1112 __pos = __sz;
1113 for (const _CharT* __ps = __p + __pos; __ps != __p;)
1114 if (!_Traits::eq(*--__ps, __c))
1115 return static_cast<_SizeT>(__ps - __p);
1116 return __npos;
1117}
1118
1119template<class _Ptr>
1120size_t _LIBCPP_INLINE_VISIBILITY __do_string_hash(_Ptr __p, _Ptr __e)
1121{
1122 typedef typename iterator_traits<_Ptr>::value_type value_type;
1123 return __murmur2_or_cityhash<size_t>()(__p, (__e-__p)*sizeof(value_type));
1124}
1125
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001126// basic_string
1127
1128template<class _CharT, class _Traits, class _Allocator>
1129basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00001130operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
1131 const basic_string<_CharT, _Traits, _Allocator>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001132
1133template<class _CharT, class _Traits, class _Allocator>
1134basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00001135operator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001136
1137template<class _CharT, class _Traits, class _Allocator>
1138basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00001139operator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001140
1141template<class _CharT, class _Traits, class _Allocator>
1142basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00001143operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001144
1145template<class _CharT, class _Traits, class _Allocator>
1146basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +00001147operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001148
1149template <bool>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001150class _LIBCPP_TYPE_VIS_ONLY __basic_string_common
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001151{
1152protected:
1153 void __throw_length_error() const;
1154 void __throw_out_of_range() const;
1155};
1156
1157template <bool __b>
1158void
1159__basic_string_common<__b>::__throw_length_error() const
1160{
1161#ifndef _LIBCPP_NO_EXCEPTIONS
1162 throw length_error("basic_string");
1163#else
1164 assert(!"basic_string length_error");
1165#endif
1166}
1167
1168template <bool __b>
1169void
1170__basic_string_common<__b>::__throw_out_of_range() const
1171{
1172#ifndef _LIBCPP_NO_EXCEPTIONS
1173 throw out_of_range("basic_string");
1174#else
1175 assert(!"basic_string out_of_range");
1176#endif
1177}
1178
Howard Hinnante9df0a52013-08-01 18:17:34 +00001179#ifdef _LIBCPP_MSVC
Howard Hinnant78b68282011-10-22 20:59:45 +00001180#pragma warning( push )
1181#pragma warning( disable: 4231 )
Howard Hinnante9df0a52013-08-01 18:17:34 +00001182#endif // _LIBCPP_MSVC
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001183_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_TYPE_VIS __basic_string_common<true>)
Howard Hinnante9df0a52013-08-01 18:17:34 +00001184#ifdef _LIBCPP_MSVC
Howard Hinnant78b68282011-10-22 20:59:45 +00001185#pragma warning( pop )
Howard Hinnante9df0a52013-08-01 18:17:34 +00001186#endif // _LIBCPP_MSVC
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001187
Howard Hinnant15467182013-04-30 21:44:48 +00001188#ifdef _LIBCPP_ALTERNATE_STRING_LAYOUT
1189
1190template <class _CharT, size_t = sizeof(_CharT)>
1191struct __padding
1192{
1193 unsigned char __xx[sizeof(_CharT)-1];
1194};
1195
1196template <class _CharT>
1197struct __padding<_CharT, 1>
1198{
1199};
1200
1201#endif // _LIBCPP_ALTERNATE_STRING_LAYOUT
1202
Howard Hinnant324bb032010-08-22 00:02:43 +00001203template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001204class _LIBCPP_TYPE_VIS_ONLY basic_string
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001205 : private __basic_string_common<true>
1206{
1207public:
1208 typedef basic_string __self;
1209 typedef _Traits traits_type;
1210 typedef typename traits_type::char_type value_type;
1211 typedef _Allocator allocator_type;
Howard Hinnante32b5e22010-11-17 17:55:08 +00001212 typedef allocator_traits<allocator_type> __alloc_traits;
1213 typedef typename __alloc_traits::size_type size_type;
1214 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001215 typedef value_type& reference;
1216 typedef const value_type& const_reference;
Howard Hinnante32b5e22010-11-17 17:55:08 +00001217 typedef typename __alloc_traits::pointer pointer;
1218 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001219
Howard Hinnant499cea12013-08-23 17:37:05 +00001220 static_assert(is_pod<value_type>::value, "Character type of basic_string must be a POD");
1221 static_assert((is_same<_CharT, value_type>::value),
1222 "traits_type::char_type must be the same type as CharT");
1223 static_assert((is_same<typename allocator_type::value_type, value_type>::value),
1224 "Allocator::value_type must be same type as value_type");
1225#if defined(_LIBCPP_RAW_ITERATORS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001226 typedef pointer iterator;
1227 typedef const_pointer const_iterator;
Howard Hinnant324bb032010-08-22 00:02:43 +00001228#else // defined(_LIBCPP_RAW_ITERATORS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001229 typedef __wrap_iter<pointer> iterator;
1230 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnant324bb032010-08-22 00:02:43 +00001231#endif // defined(_LIBCPP_RAW_ITERATORS)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001232 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
1233 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001234
1235private:
Howard Hinnant15467182013-04-30 21:44:48 +00001236
1237#ifdef _LIBCPP_ALTERNATE_STRING_LAYOUT
1238
1239 struct __long
1240 {
1241 pointer __data_;
1242 size_type __size_;
1243 size_type __cap_;
1244 };
1245
1246#if _LIBCPP_BIG_ENDIAN
1247 enum {__short_mask = 0x01};
1248 enum {__long_mask = 0x1ul};
1249#else // _LIBCPP_BIG_ENDIAN
1250 enum {__short_mask = 0x80};
1251 enum {__long_mask = ~(size_type(~0) >> 1)};
1252#endif // _LIBCPP_BIG_ENDIAN
1253
1254 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
1255 (sizeof(__long) - 1)/sizeof(value_type) : 2};
1256
1257 struct __short
1258 {
1259 value_type __data_[__min_cap];
1260 struct
1261 : __padding<value_type>
1262 {
1263 unsigned char __size_;
1264 };
1265 };
1266
1267#else
1268
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001269 struct __long
1270 {
1271 size_type __cap_;
1272 size_type __size_;
1273 pointer __data_;
1274 };
1275
1276#if _LIBCPP_BIG_ENDIAN
1277 enum {__short_mask = 0x80};
1278 enum {__long_mask = ~(size_type(~0) >> 1)};
Howard Hinnant324bb032010-08-22 00:02:43 +00001279#else // _LIBCPP_BIG_ENDIAN
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001280 enum {__short_mask = 0x01};
Howard Hinnantec3773c2011-12-01 20:21:04 +00001281 enum {__long_mask = 0x1ul};
Howard Hinnant324bb032010-08-22 00:02:43 +00001282#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001283
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001284 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
1285 (sizeof(__long) - 1)/sizeof(value_type) : 2};
1286
1287 struct __short
1288 {
1289 union
1290 {
1291 unsigned char __size_;
Howard Hinnant9c0df142012-10-30 19:06:59 +00001292 value_type __lx;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001293 };
1294 value_type __data_[__min_cap];
1295 };
1296
Howard Hinnant15467182013-04-30 21:44:48 +00001297#endif // _LIBCPP_ALTERNATE_STRING_LAYOUT
1298
Howard Hinnant499cea12013-08-23 17:37:05 +00001299 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001300
Howard Hinnant499cea12013-08-23 17:37:05 +00001301 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001302
1303 struct __raw
1304 {
1305 size_type __words[__n_words];
1306 };
1307
1308 struct __rep
1309 {
1310 union
1311 {
1312 __long __l;
1313 __short __s;
1314 __raw __r;
1315 };
1316 };
1317
1318 __compressed_pair<__rep, allocator_type> __r_;
1319
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001320public:
1321 static const size_type npos = -1;
1322
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001323 _LIBCPP_INLINE_VISIBILITY basic_string()
1324 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001325 _LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001326 basic_string(const basic_string& __str);
1327 basic_string(const basic_string& __str, const allocator_type& __a);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001328#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant9f193f22011-01-26 00:06:59 +00001329 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001330 basic_string(basic_string&& __str)
1331 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnant9f193f22011-01-26 00:06:59 +00001332 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001333 basic_string(basic_string&& __str, const allocator_type& __a);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001334#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001335 _LIBCPP_INLINE_VISIBILITY basic_string(const value_type* __s);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001336 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001337 basic_string(const value_type* __s, const allocator_type& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001338 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001339 basic_string(const value_type* __s, size_type __n);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001340 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001341 basic_string(const value_type* __s, size_type __n, const allocator_type& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001342 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001343 basic_string(size_type __n, value_type __c);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001344 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001345 basic_string(size_type __n, value_type __c, const allocator_type& __a);
1346 basic_string(const basic_string& __str, size_type __pos, size_type __n = npos,
1347 const allocator_type& __a = allocator_type());
1348 template<class _InputIterator>
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001349 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001350 basic_string(_InputIterator __first, _InputIterator __last);
1351 template<class _InputIterator>
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001352 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001353 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +00001354#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001355 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001356 basic_string(initializer_list<value_type> __il);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001357 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001358 basic_string(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +00001359#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001360
1361 ~basic_string();
1362
Howard Hinnante32b5e22010-11-17 17:55:08 +00001363 basic_string& operator=(const basic_string& __str);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001364#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001365 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001366 basic_string& operator=(basic_string&& __str)
1367 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
1368 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001369#endif
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001370 _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const value_type* __s) {return assign(__s);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001371 basic_string& operator=(value_type __c);
Howard Hinnante3e32912011-08-12 21:56:02 +00001372#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant8d7a9552010-09-23 17:31:07 +00001373 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001374 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnante3e32912011-08-12 21:56:02 +00001375#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001376
Howard Hinnant499cea12013-08-23 17:37:05 +00001377#if _LIBCPP_DEBUG_LEVEL >= 2
1378 _LIBCPP_INLINE_VISIBILITY
1379 iterator begin() _NOEXCEPT
1380 {return iterator(this, __get_pointer());}
1381 _LIBCPP_INLINE_VISIBILITY
1382 const_iterator begin() const _NOEXCEPT
1383 {return const_iterator(this, __get_pointer());}
1384 _LIBCPP_INLINE_VISIBILITY
1385 iterator end() _NOEXCEPT
1386 {return iterator(this, __get_pointer() + size());}
1387 _LIBCPP_INLINE_VISIBILITY
1388 const_iterator end() const _NOEXCEPT
1389 {return const_iterator(this, __get_pointer() + size());}
1390#else
Howard Hinnanta6119a82011-05-29 19:57:12 +00001391 _LIBCPP_INLINE_VISIBILITY
1392 iterator begin() _NOEXCEPT
1393 {return iterator(__get_pointer());}
1394 _LIBCPP_INLINE_VISIBILITY
1395 const_iterator begin() const _NOEXCEPT
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001396 {return const_iterator(__get_pointer());}
Howard Hinnanta6119a82011-05-29 19:57:12 +00001397 _LIBCPP_INLINE_VISIBILITY
1398 iterator end() _NOEXCEPT
1399 {return iterator(__get_pointer() + size());}
1400 _LIBCPP_INLINE_VISIBILITY
1401 const_iterator end() const _NOEXCEPT
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001402 {return const_iterator(__get_pointer() + size());}
Howard Hinnant499cea12013-08-23 17:37:05 +00001403#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnanta6119a82011-05-29 19:57:12 +00001404 _LIBCPP_INLINE_VISIBILITY
1405 reverse_iterator rbegin() _NOEXCEPT
1406 {return reverse_iterator(end());}
1407 _LIBCPP_INLINE_VISIBILITY
1408 const_reverse_iterator rbegin() const _NOEXCEPT
1409 {return const_reverse_iterator(end());}
1410 _LIBCPP_INLINE_VISIBILITY
1411 reverse_iterator rend() _NOEXCEPT
1412 {return reverse_iterator(begin());}
1413 _LIBCPP_INLINE_VISIBILITY
1414 const_reverse_iterator rend() const _NOEXCEPT
1415 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001416
Howard Hinnanta6119a82011-05-29 19:57:12 +00001417 _LIBCPP_INLINE_VISIBILITY
1418 const_iterator cbegin() const _NOEXCEPT
1419 {return begin();}
1420 _LIBCPP_INLINE_VISIBILITY
1421 const_iterator cend() const _NOEXCEPT
1422 {return end();}
1423 _LIBCPP_INLINE_VISIBILITY
1424 const_reverse_iterator crbegin() const _NOEXCEPT
1425 {return rbegin();}
1426 _LIBCPP_INLINE_VISIBILITY
1427 const_reverse_iterator crend() const _NOEXCEPT
1428 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001429
Howard Hinnanta6119a82011-05-29 19:57:12 +00001430 _LIBCPP_INLINE_VISIBILITY size_type size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001431 {return __is_long() ? __get_long_size() : __get_short_size();}
Howard Hinnanta6119a82011-05-29 19:57:12 +00001432 _LIBCPP_INLINE_VISIBILITY size_type length() const _NOEXCEPT {return size();}
1433 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT;
1434 _LIBCPP_INLINE_VISIBILITY size_type capacity() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001435 {return (__is_long() ? __get_long_cap() : __min_cap) - 1;}
1436
1437 void resize(size_type __n, value_type __c);
1438 _LIBCPP_INLINE_VISIBILITY void resize(size_type __n) {resize(__n, value_type());}
1439
1440 void reserve(size_type res_arg = 0);
Howard Hinnant8d7a9552010-09-23 17:31:07 +00001441 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001442 void shrink_to_fit() _NOEXCEPT {reserve();}
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001443 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001444 void clear() _NOEXCEPT;
1445 _LIBCPP_INLINE_VISIBILITY bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001446
1447 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __pos) const;
1448 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __pos);
1449
1450 const_reference at(size_type __n) const;
1451 reference at(size_type __n);
1452
1453 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const basic_string& __str) {return append(__str);}
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001454 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const value_type* __s) {return append(__s);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001455 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(value_type __c) {push_back(__c); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02 +00001456#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001457 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(initializer_list<value_type> __il) {return append(__il);}
Howard Hinnante3e32912011-08-12 21:56:02 +00001458#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001459
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001460 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001461 basic_string& append(const basic_string& __str);
Marshall Clowa93b5e22014-03-04 19:17:19 +00001462 basic_string& append(const basic_string& __str, size_type __pos, size_type __n=npos);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001463 basic_string& append(const value_type* __s, size_type __n);
1464 basic_string& append(const value_type* __s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001465 basic_string& append(size_type __n, value_type __c);
1466 template<class _InputIterator>
1467 typename enable_if
1468 <
1469 __is_input_iterator <_InputIterator>::value &&
1470 !__is_forward_iterator<_InputIterator>::value,
1471 basic_string&
1472 >::type
1473 append(_InputIterator __first, _InputIterator __last);
1474 template<class _ForwardIterator>
1475 typename enable_if
1476 <
1477 __is_forward_iterator<_ForwardIterator>::value,
1478 basic_string&
1479 >::type
1480 append(_ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:02 +00001481#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant8d7a9552010-09-23 17:31:07 +00001482 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001483 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Howard Hinnante3e32912011-08-12 21:56:02 +00001484#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001485
1486 void push_back(value_type __c);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001487 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001488 void pop_back();
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001489 _LIBCPP_INLINE_VISIBILITY reference front();
1490 _LIBCPP_INLINE_VISIBILITY const_reference front() const;
1491 _LIBCPP_INLINE_VISIBILITY reference back();
1492 _LIBCPP_INLINE_VISIBILITY const_reference back() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001493
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001494 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001495 basic_string& assign(const basic_string& __str);
Howard Hinnanta6119a82011-05-29 19:57:12 +00001496#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1497 _LIBCPP_INLINE_VISIBILITY
1498 basic_string& assign(basic_string&& str)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001499 {*this = _VSTD::move(str); return *this;}
Howard Hinnanta6119a82011-05-29 19:57:12 +00001500#endif
Marshall Clowa93b5e22014-03-04 19:17:19 +00001501 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n=npos);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001502 basic_string& assign(const value_type* __s, size_type __n);
1503 basic_string& assign(const value_type* __s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001504 basic_string& assign(size_type __n, value_type __c);
1505 template<class _InputIterator>
1506 typename enable_if
1507 <
1508 __is_input_iterator <_InputIterator>::value &&
1509 !__is_forward_iterator<_InputIterator>::value,
1510 basic_string&
1511 >::type
1512 assign(_InputIterator __first, _InputIterator __last);
1513 template<class _ForwardIterator>
1514 typename enable_if
1515 <
1516 __is_forward_iterator<_ForwardIterator>::value,
1517 basic_string&
1518 >::type
1519 assign(_ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:02 +00001520#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant8d7a9552010-09-23 17:31:07 +00001521 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001522 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnante3e32912011-08-12 21:56:02 +00001523#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001524
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001525 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001526 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clowa93b5e22014-03-04 19:17:19 +00001527 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001528 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1529 basic_string& insert(size_type __pos, const value_type* __s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001530 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1531 iterator insert(const_iterator __pos, value_type __c);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001532 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001533 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1534 template<class _InputIterator>
1535 typename enable_if
1536 <
1537 __is_input_iterator <_InputIterator>::value &&
1538 !__is_forward_iterator<_InputIterator>::value,
1539 iterator
1540 >::type
1541 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1542 template<class _ForwardIterator>
1543 typename enable_if
1544 <
1545 __is_forward_iterator<_ForwardIterator>::value,
1546 iterator
1547 >::type
1548 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:02 +00001549#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant8d7a9552010-09-23 17:31:07 +00001550 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001551 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1552 {return insert(__pos, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00001553#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001554
1555 basic_string& erase(size_type __pos = 0, size_type __n = npos);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001556 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001557 iterator erase(const_iterator __pos);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001558 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001559 iterator erase(const_iterator __first, const_iterator __last);
1560
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001561 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001562 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
Marshall Clowa93b5e22014-03-04 19:17:19 +00001563 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2=npos);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001564 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
1565 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001566 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001567 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7b2cb482010-11-17 21:11:40 +00001568 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001569 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001570 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001571 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001572 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001573 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7b2cb482010-11-17 21:11:40 +00001574 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001575 template<class _InputIterator>
1576 typename enable_if
1577 <
1578 __is_input_iterator<_InputIterator>::value,
1579 basic_string&
1580 >::type
Howard Hinnant7b2cb482010-11-17 21:11:40 +00001581 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Howard Hinnante3e32912011-08-12 21:56:02 +00001582#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant8d7a9552010-09-23 17:31:07 +00001583 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7b2cb482010-11-17 21:11:40 +00001584 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001585 {return replace(__i1, __i2, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00001586#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001587
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001588 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001589 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001590 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1591
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001592 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001593 void swap(basic_string& __str)
1594 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
1595 __is_nothrow_swappable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001596
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001597 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001598 const value_type* c_str() const _NOEXCEPT {return data();}
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001599 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001600 const value_type* data() const _NOEXCEPT {return _VSTD::__to_raw_pointer(__get_pointer());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001601
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001602 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001603 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001604
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001605 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001606 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001607 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001608 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001609 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnanta6119a82011-05-29 19:57:12 +00001610 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001611
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001612 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001613 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001614 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001615 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001616 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnanta6119a82011-05-29 19:57:12 +00001617 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001618
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001619 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001620 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001621 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001622 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001623 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001624 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001625 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001626
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001627 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001628 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001629 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001630 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001631 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001632 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001633 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001634
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001635 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001636 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001637 size_type find_first_not_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnanta6119a82011-05-29 19:57:12 +00001638 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001639 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnanta6119a82011-05-29 19:57:12 +00001640 _LIBCPP_INLINE_VISIBILITY
1641 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1642
1643 _LIBCPP_INLINE_VISIBILITY
1644 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001645 size_type find_last_not_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnanta6119a82011-05-29 19:57:12 +00001646 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001647 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnanta6119a82011-05-29 19:57:12 +00001648 _LIBCPP_INLINE_VISIBILITY
1649 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1650
1651 _LIBCPP_INLINE_VISIBILITY
1652 int compare(const basic_string& __str) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001653 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001654 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Marshall Clowa93b5e22014-03-04 19:17:19 +00001655 int compare(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2=npos) const;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001656 int compare(const value_type* __s) const _NOEXCEPT;
1657 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1658 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001659
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001660 _LIBCPP_INLINE_VISIBILITY bool __invariants() const;
Howard Hinnant08dd2532013-04-22 23:55:13 +00001661
1662 _LIBCPP_INLINE_VISIBILITY
1663 bool __is_long() const _NOEXCEPT
1664 {return bool(__r_.first().__s.__size_ & __short_mask);}
1665
Howard Hinnant499cea12013-08-23 17:37:05 +00001666#if _LIBCPP_DEBUG_LEVEL >= 2
1667
1668 bool __dereferenceable(const const_iterator* __i) const;
1669 bool __decrementable(const const_iterator* __i) const;
1670 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1671 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1672
1673#endif // _LIBCPP_DEBUG_LEVEL >= 2
1674
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001675private:
Howard Hinnanta6119a82011-05-29 19:57:12 +00001676 _LIBCPP_INLINE_VISIBILITY
1677 allocator_type& __alloc() _NOEXCEPT
1678 {return __r_.second();}
1679 _LIBCPP_INLINE_VISIBILITY
1680 const allocator_type& __alloc() const _NOEXCEPT
1681 {return __r_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001682
Howard Hinnant15467182013-04-30 21:44:48 +00001683#ifdef _LIBCPP_ALTERNATE_STRING_LAYOUT
1684
Howard Hinnanta6119a82011-05-29 19:57:12 +00001685 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001686 void __set_short_size(size_type __s) _NOEXCEPT
Howard Hinnant15467182013-04-30 21:44:48 +00001687# if _LIBCPP_BIG_ENDIAN
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001688 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
Howard Hinnant15467182013-04-30 21:44:48 +00001689# else
1690 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1691# endif
1692
Howard Hinnanta6119a82011-05-29 19:57:12 +00001693 _LIBCPP_INLINE_VISIBILITY
1694 size_type __get_short_size() const _NOEXCEPT
Howard Hinnant15467182013-04-30 21:44:48 +00001695# if _LIBCPP_BIG_ENDIAN
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001696 {return __r_.first().__s.__size_ >> 1;}
Howard Hinnant15467182013-04-30 21:44:48 +00001697# else
1698 {return __r_.first().__s.__size_;}
1699# endif
1700
1701#else // _LIBCPP_ALTERNATE_STRING_LAYOUT
1702
1703 _LIBCPP_INLINE_VISIBILITY
1704 void __set_short_size(size_type __s) _NOEXCEPT
1705# if _LIBCPP_BIG_ENDIAN
1706 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1707# else
1708 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
1709# endif
1710
1711 _LIBCPP_INLINE_VISIBILITY
1712 size_type __get_short_size() const _NOEXCEPT
1713# if _LIBCPP_BIG_ENDIAN
1714 {return __r_.first().__s.__size_;}
1715# else
1716 {return __r_.first().__s.__size_ >> 1;}
1717# endif
1718
1719#endif // _LIBCPP_ALTERNATE_STRING_LAYOUT
1720
Howard Hinnanta6119a82011-05-29 19:57:12 +00001721 _LIBCPP_INLINE_VISIBILITY
1722 void __set_long_size(size_type __s) _NOEXCEPT
1723 {__r_.first().__l.__size_ = __s;}
1724 _LIBCPP_INLINE_VISIBILITY
1725 size_type __get_long_size() const _NOEXCEPT
1726 {return __r_.first().__l.__size_;}
1727 _LIBCPP_INLINE_VISIBILITY
1728 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001729 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1730
Howard Hinnanta6119a82011-05-29 19:57:12 +00001731 _LIBCPP_INLINE_VISIBILITY
1732 void __set_long_cap(size_type __s) _NOEXCEPT
1733 {__r_.first().__l.__cap_ = __long_mask | __s;}
1734 _LIBCPP_INLINE_VISIBILITY
1735 size_type __get_long_cap() const _NOEXCEPT
Howard Hinnantec3773c2011-12-01 20:21:04 +00001736 {return __r_.first().__l.__cap_ & size_type(~__long_mask);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001737
Howard Hinnanta6119a82011-05-29 19:57:12 +00001738 _LIBCPP_INLINE_VISIBILITY
1739 void __set_long_pointer(pointer __p) _NOEXCEPT
1740 {__r_.first().__l.__data_ = __p;}
1741 _LIBCPP_INLINE_VISIBILITY
1742 pointer __get_long_pointer() _NOEXCEPT
1743 {return __r_.first().__l.__data_;}
1744 _LIBCPP_INLINE_VISIBILITY
1745 const_pointer __get_long_pointer() const _NOEXCEPT
1746 {return __r_.first().__l.__data_;}
1747 _LIBCPP_INLINE_VISIBILITY
1748 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001749 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnanta6119a82011-05-29 19:57:12 +00001750 _LIBCPP_INLINE_VISIBILITY
1751 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001752 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnanta6119a82011-05-29 19:57:12 +00001753 _LIBCPP_INLINE_VISIBILITY
1754 pointer __get_pointer() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001755 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Howard Hinnanta6119a82011-05-29 19:57:12 +00001756 _LIBCPP_INLINE_VISIBILITY
1757 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001758 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1759
Howard Hinnanta6119a82011-05-29 19:57:12 +00001760 _LIBCPP_INLINE_VISIBILITY
1761 void __zero() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001762 {
1763 size_type (&__a)[__n_words] = __r_.first().__r.__words;
1764 for (unsigned __i = 0; __i < __n_words; ++__i)
1765 __a[__i] = 0;
1766 }
1767
1768 template <size_type __a> static
Howard Hinnanta6119a82011-05-29 19:57:12 +00001769 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7f764502013-08-14 18:00:20 +00001770 size_type __align_it(size_type __s) _NOEXCEPT
Howard Hinnanta6119a82011-05-29 19:57:12 +00001771 {return __s + (__a-1) & ~(__a-1);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001772 enum {__alignment = 16};
Howard Hinnanta6119a82011-05-29 19:57:12 +00001773 static _LIBCPP_INLINE_VISIBILITY
1774 size_type __recommend(size_type __s) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001775 {return (__s < __min_cap ? __min_cap :
Howard Hinnant7f764502013-08-14 18:00:20 +00001776 __align_it<sizeof(value_type) < __alignment ?
Howard Hinnanta6119a82011-05-29 19:57:12 +00001777 __alignment/sizeof(value_type) : 1 > (__s+1)) - 1;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001778
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001779 void __init(const value_type* __s, size_type __sz, size_type __reserve);
1780 void __init(const value_type* __s, size_type __sz);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001781 void __init(size_type __n, value_type __c);
Howard Hinnant324bb032010-08-22 00:02:43 +00001782
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001783 template <class _InputIterator>
1784 typename enable_if
1785 <
1786 __is_input_iterator <_InputIterator>::value &&
1787 !__is_forward_iterator<_InputIterator>::value,
1788 void
1789 >::type
1790 __init(_InputIterator __first, _InputIterator __last);
1791
1792 template <class _ForwardIterator>
1793 typename enable_if
1794 <
1795 __is_forward_iterator<_ForwardIterator>::value,
1796 void
1797 >::type
1798 __init(_ForwardIterator __first, _ForwardIterator __last);
1799
1800 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant324bb032010-08-22 00:02:43 +00001801 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001802 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1803 size_type __n_copy, size_type __n_del,
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001804 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001805
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001806 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001807 void __erase_to_end(size_type __pos);
1808
Howard Hinnante32b5e22010-11-17 17:55:08 +00001809 _LIBCPP_INLINE_VISIBILITY
1810 void __copy_assign_alloc(const basic_string& __str)
1811 {__copy_assign_alloc(__str, integral_constant<bool,
1812 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1813
1814 _LIBCPP_INLINE_VISIBILITY
1815 void __copy_assign_alloc(const basic_string& __str, true_type)
1816 {
1817 if (__alloc() != __str.__alloc())
1818 {
1819 clear();
1820 shrink_to_fit();
1821 }
1822 __alloc() = __str.__alloc();
1823 }
1824
1825 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00001826 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnante32b5e22010-11-17 17:55:08 +00001827 {}
1828
1829#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001830 _LIBCPP_INLINE_VISIBILITY
Howard Hinnante32b5e22010-11-17 17:55:08 +00001831 void __move_assign(basic_string& __str, false_type);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001832 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001833 void __move_assign(basic_string& __str, true_type)
1834 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001835#endif
1836
1837 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00001838 void
Howard Hinnant9cbee432011-09-02 20:42:31 +00001839 __move_assign_alloc(basic_string& __str)
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00001840 _NOEXCEPT_(
1841 !__alloc_traits::propagate_on_container_move_assignment::value ||
1842 is_nothrow_move_assignable<allocator_type>::value)
1843 {__move_assign_alloc(__str, integral_constant<bool,
1844 __alloc_traits::propagate_on_container_move_assignment::value>());}
1845
1846 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +00001847 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00001848 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1849 {
1850 __alloc() = _VSTD::move(__c.__alloc());
1851 }
1852
1853 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00001854 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00001855 _NOEXCEPT
1856 {}
1857
1858 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001859 static void __swap_alloc(allocator_type& __x, allocator_type& __y)
1860 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
1861 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnante32b5e22010-11-17 17:55:08 +00001862 {__swap_alloc(__x, __y, integral_constant<bool,
1863 __alloc_traits::propagate_on_container_swap::value>());}
1864
1865 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001866 static void __swap_alloc(allocator_type& __x, allocator_type& __y, true_type)
1867 _NOEXCEPT_(__is_nothrow_swappable<allocator_type>::value)
Howard Hinnante32b5e22010-11-17 17:55:08 +00001868 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001869 using _VSTD::swap;
Howard Hinnante32b5e22010-11-17 17:55:08 +00001870 swap(__x, __y);
1871 }
1872 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00001873 static void __swap_alloc(allocator_type&, allocator_type&, false_type) _NOEXCEPT
Howard Hinnante32b5e22010-11-17 17:55:08 +00001874 {}
1875
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001876 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
1877 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(size_type);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001878
1879 friend basic_string operator+<>(const basic_string&, const basic_string&);
1880 friend basic_string operator+<>(const value_type*, const basic_string&);
1881 friend basic_string operator+<>(value_type, const basic_string&);
1882 friend basic_string operator+<>(const basic_string&, const value_type*);
1883 friend basic_string operator+<>(const basic_string&, value_type);
1884};
1885
1886template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001887inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001888void
1889basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
1890{
Howard Hinnant499cea12013-08-23 17:37:05 +00001891#if _LIBCPP_DEBUG_LEVEL >= 2
1892 __get_db()->__invalidate_all(this);
1893#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001894}
1895
1896template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001897inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001898void
Howard Hinnantec3773c2011-12-01 20:21:04 +00001899basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type
Howard Hinnant499cea12013-08-23 17:37:05 +00001900#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantec3773c2011-12-01 20:21:04 +00001901 __pos
1902#endif
1903 )
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001904{
Howard Hinnant499cea12013-08-23 17:37:05 +00001905#if _LIBCPP_DEBUG_LEVEL >= 2
1906 __c_node* __c = __get_db()->__find_c_and_lock(this);
1907 if (__c)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001908 {
Howard Hinnant499cea12013-08-23 17:37:05 +00001909 const_pointer __new_last = __get_pointer() + __pos;
1910 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001911 {
Howard Hinnant499cea12013-08-23 17:37:05 +00001912 --__p;
1913 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1914 if (__i->base() > __new_last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001915 {
Howard Hinnant499cea12013-08-23 17:37:05 +00001916 (*__p)->__c_ = nullptr;
1917 if (--__c->end_ != __p)
1918 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001919 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001920 }
Howard Hinnant499cea12013-08-23 17:37:05 +00001921 __get_db()->unlock();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001922 }
Howard Hinnant499cea12013-08-23 17:37:05 +00001923#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001924}
1925
1926template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001927inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001928basic_string<_CharT, _Traits, _Allocator>::basic_string()
1929 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001930{
Howard Hinnant499cea12013-08-23 17:37:05 +00001931#if _LIBCPP_DEBUG_LEVEL >= 2
1932 __get_db()->__insert_c(this);
1933#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001934 __zero();
1935}
1936
1937template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001938inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001939basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
1940 : __r_(__a)
1941{
Howard Hinnant499cea12013-08-23 17:37:05 +00001942#if _LIBCPP_DEBUG_LEVEL >= 2
1943 __get_db()->__insert_c(this);
1944#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001945 __zero();
1946}
1947
1948template <class _CharT, class _Traits, class _Allocator>
1949void
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001950basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz, size_type __reserve)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001951{
1952 if (__reserve > max_size())
1953 this->__throw_length_error();
1954 pointer __p;
1955 if (__reserve < __min_cap)
1956 {
1957 __set_short_size(__sz);
1958 __p = __get_short_pointer();
1959 }
1960 else
1961 {
1962 size_type __cap = __recommend(__reserve);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001963 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001964 __set_long_pointer(__p);
1965 __set_long_cap(__cap+1);
1966 __set_long_size(__sz);
1967 }
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001968 traits_type::copy(_VSTD::__to_raw_pointer(__p), __s, __sz);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001969 traits_type::assign(__p[__sz], value_type());
1970}
1971
1972template <class _CharT, class _Traits, class _Allocator>
1973void
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001974basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001975{
1976 if (__sz > max_size())
1977 this->__throw_length_error();
1978 pointer __p;
1979 if (__sz < __min_cap)
1980 {
1981 __set_short_size(__sz);
1982 __p = __get_short_pointer();
1983 }
1984 else
1985 {
1986 size_type __cap = __recommend(__sz);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001987 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001988 __set_long_pointer(__p);
1989 __set_long_cap(__cap+1);
1990 __set_long_size(__sz);
1991 }
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001992 traits_type::copy(_VSTD::__to_raw_pointer(__p), __s, __sz);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001993 traits_type::assign(__p[__sz], value_type());
1994}
1995
1996template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001997inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001998basic_string<_CharT, _Traits, _Allocator>::basic_string(const value_type* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001999{
Howard Hinnant499cea12013-08-23 17:37:05 +00002000 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002001 __init(__s, traits_type::length(__s));
Howard Hinnant499cea12013-08-23 17:37:05 +00002002#if _LIBCPP_DEBUG_LEVEL >= 2
2003 __get_db()->__insert_c(this);
2004#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002005}
2006
2007template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002008inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002009basic_string<_CharT, _Traits, _Allocator>::basic_string(const value_type* __s, const allocator_type& __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002010 : __r_(__a)
2011{
Howard Hinnant499cea12013-08-23 17:37:05 +00002012 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002013 __init(__s, traits_type::length(__s));
Howard Hinnant499cea12013-08-23 17:37:05 +00002014#if _LIBCPP_DEBUG_LEVEL >= 2
2015 __get_db()->__insert_c(this);
2016#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002017}
2018
2019template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002020inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002021basic_string<_CharT, _Traits, _Allocator>::basic_string(const value_type* __s, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002022{
Howard Hinnant499cea12013-08-23 17:37:05 +00002023 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002024 __init(__s, __n);
Howard Hinnant499cea12013-08-23 17:37:05 +00002025#if _LIBCPP_DEBUG_LEVEL >= 2
2026 __get_db()->__insert_c(this);
2027#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002028}
2029
2030template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002031inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002032basic_string<_CharT, _Traits, _Allocator>::basic_string(const value_type* __s, size_type __n, const allocator_type& __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002033 : __r_(__a)
2034{
Howard Hinnant499cea12013-08-23 17:37:05 +00002035 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002036 __init(__s, __n);
Howard Hinnant499cea12013-08-23 17:37:05 +00002037#if _LIBCPP_DEBUG_LEVEL >= 2
2038 __get_db()->__insert_c(this);
2039#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002040}
2041
2042template <class _CharT, class _Traits, class _Allocator>
2043basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Howard Hinnante32b5e22010-11-17 17:55:08 +00002044 : __r_(__alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002045{
2046 if (!__str.__is_long())
2047 __r_.first().__r = __str.__r_.first().__r;
2048 else
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002049 __init(_VSTD::__to_raw_pointer(__str.__get_long_pointer()), __str.__get_long_size());
Howard Hinnant499cea12013-08-23 17:37:05 +00002050#if _LIBCPP_DEBUG_LEVEL >= 2
2051 __get_db()->__insert_c(this);
2052#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002053}
2054
2055template <class _CharT, class _Traits, class _Allocator>
2056basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, const allocator_type& __a)
2057 : __r_(__a)
2058{
2059 if (!__str.__is_long())
2060 __r_.first().__r = __str.__r_.first().__r;
2061 else
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002062 __init(_VSTD::__to_raw_pointer(__str.__get_long_pointer()), __str.__get_long_size());
Howard Hinnant499cea12013-08-23 17:37:05 +00002063#if _LIBCPP_DEBUG_LEVEL >= 2
2064 __get_db()->__insert_c(this);
2065#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002066}
2067
Howard Hinnant73d21a42010-09-04 23:28:19 +00002068#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002069
2070template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002071inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00002072basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
2073 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00002074 : __r_(_VSTD::move(__str.__r_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002075{
2076 __str.__zero();
Howard Hinnant499cea12013-08-23 17:37:05 +00002077#if _LIBCPP_DEBUG_LEVEL >= 2
2078 __get_db()->__insert_c(this);
2079 if (__is_long())
2080 __get_db()->swap(this, &__str);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002081#endif
2082}
2083
2084template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002085inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002086basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Howard Hinnante32b5e22010-11-17 17:55:08 +00002087 : __r_(__a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002088{
Marshall Clowd5549cc2014-07-17 15:32:20 +00002089 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002090 __init(_VSTD::__to_raw_pointer(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd5549cc2014-07-17 15:32:20 +00002091 else
2092 {
2093 __r_.first().__r = __str.__r_.first().__r;
2094 __str.__zero();
2095 }
Howard Hinnant499cea12013-08-23 17:37:05 +00002096#if _LIBCPP_DEBUG_LEVEL >= 2
2097 __get_db()->__insert_c(this);
2098 if (__is_long())
2099 __get_db()->swap(this, &__str);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002100#endif
2101}
2102
Howard Hinnant73d21a42010-09-04 23:28:19 +00002103#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002104
2105template <class _CharT, class _Traits, class _Allocator>
2106void
2107basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
2108{
2109 if (__n > max_size())
2110 this->__throw_length_error();
2111 pointer __p;
2112 if (__n < __min_cap)
2113 {
2114 __set_short_size(__n);
2115 __p = __get_short_pointer();
2116 }
2117 else
2118 {
2119 size_type __cap = __recommend(__n);
Howard Hinnante32b5e22010-11-17 17:55:08 +00002120 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002121 __set_long_pointer(__p);
2122 __set_long_cap(__cap+1);
2123 __set_long_size(__n);
2124 }
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002125 traits_type::assign(_VSTD::__to_raw_pointer(__p), __n, __c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002126 traits_type::assign(__p[__n], value_type());
2127}
2128
2129template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002130inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002131basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, value_type __c)
2132{
2133 __init(__n, __c);
Howard Hinnant499cea12013-08-23 17:37:05 +00002134#if _LIBCPP_DEBUG_LEVEL >= 2
2135 __get_db()->__insert_c(this);
2136#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002137}
2138
2139template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002140inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002141basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, value_type __c, const allocator_type& __a)
2142 : __r_(__a)
2143{
2144 __init(__n, __c);
Howard Hinnant499cea12013-08-23 17:37:05 +00002145#if _LIBCPP_DEBUG_LEVEL >= 2
2146 __get_db()->__insert_c(this);
2147#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002148}
2149
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002150template <class _CharT, class _Traits, class _Allocator>
2151basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos, size_type __n,
2152 const allocator_type& __a)
2153 : __r_(__a)
2154{
2155 size_type __str_sz = __str.size();
2156 if (__pos > __str_sz)
2157 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002158 __init(__str.data() + __pos, _VSTD::min(__n, __str_sz - __pos));
Howard Hinnant499cea12013-08-23 17:37:05 +00002159#if _LIBCPP_DEBUG_LEVEL >= 2
2160 __get_db()->__insert_c(this);
2161#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002162}
2163
2164template <class _CharT, class _Traits, class _Allocator>
2165template <class _InputIterator>
2166typename enable_if
2167<
2168 __is_input_iterator <_InputIterator>::value &&
2169 !__is_forward_iterator<_InputIterator>::value,
2170 void
2171>::type
2172basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
2173{
2174 __zero();
2175#ifndef _LIBCPP_NO_EXCEPTIONS
2176 try
2177 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002178#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002179 for (; __first != __last; ++__first)
2180 push_back(*__first);
2181#ifndef _LIBCPP_NO_EXCEPTIONS
2182 }
2183 catch (...)
2184 {
2185 if (__is_long())
Howard Hinnante32b5e22010-11-17 17:55:08 +00002186 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002187 throw;
2188 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002189#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002190}
2191
2192template <class _CharT, class _Traits, class _Allocator>
2193template <class _ForwardIterator>
2194typename enable_if
2195<
2196 __is_forward_iterator<_ForwardIterator>::value,
2197 void
2198>::type
2199basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
2200{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002201 size_type __sz = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002202 if (__sz > max_size())
2203 this->__throw_length_error();
2204 pointer __p;
2205 if (__sz < __min_cap)
2206 {
2207 __set_short_size(__sz);
2208 __p = __get_short_pointer();
2209 }
2210 else
2211 {
2212 size_type __cap = __recommend(__sz);
Howard Hinnante32b5e22010-11-17 17:55:08 +00002213 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002214 __set_long_pointer(__p);
2215 __set_long_cap(__cap+1);
2216 __set_long_size(__sz);
2217 }
Eric Fiselierb9919752014-10-27 19:28:20 +00002218 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002219 traits_type::assign(*__p, *__first);
2220 traits_type::assign(*__p, value_type());
2221}
2222
2223template <class _CharT, class _Traits, class _Allocator>
2224template<class _InputIterator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002225inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002226basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
2227{
2228 __init(__first, __last);
Howard Hinnant499cea12013-08-23 17:37:05 +00002229#if _LIBCPP_DEBUG_LEVEL >= 2
2230 __get_db()->__insert_c(this);
2231#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002232}
2233
2234template <class _CharT, class _Traits, class _Allocator>
2235template<class _InputIterator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002236inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002237basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
2238 const allocator_type& __a)
2239 : __r_(__a)
2240{
2241 __init(__first, __last);
Howard Hinnant499cea12013-08-23 17:37:05 +00002242#if _LIBCPP_DEBUG_LEVEL >= 2
2243 __get_db()->__insert_c(this);
2244#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002245}
2246
Howard Hinnante3e32912011-08-12 21:56:02 +00002247#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2248
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002249template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002250inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002251basic_string<_CharT, _Traits, _Allocator>::basic_string(initializer_list<value_type> __il)
2252{
2253 __init(__il.begin(), __il.end());
Howard Hinnant499cea12013-08-23 17:37:05 +00002254#if _LIBCPP_DEBUG_LEVEL >= 2
2255 __get_db()->__insert_c(this);
2256#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002257}
2258
2259template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002260inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002261basic_string<_CharT, _Traits, _Allocator>::basic_string(initializer_list<value_type> __il, const allocator_type& __a)
2262 : __r_(__a)
2263{
2264 __init(__il.begin(), __il.end());
Howard Hinnant499cea12013-08-23 17:37:05 +00002265#if _LIBCPP_DEBUG_LEVEL >= 2
2266 __get_db()->__insert_c(this);
2267#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002268}
2269
Howard Hinnante3e32912011-08-12 21:56:02 +00002270#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2271
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002272template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002273basic_string<_CharT, _Traits, _Allocator>::~basic_string()
2274{
Howard Hinnant499cea12013-08-23 17:37:05 +00002275#if _LIBCPP_DEBUG_LEVEL >= 2
2276 __get_db()->__erase_c(this);
2277#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002278 if (__is_long())
Howard Hinnante32b5e22010-11-17 17:55:08 +00002279 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002280}
2281
2282template <class _CharT, class _Traits, class _Allocator>
2283void
2284basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
2285 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002286 size_type __n_copy, size_type __n_del, size_type __n_add, const value_type* __p_new_stuff)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002287{
2288 size_type __ms = max_size();
2289 if (__delta_cap > __ms - __old_cap - 1)
2290 this->__throw_length_error();
2291 pointer __old_p = __get_pointer();
2292 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnant0949eed2011-06-30 21:18:19 +00002293 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002294 __ms - 1;
Howard Hinnante32b5e22010-11-17 17:55:08 +00002295 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002296 __invalidate_all_iterators();
2297 if (__n_copy != 0)
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002298 traits_type::copy(_VSTD::__to_raw_pointer(__p),
2299 _VSTD::__to_raw_pointer(__old_p), __n_copy);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002300 if (__n_add != 0)
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002301 traits_type::copy(_VSTD::__to_raw_pointer(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002302 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2303 if (__sec_cp_sz != 0)
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002304 traits_type::copy(_VSTD::__to_raw_pointer(__p) + __n_copy + __n_add,
2305 _VSTD::__to_raw_pointer(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002306 if (__old_cap+1 != __min_cap)
Howard Hinnante32b5e22010-11-17 17:55:08 +00002307 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002308 __set_long_pointer(__p);
2309 __set_long_cap(__cap+1);
2310 __old_sz = __n_copy + __n_add + __sec_cp_sz;
2311 __set_long_size(__old_sz);
2312 traits_type::assign(__p[__old_sz], value_type());
2313}
2314
2315template <class _CharT, class _Traits, class _Allocator>
2316void
2317basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2318 size_type __n_copy, size_type __n_del, size_type __n_add)
2319{
2320 size_type __ms = max_size();
Marshall Clowecc8d7b2013-11-06 14:24:38 +00002321 if (__delta_cap > __ms - __old_cap)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002322 this->__throw_length_error();
2323 pointer __old_p = __get_pointer();
2324 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnant0949eed2011-06-30 21:18:19 +00002325 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002326 __ms - 1;
Howard Hinnante32b5e22010-11-17 17:55:08 +00002327 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002328 __invalidate_all_iterators();
2329 if (__n_copy != 0)
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002330 traits_type::copy(_VSTD::__to_raw_pointer(__p),
2331 _VSTD::__to_raw_pointer(__old_p), __n_copy);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002332 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2333 if (__sec_cp_sz != 0)
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002334 traits_type::copy(_VSTD::__to_raw_pointer(__p) + __n_copy + __n_add,
2335 _VSTD::__to_raw_pointer(__old_p) + __n_copy + __n_del,
2336 __sec_cp_sz);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002337 if (__old_cap+1 != __min_cap)
Howard Hinnante32b5e22010-11-17 17:55:08 +00002338 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002339 __set_long_pointer(__p);
2340 __set_long_cap(__cap+1);
2341}
2342
2343// assign
2344
2345template <class _CharT, class _Traits, class _Allocator>
2346basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002347basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002348{
Alp Tokerec34c482014-05-15 11:27:39 +00002349 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002350 size_type __cap = capacity();
2351 if (__cap >= __n)
2352 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002353 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002354 traits_type::move(__p, __s, __n);
2355 traits_type::assign(__p[__n], value_type());
2356 __set_size(__n);
2357 __invalidate_iterators_past(__n);
2358 }
2359 else
2360 {
2361 size_type __sz = size();
2362 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
2363 }
2364 return *this;
2365}
2366
2367template <class _CharT, class _Traits, class _Allocator>
2368basic_string<_CharT, _Traits, _Allocator>&
2369basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2370{
2371 size_type __cap = capacity();
2372 if (__cap < __n)
2373 {
2374 size_type __sz = size();
2375 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2376 }
2377 else
2378 __invalidate_iterators_past(__n);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002379 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002380 traits_type::assign(__p, __n, __c);
2381 traits_type::assign(__p[__n], value_type());
2382 __set_size(__n);
2383 return *this;
2384}
2385
2386template <class _CharT, class _Traits, class _Allocator>
2387basic_string<_CharT, _Traits, _Allocator>&
2388basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2389{
2390 pointer __p;
2391 if (__is_long())
2392 {
2393 __p = __get_long_pointer();
2394 __set_long_size(1);
2395 }
2396 else
2397 {
2398 __p = __get_short_pointer();
2399 __set_short_size(1);
2400 }
2401 traits_type::assign(*__p, __c);
2402 traits_type::assign(*++__p, value_type());
2403 __invalidate_iterators_past(1);
2404 return *this;
2405}
2406
2407template <class _CharT, class _Traits, class _Allocator>
Howard Hinnante32b5e22010-11-17 17:55:08 +00002408basic_string<_CharT, _Traits, _Allocator>&
2409basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2410{
2411 if (this != &__str)
2412 {
2413 __copy_assign_alloc(__str);
2414 assign(__str);
2415 }
2416 return *this;
2417}
2418
2419#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2420
2421template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002422inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnante32b5e22010-11-17 17:55:08 +00002423void
2424basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
2425{
2426 if (__alloc() != __str.__alloc())
2427 assign(__str);
2428 else
2429 __move_assign(__str, true_type());
2430}
2431
2432template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002433inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnante32b5e22010-11-17 17:55:08 +00002434void
2435basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00002436 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnante32b5e22010-11-17 17:55:08 +00002437{
2438 clear();
2439 shrink_to_fit();
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00002440 __r_.first() = __str.__r_.first();
2441 __move_assign_alloc(__str);
Howard Hinnante32b5e22010-11-17 17:55:08 +00002442 __str.__zero();
2443}
2444
2445template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002446inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnante32b5e22010-11-17 17:55:08 +00002447basic_string<_CharT, _Traits, _Allocator>&
2448basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00002449 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
2450 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnante32b5e22010-11-17 17:55:08 +00002451{
2452 __move_assign(__str, integral_constant<bool,
2453 __alloc_traits::propagate_on_container_move_assignment::value>());
2454 return *this;
2455}
2456
2457#endif
2458
2459template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002460template<class _InputIterator>
2461typename enable_if
2462<
2463 __is_input_iterator <_InputIterator>::value &&
2464 !__is_forward_iterator<_InputIterator>::value,
2465 basic_string<_CharT, _Traits, _Allocator>&
2466>::type
2467basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2468{
2469 clear();
2470 for (; __first != __last; ++__first)
2471 push_back(*__first);
Argyrios Kyrtzidis1dc6f7a2012-10-13 02:03:45 +00002472 return *this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002473}
2474
2475template <class _CharT, class _Traits, class _Allocator>
2476template<class _ForwardIterator>
2477typename enable_if
2478<
2479 __is_forward_iterator<_ForwardIterator>::value,
2480 basic_string<_CharT, _Traits, _Allocator>&
2481>::type
2482basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2483{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002484 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002485 size_type __cap = capacity();
2486 if (__cap < __n)
2487 {
2488 size_type __sz = size();
2489 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2490 }
2491 else
2492 __invalidate_iterators_past(__n);
2493 pointer __p = __get_pointer();
2494 for (; __first != __last; ++__first, ++__p)
2495 traits_type::assign(*__p, *__first);
2496 traits_type::assign(*__p, value_type());
2497 __set_size(__n);
2498 return *this;
2499}
2500
2501template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002502inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002503basic_string<_CharT, _Traits, _Allocator>&
2504basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str)
2505{
2506 return assign(__str.data(), __str.size());
2507}
2508
2509template <class _CharT, class _Traits, class _Allocator>
2510basic_string<_CharT, _Traits, _Allocator>&
2511basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2512{
2513 size_type __sz = __str.size();
2514 if (__pos > __sz)
2515 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002516 return assign(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002517}
2518
2519template <class _CharT, class _Traits, class _Allocator>
2520basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002521basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002522{
Alp Tokerec34c482014-05-15 11:27:39 +00002523 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002524 return assign(__s, traits_type::length(__s));
2525}
2526
2527// append
2528
2529template <class _CharT, class _Traits, class _Allocator>
2530basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002531basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002532{
Alp Tokerec34c482014-05-15 11:27:39 +00002533 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002534 size_type __cap = capacity();
2535 size_type __sz = size();
2536 if (__cap - __sz >= __n)
2537 {
2538 if (__n)
2539 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002540 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002541 traits_type::copy(__p + __sz, __s, __n);
2542 __sz += __n;
2543 __set_size(__sz);
2544 traits_type::assign(__p[__sz], value_type());
2545 }
2546 }
2547 else
2548 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2549 return *this;
2550}
2551
2552template <class _CharT, class _Traits, class _Allocator>
2553basic_string<_CharT, _Traits, _Allocator>&
2554basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2555{
2556 if (__n)
2557 {
2558 size_type __cap = capacity();
2559 size_type __sz = size();
2560 if (__cap - __sz < __n)
2561 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2562 pointer __p = __get_pointer();
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002563 traits_type::assign(_VSTD::__to_raw_pointer(__p) + __sz, __n, __c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002564 __sz += __n;
2565 __set_size(__sz);
2566 traits_type::assign(__p[__sz], value_type());
2567 }
2568 return *this;
2569}
2570
2571template <class _CharT, class _Traits, class _Allocator>
2572void
2573basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2574{
Howard Hinnant15467182013-04-30 21:44:48 +00002575 bool __is_short = !__is_long();
2576 size_type __cap;
2577 size_type __sz;
2578 if (__is_short)
2579 {
2580 __cap = __min_cap - 1;
2581 __sz = __get_short_size();
2582 }
2583 else
2584 {
2585 __cap = __get_long_cap() - 1;
2586 __sz = __get_long_size();
2587 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002588 if (__sz == __cap)
Howard Hinnant15467182013-04-30 21:44:48 +00002589 {
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002590 __grow_by(__cap, 1, __sz, __sz, 0);
Howard Hinnant15467182013-04-30 21:44:48 +00002591 __is_short = !__is_long();
2592 }
2593 pointer __p;
2594 if (__is_short)
2595 {
2596 __p = __get_short_pointer() + __sz;
2597 __set_short_size(__sz+1);
2598 }
2599 else
2600 {
2601 __p = __get_long_pointer() + __sz;
2602 __set_long_size(__sz+1);
2603 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002604 traits_type::assign(*__p, __c);
2605 traits_type::assign(*++__p, value_type());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002606}
2607
2608template <class _CharT, class _Traits, class _Allocator>
2609template<class _InputIterator>
2610typename enable_if
2611<
2612 __is_input_iterator <_InputIterator>::value &&
2613 !__is_forward_iterator<_InputIterator>::value,
2614 basic_string<_CharT, _Traits, _Allocator>&
2615>::type
2616basic_string<_CharT, _Traits, _Allocator>::append(_InputIterator __first, _InputIterator __last)
2617{
2618 for (; __first != __last; ++__first)
2619 push_back(*__first);
2620 return *this;
2621}
2622
2623template <class _CharT, class _Traits, class _Allocator>
2624template<class _ForwardIterator>
2625typename enable_if
2626<
2627 __is_forward_iterator<_ForwardIterator>::value,
2628 basic_string<_CharT, _Traits, _Allocator>&
2629>::type
2630basic_string<_CharT, _Traits, _Allocator>::append(_ForwardIterator __first, _ForwardIterator __last)
2631{
2632 size_type __sz = size();
2633 size_type __cap = capacity();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002634 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002635 if (__n)
2636 {
2637 if (__cap - __sz < __n)
2638 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2639 pointer __p = __get_pointer() + __sz;
2640 for (; __first != __last; ++__p, ++__first)
2641 traits_type::assign(*__p, *__first);
2642 traits_type::assign(*__p, value_type());
2643 __set_size(__sz + __n);
2644 }
2645 return *this;
2646}
2647
2648template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002649inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002650basic_string<_CharT, _Traits, _Allocator>&
2651basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2652{
2653 return append(__str.data(), __str.size());
2654}
2655
2656template <class _CharT, class _Traits, class _Allocator>
2657basic_string<_CharT, _Traits, _Allocator>&
2658basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2659{
2660 size_type __sz = __str.size();
2661 if (__pos > __sz)
2662 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002663 return append(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002664}
2665
2666template <class _CharT, class _Traits, class _Allocator>
2667basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002668basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002669{
Alp Tokerec34c482014-05-15 11:27:39 +00002670 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002671 return append(__s, traits_type::length(__s));
2672}
2673
2674// insert
2675
2676template <class _CharT, class _Traits, class _Allocator>
2677basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002678basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002679{
Alp Tokerec34c482014-05-15 11:27:39 +00002680 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002681 size_type __sz = size();
2682 if (__pos > __sz)
2683 this->__throw_out_of_range();
2684 size_type __cap = capacity();
2685 if (__cap - __sz >= __n)
2686 {
2687 if (__n)
2688 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002689 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002690 size_type __n_move = __sz - __pos;
2691 if (__n_move != 0)
2692 {
2693 if (__p + __pos <= __s && __s < __p + __sz)
2694 __s += __n;
2695 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2696 }
2697 traits_type::move(__p + __pos, __s, __n);
2698 __sz += __n;
2699 __set_size(__sz);
2700 traits_type::assign(__p[__sz], value_type());
2701 }
2702 }
2703 else
2704 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2705 return *this;
2706}
2707
2708template <class _CharT, class _Traits, class _Allocator>
2709basic_string<_CharT, _Traits, _Allocator>&
2710basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2711{
2712 size_type __sz = size();
2713 if (__pos > __sz)
2714 this->__throw_out_of_range();
2715 if (__n)
2716 {
2717 size_type __cap = capacity();
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002718 value_type* __p;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002719 if (__cap - __sz >= __n)
2720 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002721 __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002722 size_type __n_move = __sz - __pos;
2723 if (__n_move != 0)
2724 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2725 }
2726 else
2727 {
2728 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002729 __p = _VSTD::__to_raw_pointer(__get_long_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002730 }
2731 traits_type::assign(__p + __pos, __n, __c);
2732 __sz += __n;
2733 __set_size(__sz);
2734 traits_type::assign(__p[__sz], value_type());
2735 }
2736 return *this;
2737}
2738
2739template <class _CharT, class _Traits, class _Allocator>
2740template<class _InputIterator>
2741typename enable_if
2742<
2743 __is_input_iterator <_InputIterator>::value &&
2744 !__is_forward_iterator<_InputIterator>::value,
2745 typename basic_string<_CharT, _Traits, _Allocator>::iterator
2746>::type
2747basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2748{
Howard Hinnant499cea12013-08-23 17:37:05 +00002749#if _LIBCPP_DEBUG_LEVEL >= 2
2750 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2751 "string::insert(iterator, range) called with an iterator not"
2752 " referring to this string");
2753#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002754 size_type __old_sz = size();
2755 difference_type __ip = __pos - begin();
2756 for (; __first != __last; ++__first)
2757 push_back(*__first);
2758 pointer __p = __get_pointer();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002759 _VSTD::rotate(__p + __ip, __p + __old_sz, __p + size());
Howard Hinnant499cea12013-08-23 17:37:05 +00002760#if _LIBCPP_DEBUG_LEVEL >= 2
2761 return iterator(this, __p + __ip);
2762#else
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002763 return iterator(__p + __ip);
Howard Hinnant499cea12013-08-23 17:37:05 +00002764#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002765}
2766
2767template <class _CharT, class _Traits, class _Allocator>
2768template<class _ForwardIterator>
2769typename enable_if
2770<
2771 __is_forward_iterator<_ForwardIterator>::value,
2772 typename basic_string<_CharT, _Traits, _Allocator>::iterator
2773>::type
2774basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2775{
Howard Hinnant499cea12013-08-23 17:37:05 +00002776#if _LIBCPP_DEBUG_LEVEL >= 2
2777 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2778 "string::insert(iterator, range) called with an iterator not"
2779 " referring to this string");
2780#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002781 size_type __ip = static_cast<size_type>(__pos - begin());
2782 size_type __sz = size();
2783 size_type __cap = capacity();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002784 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002785 if (__n)
2786 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002787 value_type* __p;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002788 if (__cap - __sz >= __n)
2789 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002790 __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002791 size_type __n_move = __sz - __ip;
2792 if (__n_move != 0)
2793 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
2794 }
2795 else
2796 {
2797 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002798 __p = _VSTD::__to_raw_pointer(__get_long_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002799 }
2800 __sz += __n;
2801 __set_size(__sz);
2802 traits_type::assign(__p[__sz], value_type());
2803 for (__p += __ip; __first != __last; ++__p, ++__first)
2804 traits_type::assign(*__p, *__first);
2805 }
2806 return begin() + __ip;
2807}
2808
2809template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002810inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002811basic_string<_CharT, _Traits, _Allocator>&
2812basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2813{
2814 return insert(__pos1, __str.data(), __str.size());
2815}
2816
2817template <class _CharT, class _Traits, class _Allocator>
2818basic_string<_CharT, _Traits, _Allocator>&
2819basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2820 size_type __pos2, size_type __n)
2821{
2822 size_type __str_sz = __str.size();
2823 if (__pos2 > __str_sz)
2824 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002825 return insert(__pos1, __str.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002826}
2827
2828template <class _CharT, class _Traits, class _Allocator>
2829basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002830basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002831{
Alp Tokerec34c482014-05-15 11:27:39 +00002832 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002833 return insert(__pos, __s, traits_type::length(__s));
2834}
2835
2836template <class _CharT, class _Traits, class _Allocator>
2837typename basic_string<_CharT, _Traits, _Allocator>::iterator
2838basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2839{
2840 size_type __ip = static_cast<size_type>(__pos - begin());
2841 size_type __sz = size();
2842 size_type __cap = capacity();
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002843 value_type* __p;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002844 if (__cap == __sz)
2845 {
2846 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002847 __p = _VSTD::__to_raw_pointer(__get_long_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002848 }
2849 else
2850 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002851 __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002852 size_type __n_move = __sz - __ip;
2853 if (__n_move != 0)
2854 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2855 }
2856 traits_type::assign(__p[__ip], __c);
2857 traits_type::assign(__p[++__sz], value_type());
2858 __set_size(__sz);
2859 return begin() + static_cast<difference_type>(__ip);
2860}
2861
2862template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002863inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002864typename basic_string<_CharT, _Traits, _Allocator>::iterator
2865basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2866{
Howard Hinnant499cea12013-08-23 17:37:05 +00002867#if _LIBCPP_DEBUG_LEVEL >= 2
2868 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2869 "string::insert(iterator, n, value) called with an iterator not"
2870 " referring to this string");
2871#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002872 difference_type __p = __pos - begin();
2873 insert(static_cast<size_type>(__p), __n, __c);
2874 return begin() + __p;
2875}
2876
2877// replace
2878
2879template <class _CharT, class _Traits, class _Allocator>
2880basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002881basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002882{
Alp Tokerec34c482014-05-15 11:27:39 +00002883 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002884 size_type __sz = size();
2885 if (__pos > __sz)
2886 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002887 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002888 size_type __cap = capacity();
2889 if (__cap - __sz + __n1 >= __n2)
2890 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002891 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002892 if (__n1 != __n2)
2893 {
2894 size_type __n_move = __sz - __pos - __n1;
2895 if (__n_move != 0)
2896 {
2897 if (__n1 > __n2)
2898 {
2899 traits_type::move(__p + __pos, __s, __n2);
2900 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2901 goto __finish;
2902 }
2903 if (__p + __pos < __s && __s < __p + __sz)
2904 {
2905 if (__p + __pos + __n1 <= __s)
2906 __s += __n2 - __n1;
2907 else // __p + __pos < __s < __p + __pos + __n1
2908 {
2909 traits_type::move(__p + __pos, __s, __n1);
2910 __pos += __n1;
2911 __s += __n2;
2912 __n2 -= __n1;
2913 __n1 = 0;
2914 }
2915 }
2916 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2917 }
2918 }
2919 traits_type::move(__p + __pos, __s, __n2);
2920__finish:
2921 __sz += __n2 - __n1;
2922 __set_size(__sz);
2923 __invalidate_iterators_past(__sz);
2924 traits_type::assign(__p[__sz], value_type());
2925 }
2926 else
2927 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
2928 return *this;
2929}
2930
2931template <class _CharT, class _Traits, class _Allocator>
2932basic_string<_CharT, _Traits, _Allocator>&
2933basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
2934{
2935 size_type __sz = size();
2936 if (__pos > __sz)
2937 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002938 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002939 size_type __cap = capacity();
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002940 value_type* __p;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002941 if (__cap - __sz + __n1 >= __n2)
2942 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002943 __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002944 if (__n1 != __n2)
2945 {
2946 size_type __n_move = __sz - __pos - __n1;
2947 if (__n_move != 0)
2948 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2949 }
2950 }
2951 else
2952 {
2953 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002954 __p = _VSTD::__to_raw_pointer(__get_long_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002955 }
2956 traits_type::assign(__p + __pos, __n2, __c);
2957 __sz += __n2 - __n1;
2958 __set_size(__sz);
2959 __invalidate_iterators_past(__sz);
2960 traits_type::assign(__p[__sz], value_type());
2961 return *this;
2962}
2963
2964template <class _CharT, class _Traits, class _Allocator>
2965template<class _InputIterator>
2966typename enable_if
2967<
2968 __is_input_iterator<_InputIterator>::value,
2969 basic_string<_CharT, _Traits, _Allocator>&
2970>::type
Howard Hinnant7b2cb482010-11-17 21:11:40 +00002971basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002972 _InputIterator __j1, _InputIterator __j2)
2973{
2974 for (; true; ++__i1, ++__j1)
2975 {
2976 if (__i1 == __i2)
2977 {
2978 if (__j1 != __j2)
2979 insert(__i1, __j1, __j2);
2980 break;
2981 }
2982 if (__j1 == __j2)
2983 {
2984 erase(__i1, __i2);
2985 break;
2986 }
Howard Hinnant7b2cb482010-11-17 21:11:40 +00002987 traits_type::assign(const_cast<value_type&>(*__i1), *__j1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002988 }
2989 return *this;
2990}
2991
2992template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002993inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002994basic_string<_CharT, _Traits, _Allocator>&
2995basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
2996{
2997 return replace(__pos1, __n1, __str.data(), __str.size());
2998}
2999
3000template <class _CharT, class _Traits, class _Allocator>
3001basic_string<_CharT, _Traits, _Allocator>&
3002basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
3003 size_type __pos2, size_type __n2)
3004{
3005 size_type __str_sz = __str.size();
3006 if (__pos2 > __str_sz)
3007 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00003008 return replace(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003009}
3010
3011template <class _CharT, class _Traits, class _Allocator>
3012basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003013basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003014{
Alp Tokerec34c482014-05-15 11:27:39 +00003015 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003016 return replace(__pos, __n1, __s, traits_type::length(__s));
3017}
3018
3019template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003020inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003021basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant7b2cb482010-11-17 21:11:40 +00003022basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003023{
3024 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
3025 __str.data(), __str.size());
3026}
3027
3028template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003029inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003030basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003031basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003032{
3033 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
3034}
3035
3036template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003037inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003038basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003039basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003040{
3041 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
3042}
3043
3044template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003045inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003046basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant7b2cb482010-11-17 21:11:40 +00003047basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003048{
3049 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
3050}
3051
3052// erase
3053
3054template <class _CharT, class _Traits, class _Allocator>
3055basic_string<_CharT, _Traits, _Allocator>&
3056basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos, size_type __n)
3057{
3058 size_type __sz = size();
3059 if (__pos > __sz)
3060 this->__throw_out_of_range();
3061 if (__n)
3062 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003063 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnant0949eed2011-06-30 21:18:19 +00003064 __n = _VSTD::min(__n, __sz - __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003065 size_type __n_move = __sz - __pos - __n;
3066 if (__n_move != 0)
3067 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
3068 __sz -= __n;
3069 __set_size(__sz);
3070 __invalidate_iterators_past(__sz);
3071 traits_type::assign(__p[__sz], value_type());
3072 }
3073 return *this;
3074}
3075
3076template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003077inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003078typename basic_string<_CharT, _Traits, _Allocator>::iterator
3079basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
3080{
Howard Hinnant499cea12013-08-23 17:37:05 +00003081#if _LIBCPP_DEBUG_LEVEL >= 2
3082 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3083 "string::erase(iterator) called with an iterator not"
3084 " referring to this string");
3085#endif
3086 _LIBCPP_ASSERT(__pos != end(),
3087 "string::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003088 iterator __b = begin();
3089 size_type __r = static_cast<size_type>(__pos - __b);
3090 erase(__r, 1);
Howard Hinnantec3773c2011-12-01 20:21:04 +00003091 return __b + static_cast<difference_type>(__r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003092}
3093
3094template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003095inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003096typename basic_string<_CharT, _Traits, _Allocator>::iterator
3097basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
3098{
Howard Hinnant499cea12013-08-23 17:37:05 +00003099#if _LIBCPP_DEBUG_LEVEL >= 2
3100 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
3101 "string::erase(iterator, iterator) called with an iterator not"
3102 " referring to this string");
3103#endif
3104 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003105 iterator __b = begin();
3106 size_type __r = static_cast<size_type>(__first - __b);
3107 erase(__r, static_cast<size_type>(__last - __first));
Howard Hinnantec3773c2011-12-01 20:21:04 +00003108 return __b + static_cast<difference_type>(__r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003109}
3110
3111template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003112inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003113void
3114basic_string<_CharT, _Traits, _Allocator>::pop_back()
3115{
Howard Hinnant499cea12013-08-23 17:37:05 +00003116 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003117 size_type __sz;
3118 if (__is_long())
3119 {
3120 __sz = __get_long_size() - 1;
3121 __set_long_size(__sz);
3122 traits_type::assign(*(__get_long_pointer() + __sz), value_type());
3123 }
3124 else
3125 {
3126 __sz = __get_short_size() - 1;
3127 __set_short_size(__sz);
3128 traits_type::assign(*(__get_short_pointer() + __sz), value_type());
3129 }
3130 __invalidate_iterators_past(__sz);
3131}
3132
3133template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003134inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003135void
Howard Hinnanta6119a82011-05-29 19:57:12 +00003136basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003137{
3138 __invalidate_all_iterators();
3139 if (__is_long())
3140 {
3141 traits_type::assign(*__get_long_pointer(), value_type());
3142 __set_long_size(0);
3143 }
3144 else
3145 {
3146 traits_type::assign(*__get_short_pointer(), value_type());
3147 __set_short_size(0);
3148 }
3149}
3150
3151template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003152inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003153void
3154basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
3155{
3156 if (__is_long())
3157 {
3158 traits_type::assign(*(__get_long_pointer() + __pos), value_type());
3159 __set_long_size(__pos);
3160 }
3161 else
3162 {
3163 traits_type::assign(*(__get_short_pointer() + __pos), value_type());
3164 __set_short_size(__pos);
3165 }
3166 __invalidate_iterators_past(__pos);
3167}
3168
3169template <class _CharT, class _Traits, class _Allocator>
3170void
3171basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
3172{
3173 size_type __sz = size();
3174 if (__n > __sz)
3175 append(__n - __sz, __c);
3176 else
3177 __erase_to_end(__n);
3178}
3179
3180template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003181inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003182typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003183basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003184{
Howard Hinnante32b5e22010-11-17 17:55:08 +00003185 size_type __m = __alloc_traits::max_size(__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003186#if _LIBCPP_BIG_ENDIAN
Marshall Clow09f85502013-10-31 17:23:08 +00003187 return (__m <= ~__long_mask ? __m : __m/2) - __alignment;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003188#else
Marshall Clow09f85502013-10-31 17:23:08 +00003189 return __m - __alignment;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003190#endif
3191}
3192
3193template <class _CharT, class _Traits, class _Allocator>
3194void
3195basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __res_arg)
3196{
3197 if (__res_arg > max_size())
3198 this->__throw_length_error();
3199 size_type __cap = capacity();
3200 size_type __sz = size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00003201 __res_arg = _VSTD::max(__res_arg, __sz);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003202 __res_arg = __recommend(__res_arg);
3203 if (__res_arg != __cap)
3204 {
3205 pointer __new_data, __p;
3206 bool __was_long, __now_long;
3207 if (__res_arg == __min_cap - 1)
3208 {
3209 __was_long = true;
3210 __now_long = false;
3211 __new_data = __get_short_pointer();
3212 __p = __get_long_pointer();
3213 }
3214 else
3215 {
3216 if (__res_arg > __cap)
Howard Hinnante32b5e22010-11-17 17:55:08 +00003217 __new_data = __alloc_traits::allocate(__alloc(), __res_arg+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003218 else
3219 {
3220 #ifndef _LIBCPP_NO_EXCEPTIONS
3221 try
3222 {
Howard Hinnant324bb032010-08-22 00:02:43 +00003223 #endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnante32b5e22010-11-17 17:55:08 +00003224 __new_data = __alloc_traits::allocate(__alloc(), __res_arg+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003225 #ifndef _LIBCPP_NO_EXCEPTIONS
3226 }
3227 catch (...)
3228 {
3229 return;
3230 }
Howard Hinnant324bb032010-08-22 00:02:43 +00003231 #else // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003232 if (__new_data == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003233 return;
Howard Hinnant324bb032010-08-22 00:02:43 +00003234 #endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003235 }
3236 __now_long = true;
3237 __was_long = __is_long();
3238 __p = __get_pointer();
3239 }
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003240 traits_type::copy(_VSTD::__to_raw_pointer(__new_data),
3241 _VSTD::__to_raw_pointer(__p), size()+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003242 if (__was_long)
Howard Hinnante32b5e22010-11-17 17:55:08 +00003243 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003244 if (__now_long)
3245 {
3246 __set_long_cap(__res_arg+1);
3247 __set_long_size(__sz);
3248 __set_long_pointer(__new_data);
3249 }
3250 else
3251 __set_short_size(__sz);
3252 __invalidate_all_iterators();
3253 }
3254}
3255
3256template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003257inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003258typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3259basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const
3260{
Howard Hinnant499cea12013-08-23 17:37:05 +00003261 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003262 return *(data() + __pos);
3263}
3264
3265template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003266inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003267typename basic_string<_CharT, _Traits, _Allocator>::reference
3268basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos)
3269{
Howard Hinnant499cea12013-08-23 17:37:05 +00003270 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003271 return *(__get_pointer() + __pos);
3272}
3273
3274template <class _CharT, class _Traits, class _Allocator>
3275typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3276basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3277{
3278 if (__n >= size())
3279 this->__throw_out_of_range();
3280 return (*this)[__n];
3281}
3282
3283template <class _CharT, class _Traits, class _Allocator>
3284typename basic_string<_CharT, _Traits, _Allocator>::reference
3285basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
3286{
3287 if (__n >= size())
3288 this->__throw_out_of_range();
3289 return (*this)[__n];
3290}
3291
3292template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003293inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003294typename basic_string<_CharT, _Traits, _Allocator>::reference
3295basic_string<_CharT, _Traits, _Allocator>::front()
3296{
Howard Hinnant499cea12013-08-23 17:37:05 +00003297 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003298 return *__get_pointer();
3299}
3300
3301template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003302inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003303typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3304basic_string<_CharT, _Traits, _Allocator>::front() const
3305{
Howard Hinnant499cea12013-08-23 17:37:05 +00003306 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003307 return *data();
3308}
3309
3310template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003311inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003312typename basic_string<_CharT, _Traits, _Allocator>::reference
3313basic_string<_CharT, _Traits, _Allocator>::back()
3314{
Howard Hinnant499cea12013-08-23 17:37:05 +00003315 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003316 return *(__get_pointer() + size() - 1);
3317}
3318
3319template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003320inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003321typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3322basic_string<_CharT, _Traits, _Allocator>::back() const
3323{
Howard Hinnant499cea12013-08-23 17:37:05 +00003324 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003325 return *(data() + size() - 1);
3326}
3327
3328template <class _CharT, class _Traits, class _Allocator>
3329typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003330basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003331{
3332 size_type __sz = size();
3333 if (__pos > __sz)
3334 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00003335 size_type __rlen = _VSTD::min(__n, __sz - __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003336 traits_type::copy(__s, data() + __pos, __rlen);
3337 return __rlen;
3338}
3339
3340template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003341inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003342basic_string<_CharT, _Traits, _Allocator>
3343basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3344{
3345 return basic_string(*this, __pos, __n, __alloc());
3346}
3347
3348template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003349inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003350void
3351basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00003352 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
3353 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003354{
Howard Hinnant499cea12013-08-23 17:37:05 +00003355#if _LIBCPP_DEBUG_LEVEL >= 2
3356 if (!__is_long())
3357 __get_db()->__invalidate_all(this);
3358 if (!__str.__is_long())
3359 __get_db()->__invalidate_all(&__str);
3360 __get_db()->swap(this, &__str);
3361#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00003362 _VSTD::swap(__r_.first(), __str.__r_.first());
Howard Hinnante32b5e22010-11-17 17:55:08 +00003363 __swap_alloc(__alloc(), __str.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003364}
3365
3366// find
3367
3368template <class _Traits>
3369struct _LIBCPP_HIDDEN __traits_eq
3370{
3371 typedef typename _Traits::char_type char_type;
Howard Hinnanta6119a82011-05-29 19:57:12 +00003372 _LIBCPP_INLINE_VISIBILITY
3373 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3374 {return _Traits::eq(__x, __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003375};
3376
3377template<class _CharT, class _Traits, class _Allocator>
3378typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003379basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003380 size_type __pos,
3381 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003382{
Alp Tokerec34c482014-05-15 11:27:39 +00003383 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clow37025e12014-06-10 18:51:55 +00003384 return _VSTD::__str_find<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003385 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003386}
3387
3388template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003389inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003390typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003391basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3392 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003393{
Marshall Clow37025e12014-06-10 18:51:55 +00003394 return _VSTD::__str_find<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003395 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003396}
3397
3398template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003399inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003400typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003401basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003402 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003403{
Alp Tokerec34c482014-05-15 11:27:39 +00003404 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clow37025e12014-06-10 18:51:55 +00003405 return _VSTD::__str_find<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003406 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003407}
3408
3409template<class _CharT, class _Traits, class _Allocator>
3410typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003411basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3412 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003413{
Marshall Clow37025e12014-06-10 18:51:55 +00003414 return _VSTD::__str_find<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003415 (data(), size(), __c, __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003416}
3417
3418// rfind
3419
3420template<class _CharT, class _Traits, class _Allocator>
3421typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003422basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003423 size_type __pos,
3424 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003425{
Alp Tokerec34c482014-05-15 11:27:39 +00003426 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clow37025e12014-06-10 18:51:55 +00003427 return _VSTD::__str_rfind<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003428 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003429}
3430
3431template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003432inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003433typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003434basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3435 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003436{
Marshall Clow37025e12014-06-10 18:51:55 +00003437 return _VSTD::__str_rfind<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003438 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003439}
3440
3441template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003442inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003443typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003444basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003445 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003446{
Alp Tokerec34c482014-05-15 11:27:39 +00003447 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clow37025e12014-06-10 18:51:55 +00003448 return _VSTD::__str_rfind<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003449 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003450}
3451
3452template<class _CharT, class _Traits, class _Allocator>
3453typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003454basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3455 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003456{
Marshall Clow37025e12014-06-10 18:51:55 +00003457 return _VSTD::__str_rfind<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003458 (data(), size(), __c, __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003459}
3460
3461// find_first_of
3462
3463template<class _CharT, class _Traits, class _Allocator>
3464typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003465basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003466 size_type __pos,
3467 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003468{
Alp Tokerec34c482014-05-15 11:27:39 +00003469 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clow37025e12014-06-10 18:51:55 +00003470 return _VSTD::__str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003471 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003472}
3473
3474template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003475inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003476typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003477basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3478 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003479{
Marshall Clow37025e12014-06-10 18:51:55 +00003480 return _VSTD::__str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003481 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003482}
3483
3484template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003485inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003486typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003487basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003488 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003489{
Alp Tokerec34c482014-05-15 11:27:39 +00003490 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clow37025e12014-06-10 18:51:55 +00003491 return _VSTD::__str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003492 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003493}
3494
3495template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003496inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003497typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003498basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3499 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003500{
3501 return find(__c, __pos);
3502}
3503
3504// find_last_of
3505
3506template<class _CharT, class _Traits, class _Allocator>
3507typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003508basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003509 size_type __pos,
3510 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003511{
Alp Tokerec34c482014-05-15 11:27:39 +00003512 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clow37025e12014-06-10 18:51:55 +00003513 return _VSTD::__str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003514 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003515}
3516
3517template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003518inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003519typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003520basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3521 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003522{
Marshall Clow37025e12014-06-10 18:51:55 +00003523 return _VSTD::__str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003524 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003525}
3526
3527template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003528inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003529typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003530basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003531 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003532{
Alp Tokerec34c482014-05-15 11:27:39 +00003533 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clow37025e12014-06-10 18:51:55 +00003534 return _VSTD::__str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003535 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003536}
3537
3538template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003539inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003540typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003541basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3542 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003543{
3544 return rfind(__c, __pos);
3545}
3546
3547// find_first_not_of
3548
3549template<class _CharT, class _Traits, class _Allocator>
3550typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003551basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003552 size_type __pos,
3553 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003554{
Alp Tokerec34c482014-05-15 11:27:39 +00003555 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clow37025e12014-06-10 18:51:55 +00003556 return _VSTD::__str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003557 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003558}
3559
3560template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003561inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003562typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003563basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3564 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003565{
Marshall Clow37025e12014-06-10 18:51:55 +00003566 return _VSTD::__str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003567 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003568}
3569
3570template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003571inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003572typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003573basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003574 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003575{
Alp Tokerec34c482014-05-15 11:27:39 +00003576 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clow37025e12014-06-10 18:51:55 +00003577 return _VSTD::__str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003578 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003579}
3580
3581template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003582inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003583typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003584basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3585 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003586{
Marshall Clow37025e12014-06-10 18:51:55 +00003587 return _VSTD::__str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003588 (data(), size(), __c, __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003589}
3590
3591// find_last_not_of
3592
3593template<class _CharT, class _Traits, class _Allocator>
3594typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003595basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003596 size_type __pos,
3597 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003598{
Alp Tokerec34c482014-05-15 11:27:39 +00003599 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clow37025e12014-06-10 18:51:55 +00003600 return _VSTD::__str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003601 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003602}
3603
3604template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003605inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003606typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003607basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3608 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003609{
Marshall Clow37025e12014-06-10 18:51:55 +00003610 return _VSTD::__str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003611 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003612}
3613
3614template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003615inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003616typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003617basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003618 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003619{
Alp Tokerec34c482014-05-15 11:27:39 +00003620 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clow37025e12014-06-10 18:51:55 +00003621 return _VSTD::__str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003622 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003623}
3624
3625template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003626inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003627typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003628basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3629 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003630{
Marshall Clow37025e12014-06-10 18:51:55 +00003631 return _VSTD::__str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003632 (data(), size(), __c, __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003633}
3634
3635// compare
3636
3637template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003638inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003639int
Howard Hinnanta6119a82011-05-29 19:57:12 +00003640basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003641{
Howard Hinnantfa06d752011-07-24 21:45:06 +00003642 size_t __lhs_sz = size();
3643 size_t __rhs_sz = __str.size();
3644 int __result = traits_type::compare(data(), __str.data(),
3645 _VSTD::min(__lhs_sz, __rhs_sz));
3646 if (__result != 0)
3647 return __result;
3648 if (__lhs_sz < __rhs_sz)
3649 return -1;
3650 if (__lhs_sz > __rhs_sz)
3651 return 1;
3652 return 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003653}
3654
3655template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003656inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003657int
Howard Hinnanta6119a82011-05-29 19:57:12 +00003658basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3659 size_type __n1,
3660 const basic_string& __str) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003661{
3662 return compare(__pos1, __n1, __str.data(), __str.size());
3663}
3664
3665template <class _CharT, class _Traits, class _Allocator>
3666int
Howard Hinnanta6119a82011-05-29 19:57:12 +00003667basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3668 size_type __n1,
3669 const basic_string& __str,
3670 size_type __pos2,
3671 size_type __n2) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003672{
3673 size_type __sz = __str.size();
3674 if (__pos2 > __sz)
3675 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00003676 return compare(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003677 __sz - __pos2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003678}
3679
3680template <class _CharT, class _Traits, class _Allocator>
3681int
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003682basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003683{
Alp Tokerec34c482014-05-15 11:27:39 +00003684 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003685 return compare(0, npos, __s, traits_type::length(__s));
3686}
3687
3688template <class _CharT, class _Traits, class _Allocator>
3689int
Howard Hinnanta6119a82011-05-29 19:57:12 +00003690basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3691 size_type __n1,
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003692 const value_type* __s) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003693{
Alp Tokerec34c482014-05-15 11:27:39 +00003694 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003695 return compare(__pos1, __n1, __s, traits_type::length(__s));
3696}
3697
3698template <class _CharT, class _Traits, class _Allocator>
3699int
Howard Hinnanta6119a82011-05-29 19:57:12 +00003700basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3701 size_type __n1,
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003702 const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003703 size_type __n2) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003704{
Alp Tokerec34c482014-05-15 11:27:39 +00003705 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003706 size_type __sz = size();
3707 if (__pos1 > __sz || __n2 == npos)
3708 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00003709 size_type __rlen = _VSTD::min(__n1, __sz - __pos1);
3710 int __r = traits_type::compare(data() + __pos1, __s, _VSTD::min(__rlen, __n2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003711 if (__r == 0)
3712 {
3713 if (__rlen < __n2)
3714 __r = -1;
3715 else if (__rlen > __n2)
3716 __r = 1;
3717 }
3718 return __r;
3719}
3720
3721// __invariants
3722
3723template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003724inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003725bool
3726basic_string<_CharT, _Traits, _Allocator>::__invariants() const
3727{
3728 if (size() > capacity())
3729 return false;
3730 if (capacity() < __min_cap - 1)
3731 return false;
3732 if (data() == 0)
3733 return false;
3734 if (data()[size()] != value_type(0))
3735 return false;
3736 return true;
3737}
3738
3739// operator==
3740
3741template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003742inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003743bool
3744operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003745 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003746{
Howard Hinnant08dd2532013-04-22 23:55:13 +00003747 size_t __lhs_sz = __lhs.size();
3748 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
3749 __rhs.data(),
3750 __lhs_sz) == 0;
3751}
3752
3753template<class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003754inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant08dd2532013-04-22 23:55:13 +00003755bool
3756operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
3757 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
3758{
3759 size_t __lhs_sz = __lhs.size();
3760 if (__lhs_sz != __rhs.size())
3761 return false;
3762 const char* __lp = __lhs.data();
3763 const char* __rp = __rhs.data();
3764 if (__lhs.__is_long())
3765 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
3766 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
3767 if (*__lp != *__rp)
3768 return false;
3769 return true;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003770}
3771
3772template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003773inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003774bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003775operator==(const _CharT* __lhs,
3776 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003777{
3778 return __rhs.compare(__lhs) == 0;
3779}
3780
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003781template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003782inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003783bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003784operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
3785 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003786{
3787 return __lhs.compare(__rhs) == 0;
3788}
3789
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003790// operator!=
3791
Howard Hinnant324bb032010-08-22 00:02:43 +00003792template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003793inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003794bool
3795operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003796 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003797{
3798 return !(__lhs == __rhs);
3799}
3800
3801template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003802inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003803bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003804operator!=(const _CharT* __lhs,
3805 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003806{
3807 return !(__lhs == __rhs);
3808}
3809
3810template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003811inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003812bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003813operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3814 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003815{
3816 return !(__lhs == __rhs);
3817}
3818
3819// operator<
3820
3821template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003822inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003823bool
3824operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003825 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003826{
Howard Hinnant2644a7b2011-07-24 15:07:21 +00003827 return __lhs.compare(__rhs) < 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003828}
3829
3830template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003831inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003832bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003833operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3834 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003835{
Howard Hinnant2644a7b2011-07-24 15:07:21 +00003836 return __lhs.compare(__rhs) < 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003837}
3838
3839template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003840inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003841bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003842operator< (const _CharT* __lhs,
3843 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003844{
3845 return __rhs.compare(__lhs) > 0;
3846}
3847
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003848// operator>
3849
3850template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003851inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003852bool
3853operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003854 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003855{
3856 return __rhs < __lhs;
3857}
3858
3859template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003860inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003861bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003862operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3863 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003864{
3865 return __rhs < __lhs;
3866}
3867
3868template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003869inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003870bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003871operator> (const _CharT* __lhs,
3872 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003873{
3874 return __rhs < __lhs;
3875}
3876
3877// operator<=
3878
3879template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003880inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003881bool
3882operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003883 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003884{
3885 return !(__rhs < __lhs);
3886}
3887
3888template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003889inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003890bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003891operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3892 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003893{
3894 return !(__rhs < __lhs);
3895}
3896
3897template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003898inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003899bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003900operator<=(const _CharT* __lhs,
3901 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003902{
3903 return !(__rhs < __lhs);
3904}
3905
3906// operator>=
3907
3908template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003909inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003910bool
3911operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003912 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003913{
3914 return !(__lhs < __rhs);
3915}
3916
3917template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003918inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003919bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003920operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3921 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003922{
3923 return !(__lhs < __rhs);
3924}
3925
3926template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003927inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003928bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003929operator>=(const _CharT* __lhs,
3930 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003931{
3932 return !(__lhs < __rhs);
3933}
3934
3935// operator +
3936
3937template<class _CharT, class _Traits, class _Allocator>
3938basic_string<_CharT, _Traits, _Allocator>
3939operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3940 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3941{
3942 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
3943 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
3944 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
3945 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
3946 __r.append(__rhs.data(), __rhs_sz);
3947 return __r;
3948}
3949
3950template<class _CharT, class _Traits, class _Allocator>
3951basic_string<_CharT, _Traits, _Allocator>
3952operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
3953{
3954 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
3955 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = _Traits::length(__lhs);
3956 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
3957 __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz);
3958 __r.append(__rhs.data(), __rhs_sz);
3959 return __r;
3960}
3961
3962template<class _CharT, class _Traits, class _Allocator>
3963basic_string<_CharT, _Traits, _Allocator>
3964operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
3965{
3966 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
3967 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
3968 __r.__init(&__lhs, 1, 1 + __rhs_sz);
3969 __r.append(__rhs.data(), __rhs_sz);
3970 return __r;
3971}
3972
3973template<class _CharT, class _Traits, class _Allocator>
3974basic_string<_CharT, _Traits, _Allocator>
3975operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
3976{
3977 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
3978 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
3979 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = _Traits::length(__rhs);
3980 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
3981 __r.append(__rhs, __rhs_sz);
3982 return __r;
3983}
3984
3985template<class _CharT, class _Traits, class _Allocator>
3986basic_string<_CharT, _Traits, _Allocator>
3987operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
3988{
3989 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
3990 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
3991 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1);
3992 __r.push_back(__rhs);
3993 return __r;
3994}
3995
Howard Hinnant73d21a42010-09-04 23:28:19 +00003996#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003997
3998template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003999inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004000basic_string<_CharT, _Traits, _Allocator>
4001operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4002{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004003 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004004}
4005
4006template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00004007inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004008basic_string<_CharT, _Traits, _Allocator>
4009operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4010{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004011 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004012}
4013
4014template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00004015inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004016basic_string<_CharT, _Traits, _Allocator>
4017operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4018{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004019 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004020}
4021
4022template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00004023inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004024basic_string<_CharT, _Traits, _Allocator>
4025operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4026{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004027 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004028}
4029
4030template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00004031inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004032basic_string<_CharT, _Traits, _Allocator>
4033operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4034{
4035 __rhs.insert(__rhs.begin(), __lhs);
Howard Hinnant0949eed2011-06-30 21:18:19 +00004036 return _VSTD::move(__rhs);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004037}
4038
4039template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00004040inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004041basic_string<_CharT, _Traits, _Allocator>
4042operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
4043{
Howard Hinnant0949eed2011-06-30 21:18:19 +00004044 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004045}
4046
4047template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00004048inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004049basic_string<_CharT, _Traits, _Allocator>
4050operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
4051{
4052 __lhs.push_back(__rhs);
Howard Hinnant0949eed2011-06-30 21:18:19 +00004053 return _VSTD::move(__lhs);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004054}
4055
Howard Hinnant73d21a42010-09-04 23:28:19 +00004056#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004057
4058// swap
4059
4060template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00004061inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004062void
Howard Hinnanta6119a82011-05-29 19:57:12 +00004063swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00004064 basic_string<_CharT, _Traits, _Allocator>& __rhs)
4065 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004066{
4067 __lhs.swap(__rhs);
4068}
4069
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004070#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
4071
4072typedef basic_string<char16_t> u16string;
4073typedef basic_string<char32_t> u32string;
4074
Howard Hinnant324bb032010-08-22 00:02:43 +00004075#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004076
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004077_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = 0, int __base = 10);
4078_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = 0, int __base = 10);
4079_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = 0, int __base = 10);
4080_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = 0, int __base = 10);
4081_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = 0, int __base = 10);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00004082
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004083_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = 0);
4084_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = 0);
4085_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = 0);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00004086
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004087_LIBCPP_FUNC_VIS string to_string(int __val);
4088_LIBCPP_FUNC_VIS string to_string(unsigned __val);
4089_LIBCPP_FUNC_VIS string to_string(long __val);
4090_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
4091_LIBCPP_FUNC_VIS string to_string(long long __val);
4092_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
4093_LIBCPP_FUNC_VIS string to_string(float __val);
4094_LIBCPP_FUNC_VIS string to_string(double __val);
4095_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00004096
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004097_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = 0, int __base = 10);
4098_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = 0, int __base = 10);
4099_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = 0, int __base = 10);
4100_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = 0, int __base = 10);
4101_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = 0, int __base = 10);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00004102
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004103_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = 0);
4104_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = 0);
4105_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = 0);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00004106
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004107_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
4108_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
4109_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
4110_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
4111_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
4112_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
4113_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
4114_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
4115_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00004116
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004117template<class _CharT, class _Traits, class _Allocator>
4118 const typename basic_string<_CharT, _Traits, _Allocator>::size_type
4119 basic_string<_CharT, _Traits, _Allocator>::npos;
4120
4121template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004122struct _LIBCPP_TYPE_VIS_ONLY hash<basic_string<_CharT, _Traits, _Allocator> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004123 : public unary_function<basic_string<_CharT, _Traits, _Allocator>, size_t>
4124{
4125 size_t
Howard Hinnanta6119a82011-05-29 19:57:12 +00004126 operator()(const basic_string<_CharT, _Traits, _Allocator>& __val) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004127};
4128
4129template<class _CharT, class _Traits, class _Allocator>
4130size_t
4131hash<basic_string<_CharT, _Traits, _Allocator> >::operator()(
Howard Hinnanta6119a82011-05-29 19:57:12 +00004132 const basic_string<_CharT, _Traits, _Allocator>& __val) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004133{
Sean Huntaffd9e52011-07-29 23:31:56 +00004134 return __do_string_hash(__val.data(), __val.data() + __val.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004135}
4136
Howard Hinnant464aa5c2011-07-18 15:51:59 +00004137template<class _CharT, class _Traits, class _Allocator>
4138basic_ostream<_CharT, _Traits>&
4139operator<<(basic_ostream<_CharT, _Traits>& __os,
4140 const basic_string<_CharT, _Traits, _Allocator>& __str);
4141
4142template<class _CharT, class _Traits, class _Allocator>
4143basic_istream<_CharT, _Traits>&
4144operator>>(basic_istream<_CharT, _Traits>& __is,
4145 basic_string<_CharT, _Traits, _Allocator>& __str);
4146
4147template<class _CharT, class _Traits, class _Allocator>
4148basic_istream<_CharT, _Traits>&
4149getline(basic_istream<_CharT, _Traits>& __is,
4150 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4151
4152template<class _CharT, class _Traits, class _Allocator>
4153inline _LIBCPP_INLINE_VISIBILITY
4154basic_istream<_CharT, _Traits>&
4155getline(basic_istream<_CharT, _Traits>& __is,
4156 basic_string<_CharT, _Traits, _Allocator>& __str);
4157
4158#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
4159
4160template<class _CharT, class _Traits, class _Allocator>
4161inline _LIBCPP_INLINE_VISIBILITY
4162basic_istream<_CharT, _Traits>&
4163getline(basic_istream<_CharT, _Traits>&& __is,
4164 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4165
4166template<class _CharT, class _Traits, class _Allocator>
4167inline _LIBCPP_INLINE_VISIBILITY
4168basic_istream<_CharT, _Traits>&
4169getline(basic_istream<_CharT, _Traits>&& __is,
4170 basic_string<_CharT, _Traits, _Allocator>& __str);
4171
4172#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
4173
Howard Hinnant499cea12013-08-23 17:37:05 +00004174#if _LIBCPP_DEBUG_LEVEL >= 2
4175
4176template<class _CharT, class _Traits, class _Allocator>
4177bool
4178basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4179{
4180 return this->data() <= _VSTD::__to_raw_pointer(__i->base()) &&
4181 _VSTD::__to_raw_pointer(__i->base()) < this->data() + this->size();
4182}
4183
4184template<class _CharT, class _Traits, class _Allocator>
4185bool
4186basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4187{
4188 return this->data() < _VSTD::__to_raw_pointer(__i->base()) &&
4189 _VSTD::__to_raw_pointer(__i->base()) <= this->data() + this->size();
4190}
4191
4192template<class _CharT, class _Traits, class _Allocator>
4193bool
4194basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4195{
4196 const value_type* __p = _VSTD::__to_raw_pointer(__i->base()) + __n;
4197 return this->data() <= __p && __p <= this->data() + this->size();
4198}
4199
4200template<class _CharT, class _Traits, class _Allocator>
4201bool
4202basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4203{
4204 const value_type* __p = _VSTD::__to_raw_pointer(__i->base()) + __n;
4205 return this->data() <= __p && __p < this->data() + this->size();
4206}
4207
4208#endif // _LIBCPP_DEBUG_LEVEL >= 2
4209
Marshall Clow15234322013-07-23 17:05:24 +00004210#if _LIBCPP_STD_VER > 11
4211// Literal suffixes for basic_string [basic.string.literals]
Marshall Clow8d9dd7a2013-10-05 21:18:32 +00004212inline namespace literals
Marshall Clow15234322013-07-23 17:05:24 +00004213{
4214 inline namespace string_literals
4215 {
Howard Hinnantab61b2c2013-08-07 19:39:48 +00004216 inline _LIBCPP_INLINE_VISIBILITY
4217 basic_string<char> operator "" s( const char *__str, size_t __len )
4218 {
4219 return basic_string<char> (__str, __len);
4220 }
Marshall Clow15234322013-07-23 17:05:24 +00004221
Howard Hinnantab61b2c2013-08-07 19:39:48 +00004222 inline _LIBCPP_INLINE_VISIBILITY
4223 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4224 {
4225 return basic_string<wchar_t> (__str, __len);
4226 }
Marshall Clow15234322013-07-23 17:05:24 +00004227
Howard Hinnantab61b2c2013-08-07 19:39:48 +00004228 inline _LIBCPP_INLINE_VISIBILITY
4229 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4230 {
4231 return basic_string<char16_t> (__str, __len);
4232 }
Marshall Clow15234322013-07-23 17:05:24 +00004233
Howard Hinnantab61b2c2013-08-07 19:39:48 +00004234 inline _LIBCPP_INLINE_VISIBILITY
4235 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4236 {
4237 return basic_string<char32_t> (__str, __len);
4238 }
Marshall Clow15234322013-07-23 17:05:24 +00004239 }
4240}
4241#endif
4242
Howard Hinnant0f678bd2013-08-12 18:38:34 +00004243_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_TYPE_VIS basic_string<char>)
4244_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_TYPE_VIS basic_string<wchar_t>)
Howard Hinnant499cea12013-08-23 17:37:05 +00004245_LIBCPP_EXTERN_TEMPLATE(string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004246
4247_LIBCPP_END_NAMESPACE_STD
4248
4249#endif // _LIBCPP_STRING