blob: 4e29e8ae166eda9bc978ea0f0884e672c8006b5d [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;
54 static bool eq(char_type c1, char_type c2) noexcept;
55 static 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 Hinnanta6119a82011-05-29 19:57:12 +000064 static int_type not_eof(int_type c) noexcept;
65 static char_type to_char_type(int_type c) noexcept;
66 static int_type to_int_type(char_type c) noexcept;
67 static bool eq_int_type(int_type c1, int_type c2) noexcept;
68 static 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 Hinnantbc8d3f92010-05-11 19:42:16 +0000103 basic_string(const_pointer s, const allocator_type& a = allocator_type());
104 basic_string(const_pointer s, size_type n, const allocator_type& a = allocator_type());
105 basic_string(size_type n, value_type c, const allocator_type& a = allocator_type());
106 template<class InputIterator>
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 Hinnantbc8d3f92010-05-11 19:42:16 +0000120 basic_string& operator=(const_pointer s);
121 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);
159 basic_string& operator+=(const_pointer s);
160 basic_string& operator+=(value_type c);
161 basic_string& operator+=(initializer_list<value_type>);
162
163 basic_string& append(const basic_string& str);
164 basic_string& append(const basic_string& str, size_type pos, size_type n);
165 basic_string& append(const_pointer s, size_type n);
166 basic_string& append(const_pointer s);
167 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);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000181 basic_string& assign(const basic_string& str, size_type pos, size_type n);
182 basic_string& assign(const_pointer s, size_type n);
183 basic_string& assign(const_pointer s);
184 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);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000192 basic_string& insert(size_type pos, const_pointer s, size_type n);
193 basic_string& insert(size_type pos, const_pointer s);
194 basic_string& insert(size_type pos, size_type n, value_type c);
195 iterator insert(const_iterator p, value_type c);
196 iterator insert(const_iterator p, size_type n, value_type c);
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,
207 size_type pos2, size_type n2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000208 basic_string& replace(size_type pos, size_type n1, const_pointer s, size_type n2);
209 basic_string& replace(size_type pos, size_type n1, const_pointer s);
210 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);
212 basic_string& replace(const_iterator i1, const_iterator i2, const_pointer s, size_type n);
213 basic_string& replace(const_iterator i1, const_iterator i2, const_pointer s);
214 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
219 size_type copy(pointer s, size_type n, size_type pos = 0) const;
220 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 Hinnanta6119a82011-05-29 19:57:12 +0000226 const_pointer c_str() const noexcept;
227 const_pointer 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;
232 size_type find(const_pointer s, size_type pos, size_type n) const noexcept;
233 size_type find(const_pointer s, size_type pos = 0) const noexcept;
234 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;
237 size_type rfind(const_pointer s, size_type pos, size_type n) const noexcept;
238 size_type rfind(const_pointer s, size_type pos = npos) const noexcept;
239 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;
242 size_type find_first_of(const_pointer s, size_type pos, size_type n) const noexcept;
243 size_type find_first_of(const_pointer s, size_type pos = 0) const noexcept;
244 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;
247 size_type find_last_of(const_pointer s, size_type pos, size_type n) const noexcept;
248 size_type find_last_of(const_pointer s, size_type pos = npos) const noexcept;
249 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;
252 size_type find_first_not_of(const_pointer s, size_type pos, size_type n) const noexcept;
253 size_type find_first_not_of(const_pointer s, size_type pos = 0) const noexcept;
254 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;
257 size_type find_last_not_of(const_pointer s, size_type pos, size_type n) const noexcept;
258 size_type find_last_not_of(const_pointer s, size_type pos = npos) const noexcept;
259 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,
264 size_type pos2, size_type n2) const;
Howard Hinnanta6119a82011-05-29 19:57:12 +0000265 int compare(const_pointer s) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000266 int compare(size_type pos1, size_type n1, const_pointer s) const;
267 int compare(size_type pos1, size_type n1, const_pointer s, size_type n2) const;
268
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
425} // std
426
427*/
428
429#include <__config>
430#include <iosfwd>
431#include <cstring>
Howard Hinnantadff4892010-05-24 17:49:41 +0000432#include <cstdio> // For EOF.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000433#include <cwchar>
434#include <algorithm>
435#include <iterator>
436#include <utility>
437#include <memory>
438#include <stdexcept>
439#include <type_traits>
440#include <initializer_list>
441#include <__functional_base>
442#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
443#include <cstdint>
444#endif
445#if defined(_LIBCPP_NO_EXCEPTIONS) || defined(_LIBCPP_DEBUG)
446#include <cassert>
447#endif
448
Howard Hinnant08e17472011-10-17 20:05:10 +0000449#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000450#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000451#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000452
453_LIBCPP_BEGIN_NAMESPACE_STD
454
455// fpos
456
457template <class _StateT>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000458class _LIBCPP_VISIBLE fpos
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000459{
460private:
461 _StateT __st_;
462 streamoff __off_;
463public:
464 _LIBCPP_INLINE_VISIBILITY fpos(streamoff __off = streamoff()) : __st_(), __off_(__off) {}
465
466 _LIBCPP_INLINE_VISIBILITY operator streamoff() const {return __off_;}
467
468 _LIBCPP_INLINE_VISIBILITY _StateT state() const {return __st_;}
469 _LIBCPP_INLINE_VISIBILITY void state(_StateT __st) {__st_ = __st;}
470
471 _LIBCPP_INLINE_VISIBILITY fpos& operator+=(streamoff __off) {__off_ += __off; return *this;}
472 _LIBCPP_INLINE_VISIBILITY fpos operator+ (streamoff __off) const {fpos __t(*this); __t += __off; return __t;}
473 _LIBCPP_INLINE_VISIBILITY fpos& operator-=(streamoff __off) {__off_ -= __off; return *this;}
474 _LIBCPP_INLINE_VISIBILITY fpos operator- (streamoff __off) const {fpos __t(*this); __t -= __off; return __t;}
475};
476
477template <class _StateT>
478inline _LIBCPP_INLINE_VISIBILITY
479streamoff operator-(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
480 {return streamoff(__x) - streamoff(__y);}
481
482template <class _StateT>
483inline _LIBCPP_INLINE_VISIBILITY
484bool operator==(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
485 {return streamoff(__x) == streamoff(__y);}
486
487template <class _StateT>
488inline _LIBCPP_INLINE_VISIBILITY
489bool operator!=(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
490 {return streamoff(__x) != streamoff(__y);}
491
492// char_traits
493
494template <class _CharT>
Howard Hinnant36cdf022010-09-10 16:42:26 +0000495struct _LIBCPP_VISIBLE char_traits
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000496{
497 typedef _CharT char_type;
498 typedef int int_type;
499 typedef streamoff off_type;
500 typedef streampos pos_type;
501 typedef mbstate_t state_type;
502
Howard Hinnanta6119a82011-05-29 19:57:12 +0000503 _LIBCPP_INLINE_VISIBILITY
504 static void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT
505 {__c1 = __c2;}
506 _LIBCPP_INLINE_VISIBILITY
507 static bool eq(char_type __c1, char_type __c2) _NOEXCEPT
508 {return __c1 == __c2;}
509 _LIBCPP_INLINE_VISIBILITY
510 static bool lt(char_type __c1, char_type __c2) _NOEXCEPT
511 {return __c1 < __c2;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000512
513 static int compare(const char_type* __s1, const char_type* __s2, size_t __n);
514 static size_t length(const char_type* __s);
515 static const char_type* find(const char_type* __s, size_t __n, const char_type& __a);
516 static char_type* move(char_type* __s1, const char_type* __s2, size_t __n);
517 static char_type* copy(char_type* __s1, const char_type* __s2, size_t __n);
518 static char_type* assign(char_type* __s, size_t __n, char_type __a);
519
Howard Hinnanta6119a82011-05-29 19:57:12 +0000520 _LIBCPP_INLINE_VISIBILITY static int_type not_eof(int_type __c) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000521 {return eq_int_type(__c, eof()) ? ~eof() : __c;}
Howard Hinnanta6119a82011-05-29 19:57:12 +0000522 _LIBCPP_INLINE_VISIBILITY
523 static char_type to_char_type(int_type __c) _NOEXCEPT
524 {return char_type(__c);}
525 _LIBCPP_INLINE_VISIBILITY
526 static int_type to_int_type(char_type __c) _NOEXCEPT
527 {return int_type(__c);}
528 _LIBCPP_INLINE_VISIBILITY
529 static bool eq_int_type(int_type __c1, int_type __c2) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000530 {return __c1 == __c2;}
Howard Hinnanta6119a82011-05-29 19:57:12 +0000531 _LIBCPP_INLINE_VISIBILITY
532 static int_type eof() _NOEXCEPT
533 {return int_type(EOF);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000534};
535
536template <class _CharT>
537int
538char_traits<_CharT>::compare(const char_type* __s1, const char_type* __s2, size_t __n)
539{
540 for (; __n; --__n, ++__s1, ++__s2)
541 {
542 if (lt(*__s1, *__s2))
543 return -1;
544 if (lt(*__s2, *__s1))
545 return 1;
546 }
547 return 0;
548}
549
550template <class _CharT>
551inline _LIBCPP_INLINE_VISIBILITY
552size_t
553char_traits<_CharT>::length(const char_type* __s)
554{
555 size_t __len = 0;
556 for (; !eq(*__s, char_type(0)); ++__s)
557 ++__len;
558 return __len;
559}
560
561template <class _CharT>
562inline _LIBCPP_INLINE_VISIBILITY
563const _CharT*
564char_traits<_CharT>::find(const char_type* __s, size_t __n, const char_type& __a)
565{
566 for (; __n; --__n)
567 {
568 if (eq(*__s, __a))
569 return __s;
570 ++__s;
571 }
572 return 0;
573}
574
575template <class _CharT>
576_CharT*
577char_traits<_CharT>::move(char_type* __s1, const char_type* __s2, size_t __n)
578{
579 char_type* __r = __s1;
580 if (__s1 < __s2)
581 {
582 for (; __n; --__n, ++__s1, ++__s2)
583 assign(*__s1, *__s2);
584 }
585 else if (__s2 < __s1)
586 {
587 __s1 += __n;
588 __s2 += __n;
589 for (; __n; --__n)
590 assign(*--__s1, *--__s2);
591 }
592 return __r;
593}
594
595template <class _CharT>
596inline _LIBCPP_INLINE_VISIBILITY
597_CharT*
598char_traits<_CharT>::copy(char_type* __s1, const char_type* __s2, size_t __n)
599{
600 char_type* __r = __s1;
601 for (; __n; --__n, ++__s1, ++__s2)
602 assign(*__s1, *__s2);
603 return __r;
604}
605
606template <class _CharT>
607inline _LIBCPP_INLINE_VISIBILITY
608_CharT*
609char_traits<_CharT>::assign(char_type* __s, size_t __n, char_type __a)
610{
611 char_type* __r = __s;
612 for (; __n; --__n, ++__s)
613 assign(*__s, __a);
614 return __r;
615}
616
617// char_traits<char>
618
619template <>
Howard Hinnant36cdf022010-09-10 16:42:26 +0000620struct _LIBCPP_VISIBLE char_traits<char>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000621{
622 typedef char char_type;
623 typedef int int_type;
624 typedef streamoff off_type;
625 typedef streampos pos_type;
626 typedef mbstate_t state_type;
627
Howard Hinnanta6119a82011-05-29 19:57:12 +0000628 _LIBCPP_INLINE_VISIBILITY
629 static void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT
630 {__c1 = __c2;}
631 _LIBCPP_INLINE_VISIBILITY
632 static bool eq(char_type __c1, char_type __c2) _NOEXCEPT
633 {return __c1 == __c2;}
634 _LIBCPP_INLINE_VISIBILITY
635 static 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
Howard Hinnanta6119a82011-05-29 19:57:12 +0000638 _LIBCPP_INLINE_VISIBILITY
639 static int compare(const char_type* __s1, const char_type* __s2, size_t __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000640 {return memcmp(__s1, __s2, __n);}
Howard Hinnanta6119a82011-05-29 19:57:12 +0000641 _LIBCPP_INLINE_VISIBILITY
642 static size_t length(const char_type* __s) {return strlen(__s);}
643 _LIBCPP_INLINE_VISIBILITY
644 static const char_type* find(const char_type* __s, size_t __n, const char_type& __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000645 {return (const char_type*)memchr(__s, to_int_type(__a), __n);}
Howard Hinnanta6119a82011-05-29 19:57:12 +0000646 _LIBCPP_INLINE_VISIBILITY
647 static char_type* move(char_type* __s1, const char_type* __s2, size_t __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000648 {return (char_type*)memmove(__s1, __s2, __n);}
Howard Hinnanta6119a82011-05-29 19:57:12 +0000649 _LIBCPP_INLINE_VISIBILITY
650 static char_type* copy(char_type* __s1, const char_type* __s2, size_t __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000651 {return (char_type*)memcpy(__s1, __s2, __n);}
Howard Hinnanta6119a82011-05-29 19:57:12 +0000652 _LIBCPP_INLINE_VISIBILITY
653 static char_type* assign(char_type* __s, size_t __n, char_type __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000654 {return (char_type*)memset(__s, to_int_type(__a), __n);}
655
Howard Hinnanta6119a82011-05-29 19:57:12 +0000656 _LIBCPP_INLINE_VISIBILITY static int_type not_eof(int_type __c) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000657 {return eq_int_type(__c, eof()) ? ~eof() : __c;}
Howard Hinnanta6119a82011-05-29 19:57:12 +0000658 _LIBCPP_INLINE_VISIBILITY
659 static char_type to_char_type(int_type __c) _NOEXCEPT
660 {return char_type(__c);}
661 _LIBCPP_INLINE_VISIBILITY
662 static int_type to_int_type(char_type __c) _NOEXCEPT
663 {return int_type((unsigned char)__c);}
664 _LIBCPP_INLINE_VISIBILITY
665 static bool eq_int_type(int_type __c1, int_type __c2) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000666 {return __c1 == __c2;}
Howard Hinnanta6119a82011-05-29 19:57:12 +0000667 _LIBCPP_INLINE_VISIBILITY
668 static int_type eof() _NOEXCEPT
669 {return int_type(EOF);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000670};
671
672// char_traits<wchar_t>
673
674template <>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000675struct _LIBCPP_VISIBLE char_traits<wchar_t>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000676{
677 typedef wchar_t char_type;
678 typedef wint_t int_type;
679 typedef streamoff off_type;
680 typedef streampos pos_type;
681 typedef mbstate_t state_type;
682
Howard Hinnanta6119a82011-05-29 19:57:12 +0000683 _LIBCPP_INLINE_VISIBILITY
684 static void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT
685 {__c1 = __c2;}
686 _LIBCPP_INLINE_VISIBILITY
687 static bool eq(char_type __c1, char_type __c2) _NOEXCEPT
688 {return __c1 == __c2;}
689 _LIBCPP_INLINE_VISIBILITY
690 static bool lt(char_type __c1, char_type __c2) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000691 {return __c1 < __c2;}
692
Howard Hinnanta6119a82011-05-29 19:57:12 +0000693 _LIBCPP_INLINE_VISIBILITY
694 static int compare(const char_type* __s1, const char_type* __s2, size_t __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000695 {return wmemcmp(__s1, __s2, __n);}
Howard Hinnanta6119a82011-05-29 19:57:12 +0000696 _LIBCPP_INLINE_VISIBILITY
697 static size_t length(const char_type* __s)
698 {return wcslen(__s);}
699 _LIBCPP_INLINE_VISIBILITY
700 static const char_type* find(const char_type* __s, size_t __n, const char_type& __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000701 {return (const char_type*)wmemchr(__s, __a, __n);}
Howard Hinnanta6119a82011-05-29 19:57:12 +0000702 _LIBCPP_INLINE_VISIBILITY
703 static char_type* move(char_type* __s1, const char_type* __s2, size_t __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000704 {return (char_type*)wmemmove(__s1, __s2, __n);}
Howard Hinnanta6119a82011-05-29 19:57:12 +0000705 _LIBCPP_INLINE_VISIBILITY
706 static char_type* copy(char_type* __s1, const char_type* __s2, size_t __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000707 {return (char_type*)wmemcpy(__s1, __s2, __n);}
Howard Hinnanta6119a82011-05-29 19:57:12 +0000708 _LIBCPP_INLINE_VISIBILITY
709 static char_type* assign(char_type* __s, size_t __n, char_type __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000710 {return (char_type*)wmemset(__s, __a, __n);}
711
Howard Hinnanta6119a82011-05-29 19:57:12 +0000712 _LIBCPP_INLINE_VISIBILITY
713 static int_type not_eof(int_type __c) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000714 {return eq_int_type(__c, eof()) ? ~eof() : __c;}
Howard Hinnanta6119a82011-05-29 19:57:12 +0000715 _LIBCPP_INLINE_VISIBILITY
716 static char_type to_char_type(int_type __c) _NOEXCEPT
717 {return char_type(__c);}
718 _LIBCPP_INLINE_VISIBILITY
719 static int_type to_int_type(char_type __c) _NOEXCEPT
720 {return int_type(__c);}
721 _LIBCPP_INLINE_VISIBILITY
722 static bool eq_int_type(int_type __c1, int_type __c2) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000723 {return __c1 == __c2;}
Howard Hinnanta6119a82011-05-29 19:57:12 +0000724 _LIBCPP_INLINE_VISIBILITY
725 static int_type eof() _NOEXCEPT
726 {return int_type(WEOF);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000727};
728
729#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
730
731template <>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000732struct _LIBCPP_VISIBLE char_traits<char16_t>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000733{
734 typedef char16_t char_type;
735 typedef uint_least16_t int_type;
736 typedef streamoff off_type;
737 typedef u16streampos pos_type;
738 typedef mbstate_t state_type;
739
Howard Hinnanta6119a82011-05-29 19:57:12 +0000740 _LIBCPP_INLINE_VISIBILITY
741 static void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT
742 {__c1 = __c2;}
743 _LIBCPP_INLINE_VISIBILITY
744 static bool eq(char_type __c1, char_type __c2) _NOEXCEPT
745 {return __c1 == __c2;}
746 _LIBCPP_INLINE_VISIBILITY
747 static bool lt(char_type __c1, char_type __c2) _NOEXCEPT
748 {return __c1 < __c2;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000749
750 static int compare(const char_type* __s1, const char_type* __s2, size_t __n);
751 static size_t length(const char_type* __s);
752 static const char_type* find(const char_type* __s, size_t __n, const char_type& __a);
753 static char_type* move(char_type* __s1, const char_type* __s2, size_t __n);
754 static char_type* copy(char_type* __s1, const char_type* __s2, size_t __n);
755 static char_type* assign(char_type* __s, size_t __n, char_type __a);
756
Howard Hinnanta6119a82011-05-29 19:57:12 +0000757 _LIBCPP_INLINE_VISIBILITY
758 static int_type not_eof(int_type __c) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000759 {return eq_int_type(__c, eof()) ? ~eof() : __c;}
Howard Hinnanta6119a82011-05-29 19:57:12 +0000760 _LIBCPP_INLINE_VISIBILITY
761 static char_type to_char_type(int_type __c) _NOEXCEPT
762 {return char_type(__c);}
763 _LIBCPP_INLINE_VISIBILITY
764 static int_type to_int_type(char_type __c) _NOEXCEPT
765 {return int_type(__c);}
766 _LIBCPP_INLINE_VISIBILITY
767 static bool eq_int_type(int_type __c1, int_type __c2) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000768 {return __c1 == __c2;}
Howard Hinnanta6119a82011-05-29 19:57:12 +0000769 _LIBCPP_INLINE_VISIBILITY
770 static int_type eof() _NOEXCEPT
771 {return int_type(0xDFFF);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000772};
773
774inline _LIBCPP_INLINE_VISIBILITY
775int
776char_traits<char16_t>::compare(const char_type* __s1, const char_type* __s2, size_t __n)
777{
778 for (; __n; --__n, ++__s1, ++__s2)
779 {
780 if (lt(*__s1, *__s2))
781 return -1;
782 if (lt(*__s2, *__s1))
783 return 1;
784 }
785 return 0;
786}
787
788inline _LIBCPP_INLINE_VISIBILITY
789size_t
790char_traits<char16_t>::length(const char_type* __s)
791{
792 size_t __len = 0;
793 for (; !eq(*__s, char_type(0)); ++__s)
794 ++__len;
795 return __len;
796}
797
798inline _LIBCPP_INLINE_VISIBILITY
799const char16_t*
800char_traits<char16_t>::find(const char_type* __s, size_t __n, const char_type& __a)
801{
802 for (; __n; --__n)
803 {
804 if (eq(*__s, __a))
805 return __s;
806 ++__s;
807 }
808 return 0;
809}
810
811inline _LIBCPP_INLINE_VISIBILITY
812char16_t*
813char_traits<char16_t>::move(char_type* __s1, const char_type* __s2, size_t __n)
814{
815 char_type* __r = __s1;
816 if (__s1 < __s2)
817 {
818 for (; __n; --__n, ++__s1, ++__s2)
819 assign(*__s1, *__s2);
820 }
821 else if (__s2 < __s1)
822 {
823 __s1 += __n;
824 __s2 += __n;
825 for (; __n; --__n)
826 assign(*--__s1, *--__s2);
827 }
828 return __r;
829}
830
831inline _LIBCPP_INLINE_VISIBILITY
832char16_t*
833char_traits<char16_t>::copy(char_type* __s1, const char_type* __s2, size_t __n)
834{
835 char_type* __r = __s1;
836 for (; __n; --__n, ++__s1, ++__s2)
837 assign(*__s1, *__s2);
838 return __r;
839}
840
841inline _LIBCPP_INLINE_VISIBILITY
842char16_t*
843char_traits<char16_t>::assign(char_type* __s, size_t __n, char_type __a)
844{
845 char_type* __r = __s;
846 for (; __n; --__n, ++__s)
847 assign(*__s, __a);
848 return __r;
849}
850
851template <>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000852struct _LIBCPP_VISIBLE char_traits<char32_t>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000853{
854 typedef char32_t char_type;
855 typedef uint_least32_t int_type;
856 typedef streamoff off_type;
857 typedef u32streampos pos_type;
858 typedef mbstate_t state_type;
859
Howard Hinnanta6119a82011-05-29 19:57:12 +0000860 _LIBCPP_INLINE_VISIBILITY
861 static void assign(char_type& __c1, const char_type& __c2) _NOEXCEPT
862 {__c1 = __c2;}
863 _LIBCPP_INLINE_VISIBILITY
864 static bool eq(char_type __c1, char_type __c2) _NOEXCEPT
865 {return __c1 == __c2;}
866 _LIBCPP_INLINE_VISIBILITY
867 static bool lt(char_type __c1, char_type __c2) _NOEXCEPT
868 {return __c1 < __c2;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000869
870 static int compare(const char_type* __s1, const char_type* __s2, size_t __n);
871 static size_t length(const char_type* __s);
872 static const char_type* find(const char_type* __s, size_t __n, const char_type& __a);
873 static char_type* move(char_type* __s1, const char_type* __s2, size_t __n);
874 static char_type* copy(char_type* __s1, const char_type* __s2, size_t __n);
875 static char_type* assign(char_type* __s, size_t __n, char_type __a);
876
Howard Hinnanta6119a82011-05-29 19:57:12 +0000877 _LIBCPP_INLINE_VISIBILITY
878 static int_type not_eof(int_type __c) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000879 {return eq_int_type(__c, eof()) ? ~eof() : __c;}
Howard Hinnanta6119a82011-05-29 19:57:12 +0000880 _LIBCPP_INLINE_VISIBILITY
881 static char_type to_char_type(int_type __c) _NOEXCEPT
882 {return char_type(__c);}
883 _LIBCPP_INLINE_VISIBILITY
884 static int_type to_int_type(char_type __c) _NOEXCEPT
885 {return int_type(__c);}
886 _LIBCPP_INLINE_VISIBILITY
887 static bool eq_int_type(int_type __c1, int_type __c2) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000888 {return __c1 == __c2;}
Howard Hinnanta6119a82011-05-29 19:57:12 +0000889 _LIBCPP_INLINE_VISIBILITY
890 static int_type eof() _NOEXCEPT
891 {return int_type(0xFFFFFFFF);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000892};
893
894inline _LIBCPP_INLINE_VISIBILITY
895int
896char_traits<char32_t>::compare(const char_type* __s1, const char_type* __s2, size_t __n)
897{
898 for (; __n; --__n, ++__s1, ++__s2)
899 {
900 if (lt(*__s1, *__s2))
901 return -1;
902 if (lt(*__s2, *__s1))
903 return 1;
904 }
905 return 0;
906}
907
908inline _LIBCPP_INLINE_VISIBILITY
909size_t
910char_traits<char32_t>::length(const char_type* __s)
911{
912 size_t __len = 0;
913 for (; !eq(*__s, char_type(0)); ++__s)
914 ++__len;
915 return __len;
916}
917
918inline _LIBCPP_INLINE_VISIBILITY
919const char32_t*
920char_traits<char32_t>::find(const char_type* __s, size_t __n, const char_type& __a)
921{
922 for (; __n; --__n)
923 {
924 if (eq(*__s, __a))
925 return __s;
926 ++__s;
927 }
928 return 0;
929}
930
931inline _LIBCPP_INLINE_VISIBILITY
932char32_t*
933char_traits<char32_t>::move(char_type* __s1, const char_type* __s2, size_t __n)
934{
935 char_type* __r = __s1;
936 if (__s1 < __s2)
937 {
938 for (; __n; --__n, ++__s1, ++__s2)
939 assign(*__s1, *__s2);
940 }
941 else if (__s2 < __s1)
942 {
943 __s1 += __n;
944 __s2 += __n;
945 for (; __n; --__n)
946 assign(*--__s1, *--__s2);
947 }
948 return __r;
949}
950
951inline _LIBCPP_INLINE_VISIBILITY
952char32_t*
953char_traits<char32_t>::copy(char_type* __s1, const char_type* __s2, size_t __n)
954{
955 char_type* __r = __s1;
956 for (; __n; --__n, ++__s1, ++__s2)
957 assign(*__s1, *__s2);
958 return __r;
959}
960
961inline _LIBCPP_INLINE_VISIBILITY
962char32_t*
963char_traits<char32_t>::assign(char_type* __s, size_t __n, char_type __a)
964{
965 char_type* __r = __s;
966 for (; __n; --__n, ++__s)
967 assign(*__s, __a);
968 return __r;
969}
970
Howard Hinnant324bb032010-08-22 00:02:43 +0000971#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000972
973// basic_string
974
975template<class _CharT, class _Traits, class _Allocator>
976basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000977operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
978 const basic_string<_CharT, _Traits, _Allocator>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000979
980template<class _CharT, class _Traits, class _Allocator>
981basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000982operator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000983
984template<class _CharT, class _Traits, class _Allocator>
985basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000986operator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000987
988template<class _CharT, class _Traits, class _Allocator>
989basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000990operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000991
992template<class _CharT, class _Traits, class _Allocator>
993basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000994operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000995
996template <bool>
997class __basic_string_common
998{
999protected:
1000 void __throw_length_error() const;
1001 void __throw_out_of_range() const;
1002};
1003
1004template <bool __b>
1005void
1006__basic_string_common<__b>::__throw_length_error() const
1007{
1008#ifndef _LIBCPP_NO_EXCEPTIONS
1009 throw length_error("basic_string");
1010#else
1011 assert(!"basic_string length_error");
1012#endif
1013}
1014
1015template <bool __b>
1016void
1017__basic_string_common<__b>::__throw_out_of_range() const
1018{
1019#ifndef _LIBCPP_NO_EXCEPTIONS
1020 throw out_of_range("basic_string");
1021#else
1022 assert(!"basic_string out_of_range");
1023#endif
1024}
1025
1026extern template class __basic_string_common<true>;
1027
Howard Hinnant324bb032010-08-22 00:02:43 +00001028template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant36cdf022010-09-10 16:42:26 +00001029class _LIBCPP_VISIBLE basic_string
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001030 : private __basic_string_common<true>
1031{
1032public:
1033 typedef basic_string __self;
1034 typedef _Traits traits_type;
1035 typedef typename traits_type::char_type value_type;
1036 typedef _Allocator allocator_type;
Howard Hinnante32b5e22010-11-17 17:55:08 +00001037 typedef allocator_traits<allocator_type> __alloc_traits;
1038 typedef typename __alloc_traits::size_type size_type;
1039 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001040 typedef value_type& reference;
1041 typedef const value_type& const_reference;
Howard Hinnante32b5e22010-11-17 17:55:08 +00001042 typedef typename __alloc_traits::pointer pointer;
1043 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001044#ifdef _LIBCPP_DEBUG
1045 typedef __debug_iter<basic_string, pointer> iterator;
1046 typedef __debug_iter<basic_string, const_pointer> const_iterator;
1047
1048 friend class __debug_iter<basic_string, pointer>;
1049 friend class __debug_iter<basic_string, const_pointer>;
1050#elif defined(_LIBCPP_RAW_ITERATORS)
1051 typedef pointer iterator;
1052 typedef const_pointer const_iterator;
Howard Hinnant324bb032010-08-22 00:02:43 +00001053#else // defined(_LIBCPP_RAW_ITERATORS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001054 typedef __wrap_iter<pointer> iterator;
1055 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnant324bb032010-08-22 00:02:43 +00001056#endif // defined(_LIBCPP_RAW_ITERATORS)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001057 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
1058 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001059
1060private:
1061 struct __long
1062 {
1063 size_type __cap_;
1064 size_type __size_;
1065 pointer __data_;
1066 };
1067
1068#if _LIBCPP_BIG_ENDIAN
1069 enum {__short_mask = 0x80};
1070 enum {__long_mask = ~(size_type(~0) >> 1)};
Howard Hinnant324bb032010-08-22 00:02:43 +00001071#else // _LIBCPP_BIG_ENDIAN
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001072 enum {__short_mask = 0x01};
1073 enum {__long_mask = 0x1};
Howard Hinnant324bb032010-08-22 00:02:43 +00001074#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001075
1076 enum {__mask = size_type(~0) >> 1};
1077
1078 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
1079 (sizeof(__long) - 1)/sizeof(value_type) : 2};
1080
1081 struct __short
1082 {
1083 union
1084 {
1085 unsigned char __size_;
1086 value_type _;
1087 };
1088 value_type __data_[__min_cap];
1089 };
1090
1091 union _{__long _; __short __;};
1092
1093 enum {__n_words = sizeof(_) / sizeof(size_type)};
1094
1095 struct __raw
1096 {
1097 size_type __words[__n_words];
1098 };
1099
1100 struct __rep
1101 {
1102 union
1103 {
1104 __long __l;
1105 __short __s;
1106 __raw __r;
1107 };
1108 };
1109
1110 __compressed_pair<__rep, allocator_type> __r_;
1111
1112#ifdef _LIBCPP_DEBUG
1113
1114 pair<iterator*, const_iterator*> __iterator_list_;
1115
1116 _LIBCPP_INLINE_VISIBILITY iterator*& __get_iterator_list(iterator*) {return __iterator_list_.first;}
1117 _LIBCPP_INLINE_VISIBILITY const_iterator*& __get_iterator_list(const_iterator*) {return __iterator_list_.second;}
1118
1119#endif // _LIBCPP_DEBUG
1120
1121public:
1122 static const size_type npos = -1;
1123
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001124 _LIBCPP_INLINE_VISIBILITY basic_string()
1125 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001126 _LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001127 basic_string(const basic_string& __str);
1128 basic_string(const basic_string& __str, const allocator_type& __a);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001129#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant9f193f22011-01-26 00:06:59 +00001130 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001131 basic_string(basic_string&& __str)
1132 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnant9f193f22011-01-26 00:06:59 +00001133 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001134 basic_string(basic_string&& __str, const allocator_type& __a);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001135#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001136 _LIBCPP_INLINE_VISIBILITY basic_string(const_pointer __s);
1137 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001138 basic_string(const_pointer __s, const allocator_type& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001139 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001140 basic_string(const_pointer __s, size_type __n);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001141 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001142 basic_string(const_pointer __s, size_type __n, const allocator_type& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001143 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001144 basic_string(size_type __n, value_type __c);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001145 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001146 basic_string(size_type __n, value_type __c, const allocator_type& __a);
1147 basic_string(const basic_string& __str, size_type __pos, size_type __n = npos,
1148 const allocator_type& __a = allocator_type());
1149 template<class _InputIterator>
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001150 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001151 basic_string(_InputIterator __first, _InputIterator __last);
1152 template<class _InputIterator>
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001153 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001154 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +00001155#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001156 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001157 basic_string(initializer_list<value_type> __il);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001158 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001159 basic_string(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +00001160#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001161
1162 ~basic_string();
1163
Howard Hinnante32b5e22010-11-17 17:55:08 +00001164 basic_string& operator=(const basic_string& __str);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001165#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001166 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001167 basic_string& operator=(basic_string&& __str)
1168 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
1169 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001170#endif
Howard Hinnanta6119a82011-05-29 19:57:12 +00001171 _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const_pointer __s) {return assign(__s);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001172 basic_string& operator=(value_type __c);
Howard Hinnante3e32912011-08-12 21:56:02 +00001173#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant8d7a9552010-09-23 17:31:07 +00001174 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001175 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnante3e32912011-08-12 21:56:02 +00001176#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001177
1178#ifndef _LIBCPP_DEBUG
Howard Hinnanta6119a82011-05-29 19:57:12 +00001179 _LIBCPP_INLINE_VISIBILITY
1180 iterator begin() _NOEXCEPT
1181 {return iterator(__get_pointer());}
1182 _LIBCPP_INLINE_VISIBILITY
1183 const_iterator begin() const _NOEXCEPT
1184 {return const_iterator(data());}
1185 _LIBCPP_INLINE_VISIBILITY
1186 iterator end() _NOEXCEPT
1187 {return iterator(__get_pointer() + size());}
1188 _LIBCPP_INLINE_VISIBILITY
1189 const_iterator end() const _NOEXCEPT
1190 {return const_iterator(data() + size());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001191#else // _LIBCPP_DEBUG
1192 _LIBCPP_INLINE_VISIBILITY iterator begin() {return iterator(this, __get_pointer());}
1193 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const {return const_iterator(this, data());}
1194 _LIBCPP_INLINE_VISIBILITY iterator end() {return iterator(this, __get_pointer() + size());}
1195 _LIBCPP_INLINE_VISIBILITY const_iterator end() const {return const_iterator(this, data() + size());}
1196#endif // _LIBCPP_DEBUG
Howard Hinnanta6119a82011-05-29 19:57:12 +00001197 _LIBCPP_INLINE_VISIBILITY
1198 reverse_iterator rbegin() _NOEXCEPT
1199 {return reverse_iterator(end());}
1200 _LIBCPP_INLINE_VISIBILITY
1201 const_reverse_iterator rbegin() const _NOEXCEPT
1202 {return const_reverse_iterator(end());}
1203 _LIBCPP_INLINE_VISIBILITY
1204 reverse_iterator rend() _NOEXCEPT
1205 {return reverse_iterator(begin());}
1206 _LIBCPP_INLINE_VISIBILITY
1207 const_reverse_iterator rend() const _NOEXCEPT
1208 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001209
Howard Hinnanta6119a82011-05-29 19:57:12 +00001210 _LIBCPP_INLINE_VISIBILITY
1211 const_iterator cbegin() const _NOEXCEPT
1212 {return begin();}
1213 _LIBCPP_INLINE_VISIBILITY
1214 const_iterator cend() const _NOEXCEPT
1215 {return end();}
1216 _LIBCPP_INLINE_VISIBILITY
1217 const_reverse_iterator crbegin() const _NOEXCEPT
1218 {return rbegin();}
1219 _LIBCPP_INLINE_VISIBILITY
1220 const_reverse_iterator crend() const _NOEXCEPT
1221 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001222
Howard Hinnanta6119a82011-05-29 19:57:12 +00001223 _LIBCPP_INLINE_VISIBILITY size_type size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001224 {return __is_long() ? __get_long_size() : __get_short_size();}
Howard Hinnanta6119a82011-05-29 19:57:12 +00001225 _LIBCPP_INLINE_VISIBILITY size_type length() const _NOEXCEPT {return size();}
1226 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT;
1227 _LIBCPP_INLINE_VISIBILITY size_type capacity() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001228 {return (__is_long() ? __get_long_cap() : __min_cap) - 1;}
1229
1230 void resize(size_type __n, value_type __c);
1231 _LIBCPP_INLINE_VISIBILITY void resize(size_type __n) {resize(__n, value_type());}
1232
1233 void reserve(size_type res_arg = 0);
Howard Hinnant8d7a9552010-09-23 17:31:07 +00001234 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001235 void shrink_to_fit() _NOEXCEPT {reserve();}
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001236 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001237 void clear() _NOEXCEPT;
1238 _LIBCPP_INLINE_VISIBILITY bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001239
1240 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __pos) const;
1241 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __pos);
1242
1243 const_reference at(size_type __n) const;
1244 reference at(size_type __n);
1245
1246 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const basic_string& __str) {return append(__str);}
1247 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const_pointer __s) {return append(__s);}
1248 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(value_type __c) {push_back(__c); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02 +00001249#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001250 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(initializer_list<value_type> __il) {return append(__il);}
Howard Hinnante3e32912011-08-12 21:56:02 +00001251#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001252
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001253 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001254 basic_string& append(const basic_string& __str);
1255 basic_string& append(const basic_string& __str, size_type __pos, size_type __n);
1256 basic_string& append(const_pointer __s, size_type __n);
1257 basic_string& append(const_pointer __s);
1258 basic_string& append(size_type __n, value_type __c);
1259 template<class _InputIterator>
1260 typename enable_if
1261 <
1262 __is_input_iterator <_InputIterator>::value &&
1263 !__is_forward_iterator<_InputIterator>::value,
1264 basic_string&
1265 >::type
1266 append(_InputIterator __first, _InputIterator __last);
1267 template<class _ForwardIterator>
1268 typename enable_if
1269 <
1270 __is_forward_iterator<_ForwardIterator>::value,
1271 basic_string&
1272 >::type
1273 append(_ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:02 +00001274#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant8d7a9552010-09-23 17:31:07 +00001275 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001276 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Howard Hinnante3e32912011-08-12 21:56:02 +00001277#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001278
1279 void push_back(value_type __c);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001280 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001281 void pop_back();
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001282 _LIBCPP_INLINE_VISIBILITY reference front();
1283 _LIBCPP_INLINE_VISIBILITY const_reference front() const;
1284 _LIBCPP_INLINE_VISIBILITY reference back();
1285 _LIBCPP_INLINE_VISIBILITY const_reference back() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001286
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001287 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001288 basic_string& assign(const basic_string& __str);
Howard Hinnanta6119a82011-05-29 19:57:12 +00001289#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1290 _LIBCPP_INLINE_VISIBILITY
1291 basic_string& assign(basic_string&& str)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001292 {*this = _VSTD::move(str); return *this;}
Howard Hinnanta6119a82011-05-29 19:57:12 +00001293#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001294 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n);
1295 basic_string& assign(const_pointer __s, size_type __n);
1296 basic_string& assign(const_pointer __s);
1297 basic_string& assign(size_type __n, value_type __c);
1298 template<class _InputIterator>
1299 typename enable_if
1300 <
1301 __is_input_iterator <_InputIterator>::value &&
1302 !__is_forward_iterator<_InputIterator>::value,
1303 basic_string&
1304 >::type
1305 assign(_InputIterator __first, _InputIterator __last);
1306 template<class _ForwardIterator>
1307 typename enable_if
1308 <
1309 __is_forward_iterator<_ForwardIterator>::value,
1310 basic_string&
1311 >::type
1312 assign(_ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:02 +00001313#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant8d7a9552010-09-23 17:31:07 +00001314 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001315 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnante3e32912011-08-12 21:56:02 +00001316#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001317
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001318 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001319 basic_string& insert(size_type __pos1, const basic_string& __str);
1320 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n);
1321 basic_string& insert(size_type __pos, const_pointer __s, size_type __n);
1322 basic_string& insert(size_type __pos, const_pointer __s);
1323 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1324 iterator insert(const_iterator __pos, value_type __c);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001325 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001326 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1327 template<class _InputIterator>
1328 typename enable_if
1329 <
1330 __is_input_iterator <_InputIterator>::value &&
1331 !__is_forward_iterator<_InputIterator>::value,
1332 iterator
1333 >::type
1334 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1335 template<class _ForwardIterator>
1336 typename enable_if
1337 <
1338 __is_forward_iterator<_ForwardIterator>::value,
1339 iterator
1340 >::type
1341 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnante3e32912011-08-12 21:56:02 +00001342#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant8d7a9552010-09-23 17:31:07 +00001343 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001344 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1345 {return insert(__pos, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00001346#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001347
1348 basic_string& erase(size_type __pos = 0, size_type __n = npos);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001349 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001350 iterator erase(const_iterator __pos);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001351 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001352 iterator erase(const_iterator __first, const_iterator __last);
1353
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001354 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001355 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
1356 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2);
1357 basic_string& replace(size_type __pos, size_type __n1, const_pointer __s, size_type __n2);
1358 basic_string& replace(size_type __pos, size_type __n1, const_pointer __s);
1359 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001360 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7b2cb482010-11-17 21:11:40 +00001361 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001362 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7b2cb482010-11-17 21:11:40 +00001363 basic_string& replace(const_iterator __i1, const_iterator __i2, const_pointer __s, size_type __n);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001364 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7b2cb482010-11-17 21:11:40 +00001365 basic_string& replace(const_iterator __i1, const_iterator __i2, const_pointer __s);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001366 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7b2cb482010-11-17 21:11:40 +00001367 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001368 template<class _InputIterator>
1369 typename enable_if
1370 <
1371 __is_input_iterator<_InputIterator>::value,
1372 basic_string&
1373 >::type
Howard Hinnant7b2cb482010-11-17 21:11:40 +00001374 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Howard Hinnante3e32912011-08-12 21:56:02 +00001375#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant8d7a9552010-09-23 17:31:07 +00001376 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7b2cb482010-11-17 21:11:40 +00001377 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001378 {return replace(__i1, __i2, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +00001379#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001380
1381 size_type copy(pointer __s, size_type __n, size_type __pos = 0) const;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001382 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001383 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1384
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001385 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001386 void swap(basic_string& __str)
1387 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
1388 __is_nothrow_swappable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001389
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001390 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001391 const_pointer c_str() const _NOEXCEPT {return data();}
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001392 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001393 const_pointer data() const _NOEXCEPT {return __get_pointer();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001394
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001395 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001396 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001397
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001398 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001399 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
1400 size_type find(const_pointer __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001401 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001402 size_type find(const_pointer __s, size_type __pos = 0) const _NOEXCEPT;
1403 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001404
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001405 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001406 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
1407 size_type rfind(const_pointer __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001408 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001409 size_type rfind(const_pointer __s, size_type __pos = npos) const _NOEXCEPT;
1410 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001411
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001412 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001413 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
1414 size_type find_first_of(const_pointer __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001415 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001416 size_type find_first_of(const_pointer __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001417 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001418 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001419
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001420 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001421 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
1422 size_type find_last_of(const_pointer __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001423 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001424 size_type find_last_of(const_pointer __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001425 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001426 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001427
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001428 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001429 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
1430 size_type find_first_not_of(const_pointer __s, size_type __pos, size_type __n) const _NOEXCEPT;
1431 _LIBCPP_INLINE_VISIBILITY
1432 size_type find_first_not_of(const_pointer __s, size_type __pos = 0) const _NOEXCEPT;
1433 _LIBCPP_INLINE_VISIBILITY
1434 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1435
1436 _LIBCPP_INLINE_VISIBILITY
1437 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
1438 size_type find_last_not_of(const_pointer __s, size_type __pos, size_type __n) const _NOEXCEPT;
1439 _LIBCPP_INLINE_VISIBILITY
1440 size_type find_last_not_of(const_pointer __s, size_type __pos = npos) const _NOEXCEPT;
1441 _LIBCPP_INLINE_VISIBILITY
1442 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1443
1444 _LIBCPP_INLINE_VISIBILITY
1445 int compare(const basic_string& __str) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001446 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001447 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
1448 int compare(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2) const;
Howard Hinnanta6119a82011-05-29 19:57:12 +00001449 int compare(const_pointer __s) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001450 int compare(size_type __pos1, size_type __n1, const_pointer __s) const;
1451 int compare(size_type __pos1, size_type __n1, const_pointer __s, size_type __n2) const;
1452
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001453 _LIBCPP_INLINE_VISIBILITY bool __invariants() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001454private:
Howard Hinnanta6119a82011-05-29 19:57:12 +00001455 _LIBCPP_INLINE_VISIBILITY
1456 allocator_type& __alloc() _NOEXCEPT
1457 {return __r_.second();}
1458 _LIBCPP_INLINE_VISIBILITY
1459 const allocator_type& __alloc() const _NOEXCEPT
1460 {return __r_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001461
Howard Hinnanta6119a82011-05-29 19:57:12 +00001462 _LIBCPP_INLINE_VISIBILITY
1463 bool __is_long() const _NOEXCEPT
1464 {return bool(__r_.first().__s.__size_ & __short_mask);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001465
Howard Hinnanta6119a82011-05-29 19:57:12 +00001466 _LIBCPP_INLINE_VISIBILITY
1467 void __set_short_size(size_type __s) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001468#if _LIBCPP_BIG_ENDIAN
1469 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1470#else
1471 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
1472#endif
Howard Hinnanta6119a82011-05-29 19:57:12 +00001473 _LIBCPP_INLINE_VISIBILITY
1474 size_type __get_short_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001475#if _LIBCPP_BIG_ENDIAN
1476 {return __r_.first().__s.__size_;}
1477#else
1478 {return __r_.first().__s.__size_ >> 1;}
1479#endif
Howard Hinnanta6119a82011-05-29 19:57:12 +00001480 _LIBCPP_INLINE_VISIBILITY
1481 void __set_long_size(size_type __s) _NOEXCEPT
1482 {__r_.first().__l.__size_ = __s;}
1483 _LIBCPP_INLINE_VISIBILITY
1484 size_type __get_long_size() const _NOEXCEPT
1485 {return __r_.first().__l.__size_;}
1486 _LIBCPP_INLINE_VISIBILITY
1487 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001488 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1489
Howard Hinnanta6119a82011-05-29 19:57:12 +00001490 _LIBCPP_INLINE_VISIBILITY
1491 void __set_long_cap(size_type __s) _NOEXCEPT
1492 {__r_.first().__l.__cap_ = __long_mask | __s;}
1493 _LIBCPP_INLINE_VISIBILITY
1494 size_type __get_long_cap() const _NOEXCEPT
1495 {return __r_.first().__l.__cap_ & ~__long_mask;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001496
Howard Hinnanta6119a82011-05-29 19:57:12 +00001497 _LIBCPP_INLINE_VISIBILITY
1498 void __set_long_pointer(pointer __p) _NOEXCEPT
1499 {__r_.first().__l.__data_ = __p;}
1500 _LIBCPP_INLINE_VISIBILITY
1501 pointer __get_long_pointer() _NOEXCEPT
1502 {return __r_.first().__l.__data_;}
1503 _LIBCPP_INLINE_VISIBILITY
1504 const_pointer __get_long_pointer() const _NOEXCEPT
1505 {return __r_.first().__l.__data_;}
1506 _LIBCPP_INLINE_VISIBILITY
1507 pointer __get_short_pointer() _NOEXCEPT
1508 {return __r_.first().__s.__data_;}
1509 _LIBCPP_INLINE_VISIBILITY
1510 const_pointer __get_short_pointer() const _NOEXCEPT
1511 {return __r_.first().__s.__data_;}
1512 _LIBCPP_INLINE_VISIBILITY
1513 pointer __get_pointer() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001514 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Howard Hinnanta6119a82011-05-29 19:57:12 +00001515 _LIBCPP_INLINE_VISIBILITY
1516 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001517 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1518
Howard Hinnanta6119a82011-05-29 19:57:12 +00001519 _LIBCPP_INLINE_VISIBILITY
1520 void __zero() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001521 {
1522 size_type (&__a)[__n_words] = __r_.first().__r.__words;
1523 for (unsigned __i = 0; __i < __n_words; ++__i)
1524 __a[__i] = 0;
1525 }
1526
1527 template <size_type __a> static
Howard Hinnanta6119a82011-05-29 19:57:12 +00001528 _LIBCPP_INLINE_VISIBILITY
1529 size_type __align(size_type __s) _NOEXCEPT
1530 {return __s + (__a-1) & ~(__a-1);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001531 enum {__alignment = 16};
Howard Hinnanta6119a82011-05-29 19:57:12 +00001532 static _LIBCPP_INLINE_VISIBILITY
1533 size_type __recommend(size_type __s) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001534 {return (__s < __min_cap ? __min_cap :
Howard Hinnanta6119a82011-05-29 19:57:12 +00001535 __align<sizeof(value_type) < __alignment ?
1536 __alignment/sizeof(value_type) : 1 > (__s+1)) - 1;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001537
1538 void __init(const_pointer __s, size_type __sz, size_type __reserve);
1539 void __init(const_pointer __s, size_type __sz);
1540 void __init(size_type __n, value_type __c);
Howard Hinnant324bb032010-08-22 00:02:43 +00001541
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001542 template <class _InputIterator>
1543 typename enable_if
1544 <
1545 __is_input_iterator <_InputIterator>::value &&
1546 !__is_forward_iterator<_InputIterator>::value,
1547 void
1548 >::type
1549 __init(_InputIterator __first, _InputIterator __last);
1550
1551 template <class _ForwardIterator>
1552 typename enable_if
1553 <
1554 __is_forward_iterator<_ForwardIterator>::value,
1555 void
1556 >::type
1557 __init(_ForwardIterator __first, _ForwardIterator __last);
1558
1559 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant324bb032010-08-22 00:02:43 +00001560 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001561 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1562 size_type __n_copy, size_type __n_del,
1563 size_type __n_add, const_pointer __p_new_stuff);
1564
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001565 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001566 void __erase_to_end(size_type __pos);
1567
Howard Hinnante32b5e22010-11-17 17:55:08 +00001568 _LIBCPP_INLINE_VISIBILITY
1569 void __copy_assign_alloc(const basic_string& __str)
1570 {__copy_assign_alloc(__str, integral_constant<bool,
1571 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1572
1573 _LIBCPP_INLINE_VISIBILITY
1574 void __copy_assign_alloc(const basic_string& __str, true_type)
1575 {
1576 if (__alloc() != __str.__alloc())
1577 {
1578 clear();
1579 shrink_to_fit();
1580 }
1581 __alloc() = __str.__alloc();
1582 }
1583
1584 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001585 void __copy_assign_alloc(const basic_string& __str, false_type) _NOEXCEPT
Howard Hinnante32b5e22010-11-17 17:55:08 +00001586 {}
1587
1588#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001589 _LIBCPP_INLINE_VISIBILITY
Howard Hinnante32b5e22010-11-17 17:55:08 +00001590 void __move_assign(basic_string& __str, false_type);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001591 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001592 void __move_assign(basic_string& __str, true_type)
1593 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001594#endif
1595
1596 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00001597 void
Howard Hinnant9cbee432011-09-02 20:42:31 +00001598 __move_assign_alloc(basic_string& __str)
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00001599 _NOEXCEPT_(
1600 !__alloc_traits::propagate_on_container_move_assignment::value ||
1601 is_nothrow_move_assignable<allocator_type>::value)
1602 {__move_assign_alloc(__str, integral_constant<bool,
1603 __alloc_traits::propagate_on_container_move_assignment::value>());}
1604
1605 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +00001606 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00001607 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1608 {
1609 __alloc() = _VSTD::move(__c.__alloc());
1610 }
1611
1612 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +00001613 void __move_assign_alloc(basic_string& __c, false_type)
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00001614 _NOEXCEPT
1615 {}
1616
1617 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001618 static void __swap_alloc(allocator_type& __x, allocator_type& __y)
1619 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
1620 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnante32b5e22010-11-17 17:55:08 +00001621 {__swap_alloc(__x, __y, integral_constant<bool,
1622 __alloc_traits::propagate_on_container_swap::value>());}
1623
1624 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001625 static void __swap_alloc(allocator_type& __x, allocator_type& __y, true_type)
1626 _NOEXCEPT_(__is_nothrow_swappable<allocator_type>::value)
Howard Hinnante32b5e22010-11-17 17:55:08 +00001627 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001628 using _VSTD::swap;
Howard Hinnante32b5e22010-11-17 17:55:08 +00001629 swap(__x, __y);
1630 }
1631 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001632 static void __swap_alloc(allocator_type& __x, allocator_type& __y, false_type) _NOEXCEPT
Howard Hinnante32b5e22010-11-17 17:55:08 +00001633 {}
1634
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001635 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
1636 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(size_type);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001637
1638 friend basic_string operator+<>(const basic_string&, const basic_string&);
1639 friend basic_string operator+<>(const value_type*, const basic_string&);
1640 friend basic_string operator+<>(value_type, const basic_string&);
1641 friend basic_string operator+<>(const basic_string&, const value_type*);
1642 friend basic_string operator+<>(const basic_string&, value_type);
1643};
1644
1645template <class _CharT, class _Traits, class _Allocator>
1646#ifndef _LIBCPP_DEBUG
1647_LIBCPP_INLINE_VISIBILITY inline
1648#endif
1649void
1650basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
1651{
1652#ifdef _LIBCPP_DEBUG
1653 iterator::__remove_all(this);
1654 const_iterator::__remove_all(this);
Howard Hinnant324bb032010-08-22 00:02:43 +00001655#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001656}
1657
1658template <class _CharT, class _Traits, class _Allocator>
1659#ifndef _LIBCPP_DEBUG
1660_LIBCPP_INLINE_VISIBILITY inline
1661#endif
1662void
1663basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type __pos)
1664{
1665#ifdef _LIBCPP_DEBUG
1666 const_iterator __beg = begin();
1667 if (__iterator_list_.first)
1668 {
1669 for (iterator* __p = __iterator_list_.first; __p;)
1670 {
1671 if (*__p - __beg > static_cast<difference_type>(__pos))
1672 {
1673 iterator* __n = __p;
1674 __p = __p->__next;
1675 __n->__remove_owner();
1676 }
1677 else
1678 __p = __p->__next;
1679 }
1680 }
1681 if (__iterator_list_.second)
1682 {
1683 for (const_iterator* __p = __iterator_list_.second; __p;)
1684 {
1685 if (*__p - __beg > static_cast<difference_type>(__pos))
1686 {
1687 const_iterator* __n = __p;
1688 __p = __p->__next;
1689 __n->__remove_owner();
1690 }
1691 else
1692 __p = __p->__next;
1693 }
1694 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001695#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001696}
1697
1698template <class _CharT, class _Traits, class _Allocator>
1699_LIBCPP_INLINE_VISIBILITY inline
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001700basic_string<_CharT, _Traits, _Allocator>::basic_string()
1701 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001702{
1703 __zero();
1704}
1705
1706template <class _CharT, class _Traits, class _Allocator>
1707_LIBCPP_INLINE_VISIBILITY inline
1708basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
1709 : __r_(__a)
1710{
1711 __zero();
1712}
1713
1714template <class _CharT, class _Traits, class _Allocator>
1715void
1716basic_string<_CharT, _Traits, _Allocator>::__init(const_pointer __s, size_type __sz, size_type __reserve)
1717{
1718 if (__reserve > max_size())
1719 this->__throw_length_error();
1720 pointer __p;
1721 if (__reserve < __min_cap)
1722 {
1723 __set_short_size(__sz);
1724 __p = __get_short_pointer();
1725 }
1726 else
1727 {
1728 size_type __cap = __recommend(__reserve);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001729 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001730 __set_long_pointer(__p);
1731 __set_long_cap(__cap+1);
1732 __set_long_size(__sz);
1733 }
1734 traits_type::copy(__p, __s, __sz);
1735 traits_type::assign(__p[__sz], value_type());
1736}
1737
1738template <class _CharT, class _Traits, class _Allocator>
1739void
1740basic_string<_CharT, _Traits, _Allocator>::__init(const_pointer __s, size_type __sz)
1741{
1742 if (__sz > max_size())
1743 this->__throw_length_error();
1744 pointer __p;
1745 if (__sz < __min_cap)
1746 {
1747 __set_short_size(__sz);
1748 __p = __get_short_pointer();
1749 }
1750 else
1751 {
1752 size_type __cap = __recommend(__sz);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001753 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001754 __set_long_pointer(__p);
1755 __set_long_cap(__cap+1);
1756 __set_long_size(__sz);
1757 }
1758 traits_type::copy(__p, __s, __sz);
1759 traits_type::assign(__p[__sz], value_type());
1760}
1761
1762template <class _CharT, class _Traits, class _Allocator>
1763_LIBCPP_INLINE_VISIBILITY inline
1764basic_string<_CharT, _Traits, _Allocator>::basic_string(const_pointer __s)
1765{
1766#ifdef _LIBCPP_DEBUG
1767 assert(__s != 0);
1768#endif
1769 __init(__s, traits_type::length(__s));
1770}
1771
1772template <class _CharT, class _Traits, class _Allocator>
1773_LIBCPP_INLINE_VISIBILITY inline
1774basic_string<_CharT, _Traits, _Allocator>::basic_string(const_pointer __s, const allocator_type& __a)
1775 : __r_(__a)
1776{
1777#ifdef _LIBCPP_DEBUG
1778 assert(__s != 0);
1779#endif
1780 __init(__s, traits_type::length(__s));
1781}
1782
1783template <class _CharT, class _Traits, class _Allocator>
1784_LIBCPP_INLINE_VISIBILITY inline
1785basic_string<_CharT, _Traits, _Allocator>::basic_string(const_pointer __s, size_type __n)
1786{
1787#ifdef _LIBCPP_DEBUG
1788 assert(__s != 0);
1789#endif
1790 __init(__s, __n);
1791}
1792
1793template <class _CharT, class _Traits, class _Allocator>
1794_LIBCPP_INLINE_VISIBILITY inline
1795basic_string<_CharT, _Traits, _Allocator>::basic_string(const_pointer __s, size_type __n, const allocator_type& __a)
1796 : __r_(__a)
1797{
1798#ifdef _LIBCPP_DEBUG
1799 assert(__s != 0);
1800#endif
1801 __init(__s, __n);
1802}
1803
1804template <class _CharT, class _Traits, class _Allocator>
1805basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Howard Hinnante32b5e22010-11-17 17:55:08 +00001806 : __r_(__alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001807{
1808 if (!__str.__is_long())
1809 __r_.first().__r = __str.__r_.first().__r;
1810 else
1811 __init(__str.__get_long_pointer(), __str.__get_long_size());
1812}
1813
1814template <class _CharT, class _Traits, class _Allocator>
1815basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, const allocator_type& __a)
1816 : __r_(__a)
1817{
1818 if (!__str.__is_long())
1819 __r_.first().__r = __str.__r_.first().__r;
1820 else
1821 __init(__str.__get_long_pointer(), __str.__get_long_size());
1822}
1823
Howard Hinnant73d21a42010-09-04 23:28:19 +00001824#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001825
1826template <class _CharT, class _Traits, class _Allocator>
1827_LIBCPP_INLINE_VISIBILITY inline
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001828basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
1829 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001830 : __r_(_VSTD::move(__str.__r_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001831{
1832 __str.__zero();
1833#ifdef _LIBCPP_DEBUG
1834 __str.__invalidate_all_iterators();
1835#endif
1836}
1837
1838template <class _CharT, class _Traits, class _Allocator>
1839_LIBCPP_INLINE_VISIBILITY inline
1840basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Howard Hinnante32b5e22010-11-17 17:55:08 +00001841 : __r_(__a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001842{
Howard Hinnante32b5e22010-11-17 17:55:08 +00001843 if (__a == __str.__alloc() || !__str.__is_long())
1844 __r_.first().__r = __str.__r_.first().__r;
1845 else
1846 __init(__str.__get_long_pointer(), __str.__get_long_size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001847 __str.__zero();
1848#ifdef _LIBCPP_DEBUG
1849 __str.__invalidate_all_iterators();
1850#endif
1851}
1852
Howard Hinnant73d21a42010-09-04 23:28:19 +00001853#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001854
1855template <class _CharT, class _Traits, class _Allocator>
1856void
1857basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
1858{
1859 if (__n > max_size())
1860 this->__throw_length_error();
1861 pointer __p;
1862 if (__n < __min_cap)
1863 {
1864 __set_short_size(__n);
1865 __p = __get_short_pointer();
1866 }
1867 else
1868 {
1869 size_type __cap = __recommend(__n);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001870 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001871 __set_long_pointer(__p);
1872 __set_long_cap(__cap+1);
1873 __set_long_size(__n);
1874 }
1875 traits_type::assign(__p, __n, __c);
1876 traits_type::assign(__p[__n], value_type());
1877}
1878
1879template <class _CharT, class _Traits, class _Allocator>
1880_LIBCPP_INLINE_VISIBILITY inline
1881basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, value_type __c)
1882{
1883 __init(__n, __c);
1884}
1885
1886template <class _CharT, class _Traits, class _Allocator>
1887_LIBCPP_INLINE_VISIBILITY inline
1888basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, value_type __c, const allocator_type& __a)
1889 : __r_(__a)
1890{
1891 __init(__n, __c);
1892}
1893
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001894template <class _CharT, class _Traits, class _Allocator>
1895basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos, size_type __n,
1896 const allocator_type& __a)
1897 : __r_(__a)
1898{
1899 size_type __str_sz = __str.size();
1900 if (__pos > __str_sz)
1901 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001902 __init(__str.data() + __pos, _VSTD::min(__n, __str_sz - __pos));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001903}
1904
1905template <class _CharT, class _Traits, class _Allocator>
1906template <class _InputIterator>
1907typename enable_if
1908<
1909 __is_input_iterator <_InputIterator>::value &&
1910 !__is_forward_iterator<_InputIterator>::value,
1911 void
1912>::type
1913basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
1914{
1915 __zero();
1916#ifndef _LIBCPP_NO_EXCEPTIONS
1917 try
1918 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001919#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001920 for (; __first != __last; ++__first)
1921 push_back(*__first);
1922#ifndef _LIBCPP_NO_EXCEPTIONS
1923 }
1924 catch (...)
1925 {
1926 if (__is_long())
Howard Hinnante32b5e22010-11-17 17:55:08 +00001927 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001928 throw;
1929 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001930#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001931}
1932
1933template <class _CharT, class _Traits, class _Allocator>
1934template <class _ForwardIterator>
1935typename enable_if
1936<
1937 __is_forward_iterator<_ForwardIterator>::value,
1938 void
1939>::type
1940basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
1941{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001942 size_type __sz = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001943 if (__sz > max_size())
1944 this->__throw_length_error();
1945 pointer __p;
1946 if (__sz < __min_cap)
1947 {
1948 __set_short_size(__sz);
1949 __p = __get_short_pointer();
1950 }
1951 else
1952 {
1953 size_type __cap = __recommend(__sz);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001954 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001955 __set_long_pointer(__p);
1956 __set_long_cap(__cap+1);
1957 __set_long_size(__sz);
1958 }
1959 for (; __first != __last; ++__first, ++__p)
1960 traits_type::assign(*__p, *__first);
1961 traits_type::assign(*__p, value_type());
1962}
1963
1964template <class _CharT, class _Traits, class _Allocator>
1965template<class _InputIterator>
1966_LIBCPP_INLINE_VISIBILITY inline
1967basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
1968{
1969 __init(__first, __last);
1970}
1971
1972template <class _CharT, class _Traits, class _Allocator>
1973template<class _InputIterator>
1974_LIBCPP_INLINE_VISIBILITY inline
1975basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
1976 const allocator_type& __a)
1977 : __r_(__a)
1978{
1979 __init(__first, __last);
1980}
1981
Howard Hinnante3e32912011-08-12 21:56:02 +00001982#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
1983
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001984template <class _CharT, class _Traits, class _Allocator>
1985_LIBCPP_INLINE_VISIBILITY inline
1986basic_string<_CharT, _Traits, _Allocator>::basic_string(initializer_list<value_type> __il)
1987{
1988 __init(__il.begin(), __il.end());
1989}
1990
1991template <class _CharT, class _Traits, class _Allocator>
1992_LIBCPP_INLINE_VISIBILITY inline
1993basic_string<_CharT, _Traits, _Allocator>::basic_string(initializer_list<value_type> __il, const allocator_type& __a)
1994 : __r_(__a)
1995{
1996 __init(__il.begin(), __il.end());
1997}
1998
Howard Hinnante3e32912011-08-12 21:56:02 +00001999#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
2000
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002001template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002002basic_string<_CharT, _Traits, _Allocator>::~basic_string()
2003{
2004 __invalidate_all_iterators();
2005 if (__is_long())
Howard Hinnante32b5e22010-11-17 17:55:08 +00002006 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002007}
2008
2009template <class _CharT, class _Traits, class _Allocator>
2010void
2011basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
2012 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2013 size_type __n_copy, size_type __n_del, size_type __n_add, const_pointer __p_new_stuff)
2014{
2015 size_type __ms = max_size();
2016 if (__delta_cap > __ms - __old_cap - 1)
2017 this->__throw_length_error();
2018 pointer __old_p = __get_pointer();
2019 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnant0949eed2011-06-30 21:18:19 +00002020 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002021 __ms - 1;
Howard Hinnante32b5e22010-11-17 17:55:08 +00002022 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002023 __invalidate_all_iterators();
2024 if (__n_copy != 0)
2025 traits_type::copy(__p, __old_p, __n_copy);
2026 if (__n_add != 0)
2027 traits_type::copy(__p + __n_copy, __p_new_stuff, __n_add);
2028 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2029 if (__sec_cp_sz != 0)
2030 traits_type::copy(__p + __n_copy + __n_add, __old_p + __n_copy + __n_del, __sec_cp_sz);
2031 if (__old_cap+1 != __min_cap)
Howard Hinnante32b5e22010-11-17 17:55:08 +00002032 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002033 __set_long_pointer(__p);
2034 __set_long_cap(__cap+1);
2035 __old_sz = __n_copy + __n_add + __sec_cp_sz;
2036 __set_long_size(__old_sz);
2037 traits_type::assign(__p[__old_sz], value_type());
2038}
2039
2040template <class _CharT, class _Traits, class _Allocator>
2041void
2042basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2043 size_type __n_copy, size_type __n_del, size_type __n_add)
2044{
2045 size_type __ms = max_size();
2046 if (__delta_cap > __ms - __old_cap - 1)
2047 this->__throw_length_error();
2048 pointer __old_p = __get_pointer();
2049 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnant0949eed2011-06-30 21:18:19 +00002050 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002051 __ms - 1;
Howard Hinnante32b5e22010-11-17 17:55:08 +00002052 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002053 __invalidate_all_iterators();
2054 if (__n_copy != 0)
2055 traits_type::copy(__p, __old_p, __n_copy);
2056 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2057 if (__sec_cp_sz != 0)
2058 traits_type::copy(__p + __n_copy + __n_add, __old_p + __n_copy + __n_del, __sec_cp_sz);
2059 if (__old_cap+1 != __min_cap)
Howard Hinnante32b5e22010-11-17 17:55:08 +00002060 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002061 __set_long_pointer(__p);
2062 __set_long_cap(__cap+1);
2063}
2064
2065// assign
2066
2067template <class _CharT, class _Traits, class _Allocator>
2068basic_string<_CharT, _Traits, _Allocator>&
2069basic_string<_CharT, _Traits, _Allocator>::assign(const_pointer __s, size_type __n)
2070{
2071#ifdef _LIBCPP_DEBUG
2072 assert(__s != 0);
2073#endif
2074 size_type __cap = capacity();
2075 if (__cap >= __n)
2076 {
2077 pointer __p = __get_pointer();
2078 traits_type::move(__p, __s, __n);
2079 traits_type::assign(__p[__n], value_type());
2080 __set_size(__n);
2081 __invalidate_iterators_past(__n);
2082 }
2083 else
2084 {
2085 size_type __sz = size();
2086 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
2087 }
2088 return *this;
2089}
2090
2091template <class _CharT, class _Traits, class _Allocator>
2092basic_string<_CharT, _Traits, _Allocator>&
2093basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2094{
2095 size_type __cap = capacity();
2096 if (__cap < __n)
2097 {
2098 size_type __sz = size();
2099 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2100 }
2101 else
2102 __invalidate_iterators_past(__n);
2103 pointer __p = __get_pointer();
2104 traits_type::assign(__p, __n, __c);
2105 traits_type::assign(__p[__n], value_type());
2106 __set_size(__n);
2107 return *this;
2108}
2109
2110template <class _CharT, class _Traits, class _Allocator>
2111basic_string<_CharT, _Traits, _Allocator>&
2112basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2113{
2114 pointer __p;
2115 if (__is_long())
2116 {
2117 __p = __get_long_pointer();
2118 __set_long_size(1);
2119 }
2120 else
2121 {
2122 __p = __get_short_pointer();
2123 __set_short_size(1);
2124 }
2125 traits_type::assign(*__p, __c);
2126 traits_type::assign(*++__p, value_type());
2127 __invalidate_iterators_past(1);
2128 return *this;
2129}
2130
2131template <class _CharT, class _Traits, class _Allocator>
Howard Hinnante32b5e22010-11-17 17:55:08 +00002132basic_string<_CharT, _Traits, _Allocator>&
2133basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2134{
2135 if (this != &__str)
2136 {
2137 __copy_assign_alloc(__str);
2138 assign(__str);
2139 }
2140 return *this;
2141}
2142
2143#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2144
2145template <class _CharT, class _Traits, class _Allocator>
2146_LIBCPP_INLINE_VISIBILITY inline
2147void
2148basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
2149{
2150 if (__alloc() != __str.__alloc())
2151 assign(__str);
2152 else
2153 __move_assign(__str, true_type());
2154}
2155
2156template <class _CharT, class _Traits, class _Allocator>
2157_LIBCPP_INLINE_VISIBILITY inline
2158void
2159basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00002160 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnante32b5e22010-11-17 17:55:08 +00002161{
2162 clear();
2163 shrink_to_fit();
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00002164 __r_.first() = __str.__r_.first();
2165 __move_assign_alloc(__str);
Howard Hinnante32b5e22010-11-17 17:55:08 +00002166 __str.__zero();
2167}
2168
2169template <class _CharT, class _Traits, class _Allocator>
2170_LIBCPP_INLINE_VISIBILITY inline
2171basic_string<_CharT, _Traits, _Allocator>&
2172basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00002173 _NOEXCEPT_(__alloc_traits::propagate_on_container_move_assignment::value &&
2174 is_nothrow_move_assignable<allocator_type>::value)
Howard Hinnante32b5e22010-11-17 17:55:08 +00002175{
2176 __move_assign(__str, integral_constant<bool,
2177 __alloc_traits::propagate_on_container_move_assignment::value>());
2178 return *this;
2179}
2180
2181#endif
2182
2183template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002184template<class _InputIterator>
2185typename enable_if
2186<
2187 __is_input_iterator <_InputIterator>::value &&
2188 !__is_forward_iterator<_InputIterator>::value,
2189 basic_string<_CharT, _Traits, _Allocator>&
2190>::type
2191basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2192{
2193 clear();
2194 for (; __first != __last; ++__first)
2195 push_back(*__first);
2196}
2197
2198template <class _CharT, class _Traits, class _Allocator>
2199template<class _ForwardIterator>
2200typename enable_if
2201<
2202 __is_forward_iterator<_ForwardIterator>::value,
2203 basic_string<_CharT, _Traits, _Allocator>&
2204>::type
2205basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2206{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002207 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002208 size_type __cap = capacity();
2209 if (__cap < __n)
2210 {
2211 size_type __sz = size();
2212 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2213 }
2214 else
2215 __invalidate_iterators_past(__n);
2216 pointer __p = __get_pointer();
2217 for (; __first != __last; ++__first, ++__p)
2218 traits_type::assign(*__p, *__first);
2219 traits_type::assign(*__p, value_type());
2220 __set_size(__n);
2221 return *this;
2222}
2223
2224template <class _CharT, class _Traits, class _Allocator>
2225_LIBCPP_INLINE_VISIBILITY inline
2226basic_string<_CharT, _Traits, _Allocator>&
2227basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str)
2228{
2229 return assign(__str.data(), __str.size());
2230}
2231
2232template <class _CharT, class _Traits, class _Allocator>
2233basic_string<_CharT, _Traits, _Allocator>&
2234basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2235{
2236 size_type __sz = __str.size();
2237 if (__pos > __sz)
2238 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002239 return assign(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002240}
2241
2242template <class _CharT, class _Traits, class _Allocator>
2243basic_string<_CharT, _Traits, _Allocator>&
2244basic_string<_CharT, _Traits, _Allocator>::assign(const_pointer __s)
2245{
2246#ifdef _LIBCPP_DEBUG
2247 assert(__s != 0);
2248#endif
2249 return assign(__s, traits_type::length(__s));
2250}
2251
2252// append
2253
2254template <class _CharT, class _Traits, class _Allocator>
2255basic_string<_CharT, _Traits, _Allocator>&
2256basic_string<_CharT, _Traits, _Allocator>::append(const_pointer __s, size_type __n)
2257{
2258#ifdef _LIBCPP_DEBUG
2259 assert(__s != 0);
2260#endif
2261 size_type __cap = capacity();
2262 size_type __sz = size();
2263 if (__cap - __sz >= __n)
2264 {
2265 if (__n)
2266 {
2267 pointer __p = __get_pointer();
2268 traits_type::copy(__p + __sz, __s, __n);
2269 __sz += __n;
2270 __set_size(__sz);
2271 traits_type::assign(__p[__sz], value_type());
2272 }
2273 }
2274 else
2275 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2276 return *this;
2277}
2278
2279template <class _CharT, class _Traits, class _Allocator>
2280basic_string<_CharT, _Traits, _Allocator>&
2281basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2282{
2283 if (__n)
2284 {
2285 size_type __cap = capacity();
2286 size_type __sz = size();
2287 if (__cap - __sz < __n)
2288 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2289 pointer __p = __get_pointer();
2290 traits_type::assign(__p + __sz, __n, __c);
2291 __sz += __n;
2292 __set_size(__sz);
2293 traits_type::assign(__p[__sz], value_type());
2294 }
2295 return *this;
2296}
2297
2298template <class _CharT, class _Traits, class _Allocator>
2299void
2300basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2301{
2302 size_type __cap = capacity();
2303 size_type __sz = size();
2304 if (__sz == __cap)
2305 __grow_by(__cap, 1, __sz, __sz, 0);
2306 pointer __p = __get_pointer() + __sz;
2307 traits_type::assign(*__p, __c);
2308 traits_type::assign(*++__p, value_type());
2309 __set_size(__sz+1);
2310}
2311
2312template <class _CharT, class _Traits, class _Allocator>
2313template<class _InputIterator>
2314typename enable_if
2315<
2316 __is_input_iterator <_InputIterator>::value &&
2317 !__is_forward_iterator<_InputIterator>::value,
2318 basic_string<_CharT, _Traits, _Allocator>&
2319>::type
2320basic_string<_CharT, _Traits, _Allocator>::append(_InputIterator __first, _InputIterator __last)
2321{
2322 for (; __first != __last; ++__first)
2323 push_back(*__first);
2324 return *this;
2325}
2326
2327template <class _CharT, class _Traits, class _Allocator>
2328template<class _ForwardIterator>
2329typename enable_if
2330<
2331 __is_forward_iterator<_ForwardIterator>::value,
2332 basic_string<_CharT, _Traits, _Allocator>&
2333>::type
2334basic_string<_CharT, _Traits, _Allocator>::append(_ForwardIterator __first, _ForwardIterator __last)
2335{
2336 size_type __sz = size();
2337 size_type __cap = capacity();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002338 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002339 if (__n)
2340 {
2341 if (__cap - __sz < __n)
2342 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2343 pointer __p = __get_pointer() + __sz;
2344 for (; __first != __last; ++__p, ++__first)
2345 traits_type::assign(*__p, *__first);
2346 traits_type::assign(*__p, value_type());
2347 __set_size(__sz + __n);
2348 }
2349 return *this;
2350}
2351
2352template <class _CharT, class _Traits, class _Allocator>
2353_LIBCPP_INLINE_VISIBILITY inline
2354basic_string<_CharT, _Traits, _Allocator>&
2355basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2356{
2357 return append(__str.data(), __str.size());
2358}
2359
2360template <class _CharT, class _Traits, class _Allocator>
2361basic_string<_CharT, _Traits, _Allocator>&
2362basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2363{
2364 size_type __sz = __str.size();
2365 if (__pos > __sz)
2366 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002367 return append(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002368}
2369
2370template <class _CharT, class _Traits, class _Allocator>
2371basic_string<_CharT, _Traits, _Allocator>&
2372basic_string<_CharT, _Traits, _Allocator>::append(const_pointer __s)
2373{
2374#ifdef _LIBCPP_DEBUG
2375 assert(__s != 0);
2376#endif
2377 return append(__s, traits_type::length(__s));
2378}
2379
2380// insert
2381
2382template <class _CharT, class _Traits, class _Allocator>
2383basic_string<_CharT, _Traits, _Allocator>&
2384basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const_pointer __s, size_type __n)
2385{
2386#ifdef _LIBCPP_DEBUG
2387 assert(__s != 0);
2388#endif
2389 size_type __sz = size();
2390 if (__pos > __sz)
2391 this->__throw_out_of_range();
2392 size_type __cap = capacity();
2393 if (__cap - __sz >= __n)
2394 {
2395 if (__n)
2396 {
2397 pointer __p = __get_pointer();
2398 size_type __n_move = __sz - __pos;
2399 if (__n_move != 0)
2400 {
2401 if (__p + __pos <= __s && __s < __p + __sz)
2402 __s += __n;
2403 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2404 }
2405 traits_type::move(__p + __pos, __s, __n);
2406 __sz += __n;
2407 __set_size(__sz);
2408 traits_type::assign(__p[__sz], value_type());
2409 }
2410 }
2411 else
2412 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2413 return *this;
2414}
2415
2416template <class _CharT, class _Traits, class _Allocator>
2417basic_string<_CharT, _Traits, _Allocator>&
2418basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2419{
2420 size_type __sz = size();
2421 if (__pos > __sz)
2422 this->__throw_out_of_range();
2423 if (__n)
2424 {
2425 size_type __cap = capacity();
2426 pointer __p;
2427 if (__cap - __sz >= __n)
2428 {
2429 __p = __get_pointer();
2430 size_type __n_move = __sz - __pos;
2431 if (__n_move != 0)
2432 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2433 }
2434 else
2435 {
2436 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
2437 __p = __get_long_pointer();
2438 }
2439 traits_type::assign(__p + __pos, __n, __c);
2440 __sz += __n;
2441 __set_size(__sz);
2442 traits_type::assign(__p[__sz], value_type());
2443 }
2444 return *this;
2445}
2446
2447template <class _CharT, class _Traits, class _Allocator>
2448template<class _InputIterator>
2449typename enable_if
2450<
2451 __is_input_iterator <_InputIterator>::value &&
2452 !__is_forward_iterator<_InputIterator>::value,
2453 typename basic_string<_CharT, _Traits, _Allocator>::iterator
2454>::type
2455basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2456{
2457 size_type __old_sz = size();
2458 difference_type __ip = __pos - begin();
2459 for (; __first != __last; ++__first)
2460 push_back(*__first);
2461 pointer __p = __get_pointer();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002462 _VSTD::rotate(__p + __ip, __p + __old_sz, __p + size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002463 return iterator(__p + __ip);
2464}
2465
2466template <class _CharT, class _Traits, class _Allocator>
2467template<class _ForwardIterator>
2468typename enable_if
2469<
2470 __is_forward_iterator<_ForwardIterator>::value,
2471 typename basic_string<_CharT, _Traits, _Allocator>::iterator
2472>::type
2473basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2474{
2475 size_type __ip = static_cast<size_type>(__pos - begin());
2476 size_type __sz = size();
2477 size_type __cap = capacity();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002478 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002479 if (__n)
2480 {
2481 pointer __p;
2482 if (__cap - __sz >= __n)
2483 {
2484 __p = __get_pointer();
2485 size_type __n_move = __sz - __ip;
2486 if (__n_move != 0)
2487 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
2488 }
2489 else
2490 {
2491 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
2492 __p = __get_long_pointer();
2493 }
2494 __sz += __n;
2495 __set_size(__sz);
2496 traits_type::assign(__p[__sz], value_type());
2497 for (__p += __ip; __first != __last; ++__p, ++__first)
2498 traits_type::assign(*__p, *__first);
2499 }
2500 return begin() + __ip;
2501}
2502
2503template <class _CharT, class _Traits, class _Allocator>
2504_LIBCPP_INLINE_VISIBILITY inline
2505basic_string<_CharT, _Traits, _Allocator>&
2506basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2507{
2508 return insert(__pos1, __str.data(), __str.size());
2509}
2510
2511template <class _CharT, class _Traits, class _Allocator>
2512basic_string<_CharT, _Traits, _Allocator>&
2513basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2514 size_type __pos2, size_type __n)
2515{
2516 size_type __str_sz = __str.size();
2517 if (__pos2 > __str_sz)
2518 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002519 return insert(__pos1, __str.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002520}
2521
2522template <class _CharT, class _Traits, class _Allocator>
2523basic_string<_CharT, _Traits, _Allocator>&
2524basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const_pointer __s)
2525{
2526#ifdef _LIBCPP_DEBUG
2527 assert(__s != 0);
2528#endif
2529 return insert(__pos, __s, traits_type::length(__s));
2530}
2531
2532template <class _CharT, class _Traits, class _Allocator>
2533typename basic_string<_CharT, _Traits, _Allocator>::iterator
2534basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2535{
2536 size_type __ip = static_cast<size_type>(__pos - begin());
2537 size_type __sz = size();
2538 size_type __cap = capacity();
2539 pointer __p;
2540 if (__cap == __sz)
2541 {
2542 __grow_by(__cap, 1, __sz, __ip, 0, 1);
2543 __p = __get_long_pointer();
2544 }
2545 else
2546 {
2547 __p = __get_pointer();
2548 size_type __n_move = __sz - __ip;
2549 if (__n_move != 0)
2550 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2551 }
2552 traits_type::assign(__p[__ip], __c);
2553 traits_type::assign(__p[++__sz], value_type());
2554 __set_size(__sz);
2555 return begin() + static_cast<difference_type>(__ip);
2556}
2557
2558template <class _CharT, class _Traits, class _Allocator>
2559_LIBCPP_INLINE_VISIBILITY inline
2560typename basic_string<_CharT, _Traits, _Allocator>::iterator
2561basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2562{
2563 difference_type __p = __pos - begin();
2564 insert(static_cast<size_type>(__p), __n, __c);
2565 return begin() + __p;
2566}
2567
2568// replace
2569
2570template <class _CharT, class _Traits, class _Allocator>
2571basic_string<_CharT, _Traits, _Allocator>&
2572basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const_pointer __s, size_type __n2)
2573{
2574#ifdef _LIBCPP_DEBUG
2575 assert(__s != 0);
2576#endif
2577 size_type __sz = size();
2578 if (__pos > __sz)
2579 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002580 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002581 size_type __cap = capacity();
2582 if (__cap - __sz + __n1 >= __n2)
2583 {
2584 pointer __p = __get_pointer();
2585 if (__n1 != __n2)
2586 {
2587 size_type __n_move = __sz - __pos - __n1;
2588 if (__n_move != 0)
2589 {
2590 if (__n1 > __n2)
2591 {
2592 traits_type::move(__p + __pos, __s, __n2);
2593 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2594 goto __finish;
2595 }
2596 if (__p + __pos < __s && __s < __p + __sz)
2597 {
2598 if (__p + __pos + __n1 <= __s)
2599 __s += __n2 - __n1;
2600 else // __p + __pos < __s < __p + __pos + __n1
2601 {
2602 traits_type::move(__p + __pos, __s, __n1);
2603 __pos += __n1;
2604 __s += __n2;
2605 __n2 -= __n1;
2606 __n1 = 0;
2607 }
2608 }
2609 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2610 }
2611 }
2612 traits_type::move(__p + __pos, __s, __n2);
2613__finish:
2614 __sz += __n2 - __n1;
2615 __set_size(__sz);
2616 __invalidate_iterators_past(__sz);
2617 traits_type::assign(__p[__sz], value_type());
2618 }
2619 else
2620 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
2621 return *this;
2622}
2623
2624template <class _CharT, class _Traits, class _Allocator>
2625basic_string<_CharT, _Traits, _Allocator>&
2626basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
2627{
2628 size_type __sz = size();
2629 if (__pos > __sz)
2630 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002631 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002632 size_type __cap = capacity();
2633 pointer __p;
2634 if (__cap - __sz + __n1 >= __n2)
2635 {
2636 __p = __get_pointer();
2637 if (__n1 != __n2)
2638 {
2639 size_type __n_move = __sz - __pos - __n1;
2640 if (__n_move != 0)
2641 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2642 }
2643 }
2644 else
2645 {
2646 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
2647 __p = __get_long_pointer();
2648 }
2649 traits_type::assign(__p + __pos, __n2, __c);
2650 __sz += __n2 - __n1;
2651 __set_size(__sz);
2652 __invalidate_iterators_past(__sz);
2653 traits_type::assign(__p[__sz], value_type());
2654 return *this;
2655}
2656
2657template <class _CharT, class _Traits, class _Allocator>
2658template<class _InputIterator>
2659typename enable_if
2660<
2661 __is_input_iterator<_InputIterator>::value,
2662 basic_string<_CharT, _Traits, _Allocator>&
2663>::type
Howard Hinnant7b2cb482010-11-17 21:11:40 +00002664basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002665 _InputIterator __j1, _InputIterator __j2)
2666{
2667 for (; true; ++__i1, ++__j1)
2668 {
2669 if (__i1 == __i2)
2670 {
2671 if (__j1 != __j2)
2672 insert(__i1, __j1, __j2);
2673 break;
2674 }
2675 if (__j1 == __j2)
2676 {
2677 erase(__i1, __i2);
2678 break;
2679 }
Howard Hinnant7b2cb482010-11-17 21:11:40 +00002680 traits_type::assign(const_cast<value_type&>(*__i1), *__j1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002681 }
2682 return *this;
2683}
2684
2685template <class _CharT, class _Traits, class _Allocator>
2686_LIBCPP_INLINE_VISIBILITY inline
2687basic_string<_CharT, _Traits, _Allocator>&
2688basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
2689{
2690 return replace(__pos1, __n1, __str.data(), __str.size());
2691}
2692
2693template <class _CharT, class _Traits, class _Allocator>
2694basic_string<_CharT, _Traits, _Allocator>&
2695basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
2696 size_type __pos2, size_type __n2)
2697{
2698 size_type __str_sz = __str.size();
2699 if (__pos2 > __str_sz)
2700 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002701 return replace(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002702}
2703
2704template <class _CharT, class _Traits, class _Allocator>
2705basic_string<_CharT, _Traits, _Allocator>&
2706basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const_pointer __s)
2707{
2708#ifdef _LIBCPP_DEBUG
2709 assert(__s != 0);
2710#endif
2711 return replace(__pos, __n1, __s, traits_type::length(__s));
2712}
2713
2714template <class _CharT, class _Traits, class _Allocator>
2715_LIBCPP_INLINE_VISIBILITY inline
2716basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant7b2cb482010-11-17 21:11:40 +00002717basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002718{
2719 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
2720 __str.data(), __str.size());
2721}
2722
2723template <class _CharT, class _Traits, class _Allocator>
2724_LIBCPP_INLINE_VISIBILITY inline
2725basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant7b2cb482010-11-17 21:11:40 +00002726basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const_pointer __s, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002727{
2728 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
2729}
2730
2731template <class _CharT, class _Traits, class _Allocator>
2732_LIBCPP_INLINE_VISIBILITY inline
2733basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant7b2cb482010-11-17 21:11:40 +00002734basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const_pointer __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002735{
2736 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
2737}
2738
2739template <class _CharT, class _Traits, class _Allocator>
2740_LIBCPP_INLINE_VISIBILITY inline
2741basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant7b2cb482010-11-17 21:11:40 +00002742basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002743{
2744 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
2745}
2746
2747// erase
2748
2749template <class _CharT, class _Traits, class _Allocator>
2750basic_string<_CharT, _Traits, _Allocator>&
2751basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos, size_type __n)
2752{
2753 size_type __sz = size();
2754 if (__pos > __sz)
2755 this->__throw_out_of_range();
2756 if (__n)
2757 {
2758 pointer __p = __get_pointer();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002759 __n = _VSTD::min(__n, __sz - __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002760 size_type __n_move = __sz - __pos - __n;
2761 if (__n_move != 0)
2762 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
2763 __sz -= __n;
2764 __set_size(__sz);
2765 __invalidate_iterators_past(__sz);
2766 traits_type::assign(__p[__sz], value_type());
2767 }
2768 return *this;
2769}
2770
2771template <class _CharT, class _Traits, class _Allocator>
2772_LIBCPP_INLINE_VISIBILITY inline
2773typename basic_string<_CharT, _Traits, _Allocator>::iterator
2774basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
2775{
2776 iterator __b = begin();
2777 size_type __r = static_cast<size_type>(__pos - __b);
2778 erase(__r, 1);
2779 return __b + __r;
2780}
2781
2782template <class _CharT, class _Traits, class _Allocator>
2783_LIBCPP_INLINE_VISIBILITY inline
2784typename basic_string<_CharT, _Traits, _Allocator>::iterator
2785basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
2786{
2787 iterator __b = begin();
2788 size_type __r = static_cast<size_type>(__first - __b);
2789 erase(__r, static_cast<size_type>(__last - __first));
2790 return __b + __r;
2791}
2792
2793template <class _CharT, class _Traits, class _Allocator>
2794_LIBCPP_INLINE_VISIBILITY inline
2795void
2796basic_string<_CharT, _Traits, _Allocator>::pop_back()
2797{
2798#ifdef _LIBCPP_DEBUG
2799 assert(!empty());
2800#endif
2801 size_type __sz;
2802 if (__is_long())
2803 {
2804 __sz = __get_long_size() - 1;
2805 __set_long_size(__sz);
2806 traits_type::assign(*(__get_long_pointer() + __sz), value_type());
2807 }
2808 else
2809 {
2810 __sz = __get_short_size() - 1;
2811 __set_short_size(__sz);
2812 traits_type::assign(*(__get_short_pointer() + __sz), value_type());
2813 }
2814 __invalidate_iterators_past(__sz);
2815}
2816
2817template <class _CharT, class _Traits, class _Allocator>
2818_LIBCPP_INLINE_VISIBILITY inline
2819void
Howard Hinnanta6119a82011-05-29 19:57:12 +00002820basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002821{
2822 __invalidate_all_iterators();
2823 if (__is_long())
2824 {
2825 traits_type::assign(*__get_long_pointer(), value_type());
2826 __set_long_size(0);
2827 }
2828 else
2829 {
2830 traits_type::assign(*__get_short_pointer(), value_type());
2831 __set_short_size(0);
2832 }
2833}
2834
2835template <class _CharT, class _Traits, class _Allocator>
2836_LIBCPP_INLINE_VISIBILITY inline
2837void
2838basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
2839{
2840 if (__is_long())
2841 {
2842 traits_type::assign(*(__get_long_pointer() + __pos), value_type());
2843 __set_long_size(__pos);
2844 }
2845 else
2846 {
2847 traits_type::assign(*(__get_short_pointer() + __pos), value_type());
2848 __set_short_size(__pos);
2849 }
2850 __invalidate_iterators_past(__pos);
2851}
2852
2853template <class _CharT, class _Traits, class _Allocator>
2854void
2855basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
2856{
2857 size_type __sz = size();
2858 if (__n > __sz)
2859 append(__n - __sz, __c);
2860 else
2861 __erase_to_end(__n);
2862}
2863
2864template <class _CharT, class _Traits, class _Allocator>
2865_LIBCPP_INLINE_VISIBILITY inline
2866typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00002867basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002868{
Howard Hinnante32b5e22010-11-17 17:55:08 +00002869 size_type __m = __alloc_traits::max_size(__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002870#if _LIBCPP_BIG_ENDIAN
2871 return (__m <= ~__long_mask ? __m : __m/2) - 1;
2872#else
2873 return __m - 1;
2874#endif
2875}
2876
2877template <class _CharT, class _Traits, class _Allocator>
2878void
2879basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __res_arg)
2880{
2881 if (__res_arg > max_size())
2882 this->__throw_length_error();
2883 size_type __cap = capacity();
2884 size_type __sz = size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002885 __res_arg = _VSTD::max(__res_arg, __sz);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002886 __res_arg = __recommend(__res_arg);
2887 if (__res_arg != __cap)
2888 {
2889 pointer __new_data, __p;
2890 bool __was_long, __now_long;
2891 if (__res_arg == __min_cap - 1)
2892 {
2893 __was_long = true;
2894 __now_long = false;
2895 __new_data = __get_short_pointer();
2896 __p = __get_long_pointer();
2897 }
2898 else
2899 {
2900 if (__res_arg > __cap)
Howard Hinnante32b5e22010-11-17 17:55:08 +00002901 __new_data = __alloc_traits::allocate(__alloc(), __res_arg+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002902 else
2903 {
2904 #ifndef _LIBCPP_NO_EXCEPTIONS
2905 try
2906 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002907 #endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnante32b5e22010-11-17 17:55:08 +00002908 __new_data = __alloc_traits::allocate(__alloc(), __res_arg+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002909 #ifndef _LIBCPP_NO_EXCEPTIONS
2910 }
2911 catch (...)
2912 {
2913 return;
2914 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002915 #else // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002916 if (__new_data == 0)
2917 return;
Howard Hinnant324bb032010-08-22 00:02:43 +00002918 #endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002919 }
2920 __now_long = true;
2921 __was_long = __is_long();
2922 __p = __get_pointer();
2923 }
2924 traits_type::copy(__new_data, __p, size()+1);
2925 if (__was_long)
Howard Hinnante32b5e22010-11-17 17:55:08 +00002926 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002927 if (__now_long)
2928 {
2929 __set_long_cap(__res_arg+1);
2930 __set_long_size(__sz);
2931 __set_long_pointer(__new_data);
2932 }
2933 else
2934 __set_short_size(__sz);
2935 __invalidate_all_iterators();
2936 }
2937}
2938
2939template <class _CharT, class _Traits, class _Allocator>
2940_LIBCPP_INLINE_VISIBILITY inline
2941typename basic_string<_CharT, _Traits, _Allocator>::const_reference
2942basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const
2943{
2944#ifdef __LIBCPP_DEBUG
2945 assert(__pos <= size());
2946#endif
2947 return *(data() + __pos);
2948}
2949
2950template <class _CharT, class _Traits, class _Allocator>
2951_LIBCPP_INLINE_VISIBILITY inline
2952typename basic_string<_CharT, _Traits, _Allocator>::reference
2953basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos)
2954{
2955#ifdef __LIBCPP_DEBUG
2956 assert(__pos < size());
2957#endif
2958 return *(__get_pointer() + __pos);
2959}
2960
2961template <class _CharT, class _Traits, class _Allocator>
2962typename basic_string<_CharT, _Traits, _Allocator>::const_reference
2963basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
2964{
2965 if (__n >= size())
2966 this->__throw_out_of_range();
2967 return (*this)[__n];
2968}
2969
2970template <class _CharT, class _Traits, class _Allocator>
2971typename basic_string<_CharT, _Traits, _Allocator>::reference
2972basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
2973{
2974 if (__n >= size())
2975 this->__throw_out_of_range();
2976 return (*this)[__n];
2977}
2978
2979template <class _CharT, class _Traits, class _Allocator>
2980_LIBCPP_INLINE_VISIBILITY inline
2981typename basic_string<_CharT, _Traits, _Allocator>::reference
2982basic_string<_CharT, _Traits, _Allocator>::front()
2983{
2984#ifdef _LIBCPP_DEBUG
2985 assert(!empty());
2986#endif
2987 return *__get_pointer();
2988}
2989
2990template <class _CharT, class _Traits, class _Allocator>
2991_LIBCPP_INLINE_VISIBILITY inline
2992typename basic_string<_CharT, _Traits, _Allocator>::const_reference
2993basic_string<_CharT, _Traits, _Allocator>::front() const
2994{
2995#ifdef _LIBCPP_DEBUG
2996 assert(!empty());
2997#endif
2998 return *data();
2999}
3000
3001template <class _CharT, class _Traits, class _Allocator>
3002_LIBCPP_INLINE_VISIBILITY inline
3003typename basic_string<_CharT, _Traits, _Allocator>::reference
3004basic_string<_CharT, _Traits, _Allocator>::back()
3005{
3006#ifdef _LIBCPP_DEBUG
3007 assert(!empty());
3008#endif
3009 return *(__get_pointer() + size() - 1);
3010}
3011
3012template <class _CharT, class _Traits, class _Allocator>
3013_LIBCPP_INLINE_VISIBILITY inline
3014typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3015basic_string<_CharT, _Traits, _Allocator>::back() const
3016{
3017#ifdef _LIBCPP_DEBUG
3018 assert(!empty());
3019#endif
3020 return *(data() + size() - 1);
3021}
3022
3023template <class _CharT, class _Traits, class _Allocator>
3024typename basic_string<_CharT, _Traits, _Allocator>::size_type
3025basic_string<_CharT, _Traits, _Allocator>::copy(pointer __s, size_type __n, size_type __pos) const
3026{
3027 size_type __sz = size();
3028 if (__pos > __sz)
3029 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00003030 size_type __rlen = _VSTD::min(__n, __sz - __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003031 traits_type::copy(__s, data() + __pos, __rlen);
3032 return __rlen;
3033}
3034
3035template <class _CharT, class _Traits, class _Allocator>
3036_LIBCPP_INLINE_VISIBILITY inline
3037basic_string<_CharT, _Traits, _Allocator>
3038basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3039{
3040 return basic_string(*this, __pos, __n, __alloc());
3041}
3042
3043template <class _CharT, class _Traits, class _Allocator>
3044_LIBCPP_INLINE_VISIBILITY inline
3045void
3046basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00003047 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
3048 __is_nothrow_swappable<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003049{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003050 _VSTD::swap(__r_.first(), __str.__r_.first());
Howard Hinnante32b5e22010-11-17 17:55:08 +00003051 __swap_alloc(__alloc(), __str.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003052#ifdef _LIBCPP_DEBUG
3053 __invalidate_all_iterators();
3054 __str.__invalidate_all_iterators();
Howard Hinnant324bb032010-08-22 00:02:43 +00003055#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003056}
3057
3058// find
3059
3060template <class _Traits>
3061struct _LIBCPP_HIDDEN __traits_eq
3062{
3063 typedef typename _Traits::char_type char_type;
Howard Hinnanta6119a82011-05-29 19:57:12 +00003064 _LIBCPP_INLINE_VISIBILITY
3065 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3066 {return _Traits::eq(__x, __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003067};
3068
3069template<class _CharT, class _Traits, class _Allocator>
3070typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003071basic_string<_CharT, _Traits, _Allocator>::find(const_pointer __s,
3072 size_type __pos,
3073 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003074{
3075#ifdef _LIBCPP_DEBUG
3076 assert(__s != 0);
3077#endif
3078 size_type __sz = size();
3079 if (__pos > __sz || __sz - __pos < __n)
3080 return npos;
3081 if (__n == 0)
3082 return __pos;
3083 const_pointer __p = data();
Howard Hinnant0949eed2011-06-30 21:18:19 +00003084 const_pointer __r = _VSTD::search(__p + __pos, __p + __sz, __s, __s + __n,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003085 __traits_eq<traits_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003086 if (__r == __p + __sz)
3087 return npos;
3088 return static_cast<size_type>(__r - __p);
3089}
3090
3091template<class _CharT, class _Traits, class _Allocator>
3092_LIBCPP_INLINE_VISIBILITY inline
3093typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003094basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3095 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003096{
3097 return find(__str.data(), __pos, __str.size());
3098}
3099
3100template<class _CharT, class _Traits, class _Allocator>
3101_LIBCPP_INLINE_VISIBILITY inline
3102typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003103basic_string<_CharT, _Traits, _Allocator>::find(const_pointer __s,
3104 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003105{
3106#ifdef _LIBCPP_DEBUG
3107 assert(__s != 0);
3108#endif
3109 return find(__s, __pos, traits_type::length(__s));
3110}
3111
3112template<class _CharT, class _Traits, class _Allocator>
3113typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003114basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3115 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003116{
3117 size_type __sz = size();
3118 if (__pos >= __sz)
3119 return npos;
3120 const_pointer __p = data();
3121 const_pointer __r = traits_type::find(__p + __pos, __sz - __pos, __c);
3122 if (__r == 0)
3123 return npos;
3124 return static_cast<size_type>(__r - __p);
3125}
3126
3127// rfind
3128
3129template<class _CharT, class _Traits, class _Allocator>
3130typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003131basic_string<_CharT, _Traits, _Allocator>::rfind(const_pointer __s,
3132 size_type __pos,
3133 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003134{
3135#ifdef _LIBCPP_DEBUG
3136 assert(__s != 0);
3137#endif
3138 size_type __sz = size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00003139 __pos = _VSTD::min(__pos, __sz);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003140 if (__n < __sz - __pos)
3141 __pos += __n;
3142 else
3143 __pos = __sz;
3144 const_pointer __p = data();
Howard Hinnant0949eed2011-06-30 21:18:19 +00003145 const_pointer __r = _VSTD::find_end(__p, __p + __pos, __s, __s + __n,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003146 __traits_eq<traits_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003147 if (__n > 0 && __r == __p + __pos)
3148 return npos;
3149 return static_cast<size_type>(__r - __p);
3150}
3151
3152template<class _CharT, class _Traits, class _Allocator>
3153_LIBCPP_INLINE_VISIBILITY inline
3154typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003155basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3156 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003157{
3158 return rfind(__str.data(), __pos, __str.size());
3159}
3160
3161template<class _CharT, class _Traits, class _Allocator>
3162_LIBCPP_INLINE_VISIBILITY inline
3163typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003164basic_string<_CharT, _Traits, _Allocator>::rfind(const_pointer __s,
3165 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003166{
3167#ifdef _LIBCPP_DEBUG
3168 assert(__s != 0);
3169#endif
3170 return rfind(__s, __pos, traits_type::length(__s));
3171}
3172
3173template<class _CharT, class _Traits, class _Allocator>
3174typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003175basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3176 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003177{
3178 size_type __sz = size();
3179 if (__sz)
3180 {
3181 if (__pos < __sz)
3182 ++__pos;
3183 else
3184 __pos = __sz;
3185 const_pointer __p = data();
3186 for (const_pointer __ps = __p + __pos; __ps != __p;)
3187 {
3188 if (traits_type::eq(*--__ps, __c))
3189 return static_cast<size_type>(__ps - __p);
3190 }
3191 }
3192 return npos;
3193}
3194
3195// find_first_of
3196
3197template<class _CharT, class _Traits, class _Allocator>
3198typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003199basic_string<_CharT, _Traits, _Allocator>::find_first_of(const_pointer __s,
3200 size_type __pos,
3201 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003202{
3203#ifdef _LIBCPP_DEBUG
3204 assert(__s != 0);
3205#endif
3206 size_type __sz = size();
3207 if (__pos >= __sz || __n == 0)
3208 return npos;
3209 const_pointer __p = data();
Howard Hinnant0949eed2011-06-30 21:18:19 +00003210 const_pointer __r = _VSTD::find_first_of(__p + __pos, __p + __sz, __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003211 __s + __n, __traits_eq<traits_type>());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003212 if (__r == __p + __sz)
3213 return npos;
3214 return static_cast<size_type>(__r - __p);
3215}
3216
3217template<class _CharT, class _Traits, class _Allocator>
3218_LIBCPP_INLINE_VISIBILITY inline
3219typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003220basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3221 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003222{
3223 return find_first_of(__str.data(), __pos, __str.size());
3224}
3225
3226template<class _CharT, class _Traits, class _Allocator>
3227_LIBCPP_INLINE_VISIBILITY inline
3228typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003229basic_string<_CharT, _Traits, _Allocator>::find_first_of(const_pointer __s,
3230 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003231{
3232#ifdef _LIBCPP_DEBUG
3233 assert(__s != 0);
3234#endif
3235 return find_first_of(__s, __pos, traits_type::length(__s));
3236}
3237
3238template<class _CharT, class _Traits, class _Allocator>
3239_LIBCPP_INLINE_VISIBILITY inline
3240typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003241basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3242 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003243{
3244 return find(__c, __pos);
3245}
3246
3247// find_last_of
3248
3249template<class _CharT, class _Traits, class _Allocator>
3250typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003251basic_string<_CharT, _Traits, _Allocator>::find_last_of(const_pointer __s,
3252 size_type __pos,
3253 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003254{
3255#ifdef _LIBCPP_DEBUG
3256 assert(__s != 0);
3257#endif
3258 if (__n != 0)
3259 {
3260 size_type __sz = size();
3261 if (__pos < __sz)
3262 ++__pos;
3263 else
3264 __pos = __sz;
3265 const_pointer __p = data();
3266 for (const_pointer __ps = __p + __pos; __ps != __p;)
3267 {
3268 const_pointer __r = traits_type::find(__s, __n, *--__ps);
3269 if (__r)
3270 return static_cast<size_type>(__ps - __p);
3271 }
3272 }
3273 return npos;
3274}
3275
3276template<class _CharT, class _Traits, class _Allocator>
3277_LIBCPP_INLINE_VISIBILITY inline
3278typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003279basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3280 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003281{
3282 return find_last_of(__str.data(), __pos, __str.size());
3283}
3284
3285template<class _CharT, class _Traits, class _Allocator>
3286_LIBCPP_INLINE_VISIBILITY inline
3287typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003288basic_string<_CharT, _Traits, _Allocator>::find_last_of(const_pointer __s,
3289 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003290{
3291#ifdef _LIBCPP_DEBUG
3292 assert(__s != 0);
3293#endif
3294 return find_last_of(__s, __pos, traits_type::length(__s));
3295}
3296
3297template<class _CharT, class _Traits, class _Allocator>
3298_LIBCPP_INLINE_VISIBILITY inline
3299typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003300basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3301 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003302{
3303 return rfind(__c, __pos);
3304}
3305
3306// find_first_not_of
3307
3308template<class _CharT, class _Traits, class _Allocator>
3309typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003310basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const_pointer __s,
3311 size_type __pos,
3312 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003313{
3314#ifdef _LIBCPP_DEBUG
3315 assert(__s != 0);
3316#endif
3317 size_type __sz = size();
3318 if (__pos < __sz)
3319 {
3320 const_pointer __p = data();
3321 const_pointer __pe = __p + __sz;
3322 for (const_pointer __ps = __p + __pos; __ps != __pe; ++__ps)
3323 if (traits_type::find(__s, __n, *__ps) == 0)
3324 return static_cast<size_type>(__ps - __p);
3325 }
3326 return npos;
3327}
3328
3329template<class _CharT, class _Traits, class _Allocator>
3330_LIBCPP_INLINE_VISIBILITY inline
3331typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003332basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3333 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003334{
3335 return find_first_not_of(__str.data(), __pos, __str.size());
3336}
3337
3338template<class _CharT, class _Traits, class _Allocator>
3339_LIBCPP_INLINE_VISIBILITY inline
3340typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003341basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const_pointer __s,
3342 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003343{
3344#ifdef _LIBCPP_DEBUG
3345 assert(__s != 0);
3346#endif
3347 return find_first_not_of(__s, __pos, traits_type::length(__s));
3348}
3349
3350template<class _CharT, class _Traits, class _Allocator>
3351_LIBCPP_INLINE_VISIBILITY inline
3352typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003353basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3354 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003355{
3356 size_type __sz = size();
3357 if (__pos < __sz)
3358 {
3359 const_pointer __p = data();
3360 const_pointer __pe = __p + __sz;
3361 for (const_pointer __ps = __p + __pos; __p != __pe; ++__ps)
3362 if (!traits_type::eq(*__ps, __c))
3363 return static_cast<size_type>(__ps - __p);
3364 }
3365 return npos;
3366}
3367
3368// find_last_not_of
3369
3370template<class _CharT, class _Traits, class _Allocator>
3371typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003372basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const_pointer __s,
3373 size_type __pos,
3374 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003375{
3376#ifdef _LIBCPP_DEBUG
3377 assert(__s != 0);
3378#endif
3379 size_type __sz = size();
3380 if (__pos < __sz)
3381 ++__pos;
3382 else
3383 __pos = __sz;
3384 const_pointer __p = data();
3385 for (const_pointer __ps = __p + __pos; __ps != __p;)
3386 if (traits_type::find(__s, __n, *--__ps) == 0)
3387 return static_cast<size_type>(__ps - __p);
3388 return npos;
3389}
3390
3391template<class _CharT, class _Traits, class _Allocator>
3392_LIBCPP_INLINE_VISIBILITY inline
3393typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003394basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3395 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003396{
3397 return find_last_not_of(__str.data(), __pos, __str.size());
3398}
3399
3400template<class _CharT, class _Traits, class _Allocator>
3401_LIBCPP_INLINE_VISIBILITY inline
3402typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003403basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const_pointer __s,
3404 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003405{
3406#ifdef _LIBCPP_DEBUG
3407 assert(__s != 0);
3408#endif
3409 return find_last_not_of(__s, __pos, traits_type::length(__s));
3410}
3411
3412template<class _CharT, class _Traits, class _Allocator>
3413_LIBCPP_INLINE_VISIBILITY inline
3414typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003415basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3416 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003417{
3418 size_type __sz = size();
3419 if (__pos < __sz)
3420 ++__pos;
3421 else
3422 __pos = __sz;
3423 const_pointer __p = data();
3424 for (const_pointer __ps = __p + __pos; __ps != __p;)
3425 if (!traits_type::eq(*--__ps, __c))
3426 return static_cast<size_type>(__ps - __p);
3427 return npos;
3428}
3429
3430// compare
3431
3432template <class _CharT, class _Traits, class _Allocator>
3433_LIBCPP_INLINE_VISIBILITY inline
3434int
Howard Hinnanta6119a82011-05-29 19:57:12 +00003435basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003436{
Howard Hinnantfa06d752011-07-24 21:45:06 +00003437 size_t __lhs_sz = size();
3438 size_t __rhs_sz = __str.size();
3439 int __result = traits_type::compare(data(), __str.data(),
3440 _VSTD::min(__lhs_sz, __rhs_sz));
3441 if (__result != 0)
3442 return __result;
3443 if (__lhs_sz < __rhs_sz)
3444 return -1;
3445 if (__lhs_sz > __rhs_sz)
3446 return 1;
3447 return 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003448}
3449
3450template <class _CharT, class _Traits, class _Allocator>
3451_LIBCPP_INLINE_VISIBILITY inline
3452int
Howard Hinnanta6119a82011-05-29 19:57:12 +00003453basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3454 size_type __n1,
3455 const basic_string& __str) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003456{
3457 return compare(__pos1, __n1, __str.data(), __str.size());
3458}
3459
3460template <class _CharT, class _Traits, class _Allocator>
3461int
Howard Hinnanta6119a82011-05-29 19:57:12 +00003462basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3463 size_type __n1,
3464 const basic_string& __str,
3465 size_type __pos2,
3466 size_type __n2) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003467{
3468 size_type __sz = __str.size();
3469 if (__pos2 > __sz)
3470 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00003471 return compare(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003472 __sz - __pos2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003473}
3474
3475template <class _CharT, class _Traits, class _Allocator>
3476int
3477basic_string<_CharT, _Traits, _Allocator>::compare(const_pointer __s) const
3478{
3479#ifdef _LIBCPP_DEBUG
3480 assert(__s != 0);
3481#endif
3482 return compare(0, npos, __s, traits_type::length(__s));
3483}
3484
3485template <class _CharT, class _Traits, class _Allocator>
3486int
Howard Hinnanta6119a82011-05-29 19:57:12 +00003487basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3488 size_type __n1,
3489 const_pointer __s) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003490{
3491#ifdef _LIBCPP_DEBUG
3492 assert(__s != 0);
3493#endif
3494 return compare(__pos1, __n1, __s, traits_type::length(__s));
3495}
3496
3497template <class _CharT, class _Traits, class _Allocator>
3498int
Howard Hinnanta6119a82011-05-29 19:57:12 +00003499basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3500 size_type __n1,
3501 const_pointer __s,
3502 size_type __n2) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003503{
3504#ifdef _LIBCPP_DEBUG
3505 assert(__s != 0);
3506#endif
3507 size_type __sz = size();
3508 if (__pos1 > __sz || __n2 == npos)
3509 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00003510 size_type __rlen = _VSTD::min(__n1, __sz - __pos1);
3511 int __r = traits_type::compare(data() + __pos1, __s, _VSTD::min(__rlen, __n2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003512 if (__r == 0)
3513 {
3514 if (__rlen < __n2)
3515 __r = -1;
3516 else if (__rlen > __n2)
3517 __r = 1;
3518 }
3519 return __r;
3520}
3521
3522// __invariants
3523
3524template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00003525_LIBCPP_INLINE_VISIBILITY inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003526bool
3527basic_string<_CharT, _Traits, _Allocator>::__invariants() const
3528{
3529 if (size() > capacity())
3530 return false;
3531 if (capacity() < __min_cap - 1)
3532 return false;
3533 if (data() == 0)
3534 return false;
3535 if (data()[size()] != value_type(0))
3536 return false;
3537 return true;
3538}
3539
3540// operator==
3541
3542template<class _CharT, class _Traits, class _Allocator>
3543_LIBCPP_INLINE_VISIBILITY inline
3544bool
3545operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003546 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003547{
Howard Hinnanta6119a82011-05-29 19:57:12 +00003548 return __lhs.size() == __rhs.size() && _Traits::compare(__lhs.data(),
3549 __rhs.data(),
3550 __lhs.size()) == 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003551}
3552
3553template<class _CharT, class _Traits, class _Allocator>
3554_LIBCPP_INLINE_VISIBILITY inline
3555bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003556operator==(const _CharT* __lhs,
3557 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003558{
3559 return __rhs.compare(__lhs) == 0;
3560}
3561
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003562template<class _CharT, class _Traits, class _Allocator>
3563_LIBCPP_INLINE_VISIBILITY inline
3564bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003565operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
3566 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003567{
3568 return __lhs.compare(__rhs) == 0;
3569}
3570
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003571// operator!=
3572
Howard Hinnant324bb032010-08-22 00:02:43 +00003573template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003574_LIBCPP_INLINE_VISIBILITY inline
3575bool
3576operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003577 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003578{
3579 return !(__lhs == __rhs);
3580}
3581
3582template<class _CharT, class _Traits, class _Allocator>
3583_LIBCPP_INLINE_VISIBILITY inline
3584bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003585operator!=(const _CharT* __lhs,
3586 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003587{
3588 return !(__lhs == __rhs);
3589}
3590
3591template<class _CharT, class _Traits, class _Allocator>
3592_LIBCPP_INLINE_VISIBILITY inline
3593bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003594operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3595 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003596{
3597 return !(__lhs == __rhs);
3598}
3599
3600// operator<
3601
3602template<class _CharT, class _Traits, class _Allocator>
3603_LIBCPP_INLINE_VISIBILITY inline
3604bool
3605operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003606 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003607{
Howard Hinnant2644a7b2011-07-24 15:07:21 +00003608 return __lhs.compare(__rhs) < 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003609}
3610
3611template<class _CharT, class _Traits, class _Allocator>
3612_LIBCPP_INLINE_VISIBILITY inline
3613bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003614operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3615 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003616{
Howard Hinnant2644a7b2011-07-24 15:07:21 +00003617 return __lhs.compare(__rhs) < 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003618}
3619
3620template<class _CharT, class _Traits, class _Allocator>
3621_LIBCPP_INLINE_VISIBILITY inline
3622bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003623operator< (const _CharT* __lhs,
3624 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003625{
3626 return __rhs.compare(__lhs) > 0;
3627}
3628
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003629// operator>
3630
3631template<class _CharT, class _Traits, class _Allocator>
3632_LIBCPP_INLINE_VISIBILITY inline
3633bool
3634operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003635 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003636{
3637 return __rhs < __lhs;
3638}
3639
3640template<class _CharT, class _Traits, class _Allocator>
3641_LIBCPP_INLINE_VISIBILITY inline
3642bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003643operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3644 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003645{
3646 return __rhs < __lhs;
3647}
3648
3649template<class _CharT, class _Traits, class _Allocator>
3650_LIBCPP_INLINE_VISIBILITY inline
3651bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003652operator> (const _CharT* __lhs,
3653 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003654{
3655 return __rhs < __lhs;
3656}
3657
3658// operator<=
3659
3660template<class _CharT, class _Traits, class _Allocator>
3661_LIBCPP_INLINE_VISIBILITY inline
3662bool
3663operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003664 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003665{
3666 return !(__rhs < __lhs);
3667}
3668
3669template<class _CharT, class _Traits, class _Allocator>
3670_LIBCPP_INLINE_VISIBILITY inline
3671bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003672operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3673 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003674{
3675 return !(__rhs < __lhs);
3676}
3677
3678template<class _CharT, class _Traits, class _Allocator>
3679_LIBCPP_INLINE_VISIBILITY inline
3680bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003681operator<=(const _CharT* __lhs,
3682 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003683{
3684 return !(__rhs < __lhs);
3685}
3686
3687// operator>=
3688
3689template<class _CharT, class _Traits, class _Allocator>
3690_LIBCPP_INLINE_VISIBILITY inline
3691bool
3692operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003693 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003694{
3695 return !(__lhs < __rhs);
3696}
3697
3698template<class _CharT, class _Traits, class _Allocator>
3699_LIBCPP_INLINE_VISIBILITY inline
3700bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003701operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3702 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003703{
3704 return !(__lhs < __rhs);
3705}
3706
3707template<class _CharT, class _Traits, class _Allocator>
3708_LIBCPP_INLINE_VISIBILITY inline
3709bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003710operator>=(const _CharT* __lhs,
3711 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003712{
3713 return !(__lhs < __rhs);
3714}
3715
3716// operator +
3717
3718template<class _CharT, class _Traits, class _Allocator>
3719basic_string<_CharT, _Traits, _Allocator>
3720operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3721 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3722{
3723 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
3724 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
3725 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
3726 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
3727 __r.append(__rhs.data(), __rhs_sz);
3728 return __r;
3729}
3730
3731template<class _CharT, class _Traits, class _Allocator>
3732basic_string<_CharT, _Traits, _Allocator>
3733operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
3734{
3735 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
3736 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = _Traits::length(__lhs);
3737 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
3738 __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz);
3739 __r.append(__rhs.data(), __rhs_sz);
3740 return __r;
3741}
3742
3743template<class _CharT, class _Traits, class _Allocator>
3744basic_string<_CharT, _Traits, _Allocator>
3745operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
3746{
3747 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
3748 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
3749 __r.__init(&__lhs, 1, 1 + __rhs_sz);
3750 __r.append(__rhs.data(), __rhs_sz);
3751 return __r;
3752}
3753
3754template<class _CharT, class _Traits, class _Allocator>
3755basic_string<_CharT, _Traits, _Allocator>
3756operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
3757{
3758 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
3759 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
3760 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = _Traits::length(__rhs);
3761 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
3762 __r.append(__rhs, __rhs_sz);
3763 return __r;
3764}
3765
3766template<class _CharT, class _Traits, class _Allocator>
3767basic_string<_CharT, _Traits, _Allocator>
3768operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
3769{
3770 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
3771 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
3772 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1);
3773 __r.push_back(__rhs);
3774 return __r;
3775}
3776
Howard Hinnant73d21a42010-09-04 23:28:19 +00003777#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003778
3779template<class _CharT, class _Traits, class _Allocator>
3780_LIBCPP_INLINE_VISIBILITY inline
3781basic_string<_CharT, _Traits, _Allocator>
3782operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3783{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003784 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003785}
3786
3787template<class _CharT, class _Traits, class _Allocator>
3788_LIBCPP_INLINE_VISIBILITY inline
3789basic_string<_CharT, _Traits, _Allocator>
3790operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
3791{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003792 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003793}
3794
3795template<class _CharT, class _Traits, class _Allocator>
3796_LIBCPP_INLINE_VISIBILITY inline
3797basic_string<_CharT, _Traits, _Allocator>
3798operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
3799{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003800 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003801}
3802
3803template<class _CharT, class _Traits, class _Allocator>
3804_LIBCPP_INLINE_VISIBILITY inline
3805basic_string<_CharT, _Traits, _Allocator>
3806operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
3807{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003808 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003809}
3810
3811template<class _CharT, class _Traits, class _Allocator>
3812_LIBCPP_INLINE_VISIBILITY inline
3813basic_string<_CharT, _Traits, _Allocator>
3814operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
3815{
3816 __rhs.insert(__rhs.begin(), __lhs);
Howard Hinnant0949eed2011-06-30 21:18:19 +00003817 return _VSTD::move(__rhs);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003818}
3819
3820template<class _CharT, class _Traits, class _Allocator>
3821_LIBCPP_INLINE_VISIBILITY inline
3822basic_string<_CharT, _Traits, _Allocator>
3823operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
3824{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003825 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003826}
3827
3828template<class _CharT, class _Traits, class _Allocator>
3829_LIBCPP_INLINE_VISIBILITY inline
3830basic_string<_CharT, _Traits, _Allocator>
3831operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
3832{
3833 __lhs.push_back(__rhs);
Howard Hinnant0949eed2011-06-30 21:18:19 +00003834 return _VSTD::move(__lhs);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003835}
3836
Howard Hinnant73d21a42010-09-04 23:28:19 +00003837#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003838
3839// swap
3840
3841template<class _CharT, class _Traits, class _Allocator>
3842_LIBCPP_INLINE_VISIBILITY inline
3843void
Howard Hinnanta6119a82011-05-29 19:57:12 +00003844swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00003845 basic_string<_CharT, _Traits, _Allocator>& __rhs)
3846 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003847{
3848 __lhs.swap(__rhs);
3849}
3850
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003851#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
3852
3853typedef basic_string<char16_t> u16string;
3854typedef basic_string<char32_t> u32string;
3855
Howard Hinnant324bb032010-08-22 00:02:43 +00003856#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003857
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003858int stoi (const string& __str, size_t* __idx = 0, int __base = 10);
3859long stol (const string& __str, size_t* __idx = 0, int __base = 10);
3860unsigned long stoul (const string& __str, size_t* __idx = 0, int __base = 10);
3861long long stoll (const string& __str, size_t* __idx = 0, int __base = 10);
3862unsigned long long stoull(const string& __str, size_t* __idx = 0, int __base = 10);
3863
3864float stof (const string& __str, size_t* __idx = 0);
3865double stod (const string& __str, size_t* __idx = 0);
3866long double stold(const string& __str, size_t* __idx = 0);
3867
3868string to_string(int __val);
3869string to_string(unsigned __val);
3870string to_string(long __val);
3871string to_string(unsigned long __val);
3872string to_string(long long __val);
3873string to_string(unsigned long long __val);
3874string to_string(float __val);
3875string to_string(double __val);
3876string to_string(long double __val);
3877
3878int stoi (const wstring& __str, size_t* __idx = 0, int __base = 10);
3879long stol (const wstring& __str, size_t* __idx = 0, int __base = 10);
3880unsigned long stoul (const wstring& __str, size_t* __idx = 0, int __base = 10);
3881long long stoll (const wstring& __str, size_t* __idx = 0, int __base = 10);
3882unsigned long long stoull(const wstring& __str, size_t* __idx = 0, int __base = 10);
3883
3884float stof (const wstring& __str, size_t* __idx = 0);
3885double stod (const wstring& __str, size_t* __idx = 0);
3886long double stold(const wstring& __str, size_t* __idx = 0);
3887
3888wstring to_wstring(int __val);
3889wstring to_wstring(unsigned __val);
3890wstring to_wstring(long __val);
3891wstring to_wstring(unsigned long __val);
3892wstring to_wstring(long long __val);
3893wstring to_wstring(unsigned long long __val);
3894wstring to_wstring(float __val);
3895wstring to_wstring(double __val);
3896wstring to_wstring(long double __val);
3897
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003898template<class _CharT, class _Traits, class _Allocator>
3899 const typename basic_string<_CharT, _Traits, _Allocator>::size_type
3900 basic_string<_CharT, _Traits, _Allocator>::npos;
3901
Sean Huntaffd9e52011-07-29 23:31:56 +00003902template<class _Ptr>
3903size_t _LIBCPP_INLINE_VISIBILITY __do_string_hash(_Ptr __p, _Ptr __e)
3904{
3905 size_t __r = 0;
3906 const size_t __sr = __CHAR_BIT__ * sizeof(size_t) - 8;
3907 const size_t __m = size_t(0xF) << (__sr + 4);
3908 for (; __p != __e; ++__p)
3909 {
3910 __r = (__r << 4) + *__p;
3911 size_t __g = __r & __m;
3912 __r ^= __g | (__g >> __sr);
3913 }
3914 return __r;
3915}
3916
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003917template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant8d7a9552010-09-23 17:31:07 +00003918struct _LIBCPP_VISIBLE hash<basic_string<_CharT, _Traits, _Allocator> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003919 : public unary_function<basic_string<_CharT, _Traits, _Allocator>, size_t>
3920{
3921 size_t
Howard Hinnanta6119a82011-05-29 19:57:12 +00003922 operator()(const basic_string<_CharT, _Traits, _Allocator>& __val) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003923};
3924
3925template<class _CharT, class _Traits, class _Allocator>
3926size_t
3927hash<basic_string<_CharT, _Traits, _Allocator> >::operator()(
Howard Hinnanta6119a82011-05-29 19:57:12 +00003928 const basic_string<_CharT, _Traits, _Allocator>& __val) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003929{
Sean Huntaffd9e52011-07-29 23:31:56 +00003930 return __do_string_hash(__val.data(), __val.data() + __val.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003931}
3932
Howard Hinnant464aa5c2011-07-18 15:51:59 +00003933template<class _CharT, class _Traits, class _Allocator>
3934basic_ostream<_CharT, _Traits>&
3935operator<<(basic_ostream<_CharT, _Traits>& __os,
3936 const basic_string<_CharT, _Traits, _Allocator>& __str);
3937
3938template<class _CharT, class _Traits, class _Allocator>
3939basic_istream<_CharT, _Traits>&
3940operator>>(basic_istream<_CharT, _Traits>& __is,
3941 basic_string<_CharT, _Traits, _Allocator>& __str);
3942
3943template<class _CharT, class _Traits, class _Allocator>
3944basic_istream<_CharT, _Traits>&
3945getline(basic_istream<_CharT, _Traits>& __is,
3946 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
3947
3948template<class _CharT, class _Traits, class _Allocator>
3949inline _LIBCPP_INLINE_VISIBILITY
3950basic_istream<_CharT, _Traits>&
3951getline(basic_istream<_CharT, _Traits>& __is,
3952 basic_string<_CharT, _Traits, _Allocator>& __str);
3953
3954#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
3955
3956template<class _CharT, class _Traits, class _Allocator>
3957inline _LIBCPP_INLINE_VISIBILITY
3958basic_istream<_CharT, _Traits>&
3959getline(basic_istream<_CharT, _Traits>&& __is,
3960 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
3961
3962template<class _CharT, class _Traits, class _Allocator>
3963inline _LIBCPP_INLINE_VISIBILITY
3964basic_istream<_CharT, _Traits>&
3965getline(basic_istream<_CharT, _Traits>&& __is,
3966 basic_string<_CharT, _Traits, _Allocator>& __str);
3967
3968#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
3969
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003970extern template class basic_string<char>;
3971extern template class basic_string<wchar_t>;
3972
3973extern template
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003974 string
3975 operator+<char, char_traits<char>, allocator<char> >(char const*, string const&);
3976
3977_LIBCPP_END_NAMESPACE_STD
3978
3979#endif // _LIBCPP_STRING