blob: 92f0ed19f777389c01cef7e84ed3a68bdeaa1409 [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);
Howard Hinnant7b2cb482010-11-17 21:11:40 +0000203 basic_string& replace(const_iterator i1, const_iterator i2, const basic_string& str);
204 basic_string& replace(const_iterator i1, const_iterator i2, const_pointer s, size_type n);
205 basic_string& replace(const_iterator i1, const_iterator i2, const_pointer s);
206 basic_string& replace(const_iterator i1, const_iterator i2, size_type n, value_type c);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000207 template<class InputIterator>
Howard Hinnant7b2cb482010-11-17 21:11:40 +0000208 basic_string& replace(const_iterator i1, const_iterator i2, InputIterator j1, InputIterator j2);
209 basic_string& replace(const_iterator i1, const_iterator i2, initializer_list<value_type>);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000210
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
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001031 _LIBCPP_INLINE_VISIBILITY basic_string();
1032 _LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001033 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 Hinnant2d72b1e2010-12-17 14:46:43 +00001039 _LIBCPP_INLINE_VISIBILITY basic_string(const_pointer __s);
1040 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001041 basic_string(const_pointer __s, const allocator_type& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001042 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001043 basic_string(const_pointer __s, size_type __n);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001044 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001045 basic_string(const_pointer __s, size_type __n, const allocator_type& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001046 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001047 basic_string(size_type __n, value_type __c);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001048 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001049 basic_string(size_type __n, value_type __c, const allocator_type& __a);
1050 basic_string(const basic_string& __str, size_type __pos, size_type __n = npos,
1051 const allocator_type& __a = allocator_type());
1052 template<class _InputIterator>
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001053 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001054 basic_string(_InputIterator __first, _InputIterator __last);
1055 template<class _InputIterator>
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001056 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001057 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001058 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001059 basic_string(initializer_list<value_type> __il);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001060 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001061 basic_string(initializer_list<value_type> __il, const allocator_type& __a);
1062
1063 ~basic_string();
1064
Howard Hinnante32b5e22010-11-17 17:55:08 +00001065 basic_string& operator=(const basic_string& __str);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001066#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001067 _LIBCPP_INLINE_VISIBILITY
Howard Hinnante32b5e22010-11-17 17:55:08 +00001068 basic_string& operator=(basic_string&& __str);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001069#endif
1070 _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const_pointer __s) {return assign(__s);}
1071 basic_string& operator=(value_type __c);
Howard Hinnant8d7a9552010-09-23 17:31:07 +00001072 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001073 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
1074
1075#ifndef _LIBCPP_DEBUG
1076 _LIBCPP_INLINE_VISIBILITY iterator begin() {return iterator(__get_pointer());}
1077 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const {return const_iterator(data());}
1078 _LIBCPP_INLINE_VISIBILITY iterator end() {return iterator(__get_pointer() + size());}
1079 _LIBCPP_INLINE_VISIBILITY const_iterator end() const {return const_iterator(data() + size());}
1080#else // _LIBCPP_DEBUG
1081 _LIBCPP_INLINE_VISIBILITY iterator begin() {return iterator(this, __get_pointer());}
1082 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const {return const_iterator(this, data());}
1083 _LIBCPP_INLINE_VISIBILITY iterator end() {return iterator(this, __get_pointer() + size());}
1084 _LIBCPP_INLINE_VISIBILITY const_iterator end() const {return const_iterator(this, data() + size());}
1085#endif // _LIBCPP_DEBUG
1086 _LIBCPP_INLINE_VISIBILITY reverse_iterator rbegin() {return reverse_iterator(end());}
1087 _LIBCPP_INLINE_VISIBILITY const_reverse_iterator rbegin() const {return const_reverse_iterator(end());}
1088 _LIBCPP_INLINE_VISIBILITY reverse_iterator rend() {return reverse_iterator(begin());}
1089 _LIBCPP_INLINE_VISIBILITY const_reverse_iterator rend() const {return const_reverse_iterator(begin());}
1090
1091 _LIBCPP_INLINE_VISIBILITY const_iterator cbegin() const {return begin();}
1092 _LIBCPP_INLINE_VISIBILITY const_iterator cend() const {return end();}
1093 _LIBCPP_INLINE_VISIBILITY const_reverse_iterator crbegin() const {return rbegin();}
1094 _LIBCPP_INLINE_VISIBILITY const_reverse_iterator crend() const {return rend();}
1095
1096 _LIBCPP_INLINE_VISIBILITY size_type size() const
1097 {return __is_long() ? __get_long_size() : __get_short_size();}
1098 _LIBCPP_INLINE_VISIBILITY size_type length() const {return size();}
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001099 _LIBCPP_INLINE_VISIBILITY size_type max_size() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001100 _LIBCPP_INLINE_VISIBILITY size_type capacity() const
1101 {return (__is_long() ? __get_long_cap() : __min_cap) - 1;}
1102
1103 void resize(size_type __n, value_type __c);
1104 _LIBCPP_INLINE_VISIBILITY void resize(size_type __n) {resize(__n, value_type());}
1105
1106 void reserve(size_type res_arg = 0);
Howard Hinnant8d7a9552010-09-23 17:31:07 +00001107 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001108 void shrink_to_fit() {reserve();}
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001109 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001110 void clear();
1111 _LIBCPP_INLINE_VISIBILITY bool empty() const {return size() == 0;}
1112
1113 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __pos) const;
1114 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __pos);
1115
1116 const_reference at(size_type __n) const;
1117 reference at(size_type __n);
1118
1119 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const basic_string& __str) {return append(__str);}
1120 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const_pointer __s) {return append(__s);}
1121 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(value_type __c) {push_back(__c); return *this;}
1122 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(initializer_list<value_type> __il) {return append(__il);}
1123
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001124 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001125 basic_string& append(const basic_string& __str);
1126 basic_string& append(const basic_string& __str, size_type __pos, size_type __n);
1127 basic_string& append(const_pointer __s, size_type __n);
1128 basic_string& append(const_pointer __s);
1129 basic_string& append(size_type __n, value_type __c);
1130 template<class _InputIterator>
1131 typename enable_if
1132 <
1133 __is_input_iterator <_InputIterator>::value &&
1134 !__is_forward_iterator<_InputIterator>::value,
1135 basic_string&
1136 >::type
1137 append(_InputIterator __first, _InputIterator __last);
1138 template<class _ForwardIterator>
1139 typename enable_if
1140 <
1141 __is_forward_iterator<_ForwardIterator>::value,
1142 basic_string&
1143 >::type
1144 append(_ForwardIterator __first, _ForwardIterator __last);
Howard Hinnant8d7a9552010-09-23 17:31:07 +00001145 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001146 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
1147
1148 void push_back(value_type __c);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001149 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001150 void pop_back();
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001151 _LIBCPP_INLINE_VISIBILITY reference front();
1152 _LIBCPP_INLINE_VISIBILITY const_reference front() const;
1153 _LIBCPP_INLINE_VISIBILITY reference back();
1154 _LIBCPP_INLINE_VISIBILITY const_reference back() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001155
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001156 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001157 basic_string& assign(const basic_string& __str);
1158 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n);
1159 basic_string& assign(const_pointer __s, size_type __n);
1160 basic_string& assign(const_pointer __s);
1161 basic_string& assign(size_type __n, value_type __c);
1162 template<class _InputIterator>
1163 typename enable_if
1164 <
1165 __is_input_iterator <_InputIterator>::value &&
1166 !__is_forward_iterator<_InputIterator>::value,
1167 basic_string&
1168 >::type
1169 assign(_InputIterator __first, _InputIterator __last);
1170 template<class _ForwardIterator>
1171 typename enable_if
1172 <
1173 __is_forward_iterator<_ForwardIterator>::value,
1174 basic_string&
1175 >::type
1176 assign(_ForwardIterator __first, _ForwardIterator __last);
Howard Hinnant8d7a9552010-09-23 17:31:07 +00001177 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001178 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
1179
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001180 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001181 basic_string& insert(size_type __pos1, const basic_string& __str);
1182 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n);
1183 basic_string& insert(size_type __pos, const_pointer __s, size_type __n);
1184 basic_string& insert(size_type __pos, const_pointer __s);
1185 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1186 iterator insert(const_iterator __pos, value_type __c);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001187 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001188 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1189 template<class _InputIterator>
1190 typename enable_if
1191 <
1192 __is_input_iterator <_InputIterator>::value &&
1193 !__is_forward_iterator<_InputIterator>::value,
1194 iterator
1195 >::type
1196 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1197 template<class _ForwardIterator>
1198 typename enable_if
1199 <
1200 __is_forward_iterator<_ForwardIterator>::value,
1201 iterator
1202 >::type
1203 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Howard Hinnant8d7a9552010-09-23 17:31:07 +00001204 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001205 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1206 {return insert(__pos, __il.begin(), __il.end());}
1207
1208 basic_string& erase(size_type __pos = 0, size_type __n = npos);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001209 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001210 iterator erase(const_iterator __pos);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001211 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001212 iterator erase(const_iterator __first, const_iterator __last);
1213
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001214 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001215 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
1216 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2);
1217 basic_string& replace(size_type __pos, size_type __n1, const_pointer __s, size_type __n2);
1218 basic_string& replace(size_type __pos, size_type __n1, const_pointer __s);
1219 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001220 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7b2cb482010-11-17 21:11:40 +00001221 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001222 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7b2cb482010-11-17 21:11:40 +00001223 basic_string& replace(const_iterator __i1, const_iterator __i2, const_pointer __s, size_type __n);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001224 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7b2cb482010-11-17 21:11:40 +00001225 basic_string& replace(const_iterator __i1, const_iterator __i2, const_pointer __s);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001226 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7b2cb482010-11-17 21:11:40 +00001227 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001228 template<class _InputIterator>
1229 typename enable_if
1230 <
1231 __is_input_iterator<_InputIterator>::value,
1232 basic_string&
1233 >::type
Howard Hinnant7b2cb482010-11-17 21:11:40 +00001234 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Howard Hinnant8d7a9552010-09-23 17:31:07 +00001235 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7b2cb482010-11-17 21:11:40 +00001236 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001237 {return replace(__i1, __i2, __il.begin(), __il.end());}
1238
1239 size_type copy(pointer __s, size_type __n, size_type __pos = 0) const;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001240 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001241 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1242
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001243 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001244 void swap(basic_string& __str);
1245
1246 _LIBCPP_INLINE_VISIBILITY const_pointer c_str() const {return data();}
1247 _LIBCPP_INLINE_VISIBILITY const_pointer data() const {return __get_pointer();}
1248
1249 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const {return __alloc();}
1250
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001251 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001252 size_type find(const basic_string& __str, size_type __pos = 0) const;
1253 size_type find(const_pointer __s, size_type __pos, size_type __n) const;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001254 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001255 size_type find(const_pointer __s, size_type __pos = 0) const;
1256 size_type find(value_type __c, size_type __pos = 0) const;
1257
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001258 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001259 size_type rfind(const basic_string& __str, size_type __pos = npos) const;
1260 size_type rfind(const_pointer __s, size_type __pos, size_type __n) const;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001261 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001262 size_type rfind(const_pointer __s, size_type __pos = npos) const;
1263 size_type rfind(value_type __c, size_type __pos = npos) const;
1264
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001265 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001266 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const;
1267 size_type find_first_of(const_pointer __s, size_type __pos, size_type __n) const;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001268 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001269 size_type find_first_of(const_pointer __s, size_type __pos = 0) const;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001270 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001271 size_type find_first_of(value_type __c, size_type __pos = 0) const;
1272
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001273 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001274 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const;
1275 size_type find_last_of(const_pointer __s, size_type __pos, size_type __n) const;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001276 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001277 size_type find_last_of(const_pointer __s, size_type __pos = npos) const;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001278 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001279 size_type find_last_of(value_type __c, size_type __pos = npos) const;
1280
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001281 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001282 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const;
1283 size_type find_first_not_of(const_pointer __s, size_type __pos, size_type __n) const;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001284 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001285 size_type find_first_not_of(const_pointer __s, size_type __pos = 0) const;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001286 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001287 size_type find_first_not_of(value_type __c, size_type __pos = 0) const;
1288
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001289 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001290 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const;
1291 size_type find_last_not_of(const_pointer __s, size_type __pos, size_type __n) const;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001292 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001293 size_type find_last_not_of(const_pointer __s, size_type __pos = npos) const;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001294 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001295 size_type find_last_not_of(value_type __c, size_type __pos = npos) const;
1296
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001297 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001298 int compare(const basic_string& __str) const;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001299 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001300 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
1301 int compare(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2) const;
1302 int compare(const_pointer __s) const;
1303 int compare(size_type __pos1, size_type __n1, const_pointer __s) const;
1304 int compare(size_type __pos1, size_type __n1, const_pointer __s, size_type __n2) const;
1305
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001306 _LIBCPP_INLINE_VISIBILITY bool __invariants() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001307private:
1308 _LIBCPP_INLINE_VISIBILITY allocator_type& __alloc() {return __r_.second();}
1309 _LIBCPP_INLINE_VISIBILITY const allocator_type& __alloc() const {return __r_.second();}
1310
1311 _LIBCPP_INLINE_VISIBILITY bool __is_long() const {return bool(__r_.first().__s.__size_ & __short_mask);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001312
1313 _LIBCPP_INLINE_VISIBILITY void __set_short_size(size_type __s)
1314#if _LIBCPP_BIG_ENDIAN
1315 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1316#else
1317 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
1318#endif
1319 _LIBCPP_INLINE_VISIBILITY size_type __get_short_size() const
1320#if _LIBCPP_BIG_ENDIAN
1321 {return __r_.first().__s.__size_;}
1322#else
1323 {return __r_.first().__s.__size_ >> 1;}
1324#endif
1325 _LIBCPP_INLINE_VISIBILITY void __set_long_size(size_type __s) {__r_.first().__l.__size_ = __s;}
1326 _LIBCPP_INLINE_VISIBILITY size_type __get_long_size() const {return __r_.first().__l.__size_;}
1327 _LIBCPP_INLINE_VISIBILITY void __set_size(size_type __s)
1328 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1329
1330 _LIBCPP_INLINE_VISIBILITY void __set_long_cap(size_type __s) {__r_.first().__l.__cap_ = __long_mask | __s;}
1331 _LIBCPP_INLINE_VISIBILITY size_type __get_long_cap() const {return __r_.first().__l.__cap_ & ~__long_mask;}
1332
1333 _LIBCPP_INLINE_VISIBILITY void __set_long_pointer(pointer __p) {__r_.first().__l.__data_ = __p;}
1334 _LIBCPP_INLINE_VISIBILITY pointer __get_long_pointer() {return __r_.first().__l.__data_;}
1335 _LIBCPP_INLINE_VISIBILITY const_pointer __get_long_pointer() const {return __r_.first().__l.__data_;}
1336 _LIBCPP_INLINE_VISIBILITY pointer __get_short_pointer() {return __r_.first().__s.__data_;}
1337 _LIBCPP_INLINE_VISIBILITY const_pointer __get_short_pointer() const {return __r_.first().__s.__data_;}
1338 _LIBCPP_INLINE_VISIBILITY pointer __get_pointer()
1339 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1340 _LIBCPP_INLINE_VISIBILITY const_pointer __get_pointer() const
1341 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1342
1343 _LIBCPP_INLINE_VISIBILITY void __zero()
1344 {
1345 size_type (&__a)[__n_words] = __r_.first().__r.__words;
1346 for (unsigned __i = 0; __i < __n_words; ++__i)
1347 __a[__i] = 0;
1348 }
1349
1350 template <size_type __a> static
1351 _LIBCPP_INLINE_VISIBILITY size_type __align(size_type __s) {return __s + (__a-1) & ~(__a-1);}
1352 enum {__alignment = 16};
1353 static _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __s)
1354 {return (__s < __min_cap ? __min_cap :
1355 __align<sizeof(value_type) < __alignment ? __alignment/sizeof(value_type) : 1>(__s+1)) - 1;}
1356
1357 void __init(const_pointer __s, size_type __sz, size_type __reserve);
1358 void __init(const_pointer __s, size_type __sz);
1359 void __init(size_type __n, value_type __c);
Howard Hinnant324bb032010-08-22 00:02:43 +00001360
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001361 template <class _InputIterator>
1362 typename enable_if
1363 <
1364 __is_input_iterator <_InputIterator>::value &&
1365 !__is_forward_iterator<_InputIterator>::value,
1366 void
1367 >::type
1368 __init(_InputIterator __first, _InputIterator __last);
1369
1370 template <class _ForwardIterator>
1371 typename enable_if
1372 <
1373 __is_forward_iterator<_ForwardIterator>::value,
1374 void
1375 >::type
1376 __init(_ForwardIterator __first, _ForwardIterator __last);
1377
1378 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant324bb032010-08-22 00:02:43 +00001379 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001380 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1381 size_type __n_copy, size_type __n_del,
1382 size_type __n_add, const_pointer __p_new_stuff);
1383
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001384 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001385 void __erase_to_end(size_type __pos);
1386
Howard Hinnante32b5e22010-11-17 17:55:08 +00001387 _LIBCPP_INLINE_VISIBILITY
1388 void __copy_assign_alloc(const basic_string& __str)
1389 {__copy_assign_alloc(__str, integral_constant<bool,
1390 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1391
1392 _LIBCPP_INLINE_VISIBILITY
1393 void __copy_assign_alloc(const basic_string& __str, true_type)
1394 {
1395 if (__alloc() != __str.__alloc())
1396 {
1397 clear();
1398 shrink_to_fit();
1399 }
1400 __alloc() = __str.__alloc();
1401 }
1402
1403 _LIBCPP_INLINE_VISIBILITY
1404 void __copy_assign_alloc(const basic_string& __str, false_type)
1405 {}
1406
1407#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001408 _LIBCPP_INLINE_VISIBILITY
Howard Hinnante32b5e22010-11-17 17:55:08 +00001409 void __move_assign(basic_string& __str, false_type);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001410 _LIBCPP_INLINE_VISIBILITY
Howard Hinnante32b5e22010-11-17 17:55:08 +00001411 void __move_assign(basic_string& __str, true_type);
1412#endif
1413
1414 _LIBCPP_INLINE_VISIBILITY
1415 static void __swap_alloc(allocator_type& __x, allocator_type& __y)
1416 {__swap_alloc(__x, __y, integral_constant<bool,
1417 __alloc_traits::propagate_on_container_swap::value>());}
1418
1419 _LIBCPP_INLINE_VISIBILITY
1420 static void __swap_alloc(allocator_type& __x, allocator_type& __y, true_type)
1421 {
1422 using _STD::swap;
1423 swap(__x, __y);
1424 }
1425 _LIBCPP_INLINE_VISIBILITY
1426 static void __swap_alloc(allocator_type& __x, allocator_type& __y, false_type)
1427 {}
1428
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001429 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
1430 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(size_type);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001431
1432 friend basic_string operator+<>(const basic_string&, const basic_string&);
1433 friend basic_string operator+<>(const value_type*, const basic_string&);
1434 friend basic_string operator+<>(value_type, const basic_string&);
1435 friend basic_string operator+<>(const basic_string&, const value_type*);
1436 friend basic_string operator+<>(const basic_string&, value_type);
1437};
1438
1439template <class _CharT, class _Traits, class _Allocator>
1440#ifndef _LIBCPP_DEBUG
1441_LIBCPP_INLINE_VISIBILITY inline
1442#endif
1443void
1444basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
1445{
1446#ifdef _LIBCPP_DEBUG
1447 iterator::__remove_all(this);
1448 const_iterator::__remove_all(this);
Howard Hinnant324bb032010-08-22 00:02:43 +00001449#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001450}
1451
1452template <class _CharT, class _Traits, class _Allocator>
1453#ifndef _LIBCPP_DEBUG
1454_LIBCPP_INLINE_VISIBILITY inline
1455#endif
1456void
1457basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type __pos)
1458{
1459#ifdef _LIBCPP_DEBUG
1460 const_iterator __beg = begin();
1461 if (__iterator_list_.first)
1462 {
1463 for (iterator* __p = __iterator_list_.first; __p;)
1464 {
1465 if (*__p - __beg > static_cast<difference_type>(__pos))
1466 {
1467 iterator* __n = __p;
1468 __p = __p->__next;
1469 __n->__remove_owner();
1470 }
1471 else
1472 __p = __p->__next;
1473 }
1474 }
1475 if (__iterator_list_.second)
1476 {
1477 for (const_iterator* __p = __iterator_list_.second; __p;)
1478 {
1479 if (*__p - __beg > static_cast<difference_type>(__pos))
1480 {
1481 const_iterator* __n = __p;
1482 __p = __p->__next;
1483 __n->__remove_owner();
1484 }
1485 else
1486 __p = __p->__next;
1487 }
1488 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001489#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001490}
1491
1492template <class _CharT, class _Traits, class _Allocator>
1493_LIBCPP_INLINE_VISIBILITY inline
1494basic_string<_CharT, _Traits, _Allocator>::basic_string()
1495{
1496 __zero();
1497}
1498
1499template <class _CharT, class _Traits, class _Allocator>
1500_LIBCPP_INLINE_VISIBILITY inline
1501basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
1502 : __r_(__a)
1503{
1504 __zero();
1505}
1506
1507template <class _CharT, class _Traits, class _Allocator>
1508void
1509basic_string<_CharT, _Traits, _Allocator>::__init(const_pointer __s, size_type __sz, size_type __reserve)
1510{
1511 if (__reserve > max_size())
1512 this->__throw_length_error();
1513 pointer __p;
1514 if (__reserve < __min_cap)
1515 {
1516 __set_short_size(__sz);
1517 __p = __get_short_pointer();
1518 }
1519 else
1520 {
1521 size_type __cap = __recommend(__reserve);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001522 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001523 __set_long_pointer(__p);
1524 __set_long_cap(__cap+1);
1525 __set_long_size(__sz);
1526 }
1527 traits_type::copy(__p, __s, __sz);
1528 traits_type::assign(__p[__sz], value_type());
1529}
1530
1531template <class _CharT, class _Traits, class _Allocator>
1532void
1533basic_string<_CharT, _Traits, _Allocator>::__init(const_pointer __s, size_type __sz)
1534{
1535 if (__sz > max_size())
1536 this->__throw_length_error();
1537 pointer __p;
1538 if (__sz < __min_cap)
1539 {
1540 __set_short_size(__sz);
1541 __p = __get_short_pointer();
1542 }
1543 else
1544 {
1545 size_type __cap = __recommend(__sz);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001546 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001547 __set_long_pointer(__p);
1548 __set_long_cap(__cap+1);
1549 __set_long_size(__sz);
1550 }
1551 traits_type::copy(__p, __s, __sz);
1552 traits_type::assign(__p[__sz], value_type());
1553}
1554
1555template <class _CharT, class _Traits, class _Allocator>
1556_LIBCPP_INLINE_VISIBILITY inline
1557basic_string<_CharT, _Traits, _Allocator>::basic_string(const_pointer __s)
1558{
1559#ifdef _LIBCPP_DEBUG
1560 assert(__s != 0);
1561#endif
1562 __init(__s, traits_type::length(__s));
1563}
1564
1565template <class _CharT, class _Traits, class _Allocator>
1566_LIBCPP_INLINE_VISIBILITY inline
1567basic_string<_CharT, _Traits, _Allocator>::basic_string(const_pointer __s, const allocator_type& __a)
1568 : __r_(__a)
1569{
1570#ifdef _LIBCPP_DEBUG
1571 assert(__s != 0);
1572#endif
1573 __init(__s, traits_type::length(__s));
1574}
1575
1576template <class _CharT, class _Traits, class _Allocator>
1577_LIBCPP_INLINE_VISIBILITY inline
1578basic_string<_CharT, _Traits, _Allocator>::basic_string(const_pointer __s, size_type __n)
1579{
1580#ifdef _LIBCPP_DEBUG
1581 assert(__s != 0);
1582#endif
1583 __init(__s, __n);
1584}
1585
1586template <class _CharT, class _Traits, class _Allocator>
1587_LIBCPP_INLINE_VISIBILITY inline
1588basic_string<_CharT, _Traits, _Allocator>::basic_string(const_pointer __s, size_type __n, const allocator_type& __a)
1589 : __r_(__a)
1590{
1591#ifdef _LIBCPP_DEBUG
1592 assert(__s != 0);
1593#endif
1594 __init(__s, __n);
1595}
1596
1597template <class _CharT, class _Traits, class _Allocator>
1598basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Howard Hinnante32b5e22010-11-17 17:55:08 +00001599 : __r_(__alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001600{
1601 if (!__str.__is_long())
1602 __r_.first().__r = __str.__r_.first().__r;
1603 else
1604 __init(__str.__get_long_pointer(), __str.__get_long_size());
1605}
1606
1607template <class _CharT, class _Traits, class _Allocator>
1608basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, const allocator_type& __a)
1609 : __r_(__a)
1610{
1611 if (!__str.__is_long())
1612 __r_.first().__r = __str.__r_.first().__r;
1613 else
1614 __init(__str.__get_long_pointer(), __str.__get_long_size());
1615}
1616
Howard Hinnant73d21a42010-09-04 23:28:19 +00001617#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001618
1619template <class _CharT, class _Traits, class _Allocator>
1620_LIBCPP_INLINE_VISIBILITY inline
1621basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
1622 : __r_(_STD::move(__str.__r_))
1623{
1624 __str.__zero();
1625#ifdef _LIBCPP_DEBUG
1626 __str.__invalidate_all_iterators();
1627#endif
1628}
1629
1630template <class _CharT, class _Traits, class _Allocator>
1631_LIBCPP_INLINE_VISIBILITY inline
1632basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Howard Hinnante32b5e22010-11-17 17:55:08 +00001633 : __r_(__a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001634{
Howard Hinnante32b5e22010-11-17 17:55:08 +00001635 if (__a == __str.__alloc() || !__str.__is_long())
1636 __r_.first().__r = __str.__r_.first().__r;
1637 else
1638 __init(__str.__get_long_pointer(), __str.__get_long_size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001639 __str.__zero();
1640#ifdef _LIBCPP_DEBUG
1641 __str.__invalidate_all_iterators();
1642#endif
1643}
1644
Howard Hinnant73d21a42010-09-04 23:28:19 +00001645#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001646
1647template <class _CharT, class _Traits, class _Allocator>
1648void
1649basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
1650{
1651 if (__n > max_size())
1652 this->__throw_length_error();
1653 pointer __p;
1654 if (__n < __min_cap)
1655 {
1656 __set_short_size(__n);
1657 __p = __get_short_pointer();
1658 }
1659 else
1660 {
1661 size_type __cap = __recommend(__n);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001662 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001663 __set_long_pointer(__p);
1664 __set_long_cap(__cap+1);
1665 __set_long_size(__n);
1666 }
1667 traits_type::assign(__p, __n, __c);
1668 traits_type::assign(__p[__n], value_type());
1669}
1670
1671template <class _CharT, class _Traits, class _Allocator>
1672_LIBCPP_INLINE_VISIBILITY inline
1673basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, value_type __c)
1674{
1675 __init(__n, __c);
1676}
1677
1678template <class _CharT, class _Traits, class _Allocator>
1679_LIBCPP_INLINE_VISIBILITY inline
1680basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, value_type __c, const allocator_type& __a)
1681 : __r_(__a)
1682{
1683 __init(__n, __c);
1684}
1685
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001686template <class _CharT, class _Traits, class _Allocator>
1687basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos, size_type __n,
1688 const allocator_type& __a)
1689 : __r_(__a)
1690{
1691 size_type __str_sz = __str.size();
1692 if (__pos > __str_sz)
1693 this->__throw_out_of_range();
1694 __init(__str.data() + __pos, _STD::min(__n, __str_sz - __pos));
1695}
1696
1697template <class _CharT, class _Traits, class _Allocator>
1698template <class _InputIterator>
1699typename enable_if
1700<
1701 __is_input_iterator <_InputIterator>::value &&
1702 !__is_forward_iterator<_InputIterator>::value,
1703 void
1704>::type
1705basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
1706{
1707 __zero();
1708#ifndef _LIBCPP_NO_EXCEPTIONS
1709 try
1710 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001711#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001712 for (; __first != __last; ++__first)
1713 push_back(*__first);
1714#ifndef _LIBCPP_NO_EXCEPTIONS
1715 }
1716 catch (...)
1717 {
1718 if (__is_long())
Howard Hinnante32b5e22010-11-17 17:55:08 +00001719 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001720 throw;
1721 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001722#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001723}
1724
1725template <class _CharT, class _Traits, class _Allocator>
1726template <class _ForwardIterator>
1727typename enable_if
1728<
1729 __is_forward_iterator<_ForwardIterator>::value,
1730 void
1731>::type
1732basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
1733{
1734 size_type __sz = static_cast<size_type>(_STD::distance(__first, __last));
1735 if (__sz > max_size())
1736 this->__throw_length_error();
1737 pointer __p;
1738 if (__sz < __min_cap)
1739 {
1740 __set_short_size(__sz);
1741 __p = __get_short_pointer();
1742 }
1743 else
1744 {
1745 size_type __cap = __recommend(__sz);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001746 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001747 __set_long_pointer(__p);
1748 __set_long_cap(__cap+1);
1749 __set_long_size(__sz);
1750 }
1751 for (; __first != __last; ++__first, ++__p)
1752 traits_type::assign(*__p, *__first);
1753 traits_type::assign(*__p, value_type());
1754}
1755
1756template <class _CharT, class _Traits, class _Allocator>
1757template<class _InputIterator>
1758_LIBCPP_INLINE_VISIBILITY inline
1759basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
1760{
1761 __init(__first, __last);
1762}
1763
1764template <class _CharT, class _Traits, class _Allocator>
1765template<class _InputIterator>
1766_LIBCPP_INLINE_VISIBILITY inline
1767basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
1768 const allocator_type& __a)
1769 : __r_(__a)
1770{
1771 __init(__first, __last);
1772}
1773
1774template <class _CharT, class _Traits, class _Allocator>
1775_LIBCPP_INLINE_VISIBILITY inline
1776basic_string<_CharT, _Traits, _Allocator>::basic_string(initializer_list<value_type> __il)
1777{
1778 __init(__il.begin(), __il.end());
1779}
1780
1781template <class _CharT, class _Traits, class _Allocator>
1782_LIBCPP_INLINE_VISIBILITY inline
1783basic_string<_CharT, _Traits, _Allocator>::basic_string(initializer_list<value_type> __il, const allocator_type& __a)
1784 : __r_(__a)
1785{
1786 __init(__il.begin(), __il.end());
1787}
1788
1789template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001790basic_string<_CharT, _Traits, _Allocator>::~basic_string()
1791{
1792 __invalidate_all_iterators();
1793 if (__is_long())
Howard Hinnante32b5e22010-11-17 17:55:08 +00001794 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001795}
1796
1797template <class _CharT, class _Traits, class _Allocator>
1798void
1799basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
1800 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1801 size_type __n_copy, size_type __n_del, size_type __n_add, const_pointer __p_new_stuff)
1802{
1803 size_type __ms = max_size();
1804 if (__delta_cap > __ms - __old_cap - 1)
1805 this->__throw_length_error();
1806 pointer __old_p = __get_pointer();
1807 size_type __cap = __old_cap < __ms / 2 - __alignment ?
1808 __recommend(_STD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
1809 __ms - 1;
Howard Hinnante32b5e22010-11-17 17:55:08 +00001810 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001811 __invalidate_all_iterators();
1812 if (__n_copy != 0)
1813 traits_type::copy(__p, __old_p, __n_copy);
1814 if (__n_add != 0)
1815 traits_type::copy(__p + __n_copy, __p_new_stuff, __n_add);
1816 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
1817 if (__sec_cp_sz != 0)
1818 traits_type::copy(__p + __n_copy + __n_add, __old_p + __n_copy + __n_del, __sec_cp_sz);
1819 if (__old_cap+1 != __min_cap)
Howard Hinnante32b5e22010-11-17 17:55:08 +00001820 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001821 __set_long_pointer(__p);
1822 __set_long_cap(__cap+1);
1823 __old_sz = __n_copy + __n_add + __sec_cp_sz;
1824 __set_long_size(__old_sz);
1825 traits_type::assign(__p[__old_sz], value_type());
1826}
1827
1828template <class _CharT, class _Traits, class _Allocator>
1829void
1830basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1831 size_type __n_copy, size_type __n_del, size_type __n_add)
1832{
1833 size_type __ms = max_size();
1834 if (__delta_cap > __ms - __old_cap - 1)
1835 this->__throw_length_error();
1836 pointer __old_p = __get_pointer();
1837 size_type __cap = __old_cap < __ms / 2 - __alignment ?
1838 __recommend(_STD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
1839 __ms - 1;
Howard Hinnante32b5e22010-11-17 17:55:08 +00001840 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001841 __invalidate_all_iterators();
1842 if (__n_copy != 0)
1843 traits_type::copy(__p, __old_p, __n_copy);
1844 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
1845 if (__sec_cp_sz != 0)
1846 traits_type::copy(__p + __n_copy + __n_add, __old_p + __n_copy + __n_del, __sec_cp_sz);
1847 if (__old_cap+1 != __min_cap)
Howard Hinnante32b5e22010-11-17 17:55:08 +00001848 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001849 __set_long_pointer(__p);
1850 __set_long_cap(__cap+1);
1851}
1852
1853// assign
1854
1855template <class _CharT, class _Traits, class _Allocator>
1856basic_string<_CharT, _Traits, _Allocator>&
1857basic_string<_CharT, _Traits, _Allocator>::assign(const_pointer __s, size_type __n)
1858{
1859#ifdef _LIBCPP_DEBUG
1860 assert(__s != 0);
1861#endif
1862 size_type __cap = capacity();
1863 if (__cap >= __n)
1864 {
1865 pointer __p = __get_pointer();
1866 traits_type::move(__p, __s, __n);
1867 traits_type::assign(__p[__n], value_type());
1868 __set_size(__n);
1869 __invalidate_iterators_past(__n);
1870 }
1871 else
1872 {
1873 size_type __sz = size();
1874 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
1875 }
1876 return *this;
1877}
1878
1879template <class _CharT, class _Traits, class _Allocator>
1880basic_string<_CharT, _Traits, _Allocator>&
1881basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
1882{
1883 size_type __cap = capacity();
1884 if (__cap < __n)
1885 {
1886 size_type __sz = size();
1887 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
1888 }
1889 else
1890 __invalidate_iterators_past(__n);
1891 pointer __p = __get_pointer();
1892 traits_type::assign(__p, __n, __c);
1893 traits_type::assign(__p[__n], value_type());
1894 __set_size(__n);
1895 return *this;
1896}
1897
1898template <class _CharT, class _Traits, class _Allocator>
1899basic_string<_CharT, _Traits, _Allocator>&
1900basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
1901{
1902 pointer __p;
1903 if (__is_long())
1904 {
1905 __p = __get_long_pointer();
1906 __set_long_size(1);
1907 }
1908 else
1909 {
1910 __p = __get_short_pointer();
1911 __set_short_size(1);
1912 }
1913 traits_type::assign(*__p, __c);
1914 traits_type::assign(*++__p, value_type());
1915 __invalidate_iterators_past(1);
1916 return *this;
1917}
1918
1919template <class _CharT, class _Traits, class _Allocator>
Howard Hinnante32b5e22010-11-17 17:55:08 +00001920basic_string<_CharT, _Traits, _Allocator>&
1921basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
1922{
1923 if (this != &__str)
1924 {
1925 __copy_assign_alloc(__str);
1926 assign(__str);
1927 }
1928 return *this;
1929}
1930
1931#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1932
1933template <class _CharT, class _Traits, class _Allocator>
1934_LIBCPP_INLINE_VISIBILITY inline
1935void
1936basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
1937{
1938 if (__alloc() != __str.__alloc())
1939 assign(__str);
1940 else
1941 __move_assign(__str, true_type());
1942}
1943
1944template <class _CharT, class _Traits, class _Allocator>
1945_LIBCPP_INLINE_VISIBILITY inline
1946void
1947basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
1948{
1949 clear();
1950 shrink_to_fit();
1951 __r_ = _STD::move(__str.__r_);
1952 __str.__zero();
1953}
1954
1955template <class _CharT, class _Traits, class _Allocator>
1956_LIBCPP_INLINE_VISIBILITY inline
1957basic_string<_CharT, _Traits, _Allocator>&
1958basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
1959{
1960 __move_assign(__str, integral_constant<bool,
1961 __alloc_traits::propagate_on_container_move_assignment::value>());
1962 return *this;
1963}
1964
1965#endif
1966
1967template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001968template<class _InputIterator>
1969typename enable_if
1970<
1971 __is_input_iterator <_InputIterator>::value &&
1972 !__is_forward_iterator<_InputIterator>::value,
1973 basic_string<_CharT, _Traits, _Allocator>&
1974>::type
1975basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
1976{
1977 clear();
1978 for (; __first != __last; ++__first)
1979 push_back(*__first);
1980}
1981
1982template <class _CharT, class _Traits, class _Allocator>
1983template<class _ForwardIterator>
1984typename enable_if
1985<
1986 __is_forward_iterator<_ForwardIterator>::value,
1987 basic_string<_CharT, _Traits, _Allocator>&
1988>::type
1989basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
1990{
1991 size_type __n = static_cast<size_type>(_STD::distance(__first, __last));
1992 size_type __cap = capacity();
1993 if (__cap < __n)
1994 {
1995 size_type __sz = size();
1996 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
1997 }
1998 else
1999 __invalidate_iterators_past(__n);
2000 pointer __p = __get_pointer();
2001 for (; __first != __last; ++__first, ++__p)
2002 traits_type::assign(*__p, *__first);
2003 traits_type::assign(*__p, value_type());
2004 __set_size(__n);
2005 return *this;
2006}
2007
2008template <class _CharT, class _Traits, class _Allocator>
2009_LIBCPP_INLINE_VISIBILITY inline
2010basic_string<_CharT, _Traits, _Allocator>&
2011basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str)
2012{
2013 return assign(__str.data(), __str.size());
2014}
2015
2016template <class _CharT, class _Traits, class _Allocator>
2017basic_string<_CharT, _Traits, _Allocator>&
2018basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2019{
2020 size_type __sz = __str.size();
2021 if (__pos > __sz)
2022 this->__throw_out_of_range();
2023 return assign(__str.data() + __pos, _STD::min(__n, __sz - __pos));
2024}
2025
2026template <class _CharT, class _Traits, class _Allocator>
2027basic_string<_CharT, _Traits, _Allocator>&
2028basic_string<_CharT, _Traits, _Allocator>::assign(const_pointer __s)
2029{
2030#ifdef _LIBCPP_DEBUG
2031 assert(__s != 0);
2032#endif
2033 return assign(__s, traits_type::length(__s));
2034}
2035
2036// append
2037
2038template <class _CharT, class _Traits, class _Allocator>
2039basic_string<_CharT, _Traits, _Allocator>&
2040basic_string<_CharT, _Traits, _Allocator>::append(const_pointer __s, size_type __n)
2041{
2042#ifdef _LIBCPP_DEBUG
2043 assert(__s != 0);
2044#endif
2045 size_type __cap = capacity();
2046 size_type __sz = size();
2047 if (__cap - __sz >= __n)
2048 {
2049 if (__n)
2050 {
2051 pointer __p = __get_pointer();
2052 traits_type::copy(__p + __sz, __s, __n);
2053 __sz += __n;
2054 __set_size(__sz);
2055 traits_type::assign(__p[__sz], value_type());
2056 }
2057 }
2058 else
2059 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2060 return *this;
2061}
2062
2063template <class _CharT, class _Traits, class _Allocator>
2064basic_string<_CharT, _Traits, _Allocator>&
2065basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2066{
2067 if (__n)
2068 {
2069 size_type __cap = capacity();
2070 size_type __sz = size();
2071 if (__cap - __sz < __n)
2072 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2073 pointer __p = __get_pointer();
2074 traits_type::assign(__p + __sz, __n, __c);
2075 __sz += __n;
2076 __set_size(__sz);
2077 traits_type::assign(__p[__sz], value_type());
2078 }
2079 return *this;
2080}
2081
2082template <class _CharT, class _Traits, class _Allocator>
2083void
2084basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2085{
2086 size_type __cap = capacity();
2087 size_type __sz = size();
2088 if (__sz == __cap)
2089 __grow_by(__cap, 1, __sz, __sz, 0);
2090 pointer __p = __get_pointer() + __sz;
2091 traits_type::assign(*__p, __c);
2092 traits_type::assign(*++__p, value_type());
2093 __set_size(__sz+1);
2094}
2095
2096template <class _CharT, class _Traits, class _Allocator>
2097template<class _InputIterator>
2098typename enable_if
2099<
2100 __is_input_iterator <_InputIterator>::value &&
2101 !__is_forward_iterator<_InputIterator>::value,
2102 basic_string<_CharT, _Traits, _Allocator>&
2103>::type
2104basic_string<_CharT, _Traits, _Allocator>::append(_InputIterator __first, _InputIterator __last)
2105{
2106 for (; __first != __last; ++__first)
2107 push_back(*__first);
2108 return *this;
2109}
2110
2111template <class _CharT, class _Traits, class _Allocator>
2112template<class _ForwardIterator>
2113typename enable_if
2114<
2115 __is_forward_iterator<_ForwardIterator>::value,
2116 basic_string<_CharT, _Traits, _Allocator>&
2117>::type
2118basic_string<_CharT, _Traits, _Allocator>::append(_ForwardIterator __first, _ForwardIterator __last)
2119{
2120 size_type __sz = size();
2121 size_type __cap = capacity();
2122 size_type __n = static_cast<size_type>(_STD::distance(__first, __last));
2123 if (__n)
2124 {
2125 if (__cap - __sz < __n)
2126 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2127 pointer __p = __get_pointer() + __sz;
2128 for (; __first != __last; ++__p, ++__first)
2129 traits_type::assign(*__p, *__first);
2130 traits_type::assign(*__p, value_type());
2131 __set_size(__sz + __n);
2132 }
2133 return *this;
2134}
2135
2136template <class _CharT, class _Traits, class _Allocator>
2137_LIBCPP_INLINE_VISIBILITY inline
2138basic_string<_CharT, _Traits, _Allocator>&
2139basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2140{
2141 return append(__str.data(), __str.size());
2142}
2143
2144template <class _CharT, class _Traits, class _Allocator>
2145basic_string<_CharT, _Traits, _Allocator>&
2146basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2147{
2148 size_type __sz = __str.size();
2149 if (__pos > __sz)
2150 this->__throw_out_of_range();
2151 return append(__str.data() + __pos, _STD::min(__n, __sz - __pos));
2152}
2153
2154template <class _CharT, class _Traits, class _Allocator>
2155basic_string<_CharT, _Traits, _Allocator>&
2156basic_string<_CharT, _Traits, _Allocator>::append(const_pointer __s)
2157{
2158#ifdef _LIBCPP_DEBUG
2159 assert(__s != 0);
2160#endif
2161 return append(__s, traits_type::length(__s));
2162}
2163
2164// insert
2165
2166template <class _CharT, class _Traits, class _Allocator>
2167basic_string<_CharT, _Traits, _Allocator>&
2168basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const_pointer __s, size_type __n)
2169{
2170#ifdef _LIBCPP_DEBUG
2171 assert(__s != 0);
2172#endif
2173 size_type __sz = size();
2174 if (__pos > __sz)
2175 this->__throw_out_of_range();
2176 size_type __cap = capacity();
2177 if (__cap - __sz >= __n)
2178 {
2179 if (__n)
2180 {
2181 pointer __p = __get_pointer();
2182 size_type __n_move = __sz - __pos;
2183 if (__n_move != 0)
2184 {
2185 if (__p + __pos <= __s && __s < __p + __sz)
2186 __s += __n;
2187 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2188 }
2189 traits_type::move(__p + __pos, __s, __n);
2190 __sz += __n;
2191 __set_size(__sz);
2192 traits_type::assign(__p[__sz], value_type());
2193 }
2194 }
2195 else
2196 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2197 return *this;
2198}
2199
2200template <class _CharT, class _Traits, class _Allocator>
2201basic_string<_CharT, _Traits, _Allocator>&
2202basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2203{
2204 size_type __sz = size();
2205 if (__pos > __sz)
2206 this->__throw_out_of_range();
2207 if (__n)
2208 {
2209 size_type __cap = capacity();
2210 pointer __p;
2211 if (__cap - __sz >= __n)
2212 {
2213 __p = __get_pointer();
2214 size_type __n_move = __sz - __pos;
2215 if (__n_move != 0)
2216 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2217 }
2218 else
2219 {
2220 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
2221 __p = __get_long_pointer();
2222 }
2223 traits_type::assign(__p + __pos, __n, __c);
2224 __sz += __n;
2225 __set_size(__sz);
2226 traits_type::assign(__p[__sz], value_type());
2227 }
2228 return *this;
2229}
2230
2231template <class _CharT, class _Traits, class _Allocator>
2232template<class _InputIterator>
2233typename enable_if
2234<
2235 __is_input_iterator <_InputIterator>::value &&
2236 !__is_forward_iterator<_InputIterator>::value,
2237 typename basic_string<_CharT, _Traits, _Allocator>::iterator
2238>::type
2239basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2240{
2241 size_type __old_sz = size();
2242 difference_type __ip = __pos - begin();
2243 for (; __first != __last; ++__first)
2244 push_back(*__first);
2245 pointer __p = __get_pointer();
2246 _STD::rotate(__p + __ip, __p + __old_sz, __p + size());
2247 return iterator(__p + __ip);
2248}
2249
2250template <class _CharT, class _Traits, class _Allocator>
2251template<class _ForwardIterator>
2252typename enable_if
2253<
2254 __is_forward_iterator<_ForwardIterator>::value,
2255 typename basic_string<_CharT, _Traits, _Allocator>::iterator
2256>::type
2257basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2258{
2259 size_type __ip = static_cast<size_type>(__pos - begin());
2260 size_type __sz = size();
2261 size_type __cap = capacity();
2262 size_type __n = static_cast<size_type>(_STD::distance(__first, __last));
2263 if (__n)
2264 {
2265 pointer __p;
2266 if (__cap - __sz >= __n)
2267 {
2268 __p = __get_pointer();
2269 size_type __n_move = __sz - __ip;
2270 if (__n_move != 0)
2271 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
2272 }
2273 else
2274 {
2275 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
2276 __p = __get_long_pointer();
2277 }
2278 __sz += __n;
2279 __set_size(__sz);
2280 traits_type::assign(__p[__sz], value_type());
2281 for (__p += __ip; __first != __last; ++__p, ++__first)
2282 traits_type::assign(*__p, *__first);
2283 }
2284 return begin() + __ip;
2285}
2286
2287template <class _CharT, class _Traits, class _Allocator>
2288_LIBCPP_INLINE_VISIBILITY inline
2289basic_string<_CharT, _Traits, _Allocator>&
2290basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2291{
2292 return insert(__pos1, __str.data(), __str.size());
2293}
2294
2295template <class _CharT, class _Traits, class _Allocator>
2296basic_string<_CharT, _Traits, _Allocator>&
2297basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2298 size_type __pos2, size_type __n)
2299{
2300 size_type __str_sz = __str.size();
2301 if (__pos2 > __str_sz)
2302 this->__throw_out_of_range();
2303 return insert(__pos1, __str.data() + __pos2, _STD::min(__n, __str_sz - __pos2));
2304}
2305
2306template <class _CharT, class _Traits, class _Allocator>
2307basic_string<_CharT, _Traits, _Allocator>&
2308basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const_pointer __s)
2309{
2310#ifdef _LIBCPP_DEBUG
2311 assert(__s != 0);
2312#endif
2313 return insert(__pos, __s, traits_type::length(__s));
2314}
2315
2316template <class _CharT, class _Traits, class _Allocator>
2317typename basic_string<_CharT, _Traits, _Allocator>::iterator
2318basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2319{
2320 size_type __ip = static_cast<size_type>(__pos - begin());
2321 size_type __sz = size();
2322 size_type __cap = capacity();
2323 pointer __p;
2324 if (__cap == __sz)
2325 {
2326 __grow_by(__cap, 1, __sz, __ip, 0, 1);
2327 __p = __get_long_pointer();
2328 }
2329 else
2330 {
2331 __p = __get_pointer();
2332 size_type __n_move = __sz - __ip;
2333 if (__n_move != 0)
2334 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2335 }
2336 traits_type::assign(__p[__ip], __c);
2337 traits_type::assign(__p[++__sz], value_type());
2338 __set_size(__sz);
2339 return begin() + static_cast<difference_type>(__ip);
2340}
2341
2342template <class _CharT, class _Traits, class _Allocator>
2343_LIBCPP_INLINE_VISIBILITY inline
2344typename basic_string<_CharT, _Traits, _Allocator>::iterator
2345basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2346{
2347 difference_type __p = __pos - begin();
2348 insert(static_cast<size_type>(__p), __n, __c);
2349 return begin() + __p;
2350}
2351
2352// replace
2353
2354template <class _CharT, class _Traits, class _Allocator>
2355basic_string<_CharT, _Traits, _Allocator>&
2356basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const_pointer __s, size_type __n2)
2357{
2358#ifdef _LIBCPP_DEBUG
2359 assert(__s != 0);
2360#endif
2361 size_type __sz = size();
2362 if (__pos > __sz)
2363 this->__throw_out_of_range();
2364 __n1 = _STD::min(__n1, __sz - __pos);
2365 size_type __cap = capacity();
2366 if (__cap - __sz + __n1 >= __n2)
2367 {
2368 pointer __p = __get_pointer();
2369 if (__n1 != __n2)
2370 {
2371 size_type __n_move = __sz - __pos - __n1;
2372 if (__n_move != 0)
2373 {
2374 if (__n1 > __n2)
2375 {
2376 traits_type::move(__p + __pos, __s, __n2);
2377 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2378 goto __finish;
2379 }
2380 if (__p + __pos < __s && __s < __p + __sz)
2381 {
2382 if (__p + __pos + __n1 <= __s)
2383 __s += __n2 - __n1;
2384 else // __p + __pos < __s < __p + __pos + __n1
2385 {
2386 traits_type::move(__p + __pos, __s, __n1);
2387 __pos += __n1;
2388 __s += __n2;
2389 __n2 -= __n1;
2390 __n1 = 0;
2391 }
2392 }
2393 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2394 }
2395 }
2396 traits_type::move(__p + __pos, __s, __n2);
2397__finish:
2398 __sz += __n2 - __n1;
2399 __set_size(__sz);
2400 __invalidate_iterators_past(__sz);
2401 traits_type::assign(__p[__sz], value_type());
2402 }
2403 else
2404 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
2405 return *this;
2406}
2407
2408template <class _CharT, class _Traits, class _Allocator>
2409basic_string<_CharT, _Traits, _Allocator>&
2410basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
2411{
2412 size_type __sz = size();
2413 if (__pos > __sz)
2414 this->__throw_out_of_range();
2415 __n1 = _STD::min(__n1, __sz - __pos);
2416 size_type __cap = capacity();
2417 pointer __p;
2418 if (__cap - __sz + __n1 >= __n2)
2419 {
2420 __p = __get_pointer();
2421 if (__n1 != __n2)
2422 {
2423 size_type __n_move = __sz - __pos - __n1;
2424 if (__n_move != 0)
2425 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2426 }
2427 }
2428 else
2429 {
2430 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
2431 __p = __get_long_pointer();
2432 }
2433 traits_type::assign(__p + __pos, __n2, __c);
2434 __sz += __n2 - __n1;
2435 __set_size(__sz);
2436 __invalidate_iterators_past(__sz);
2437 traits_type::assign(__p[__sz], value_type());
2438 return *this;
2439}
2440
2441template <class _CharT, class _Traits, class _Allocator>
2442template<class _InputIterator>
2443typename enable_if
2444<
2445 __is_input_iterator<_InputIterator>::value,
2446 basic_string<_CharT, _Traits, _Allocator>&
2447>::type
Howard Hinnant7b2cb482010-11-17 21:11:40 +00002448basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002449 _InputIterator __j1, _InputIterator __j2)
2450{
2451 for (; true; ++__i1, ++__j1)
2452 {
2453 if (__i1 == __i2)
2454 {
2455 if (__j1 != __j2)
2456 insert(__i1, __j1, __j2);
2457 break;
2458 }
2459 if (__j1 == __j2)
2460 {
2461 erase(__i1, __i2);
2462 break;
2463 }
Howard Hinnant7b2cb482010-11-17 21:11:40 +00002464 traits_type::assign(const_cast<value_type&>(*__i1), *__j1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002465 }
2466 return *this;
2467}
2468
2469template <class _CharT, class _Traits, class _Allocator>
2470_LIBCPP_INLINE_VISIBILITY inline
2471basic_string<_CharT, _Traits, _Allocator>&
2472basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
2473{
2474 return replace(__pos1, __n1, __str.data(), __str.size());
2475}
2476
2477template <class _CharT, class _Traits, class _Allocator>
2478basic_string<_CharT, _Traits, _Allocator>&
2479basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
2480 size_type __pos2, size_type __n2)
2481{
2482 size_type __str_sz = __str.size();
2483 if (__pos2 > __str_sz)
2484 this->__throw_out_of_range();
2485 return replace(__pos1, __n1, __str.data() + __pos2, _STD::min(__n2, __str_sz - __pos2));
2486}
2487
2488template <class _CharT, class _Traits, class _Allocator>
2489basic_string<_CharT, _Traits, _Allocator>&
2490basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const_pointer __s)
2491{
2492#ifdef _LIBCPP_DEBUG
2493 assert(__s != 0);
2494#endif
2495 return replace(__pos, __n1, __s, traits_type::length(__s));
2496}
2497
2498template <class _CharT, class _Traits, class _Allocator>
2499_LIBCPP_INLINE_VISIBILITY inline
2500basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant7b2cb482010-11-17 21:11:40 +00002501basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002502{
2503 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
2504 __str.data(), __str.size());
2505}
2506
2507template <class _CharT, class _Traits, class _Allocator>
2508_LIBCPP_INLINE_VISIBILITY inline
2509basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant7b2cb482010-11-17 21:11:40 +00002510basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const_pointer __s, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002511{
2512 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
2513}
2514
2515template <class _CharT, class _Traits, class _Allocator>
2516_LIBCPP_INLINE_VISIBILITY inline
2517basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant7b2cb482010-11-17 21:11:40 +00002518basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const_pointer __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002519{
2520 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
2521}
2522
2523template <class _CharT, class _Traits, class _Allocator>
2524_LIBCPP_INLINE_VISIBILITY inline
2525basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant7b2cb482010-11-17 21:11:40 +00002526basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002527{
2528 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
2529}
2530
2531// erase
2532
2533template <class _CharT, class _Traits, class _Allocator>
2534basic_string<_CharT, _Traits, _Allocator>&
2535basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos, size_type __n)
2536{
2537 size_type __sz = size();
2538 if (__pos > __sz)
2539 this->__throw_out_of_range();
2540 if (__n)
2541 {
2542 pointer __p = __get_pointer();
2543 __n = _STD::min(__n, __sz - __pos);
2544 size_type __n_move = __sz - __pos - __n;
2545 if (__n_move != 0)
2546 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
2547 __sz -= __n;
2548 __set_size(__sz);
2549 __invalidate_iterators_past(__sz);
2550 traits_type::assign(__p[__sz], value_type());
2551 }
2552 return *this;
2553}
2554
2555template <class _CharT, class _Traits, class _Allocator>
2556_LIBCPP_INLINE_VISIBILITY inline
2557typename basic_string<_CharT, _Traits, _Allocator>::iterator
2558basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
2559{
2560 iterator __b = begin();
2561 size_type __r = static_cast<size_type>(__pos - __b);
2562 erase(__r, 1);
2563 return __b + __r;
2564}
2565
2566template <class _CharT, class _Traits, class _Allocator>
2567_LIBCPP_INLINE_VISIBILITY inline
2568typename basic_string<_CharT, _Traits, _Allocator>::iterator
2569basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
2570{
2571 iterator __b = begin();
2572 size_type __r = static_cast<size_type>(__first - __b);
2573 erase(__r, static_cast<size_type>(__last - __first));
2574 return __b + __r;
2575}
2576
2577template <class _CharT, class _Traits, class _Allocator>
2578_LIBCPP_INLINE_VISIBILITY inline
2579void
2580basic_string<_CharT, _Traits, _Allocator>::pop_back()
2581{
2582#ifdef _LIBCPP_DEBUG
2583 assert(!empty());
2584#endif
2585 size_type __sz;
2586 if (__is_long())
2587 {
2588 __sz = __get_long_size() - 1;
2589 __set_long_size(__sz);
2590 traits_type::assign(*(__get_long_pointer() + __sz), value_type());
2591 }
2592 else
2593 {
2594 __sz = __get_short_size() - 1;
2595 __set_short_size(__sz);
2596 traits_type::assign(*(__get_short_pointer() + __sz), value_type());
2597 }
2598 __invalidate_iterators_past(__sz);
2599}
2600
2601template <class _CharT, class _Traits, class _Allocator>
2602_LIBCPP_INLINE_VISIBILITY inline
2603void
2604basic_string<_CharT, _Traits, _Allocator>::clear()
2605{
2606 __invalidate_all_iterators();
2607 if (__is_long())
2608 {
2609 traits_type::assign(*__get_long_pointer(), value_type());
2610 __set_long_size(0);
2611 }
2612 else
2613 {
2614 traits_type::assign(*__get_short_pointer(), value_type());
2615 __set_short_size(0);
2616 }
2617}
2618
2619template <class _CharT, class _Traits, class _Allocator>
2620_LIBCPP_INLINE_VISIBILITY inline
2621void
2622basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
2623{
2624 if (__is_long())
2625 {
2626 traits_type::assign(*(__get_long_pointer() + __pos), value_type());
2627 __set_long_size(__pos);
2628 }
2629 else
2630 {
2631 traits_type::assign(*(__get_short_pointer() + __pos), value_type());
2632 __set_short_size(__pos);
2633 }
2634 __invalidate_iterators_past(__pos);
2635}
2636
2637template <class _CharT, class _Traits, class _Allocator>
2638void
2639basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
2640{
2641 size_type __sz = size();
2642 if (__n > __sz)
2643 append(__n - __sz, __c);
2644 else
2645 __erase_to_end(__n);
2646}
2647
2648template <class _CharT, class _Traits, class _Allocator>
2649_LIBCPP_INLINE_VISIBILITY inline
2650typename basic_string<_CharT, _Traits, _Allocator>::size_type
2651basic_string<_CharT, _Traits, _Allocator>::max_size() const
2652{
Howard Hinnante32b5e22010-11-17 17:55:08 +00002653 size_type __m = __alloc_traits::max_size(__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002654#if _LIBCPP_BIG_ENDIAN
2655 return (__m <= ~__long_mask ? __m : __m/2) - 1;
2656#else
2657 return __m - 1;
2658#endif
2659}
2660
2661template <class _CharT, class _Traits, class _Allocator>
2662void
2663basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __res_arg)
2664{
2665 if (__res_arg > max_size())
2666 this->__throw_length_error();
2667 size_type __cap = capacity();
2668 size_type __sz = size();
2669 __res_arg = _STD::max(__res_arg, __sz);
2670 __res_arg = __recommend(__res_arg);
2671 if (__res_arg != __cap)
2672 {
2673 pointer __new_data, __p;
2674 bool __was_long, __now_long;
2675 if (__res_arg == __min_cap - 1)
2676 {
2677 __was_long = true;
2678 __now_long = false;
2679 __new_data = __get_short_pointer();
2680 __p = __get_long_pointer();
2681 }
2682 else
2683 {
2684 if (__res_arg > __cap)
Howard Hinnante32b5e22010-11-17 17:55:08 +00002685 __new_data = __alloc_traits::allocate(__alloc(), __res_arg+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002686 else
2687 {
2688 #ifndef _LIBCPP_NO_EXCEPTIONS
2689 try
2690 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002691 #endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnante32b5e22010-11-17 17:55:08 +00002692 __new_data = __alloc_traits::allocate(__alloc(), __res_arg+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002693 #ifndef _LIBCPP_NO_EXCEPTIONS
2694 }
2695 catch (...)
2696 {
2697 return;
2698 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002699 #else // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002700 if (__new_data == 0)
2701 return;
Howard Hinnant324bb032010-08-22 00:02:43 +00002702 #endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002703 }
2704 __now_long = true;
2705 __was_long = __is_long();
2706 __p = __get_pointer();
2707 }
2708 traits_type::copy(__new_data, __p, size()+1);
2709 if (__was_long)
Howard Hinnante32b5e22010-11-17 17:55:08 +00002710 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002711 if (__now_long)
2712 {
2713 __set_long_cap(__res_arg+1);
2714 __set_long_size(__sz);
2715 __set_long_pointer(__new_data);
2716 }
2717 else
2718 __set_short_size(__sz);
2719 __invalidate_all_iterators();
2720 }
2721}
2722
2723template <class _CharT, class _Traits, class _Allocator>
2724_LIBCPP_INLINE_VISIBILITY inline
2725typename basic_string<_CharT, _Traits, _Allocator>::const_reference
2726basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const
2727{
2728#ifdef __LIBCPP_DEBUG
2729 assert(__pos <= size());
2730#endif
2731 return *(data() + __pos);
2732}
2733
2734template <class _CharT, class _Traits, class _Allocator>
2735_LIBCPP_INLINE_VISIBILITY inline
2736typename basic_string<_CharT, _Traits, _Allocator>::reference
2737basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos)
2738{
2739#ifdef __LIBCPP_DEBUG
2740 assert(__pos < size());
2741#endif
2742 return *(__get_pointer() + __pos);
2743}
2744
2745template <class _CharT, class _Traits, class _Allocator>
2746typename basic_string<_CharT, _Traits, _Allocator>::const_reference
2747basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
2748{
2749 if (__n >= size())
2750 this->__throw_out_of_range();
2751 return (*this)[__n];
2752}
2753
2754template <class _CharT, class _Traits, class _Allocator>
2755typename basic_string<_CharT, _Traits, _Allocator>::reference
2756basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
2757{
2758 if (__n >= size())
2759 this->__throw_out_of_range();
2760 return (*this)[__n];
2761}
2762
2763template <class _CharT, class _Traits, class _Allocator>
2764_LIBCPP_INLINE_VISIBILITY inline
2765typename basic_string<_CharT, _Traits, _Allocator>::reference
2766basic_string<_CharT, _Traits, _Allocator>::front()
2767{
2768#ifdef _LIBCPP_DEBUG
2769 assert(!empty());
2770#endif
2771 return *__get_pointer();
2772}
2773
2774template <class _CharT, class _Traits, class _Allocator>
2775_LIBCPP_INLINE_VISIBILITY inline
2776typename basic_string<_CharT, _Traits, _Allocator>::const_reference
2777basic_string<_CharT, _Traits, _Allocator>::front() const
2778{
2779#ifdef _LIBCPP_DEBUG
2780 assert(!empty());
2781#endif
2782 return *data();
2783}
2784
2785template <class _CharT, class _Traits, class _Allocator>
2786_LIBCPP_INLINE_VISIBILITY inline
2787typename basic_string<_CharT, _Traits, _Allocator>::reference
2788basic_string<_CharT, _Traits, _Allocator>::back()
2789{
2790#ifdef _LIBCPP_DEBUG
2791 assert(!empty());
2792#endif
2793 return *(__get_pointer() + size() - 1);
2794}
2795
2796template <class _CharT, class _Traits, class _Allocator>
2797_LIBCPP_INLINE_VISIBILITY inline
2798typename basic_string<_CharT, _Traits, _Allocator>::const_reference
2799basic_string<_CharT, _Traits, _Allocator>::back() const
2800{
2801#ifdef _LIBCPP_DEBUG
2802 assert(!empty());
2803#endif
2804 return *(data() + size() - 1);
2805}
2806
2807template <class _CharT, class _Traits, class _Allocator>
2808typename basic_string<_CharT, _Traits, _Allocator>::size_type
2809basic_string<_CharT, _Traits, _Allocator>::copy(pointer __s, size_type __n, size_type __pos) const
2810{
2811 size_type __sz = size();
2812 if (__pos > __sz)
2813 this->__throw_out_of_range();
2814 size_type __rlen = _STD::min(__n, __sz - __pos);
2815 traits_type::copy(__s, data() + __pos, __rlen);
2816 return __rlen;
2817}
2818
2819template <class _CharT, class _Traits, class _Allocator>
2820_LIBCPP_INLINE_VISIBILITY inline
2821basic_string<_CharT, _Traits, _Allocator>
2822basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
2823{
2824 return basic_string(*this, __pos, __n, __alloc());
2825}
2826
2827template <class _CharT, class _Traits, class _Allocator>
2828_LIBCPP_INLINE_VISIBILITY inline
2829void
2830basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
2831{
Howard Hinnante32b5e22010-11-17 17:55:08 +00002832 _STD::swap(__r_.first(), __str.__r_.first());
2833 __swap_alloc(__alloc(), __str.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002834#ifdef _LIBCPP_DEBUG
2835 __invalidate_all_iterators();
2836 __str.__invalidate_all_iterators();
Howard Hinnant324bb032010-08-22 00:02:43 +00002837#endif // _LIBCPP_DEBUG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002838}
2839
2840// find
2841
2842template <class _Traits>
2843struct _LIBCPP_HIDDEN __traits_eq
2844{
2845 typedef typename _Traits::char_type char_type;
2846 _LIBCPP_INLINE_VISIBILITY bool operator()(const char_type& __x, const char_type& __y) {return _Traits::eq(__x, __y);}
2847};
2848
2849template<class _CharT, class _Traits, class _Allocator>
2850typename basic_string<_CharT, _Traits, _Allocator>::size_type
2851basic_string<_CharT, _Traits, _Allocator>::find(const_pointer __s, size_type __pos, size_type __n) const
2852{
2853#ifdef _LIBCPP_DEBUG
2854 assert(__s != 0);
2855#endif
2856 size_type __sz = size();
2857 if (__pos > __sz || __sz - __pos < __n)
2858 return npos;
2859 if (__n == 0)
2860 return __pos;
2861 const_pointer __p = data();
2862 const_pointer __r = _STD::search(__p + __pos, __p + __sz, __s, __s + __n, __traits_eq<traits_type>());
2863 if (__r == __p + __sz)
2864 return npos;
2865 return static_cast<size_type>(__r - __p);
2866}
2867
2868template<class _CharT, class _Traits, class _Allocator>
2869_LIBCPP_INLINE_VISIBILITY inline
2870typename basic_string<_CharT, _Traits, _Allocator>::size_type
2871basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str, size_type __pos) const
2872{
2873 return find(__str.data(), __pos, __str.size());
2874}
2875
2876template<class _CharT, class _Traits, class _Allocator>
2877_LIBCPP_INLINE_VISIBILITY inline
2878typename basic_string<_CharT, _Traits, _Allocator>::size_type
2879basic_string<_CharT, _Traits, _Allocator>::find(const_pointer __s, size_type __pos) const
2880{
2881#ifdef _LIBCPP_DEBUG
2882 assert(__s != 0);
2883#endif
2884 return find(__s, __pos, traits_type::length(__s));
2885}
2886
2887template<class _CharT, class _Traits, class _Allocator>
2888typename basic_string<_CharT, _Traits, _Allocator>::size_type
2889basic_string<_CharT, _Traits, _Allocator>::find(value_type __c, size_type __pos) const
2890{
2891 size_type __sz = size();
2892 if (__pos >= __sz)
2893 return npos;
2894 const_pointer __p = data();
2895 const_pointer __r = traits_type::find(__p + __pos, __sz - __pos, __c);
2896 if (__r == 0)
2897 return npos;
2898 return static_cast<size_type>(__r - __p);
2899}
2900
2901// rfind
2902
2903template<class _CharT, class _Traits, class _Allocator>
2904typename basic_string<_CharT, _Traits, _Allocator>::size_type
2905basic_string<_CharT, _Traits, _Allocator>::rfind(const_pointer __s, size_type __pos, size_type __n) const
2906{
2907#ifdef _LIBCPP_DEBUG
2908 assert(__s != 0);
2909#endif
2910 size_type __sz = size();
2911 __pos = _STD::min(__pos, __sz);
2912 if (__n < __sz - __pos)
2913 __pos += __n;
2914 else
2915 __pos = __sz;
2916 const_pointer __p = data();
2917 const_pointer __r = _STD::find_end(__p, __p + __pos, __s, __s + __n, __traits_eq<traits_type>());
2918 if (__n > 0 && __r == __p + __pos)
2919 return npos;
2920 return static_cast<size_type>(__r - __p);
2921}
2922
2923template<class _CharT, class _Traits, class _Allocator>
2924_LIBCPP_INLINE_VISIBILITY inline
2925typename basic_string<_CharT, _Traits, _Allocator>::size_type
2926basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str, size_type __pos) const
2927{
2928 return rfind(__str.data(), __pos, __str.size());
2929}
2930
2931template<class _CharT, class _Traits, class _Allocator>
2932_LIBCPP_INLINE_VISIBILITY inline
2933typename basic_string<_CharT, _Traits, _Allocator>::size_type
2934basic_string<_CharT, _Traits, _Allocator>::rfind(const_pointer __s, size_type __pos) const
2935{
2936#ifdef _LIBCPP_DEBUG
2937 assert(__s != 0);
2938#endif
2939 return rfind(__s, __pos, traits_type::length(__s));
2940}
2941
2942template<class _CharT, class _Traits, class _Allocator>
2943typename basic_string<_CharT, _Traits, _Allocator>::size_type
2944basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c, size_type __pos) const
2945{
2946 size_type __sz = size();
2947 if (__sz)
2948 {
2949 if (__pos < __sz)
2950 ++__pos;
2951 else
2952 __pos = __sz;
2953 const_pointer __p = data();
2954 for (const_pointer __ps = __p + __pos; __ps != __p;)
2955 {
2956 if (traits_type::eq(*--__ps, __c))
2957 return static_cast<size_type>(__ps - __p);
2958 }
2959 }
2960 return npos;
2961}
2962
2963// find_first_of
2964
2965template<class _CharT, class _Traits, class _Allocator>
2966typename basic_string<_CharT, _Traits, _Allocator>::size_type
2967basic_string<_CharT, _Traits, _Allocator>::find_first_of(const_pointer __s, size_type __pos, size_type __n) const
2968{
2969#ifdef _LIBCPP_DEBUG
2970 assert(__s != 0);
2971#endif
2972 size_type __sz = size();
2973 if (__pos >= __sz || __n == 0)
2974 return npos;
2975 const_pointer __p = data();
2976 const_pointer __r = _STD::find_first_of(__p + __pos, __p + __sz, __s, __s + __n, __traits_eq<traits_type>());
2977 if (__r == __p + __sz)
2978 return npos;
2979 return static_cast<size_type>(__r - __p);
2980}
2981
2982template<class _CharT, class _Traits, class _Allocator>
2983_LIBCPP_INLINE_VISIBILITY inline
2984typename basic_string<_CharT, _Traits, _Allocator>::size_type
2985basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str, size_type __pos) const
2986{
2987 return find_first_of(__str.data(), __pos, __str.size());
2988}
2989
2990template<class _CharT, class _Traits, class _Allocator>
2991_LIBCPP_INLINE_VISIBILITY inline
2992typename basic_string<_CharT, _Traits, _Allocator>::size_type
2993basic_string<_CharT, _Traits, _Allocator>::find_first_of(const_pointer __s, size_type __pos) const
2994{
2995#ifdef _LIBCPP_DEBUG
2996 assert(__s != 0);
2997#endif
2998 return find_first_of(__s, __pos, traits_type::length(__s));
2999}
3000
3001template<class _CharT, class _Traits, class _Allocator>
3002_LIBCPP_INLINE_VISIBILITY inline
3003typename basic_string<_CharT, _Traits, _Allocator>::size_type
3004basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c, size_type __pos) const
3005{
3006 return find(__c, __pos);
3007}
3008
3009// find_last_of
3010
3011template<class _CharT, class _Traits, class _Allocator>
3012typename basic_string<_CharT, _Traits, _Allocator>::size_type
3013basic_string<_CharT, _Traits, _Allocator>::find_last_of(const_pointer __s, size_type __pos, size_type __n) const
3014{
3015#ifdef _LIBCPP_DEBUG
3016 assert(__s != 0);
3017#endif
3018 if (__n != 0)
3019 {
3020 size_type __sz = size();
3021 if (__pos < __sz)
3022 ++__pos;
3023 else
3024 __pos = __sz;
3025 const_pointer __p = data();
3026 for (const_pointer __ps = __p + __pos; __ps != __p;)
3027 {
3028 const_pointer __r = traits_type::find(__s, __n, *--__ps);
3029 if (__r)
3030 return static_cast<size_type>(__ps - __p);
3031 }
3032 }
3033 return npos;
3034}
3035
3036template<class _CharT, class _Traits, class _Allocator>
3037_LIBCPP_INLINE_VISIBILITY inline
3038typename basic_string<_CharT, _Traits, _Allocator>::size_type
3039basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str, size_type __pos) const
3040{
3041 return find_last_of(__str.data(), __pos, __str.size());
3042}
3043
3044template<class _CharT, class _Traits, class _Allocator>
3045_LIBCPP_INLINE_VISIBILITY inline
3046typename basic_string<_CharT, _Traits, _Allocator>::size_type
3047basic_string<_CharT, _Traits, _Allocator>::find_last_of(const_pointer __s, size_type __pos) const
3048{
3049#ifdef _LIBCPP_DEBUG
3050 assert(__s != 0);
3051#endif
3052 return find_last_of(__s, __pos, traits_type::length(__s));
3053}
3054
3055template<class _CharT, class _Traits, class _Allocator>
3056_LIBCPP_INLINE_VISIBILITY inline
3057typename basic_string<_CharT, _Traits, _Allocator>::size_type
3058basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c, size_type __pos) const
3059{
3060 return rfind(__c, __pos);
3061}
3062
3063// find_first_not_of
3064
3065template<class _CharT, class _Traits, class _Allocator>
3066typename basic_string<_CharT, _Traits, _Allocator>::size_type
3067basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const_pointer __s, size_type __pos, size_type __n) const
3068{
3069#ifdef _LIBCPP_DEBUG
3070 assert(__s != 0);
3071#endif
3072 size_type __sz = size();
3073 if (__pos < __sz)
3074 {
3075 const_pointer __p = data();
3076 const_pointer __pe = __p + __sz;
3077 for (const_pointer __ps = __p + __pos; __ps != __pe; ++__ps)
3078 if (traits_type::find(__s, __n, *__ps) == 0)
3079 return static_cast<size_type>(__ps - __p);
3080 }
3081 return npos;
3082}
3083
3084template<class _CharT, class _Traits, class _Allocator>
3085_LIBCPP_INLINE_VISIBILITY inline
3086typename basic_string<_CharT, _Traits, _Allocator>::size_type
3087basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str, size_type __pos) const
3088{
3089 return find_first_not_of(__str.data(), __pos, __str.size());
3090}
3091
3092template<class _CharT, class _Traits, class _Allocator>
3093_LIBCPP_INLINE_VISIBILITY inline
3094typename basic_string<_CharT, _Traits, _Allocator>::size_type
3095basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const_pointer __s, size_type __pos) const
3096{
3097#ifdef _LIBCPP_DEBUG
3098 assert(__s != 0);
3099#endif
3100 return find_first_not_of(__s, __pos, traits_type::length(__s));
3101}
3102
3103template<class _CharT, class _Traits, class _Allocator>
3104_LIBCPP_INLINE_VISIBILITY inline
3105typename basic_string<_CharT, _Traits, _Allocator>::size_type
3106basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c, size_type __pos) const
3107{
3108 size_type __sz = size();
3109 if (__pos < __sz)
3110 {
3111 const_pointer __p = data();
3112 const_pointer __pe = __p + __sz;
3113 for (const_pointer __ps = __p + __pos; __p != __pe; ++__ps)
3114 if (!traits_type::eq(*__ps, __c))
3115 return static_cast<size_type>(__ps - __p);
3116 }
3117 return npos;
3118}
3119
3120// find_last_not_of
3121
3122template<class _CharT, class _Traits, class _Allocator>
3123typename basic_string<_CharT, _Traits, _Allocator>::size_type
3124basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const_pointer __s, size_type __pos, size_type __n) const
3125{
3126#ifdef _LIBCPP_DEBUG
3127 assert(__s != 0);
3128#endif
3129 size_type __sz = size();
3130 if (__pos < __sz)
3131 ++__pos;
3132 else
3133 __pos = __sz;
3134 const_pointer __p = data();
3135 for (const_pointer __ps = __p + __pos; __ps != __p;)
3136 if (traits_type::find(__s, __n, *--__ps) == 0)
3137 return static_cast<size_type>(__ps - __p);
3138 return npos;
3139}
3140
3141template<class _CharT, class _Traits, class _Allocator>
3142_LIBCPP_INLINE_VISIBILITY inline
3143typename basic_string<_CharT, _Traits, _Allocator>::size_type
3144basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str, size_type __pos) const
3145{
3146 return find_last_not_of(__str.data(), __pos, __str.size());
3147}
3148
3149template<class _CharT, class _Traits, class _Allocator>
3150_LIBCPP_INLINE_VISIBILITY inline
3151typename basic_string<_CharT, _Traits, _Allocator>::size_type
3152basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const_pointer __s, size_type __pos) const
3153{
3154#ifdef _LIBCPP_DEBUG
3155 assert(__s != 0);
3156#endif
3157 return find_last_not_of(__s, __pos, traits_type::length(__s));
3158}
3159
3160template<class _CharT, class _Traits, class _Allocator>
3161_LIBCPP_INLINE_VISIBILITY inline
3162typename basic_string<_CharT, _Traits, _Allocator>::size_type
3163basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c, size_type __pos) const
3164{
3165 size_type __sz = size();
3166 if (__pos < __sz)
3167 ++__pos;
3168 else
3169 __pos = __sz;
3170 const_pointer __p = data();
3171 for (const_pointer __ps = __p + __pos; __ps != __p;)
3172 if (!traits_type::eq(*--__ps, __c))
3173 return static_cast<size_type>(__ps - __p);
3174 return npos;
3175}
3176
3177// compare
3178
3179template <class _CharT, class _Traits, class _Allocator>
3180_LIBCPP_INLINE_VISIBILITY inline
3181int
3182basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const
3183{
3184 return compare(0, npos, __str.data(), __str.size());
3185}
3186
3187template <class _CharT, class _Traits, class _Allocator>
3188_LIBCPP_INLINE_VISIBILITY inline
3189int
3190basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1, size_type __n1, const basic_string& __str) const
3191{
3192 return compare(__pos1, __n1, __str.data(), __str.size());
3193}
3194
3195template <class _CharT, class _Traits, class _Allocator>
3196int
3197basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1, size_type __n1, const basic_string& __str,
3198 size_type __pos2, size_type __n2) const
3199{
3200 size_type __sz = __str.size();
3201 if (__pos2 > __sz)
3202 this->__throw_out_of_range();
3203 return compare(__pos1, __n1, __str.data() + __pos2, _STD::min(__n2, __sz - __pos2));
3204}
3205
3206template <class _CharT, class _Traits, class _Allocator>
3207int
3208basic_string<_CharT, _Traits, _Allocator>::compare(const_pointer __s) const
3209{
3210#ifdef _LIBCPP_DEBUG
3211 assert(__s != 0);
3212#endif
3213 return compare(0, npos, __s, traits_type::length(__s));
3214}
3215
3216template <class _CharT, class _Traits, class _Allocator>
3217int
3218basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1, size_type __n1, const_pointer __s) const
3219{
3220#ifdef _LIBCPP_DEBUG
3221 assert(__s != 0);
3222#endif
3223 return compare(__pos1, __n1, __s, traits_type::length(__s));
3224}
3225
3226template <class _CharT, class _Traits, class _Allocator>
3227int
3228basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1, size_type __n1,
3229 const_pointer __s, size_type __n2) const
3230{
3231#ifdef _LIBCPP_DEBUG
3232 assert(__s != 0);
3233#endif
3234 size_type __sz = size();
3235 if (__pos1 > __sz || __n2 == npos)
3236 this->__throw_out_of_range();
3237 size_type __rlen = _STD::min(__n1, __sz - __pos1);
3238 int __r = traits_type::compare(data() + __pos1, __s, _STD::min(__rlen, __n2));
3239 if (__r == 0)
3240 {
3241 if (__rlen < __n2)
3242 __r = -1;
3243 else if (__rlen > __n2)
3244 __r = 1;
3245 }
3246 return __r;
3247}
3248
3249// __invariants
3250
3251template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00003252_LIBCPP_INLINE_VISIBILITY inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003253bool
3254basic_string<_CharT, _Traits, _Allocator>::__invariants() const
3255{
3256 if (size() > capacity())
3257 return false;
3258 if (capacity() < __min_cap - 1)
3259 return false;
3260 if (data() == 0)
3261 return false;
3262 if (data()[size()] != value_type(0))
3263 return false;
3264 return true;
3265}
3266
3267// operator==
3268
3269template<class _CharT, class _Traits, class _Allocator>
3270_LIBCPP_INLINE_VISIBILITY inline
3271bool
3272operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3273 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3274{
3275 return __lhs.size() == __rhs.size() && _Traits::compare(__lhs.data(), __rhs.data(), __lhs.size()) == 0;
3276}
3277
3278template<class _CharT, class _Traits, class _Allocator>
3279_LIBCPP_INLINE_VISIBILITY inline
3280bool
3281operator==(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3282{
3283 return __rhs.compare(__lhs) == 0;
3284}
3285
3286template<class _Allocator>
3287_LIBCPP_INLINE_VISIBILITY inline
3288bool
3289operator==(const char* __lhs, const basic_string<char, char_traits<char>, _Allocator>& __rhs)
3290{
3291 return strcmp(__lhs, __rhs.data()) == 0;
3292}
3293
3294template<class _Allocator>
3295_LIBCPP_INLINE_VISIBILITY inline
3296bool
3297operator==(const wchar_t* __lhs, const basic_string<wchar_t, char_traits<wchar_t>, _Allocator>& __rhs)
3298{
3299 return wcscmp(__lhs, __rhs.data()) == 0;
3300}
3301
3302template<class _CharT, class _Traits, class _Allocator>
3303_LIBCPP_INLINE_VISIBILITY inline
3304bool
3305operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs, const _CharT* __rhs)
3306{
3307 return __lhs.compare(__rhs) == 0;
3308}
3309
3310template<class _Allocator>
3311_LIBCPP_INLINE_VISIBILITY inline
3312bool
3313operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs, const char* __rhs)
3314{
3315 return strcmp(__lhs.data(), __rhs) == 0;
3316}
3317
3318template<class _Allocator>
3319_LIBCPP_INLINE_VISIBILITY inline
3320bool
3321operator==(const basic_string<wchar_t, char_traits<wchar_t>, _Allocator>& __lhs, const wchar_t* __rhs)
3322{
3323 return wcscmp(__lhs.data(), __rhs) == 0;
3324}
3325
3326// operator!=
3327
Howard Hinnant324bb032010-08-22 00:02:43 +00003328template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003329_LIBCPP_INLINE_VISIBILITY inline
3330bool
3331operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
3332 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3333{
3334 return !(__lhs == __rhs);
3335}
3336
3337template<class _CharT, class _Traits, class _Allocator>
3338_LIBCPP_INLINE_VISIBILITY inline
3339bool
3340operator!=(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3341{
3342 return !(__lhs == __rhs);
3343}
3344
3345template<class _CharT, class _Traits, class _Allocator>
3346_LIBCPP_INLINE_VISIBILITY inline
3347bool
3348operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
3349{
3350 return !(__lhs == __rhs);
3351}
3352
3353// operator<
3354
3355template<class _CharT, class _Traits, class _Allocator>
3356_LIBCPP_INLINE_VISIBILITY inline
3357bool
3358operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3359 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3360{
3361 return __lhs.cmpare(__rhs) < 0;
3362}
3363
3364template<class _Allocator>
3365_LIBCPP_INLINE_VISIBILITY inline
3366bool
3367operator< (const basic_string<char, char_traits<char>, _Allocator>& __lhs,
3368 const basic_string<char, char_traits<char>, _Allocator>& __rhs)
3369{
3370 return strcmp(__lhs.data(), __rhs.data()) < 0;
3371}
3372
3373template<class _Allocator>
3374_LIBCPP_INLINE_VISIBILITY inline
3375bool
3376operator< (const basic_string<wchar_t, char_traits<wchar_t>, _Allocator>& __lhs,
3377 const basic_string<wchar_t, char_traits<wchar_t>, _Allocator>& __rhs)
3378{
3379 return wcscmp(__lhs.data(), __rhs.data()) < 0;
3380}
3381
3382template<class _CharT, class _Traits, class _Allocator>
3383_LIBCPP_INLINE_VISIBILITY inline
3384bool
3385operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
3386{
3387 return __lhs.compare(__rhs);
3388}
3389
3390template<class _Allocator>
3391_LIBCPP_INLINE_VISIBILITY inline
3392bool
3393operator< (const basic_string<char, char_traits<char>, _Allocator>& __lhs, const char* __rhs)
3394{
3395 return strcmp(__lhs.data(), __rhs) < 0;
3396}
3397
3398template<class _Allocator>
3399_LIBCPP_INLINE_VISIBILITY inline
3400bool
3401operator< (const basic_string<wchar_t, char_traits<wchar_t>, _Allocator>& __lhs, const wchar_t* __rhs)
3402{
3403 return wcscmp(__lhs.data(), __rhs) < 0;
3404}
3405
3406template<class _CharT, class _Traits, class _Allocator>
3407_LIBCPP_INLINE_VISIBILITY inline
3408bool
3409operator< (const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3410{
3411 return __rhs.compare(__lhs) > 0;
3412}
3413
3414template<class _Allocator>
3415_LIBCPP_INLINE_VISIBILITY inline
3416bool
3417operator< (const char* __lhs, const basic_string<char, char_traits<char>, _Allocator>& __rhs)
3418{
3419 return strcmp(__lhs, __rhs.data()) < 0;
3420}
3421
3422template<class _Allocator>
3423_LIBCPP_INLINE_VISIBILITY inline
3424bool
3425operator< (const wchar_t* __lhs, const basic_string<wchar_t, char_traits<wchar_t>, _Allocator>& __rhs)
3426{
3427 return wcscmp(__lhs, __rhs.data()) < 0;
3428}
3429
3430// operator>
3431
3432template<class _CharT, class _Traits, class _Allocator>
3433_LIBCPP_INLINE_VISIBILITY inline
3434bool
3435operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3436 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3437{
3438 return __rhs < __lhs;
3439}
3440
3441template<class _CharT, class _Traits, class _Allocator>
3442_LIBCPP_INLINE_VISIBILITY inline
3443bool
3444operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
3445{
3446 return __rhs < __lhs;
3447}
3448
3449template<class _CharT, class _Traits, class _Allocator>
3450_LIBCPP_INLINE_VISIBILITY inline
3451bool
3452operator> (const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3453{
3454 return __rhs < __lhs;
3455}
3456
3457// operator<=
3458
3459template<class _CharT, class _Traits, class _Allocator>
3460_LIBCPP_INLINE_VISIBILITY inline
3461bool
3462operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3463 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3464{
3465 return !(__rhs < __lhs);
3466}
3467
3468template<class _CharT, class _Traits, class _Allocator>
3469_LIBCPP_INLINE_VISIBILITY inline
3470bool
3471operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
3472{
3473 return !(__rhs < __lhs);
3474}
3475
3476template<class _CharT, class _Traits, class _Allocator>
3477_LIBCPP_INLINE_VISIBILITY inline
3478bool
3479operator<=(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3480{
3481 return !(__rhs < __lhs);
3482}
3483
3484// operator>=
3485
3486template<class _CharT, class _Traits, class _Allocator>
3487_LIBCPP_INLINE_VISIBILITY inline
3488bool
3489operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3490 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3491{
3492 return !(__lhs < __rhs);
3493}
3494
3495template<class _CharT, class _Traits, class _Allocator>
3496_LIBCPP_INLINE_VISIBILITY inline
3497bool
3498operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
3499{
3500 return !(__lhs < __rhs);
3501}
3502
3503template<class _CharT, class _Traits, class _Allocator>
3504_LIBCPP_INLINE_VISIBILITY inline
3505bool
3506operator>=(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3507{
3508 return !(__lhs < __rhs);
3509}
3510
3511// operator +
3512
3513template<class _CharT, class _Traits, class _Allocator>
3514basic_string<_CharT, _Traits, _Allocator>
3515operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3516 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3517{
3518 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
3519 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
3520 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
3521 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
3522 __r.append(__rhs.data(), __rhs_sz);
3523 return __r;
3524}
3525
3526template<class _CharT, class _Traits, class _Allocator>
3527basic_string<_CharT, _Traits, _Allocator>
3528operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
3529{
3530 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
3531 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = _Traits::length(__lhs);
3532 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
3533 __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz);
3534 __r.append(__rhs.data(), __rhs_sz);
3535 return __r;
3536}
3537
3538template<class _CharT, class _Traits, class _Allocator>
3539basic_string<_CharT, _Traits, _Allocator>
3540operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
3541{
3542 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
3543 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
3544 __r.__init(&__lhs, 1, 1 + __rhs_sz);
3545 __r.append(__rhs.data(), __rhs_sz);
3546 return __r;
3547}
3548
3549template<class _CharT, class _Traits, class _Allocator>
3550basic_string<_CharT, _Traits, _Allocator>
3551operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
3552{
3553 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
3554 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
3555 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = _Traits::length(__rhs);
3556 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
3557 __r.append(__rhs, __rhs_sz);
3558 return __r;
3559}
3560
3561template<class _CharT, class _Traits, class _Allocator>
3562basic_string<_CharT, _Traits, _Allocator>
3563operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
3564{
3565 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
3566 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
3567 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1);
3568 __r.push_back(__rhs);
3569 return __r;
3570}
3571
Howard Hinnant73d21a42010-09-04 23:28:19 +00003572#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003573
3574template<class _CharT, class _Traits, class _Allocator>
3575_LIBCPP_INLINE_VISIBILITY inline
3576basic_string<_CharT, _Traits, _Allocator>
3577operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3578{
3579 return _STD::move(__lhs.append(__rhs));
3580}
3581
3582template<class _CharT, class _Traits, class _Allocator>
3583_LIBCPP_INLINE_VISIBILITY inline
3584basic_string<_CharT, _Traits, _Allocator>
3585operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
3586{
3587 return _STD::move(__rhs.insert(0, __lhs));
3588}
3589
3590template<class _CharT, class _Traits, class _Allocator>
3591_LIBCPP_INLINE_VISIBILITY inline
3592basic_string<_CharT, _Traits, _Allocator>
3593operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
3594{
3595 return _STD::move(__lhs.append(__rhs));
3596}
3597
3598template<class _CharT, class _Traits, class _Allocator>
3599_LIBCPP_INLINE_VISIBILITY inline
3600basic_string<_CharT, _Traits, _Allocator>
3601operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
3602{
3603 return _STD::move(__rhs.insert(0, __lhs));
3604}
3605
3606template<class _CharT, class _Traits, class _Allocator>
3607_LIBCPP_INLINE_VISIBILITY inline
3608basic_string<_CharT, _Traits, _Allocator>
3609operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
3610{
3611 __rhs.insert(__rhs.begin(), __lhs);
3612 return _STD::move(__rhs);
3613}
3614
3615template<class _CharT, class _Traits, class _Allocator>
3616_LIBCPP_INLINE_VISIBILITY inline
3617basic_string<_CharT, _Traits, _Allocator>
3618operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
3619{
3620 return _STD::move(__lhs.append(__rhs));
3621}
3622
3623template<class _CharT, class _Traits, class _Allocator>
3624_LIBCPP_INLINE_VISIBILITY inline
3625basic_string<_CharT, _Traits, _Allocator>
3626operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
3627{
3628 __lhs.push_back(__rhs);
3629 return _STD::move(__lhs);
3630}
3631
Howard Hinnant73d21a42010-09-04 23:28:19 +00003632#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003633
3634// swap
3635
3636template<class _CharT, class _Traits, class _Allocator>
3637_LIBCPP_INLINE_VISIBILITY inline
3638void
3639swap(basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>& __rhs)
3640{
3641 __lhs.swap(__rhs);
3642}
3643
3644template<class _CharT, class _Traits, class _Allocator>
3645struct __is_zero_default_constructible<basic_string<_CharT, _Traits, _Allocator> >
3646 : public integral_constant<bool, __is_zero_default_constructible<_Allocator>::value> {};
3647
3648#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
3649
3650typedef basic_string<char16_t> u16string;
3651typedef basic_string<char32_t> u32string;
3652
Howard Hinnant324bb032010-08-22 00:02:43 +00003653#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003654
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003655int stoi (const string& __str, size_t* __idx = 0, int __base = 10);
3656long stol (const string& __str, size_t* __idx = 0, int __base = 10);
3657unsigned long stoul (const string& __str, size_t* __idx = 0, int __base = 10);
3658long long stoll (const string& __str, size_t* __idx = 0, int __base = 10);
3659unsigned long long stoull(const string& __str, size_t* __idx = 0, int __base = 10);
3660
3661float stof (const string& __str, size_t* __idx = 0);
3662double stod (const string& __str, size_t* __idx = 0);
3663long double stold(const string& __str, size_t* __idx = 0);
3664
3665string to_string(int __val);
3666string to_string(unsigned __val);
3667string to_string(long __val);
3668string to_string(unsigned long __val);
3669string to_string(long long __val);
3670string to_string(unsigned long long __val);
3671string to_string(float __val);
3672string to_string(double __val);
3673string to_string(long double __val);
3674
3675int stoi (const wstring& __str, size_t* __idx = 0, int __base = 10);
3676long stol (const wstring& __str, size_t* __idx = 0, int __base = 10);
3677unsigned long stoul (const wstring& __str, size_t* __idx = 0, int __base = 10);
3678long long stoll (const wstring& __str, size_t* __idx = 0, int __base = 10);
3679unsigned long long stoull(const wstring& __str, size_t* __idx = 0, int __base = 10);
3680
3681float stof (const wstring& __str, size_t* __idx = 0);
3682double stod (const wstring& __str, size_t* __idx = 0);
3683long double stold(const wstring& __str, size_t* __idx = 0);
3684
3685wstring to_wstring(int __val);
3686wstring to_wstring(unsigned __val);
3687wstring to_wstring(long __val);
3688wstring to_wstring(unsigned long __val);
3689wstring to_wstring(long long __val);
3690wstring to_wstring(unsigned long long __val);
3691wstring to_wstring(float __val);
3692wstring to_wstring(double __val);
3693wstring to_wstring(long double __val);
3694
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003695template<class _CharT, class _Traits, class _Allocator>
3696 const typename basic_string<_CharT, _Traits, _Allocator>::size_type
3697 basic_string<_CharT, _Traits, _Allocator>::npos;
3698
3699template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant8d7a9552010-09-23 17:31:07 +00003700struct _LIBCPP_VISIBLE hash<basic_string<_CharT, _Traits, _Allocator> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003701 : public unary_function<basic_string<_CharT, _Traits, _Allocator>, size_t>
3702{
3703 size_t
3704 operator()(const basic_string<_CharT, _Traits, _Allocator>& __val) const;
3705};
3706
3707template<class _CharT, class _Traits, class _Allocator>
3708size_t
3709hash<basic_string<_CharT, _Traits, _Allocator> >::operator()(
3710 const basic_string<_CharT, _Traits, _Allocator>& __val) const
3711{
3712 typedef basic_string<_CharT, _Traits, _Allocator> S;
3713 typedef typename S::const_pointer const_pointer;
3714 size_t __r = 0;
3715 const size_t __sr = __CHAR_BIT__ * sizeof(size_t) - 8;
3716 const size_t __m = size_t(0xF) << (__sr + 4);
3717 const_pointer __p = __val.data();
3718 const_pointer __e = __p + __val.size();
3719 for (; __p != __e; ++__p)
3720 {
3721 __r = (__r << 4) + *__p;
3722 size_t __g = __r & __m;
3723 __r ^= __g | (__g >> __sr);
3724 }
3725 return __r;
3726}
3727
3728extern template class basic_string<char>;
3729extern template class basic_string<wchar_t>;
3730
3731extern template
3732 enable_if<__is_forward_iterator<char const*>::value, void>::type
3733 basic_string<char, char_traits<char>, allocator<char> >::
3734 __init<char const*>(char const*, char const*);
3735
3736extern template
3737 enable_if<__is_forward_iterator<wchar_t const*>::value, void>::type
3738 basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> >::
3739 __init<wchar_t const*>(wchar_t const*, wchar_t const*);
3740
3741extern template
3742 enable_if<__is_forward_iterator<char*>::value,
3743 basic_string<char, char_traits<char>, allocator<char> >&>::type
3744 basic_string<char, char_traits<char>, allocator<char> >::
3745 append<char*>(char*, char*);
3746
3747extern template
3748 enable_if<__is_forward_iterator<wchar_t*>::value,
3749 basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> >&>::type
3750 basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> >::
3751 append<wchar_t*>(wchar_t*, wchar_t*);
3752
3753extern template
3754 enable_if<__is_forward_iterator<char const*>::value,
3755 string::iterator>::type
3756 string::
3757 insert<char const*>(string::const_iterator, char const*, char const*);
3758
3759extern template
3760 enable_if<__is_forward_iterator<wchar_t const*>::value,
3761 wstring::iterator>::type
3762 wstring::
3763 insert<wchar_t const*>(wstring::const_iterator, wchar_t const*, wchar_t const*);
3764
3765extern template
3766 enable_if<__is_input_iterator<char const*>::value, string&>::type
3767 string::
Howard Hinnant7b2cb482010-11-17 21:11:40 +00003768 replace<char const*>(string::const_iterator, string::const_iterator, char const*, char const*);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003769
3770extern template
3771 enable_if<__is_input_iterator<wchar_t const*>::value, wstring&>::type
3772 wstring::
Howard Hinnant7b2cb482010-11-17 21:11:40 +00003773 replace<wchar_t const*>(wstring::const_iterator, wstring::const_iterator, wchar_t const*, wchar_t const*);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003774
3775extern template
3776 enable_if<__is_forward_iterator<wchar_t*>::value, wstring&>::type
3777 wstring::assign<wchar_t*>(wchar_t*, wchar_t*);
3778
3779extern template
3780 string
3781 operator+<char, char_traits<char>, allocator<char> >(char const*, string const&);
3782
3783_LIBCPP_END_NAMESPACE_STD
3784
3785#endif // _LIBCPP_STRING