blob: 61f993a957891f9763d88ef568949a649af69750 [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
53 static void assign(char_type& c1, const char_type& c2);
54 static bool eq(char_type c1, char_type c2);
55 static bool lt(char_type c1, char_type c2);
56
57 static int compare(const char_type* s1, const char_type* s2, size_t n);
58 static size_t length(const char_type* s);
59 static const char_type* find(const char_type* s, size_t n, const char_type& a);
60 static char_type* move(char_type* s1, const char_type* s2, size_t n);
61 static char_type* copy(char_type* s1, const char_type* s2, size_t n);
62 static char_type* assign(char_type* s, size_t n, char_type a);
63
64 static int_type not_eof(int_type c);
65 static char_type to_char_type(int_type c);
66 static int_type to_int_type(char_type c);
67 static bool eq_int_type(int_type c1, int_type c2);
68 static int_type eof();
69};
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
95 explicit basic_string(const allocator_type& a = allocator_type());
96 basic_string(const basic_string& str);
97 basic_string(basic_string&& str);
Howard Hinnanta6a062d2010-06-02 18:20:39 +000098 basic_string(const basic_string& str, size_type pos, size_type n = npos,
99 const allocator_type& a = allocator_type());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000100 basic_string(const_pointer s, const allocator_type& a = allocator_type());
101 basic_string(const_pointer s, size_type n, const allocator_type& a = allocator_type());
102 basic_string(size_type n, value_type c, const allocator_type& a = allocator_type());
103 template<class InputIterator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000104 basic_string(InputIterator begin, InputIterator end,
105 const allocator_type& a = allocator_type());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000106 basic_string(initializer_list<value_type>, const Allocator& = Allocator());
107 basic_string(const basic_string&, const Allocator&);
108 basic_string(basic_string&&, const Allocator&);
109
110 ~basic_string();
111
112 basic_string& operator=(const basic_string& str);
113 basic_string& operator=(const_pointer s);
114 basic_string& operator=(value_type c);
115 basic_string& operator=(initializer_list<value_type>);
116
117 iterator begin();
118 const_iterator begin() const;
119 iterator end();
120 const_iterator end() const;
121
122 reverse_iterator rbegin();
123 const_reverse_iterator rbegin() const;
124 reverse_iterator rend();
125 const_reverse_iterator rend() const;
126
127 const_iterator cbegin() const;
128 const_iterator cend() const;
129 const_reverse_iterator crbegin() const;
130 const_reverse_iterator crend() const;
131
132 size_type size() const;
133 size_type length() const;
134 size_type max_size() const;
135 size_type capacity() const;
136
137 void resize(size_type n, value_type c);
138 void resize(size_type n);
139
140 void reserve(size_type res_arg = 0);
141 void shrink_to_fit();
142 void clear();
143 bool empty() const;
144
145 const_reference operator[](size_type pos) const;
146 reference operator[](size_type pos);
147
148 const_reference at(size_type n) const;
149 reference at(size_type n);
150
151 basic_string& operator+=(const basic_string& str);
152 basic_string& operator+=(const_pointer s);
153 basic_string& operator+=(value_type c);
154 basic_string& operator+=(initializer_list<value_type>);
155
156 basic_string& append(const basic_string& str);
157 basic_string& append(const basic_string& str, size_type pos, size_type n);
158 basic_string& append(const_pointer s, size_type n);
159 basic_string& append(const_pointer s);
160 basic_string& append(size_type n, value_type c);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000161 template<class InputIterator>
162 basic_string& append(InputIterator first, InputIterator last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000163 basic_string& append(initializer_list<value_type>);
164
165 void push_back(value_type c);
166 void pop_back();
167 reference front();
168 const_reference front() const;
169 reference back();
170 const_reference back() const;
171
172 basic_string& assign(const basic_string& str);
173 basic_string& assign(const basic_string& str, size_type pos, size_type n);
174 basic_string& assign(const_pointer s, size_type n);
175 basic_string& assign(const_pointer s);
176 basic_string& assign(size_type n, value_type c);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000177 template<class InputIterator>
178 basic_string& assign(InputIterator first, InputIterator last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000179 basic_string& assign(initializer_list<value_type>);
180
181 basic_string& insert(size_type pos1, const basic_string& str);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000182 basic_string& insert(size_type pos1, const basic_string& str,
183 size_type pos2, size_type n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000184 basic_string& insert(size_type pos, const_pointer s, size_type n);
185 basic_string& insert(size_type pos, const_pointer s);
186 basic_string& insert(size_type pos, size_type n, value_type c);
187 iterator insert(const_iterator p, value_type c);
188 iterator insert(const_iterator p, size_type n, value_type c);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000189 template<class InputIterator>
190 iterator insert(const_iterator p, InputIterator first, InputIterator last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000191 iterator insert(const_iterator p, initializer_list<value_type>);
192
193 basic_string& erase(size_type pos = 0, size_type n = npos);
194 iterator erase(const_iterator position);
195 iterator erase(const_iterator first, const_iterator last);
196
197 basic_string& replace(size_type pos1, size_type n1, const basic_string& str);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000198 basic_string& replace(size_type pos1, size_type n1, const basic_string& str,
199 size_type pos2, size_type n2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000200 basic_string& replace(size_type pos, size_type n1, const_pointer s, size_type n2);
201 basic_string& replace(size_type pos, size_type n1, const_pointer s);
202 basic_string& replace(size_type pos, size_type n1, size_type n2, value_type c);
203 basic_string& replace(iterator i1, iterator i2, const basic_string& str);
204 basic_string& replace(iterator i1, iterator i2, const_pointer s, size_type n);
205 basic_string& replace(iterator i1, iterator i2, const_pointer s);
206 basic_string& replace(iterator i1, iterator i2, size_type n, value_type c);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000207 template<class InputIterator>
208 basic_string& replace(iterator i1, iterator i2, InputIterator j1, InputIterator j2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000209 basic_string& replace(iterator i1, iterator i2, initializer_list<value_type>);
210
211 size_type copy(pointer s, size_type n, size_type pos = 0) const;
212 basic_string substr(size_type pos = 0, size_type n = npos) const;
213
214 void swap(basic_string& str);
215
216 const_pointer c_str() const;
217 const_pointer data() const;
218
219 allocator_type get_allocator() const;
220
221 size_type find(const basic_string& str, size_type pos = 0) const;
222 size_type find(const_pointer s, size_type pos, size_type n) const;
223 size_type find(const_pointer s, size_type pos = 0) const;
224 size_type find(value_type c, size_type pos = 0) const;
225
226 size_type rfind(const basic_string& str, size_type pos = npos) const;
227 size_type rfind(const_pointer s, size_type pos, size_type n) const;
228 size_type rfind(const_pointer s, size_type pos = npos) const;
229 size_type rfind(value_type c, size_type pos = npos) const;
230
231 size_type find_first_of(const basic_string& str, size_type pos = 0) const;
232 size_type find_first_of(const_pointer s, size_type pos, size_type n) const;
233 size_type find_first_of(const_pointer s, size_type pos = 0) const;
234 size_type find_first_of(value_type c, size_type pos = 0) const;
235
236 size_type find_last_of(const basic_string& str, size_type pos = npos) const;
237 size_type find_last_of(const_pointer s, size_type pos, size_type n) const;
238 size_type find_last_of(const_pointer s, size_type pos = npos) const;
239 size_type find_last_of(value_type c, size_type pos = npos) const;
240
241 size_type find_first_not_of(const basic_string& str, size_type pos = 0) const;
242 size_type find_first_not_of(const_pointer s, size_type pos, size_type n) const;
243 size_type find_first_not_of(const_pointer s, size_type pos = 0) const;
244 size_type find_first_not_of(value_type c, size_type pos = 0) const;
245
246 size_type find_last_not_of(const basic_string& str, size_type pos = npos) const;
247 size_type find_last_not_of(const_pointer s, size_type pos, size_type n) const;
248 size_type find_last_not_of(const_pointer s, size_type pos = npos) const;
249 size_type find_last_not_of(value_type c, size_type pos = npos) const;
250
251 int compare(const basic_string& str) const;
252 int compare(size_type pos1, size_type n1, const basic_string& str) const;
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000253 int compare(size_type pos1, size_type n1, const basic_string& str,
254 size_type pos2, size_type n2) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000255 int compare(const_pointer s) const;
256 int compare(size_type pos1, size_type n1, const_pointer s) const;
257 int compare(size_type pos1, size_type n1, const_pointer s, size_type n2) const;
258
259 bool __invariants() const;
260};
261
262template<class charT, class traits, class Allocator>
263basic_string<charT, traits, Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000264operator+(const basic_string<charT, traits, Allocator>& lhs,
265 const basic_string<charT, traits, Allocator>& rhs);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000266
267template<class charT, class traits, class Allocator>
268basic_string<charT, traits, Allocator>
269operator+(const charT* lhs , const basic_string<charT,traits,Allocator>&rhs);
270
271template<class charT, class traits, class Allocator>
272basic_string<charT, traits, Allocator>
273operator+(charT lhs, const basic_string<charT,traits,Allocator>& rhs);
274
275template<class charT, class traits, class Allocator>
276basic_string<charT, traits, Allocator>
277operator+(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs);
278
279template<class charT, class traits, class Allocator>
280basic_string<charT, traits, Allocator>
281operator+(const basic_string<charT, traits, Allocator>& lhs, charT rhs);
282
283template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000284bool operator==(const basic_string<charT, traits, Allocator>& lhs,
285 const basic_string<charT, traits, Allocator>& rhs);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000286
287template<class charT, class traits, class Allocator>
288bool operator==(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs);
289
290template<class charT, class traits, class Allocator>
291bool operator==(const basic_string<charT,traits,Allocator>& lhs, const charT* rhs);
292
Howard Hinnant324bb032010-08-22 00:02:43 +0000293template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000294bool operator!=(const basic_string<charT,traits,Allocator>& lhs,
295 const basic_string<charT, traits, Allocator>& rhs);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000296
297template<class charT, class traits, class Allocator>
298bool operator!=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs);
299
300template<class charT, class traits, class Allocator>
301bool operator!=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs);
302
303template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000304bool operator< (const basic_string<charT, traits, Allocator>& lhs,
305 const basic_string<charT, traits, Allocator>& rhs);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000306
307template<class charT, class traits, class Allocator>
308bool operator< (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs);
309
310template<class charT, class traits, class Allocator>
311bool operator< (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs);
312
313template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000314bool operator> (const basic_string<charT, traits, Allocator>& lhs,
315 const basic_string<charT, traits, Allocator>& rhs);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000316
317template<class charT, class traits, class Allocator>
318bool operator> (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs);
319
320template<class charT, class traits, class Allocator>
321bool operator> (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs);
322
323template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000324bool operator<=(const basic_string<charT, traits, Allocator>& lhs,
325 const basic_string<charT, traits, Allocator>& rhs);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000326
327template<class charT, class traits, class Allocator>
328bool operator<=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs);
329
330template<class charT, class traits, class Allocator>
331bool operator<=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs);
332
333template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000334bool operator>=(const basic_string<charT, traits, Allocator>& lhs,
335 const basic_string<charT, traits, Allocator>& rhs);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000336
337template<class charT, class traits, class Allocator>
338bool operator>=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs);
339
340template<class charT, class traits, class Allocator>
341bool operator>=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs);
342
343template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000344void swap(basic_string<charT, traits, Allocator>& lhs,
345 basic_string<charT, traits, Allocator>& rhs);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000346
347template<class charT, class traits, class Allocator>
348basic_istream<charT, traits>&
349operator>>(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
350
351template<class charT, class traits, class Allocator>
352basic_ostream<charT, traits>&
353operator<<(basic_ostream<charT, traits>& os, const basic_string<charT, traits, Allocator>& str);
354
355template<class charT, class traits, class Allocator>
Howard Hinnant324bb032010-08-22 00:02:43 +0000356basic_istream<charT, traits>&
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000357getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str,
358 charT delim);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000359
360template<class charT, class traits, class Allocator>
361basic_istream<charT, traits>&
362getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
363
364typedef basic_string<char> string;
365typedef basic_string<wchar_t> wstring;
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000366typedef basic_string<char16_t> u16string;
367typedef basic_string<char32_t> u32string;
368
369int stoi (const string& str, size_t* idx = 0, int base = 10);
370long stol (const string& str, size_t* idx = 0, int base = 10);
371unsigned long stoul (const string& str, size_t* idx = 0, int base = 10);
372long long stoll (const string& str, size_t* idx = 0, int base = 10);
373unsigned long long stoull(const string& str, size_t* idx = 0, int base = 10);
374
375float stof (const string& str, size_t* idx = 0);
376double stod (const string& str, size_t* idx = 0);
377long double stold(const string& str, size_t* idx = 0);
378
379string to_string(int val);
380string to_string(unsigned val);
381string to_string(long val);
382string to_string(unsigned long val);
383string to_string(long long val);
384string to_string(unsigned long long val);
385string to_string(float val);
386string to_string(double val);
387string to_string(long double val);
388
389int stoi (const wstring& str, size_t* idx = 0, int base = 10);
390long stol (const wstring& str, size_t* idx = 0, int base = 10);
391unsigned long stoul (const wstring& str, size_t* idx = 0, int base = 10);
392long long stoll (const wstring& str, size_t* idx = 0, int base = 10);
393unsigned long long stoull(const wstring& str, size_t* idx = 0, int base = 10);
394
395float stof (const wstring& str, size_t* idx = 0);
396double stod (const wstring& str, size_t* idx = 0);
397long double stold(const wstring& str, size_t* idx = 0);
398
399wstring to_wstring(int val);
400wstring to_wstring(unsigned val);
401wstring to_wstring(long val);
402wstring to_wstring(unsigned long val);
403wstring to_wstring(long long val);
404wstring to_wstring(unsigned long long val);
405wstring to_wstring(float val);
406wstring to_wstring(double val);
407wstring to_wstring(long double val);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000408
409template <> struct hash<string>;
410template <> struct hash<u16string>;
411template <> struct hash<u32string>;
412template <> struct hash<wstring>;
413
414} // std
415
416*/
417
418#include <__config>
419#include <iosfwd>
420#include <cstring>
Howard Hinnantadff4892010-05-24 17:49:41 +0000421#include <cstdio> // For EOF.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000422#include <cwchar>
423#include <algorithm>
424#include <iterator>
425#include <utility>
426#include <memory>
427#include <stdexcept>
428#include <type_traits>
429#include <initializer_list>
430#include <__functional_base>
431#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
432#include <cstdint>
433#endif
434#if defined(_LIBCPP_NO_EXCEPTIONS) || defined(_LIBCPP_DEBUG)
435#include <cassert>
436#endif
437
438#pragma GCC system_header
439
440_LIBCPP_BEGIN_NAMESPACE_STD
441
442// fpos
443
444template <class _StateT>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000445class _LIBCPP_VISIBLE fpos
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000446{
447private:
448 _StateT __st_;
449 streamoff __off_;
450public:
451 _LIBCPP_INLINE_VISIBILITY fpos(streamoff __off = streamoff()) : __st_(), __off_(__off) {}
452
453 _LIBCPP_INLINE_VISIBILITY operator streamoff() const {return __off_;}
454
455 _LIBCPP_INLINE_VISIBILITY _StateT state() const {return __st_;}
456 _LIBCPP_INLINE_VISIBILITY void state(_StateT __st) {__st_ = __st;}
457
458 _LIBCPP_INLINE_VISIBILITY fpos& operator+=(streamoff __off) {__off_ += __off; return *this;}
459 _LIBCPP_INLINE_VISIBILITY fpos operator+ (streamoff __off) const {fpos __t(*this); __t += __off; return __t;}
460 _LIBCPP_INLINE_VISIBILITY fpos& operator-=(streamoff __off) {__off_ -= __off; return *this;}
461 _LIBCPP_INLINE_VISIBILITY fpos operator- (streamoff __off) const {fpos __t(*this); __t -= __off; return __t;}
462};
463
464template <class _StateT>
465inline _LIBCPP_INLINE_VISIBILITY
466streamoff operator-(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
467 {return streamoff(__x) - streamoff(__y);}
468
469template <class _StateT>
470inline _LIBCPP_INLINE_VISIBILITY
471bool operator==(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
472 {return streamoff(__x) == streamoff(__y);}
473
474template <class _StateT>
475inline _LIBCPP_INLINE_VISIBILITY
476bool operator!=(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
477 {return streamoff(__x) != streamoff(__y);}
478
479// char_traits
480
481template <class _CharT>
Howard Hinnant36cdf022010-09-10 16:42:26 +0000482struct _LIBCPP_VISIBLE char_traits
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000483{
484 typedef _CharT char_type;
485 typedef int int_type;
486 typedef streamoff off_type;
487 typedef streampos pos_type;
488 typedef mbstate_t state_type;
489
490 _LIBCPP_INLINE_VISIBILITY static void assign(char_type& __c1, const char_type& __c2) {__c1 = __c2;}
491 _LIBCPP_INLINE_VISIBILITY static bool eq(char_type __c1, char_type __c2) {return __c1 == __c2;}
492 _LIBCPP_INLINE_VISIBILITY static bool lt(char_type __c1, char_type __c2) {return __c1 < __c2;}
493
494 static int compare(const char_type* __s1, const char_type* __s2, size_t __n);
495 static size_t length(const char_type* __s);
496 static const char_type* find(const char_type* __s, size_t __n, const char_type& __a);
497 static char_type* move(char_type* __s1, const char_type* __s2, size_t __n);
498 static char_type* copy(char_type* __s1, const char_type* __s2, size_t __n);
499 static char_type* assign(char_type* __s, size_t __n, char_type __a);
500
501 _LIBCPP_INLINE_VISIBILITY static int_type not_eof(int_type __c)
502 {return eq_int_type(__c, eof()) ? ~eof() : __c;}
503 _LIBCPP_INLINE_VISIBILITY static char_type to_char_type(int_type __c) {return char_type(__c);}
504 _LIBCPP_INLINE_VISIBILITY static int_type to_int_type(char_type __c) {return int_type(__c);}
505 _LIBCPP_INLINE_VISIBILITY static bool eq_int_type(int_type __c1, int_type __c2)
506 {return __c1 == __c2;}
507 _LIBCPP_INLINE_VISIBILITY static int_type eof() {return int_type(EOF);}
508};
509
510template <class _CharT>
511int
512char_traits<_CharT>::compare(const char_type* __s1, const char_type* __s2, size_t __n)
513{
514 for (; __n; --__n, ++__s1, ++__s2)
515 {
516 if (lt(*__s1, *__s2))
517 return -1;
518 if (lt(*__s2, *__s1))
519 return 1;
520 }
521 return 0;
522}
523
524template <class _CharT>
525inline _LIBCPP_INLINE_VISIBILITY
526size_t
527char_traits<_CharT>::length(const char_type* __s)
528{
529 size_t __len = 0;
530 for (; !eq(*__s, char_type(0)); ++__s)
531 ++__len;
532 return __len;
533}
534
535template <class _CharT>
536inline _LIBCPP_INLINE_VISIBILITY
537const _CharT*
538char_traits<_CharT>::find(const char_type* __s, size_t __n, const char_type& __a)
539{
540 for (; __n; --__n)
541 {
542 if (eq(*__s, __a))
543 return __s;
544 ++__s;
545 }
546 return 0;
547}
548
549template <class _CharT>
550_CharT*
551char_traits<_CharT>::move(char_type* __s1, const char_type* __s2, size_t __n)
552{
553 char_type* __r = __s1;
554 if (__s1 < __s2)
555 {
556 for (; __n; --__n, ++__s1, ++__s2)
557 assign(*__s1, *__s2);
558 }
559 else if (__s2 < __s1)
560 {
561 __s1 += __n;
562 __s2 += __n;
563 for (; __n; --__n)
564 assign(*--__s1, *--__s2);
565 }
566 return __r;
567}
568
569template <class _CharT>
570inline _LIBCPP_INLINE_VISIBILITY
571_CharT*
572char_traits<_CharT>::copy(char_type* __s1, const char_type* __s2, size_t __n)
573{
574 char_type* __r = __s1;
575 for (; __n; --__n, ++__s1, ++__s2)
576 assign(*__s1, *__s2);
577 return __r;
578}
579
580template <class _CharT>
581inline _LIBCPP_INLINE_VISIBILITY
582_CharT*
583char_traits<_CharT>::assign(char_type* __s, size_t __n, char_type __a)
584{
585 char_type* __r = __s;
586 for (; __n; --__n, ++__s)
587 assign(*__s, __a);
588 return __r;
589}
590
591// char_traits<char>
592
593template <>
Howard Hinnant36cdf022010-09-10 16:42:26 +0000594struct _LIBCPP_VISIBLE char_traits<char>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000595{
596 typedef char char_type;
597 typedef int int_type;
598 typedef streamoff off_type;
599 typedef streampos pos_type;
600 typedef mbstate_t state_type;
601
602 _LIBCPP_INLINE_VISIBILITY static void assign(char_type& __c1, const char_type& __c2) {__c1 = __c2;}
603 _LIBCPP_INLINE_VISIBILITY static bool eq(char_type __c1, char_type __c2) {return __c1 == __c2;}
604 _LIBCPP_INLINE_VISIBILITY static bool lt(char_type __c1, char_type __c2)
605 {return (unsigned char)__c1 < (unsigned char)__c2;}
606
607 _LIBCPP_INLINE_VISIBILITY static int compare(const char_type* __s1, const char_type* __s2, size_t __n)
608 {return memcmp(__s1, __s2, __n);}
609 _LIBCPP_INLINE_VISIBILITY static size_t length(const char_type* __s) {return strlen(__s);}
610 _LIBCPP_INLINE_VISIBILITY static const char_type* find(const char_type* __s, size_t __n, const char_type& __a)
611 {return (const char_type*)memchr(__s, to_int_type(__a), __n);}
612 _LIBCPP_INLINE_VISIBILITY static char_type* move(char_type* __s1, const char_type* __s2, size_t __n)
613 {return (char_type*)memmove(__s1, __s2, __n);}
614 _LIBCPP_INLINE_VISIBILITY static char_type* copy(char_type* __s1, const char_type* __s2, size_t __n)
615 {return (char_type*)memcpy(__s1, __s2, __n);}
616 _LIBCPP_INLINE_VISIBILITY static char_type* assign(char_type* __s, size_t __n, char_type __a)
617 {return (char_type*)memset(__s, to_int_type(__a), __n);}
618
619 _LIBCPP_INLINE_VISIBILITY static int_type not_eof(int_type __c)
620 {return eq_int_type(__c, eof()) ? ~eof() : __c;}
621 _LIBCPP_INLINE_VISIBILITY static char_type to_char_type(int_type __c) {return char_type(__c);}
622 _LIBCPP_INLINE_VISIBILITY static int_type to_int_type(char_type __c) {return int_type((unsigned char)__c);}
623 _LIBCPP_INLINE_VISIBILITY static bool eq_int_type(int_type __c1, int_type __c2)
624 {return __c1 == __c2;}
625 _LIBCPP_INLINE_VISIBILITY static int_type eof() {return int_type(EOF);}
626};
627
628// char_traits<wchar_t>
629
630template <>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000631struct _LIBCPP_VISIBLE char_traits<wchar_t>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000632{
633 typedef wchar_t char_type;
634 typedef wint_t int_type;
635 typedef streamoff off_type;
636 typedef streampos pos_type;
637 typedef mbstate_t state_type;
638
639 _LIBCPP_INLINE_VISIBILITY static void assign(char_type& __c1, const char_type& __c2) {__c1 = __c2;}
640 _LIBCPP_INLINE_VISIBILITY static bool eq(char_type __c1, char_type __c2) {return __c1 == __c2;}
641 _LIBCPP_INLINE_VISIBILITY static bool lt(char_type __c1, char_type __c2)
642 {return __c1 < __c2;}
643
644 _LIBCPP_INLINE_VISIBILITY static int compare(const char_type* __s1, const char_type* __s2, size_t __n)
645 {return wmemcmp(__s1, __s2, __n);}
646 _LIBCPP_INLINE_VISIBILITY static size_t length(const char_type* __s) {return wcslen(__s);}
647 _LIBCPP_INLINE_VISIBILITY static const char_type* find(const char_type* __s, size_t __n, const char_type& __a)
648 {return (const char_type*)wmemchr(__s, __a, __n);}
649 _LIBCPP_INLINE_VISIBILITY static char_type* move(char_type* __s1, const char_type* __s2, size_t __n)
650 {return (char_type*)wmemmove(__s1, __s2, __n);}
651 _LIBCPP_INLINE_VISIBILITY static char_type* copy(char_type* __s1, const char_type* __s2, size_t __n)
652 {return (char_type*)wmemcpy(__s1, __s2, __n);}
653 _LIBCPP_INLINE_VISIBILITY static char_type* assign(char_type* __s, size_t __n, char_type __a)
654 {return (char_type*)wmemset(__s, __a, __n);}
655
656 _LIBCPP_INLINE_VISIBILITY static int_type not_eof(int_type __c)
657 {return eq_int_type(__c, eof()) ? ~eof() : __c;}
658 _LIBCPP_INLINE_VISIBILITY static char_type to_char_type(int_type __c) {return char_type(__c);}
659 _LIBCPP_INLINE_VISIBILITY static int_type to_int_type(char_type __c) {return int_type(__c);}
660 _LIBCPP_INLINE_VISIBILITY static bool eq_int_type(int_type __c1, int_type __c2)
661 {return __c1 == __c2;}
662 _LIBCPP_INLINE_VISIBILITY static int_type eof() {return int_type(WEOF);}
663};
664
665#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
666
667template <>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000668struct _LIBCPP_VISIBLE char_traits<char16_t>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000669{
670 typedef char16_t char_type;
671 typedef uint_least16_t int_type;
672 typedef streamoff off_type;
673 typedef u16streampos pos_type;
674 typedef mbstate_t state_type;
675
676 _LIBCPP_INLINE_VISIBILITY static void assign(char_type& __c1, const char_type& __c2) {__c1 = __c2;}
677 _LIBCPP_INLINE_VISIBILITY static bool eq(char_type __c1, char_type __c2) {return __c1 == __c2;}
678 _LIBCPP_INLINE_VISIBILITY static bool lt(char_type __c1, char_type __c2) {return __c1 < __c2;}
679
680 static int compare(const char_type* __s1, const char_type* __s2, size_t __n);
681 static size_t length(const char_type* __s);
682 static const char_type* find(const char_type* __s, size_t __n, const char_type& __a);
683 static char_type* move(char_type* __s1, const char_type* __s2, size_t __n);
684 static char_type* copy(char_type* __s1, const char_type* __s2, size_t __n);
685 static char_type* assign(char_type* __s, size_t __n, char_type __a);
686
687 _LIBCPP_INLINE_VISIBILITY static int_type not_eof(int_type __c)
688 {return eq_int_type(__c, eof()) ? ~eof() : __c;}
689 _LIBCPP_INLINE_VISIBILITY static char_type to_char_type(int_type __c) {return char_type(__c);}
690 _LIBCPP_INLINE_VISIBILITY static int_type to_int_type(char_type __c) {return int_type(__c);}
691 _LIBCPP_INLINE_VISIBILITY static bool eq_int_type(int_type __c1, int_type __c2)
692 {return __c1 == __c2;}
693 _LIBCPP_INLINE_VISIBILITY static int_type eof() {return int_type(0xDFFF);}
694};
695
696inline _LIBCPP_INLINE_VISIBILITY
697int
698char_traits<char16_t>::compare(const char_type* __s1, const char_type* __s2, size_t __n)
699{
700 for (; __n; --__n, ++__s1, ++__s2)
701 {
702 if (lt(*__s1, *__s2))
703 return -1;
704 if (lt(*__s2, *__s1))
705 return 1;
706 }
707 return 0;
708}
709
710inline _LIBCPP_INLINE_VISIBILITY
711size_t
712char_traits<char16_t>::length(const char_type* __s)
713{
714 size_t __len = 0;
715 for (; !eq(*__s, char_type(0)); ++__s)
716 ++__len;
717 return __len;
718}
719
720inline _LIBCPP_INLINE_VISIBILITY
721const char16_t*
722char_traits<char16_t>::find(const char_type* __s, size_t __n, const char_type& __a)
723{
724 for (; __n; --__n)
725 {
726 if (eq(*__s, __a))
727 return __s;
728 ++__s;
729 }
730 return 0;
731}
732
733inline _LIBCPP_INLINE_VISIBILITY
734char16_t*
735char_traits<char16_t>::move(char_type* __s1, const char_type* __s2, size_t __n)
736{
737 char_type* __r = __s1;
738 if (__s1 < __s2)
739 {
740 for (; __n; --__n, ++__s1, ++__s2)
741 assign(*__s1, *__s2);
742 }
743 else if (__s2 < __s1)
744 {
745 __s1 += __n;
746 __s2 += __n;
747 for (; __n; --__n)
748 assign(*--__s1, *--__s2);
749 }
750 return __r;
751}
752
753inline _LIBCPP_INLINE_VISIBILITY
754char16_t*
755char_traits<char16_t>::copy(char_type* __s1, const char_type* __s2, size_t __n)
756{
757 char_type* __r = __s1;
758 for (; __n; --__n, ++__s1, ++__s2)
759 assign(*__s1, *__s2);
760 return __r;
761}
762
763inline _LIBCPP_INLINE_VISIBILITY
764char16_t*
765char_traits<char16_t>::assign(char_type* __s, size_t __n, char_type __a)
766{
767 char_type* __r = __s;
768 for (; __n; --__n, ++__s)
769 assign(*__s, __a);
770 return __r;
771}
772
773template <>
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000774struct _LIBCPP_VISIBLE char_traits<char32_t>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000775{
776 typedef char32_t char_type;
777 typedef uint_least32_t int_type;
778 typedef streamoff off_type;
779 typedef u32streampos pos_type;
780 typedef mbstate_t state_type;
781
782 _LIBCPP_INLINE_VISIBILITY static void assign(char_type& __c1, const char_type& __c2) {__c1 = __c2;}
783 _LIBCPP_INLINE_VISIBILITY static bool eq(char_type __c1, char_type __c2) {return __c1 == __c2;}
784 _LIBCPP_INLINE_VISIBILITY static bool lt(char_type __c1, char_type __c2) {return __c1 < __c2;}
785
786 static int compare(const char_type* __s1, const char_type* __s2, size_t __n);
787 static size_t length(const char_type* __s);
788 static const char_type* find(const char_type* __s, size_t __n, const char_type& __a);
789 static char_type* move(char_type* __s1, const char_type* __s2, size_t __n);
790 static char_type* copy(char_type* __s1, const char_type* __s2, size_t __n);
791 static char_type* assign(char_type* __s, size_t __n, char_type __a);
792
793 _LIBCPP_INLINE_VISIBILITY static int_type not_eof(int_type __c)
794 {return eq_int_type(__c, eof()) ? ~eof() : __c;}
795 _LIBCPP_INLINE_VISIBILITY static char_type to_char_type(int_type __c) {return char_type(__c);}
796 _LIBCPP_INLINE_VISIBILITY static int_type to_int_type(char_type __c) {return int_type(__c);}
797 _LIBCPP_INLINE_VISIBILITY static bool eq_int_type(int_type __c1, int_type __c2)
798 {return __c1 == __c2;}
799 _LIBCPP_INLINE_VISIBILITY static int_type eof() {return int_type(0xFFFFFFFF);}
800};
801
802inline _LIBCPP_INLINE_VISIBILITY
803int
804char_traits<char32_t>::compare(const char_type* __s1, const char_type* __s2, size_t __n)
805{
806 for (; __n; --__n, ++__s1, ++__s2)
807 {
808 if (lt(*__s1, *__s2))
809 return -1;
810 if (lt(*__s2, *__s1))
811 return 1;
812 }
813 return 0;
814}
815
816inline _LIBCPP_INLINE_VISIBILITY
817size_t
818char_traits<char32_t>::length(const char_type* __s)
819{
820 size_t __len = 0;
821 for (; !eq(*__s, char_type(0)); ++__s)
822 ++__len;
823 return __len;
824}
825
826inline _LIBCPP_INLINE_VISIBILITY
827const char32_t*
828char_traits<char32_t>::find(const char_type* __s, size_t __n, const char_type& __a)
829{
830 for (; __n; --__n)
831 {
832 if (eq(*__s, __a))
833 return __s;
834 ++__s;
835 }
836 return 0;
837}
838
839inline _LIBCPP_INLINE_VISIBILITY
840char32_t*
841char_traits<char32_t>::move(char_type* __s1, const char_type* __s2, size_t __n)
842{
843 char_type* __r = __s1;
844 if (__s1 < __s2)
845 {
846 for (; __n; --__n, ++__s1, ++__s2)
847 assign(*__s1, *__s2);
848 }
849 else if (__s2 < __s1)
850 {
851 __s1 += __n;
852 __s2 += __n;
853 for (; __n; --__n)
854 assign(*--__s1, *--__s2);
855 }
856 return __r;
857}
858
859inline _LIBCPP_INLINE_VISIBILITY
860char32_t*
861char_traits<char32_t>::copy(char_type* __s1, const char_type* __s2, size_t __n)
862{
863 char_type* __r = __s1;
864 for (; __n; --__n, ++__s1, ++__s2)
865 assign(*__s1, *__s2);
866 return __r;
867}
868
869inline _LIBCPP_INLINE_VISIBILITY
870char32_t*
871char_traits<char32_t>::assign(char_type* __s, size_t __n, char_type __a)
872{
873 char_type* __r = __s;
874 for (; __n; --__n, ++__s)
875 assign(*__s, __a);
876 return __r;
877}
878
Howard Hinnant324bb032010-08-22 00:02:43 +0000879#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000880
881// basic_string
882
883template<class _CharT, class _Traits, class _Allocator>
884basic_string<_CharT, _Traits, _Allocator>
885operator+(const basic_string<_CharT, _Traits, _Allocator>&, const basic_string<_CharT, _Traits, _Allocator>&);
886
887template<class _CharT, class _Traits, class _Allocator>
888basic_string<_CharT, _Traits, _Allocator>
889operator+(const _CharT*, const basic_string<_CharT,_Traits,_Allocator>&);
890
891template<class _CharT, class _Traits, class _Allocator>
892basic_string<_CharT, _Traits, _Allocator>
893operator+(_CharT, const basic_string<_CharT,_Traits,_Allocator>&);
894
895template<class _CharT, class _Traits, class _Allocator>
896basic_string<_CharT, _Traits, _Allocator>
897operator+(const basic_string<_CharT, _Traits, _Allocator>&, const _CharT*);
898
899template<class _CharT, class _Traits, class _Allocator>
900basic_string<_CharT, _Traits, _Allocator>
901operator+(const basic_string<_CharT, _Traits, _Allocator>&, _CharT);
902
903template <bool>
904class __basic_string_common
905{
906protected:
907 void __throw_length_error() const;
908 void __throw_out_of_range() const;
909};
910
911template <bool __b>
912void
913__basic_string_common<__b>::__throw_length_error() const
914{
915#ifndef _LIBCPP_NO_EXCEPTIONS
916 throw length_error("basic_string");
917#else
918 assert(!"basic_string length_error");
919#endif
920}
921
922template <bool __b>
923void
924__basic_string_common<__b>::__throw_out_of_range() const
925{
926#ifndef _LIBCPP_NO_EXCEPTIONS
927 throw out_of_range("basic_string");
928#else
929 assert(!"basic_string out_of_range");
930#endif
931}
932
933extern template class __basic_string_common<true>;
934
Howard Hinnant324bb032010-08-22 00:02:43 +0000935template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant36cdf022010-09-10 16:42:26 +0000936class _LIBCPP_VISIBLE basic_string
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000937 : private __basic_string_common<true>
938{
939public:
940 typedef basic_string __self;
941 typedef _Traits traits_type;
942 typedef typename traits_type::char_type value_type;
943 typedef _Allocator allocator_type;
Howard Hinnante32b5e22010-11-17 17:55:08 +0000944 typedef allocator_traits<allocator_type> __alloc_traits;
945 typedef typename __alloc_traits::size_type size_type;
946 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000947 typedef typename allocator_type::reference reference;
948 typedef typename allocator_type::const_reference const_reference;
Howard Hinnante32b5e22010-11-17 17:55:08 +0000949 typedef typename __alloc_traits::pointer pointer;
950 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000951#ifdef _LIBCPP_DEBUG
952 typedef __debug_iter<basic_string, pointer> iterator;
953 typedef __debug_iter<basic_string, const_pointer> const_iterator;
954
955 friend class __debug_iter<basic_string, pointer>;
956 friend class __debug_iter<basic_string, const_pointer>;
957#elif defined(_LIBCPP_RAW_ITERATORS)
958 typedef pointer iterator;
959 typedef const_pointer const_iterator;
Howard Hinnant324bb032010-08-22 00:02:43 +0000960#else // defined(_LIBCPP_RAW_ITERATORS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000961 typedef __wrap_iter<pointer> iterator;
962 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnant324bb032010-08-22 00:02:43 +0000963#endif // defined(_LIBCPP_RAW_ITERATORS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000964 typedef _STD::reverse_iterator<iterator> reverse_iterator;
965 typedef _STD::reverse_iterator<const_iterator> const_reverse_iterator;
966
967private:
968 struct __long
969 {
970 size_type __cap_;
971 size_type __size_;
972 pointer __data_;
973 };
974
975#if _LIBCPP_BIG_ENDIAN
976 enum {__short_mask = 0x80};
977 enum {__long_mask = ~(size_type(~0) >> 1)};
Howard Hinnant324bb032010-08-22 00:02:43 +0000978#else // _LIBCPP_BIG_ENDIAN
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000979 enum {__short_mask = 0x01};
980 enum {__long_mask = 0x1};
Howard Hinnant324bb032010-08-22 00:02:43 +0000981#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000982
983 enum {__mask = size_type(~0) >> 1};
984
985 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
986 (sizeof(__long) - 1)/sizeof(value_type) : 2};
987
988 struct __short
989 {
990 union
991 {
992 unsigned char __size_;
993 value_type _;
994 };
995 value_type __data_[__min_cap];
996 };
997
998 union _{__long _; __short __;};
999
1000 enum {__n_words = sizeof(_) / sizeof(size_type)};
1001
1002 struct __raw
1003 {
1004 size_type __words[__n_words];
1005 };
1006
1007 struct __rep
1008 {
1009 union
1010 {
1011 __long __l;
1012 __short __s;
1013 __raw __r;
1014 };
1015 };
1016
1017 __compressed_pair<__rep, allocator_type> __r_;
1018
1019#ifdef _LIBCPP_DEBUG
1020
1021 pair<iterator*, const_iterator*> __iterator_list_;
1022
1023 _LIBCPP_INLINE_VISIBILITY iterator*& __get_iterator_list(iterator*) {return __iterator_list_.first;}
1024 _LIBCPP_INLINE_VISIBILITY const_iterator*& __get_iterator_list(const_iterator*) {return __iterator_list_.second;}
1025
1026#endif // _LIBCPP_DEBUG
1027
1028public:
1029 static const size_type npos = -1;
1030
1031 basic_string();
1032 explicit basic_string(const allocator_type& __a);
1033 basic_string(const basic_string& __str);
1034 basic_string(const basic_string& __str, const allocator_type& __a);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001035#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001036 basic_string(basic_string&& __str);
1037 basic_string(basic_string&& __str, const allocator_type& __a);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001038#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001039 basic_string(const_pointer __s);
1040 basic_string(const_pointer __s, const allocator_type& __a);
1041 basic_string(const_pointer __s, size_type __n);
1042 basic_string(const_pointer __s, size_type __n, const allocator_type& __a);
1043 basic_string(size_type __n, value_type __c);
1044 basic_string(size_type __n, value_type __c, const allocator_type& __a);
1045 basic_string(const basic_string& __str, size_type __pos, size_type __n = npos,
1046 const allocator_type& __a = allocator_type());
1047 template<class _InputIterator>
1048 basic_string(_InputIterator __first, _InputIterator __last);
1049 template<class _InputIterator>
1050 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
1051 basic_string(initializer_list<value_type> __il);
1052 basic_string(initializer_list<value_type> __il, const allocator_type& __a);
1053
1054 ~basic_string();
1055
Howard Hinnante32b5e22010-11-17 17:55:08 +00001056 basic_string& operator=(const basic_string& __str);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001057#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante32b5e22010-11-17 17:55:08 +00001058 basic_string& operator=(basic_string&& __str);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001059#endif
1060 _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const_pointer __s) {return assign(__s);}
1061 basic_string& operator=(value_type __c);
Howard Hinnant8d7a9552010-09-23 17:31:07 +00001062 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001063 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
1064
1065#ifndef _LIBCPP_DEBUG
1066 _LIBCPP_INLINE_VISIBILITY iterator begin() {return iterator(__get_pointer());}
1067 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const {return const_iterator(data());}
1068 _LIBCPP_INLINE_VISIBILITY iterator end() {return iterator(__get_pointer() + size());}
1069 _LIBCPP_INLINE_VISIBILITY const_iterator end() const {return const_iterator(data() + size());}
1070#else // _LIBCPP_DEBUG
1071 _LIBCPP_INLINE_VISIBILITY iterator begin() {return iterator(this, __get_pointer());}
1072 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const {return const_iterator(this, data());}
1073 _LIBCPP_INLINE_VISIBILITY iterator end() {return iterator(this, __get_pointer() + size());}
1074 _LIBCPP_INLINE_VISIBILITY const_iterator end() const {return const_iterator(this, data() + size());}
1075#endif // _LIBCPP_DEBUG
1076 _LIBCPP_INLINE_VISIBILITY reverse_iterator rbegin() {return reverse_iterator(end());}
1077 _LIBCPP_INLINE_VISIBILITY const_reverse_iterator rbegin() const {return const_reverse_iterator(end());}
1078 _LIBCPP_INLINE_VISIBILITY reverse_iterator rend() {return reverse_iterator(begin());}
1079 _LIBCPP_INLINE_VISIBILITY const_reverse_iterator rend() const {return const_reverse_iterator(begin());}
1080
1081 _LIBCPP_INLINE_VISIBILITY const_iterator cbegin() const {return begin();}
1082 _LIBCPP_INLINE_VISIBILITY const_iterator cend() const {return end();}
1083 _LIBCPP_INLINE_VISIBILITY const_reverse_iterator crbegin() const {return rbegin();}
1084 _LIBCPP_INLINE_VISIBILITY const_reverse_iterator crend() const {return rend();}
1085
1086 _LIBCPP_INLINE_VISIBILITY size_type size() const
1087 {return __is_long() ? __get_long_size() : __get_short_size();}
1088 _LIBCPP_INLINE_VISIBILITY size_type length() const {return size();}
1089 size_type max_size() const;
1090 _LIBCPP_INLINE_VISIBILITY size_type capacity() const
1091 {return (__is_long() ? __get_long_cap() : __min_cap) - 1;}
1092
1093 void resize(size_type __n, value_type __c);
1094 _LIBCPP_INLINE_VISIBILITY void resize(size_type __n) {resize(__n, value_type());}
1095
1096 void reserve(size_type res_arg = 0);
Howard Hinnant8d7a9552010-09-23 17:31:07 +00001097 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001098 void shrink_to_fit() {reserve();}
1099 void clear();
1100 _LIBCPP_INLINE_VISIBILITY bool empty() const {return size() == 0;}
1101
1102 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __pos) const;
1103 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __pos);
1104
1105 const_reference at(size_type __n) const;
1106 reference at(size_type __n);
1107
1108 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const basic_string& __str) {return append(__str);}
1109 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const_pointer __s) {return append(__s);}
1110 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(value_type __c) {push_back(__c); return *this;}
1111 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(initializer_list<value_type> __il) {return append(__il);}
1112
1113 basic_string& append(const basic_string& __str);
1114 basic_string& append(const basic_string& __str, size_type __pos, size_type __n);
1115 basic_string& append(const_pointer __s, size_type __n);
1116 basic_string& append(const_pointer __s);
1117 basic_string& append(size_type __n, value_type __c);
1118 template<class _InputIterator>
1119 typename enable_if
1120 <
1121 __is_input_iterator <_InputIterator>::value &&
1122 !__is_forward_iterator<_InputIterator>::value,
1123 basic_string&
1124 >::type
1125 append(_InputIterator __first, _InputIterator __last);
1126 template<class _ForwardIterator>
1127 typename enable_if
1128 <
1129 __is_forward_iterator<_ForwardIterator>::value,
1130 basic_string&
1131 >::type
1132 append(_ForwardIterator __first, _ForwardIterator __last);
Howard Hinnant8d7a9552010-09-23 17:31:07 +00001133 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001134 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
1135
1136 void push_back(value_type __c);
1137 void pop_back();
1138 reference front();
1139 const_reference front() const;
1140 reference back();
1141 const_reference back() const;
1142
1143 basic_string& assign(const basic_string& __str);
1144 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n);
1145 basic_string& assign(const_pointer __s, size_type __n);
1146 basic_string& assign(const_pointer __s);
1147 basic_string& assign(size_type __n, value_type __c);
1148 template<class _InputIterator>
1149 typename enable_if
1150 <
1151 __is_input_iterator <_InputIterator>::value &&
1152 !__is_forward_iterator<_InputIterator>::value,
1153 basic_string&
1154 >::type
1155 assign(_InputIterator __first, _InputIterator __last);
1156 template<class _ForwardIterator>
1157 typename enable_if
1158 <
1159 __is_forward_iterator<_ForwardIterator>::value,
1160 basic_string&
1161 >::type
1162 assign(_ForwardIterator __first, _ForwardIterator __last);
Howard Hinnant8d7a9552010-09-23 17:31:07 +00001163 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001164 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
1165
1166 basic_string& insert(size_type __pos1, const basic_string& __str);
1167 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n);
1168 basic_string& insert(size_type __pos, const_pointer __s, size_type __n);
1169 basic_string& insert(size_type __pos, const_pointer __s);
1170 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1171 iterator insert(const_iterator __pos, value_type __c);
1172 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1173 template<class _InputIterator>
1174 typename enable_if
1175 <
1176 __is_input_iterator <_InputIterator>::value &&
1177 !__is_forward_iterator<_InputIterator>::value,
1178 iterator
1179 >::type
1180 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1181 template<class _ForwardIterator>
1182 typename enable_if
1183 <
1184 __is_forward_iterator<_ForwardIterator>::value,
1185 iterator
1186 >::type
1187 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnant8d7a9552010-09-23 17:31:07 +00001188 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001189 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1190 {return insert(__pos, __il.begin(), __il.end());}
1191
1192 basic_string& erase(size_type __pos = 0, size_type __n = npos);
1193 iterator erase(const_iterator __pos);
1194 iterator erase(const_iterator __first, const_iterator __last);
1195
1196 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
1197 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2);
1198 basic_string& replace(size_type __pos, size_type __n1, const_pointer __s, size_type __n2);
1199 basic_string& replace(size_type __pos, size_type __n1, const_pointer __s);
1200 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
1201 basic_string& replace(iterator __i1, iterator __i2, const basic_string& __str);
1202 basic_string& replace(iterator __i1, iterator __i2, const_pointer __s, size_type __n);
1203 basic_string& replace(iterator __i1, iterator __i2, const_pointer __s);
1204 basic_string& replace(iterator __i1, iterator __i2, size_type __n, value_type __c);
1205 template<class _InputIterator>
1206 typename enable_if
1207 <
1208 __is_input_iterator<_InputIterator>::value,
1209 basic_string&
1210 >::type
1211 replace(iterator __i1, iterator __i2, _InputIterator __j1, _InputIterator __j2);
Howard Hinnant8d7a9552010-09-23 17:31:07 +00001212 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001213 basic_string& replace(iterator __i1, iterator __i2, initializer_list<value_type> __il)
1214 {return replace(__i1, __i2, __il.begin(), __il.end());}
1215
1216 size_type copy(pointer __s, size_type __n, size_type __pos = 0) const;
1217 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1218
1219 void swap(basic_string& __str);
1220
1221 _LIBCPP_INLINE_VISIBILITY const_pointer c_str() const {return data();}
1222 _LIBCPP_INLINE_VISIBILITY const_pointer data() const {return __get_pointer();}
1223
1224 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const {return __alloc();}
1225
1226 size_type find(const basic_string& __str, size_type __pos = 0) const;
1227 size_type find(const_pointer __s, size_type __pos, size_type __n) const;
1228 size_type find(const_pointer __s, size_type __pos = 0) const;
1229 size_type find(value_type __c, size_type __pos = 0) const;
1230
1231 size_type rfind(const basic_string& __str, size_type __pos = npos) const;
1232 size_type rfind(const_pointer __s, size_type __pos, size_type __n) const;
1233 size_type rfind(const_pointer __s, size_type __pos = npos) const;
1234 size_type rfind(value_type __c, size_type __pos = npos) const;
1235
1236 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const;
1237 size_type find_first_of(const_pointer __s, size_type __pos, size_type __n) const;
1238 size_type find_first_of(const_pointer __s, size_type __pos = 0) const;
1239 size_type find_first_of(value_type __c, size_type __pos = 0) const;
1240
1241 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const;
1242 size_type find_last_of(const_pointer __s, size_type __pos, size_type __n) const;
1243 size_type find_last_of(const_pointer __s, size_type __pos = npos) const;
1244 size_type find_last_of(value_type __c, size_type __pos = npos) const;
1245
1246 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const;
1247 size_type find_first_not_of(const_pointer __s, size_type __pos, size_type __n) const;
1248 size_type find_first_not_of(const_pointer __s, size_type __pos = 0) const;
1249 size_type find_first_not_of(value_type __c, size_type __pos = 0) const;
1250
1251 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const;
1252 size_type find_last_not_of(const_pointer __s, size_type __pos, size_type __n) const;
1253 size_type find_last_not_of(const_pointer __s, size_type __pos = npos) const;
1254 size_type find_last_not_of(value_type __c, size_type __pos = npos) const;
1255
1256 int compare(const basic_string& __str) const;
1257 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
1258 int compare(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2) const;
1259 int compare(const_pointer __s) const;
1260 int compare(size_type __pos1, size_type __n1, const_pointer __s) const;
1261 int compare(size_type __pos1, size_type __n1, const_pointer __s, size_type __n2) const;
1262
1263 bool __invariants() const;
1264private:
1265 _LIBCPP_INLINE_VISIBILITY allocator_type& __alloc() {return __r_.second();}
1266 _LIBCPP_INLINE_VISIBILITY const allocator_type& __alloc() const {return __r_.second();}
1267
1268 _LIBCPP_INLINE_VISIBILITY bool __is_long() const {return bool(__r_.first().__s.__size_ & __short_mask);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001269
1270 _LIBCPP_INLINE_VISIBILITY void __set_short_size(size_type __s)
1271#if _LIBCPP_BIG_ENDIAN
1272 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1273#else
1274 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
1275#endif
1276 _LIBCPP_INLINE_VISIBILITY size_type __get_short_size() const
1277#if _LIBCPP_BIG_ENDIAN
1278 {return __r_.first().__s.__size_;}
1279#else
1280 {return __r_.first().__s.__size_ >> 1;}
1281#endif
1282 _LIBCPP_INLINE_VISIBILITY void __set_long_size(size_type __s) {__r_.first().__l.__size_ = __s;}
1283 _LIBCPP_INLINE_VISIBILITY size_type __get_long_size() const {return __r_.first().__l.__size_;}
1284 _LIBCPP_INLINE_VISIBILITY void __set_size(size_type __s)
1285 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1286
1287 _LIBCPP_INLINE_VISIBILITY void __set_long_cap(size_type __s) {__r_.first().__l.__cap_ = __long_mask | __s;}
1288 _LIBCPP_INLINE_VISIBILITY size_type __get_long_cap() const {return __r_.first().__l.__cap_ & ~__long_mask;}
1289
1290 _LIBCPP_INLINE_VISIBILITY void __set_long_pointer(pointer __p) {__r_.first().__l.__data_ = __p;}
1291 _LIBCPP_INLINE_VISIBILITY pointer __get_long_pointer() {return __r_.first().__l.__data_;}
1292 _LIBCPP_INLINE_VISIBILITY const_pointer __get_long_pointer() const {return __r_.first().__l.__data_;}
1293 _LIBCPP_INLINE_VISIBILITY pointer __get_short_pointer() {return __r_.first().__s.__data_;}
1294 _LIBCPP_INLINE_VISIBILITY const_pointer __get_short_pointer() const {return __r_.first().__s.__data_;}
1295 _LIBCPP_INLINE_VISIBILITY pointer __get_pointer()
1296 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1297 _LIBCPP_INLINE_VISIBILITY const_pointer __get_pointer() const
1298 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1299
1300 _LIBCPP_INLINE_VISIBILITY void __zero()
1301 {
1302 size_type (&__a)[__n_words] = __r_.first().__r.__words;
1303 for (unsigned __i = 0; __i < __n_words; ++__i)
1304 __a[__i] = 0;
1305 }
1306
1307 template <size_type __a> static
1308 _LIBCPP_INLINE_VISIBILITY size_type __align(size_type __s) {return __s + (__a-1) & ~(__a-1);}
1309 enum {__alignment = 16};
1310 static _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __s)
1311 {return (__s < __min_cap ? __min_cap :
1312 __align<sizeof(value_type) < __alignment ? __alignment/sizeof(value_type) : 1>(__s+1)) - 1;}
1313
1314 void __init(const_pointer __s, size_type __sz, size_type __reserve);
1315 void __init(const_pointer __s, size_type __sz);
1316 void __init(size_type __n, value_type __c);
Howard Hinnant324bb032010-08-22 00:02:43 +00001317
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001318 template <class _InputIterator>
1319 typename enable_if
1320 <
1321 __is_input_iterator <_InputIterator>::value &&
1322 !__is_forward_iterator<_InputIterator>::value,
1323 void
1324 >::type
1325 __init(_InputIterator __first, _InputIterator __last);
1326
1327 template <class _ForwardIterator>
1328 typename enable_if
1329 <
1330 __is_forward_iterator<_ForwardIterator>::value,
1331 void
1332 >::type
1333 __init(_ForwardIterator __first, _ForwardIterator __last);
1334
1335 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant324bb032010-08-22 00:02:43 +00001336 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001337 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1338 size_type __n_copy, size_type __n_del,
1339 size_type __n_add, const_pointer __p_new_stuff);
1340
1341 void __erase_to_end(size_type __pos);
1342
Howard Hinnante32b5e22010-11-17 17:55:08 +00001343 _LIBCPP_INLINE_VISIBILITY
1344 void __copy_assign_alloc(const basic_string& __str)
1345 {__copy_assign_alloc(__str, integral_constant<bool,
1346 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1347
1348 _LIBCPP_INLINE_VISIBILITY
1349 void __copy_assign_alloc(const basic_string& __str, true_type)
1350 {
1351 if (__alloc() != __str.__alloc())
1352 {
1353 clear();
1354 shrink_to_fit();
1355 }
1356 __alloc() = __str.__alloc();
1357 }
1358
1359 _LIBCPP_INLINE_VISIBILITY
1360 void __copy_assign_alloc(const basic_string& __str, false_type)
1361 {}
1362
1363#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1364 void __move_assign(basic_string& __str, false_type);
1365 void __move_assign(basic_string& __str, true_type);
1366#endif
1367
1368 _LIBCPP_INLINE_VISIBILITY
1369 static void __swap_alloc(allocator_type& __x, allocator_type& __y)
1370 {__swap_alloc(__x, __y, integral_constant<bool,
1371 __alloc_traits::propagate_on_container_swap::value>());}
1372
1373 _LIBCPP_INLINE_VISIBILITY
1374 static void __swap_alloc(allocator_type& __x, allocator_type& __y, true_type)
1375 {
1376 using _STD::swap;
1377 swap(__x, __y);
1378 }
1379 _LIBCPP_INLINE_VISIBILITY
1380 static void __swap_alloc(allocator_type& __x, allocator_type& __y, false_type)
1381 {}
1382
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001383 void __invalidate_all_iterators();
1384 void __invalidate_iterators_past(size_type);
1385
1386 friend basic_string operator+<>(const basic_string&, const basic_string&);
1387 friend basic_string operator+<>(const value_type*, const basic_string&);
1388 friend basic_string operator+<>(value_type, const basic_string&);
1389 friend basic_string operator+<>(const basic_string&, const value_type*);
1390 friend basic_string operator+<>(const basic_string&, value_type);
1391};
1392
1393template <class _CharT, class _Traits, class _Allocator>
1394#ifndef _LIBCPP_DEBUG
1395_LIBCPP_INLINE_VISIBILITY inline
1396#endif
1397void
1398basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
1399{
1400#ifdef _LIBCPP_DEBUG
1401 iterator::__remove_all(this);
1402 const_iterator::__remove_all(this);
Howard Hinnant324bb032010-08-22 00:02:43 +00001403#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001404}
1405
1406template <class _CharT, class _Traits, class _Allocator>
1407#ifndef _LIBCPP_DEBUG
1408_LIBCPP_INLINE_VISIBILITY inline
1409#endif
1410void
1411basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type __pos)
1412{
1413#ifdef _LIBCPP_DEBUG
1414 const_iterator __beg = begin();
1415 if (__iterator_list_.first)
1416 {
1417 for (iterator* __p = __iterator_list_.first; __p;)
1418 {
1419 if (*__p - __beg > static_cast<difference_type>(__pos))
1420 {
1421 iterator* __n = __p;
1422 __p = __p->__next;
1423 __n->__remove_owner();
1424 }
1425 else
1426 __p = __p->__next;
1427 }
1428 }
1429 if (__iterator_list_.second)
1430 {
1431 for (const_iterator* __p = __iterator_list_.second; __p;)
1432 {
1433 if (*__p - __beg > static_cast<difference_type>(__pos))
1434 {
1435 const_iterator* __n = __p;
1436 __p = __p->__next;
1437 __n->__remove_owner();
1438 }
1439 else
1440 __p = __p->__next;
1441 }
1442 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001443#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001444}
1445
1446template <class _CharT, class _Traits, class _Allocator>
1447_LIBCPP_INLINE_VISIBILITY inline
1448basic_string<_CharT, _Traits, _Allocator>::basic_string()
1449{
1450 __zero();
1451}
1452
1453template <class _CharT, class _Traits, class _Allocator>
1454_LIBCPP_INLINE_VISIBILITY inline
1455basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
1456 : __r_(__a)
1457{
1458 __zero();
1459}
1460
1461template <class _CharT, class _Traits, class _Allocator>
1462void
1463basic_string<_CharT, _Traits, _Allocator>::__init(const_pointer __s, size_type __sz, size_type __reserve)
1464{
1465 if (__reserve > max_size())
1466 this->__throw_length_error();
1467 pointer __p;
1468 if (__reserve < __min_cap)
1469 {
1470 __set_short_size(__sz);
1471 __p = __get_short_pointer();
1472 }
1473 else
1474 {
1475 size_type __cap = __recommend(__reserve);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001476 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001477 __set_long_pointer(__p);
1478 __set_long_cap(__cap+1);
1479 __set_long_size(__sz);
1480 }
1481 traits_type::copy(__p, __s, __sz);
1482 traits_type::assign(__p[__sz], value_type());
1483}
1484
1485template <class _CharT, class _Traits, class _Allocator>
1486void
1487basic_string<_CharT, _Traits, _Allocator>::__init(const_pointer __s, size_type __sz)
1488{
1489 if (__sz > max_size())
1490 this->__throw_length_error();
1491 pointer __p;
1492 if (__sz < __min_cap)
1493 {
1494 __set_short_size(__sz);
1495 __p = __get_short_pointer();
1496 }
1497 else
1498 {
1499 size_type __cap = __recommend(__sz);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001500 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001501 __set_long_pointer(__p);
1502 __set_long_cap(__cap+1);
1503 __set_long_size(__sz);
1504 }
1505 traits_type::copy(__p, __s, __sz);
1506 traits_type::assign(__p[__sz], value_type());
1507}
1508
1509template <class _CharT, class _Traits, class _Allocator>
1510_LIBCPP_INLINE_VISIBILITY inline
1511basic_string<_CharT, _Traits, _Allocator>::basic_string(const_pointer __s)
1512{
1513#ifdef _LIBCPP_DEBUG
1514 assert(__s != 0);
1515#endif
1516 __init(__s, traits_type::length(__s));
1517}
1518
1519template <class _CharT, class _Traits, class _Allocator>
1520_LIBCPP_INLINE_VISIBILITY inline
1521basic_string<_CharT, _Traits, _Allocator>::basic_string(const_pointer __s, const allocator_type& __a)
1522 : __r_(__a)
1523{
1524#ifdef _LIBCPP_DEBUG
1525 assert(__s != 0);
1526#endif
1527 __init(__s, traits_type::length(__s));
1528}
1529
1530template <class _CharT, class _Traits, class _Allocator>
1531_LIBCPP_INLINE_VISIBILITY inline
1532basic_string<_CharT, _Traits, _Allocator>::basic_string(const_pointer __s, size_type __n)
1533{
1534#ifdef _LIBCPP_DEBUG
1535 assert(__s != 0);
1536#endif
1537 __init(__s, __n);
1538}
1539
1540template <class _CharT, class _Traits, class _Allocator>
1541_LIBCPP_INLINE_VISIBILITY inline
1542basic_string<_CharT, _Traits, _Allocator>::basic_string(const_pointer __s, size_type __n, const allocator_type& __a)
1543 : __r_(__a)
1544{
1545#ifdef _LIBCPP_DEBUG
1546 assert(__s != 0);
1547#endif
1548 __init(__s, __n);
1549}
1550
1551template <class _CharT, class _Traits, class _Allocator>
1552basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Howard Hinnante32b5e22010-11-17 17:55:08 +00001553 : __r_(__alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001554{
1555 if (!__str.__is_long())
1556 __r_.first().__r = __str.__r_.first().__r;
1557 else
1558 __init(__str.__get_long_pointer(), __str.__get_long_size());
1559}
1560
1561template <class _CharT, class _Traits, class _Allocator>
1562basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, const allocator_type& __a)
1563 : __r_(__a)
1564{
1565 if (!__str.__is_long())
1566 __r_.first().__r = __str.__r_.first().__r;
1567 else
1568 __init(__str.__get_long_pointer(), __str.__get_long_size());
1569}
1570
Howard Hinnant73d21a42010-09-04 23:28:19 +00001571#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001572
1573template <class _CharT, class _Traits, class _Allocator>
1574_LIBCPP_INLINE_VISIBILITY inline
1575basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
1576 : __r_(_STD::move(__str.__r_))
1577{
1578 __str.__zero();
1579#ifdef _LIBCPP_DEBUG
1580 __str.__invalidate_all_iterators();
1581#endif
1582}
1583
1584template <class _CharT, class _Traits, class _Allocator>
1585_LIBCPP_INLINE_VISIBILITY inline
1586basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Howard Hinnante32b5e22010-11-17 17:55:08 +00001587 : __r_(__a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001588{
Howard Hinnante32b5e22010-11-17 17:55:08 +00001589 if (__a == __str.__alloc() || !__str.__is_long())
1590 __r_.first().__r = __str.__r_.first().__r;
1591 else
1592 __init(__str.__get_long_pointer(), __str.__get_long_size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001593 __str.__zero();
1594#ifdef _LIBCPP_DEBUG
1595 __str.__invalidate_all_iterators();
1596#endif
1597}
1598
Howard Hinnant73d21a42010-09-04 23:28:19 +00001599#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001600
1601template <class _CharT, class _Traits, class _Allocator>
1602void
1603basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
1604{
1605 if (__n > max_size())
1606 this->__throw_length_error();
1607 pointer __p;
1608 if (__n < __min_cap)
1609 {
1610 __set_short_size(__n);
1611 __p = __get_short_pointer();
1612 }
1613 else
1614 {
1615 size_type __cap = __recommend(__n);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001616 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001617 __set_long_pointer(__p);
1618 __set_long_cap(__cap+1);
1619 __set_long_size(__n);
1620 }
1621 traits_type::assign(__p, __n, __c);
1622 traits_type::assign(__p[__n], value_type());
1623}
1624
1625template <class _CharT, class _Traits, class _Allocator>
1626_LIBCPP_INLINE_VISIBILITY inline
1627basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, value_type __c)
1628{
1629 __init(__n, __c);
1630}
1631
1632template <class _CharT, class _Traits, class _Allocator>
1633_LIBCPP_INLINE_VISIBILITY inline
1634basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, value_type __c, const allocator_type& __a)
1635 : __r_(__a)
1636{
1637 __init(__n, __c);
1638}
1639
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001640template <class _CharT, class _Traits, class _Allocator>
1641basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos, size_type __n,
1642 const allocator_type& __a)
1643 : __r_(__a)
1644{
1645 size_type __str_sz = __str.size();
1646 if (__pos > __str_sz)
1647 this->__throw_out_of_range();
1648 __init(__str.data() + __pos, _STD::min(__n, __str_sz - __pos));
1649}
1650
1651template <class _CharT, class _Traits, class _Allocator>
1652template <class _InputIterator>
1653typename enable_if
1654<
1655 __is_input_iterator <_InputIterator>::value &&
1656 !__is_forward_iterator<_InputIterator>::value,
1657 void
1658>::type
1659basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
1660{
1661 __zero();
1662#ifndef _LIBCPP_NO_EXCEPTIONS
1663 try
1664 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001665#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001666 for (; __first != __last; ++__first)
1667 push_back(*__first);
1668#ifndef _LIBCPP_NO_EXCEPTIONS
1669 }
1670 catch (...)
1671 {
1672 if (__is_long())
Howard Hinnante32b5e22010-11-17 17:55:08 +00001673 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001674 throw;
1675 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001676#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001677}
1678
1679template <class _CharT, class _Traits, class _Allocator>
1680template <class _ForwardIterator>
1681typename enable_if
1682<
1683 __is_forward_iterator<_ForwardIterator>::value,
1684 void
1685>::type
1686basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
1687{
1688 size_type __sz = static_cast<size_type>(_STD::distance(__first, __last));
1689 if (__sz > max_size())
1690 this->__throw_length_error();
1691 pointer __p;
1692 if (__sz < __min_cap)
1693 {
1694 __set_short_size(__sz);
1695 __p = __get_short_pointer();
1696 }
1697 else
1698 {
1699 size_type __cap = __recommend(__sz);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001700 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001701 __set_long_pointer(__p);
1702 __set_long_cap(__cap+1);
1703 __set_long_size(__sz);
1704 }
1705 for (; __first != __last; ++__first, ++__p)
1706 traits_type::assign(*__p, *__first);
1707 traits_type::assign(*__p, value_type());
1708}
1709
1710template <class _CharT, class _Traits, class _Allocator>
1711template<class _InputIterator>
1712_LIBCPP_INLINE_VISIBILITY inline
1713basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
1714{
1715 __init(__first, __last);
1716}
1717
1718template <class _CharT, class _Traits, class _Allocator>
1719template<class _InputIterator>
1720_LIBCPP_INLINE_VISIBILITY inline
1721basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
1722 const allocator_type& __a)
1723 : __r_(__a)
1724{
1725 __init(__first, __last);
1726}
1727
1728template <class _CharT, class _Traits, class _Allocator>
1729_LIBCPP_INLINE_VISIBILITY inline
1730basic_string<_CharT, _Traits, _Allocator>::basic_string(initializer_list<value_type> __il)
1731{
1732 __init(__il.begin(), __il.end());
1733}
1734
1735template <class _CharT, class _Traits, class _Allocator>
1736_LIBCPP_INLINE_VISIBILITY inline
1737basic_string<_CharT, _Traits, _Allocator>::basic_string(initializer_list<value_type> __il, const allocator_type& __a)
1738 : __r_(__a)
1739{
1740 __init(__il.begin(), __il.end());
1741}
1742
1743template <class _CharT, class _Traits, class _Allocator>
1744_LIBCPP_INLINE_VISIBILITY inline
1745basic_string<_CharT, _Traits, _Allocator>::~basic_string()
1746{
1747 __invalidate_all_iterators();
1748 if (__is_long())
Howard Hinnante32b5e22010-11-17 17:55:08 +00001749 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001750}
1751
1752template <class _CharT, class _Traits, class _Allocator>
1753void
1754basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
1755 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1756 size_type __n_copy, size_type __n_del, size_type __n_add, const_pointer __p_new_stuff)
1757{
1758 size_type __ms = max_size();
1759 if (__delta_cap > __ms - __old_cap - 1)
1760 this->__throw_length_error();
1761 pointer __old_p = __get_pointer();
1762 size_type __cap = __old_cap < __ms / 2 - __alignment ?
1763 __recommend(_STD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
1764 __ms - 1;
Howard Hinnante32b5e22010-11-17 17:55:08 +00001765 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001766 __invalidate_all_iterators();
1767 if (__n_copy != 0)
1768 traits_type::copy(__p, __old_p, __n_copy);
1769 if (__n_add != 0)
1770 traits_type::copy(__p + __n_copy, __p_new_stuff, __n_add);
1771 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
1772 if (__sec_cp_sz != 0)
1773 traits_type::copy(__p + __n_copy + __n_add, __old_p + __n_copy + __n_del, __sec_cp_sz);
1774 if (__old_cap+1 != __min_cap)
Howard Hinnante32b5e22010-11-17 17:55:08 +00001775 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001776 __set_long_pointer(__p);
1777 __set_long_cap(__cap+1);
1778 __old_sz = __n_copy + __n_add + __sec_cp_sz;
1779 __set_long_size(__old_sz);
1780 traits_type::assign(__p[__old_sz], value_type());
1781}
1782
1783template <class _CharT, class _Traits, class _Allocator>
1784void
1785basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1786 size_type __n_copy, size_type __n_del, size_type __n_add)
1787{
1788 size_type __ms = max_size();
1789 if (__delta_cap > __ms - __old_cap - 1)
1790 this->__throw_length_error();
1791 pointer __old_p = __get_pointer();
1792 size_type __cap = __old_cap < __ms / 2 - __alignment ?
1793 __recommend(_STD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
1794 __ms - 1;
Howard Hinnante32b5e22010-11-17 17:55:08 +00001795 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001796 __invalidate_all_iterators();
1797 if (__n_copy != 0)
1798 traits_type::copy(__p, __old_p, __n_copy);
1799 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
1800 if (__sec_cp_sz != 0)
1801 traits_type::copy(__p + __n_copy + __n_add, __old_p + __n_copy + __n_del, __sec_cp_sz);
1802 if (__old_cap+1 != __min_cap)
Howard Hinnante32b5e22010-11-17 17:55:08 +00001803 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001804 __set_long_pointer(__p);
1805 __set_long_cap(__cap+1);
1806}
1807
1808// assign
1809
1810template <class _CharT, class _Traits, class _Allocator>
1811basic_string<_CharT, _Traits, _Allocator>&
1812basic_string<_CharT, _Traits, _Allocator>::assign(const_pointer __s, size_type __n)
1813{
1814#ifdef _LIBCPP_DEBUG
1815 assert(__s != 0);
1816#endif
1817 size_type __cap = capacity();
1818 if (__cap >= __n)
1819 {
1820 pointer __p = __get_pointer();
1821 traits_type::move(__p, __s, __n);
1822 traits_type::assign(__p[__n], value_type());
1823 __set_size(__n);
1824 __invalidate_iterators_past(__n);
1825 }
1826 else
1827 {
1828 size_type __sz = size();
1829 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
1830 }
1831 return *this;
1832}
1833
1834template <class _CharT, class _Traits, class _Allocator>
1835basic_string<_CharT, _Traits, _Allocator>&
1836basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
1837{
1838 size_type __cap = capacity();
1839 if (__cap < __n)
1840 {
1841 size_type __sz = size();
1842 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
1843 }
1844 else
1845 __invalidate_iterators_past(__n);
1846 pointer __p = __get_pointer();
1847 traits_type::assign(__p, __n, __c);
1848 traits_type::assign(__p[__n], value_type());
1849 __set_size(__n);
1850 return *this;
1851}
1852
1853template <class _CharT, class _Traits, class _Allocator>
1854basic_string<_CharT, _Traits, _Allocator>&
1855basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
1856{
1857 pointer __p;
1858 if (__is_long())
1859 {
1860 __p = __get_long_pointer();
1861 __set_long_size(1);
1862 }
1863 else
1864 {
1865 __p = __get_short_pointer();
1866 __set_short_size(1);
1867 }
1868 traits_type::assign(*__p, __c);
1869 traits_type::assign(*++__p, value_type());
1870 __invalidate_iterators_past(1);
1871 return *this;
1872}
1873
1874template <class _CharT, class _Traits, class _Allocator>
Howard Hinnante32b5e22010-11-17 17:55:08 +00001875basic_string<_CharT, _Traits, _Allocator>&
1876basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
1877{
1878 if (this != &__str)
1879 {
1880 __copy_assign_alloc(__str);
1881 assign(__str);
1882 }
1883 return *this;
1884}
1885
1886#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1887
1888template <class _CharT, class _Traits, class _Allocator>
1889_LIBCPP_INLINE_VISIBILITY inline
1890void
1891basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
1892{
1893 if (__alloc() != __str.__alloc())
1894 assign(__str);
1895 else
1896 __move_assign(__str, true_type());
1897}
1898
1899template <class _CharT, class _Traits, class _Allocator>
1900_LIBCPP_INLINE_VISIBILITY inline
1901void
1902basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
1903{
1904 clear();
1905 shrink_to_fit();
1906 __r_ = _STD::move(__str.__r_);
1907 __str.__zero();
1908}
1909
1910template <class _CharT, class _Traits, class _Allocator>
1911_LIBCPP_INLINE_VISIBILITY inline
1912basic_string<_CharT, _Traits, _Allocator>&
1913basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
1914{
1915 __move_assign(__str, integral_constant<bool,
1916 __alloc_traits::propagate_on_container_move_assignment::value>());
1917 return *this;
1918}
1919
1920#endif
1921
1922template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001923template<class _InputIterator>
1924typename enable_if
1925<
1926 __is_input_iterator <_InputIterator>::value &&
1927 !__is_forward_iterator<_InputIterator>::value,
1928 basic_string<_CharT, _Traits, _Allocator>&
1929>::type
1930basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
1931{
1932 clear();
1933 for (; __first != __last; ++__first)
1934 push_back(*__first);
1935}
1936
1937template <class _CharT, class _Traits, class _Allocator>
1938template<class _ForwardIterator>
1939typename enable_if
1940<
1941 __is_forward_iterator<_ForwardIterator>::value,
1942 basic_string<_CharT, _Traits, _Allocator>&
1943>::type
1944basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
1945{
1946 size_type __n = static_cast<size_type>(_STD::distance(__first, __last));
1947 size_type __cap = capacity();
1948 if (__cap < __n)
1949 {
1950 size_type __sz = size();
1951 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
1952 }
1953 else
1954 __invalidate_iterators_past(__n);
1955 pointer __p = __get_pointer();
1956 for (; __first != __last; ++__first, ++__p)
1957 traits_type::assign(*__p, *__first);
1958 traits_type::assign(*__p, value_type());
1959 __set_size(__n);
1960 return *this;
1961}
1962
1963template <class _CharT, class _Traits, class _Allocator>
1964_LIBCPP_INLINE_VISIBILITY inline
1965basic_string<_CharT, _Traits, _Allocator>&
1966basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str)
1967{
1968 return assign(__str.data(), __str.size());
1969}
1970
1971template <class _CharT, class _Traits, class _Allocator>
1972basic_string<_CharT, _Traits, _Allocator>&
1973basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
1974{
1975 size_type __sz = __str.size();
1976 if (__pos > __sz)
1977 this->__throw_out_of_range();
1978 return assign(__str.data() + __pos, _STD::min(__n, __sz - __pos));
1979}
1980
1981template <class _CharT, class _Traits, class _Allocator>
1982basic_string<_CharT, _Traits, _Allocator>&
1983basic_string<_CharT, _Traits, _Allocator>::assign(const_pointer __s)
1984{
1985#ifdef _LIBCPP_DEBUG
1986 assert(__s != 0);
1987#endif
1988 return assign(__s, traits_type::length(__s));
1989}
1990
1991// append
1992
1993template <class _CharT, class _Traits, class _Allocator>
1994basic_string<_CharT, _Traits, _Allocator>&
1995basic_string<_CharT, _Traits, _Allocator>::append(const_pointer __s, size_type __n)
1996{
1997#ifdef _LIBCPP_DEBUG
1998 assert(__s != 0);
1999#endif
2000 size_type __cap = capacity();
2001 size_type __sz = size();
2002 if (__cap - __sz >= __n)
2003 {
2004 if (__n)
2005 {
2006 pointer __p = __get_pointer();
2007 traits_type::copy(__p + __sz, __s, __n);
2008 __sz += __n;
2009 __set_size(__sz);
2010 traits_type::assign(__p[__sz], value_type());
2011 }
2012 }
2013 else
2014 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2015 return *this;
2016}
2017
2018template <class _CharT, class _Traits, class _Allocator>
2019basic_string<_CharT, _Traits, _Allocator>&
2020basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2021{
2022 if (__n)
2023 {
2024 size_type __cap = capacity();
2025 size_type __sz = size();
2026 if (__cap - __sz < __n)
2027 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2028 pointer __p = __get_pointer();
2029 traits_type::assign(__p + __sz, __n, __c);
2030 __sz += __n;
2031 __set_size(__sz);
2032 traits_type::assign(__p[__sz], value_type());
2033 }
2034 return *this;
2035}
2036
2037template <class _CharT, class _Traits, class _Allocator>
2038void
2039basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2040{
2041 size_type __cap = capacity();
2042 size_type __sz = size();
2043 if (__sz == __cap)
2044 __grow_by(__cap, 1, __sz, __sz, 0);
2045 pointer __p = __get_pointer() + __sz;
2046 traits_type::assign(*__p, __c);
2047 traits_type::assign(*++__p, value_type());
2048 __set_size(__sz+1);
2049}
2050
2051template <class _CharT, class _Traits, class _Allocator>
2052template<class _InputIterator>
2053typename enable_if
2054<
2055 __is_input_iterator <_InputIterator>::value &&
2056 !__is_forward_iterator<_InputIterator>::value,
2057 basic_string<_CharT, _Traits, _Allocator>&
2058>::type
2059basic_string<_CharT, _Traits, _Allocator>::append(_InputIterator __first, _InputIterator __last)
2060{
2061 for (; __first != __last; ++__first)
2062 push_back(*__first);
2063 return *this;
2064}
2065
2066template <class _CharT, class _Traits, class _Allocator>
2067template<class _ForwardIterator>
2068typename enable_if
2069<
2070 __is_forward_iterator<_ForwardIterator>::value,
2071 basic_string<_CharT, _Traits, _Allocator>&
2072>::type
2073basic_string<_CharT, _Traits, _Allocator>::append(_ForwardIterator __first, _ForwardIterator __last)
2074{
2075 size_type __sz = size();
2076 size_type __cap = capacity();
2077 size_type __n = static_cast<size_type>(_STD::distance(__first, __last));
2078 if (__n)
2079 {
2080 if (__cap - __sz < __n)
2081 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2082 pointer __p = __get_pointer() + __sz;
2083 for (; __first != __last; ++__p, ++__first)
2084 traits_type::assign(*__p, *__first);
2085 traits_type::assign(*__p, value_type());
2086 __set_size(__sz + __n);
2087 }
2088 return *this;
2089}
2090
2091template <class _CharT, class _Traits, class _Allocator>
2092_LIBCPP_INLINE_VISIBILITY inline
2093basic_string<_CharT, _Traits, _Allocator>&
2094basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2095{
2096 return append(__str.data(), __str.size());
2097}
2098
2099template <class _CharT, class _Traits, class _Allocator>
2100basic_string<_CharT, _Traits, _Allocator>&
2101basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2102{
2103 size_type __sz = __str.size();
2104 if (__pos > __sz)
2105 this->__throw_out_of_range();
2106 return append(__str.data() + __pos, _STD::min(__n, __sz - __pos));
2107}
2108
2109template <class _CharT, class _Traits, class _Allocator>
2110basic_string<_CharT, _Traits, _Allocator>&
2111basic_string<_CharT, _Traits, _Allocator>::append(const_pointer __s)
2112{
2113#ifdef _LIBCPP_DEBUG
2114 assert(__s != 0);
2115#endif
2116 return append(__s, traits_type::length(__s));
2117}
2118
2119// insert
2120
2121template <class _CharT, class _Traits, class _Allocator>
2122basic_string<_CharT, _Traits, _Allocator>&
2123basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const_pointer __s, size_type __n)
2124{
2125#ifdef _LIBCPP_DEBUG
2126 assert(__s != 0);
2127#endif
2128 size_type __sz = size();
2129 if (__pos > __sz)
2130 this->__throw_out_of_range();
2131 size_type __cap = capacity();
2132 if (__cap - __sz >= __n)
2133 {
2134 if (__n)
2135 {
2136 pointer __p = __get_pointer();
2137 size_type __n_move = __sz - __pos;
2138 if (__n_move != 0)
2139 {
2140 if (__p + __pos <= __s && __s < __p + __sz)
2141 __s += __n;
2142 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2143 }
2144 traits_type::move(__p + __pos, __s, __n);
2145 __sz += __n;
2146 __set_size(__sz);
2147 traits_type::assign(__p[__sz], value_type());
2148 }
2149 }
2150 else
2151 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2152 return *this;
2153}
2154
2155template <class _CharT, class _Traits, class _Allocator>
2156basic_string<_CharT, _Traits, _Allocator>&
2157basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2158{
2159 size_type __sz = size();
2160 if (__pos > __sz)
2161 this->__throw_out_of_range();
2162 if (__n)
2163 {
2164 size_type __cap = capacity();
2165 pointer __p;
2166 if (__cap - __sz >= __n)
2167 {
2168 __p = __get_pointer();
2169 size_type __n_move = __sz - __pos;
2170 if (__n_move != 0)
2171 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2172 }
2173 else
2174 {
2175 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
2176 __p = __get_long_pointer();
2177 }
2178 traits_type::assign(__p + __pos, __n, __c);
2179 __sz += __n;
2180 __set_size(__sz);
2181 traits_type::assign(__p[__sz], value_type());
2182 }
2183 return *this;
2184}
2185
2186template <class _CharT, class _Traits, class _Allocator>
2187template<class _InputIterator>
2188typename enable_if
2189<
2190 __is_input_iterator <_InputIterator>::value &&
2191 !__is_forward_iterator<_InputIterator>::value,
2192 typename basic_string<_CharT, _Traits, _Allocator>::iterator
2193>::type
2194basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2195{
2196 size_type __old_sz = size();
2197 difference_type __ip = __pos - begin();
2198 for (; __first != __last; ++__first)
2199 push_back(*__first);
2200 pointer __p = __get_pointer();
2201 _STD::rotate(__p + __ip, __p + __old_sz, __p + size());
2202 return iterator(__p + __ip);
2203}
2204
2205template <class _CharT, class _Traits, class _Allocator>
2206template<class _ForwardIterator>
2207typename enable_if
2208<
2209 __is_forward_iterator<_ForwardIterator>::value,
2210 typename basic_string<_CharT, _Traits, _Allocator>::iterator
2211>::type
2212basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2213{
2214 size_type __ip = static_cast<size_type>(__pos - begin());
2215 size_type __sz = size();
2216 size_type __cap = capacity();
2217 size_type __n = static_cast<size_type>(_STD::distance(__first, __last));
2218 if (__n)
2219 {
2220 pointer __p;
2221 if (__cap - __sz >= __n)
2222 {
2223 __p = __get_pointer();
2224 size_type __n_move = __sz - __ip;
2225 if (__n_move != 0)
2226 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
2227 }
2228 else
2229 {
2230 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
2231 __p = __get_long_pointer();
2232 }
2233 __sz += __n;
2234 __set_size(__sz);
2235 traits_type::assign(__p[__sz], value_type());
2236 for (__p += __ip; __first != __last; ++__p, ++__first)
2237 traits_type::assign(*__p, *__first);
2238 }
2239 return begin() + __ip;
2240}
2241
2242template <class _CharT, class _Traits, class _Allocator>
2243_LIBCPP_INLINE_VISIBILITY inline
2244basic_string<_CharT, _Traits, _Allocator>&
2245basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2246{
2247 return insert(__pos1, __str.data(), __str.size());
2248}
2249
2250template <class _CharT, class _Traits, class _Allocator>
2251basic_string<_CharT, _Traits, _Allocator>&
2252basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2253 size_type __pos2, size_type __n)
2254{
2255 size_type __str_sz = __str.size();
2256 if (__pos2 > __str_sz)
2257 this->__throw_out_of_range();
2258 return insert(__pos1, __str.data() + __pos2, _STD::min(__n, __str_sz - __pos2));
2259}
2260
2261template <class _CharT, class _Traits, class _Allocator>
2262basic_string<_CharT, _Traits, _Allocator>&
2263basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const_pointer __s)
2264{
2265#ifdef _LIBCPP_DEBUG
2266 assert(__s != 0);
2267#endif
2268 return insert(__pos, __s, traits_type::length(__s));
2269}
2270
2271template <class _CharT, class _Traits, class _Allocator>
2272typename basic_string<_CharT, _Traits, _Allocator>::iterator
2273basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2274{
2275 size_type __ip = static_cast<size_type>(__pos - begin());
2276 size_type __sz = size();
2277 size_type __cap = capacity();
2278 pointer __p;
2279 if (__cap == __sz)
2280 {
2281 __grow_by(__cap, 1, __sz, __ip, 0, 1);
2282 __p = __get_long_pointer();
2283 }
2284 else
2285 {
2286 __p = __get_pointer();
2287 size_type __n_move = __sz - __ip;
2288 if (__n_move != 0)
2289 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2290 }
2291 traits_type::assign(__p[__ip], __c);
2292 traits_type::assign(__p[++__sz], value_type());
2293 __set_size(__sz);
2294 return begin() + static_cast<difference_type>(__ip);
2295}
2296
2297template <class _CharT, class _Traits, class _Allocator>
2298_LIBCPP_INLINE_VISIBILITY inline
2299typename basic_string<_CharT, _Traits, _Allocator>::iterator
2300basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2301{
2302 difference_type __p = __pos - begin();
2303 insert(static_cast<size_type>(__p), __n, __c);
2304 return begin() + __p;
2305}
2306
2307// replace
2308
2309template <class _CharT, class _Traits, class _Allocator>
2310basic_string<_CharT, _Traits, _Allocator>&
2311basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const_pointer __s, size_type __n2)
2312{
2313#ifdef _LIBCPP_DEBUG
2314 assert(__s != 0);
2315#endif
2316 size_type __sz = size();
2317 if (__pos > __sz)
2318 this->__throw_out_of_range();
2319 __n1 = _STD::min(__n1, __sz - __pos);
2320 size_type __cap = capacity();
2321 if (__cap - __sz + __n1 >= __n2)
2322 {
2323 pointer __p = __get_pointer();
2324 if (__n1 != __n2)
2325 {
2326 size_type __n_move = __sz - __pos - __n1;
2327 if (__n_move != 0)
2328 {
2329 if (__n1 > __n2)
2330 {
2331 traits_type::move(__p + __pos, __s, __n2);
2332 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2333 goto __finish;
2334 }
2335 if (__p + __pos < __s && __s < __p + __sz)
2336 {
2337 if (__p + __pos + __n1 <= __s)
2338 __s += __n2 - __n1;
2339 else // __p + __pos < __s < __p + __pos + __n1
2340 {
2341 traits_type::move(__p + __pos, __s, __n1);
2342 __pos += __n1;
2343 __s += __n2;
2344 __n2 -= __n1;
2345 __n1 = 0;
2346 }
2347 }
2348 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2349 }
2350 }
2351 traits_type::move(__p + __pos, __s, __n2);
2352__finish:
2353 __sz += __n2 - __n1;
2354 __set_size(__sz);
2355 __invalidate_iterators_past(__sz);
2356 traits_type::assign(__p[__sz], value_type());
2357 }
2358 else
2359 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
2360 return *this;
2361}
2362
2363template <class _CharT, class _Traits, class _Allocator>
2364basic_string<_CharT, _Traits, _Allocator>&
2365basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
2366{
2367 size_type __sz = size();
2368 if (__pos > __sz)
2369 this->__throw_out_of_range();
2370 __n1 = _STD::min(__n1, __sz - __pos);
2371 size_type __cap = capacity();
2372 pointer __p;
2373 if (__cap - __sz + __n1 >= __n2)
2374 {
2375 __p = __get_pointer();
2376 if (__n1 != __n2)
2377 {
2378 size_type __n_move = __sz - __pos - __n1;
2379 if (__n_move != 0)
2380 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2381 }
2382 }
2383 else
2384 {
2385 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
2386 __p = __get_long_pointer();
2387 }
2388 traits_type::assign(__p + __pos, __n2, __c);
2389 __sz += __n2 - __n1;
2390 __set_size(__sz);
2391 __invalidate_iterators_past(__sz);
2392 traits_type::assign(__p[__sz], value_type());
2393 return *this;
2394}
2395
2396template <class _CharT, class _Traits, class _Allocator>
2397template<class _InputIterator>
2398typename enable_if
2399<
2400 __is_input_iterator<_InputIterator>::value,
2401 basic_string<_CharT, _Traits, _Allocator>&
2402>::type
2403basic_string<_CharT, _Traits, _Allocator>::replace(iterator __i1, iterator __i2,
2404 _InputIterator __j1, _InputIterator __j2)
2405{
2406 for (; true; ++__i1, ++__j1)
2407 {
2408 if (__i1 == __i2)
2409 {
2410 if (__j1 != __j2)
2411 insert(__i1, __j1, __j2);
2412 break;
2413 }
2414 if (__j1 == __j2)
2415 {
2416 erase(__i1, __i2);
2417 break;
2418 }
2419 traits_type::assign(*__i1, *__j1);
2420 }
2421 return *this;
2422}
2423
2424template <class _CharT, class _Traits, class _Allocator>
2425_LIBCPP_INLINE_VISIBILITY inline
2426basic_string<_CharT, _Traits, _Allocator>&
2427basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
2428{
2429 return replace(__pos1, __n1, __str.data(), __str.size());
2430}
2431
2432template <class _CharT, class _Traits, class _Allocator>
2433basic_string<_CharT, _Traits, _Allocator>&
2434basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
2435 size_type __pos2, size_type __n2)
2436{
2437 size_type __str_sz = __str.size();
2438 if (__pos2 > __str_sz)
2439 this->__throw_out_of_range();
2440 return replace(__pos1, __n1, __str.data() + __pos2, _STD::min(__n2, __str_sz - __pos2));
2441}
2442
2443template <class _CharT, class _Traits, class _Allocator>
2444basic_string<_CharT, _Traits, _Allocator>&
2445basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const_pointer __s)
2446{
2447#ifdef _LIBCPP_DEBUG
2448 assert(__s != 0);
2449#endif
2450 return replace(__pos, __n1, __s, traits_type::length(__s));
2451}
2452
2453template <class _CharT, class _Traits, class _Allocator>
2454_LIBCPP_INLINE_VISIBILITY inline
2455basic_string<_CharT, _Traits, _Allocator>&
2456basic_string<_CharT, _Traits, _Allocator>::replace(iterator __i1, iterator __i2, const basic_string& __str)
2457{
2458 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
2459 __str.data(), __str.size());
2460}
2461
2462template <class _CharT, class _Traits, class _Allocator>
2463_LIBCPP_INLINE_VISIBILITY inline
2464basic_string<_CharT, _Traits, _Allocator>&
2465basic_string<_CharT, _Traits, _Allocator>::replace(iterator __i1, iterator __i2, const_pointer __s, size_type __n)
2466{
2467 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
2468}
2469
2470template <class _CharT, class _Traits, class _Allocator>
2471_LIBCPP_INLINE_VISIBILITY inline
2472basic_string<_CharT, _Traits, _Allocator>&
2473basic_string<_CharT, _Traits, _Allocator>::replace(iterator __i1, iterator __i2, const_pointer __s)
2474{
2475 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
2476}
2477
2478template <class _CharT, class _Traits, class _Allocator>
2479_LIBCPP_INLINE_VISIBILITY inline
2480basic_string<_CharT, _Traits, _Allocator>&
2481basic_string<_CharT, _Traits, _Allocator>::replace(iterator __i1, iterator __i2, size_type __n, value_type __c)
2482{
2483 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
2484}
2485
2486// erase
2487
2488template <class _CharT, class _Traits, class _Allocator>
2489basic_string<_CharT, _Traits, _Allocator>&
2490basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos, size_type __n)
2491{
2492 size_type __sz = size();
2493 if (__pos > __sz)
2494 this->__throw_out_of_range();
2495 if (__n)
2496 {
2497 pointer __p = __get_pointer();
2498 __n = _STD::min(__n, __sz - __pos);
2499 size_type __n_move = __sz - __pos - __n;
2500 if (__n_move != 0)
2501 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
2502 __sz -= __n;
2503 __set_size(__sz);
2504 __invalidate_iterators_past(__sz);
2505 traits_type::assign(__p[__sz], value_type());
2506 }
2507 return *this;
2508}
2509
2510template <class _CharT, class _Traits, class _Allocator>
2511_LIBCPP_INLINE_VISIBILITY inline
2512typename basic_string<_CharT, _Traits, _Allocator>::iterator
2513basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
2514{
2515 iterator __b = begin();
2516 size_type __r = static_cast<size_type>(__pos - __b);
2517 erase(__r, 1);
2518 return __b + __r;
2519}
2520
2521template <class _CharT, class _Traits, class _Allocator>
2522_LIBCPP_INLINE_VISIBILITY inline
2523typename basic_string<_CharT, _Traits, _Allocator>::iterator
2524basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
2525{
2526 iterator __b = begin();
2527 size_type __r = static_cast<size_type>(__first - __b);
2528 erase(__r, static_cast<size_type>(__last - __first));
2529 return __b + __r;
2530}
2531
2532template <class _CharT, class _Traits, class _Allocator>
2533_LIBCPP_INLINE_VISIBILITY inline
2534void
2535basic_string<_CharT, _Traits, _Allocator>::pop_back()
2536{
2537#ifdef _LIBCPP_DEBUG
2538 assert(!empty());
2539#endif
2540 size_type __sz;
2541 if (__is_long())
2542 {
2543 __sz = __get_long_size() - 1;
2544 __set_long_size(__sz);
2545 traits_type::assign(*(__get_long_pointer() + __sz), value_type());
2546 }
2547 else
2548 {
2549 __sz = __get_short_size() - 1;
2550 __set_short_size(__sz);
2551 traits_type::assign(*(__get_short_pointer() + __sz), value_type());
2552 }
2553 __invalidate_iterators_past(__sz);
2554}
2555
2556template <class _CharT, class _Traits, class _Allocator>
2557_LIBCPP_INLINE_VISIBILITY inline
2558void
2559basic_string<_CharT, _Traits, _Allocator>::clear()
2560{
2561 __invalidate_all_iterators();
2562 if (__is_long())
2563 {
2564 traits_type::assign(*__get_long_pointer(), value_type());
2565 __set_long_size(0);
2566 }
2567 else
2568 {
2569 traits_type::assign(*__get_short_pointer(), value_type());
2570 __set_short_size(0);
2571 }
2572}
2573
2574template <class _CharT, class _Traits, class _Allocator>
2575_LIBCPP_INLINE_VISIBILITY inline
2576void
2577basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
2578{
2579 if (__is_long())
2580 {
2581 traits_type::assign(*(__get_long_pointer() + __pos), value_type());
2582 __set_long_size(__pos);
2583 }
2584 else
2585 {
2586 traits_type::assign(*(__get_short_pointer() + __pos), value_type());
2587 __set_short_size(__pos);
2588 }
2589 __invalidate_iterators_past(__pos);
2590}
2591
2592template <class _CharT, class _Traits, class _Allocator>
2593void
2594basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
2595{
2596 size_type __sz = size();
2597 if (__n > __sz)
2598 append(__n - __sz, __c);
2599 else
2600 __erase_to_end(__n);
2601}
2602
2603template <class _CharT, class _Traits, class _Allocator>
2604_LIBCPP_INLINE_VISIBILITY inline
2605typename basic_string<_CharT, _Traits, _Allocator>::size_type
2606basic_string<_CharT, _Traits, _Allocator>::max_size() const
2607{
Howard Hinnante32b5e22010-11-17 17:55:08 +00002608 size_type __m = __alloc_traits::max_size(__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002609#if _LIBCPP_BIG_ENDIAN
2610 return (__m <= ~__long_mask ? __m : __m/2) - 1;
2611#else
2612 return __m - 1;
2613#endif
2614}
2615
2616template <class _CharT, class _Traits, class _Allocator>
2617void
2618basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __res_arg)
2619{
2620 if (__res_arg > max_size())
2621 this->__throw_length_error();
2622 size_type __cap = capacity();
2623 size_type __sz = size();
2624 __res_arg = _STD::max(__res_arg, __sz);
2625 __res_arg = __recommend(__res_arg);
2626 if (__res_arg != __cap)
2627 {
2628 pointer __new_data, __p;
2629 bool __was_long, __now_long;
2630 if (__res_arg == __min_cap - 1)
2631 {
2632 __was_long = true;
2633 __now_long = false;
2634 __new_data = __get_short_pointer();
2635 __p = __get_long_pointer();
2636 }
2637 else
2638 {
2639 if (__res_arg > __cap)
Howard Hinnante32b5e22010-11-17 17:55:08 +00002640 __new_data = __alloc_traits::allocate(__alloc(), __res_arg+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002641 else
2642 {
2643 #ifndef _LIBCPP_NO_EXCEPTIONS
2644 try
2645 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002646 #endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnante32b5e22010-11-17 17:55:08 +00002647 __new_data = __alloc_traits::allocate(__alloc(), __res_arg+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002648 #ifndef _LIBCPP_NO_EXCEPTIONS
2649 }
2650 catch (...)
2651 {
2652 return;
2653 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002654 #else // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002655 if (__new_data == 0)
2656 return;
Howard Hinnant324bb032010-08-22 00:02:43 +00002657 #endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002658 }
2659 __now_long = true;
2660 __was_long = __is_long();
2661 __p = __get_pointer();
2662 }
2663 traits_type::copy(__new_data, __p, size()+1);
2664 if (__was_long)
Howard Hinnante32b5e22010-11-17 17:55:08 +00002665 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002666 if (__now_long)
2667 {
2668 __set_long_cap(__res_arg+1);
2669 __set_long_size(__sz);
2670 __set_long_pointer(__new_data);
2671 }
2672 else
2673 __set_short_size(__sz);
2674 __invalidate_all_iterators();
2675 }
2676}
2677
2678template <class _CharT, class _Traits, class _Allocator>
2679_LIBCPP_INLINE_VISIBILITY inline
2680typename basic_string<_CharT, _Traits, _Allocator>::const_reference
2681basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const
2682{
2683#ifdef __LIBCPP_DEBUG
2684 assert(__pos <= size());
2685#endif
2686 return *(data() + __pos);
2687}
2688
2689template <class _CharT, class _Traits, class _Allocator>
2690_LIBCPP_INLINE_VISIBILITY inline
2691typename basic_string<_CharT, _Traits, _Allocator>::reference
2692basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos)
2693{
2694#ifdef __LIBCPP_DEBUG
2695 assert(__pos < size());
2696#endif
2697 return *(__get_pointer() + __pos);
2698}
2699
2700template <class _CharT, class _Traits, class _Allocator>
2701typename basic_string<_CharT, _Traits, _Allocator>::const_reference
2702basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
2703{
2704 if (__n >= size())
2705 this->__throw_out_of_range();
2706 return (*this)[__n];
2707}
2708
2709template <class _CharT, class _Traits, class _Allocator>
2710typename basic_string<_CharT, _Traits, _Allocator>::reference
2711basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
2712{
2713 if (__n >= size())
2714 this->__throw_out_of_range();
2715 return (*this)[__n];
2716}
2717
2718template <class _CharT, class _Traits, class _Allocator>
2719_LIBCPP_INLINE_VISIBILITY inline
2720typename basic_string<_CharT, _Traits, _Allocator>::reference
2721basic_string<_CharT, _Traits, _Allocator>::front()
2722{
2723#ifdef _LIBCPP_DEBUG
2724 assert(!empty());
2725#endif
2726 return *__get_pointer();
2727}
2728
2729template <class _CharT, class _Traits, class _Allocator>
2730_LIBCPP_INLINE_VISIBILITY inline
2731typename basic_string<_CharT, _Traits, _Allocator>::const_reference
2732basic_string<_CharT, _Traits, _Allocator>::front() const
2733{
2734#ifdef _LIBCPP_DEBUG
2735 assert(!empty());
2736#endif
2737 return *data();
2738}
2739
2740template <class _CharT, class _Traits, class _Allocator>
2741_LIBCPP_INLINE_VISIBILITY inline
2742typename basic_string<_CharT, _Traits, _Allocator>::reference
2743basic_string<_CharT, _Traits, _Allocator>::back()
2744{
2745#ifdef _LIBCPP_DEBUG
2746 assert(!empty());
2747#endif
2748 return *(__get_pointer() + size() - 1);
2749}
2750
2751template <class _CharT, class _Traits, class _Allocator>
2752_LIBCPP_INLINE_VISIBILITY inline
2753typename basic_string<_CharT, _Traits, _Allocator>::const_reference
2754basic_string<_CharT, _Traits, _Allocator>::back() const
2755{
2756#ifdef _LIBCPP_DEBUG
2757 assert(!empty());
2758#endif
2759 return *(data() + size() - 1);
2760}
2761
2762template <class _CharT, class _Traits, class _Allocator>
2763typename basic_string<_CharT, _Traits, _Allocator>::size_type
2764basic_string<_CharT, _Traits, _Allocator>::copy(pointer __s, size_type __n, size_type __pos) const
2765{
2766 size_type __sz = size();
2767 if (__pos > __sz)
2768 this->__throw_out_of_range();
2769 size_type __rlen = _STD::min(__n, __sz - __pos);
2770 traits_type::copy(__s, data() + __pos, __rlen);
2771 return __rlen;
2772}
2773
2774template <class _CharT, class _Traits, class _Allocator>
2775_LIBCPP_INLINE_VISIBILITY inline
2776basic_string<_CharT, _Traits, _Allocator>
2777basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
2778{
2779 return basic_string(*this, __pos, __n, __alloc());
2780}
2781
2782template <class _CharT, class _Traits, class _Allocator>
2783_LIBCPP_INLINE_VISIBILITY inline
2784void
2785basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
2786{
Howard Hinnante32b5e22010-11-17 17:55:08 +00002787 _STD::swap(__r_.first(), __str.__r_.first());
2788 __swap_alloc(__alloc(), __str.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002789#ifdef _LIBCPP_DEBUG
2790 __invalidate_all_iterators();
2791 __str.__invalidate_all_iterators();
Howard Hinnant324bb032010-08-22 00:02:43 +00002792#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002793}
2794
2795// find
2796
2797template <class _Traits>
2798struct _LIBCPP_HIDDEN __traits_eq
2799{
2800 typedef typename _Traits::char_type char_type;
2801 _LIBCPP_INLINE_VISIBILITY bool operator()(const char_type& __x, const char_type& __y) {return _Traits::eq(__x, __y);}
2802};
2803
2804template<class _CharT, class _Traits, class _Allocator>
2805typename basic_string<_CharT, _Traits, _Allocator>::size_type
2806basic_string<_CharT, _Traits, _Allocator>::find(const_pointer __s, size_type __pos, size_type __n) const
2807{
2808#ifdef _LIBCPP_DEBUG
2809 assert(__s != 0);
2810#endif
2811 size_type __sz = size();
2812 if (__pos > __sz || __sz - __pos < __n)
2813 return npos;
2814 if (__n == 0)
2815 return __pos;
2816 const_pointer __p = data();
2817 const_pointer __r = _STD::search(__p + __pos, __p + __sz, __s, __s + __n, __traits_eq<traits_type>());
2818 if (__r == __p + __sz)
2819 return npos;
2820 return static_cast<size_type>(__r - __p);
2821}
2822
2823template<class _CharT, class _Traits, class _Allocator>
2824_LIBCPP_INLINE_VISIBILITY inline
2825typename basic_string<_CharT, _Traits, _Allocator>::size_type
2826basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str, size_type __pos) const
2827{
2828 return find(__str.data(), __pos, __str.size());
2829}
2830
2831template<class _CharT, class _Traits, class _Allocator>
2832_LIBCPP_INLINE_VISIBILITY inline
2833typename basic_string<_CharT, _Traits, _Allocator>::size_type
2834basic_string<_CharT, _Traits, _Allocator>::find(const_pointer __s, size_type __pos) const
2835{
2836#ifdef _LIBCPP_DEBUG
2837 assert(__s != 0);
2838#endif
2839 return find(__s, __pos, traits_type::length(__s));
2840}
2841
2842template<class _CharT, class _Traits, class _Allocator>
2843typename basic_string<_CharT, _Traits, _Allocator>::size_type
2844basic_string<_CharT, _Traits, _Allocator>::find(value_type __c, size_type __pos) const
2845{
2846 size_type __sz = size();
2847 if (__pos >= __sz)
2848 return npos;
2849 const_pointer __p = data();
2850 const_pointer __r = traits_type::find(__p + __pos, __sz - __pos, __c);
2851 if (__r == 0)
2852 return npos;
2853 return static_cast<size_type>(__r - __p);
2854}
2855
2856// rfind
2857
2858template<class _CharT, class _Traits, class _Allocator>
2859typename basic_string<_CharT, _Traits, _Allocator>::size_type
2860basic_string<_CharT, _Traits, _Allocator>::rfind(const_pointer __s, size_type __pos, size_type __n) const
2861{
2862#ifdef _LIBCPP_DEBUG
2863 assert(__s != 0);
2864#endif
2865 size_type __sz = size();
2866 __pos = _STD::min(__pos, __sz);
2867 if (__n < __sz - __pos)
2868 __pos += __n;
2869 else
2870 __pos = __sz;
2871 const_pointer __p = data();
2872 const_pointer __r = _STD::find_end(__p, __p + __pos, __s, __s + __n, __traits_eq<traits_type>());
2873 if (__n > 0 && __r == __p + __pos)
2874 return npos;
2875 return static_cast<size_type>(__r - __p);
2876}
2877
2878template<class _CharT, class _Traits, class _Allocator>
2879_LIBCPP_INLINE_VISIBILITY inline
2880typename basic_string<_CharT, _Traits, _Allocator>::size_type
2881basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str, size_type __pos) const
2882{
2883 return rfind(__str.data(), __pos, __str.size());
2884}
2885
2886template<class _CharT, class _Traits, class _Allocator>
2887_LIBCPP_INLINE_VISIBILITY inline
2888typename basic_string<_CharT, _Traits, _Allocator>::size_type
2889basic_string<_CharT, _Traits, _Allocator>::rfind(const_pointer __s, size_type __pos) const
2890{
2891#ifdef _LIBCPP_DEBUG
2892 assert(__s != 0);
2893#endif
2894 return rfind(__s, __pos, traits_type::length(__s));
2895}
2896
2897template<class _CharT, class _Traits, class _Allocator>
2898typename basic_string<_CharT, _Traits, _Allocator>::size_type
2899basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c, size_type __pos) const
2900{
2901 size_type __sz = size();
2902 if (__sz)
2903 {
2904 if (__pos < __sz)
2905 ++__pos;
2906 else
2907 __pos = __sz;
2908 const_pointer __p = data();
2909 for (const_pointer __ps = __p + __pos; __ps != __p;)
2910 {
2911 if (traits_type::eq(*--__ps, __c))
2912 return static_cast<size_type>(__ps - __p);
2913 }
2914 }
2915 return npos;
2916}
2917
2918// find_first_of
2919
2920template<class _CharT, class _Traits, class _Allocator>
2921typename basic_string<_CharT, _Traits, _Allocator>::size_type
2922basic_string<_CharT, _Traits, _Allocator>::find_first_of(const_pointer __s, size_type __pos, size_type __n) const
2923{
2924#ifdef _LIBCPP_DEBUG
2925 assert(__s != 0);
2926#endif
2927 size_type __sz = size();
2928 if (__pos >= __sz || __n == 0)
2929 return npos;
2930 const_pointer __p = data();
2931 const_pointer __r = _STD::find_first_of(__p + __pos, __p + __sz, __s, __s + __n, __traits_eq<traits_type>());
2932 if (__r == __p + __sz)
2933 return npos;
2934 return static_cast<size_type>(__r - __p);
2935}
2936
2937template<class _CharT, class _Traits, class _Allocator>
2938_LIBCPP_INLINE_VISIBILITY inline
2939typename basic_string<_CharT, _Traits, _Allocator>::size_type
2940basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str, size_type __pos) const
2941{
2942 return find_first_of(__str.data(), __pos, __str.size());
2943}
2944
2945template<class _CharT, class _Traits, class _Allocator>
2946_LIBCPP_INLINE_VISIBILITY inline
2947typename basic_string<_CharT, _Traits, _Allocator>::size_type
2948basic_string<_CharT, _Traits, _Allocator>::find_first_of(const_pointer __s, size_type __pos) const
2949{
2950#ifdef _LIBCPP_DEBUG
2951 assert(__s != 0);
2952#endif
2953 return find_first_of(__s, __pos, traits_type::length(__s));
2954}
2955
2956template<class _CharT, class _Traits, class _Allocator>
2957_LIBCPP_INLINE_VISIBILITY inline
2958typename basic_string<_CharT, _Traits, _Allocator>::size_type
2959basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c, size_type __pos) const
2960{
2961 return find(__c, __pos);
2962}
2963
2964// find_last_of
2965
2966template<class _CharT, class _Traits, class _Allocator>
2967typename basic_string<_CharT, _Traits, _Allocator>::size_type
2968basic_string<_CharT, _Traits, _Allocator>::find_last_of(const_pointer __s, size_type __pos, size_type __n) const
2969{
2970#ifdef _LIBCPP_DEBUG
2971 assert(__s != 0);
2972#endif
2973 if (__n != 0)
2974 {
2975 size_type __sz = size();
2976 if (__pos < __sz)
2977 ++__pos;
2978 else
2979 __pos = __sz;
2980 const_pointer __p = data();
2981 for (const_pointer __ps = __p + __pos; __ps != __p;)
2982 {
2983 const_pointer __r = traits_type::find(__s, __n, *--__ps);
2984 if (__r)
2985 return static_cast<size_type>(__ps - __p);
2986 }
2987 }
2988 return npos;
2989}
2990
2991template<class _CharT, class _Traits, class _Allocator>
2992_LIBCPP_INLINE_VISIBILITY inline
2993typename basic_string<_CharT, _Traits, _Allocator>::size_type
2994basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str, size_type __pos) const
2995{
2996 return find_last_of(__str.data(), __pos, __str.size());
2997}
2998
2999template<class _CharT, class _Traits, class _Allocator>
3000_LIBCPP_INLINE_VISIBILITY inline
3001typename basic_string<_CharT, _Traits, _Allocator>::size_type
3002basic_string<_CharT, _Traits, _Allocator>::find_last_of(const_pointer __s, size_type __pos) const
3003{
3004#ifdef _LIBCPP_DEBUG
3005 assert(__s != 0);
3006#endif
3007 return find_last_of(__s, __pos, traits_type::length(__s));
3008}
3009
3010template<class _CharT, class _Traits, class _Allocator>
3011_LIBCPP_INLINE_VISIBILITY inline
3012typename basic_string<_CharT, _Traits, _Allocator>::size_type
3013basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c, size_type __pos) const
3014{
3015 return rfind(__c, __pos);
3016}
3017
3018// find_first_not_of
3019
3020template<class _CharT, class _Traits, class _Allocator>
3021typename basic_string<_CharT, _Traits, _Allocator>::size_type
3022basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const_pointer __s, size_type __pos, size_type __n) const
3023{
3024#ifdef _LIBCPP_DEBUG
3025 assert(__s != 0);
3026#endif
3027 size_type __sz = size();
3028 if (__pos < __sz)
3029 {
3030 const_pointer __p = data();
3031 const_pointer __pe = __p + __sz;
3032 for (const_pointer __ps = __p + __pos; __ps != __pe; ++__ps)
3033 if (traits_type::find(__s, __n, *__ps) == 0)
3034 return static_cast<size_type>(__ps - __p);
3035 }
3036 return npos;
3037}
3038
3039template<class _CharT, class _Traits, class _Allocator>
3040_LIBCPP_INLINE_VISIBILITY inline
3041typename basic_string<_CharT, _Traits, _Allocator>::size_type
3042basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str, size_type __pos) const
3043{
3044 return find_first_not_of(__str.data(), __pos, __str.size());
3045}
3046
3047template<class _CharT, class _Traits, class _Allocator>
3048_LIBCPP_INLINE_VISIBILITY inline
3049typename basic_string<_CharT, _Traits, _Allocator>::size_type
3050basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const_pointer __s, size_type __pos) const
3051{
3052#ifdef _LIBCPP_DEBUG
3053 assert(__s != 0);
3054#endif
3055 return find_first_not_of(__s, __pos, traits_type::length(__s));
3056}
3057
3058template<class _CharT, class _Traits, class _Allocator>
3059_LIBCPP_INLINE_VISIBILITY inline
3060typename basic_string<_CharT, _Traits, _Allocator>::size_type
3061basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c, size_type __pos) const
3062{
3063 size_type __sz = size();
3064 if (__pos < __sz)
3065 {
3066 const_pointer __p = data();
3067 const_pointer __pe = __p + __sz;
3068 for (const_pointer __ps = __p + __pos; __p != __pe; ++__ps)
3069 if (!traits_type::eq(*__ps, __c))
3070 return static_cast<size_type>(__ps - __p);
3071 }
3072 return npos;
3073}
3074
3075// find_last_not_of
3076
3077template<class _CharT, class _Traits, class _Allocator>
3078typename basic_string<_CharT, _Traits, _Allocator>::size_type
3079basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const_pointer __s, size_type __pos, size_type __n) const
3080{
3081#ifdef _LIBCPP_DEBUG
3082 assert(__s != 0);
3083#endif
3084 size_type __sz = size();
3085 if (__pos < __sz)
3086 ++__pos;
3087 else
3088 __pos = __sz;
3089 const_pointer __p = data();
3090 for (const_pointer __ps = __p + __pos; __ps != __p;)
3091 if (traits_type::find(__s, __n, *--__ps) == 0)
3092 return static_cast<size_type>(__ps - __p);
3093 return npos;
3094}
3095
3096template<class _CharT, class _Traits, class _Allocator>
3097_LIBCPP_INLINE_VISIBILITY inline
3098typename basic_string<_CharT, _Traits, _Allocator>::size_type
3099basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str, size_type __pos) const
3100{
3101 return find_last_not_of(__str.data(), __pos, __str.size());
3102}
3103
3104template<class _CharT, class _Traits, class _Allocator>
3105_LIBCPP_INLINE_VISIBILITY inline
3106typename basic_string<_CharT, _Traits, _Allocator>::size_type
3107basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const_pointer __s, size_type __pos) const
3108{
3109#ifdef _LIBCPP_DEBUG
3110 assert(__s != 0);
3111#endif
3112 return find_last_not_of(__s, __pos, traits_type::length(__s));
3113}
3114
3115template<class _CharT, class _Traits, class _Allocator>
3116_LIBCPP_INLINE_VISIBILITY inline
3117typename basic_string<_CharT, _Traits, _Allocator>::size_type
3118basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c, size_type __pos) const
3119{
3120 size_type __sz = size();
3121 if (__pos < __sz)
3122 ++__pos;
3123 else
3124 __pos = __sz;
3125 const_pointer __p = data();
3126 for (const_pointer __ps = __p + __pos; __ps != __p;)
3127 if (!traits_type::eq(*--__ps, __c))
3128 return static_cast<size_type>(__ps - __p);
3129 return npos;
3130}
3131
3132// compare
3133
3134template <class _CharT, class _Traits, class _Allocator>
3135_LIBCPP_INLINE_VISIBILITY inline
3136int
3137basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const
3138{
3139 return compare(0, npos, __str.data(), __str.size());
3140}
3141
3142template <class _CharT, class _Traits, class _Allocator>
3143_LIBCPP_INLINE_VISIBILITY inline
3144int
3145basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1, size_type __n1, const basic_string& __str) const
3146{
3147 return compare(__pos1, __n1, __str.data(), __str.size());
3148}
3149
3150template <class _CharT, class _Traits, class _Allocator>
3151int
3152basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1, size_type __n1, const basic_string& __str,
3153 size_type __pos2, size_type __n2) const
3154{
3155 size_type __sz = __str.size();
3156 if (__pos2 > __sz)
3157 this->__throw_out_of_range();
3158 return compare(__pos1, __n1, __str.data() + __pos2, _STD::min(__n2, __sz - __pos2));
3159}
3160
3161template <class _CharT, class _Traits, class _Allocator>
3162int
3163basic_string<_CharT, _Traits, _Allocator>::compare(const_pointer __s) const
3164{
3165#ifdef _LIBCPP_DEBUG
3166 assert(__s != 0);
3167#endif
3168 return compare(0, npos, __s, traits_type::length(__s));
3169}
3170
3171template <class _CharT, class _Traits, class _Allocator>
3172int
3173basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1, size_type __n1, const_pointer __s) const
3174{
3175#ifdef _LIBCPP_DEBUG
3176 assert(__s != 0);
3177#endif
3178 return compare(__pos1, __n1, __s, traits_type::length(__s));
3179}
3180
3181template <class _CharT, class _Traits, class _Allocator>
3182int
3183basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1, size_type __n1,
3184 const_pointer __s, size_type __n2) const
3185{
3186#ifdef _LIBCPP_DEBUG
3187 assert(__s != 0);
3188#endif
3189 size_type __sz = size();
3190 if (__pos1 > __sz || __n2 == npos)
3191 this->__throw_out_of_range();
3192 size_type __rlen = _STD::min(__n1, __sz - __pos1);
3193 int __r = traits_type::compare(data() + __pos1, __s, _STD::min(__rlen, __n2));
3194 if (__r == 0)
3195 {
3196 if (__rlen < __n2)
3197 __r = -1;
3198 else if (__rlen > __n2)
3199 __r = 1;
3200 }
3201 return __r;
3202}
3203
3204// __invariants
3205
3206template<class _CharT, class _Traits, class _Allocator>
3207bool
3208basic_string<_CharT, _Traits, _Allocator>::__invariants() const
3209{
3210 if (size() > capacity())
3211 return false;
3212 if (capacity() < __min_cap - 1)
3213 return false;
3214 if (data() == 0)
3215 return false;
3216 if (data()[size()] != value_type(0))
3217 return false;
3218 return true;
3219}
3220
3221// operator==
3222
3223template<class _CharT, class _Traits, class _Allocator>
3224_LIBCPP_INLINE_VISIBILITY inline
3225bool
3226operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3227 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3228{
3229 return __lhs.size() == __rhs.size() && _Traits::compare(__lhs.data(), __rhs.data(), __lhs.size()) == 0;
3230}
3231
3232template<class _CharT, class _Traits, class _Allocator>
3233_LIBCPP_INLINE_VISIBILITY inline
3234bool
3235operator==(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3236{
3237 return __rhs.compare(__lhs) == 0;
3238}
3239
3240template<class _Allocator>
3241_LIBCPP_INLINE_VISIBILITY inline
3242bool
3243operator==(const char* __lhs, const basic_string<char, char_traits<char>, _Allocator>& __rhs)
3244{
3245 return strcmp(__lhs, __rhs.data()) == 0;
3246}
3247
3248template<class _Allocator>
3249_LIBCPP_INLINE_VISIBILITY inline
3250bool
3251operator==(const wchar_t* __lhs, const basic_string<wchar_t, char_traits<wchar_t>, _Allocator>& __rhs)
3252{
3253 return wcscmp(__lhs, __rhs.data()) == 0;
3254}
3255
3256template<class _CharT, class _Traits, class _Allocator>
3257_LIBCPP_INLINE_VISIBILITY inline
3258bool
3259operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs, const _CharT* __rhs)
3260{
3261 return __lhs.compare(__rhs) == 0;
3262}
3263
3264template<class _Allocator>
3265_LIBCPP_INLINE_VISIBILITY inline
3266bool
3267operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs, const char* __rhs)
3268{
3269 return strcmp(__lhs.data(), __rhs) == 0;
3270}
3271
3272template<class _Allocator>
3273_LIBCPP_INLINE_VISIBILITY inline
3274bool
3275operator==(const basic_string<wchar_t, char_traits<wchar_t>, _Allocator>& __lhs, const wchar_t* __rhs)
3276{
3277 return wcscmp(__lhs.data(), __rhs) == 0;
3278}
3279
3280// operator!=
3281
Howard Hinnant324bb032010-08-22 00:02:43 +00003282template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003283_LIBCPP_INLINE_VISIBILITY inline
3284bool
3285operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
3286 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3287{
3288 return !(__lhs == __rhs);
3289}
3290
3291template<class _CharT, class _Traits, class _Allocator>
3292_LIBCPP_INLINE_VISIBILITY inline
3293bool
3294operator!=(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3295{
3296 return !(__lhs == __rhs);
3297}
3298
3299template<class _CharT, class _Traits, class _Allocator>
3300_LIBCPP_INLINE_VISIBILITY inline
3301bool
3302operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
3303{
3304 return !(__lhs == __rhs);
3305}
3306
3307// operator<
3308
3309template<class _CharT, class _Traits, class _Allocator>
3310_LIBCPP_INLINE_VISIBILITY inline
3311bool
3312operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3313 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3314{
3315 return __lhs.cmpare(__rhs) < 0;
3316}
3317
3318template<class _Allocator>
3319_LIBCPP_INLINE_VISIBILITY inline
3320bool
3321operator< (const basic_string<char, char_traits<char>, _Allocator>& __lhs,
3322 const basic_string<char, char_traits<char>, _Allocator>& __rhs)
3323{
3324 return strcmp(__lhs.data(), __rhs.data()) < 0;
3325}
3326
3327template<class _Allocator>
3328_LIBCPP_INLINE_VISIBILITY inline
3329bool
3330operator< (const basic_string<wchar_t, char_traits<wchar_t>, _Allocator>& __lhs,
3331 const basic_string<wchar_t, char_traits<wchar_t>, _Allocator>& __rhs)
3332{
3333 return wcscmp(__lhs.data(), __rhs.data()) < 0;
3334}
3335
3336template<class _CharT, class _Traits, class _Allocator>
3337_LIBCPP_INLINE_VISIBILITY inline
3338bool
3339operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
3340{
3341 return __lhs.compare(__rhs);
3342}
3343
3344template<class _Allocator>
3345_LIBCPP_INLINE_VISIBILITY inline
3346bool
3347operator< (const basic_string<char, char_traits<char>, _Allocator>& __lhs, const char* __rhs)
3348{
3349 return strcmp(__lhs.data(), __rhs) < 0;
3350}
3351
3352template<class _Allocator>
3353_LIBCPP_INLINE_VISIBILITY inline
3354bool
3355operator< (const basic_string<wchar_t, char_traits<wchar_t>, _Allocator>& __lhs, const wchar_t* __rhs)
3356{
3357 return wcscmp(__lhs.data(), __rhs) < 0;
3358}
3359
3360template<class _CharT, class _Traits, class _Allocator>
3361_LIBCPP_INLINE_VISIBILITY inline
3362bool
3363operator< (const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3364{
3365 return __rhs.compare(__lhs) > 0;
3366}
3367
3368template<class _Allocator>
3369_LIBCPP_INLINE_VISIBILITY inline
3370bool
3371operator< (const char* __lhs, const basic_string<char, char_traits<char>, _Allocator>& __rhs)
3372{
3373 return strcmp(__lhs, __rhs.data()) < 0;
3374}
3375
3376template<class _Allocator>
3377_LIBCPP_INLINE_VISIBILITY inline
3378bool
3379operator< (const wchar_t* __lhs, const basic_string<wchar_t, char_traits<wchar_t>, _Allocator>& __rhs)
3380{
3381 return wcscmp(__lhs, __rhs.data()) < 0;
3382}
3383
3384// operator>
3385
3386template<class _CharT, class _Traits, class _Allocator>
3387_LIBCPP_INLINE_VISIBILITY inline
3388bool
3389operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3390 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3391{
3392 return __rhs < __lhs;
3393}
3394
3395template<class _CharT, class _Traits, class _Allocator>
3396_LIBCPP_INLINE_VISIBILITY inline
3397bool
3398operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
3399{
3400 return __rhs < __lhs;
3401}
3402
3403template<class _CharT, class _Traits, class _Allocator>
3404_LIBCPP_INLINE_VISIBILITY inline
3405bool
3406operator> (const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3407{
3408 return __rhs < __lhs;
3409}
3410
3411// operator<=
3412
3413template<class _CharT, class _Traits, class _Allocator>
3414_LIBCPP_INLINE_VISIBILITY inline
3415bool
3416operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3417 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3418{
3419 return !(__rhs < __lhs);
3420}
3421
3422template<class _CharT, class _Traits, class _Allocator>
3423_LIBCPP_INLINE_VISIBILITY inline
3424bool
3425operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
3426{
3427 return !(__rhs < __lhs);
3428}
3429
3430template<class _CharT, class _Traits, class _Allocator>
3431_LIBCPP_INLINE_VISIBILITY inline
3432bool
3433operator<=(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3434{
3435 return !(__rhs < __lhs);
3436}
3437
3438// operator>=
3439
3440template<class _CharT, class _Traits, class _Allocator>
3441_LIBCPP_INLINE_VISIBILITY inline
3442bool
3443operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3444 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3445{
3446 return !(__lhs < __rhs);
3447}
3448
3449template<class _CharT, class _Traits, class _Allocator>
3450_LIBCPP_INLINE_VISIBILITY inline
3451bool
3452operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
3453{
3454 return !(__lhs < __rhs);
3455}
3456
3457template<class _CharT, class _Traits, class _Allocator>
3458_LIBCPP_INLINE_VISIBILITY inline
3459bool
3460operator>=(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3461{
3462 return !(__lhs < __rhs);
3463}
3464
3465// operator +
3466
3467template<class _CharT, class _Traits, class _Allocator>
3468basic_string<_CharT, _Traits, _Allocator>
3469operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3470 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3471{
3472 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
3473 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
3474 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
3475 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
3476 __r.append(__rhs.data(), __rhs_sz);
3477 return __r;
3478}
3479
3480template<class _CharT, class _Traits, class _Allocator>
3481basic_string<_CharT, _Traits, _Allocator>
3482operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
3483{
3484 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
3485 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = _Traits::length(__lhs);
3486 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
3487 __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz);
3488 __r.append(__rhs.data(), __rhs_sz);
3489 return __r;
3490}
3491
3492template<class _CharT, class _Traits, class _Allocator>
3493basic_string<_CharT, _Traits, _Allocator>
3494operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
3495{
3496 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
3497 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
3498 __r.__init(&__lhs, 1, 1 + __rhs_sz);
3499 __r.append(__rhs.data(), __rhs_sz);
3500 return __r;
3501}
3502
3503template<class _CharT, class _Traits, class _Allocator>
3504basic_string<_CharT, _Traits, _Allocator>
3505operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
3506{
3507 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
3508 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
3509 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = _Traits::length(__rhs);
3510 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
3511 __r.append(__rhs, __rhs_sz);
3512 return __r;
3513}
3514
3515template<class _CharT, class _Traits, class _Allocator>
3516basic_string<_CharT, _Traits, _Allocator>
3517operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
3518{
3519 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
3520 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
3521 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1);
3522 __r.push_back(__rhs);
3523 return __r;
3524}
3525
Howard Hinnant73d21a42010-09-04 23:28:19 +00003526#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003527
3528template<class _CharT, class _Traits, class _Allocator>
3529_LIBCPP_INLINE_VISIBILITY inline
3530basic_string<_CharT, _Traits, _Allocator>
3531operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3532{
3533 return _STD::move(__lhs.append(__rhs));
3534}
3535
3536template<class _CharT, class _Traits, class _Allocator>
3537_LIBCPP_INLINE_VISIBILITY inline
3538basic_string<_CharT, _Traits, _Allocator>
3539operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
3540{
3541 return _STD::move(__rhs.insert(0, __lhs));
3542}
3543
3544template<class _CharT, class _Traits, class _Allocator>
3545_LIBCPP_INLINE_VISIBILITY inline
3546basic_string<_CharT, _Traits, _Allocator>
3547operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
3548{
3549 return _STD::move(__lhs.append(__rhs));
3550}
3551
3552template<class _CharT, class _Traits, class _Allocator>
3553_LIBCPP_INLINE_VISIBILITY inline
3554basic_string<_CharT, _Traits, _Allocator>
3555operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
3556{
3557 return _STD::move(__rhs.insert(0, __lhs));
3558}
3559
3560template<class _CharT, class _Traits, class _Allocator>
3561_LIBCPP_INLINE_VISIBILITY inline
3562basic_string<_CharT, _Traits, _Allocator>
3563operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
3564{
3565 __rhs.insert(__rhs.begin(), __lhs);
3566 return _STD::move(__rhs);
3567}
3568
3569template<class _CharT, class _Traits, class _Allocator>
3570_LIBCPP_INLINE_VISIBILITY inline
3571basic_string<_CharT, _Traits, _Allocator>
3572operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
3573{
3574 return _STD::move(__lhs.append(__rhs));
3575}
3576
3577template<class _CharT, class _Traits, class _Allocator>
3578_LIBCPP_INLINE_VISIBILITY inline
3579basic_string<_CharT, _Traits, _Allocator>
3580operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
3581{
3582 __lhs.push_back(__rhs);
3583 return _STD::move(__lhs);
3584}
3585
Howard Hinnant73d21a42010-09-04 23:28:19 +00003586#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003587
3588// swap
3589
3590template<class _CharT, class _Traits, class _Allocator>
3591_LIBCPP_INLINE_VISIBILITY inline
3592void
3593swap(basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>& __rhs)
3594{
3595 __lhs.swap(__rhs);
3596}
3597
3598template<class _CharT, class _Traits, class _Allocator>
3599struct __is_zero_default_constructible<basic_string<_CharT, _Traits, _Allocator> >
3600 : public integral_constant<bool, __is_zero_default_constructible<_Allocator>::value> {};
3601
3602#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
3603
3604typedef basic_string<char16_t> u16string;
3605typedef basic_string<char32_t> u32string;
3606
Howard Hinnant324bb032010-08-22 00:02:43 +00003607#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003608
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003609int stoi (const string& __str, size_t* __idx = 0, int __base = 10);
3610long stol (const string& __str, size_t* __idx = 0, int __base = 10);
3611unsigned long stoul (const string& __str, size_t* __idx = 0, int __base = 10);
3612long long stoll (const string& __str, size_t* __idx = 0, int __base = 10);
3613unsigned long long stoull(const string& __str, size_t* __idx = 0, int __base = 10);
3614
3615float stof (const string& __str, size_t* __idx = 0);
3616double stod (const string& __str, size_t* __idx = 0);
3617long double stold(const string& __str, size_t* __idx = 0);
3618
3619string to_string(int __val);
3620string to_string(unsigned __val);
3621string to_string(long __val);
3622string to_string(unsigned long __val);
3623string to_string(long long __val);
3624string to_string(unsigned long long __val);
3625string to_string(float __val);
3626string to_string(double __val);
3627string to_string(long double __val);
3628
3629int stoi (const wstring& __str, size_t* __idx = 0, int __base = 10);
3630long stol (const wstring& __str, size_t* __idx = 0, int __base = 10);
3631unsigned long stoul (const wstring& __str, size_t* __idx = 0, int __base = 10);
3632long long stoll (const wstring& __str, size_t* __idx = 0, int __base = 10);
3633unsigned long long stoull(const wstring& __str, size_t* __idx = 0, int __base = 10);
3634
3635float stof (const wstring& __str, size_t* __idx = 0);
3636double stod (const wstring& __str, size_t* __idx = 0);
3637long double stold(const wstring& __str, size_t* __idx = 0);
3638
3639wstring to_wstring(int __val);
3640wstring to_wstring(unsigned __val);
3641wstring to_wstring(long __val);
3642wstring to_wstring(unsigned long __val);
3643wstring to_wstring(long long __val);
3644wstring to_wstring(unsigned long long __val);
3645wstring to_wstring(float __val);
3646wstring to_wstring(double __val);
3647wstring to_wstring(long double __val);
3648
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003649template<class _CharT, class _Traits, class _Allocator>
3650 const typename basic_string<_CharT, _Traits, _Allocator>::size_type
3651 basic_string<_CharT, _Traits, _Allocator>::npos;
3652
3653template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant8d7a9552010-09-23 17:31:07 +00003654struct _LIBCPP_VISIBLE hash<basic_string<_CharT, _Traits, _Allocator> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003655 : public unary_function<basic_string<_CharT, _Traits, _Allocator>, size_t>
3656{
3657 size_t
3658 operator()(const basic_string<_CharT, _Traits, _Allocator>& __val) const;
3659};
3660
3661template<class _CharT, class _Traits, class _Allocator>
3662size_t
3663hash<basic_string<_CharT, _Traits, _Allocator> >::operator()(
3664 const basic_string<_CharT, _Traits, _Allocator>& __val) const
3665{
3666 typedef basic_string<_CharT, _Traits, _Allocator> S;
3667 typedef typename S::const_pointer const_pointer;
3668 size_t __r = 0;
3669 const size_t __sr = __CHAR_BIT__ * sizeof(size_t) - 8;
3670 const size_t __m = size_t(0xF) << (__sr + 4);
3671 const_pointer __p = __val.data();
3672 const_pointer __e = __p + __val.size();
3673 for (; __p != __e; ++__p)
3674 {
3675 __r = (__r << 4) + *__p;
3676 size_t __g = __r & __m;
3677 __r ^= __g | (__g >> __sr);
3678 }
3679 return __r;
3680}
3681
3682extern template class basic_string<char>;
3683extern template class basic_string<wchar_t>;
3684
3685extern template
3686 enable_if<__is_forward_iterator<char const*>::value, void>::type
3687 basic_string<char, char_traits<char>, allocator<char> >::
3688 __init<char const*>(char const*, char const*);
3689
3690extern template
3691 enable_if<__is_forward_iterator<wchar_t const*>::value, void>::type
3692 basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> >::
3693 __init<wchar_t const*>(wchar_t const*, wchar_t const*);
3694
3695extern template
3696 enable_if<__is_forward_iterator<char*>::value,
3697 basic_string<char, char_traits<char>, allocator<char> >&>::type
3698 basic_string<char, char_traits<char>, allocator<char> >::
3699 append<char*>(char*, char*);
3700
3701extern template
3702 enable_if<__is_forward_iterator<wchar_t*>::value,
3703 basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> >&>::type
3704 basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> >::
3705 append<wchar_t*>(wchar_t*, wchar_t*);
3706
3707extern template
3708 enable_if<__is_forward_iterator<char const*>::value,
3709 string::iterator>::type
3710 string::
3711 insert<char const*>(string::const_iterator, char const*, char const*);
3712
3713extern template
3714 enable_if<__is_forward_iterator<wchar_t const*>::value,
3715 wstring::iterator>::type
3716 wstring::
3717 insert<wchar_t const*>(wstring::const_iterator, wchar_t const*, wchar_t const*);
3718
3719extern template
3720 enable_if<__is_input_iterator<char const*>::value, string&>::type
3721 string::
3722 replace<char const*>(string::iterator, string::iterator, char const*, char const*);
3723
3724extern template
3725 enable_if<__is_input_iterator<wchar_t const*>::value, wstring&>::type
3726 wstring::
3727 replace<wchar_t const*>(wstring::iterator, wstring::iterator, wchar_t const*, wchar_t const*);
3728
3729extern template
3730 enable_if<__is_forward_iterator<wchar_t*>::value, wstring&>::type
3731 wstring::assign<wchar_t*>(wchar_t*, wchar_t*);
3732
3733extern template
3734 string
3735 operator+<char, char_traits<char>, allocator<char> >(char const*, string const&);
3736
3737_LIBCPP_END_NAMESPACE_STD
3738
3739#endif // _LIBCPP_STRING