blob: 2ca5986358aca817e5607d651be05df79080cb85 [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
74template<class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
75class basic_string
76{
77public:
78// types:
79 typedef traits traits_type;
80 typedef typename traits_type::char_type value_type;
81 typedef Allocator allocator_type;
82 typedef typename allocator_type::size_type size_type;
83 typedef typename allocator_type::difference_type difference_type;
84 typedef typename allocator_type::reference reference;
85 typedef typename allocator_type::const_reference const_reference;
86 typedef typename allocator_type::pointer pointer;
87 typedef typename allocator_type::const_pointer const_pointer;
88 typedef implementation-defined iterator;
89 typedef implementation-defined const_iterator;
90 typedef std::reverse_iterator<iterator> reverse_iterator;
91 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
92
93 static const size_type npos = -1;
94
95 explicit basic_string(const allocator_type& a = allocator_type());
96 basic_string(const basic_string& str);
97 basic_string(basic_string&& str);
Howard Hinnanta6a062d2010-06-02 18:20:39 +000098 basic_string(const basic_string& str, size_type pos, size_type n = npos,
99 const allocator_type& a = allocator_type());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000100 basic_string(const_pointer s, const allocator_type& a = allocator_type());
101 basic_string(const_pointer s, size_type n, const allocator_type& a = allocator_type());
102 basic_string(size_type n, value_type c, const allocator_type& a = allocator_type());
103 template<class InputIterator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000104 basic_string(InputIterator begin, InputIterator end,
105 const allocator_type& a = allocator_type());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000106 basic_string(initializer_list<value_type>, const Allocator& = Allocator());
107 basic_string(const basic_string&, const Allocator&);
108 basic_string(basic_string&&, const Allocator&);
109
110 ~basic_string();
111
112 basic_string& operator=(const basic_string& str);
113 basic_string& operator=(const_pointer s);
114 basic_string& operator=(value_type c);
115 basic_string& operator=(initializer_list<value_type>);
116
117 iterator begin();
118 const_iterator begin() const;
119 iterator end();
120 const_iterator end() const;
121
122 reverse_iterator rbegin();
123 const_reverse_iterator rbegin() const;
124 reverse_iterator rend();
125 const_reverse_iterator rend() const;
126
127 const_iterator cbegin() const;
128 const_iterator cend() const;
129 const_reverse_iterator crbegin() const;
130 const_reverse_iterator crend() const;
131
132 size_type size() const;
133 size_type length() const;
134 size_type max_size() const;
135 size_type capacity() const;
136
137 void resize(size_type n, value_type c);
138 void resize(size_type n);
139
140 void reserve(size_type res_arg = 0);
141 void shrink_to_fit();
142 void clear();
143 bool empty() const;
144
145 const_reference operator[](size_type pos) const;
146 reference operator[](size_type pos);
147
148 const_reference at(size_type n) const;
149 reference at(size_type n);
150
151 basic_string& operator+=(const basic_string& str);
152 basic_string& operator+=(const_pointer s);
153 basic_string& operator+=(value_type c);
154 basic_string& operator+=(initializer_list<value_type>);
155
156 basic_string& append(const basic_string& str);
157 basic_string& append(const basic_string& str, size_type pos, size_type n);
158 basic_string& append(const_pointer s, size_type n);
159 basic_string& append(const_pointer s);
160 basic_string& append(size_type n, value_type c);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000161 template<class InputIterator>
162 basic_string& append(InputIterator first, InputIterator last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000163 basic_string& append(initializer_list<value_type>);
164
165 void push_back(value_type c);
166 void pop_back();
167 reference front();
168 const_reference front() const;
169 reference back();
170 const_reference back() const;
171
172 basic_string& assign(const basic_string& str);
173 basic_string& assign(const basic_string& str, size_type pos, size_type n);
174 basic_string& assign(const_pointer s, size_type n);
175 basic_string& assign(const_pointer s);
176 basic_string& assign(size_type n, value_type c);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000177 template<class InputIterator>
178 basic_string& assign(InputIterator first, InputIterator last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000179 basic_string& assign(initializer_list<value_type>);
180
181 basic_string& insert(size_type pos1, const basic_string& str);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000182 basic_string& insert(size_type pos1, const basic_string& str,
183 size_type pos2, size_type n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000184 basic_string& insert(size_type pos, const_pointer s, size_type n);
185 basic_string& insert(size_type pos, const_pointer s);
186 basic_string& insert(size_type pos, size_type n, value_type c);
187 iterator insert(const_iterator p, value_type c);
188 iterator insert(const_iterator p, size_type n, value_type c);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000189 template<class InputIterator>
190 iterator insert(const_iterator p, InputIterator first, InputIterator last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000191 iterator insert(const_iterator p, initializer_list<value_type>);
192
193 basic_string& erase(size_type pos = 0, size_type n = npos);
194 iterator erase(const_iterator position);
195 iterator erase(const_iterator first, const_iterator last);
196
197 basic_string& replace(size_type pos1, size_type n1, const basic_string& str);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000198 basic_string& replace(size_type pos1, size_type n1, const basic_string& str,
199 size_type pos2, size_type n2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000200 basic_string& replace(size_type pos, size_type n1, const_pointer s, size_type n2);
201 basic_string& replace(size_type pos, size_type n1, const_pointer s);
202 basic_string& replace(size_type pos, size_type n1, size_type n2, value_type c);
203 basic_string& replace(iterator i1, iterator i2, const basic_string& str);
204 basic_string& replace(iterator i1, iterator i2, const_pointer s, size_type n);
205 basic_string& replace(iterator i1, iterator i2, const_pointer s);
206 basic_string& replace(iterator i1, iterator i2, size_type n, value_type c);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000207 template<class InputIterator>
208 basic_string& replace(iterator i1, iterator i2, InputIterator j1, InputIterator j2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000209 basic_string& replace(iterator i1, iterator i2, initializer_list<value_type>);
210
211 size_type copy(pointer s, size_type n, size_type pos = 0) const;
212 basic_string substr(size_type pos = 0, size_type n = npos) const;
213
214 void swap(basic_string& str);
215
216 const_pointer c_str() const;
217 const_pointer data() const;
218
219 allocator_type get_allocator() const;
220
221 size_type find(const basic_string& str, size_type pos = 0) const;
222 size_type find(const_pointer s, size_type pos, size_type n) const;
223 size_type find(const_pointer s, size_type pos = 0) const;
224 size_type find(value_type c, size_type pos = 0) const;
225
226 size_type rfind(const basic_string& str, size_type pos = npos) const;
227 size_type rfind(const_pointer s, size_type pos, size_type n) const;
228 size_type rfind(const_pointer s, size_type pos = npos) const;
229 size_type rfind(value_type c, size_type pos = npos) const;
230
231 size_type find_first_of(const basic_string& str, size_type pos = 0) const;
232 size_type find_first_of(const_pointer s, size_type pos, size_type n) const;
233 size_type find_first_of(const_pointer s, size_type pos = 0) const;
234 size_type find_first_of(value_type c, size_type pos = 0) const;
235
236 size_type find_last_of(const basic_string& str, size_type pos = npos) const;
237 size_type find_last_of(const_pointer s, size_type pos, size_type n) const;
238 size_type find_last_of(const_pointer s, size_type pos = npos) const;
239 size_type find_last_of(value_type c, size_type pos = npos) const;
240
241 size_type find_first_not_of(const basic_string& str, size_type pos = 0) const;
242 size_type find_first_not_of(const_pointer s, size_type pos, size_type n) const;
243 size_type find_first_not_of(const_pointer s, size_type pos = 0) const;
244 size_type find_first_not_of(value_type c, size_type pos = 0) const;
245
246 size_type find_last_not_of(const basic_string& str, size_type pos = npos) const;
247 size_type find_last_not_of(const_pointer s, size_type pos, size_type n) const;
248 size_type find_last_not_of(const_pointer s, size_type pos = npos) const;
249 size_type find_last_not_of(value_type c, size_type pos = npos) const;
250
251 int compare(const basic_string& str) const;
252 int compare(size_type pos1, size_type n1, const basic_string& str) const;
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000253 int compare(size_type pos1, size_type n1, const basic_string& str,
254 size_type pos2, size_type n2) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000255 int compare(const_pointer s) const;
256 int compare(size_type pos1, size_type n1, const_pointer s) const;
257 int compare(size_type pos1, size_type n1, const_pointer s, size_type n2) const;
258
259 bool __invariants() const;
260};
261
262template<class charT, class traits, class Allocator>
263basic_string<charT, traits, Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000264operator+(const basic_string<charT, traits, Allocator>& lhs,
265 const basic_string<charT, traits, Allocator>& rhs);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000266
267template<class charT, class traits, class Allocator>
268basic_string<charT, traits, Allocator>
269operator+(const charT* lhs , const basic_string<charT,traits,Allocator>&rhs);
270
271template<class charT, class traits, class Allocator>
272basic_string<charT, traits, Allocator>
273operator+(charT lhs, const basic_string<charT,traits,Allocator>& rhs);
274
275template<class charT, class traits, class Allocator>
276basic_string<charT, traits, Allocator>
277operator+(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs);
278
279template<class charT, class traits, class Allocator>
280basic_string<charT, traits, Allocator>
281operator+(const basic_string<charT, traits, Allocator>& lhs, charT rhs);
282
283template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000284bool operator==(const basic_string<charT, traits, Allocator>& lhs,
285 const basic_string<charT, traits, Allocator>& rhs);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000286
287template<class charT, class traits, class Allocator>
288bool operator==(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs);
289
290template<class charT, class traits, class Allocator>
291bool operator==(const basic_string<charT,traits,Allocator>& lhs, const charT* rhs);
292
293template<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>
356basic_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>
445class fpos
446{
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>
482struct char_traits
483{
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 <>
594struct char_traits<char>
595{
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 <>
631struct char_traits<wchar_t>
632{
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 <>
668struct char_traits<char16_t>
669{
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 <>
774struct char_traits<char32_t>
775{
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
879#endif
880
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
935template<class _CharT, class _Traits, class _Allocator>
936class basic_string
937 : 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;
944 typedef typename allocator_type::size_type size_type;
945 typedef typename allocator_type::difference_type difference_type;
946 typedef typename allocator_type::reference reference;
947 typedef typename allocator_type::const_reference const_reference;
948 typedef typename allocator_type::pointer pointer;
949 typedef typename allocator_type::const_pointer const_pointer;
950#ifdef _LIBCPP_DEBUG
951 typedef __debug_iter<basic_string, pointer> iterator;
952 typedef __debug_iter<basic_string, const_pointer> const_iterator;
953
954 friend class __debug_iter<basic_string, pointer>;
955 friend class __debug_iter<basic_string, const_pointer>;
956#elif defined(_LIBCPP_RAW_ITERATORS)
957 typedef pointer iterator;
958 typedef const_pointer const_iterator;
959#else
960 typedef __wrap_iter<pointer> iterator;
961 typedef __wrap_iter<const_pointer> const_iterator;
962#endif
963 typedef _STD::reverse_iterator<iterator> reverse_iterator;
964 typedef _STD::reverse_iterator<const_iterator> const_reverse_iterator;
965
966private:
967 struct __long
968 {
969 size_type __cap_;
970 size_type __size_;
971 pointer __data_;
972 };
973
974#if _LIBCPP_BIG_ENDIAN
975 enum {__short_mask = 0x80};
976 enum {__long_mask = ~(size_type(~0) >> 1)};
977#else
978 enum {__short_mask = 0x01};
979 enum {__long_mask = 0x1};
980#endif
981
982 enum {__mask = size_type(~0) >> 1};
983
984 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
985 (sizeof(__long) - 1)/sizeof(value_type) : 2};
986
987 struct __short
988 {
989 union
990 {
991 unsigned char __size_;
992 value_type _;
993 };
994 value_type __data_[__min_cap];
995 };
996
997 union _{__long _; __short __;};
998
999 enum {__n_words = sizeof(_) / sizeof(size_type)};
1000
1001 struct __raw
1002 {
1003 size_type __words[__n_words];
1004 };
1005
1006 struct __rep
1007 {
1008 union
1009 {
1010 __long __l;
1011 __short __s;
1012 __raw __r;
1013 };
1014 };
1015
1016 __compressed_pair<__rep, allocator_type> __r_;
1017
1018#ifdef _LIBCPP_DEBUG
1019
1020 pair<iterator*, const_iterator*> __iterator_list_;
1021
1022 _LIBCPP_INLINE_VISIBILITY iterator*& __get_iterator_list(iterator*) {return __iterator_list_.first;}
1023 _LIBCPP_INLINE_VISIBILITY const_iterator*& __get_iterator_list(const_iterator*) {return __iterator_list_.second;}
1024
1025#endif // _LIBCPP_DEBUG
1026
1027public:
1028 static const size_type npos = -1;
1029
1030 basic_string();
1031 explicit basic_string(const allocator_type& __a);
1032 basic_string(const basic_string& __str);
1033 basic_string(const basic_string& __str, const allocator_type& __a);
1034#ifdef _LIBCPP_MOVE
1035 basic_string(basic_string&& __str);
1036 basic_string(basic_string&& __str, const allocator_type& __a);
1037#endif
1038 basic_string(const_pointer __s);
1039 basic_string(const_pointer __s, const allocator_type& __a);
1040 basic_string(const_pointer __s, size_type __n);
1041 basic_string(const_pointer __s, size_type __n, const allocator_type& __a);
1042 basic_string(size_type __n, value_type __c);
1043 basic_string(size_type __n, value_type __c, const allocator_type& __a);
1044 basic_string(const basic_string& __str, size_type __pos, size_type __n = npos,
1045 const allocator_type& __a = allocator_type());
1046 template<class _InputIterator>
1047 basic_string(_InputIterator __first, _InputIterator __last);
1048 template<class _InputIterator>
1049 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
1050 basic_string(initializer_list<value_type> __il);
1051 basic_string(initializer_list<value_type> __il, const allocator_type& __a);
1052
1053 ~basic_string();
1054
1055 _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const basic_string& __str) {return assign(__str);}
1056#ifdef _LIBCPP_MOVE
1057 _LIBCPP_INLINE_VISIBILITY basic_string& operator=(basic_string&& __str) {swap(__str); return *this;}
1058#endif
1059 _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const_pointer __s) {return assign(__s);}
1060 basic_string& operator=(value_type __c);
1061 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
1062
1063#ifndef _LIBCPP_DEBUG
1064 _LIBCPP_INLINE_VISIBILITY iterator begin() {return iterator(__get_pointer());}
1065 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const {return const_iterator(data());}
1066 _LIBCPP_INLINE_VISIBILITY iterator end() {return iterator(__get_pointer() + size());}
1067 _LIBCPP_INLINE_VISIBILITY const_iterator end() const {return const_iterator(data() + size());}
1068#else // _LIBCPP_DEBUG
1069 _LIBCPP_INLINE_VISIBILITY iterator begin() {return iterator(this, __get_pointer());}
1070 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const {return const_iterator(this, data());}
1071 _LIBCPP_INLINE_VISIBILITY iterator end() {return iterator(this, __get_pointer() + size());}
1072 _LIBCPP_INLINE_VISIBILITY const_iterator end() const {return const_iterator(this, data() + size());}
1073#endif // _LIBCPP_DEBUG
1074 _LIBCPP_INLINE_VISIBILITY reverse_iterator rbegin() {return reverse_iterator(end());}
1075 _LIBCPP_INLINE_VISIBILITY const_reverse_iterator rbegin() const {return const_reverse_iterator(end());}
1076 _LIBCPP_INLINE_VISIBILITY reverse_iterator rend() {return reverse_iterator(begin());}
1077 _LIBCPP_INLINE_VISIBILITY const_reverse_iterator rend() const {return const_reverse_iterator(begin());}
1078
1079 _LIBCPP_INLINE_VISIBILITY const_iterator cbegin() const {return begin();}
1080 _LIBCPP_INLINE_VISIBILITY const_iterator cend() const {return end();}
1081 _LIBCPP_INLINE_VISIBILITY const_reverse_iterator crbegin() const {return rbegin();}
1082 _LIBCPP_INLINE_VISIBILITY const_reverse_iterator crend() const {return rend();}
1083
1084 _LIBCPP_INLINE_VISIBILITY size_type size() const
1085 {return __is_long() ? __get_long_size() : __get_short_size();}
1086 _LIBCPP_INLINE_VISIBILITY size_type length() const {return size();}
1087 size_type max_size() const;
1088 _LIBCPP_INLINE_VISIBILITY size_type capacity() const
1089 {return (__is_long() ? __get_long_cap() : __min_cap) - 1;}
1090
1091 void resize(size_type __n, value_type __c);
1092 _LIBCPP_INLINE_VISIBILITY void resize(size_type __n) {resize(__n, value_type());}
1093
1094 void reserve(size_type res_arg = 0);
1095 void shrink_to_fit() {reserve();}
1096 void clear();
1097 _LIBCPP_INLINE_VISIBILITY bool empty() const {return size() == 0;}
1098
1099 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __pos) const;
1100 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __pos);
1101
1102 const_reference at(size_type __n) const;
1103 reference at(size_type __n);
1104
1105 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const basic_string& __str) {return append(__str);}
1106 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const_pointer __s) {return append(__s);}
1107 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(value_type __c) {push_back(__c); return *this;}
1108 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(initializer_list<value_type> __il) {return append(__il);}
1109
1110 basic_string& append(const basic_string& __str);
1111 basic_string& append(const basic_string& __str, size_type __pos, size_type __n);
1112 basic_string& append(const_pointer __s, size_type __n);
1113 basic_string& append(const_pointer __s);
1114 basic_string& append(size_type __n, value_type __c);
1115 template<class _InputIterator>
1116 typename enable_if
1117 <
1118 __is_input_iterator <_InputIterator>::value &&
1119 !__is_forward_iterator<_InputIterator>::value,
1120 basic_string&
1121 >::type
1122 append(_InputIterator __first, _InputIterator __last);
1123 template<class _ForwardIterator>
1124 typename enable_if
1125 <
1126 __is_forward_iterator<_ForwardIterator>::value,
1127 basic_string&
1128 >::type
1129 append(_ForwardIterator __first, _ForwardIterator __last);
1130 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
1131
1132 void push_back(value_type __c);
1133 void pop_back();
1134 reference front();
1135 const_reference front() const;
1136 reference back();
1137 const_reference back() const;
1138
1139 basic_string& assign(const basic_string& __str);
1140 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n);
1141 basic_string& assign(const_pointer __s, size_type __n);
1142 basic_string& assign(const_pointer __s);
1143 basic_string& assign(size_type __n, value_type __c);
1144 template<class _InputIterator>
1145 typename enable_if
1146 <
1147 __is_input_iterator <_InputIterator>::value &&
1148 !__is_forward_iterator<_InputIterator>::value,
1149 basic_string&
1150 >::type
1151 assign(_InputIterator __first, _InputIterator __last);
1152 template<class _ForwardIterator>
1153 typename enable_if
1154 <
1155 __is_forward_iterator<_ForwardIterator>::value,
1156 basic_string&
1157 >::type
1158 assign(_ForwardIterator __first, _ForwardIterator __last);
1159 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
1160
1161 basic_string& insert(size_type __pos1, const basic_string& __str);
1162 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n);
1163 basic_string& insert(size_type __pos, const_pointer __s, size_type __n);
1164 basic_string& insert(size_type __pos, const_pointer __s);
1165 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1166 iterator insert(const_iterator __pos, value_type __c);
1167 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1168 template<class _InputIterator>
1169 typename enable_if
1170 <
1171 __is_input_iterator <_InputIterator>::value &&
1172 !__is_forward_iterator<_InputIterator>::value,
1173 iterator
1174 >::type
1175 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1176 template<class _ForwardIterator>
1177 typename enable_if
1178 <
1179 __is_forward_iterator<_ForwardIterator>::value,
1180 iterator
1181 >::type
1182 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
1183 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1184 {return insert(__pos, __il.begin(), __il.end());}
1185
1186 basic_string& erase(size_type __pos = 0, size_type __n = npos);
1187 iterator erase(const_iterator __pos);
1188 iterator erase(const_iterator __first, const_iterator __last);
1189
1190 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
1191 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2);
1192 basic_string& replace(size_type __pos, size_type __n1, const_pointer __s, size_type __n2);
1193 basic_string& replace(size_type __pos, size_type __n1, const_pointer __s);
1194 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
1195 basic_string& replace(iterator __i1, iterator __i2, const basic_string& __str);
1196 basic_string& replace(iterator __i1, iterator __i2, const_pointer __s, size_type __n);
1197 basic_string& replace(iterator __i1, iterator __i2, const_pointer __s);
1198 basic_string& replace(iterator __i1, iterator __i2, size_type __n, value_type __c);
1199 template<class _InputIterator>
1200 typename enable_if
1201 <
1202 __is_input_iterator<_InputIterator>::value,
1203 basic_string&
1204 >::type
1205 replace(iterator __i1, iterator __i2, _InputIterator __j1, _InputIterator __j2);
1206 basic_string& replace(iterator __i1, iterator __i2, initializer_list<value_type> __il)
1207 {return replace(__i1, __i2, __il.begin(), __il.end());}
1208
1209 size_type copy(pointer __s, size_type __n, size_type __pos = 0) const;
1210 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1211
1212 void swap(basic_string& __str);
1213
1214 _LIBCPP_INLINE_VISIBILITY const_pointer c_str() const {return data();}
1215 _LIBCPP_INLINE_VISIBILITY const_pointer data() const {return __get_pointer();}
1216
1217 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const {return __alloc();}
1218
1219 size_type find(const basic_string& __str, size_type __pos = 0) const;
1220 size_type find(const_pointer __s, size_type __pos, size_type __n) const;
1221 size_type find(const_pointer __s, size_type __pos = 0) const;
1222 size_type find(value_type __c, size_type __pos = 0) const;
1223
1224 size_type rfind(const basic_string& __str, size_type __pos = npos) const;
1225 size_type rfind(const_pointer __s, size_type __pos, size_type __n) const;
1226 size_type rfind(const_pointer __s, size_type __pos = npos) const;
1227 size_type rfind(value_type __c, size_type __pos = npos) const;
1228
1229 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const;
1230 size_type find_first_of(const_pointer __s, size_type __pos, size_type __n) const;
1231 size_type find_first_of(const_pointer __s, size_type __pos = 0) const;
1232 size_type find_first_of(value_type __c, size_type __pos = 0) const;
1233
1234 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const;
1235 size_type find_last_of(const_pointer __s, size_type __pos, size_type __n) const;
1236 size_type find_last_of(const_pointer __s, size_type __pos = npos) const;
1237 size_type find_last_of(value_type __c, size_type __pos = npos) const;
1238
1239 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const;
1240 size_type find_first_not_of(const_pointer __s, size_type __pos, size_type __n) const;
1241 size_type find_first_not_of(const_pointer __s, size_type __pos = 0) const;
1242 size_type find_first_not_of(value_type __c, size_type __pos = 0) const;
1243
1244 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const;
1245 size_type find_last_not_of(const_pointer __s, size_type __pos, size_type __n) const;
1246 size_type find_last_not_of(const_pointer __s, size_type __pos = npos) const;
1247 size_type find_last_not_of(value_type __c, size_type __pos = npos) const;
1248
1249 int compare(const basic_string& __str) const;
1250 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
1251 int compare(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2) const;
1252 int compare(const_pointer __s) const;
1253 int compare(size_type __pos1, size_type __n1, const_pointer __s) const;
1254 int compare(size_type __pos1, size_type __n1, const_pointer __s, size_type __n2) const;
1255
1256 bool __invariants() const;
1257private:
1258 _LIBCPP_INLINE_VISIBILITY allocator_type& __alloc() {return __r_.second();}
1259 _LIBCPP_INLINE_VISIBILITY const allocator_type& __alloc() const {return __r_.second();}
1260
1261 _LIBCPP_INLINE_VISIBILITY bool __is_long() const {return bool(__r_.first().__s.__size_ & __short_mask);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001262
1263 _LIBCPP_INLINE_VISIBILITY void __set_short_size(size_type __s)
1264#if _LIBCPP_BIG_ENDIAN
1265 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1266#else
1267 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
1268#endif
1269 _LIBCPP_INLINE_VISIBILITY size_type __get_short_size() const
1270#if _LIBCPP_BIG_ENDIAN
1271 {return __r_.first().__s.__size_;}
1272#else
1273 {return __r_.first().__s.__size_ >> 1;}
1274#endif
1275 _LIBCPP_INLINE_VISIBILITY void __set_long_size(size_type __s) {__r_.first().__l.__size_ = __s;}
1276 _LIBCPP_INLINE_VISIBILITY size_type __get_long_size() const {return __r_.first().__l.__size_;}
1277 _LIBCPP_INLINE_VISIBILITY void __set_size(size_type __s)
1278 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1279
1280 _LIBCPP_INLINE_VISIBILITY void __set_long_cap(size_type __s) {__r_.first().__l.__cap_ = __long_mask | __s;}
1281 _LIBCPP_INLINE_VISIBILITY size_type __get_long_cap() const {return __r_.first().__l.__cap_ & ~__long_mask;}
1282
1283 _LIBCPP_INLINE_VISIBILITY void __set_long_pointer(pointer __p) {__r_.first().__l.__data_ = __p;}
1284 _LIBCPP_INLINE_VISIBILITY pointer __get_long_pointer() {return __r_.first().__l.__data_;}
1285 _LIBCPP_INLINE_VISIBILITY const_pointer __get_long_pointer() const {return __r_.first().__l.__data_;}
1286 _LIBCPP_INLINE_VISIBILITY pointer __get_short_pointer() {return __r_.first().__s.__data_;}
1287 _LIBCPP_INLINE_VISIBILITY const_pointer __get_short_pointer() const {return __r_.first().__s.__data_;}
1288 _LIBCPP_INLINE_VISIBILITY pointer __get_pointer()
1289 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1290 _LIBCPP_INLINE_VISIBILITY const_pointer __get_pointer() const
1291 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1292
1293 _LIBCPP_INLINE_VISIBILITY void __zero()
1294 {
1295 size_type (&__a)[__n_words] = __r_.first().__r.__words;
1296 for (unsigned __i = 0; __i < __n_words; ++__i)
1297 __a[__i] = 0;
1298 }
1299
1300 template <size_type __a> static
1301 _LIBCPP_INLINE_VISIBILITY size_type __align(size_type __s) {return __s + (__a-1) & ~(__a-1);}
1302 enum {__alignment = 16};
1303 static _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __s)
1304 {return (__s < __min_cap ? __min_cap :
1305 __align<sizeof(value_type) < __alignment ? __alignment/sizeof(value_type) : 1>(__s+1)) - 1;}
1306
1307 void __init(const_pointer __s, size_type __sz, size_type __reserve);
1308 void __init(const_pointer __s, size_type __sz);
1309 void __init(size_type __n, value_type __c);
1310
1311 template <class _InputIterator>
1312 typename enable_if
1313 <
1314 __is_input_iterator <_InputIterator>::value &&
1315 !__is_forward_iterator<_InputIterator>::value,
1316 void
1317 >::type
1318 __init(_InputIterator __first, _InputIterator __last);
1319
1320 template <class _ForwardIterator>
1321 typename enable_if
1322 <
1323 __is_forward_iterator<_ForwardIterator>::value,
1324 void
1325 >::type
1326 __init(_ForwardIterator __first, _ForwardIterator __last);
1327
1328 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1329 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
1330 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1331 size_type __n_copy, size_type __n_del,
1332 size_type __n_add, const_pointer __p_new_stuff);
1333
1334 void __erase_to_end(size_type __pos);
1335
1336 void __invalidate_all_iterators();
1337 void __invalidate_iterators_past(size_type);
1338
1339 friend basic_string operator+<>(const basic_string&, const basic_string&);
1340 friend basic_string operator+<>(const value_type*, const basic_string&);
1341 friend basic_string operator+<>(value_type, const basic_string&);
1342 friend basic_string operator+<>(const basic_string&, const value_type*);
1343 friend basic_string operator+<>(const basic_string&, value_type);
1344};
1345
1346template <class _CharT, class _Traits, class _Allocator>
1347#ifndef _LIBCPP_DEBUG
1348_LIBCPP_INLINE_VISIBILITY inline
1349#endif
1350void
1351basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
1352{
1353#ifdef _LIBCPP_DEBUG
1354 iterator::__remove_all(this);
1355 const_iterator::__remove_all(this);
1356#endif
1357}
1358
1359template <class _CharT, class _Traits, class _Allocator>
1360#ifndef _LIBCPP_DEBUG
1361_LIBCPP_INLINE_VISIBILITY inline
1362#endif
1363void
1364basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type __pos)
1365{
1366#ifdef _LIBCPP_DEBUG
1367 const_iterator __beg = begin();
1368 if (__iterator_list_.first)
1369 {
1370 for (iterator* __p = __iterator_list_.first; __p;)
1371 {
1372 if (*__p - __beg > static_cast<difference_type>(__pos))
1373 {
1374 iterator* __n = __p;
1375 __p = __p->__next;
1376 __n->__remove_owner();
1377 }
1378 else
1379 __p = __p->__next;
1380 }
1381 }
1382 if (__iterator_list_.second)
1383 {
1384 for (const_iterator* __p = __iterator_list_.second; __p;)
1385 {
1386 if (*__p - __beg > static_cast<difference_type>(__pos))
1387 {
1388 const_iterator* __n = __p;
1389 __p = __p->__next;
1390 __n->__remove_owner();
1391 }
1392 else
1393 __p = __p->__next;
1394 }
1395 }
1396#endif
1397}
1398
1399template <class _CharT, class _Traits, class _Allocator>
1400_LIBCPP_INLINE_VISIBILITY inline
1401basic_string<_CharT, _Traits, _Allocator>::basic_string()
1402{
1403 __zero();
1404}
1405
1406template <class _CharT, class _Traits, class _Allocator>
1407_LIBCPP_INLINE_VISIBILITY inline
1408basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
1409 : __r_(__a)
1410{
1411 __zero();
1412}
1413
1414template <class _CharT, class _Traits, class _Allocator>
1415void
1416basic_string<_CharT, _Traits, _Allocator>::__init(const_pointer __s, size_type __sz, size_type __reserve)
1417{
1418 if (__reserve > max_size())
1419 this->__throw_length_error();
1420 pointer __p;
1421 if (__reserve < __min_cap)
1422 {
1423 __set_short_size(__sz);
1424 __p = __get_short_pointer();
1425 }
1426 else
1427 {
1428 size_type __cap = __recommend(__reserve);
1429 __p = __alloc().allocate(__cap+1);
1430 __set_long_pointer(__p);
1431 __set_long_cap(__cap+1);
1432 __set_long_size(__sz);
1433 }
1434 traits_type::copy(__p, __s, __sz);
1435 traits_type::assign(__p[__sz], value_type());
1436}
1437
1438template <class _CharT, class _Traits, class _Allocator>
1439void
1440basic_string<_CharT, _Traits, _Allocator>::__init(const_pointer __s, size_type __sz)
1441{
1442 if (__sz > max_size())
1443 this->__throw_length_error();
1444 pointer __p;
1445 if (__sz < __min_cap)
1446 {
1447 __set_short_size(__sz);
1448 __p = __get_short_pointer();
1449 }
1450 else
1451 {
1452 size_type __cap = __recommend(__sz);
1453 __p = __alloc().allocate(__cap+1);
1454 __set_long_pointer(__p);
1455 __set_long_cap(__cap+1);
1456 __set_long_size(__sz);
1457 }
1458 traits_type::copy(__p, __s, __sz);
1459 traits_type::assign(__p[__sz], value_type());
1460}
1461
1462template <class _CharT, class _Traits, class _Allocator>
1463_LIBCPP_INLINE_VISIBILITY inline
1464basic_string<_CharT, _Traits, _Allocator>::basic_string(const_pointer __s)
1465{
1466#ifdef _LIBCPP_DEBUG
1467 assert(__s != 0);
1468#endif
1469 __init(__s, traits_type::length(__s));
1470}
1471
1472template <class _CharT, class _Traits, class _Allocator>
1473_LIBCPP_INLINE_VISIBILITY inline
1474basic_string<_CharT, _Traits, _Allocator>::basic_string(const_pointer __s, const allocator_type& __a)
1475 : __r_(__a)
1476{
1477#ifdef _LIBCPP_DEBUG
1478 assert(__s != 0);
1479#endif
1480 __init(__s, traits_type::length(__s));
1481}
1482
1483template <class _CharT, class _Traits, class _Allocator>
1484_LIBCPP_INLINE_VISIBILITY inline
1485basic_string<_CharT, _Traits, _Allocator>::basic_string(const_pointer __s, size_type __n)
1486{
1487#ifdef _LIBCPP_DEBUG
1488 assert(__s != 0);
1489#endif
1490 __init(__s, __n);
1491}
1492
1493template <class _CharT, class _Traits, class _Allocator>
1494_LIBCPP_INLINE_VISIBILITY inline
1495basic_string<_CharT, _Traits, _Allocator>::basic_string(const_pointer __s, size_type __n, const allocator_type& __a)
1496 : __r_(__a)
1497{
1498#ifdef _LIBCPP_DEBUG
1499 assert(__s != 0);
1500#endif
1501 __init(__s, __n);
1502}
1503
1504template <class _CharT, class _Traits, class _Allocator>
1505basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
1506 : __r_(__str.__alloc())
1507{
1508 if (!__str.__is_long())
1509 __r_.first().__r = __str.__r_.first().__r;
1510 else
1511 __init(__str.__get_long_pointer(), __str.__get_long_size());
1512}
1513
1514template <class _CharT, class _Traits, class _Allocator>
1515basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, const allocator_type& __a)
1516 : __r_(__a)
1517{
1518 if (!__str.__is_long())
1519 __r_.first().__r = __str.__r_.first().__r;
1520 else
1521 __init(__str.__get_long_pointer(), __str.__get_long_size());
1522}
1523
1524#ifdef _LIBCPP_MOVE
1525
1526template <class _CharT, class _Traits, class _Allocator>
1527_LIBCPP_INLINE_VISIBILITY inline
1528basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
1529 : __r_(_STD::move(__str.__r_))
1530{
1531 __str.__zero();
1532#ifdef _LIBCPP_DEBUG
1533 __str.__invalidate_all_iterators();
1534#endif
1535}
1536
1537template <class _CharT, class _Traits, class _Allocator>
1538_LIBCPP_INLINE_VISIBILITY inline
1539basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
1540 : __r_(__str.__r_.first(), __a)
1541{
1542 __str.__zero();
1543#ifdef _LIBCPP_DEBUG
1544 __str.__invalidate_all_iterators();
1545#endif
1546}
1547
1548#endif // _LIBCPP_MOVE
1549
1550template <class _CharT, class _Traits, class _Allocator>
1551void
1552basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
1553{
1554 if (__n > max_size())
1555 this->__throw_length_error();
1556 pointer __p;
1557 if (__n < __min_cap)
1558 {
1559 __set_short_size(__n);
1560 __p = __get_short_pointer();
1561 }
1562 else
1563 {
1564 size_type __cap = __recommend(__n);
1565 __p = __alloc().allocate(__cap+1);
1566 __set_long_pointer(__p);
1567 __set_long_cap(__cap+1);
1568 __set_long_size(__n);
1569 }
1570 traits_type::assign(__p, __n, __c);
1571 traits_type::assign(__p[__n], value_type());
1572}
1573
1574template <class _CharT, class _Traits, class _Allocator>
1575_LIBCPP_INLINE_VISIBILITY inline
1576basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, value_type __c)
1577{
1578 __init(__n, __c);
1579}
1580
1581template <class _CharT, class _Traits, class _Allocator>
1582_LIBCPP_INLINE_VISIBILITY inline
1583basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, value_type __c, const allocator_type& __a)
1584 : __r_(__a)
1585{
1586 __init(__n, __c);
1587}
1588
1589
1590template <class _CharT, class _Traits, class _Allocator>
1591basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos, size_type __n,
1592 const allocator_type& __a)
1593 : __r_(__a)
1594{
1595 size_type __str_sz = __str.size();
1596 if (__pos > __str_sz)
1597 this->__throw_out_of_range();
1598 __init(__str.data() + __pos, _STD::min(__n, __str_sz - __pos));
1599}
1600
1601template <class _CharT, class _Traits, class _Allocator>
1602template <class _InputIterator>
1603typename enable_if
1604<
1605 __is_input_iterator <_InputIterator>::value &&
1606 !__is_forward_iterator<_InputIterator>::value,
1607 void
1608>::type
1609basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
1610{
1611 __zero();
1612#ifndef _LIBCPP_NO_EXCEPTIONS
1613 try
1614 {
1615#endif
1616 for (; __first != __last; ++__first)
1617 push_back(*__first);
1618#ifndef _LIBCPP_NO_EXCEPTIONS
1619 }
1620 catch (...)
1621 {
1622 if (__is_long())
1623 __alloc().deallocate(__get_long_pointer(), __get_long_cap());
1624 throw;
1625 }
1626#endif
1627}
1628
1629template <class _CharT, class _Traits, class _Allocator>
1630template <class _ForwardIterator>
1631typename enable_if
1632<
1633 __is_forward_iterator<_ForwardIterator>::value,
1634 void
1635>::type
1636basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
1637{
1638 size_type __sz = static_cast<size_type>(_STD::distance(__first, __last));
1639 if (__sz > max_size())
1640 this->__throw_length_error();
1641 pointer __p;
1642 if (__sz < __min_cap)
1643 {
1644 __set_short_size(__sz);
1645 __p = __get_short_pointer();
1646 }
1647 else
1648 {
1649 size_type __cap = __recommend(__sz);
1650 __p = __alloc().allocate(__cap+1);
1651 __set_long_pointer(__p);
1652 __set_long_cap(__cap+1);
1653 __set_long_size(__sz);
1654 }
1655 for (; __first != __last; ++__first, ++__p)
1656 traits_type::assign(*__p, *__first);
1657 traits_type::assign(*__p, value_type());
1658}
1659
1660template <class _CharT, class _Traits, class _Allocator>
1661template<class _InputIterator>
1662_LIBCPP_INLINE_VISIBILITY inline
1663basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
1664{
1665 __init(__first, __last);
1666}
1667
1668template <class _CharT, class _Traits, class _Allocator>
1669template<class _InputIterator>
1670_LIBCPP_INLINE_VISIBILITY inline
1671basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
1672 const allocator_type& __a)
1673 : __r_(__a)
1674{
1675 __init(__first, __last);
1676}
1677
1678template <class _CharT, class _Traits, class _Allocator>
1679_LIBCPP_INLINE_VISIBILITY inline
1680basic_string<_CharT, _Traits, _Allocator>::basic_string(initializer_list<value_type> __il)
1681{
1682 __init(__il.begin(), __il.end());
1683}
1684
1685template <class _CharT, class _Traits, class _Allocator>
1686_LIBCPP_INLINE_VISIBILITY inline
1687basic_string<_CharT, _Traits, _Allocator>::basic_string(initializer_list<value_type> __il, const allocator_type& __a)
1688 : __r_(__a)
1689{
1690 __init(__il.begin(), __il.end());
1691}
1692
1693template <class _CharT, class _Traits, class _Allocator>
1694_LIBCPP_INLINE_VISIBILITY inline
1695basic_string<_CharT, _Traits, _Allocator>::~basic_string()
1696{
1697 __invalidate_all_iterators();
1698 if (__is_long())
1699 __alloc().deallocate(__get_long_pointer(), __get_long_cap());
1700}
1701
1702template <class _CharT, class _Traits, class _Allocator>
1703void
1704basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
1705 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1706 size_type __n_copy, size_type __n_del, size_type __n_add, const_pointer __p_new_stuff)
1707{
1708 size_type __ms = max_size();
1709 if (__delta_cap > __ms - __old_cap - 1)
1710 this->__throw_length_error();
1711 pointer __old_p = __get_pointer();
1712 size_type __cap = __old_cap < __ms / 2 - __alignment ?
1713 __recommend(_STD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
1714 __ms - 1;
1715 pointer __p = __alloc().allocate(__cap+1);
1716 __invalidate_all_iterators();
1717 if (__n_copy != 0)
1718 traits_type::copy(__p, __old_p, __n_copy);
1719 if (__n_add != 0)
1720 traits_type::copy(__p + __n_copy, __p_new_stuff, __n_add);
1721 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
1722 if (__sec_cp_sz != 0)
1723 traits_type::copy(__p + __n_copy + __n_add, __old_p + __n_copy + __n_del, __sec_cp_sz);
1724 if (__old_cap+1 != __min_cap)
1725 __alloc().deallocate(__old_p, __old_cap+1);
1726 __set_long_pointer(__p);
1727 __set_long_cap(__cap+1);
1728 __old_sz = __n_copy + __n_add + __sec_cp_sz;
1729 __set_long_size(__old_sz);
1730 traits_type::assign(__p[__old_sz], value_type());
1731}
1732
1733template <class _CharT, class _Traits, class _Allocator>
1734void
1735basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1736 size_type __n_copy, size_type __n_del, size_type __n_add)
1737{
1738 size_type __ms = max_size();
1739 if (__delta_cap > __ms - __old_cap - 1)
1740 this->__throw_length_error();
1741 pointer __old_p = __get_pointer();
1742 size_type __cap = __old_cap < __ms / 2 - __alignment ?
1743 __recommend(_STD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
1744 __ms - 1;
1745 pointer __p = __alloc().allocate(__cap+1);
1746 __invalidate_all_iterators();
1747 if (__n_copy != 0)
1748 traits_type::copy(__p, __old_p, __n_copy);
1749 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
1750 if (__sec_cp_sz != 0)
1751 traits_type::copy(__p + __n_copy + __n_add, __old_p + __n_copy + __n_del, __sec_cp_sz);
1752 if (__old_cap+1 != __min_cap)
1753 __alloc().deallocate(__old_p, __old_cap+1);
1754 __set_long_pointer(__p);
1755 __set_long_cap(__cap+1);
1756}
1757
1758// assign
1759
1760template <class _CharT, class _Traits, class _Allocator>
1761basic_string<_CharT, _Traits, _Allocator>&
1762basic_string<_CharT, _Traits, _Allocator>::assign(const_pointer __s, size_type __n)
1763{
1764#ifdef _LIBCPP_DEBUG
1765 assert(__s != 0);
1766#endif
1767 size_type __cap = capacity();
1768 if (__cap >= __n)
1769 {
1770 pointer __p = __get_pointer();
1771 traits_type::move(__p, __s, __n);
1772 traits_type::assign(__p[__n], value_type());
1773 __set_size(__n);
1774 __invalidate_iterators_past(__n);
1775 }
1776 else
1777 {
1778 size_type __sz = size();
1779 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
1780 }
1781 return *this;
1782}
1783
1784template <class _CharT, class _Traits, class _Allocator>
1785basic_string<_CharT, _Traits, _Allocator>&
1786basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
1787{
1788 size_type __cap = capacity();
1789 if (__cap < __n)
1790 {
1791 size_type __sz = size();
1792 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
1793 }
1794 else
1795 __invalidate_iterators_past(__n);
1796 pointer __p = __get_pointer();
1797 traits_type::assign(__p, __n, __c);
1798 traits_type::assign(__p[__n], value_type());
1799 __set_size(__n);
1800 return *this;
1801}
1802
1803template <class _CharT, class _Traits, class _Allocator>
1804basic_string<_CharT, _Traits, _Allocator>&
1805basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
1806{
1807 pointer __p;
1808 if (__is_long())
1809 {
1810 __p = __get_long_pointer();
1811 __set_long_size(1);
1812 }
1813 else
1814 {
1815 __p = __get_short_pointer();
1816 __set_short_size(1);
1817 }
1818 traits_type::assign(*__p, __c);
1819 traits_type::assign(*++__p, value_type());
1820 __invalidate_iterators_past(1);
1821 return *this;
1822}
1823
1824template <class _CharT, class _Traits, class _Allocator>
1825template<class _InputIterator>
1826typename enable_if
1827<
1828 __is_input_iterator <_InputIterator>::value &&
1829 !__is_forward_iterator<_InputIterator>::value,
1830 basic_string<_CharT, _Traits, _Allocator>&
1831>::type
1832basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
1833{
1834 clear();
1835 for (; __first != __last; ++__first)
1836 push_back(*__first);
1837}
1838
1839template <class _CharT, class _Traits, class _Allocator>
1840template<class _ForwardIterator>
1841typename enable_if
1842<
1843 __is_forward_iterator<_ForwardIterator>::value,
1844 basic_string<_CharT, _Traits, _Allocator>&
1845>::type
1846basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
1847{
1848 size_type __n = static_cast<size_type>(_STD::distance(__first, __last));
1849 size_type __cap = capacity();
1850 if (__cap < __n)
1851 {
1852 size_type __sz = size();
1853 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
1854 }
1855 else
1856 __invalidate_iterators_past(__n);
1857 pointer __p = __get_pointer();
1858 for (; __first != __last; ++__first, ++__p)
1859 traits_type::assign(*__p, *__first);
1860 traits_type::assign(*__p, value_type());
1861 __set_size(__n);
1862 return *this;
1863}
1864
1865template <class _CharT, class _Traits, class _Allocator>
1866_LIBCPP_INLINE_VISIBILITY inline
1867basic_string<_CharT, _Traits, _Allocator>&
1868basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str)
1869{
1870 return assign(__str.data(), __str.size());
1871}
1872
1873template <class _CharT, class _Traits, class _Allocator>
1874basic_string<_CharT, _Traits, _Allocator>&
1875basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
1876{
1877 size_type __sz = __str.size();
1878 if (__pos > __sz)
1879 this->__throw_out_of_range();
1880 return assign(__str.data() + __pos, _STD::min(__n, __sz - __pos));
1881}
1882
1883template <class _CharT, class _Traits, class _Allocator>
1884basic_string<_CharT, _Traits, _Allocator>&
1885basic_string<_CharT, _Traits, _Allocator>::assign(const_pointer __s)
1886{
1887#ifdef _LIBCPP_DEBUG
1888 assert(__s != 0);
1889#endif
1890 return assign(__s, traits_type::length(__s));
1891}
1892
1893// append
1894
1895template <class _CharT, class _Traits, class _Allocator>
1896basic_string<_CharT, _Traits, _Allocator>&
1897basic_string<_CharT, _Traits, _Allocator>::append(const_pointer __s, size_type __n)
1898{
1899#ifdef _LIBCPP_DEBUG
1900 assert(__s != 0);
1901#endif
1902 size_type __cap = capacity();
1903 size_type __sz = size();
1904 if (__cap - __sz >= __n)
1905 {
1906 if (__n)
1907 {
1908 pointer __p = __get_pointer();
1909 traits_type::copy(__p + __sz, __s, __n);
1910 __sz += __n;
1911 __set_size(__sz);
1912 traits_type::assign(__p[__sz], value_type());
1913 }
1914 }
1915 else
1916 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
1917 return *this;
1918}
1919
1920template <class _CharT, class _Traits, class _Allocator>
1921basic_string<_CharT, _Traits, _Allocator>&
1922basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
1923{
1924 if (__n)
1925 {
1926 size_type __cap = capacity();
1927 size_type __sz = size();
1928 if (__cap - __sz < __n)
1929 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
1930 pointer __p = __get_pointer();
1931 traits_type::assign(__p + __sz, __n, __c);
1932 __sz += __n;
1933 __set_size(__sz);
1934 traits_type::assign(__p[__sz], value_type());
1935 }
1936 return *this;
1937}
1938
1939template <class _CharT, class _Traits, class _Allocator>
1940void
1941basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
1942{
1943 size_type __cap = capacity();
1944 size_type __sz = size();
1945 if (__sz == __cap)
1946 __grow_by(__cap, 1, __sz, __sz, 0);
1947 pointer __p = __get_pointer() + __sz;
1948 traits_type::assign(*__p, __c);
1949 traits_type::assign(*++__p, value_type());
1950 __set_size(__sz+1);
1951}
1952
1953template <class _CharT, class _Traits, class _Allocator>
1954template<class _InputIterator>
1955typename enable_if
1956<
1957 __is_input_iterator <_InputIterator>::value &&
1958 !__is_forward_iterator<_InputIterator>::value,
1959 basic_string<_CharT, _Traits, _Allocator>&
1960>::type
1961basic_string<_CharT, _Traits, _Allocator>::append(_InputIterator __first, _InputIterator __last)
1962{
1963 for (; __first != __last; ++__first)
1964 push_back(*__first);
1965 return *this;
1966}
1967
1968template <class _CharT, class _Traits, class _Allocator>
1969template<class _ForwardIterator>
1970typename enable_if
1971<
1972 __is_forward_iterator<_ForwardIterator>::value,
1973 basic_string<_CharT, _Traits, _Allocator>&
1974>::type
1975basic_string<_CharT, _Traits, _Allocator>::append(_ForwardIterator __first, _ForwardIterator __last)
1976{
1977 size_type __sz = size();
1978 size_type __cap = capacity();
1979 size_type __n = static_cast<size_type>(_STD::distance(__first, __last));
1980 if (__n)
1981 {
1982 if (__cap - __sz < __n)
1983 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
1984 pointer __p = __get_pointer() + __sz;
1985 for (; __first != __last; ++__p, ++__first)
1986 traits_type::assign(*__p, *__first);
1987 traits_type::assign(*__p, value_type());
1988 __set_size(__sz + __n);
1989 }
1990 return *this;
1991}
1992
1993template <class _CharT, class _Traits, class _Allocator>
1994_LIBCPP_INLINE_VISIBILITY inline
1995basic_string<_CharT, _Traits, _Allocator>&
1996basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
1997{
1998 return append(__str.data(), __str.size());
1999}
2000
2001template <class _CharT, class _Traits, class _Allocator>
2002basic_string<_CharT, _Traits, _Allocator>&
2003basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2004{
2005 size_type __sz = __str.size();
2006 if (__pos > __sz)
2007 this->__throw_out_of_range();
2008 return append(__str.data() + __pos, _STD::min(__n, __sz - __pos));
2009}
2010
2011template <class _CharT, class _Traits, class _Allocator>
2012basic_string<_CharT, _Traits, _Allocator>&
2013basic_string<_CharT, _Traits, _Allocator>::append(const_pointer __s)
2014{
2015#ifdef _LIBCPP_DEBUG
2016 assert(__s != 0);
2017#endif
2018 return append(__s, traits_type::length(__s));
2019}
2020
2021// insert
2022
2023template <class _CharT, class _Traits, class _Allocator>
2024basic_string<_CharT, _Traits, _Allocator>&
2025basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const_pointer __s, size_type __n)
2026{
2027#ifdef _LIBCPP_DEBUG
2028 assert(__s != 0);
2029#endif
2030 size_type __sz = size();
2031 if (__pos > __sz)
2032 this->__throw_out_of_range();
2033 size_type __cap = capacity();
2034 if (__cap - __sz >= __n)
2035 {
2036 if (__n)
2037 {
2038 pointer __p = __get_pointer();
2039 size_type __n_move = __sz - __pos;
2040 if (__n_move != 0)
2041 {
2042 if (__p + __pos <= __s && __s < __p + __sz)
2043 __s += __n;
2044 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2045 }
2046 traits_type::move(__p + __pos, __s, __n);
2047 __sz += __n;
2048 __set_size(__sz);
2049 traits_type::assign(__p[__sz], value_type());
2050 }
2051 }
2052 else
2053 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2054 return *this;
2055}
2056
2057template <class _CharT, class _Traits, class _Allocator>
2058basic_string<_CharT, _Traits, _Allocator>&
2059basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2060{
2061 size_type __sz = size();
2062 if (__pos > __sz)
2063 this->__throw_out_of_range();
2064 if (__n)
2065 {
2066 size_type __cap = capacity();
2067 pointer __p;
2068 if (__cap - __sz >= __n)
2069 {
2070 __p = __get_pointer();
2071 size_type __n_move = __sz - __pos;
2072 if (__n_move != 0)
2073 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2074 }
2075 else
2076 {
2077 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
2078 __p = __get_long_pointer();
2079 }
2080 traits_type::assign(__p + __pos, __n, __c);
2081 __sz += __n;
2082 __set_size(__sz);
2083 traits_type::assign(__p[__sz], value_type());
2084 }
2085 return *this;
2086}
2087
2088template <class _CharT, class _Traits, class _Allocator>
2089template<class _InputIterator>
2090typename enable_if
2091<
2092 __is_input_iterator <_InputIterator>::value &&
2093 !__is_forward_iterator<_InputIterator>::value,
2094 typename basic_string<_CharT, _Traits, _Allocator>::iterator
2095>::type
2096basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2097{
2098 size_type __old_sz = size();
2099 difference_type __ip = __pos - begin();
2100 for (; __first != __last; ++__first)
2101 push_back(*__first);
2102 pointer __p = __get_pointer();
2103 _STD::rotate(__p + __ip, __p + __old_sz, __p + size());
2104 return iterator(__p + __ip);
2105}
2106
2107template <class _CharT, class _Traits, class _Allocator>
2108template<class _ForwardIterator>
2109typename enable_if
2110<
2111 __is_forward_iterator<_ForwardIterator>::value,
2112 typename basic_string<_CharT, _Traits, _Allocator>::iterator
2113>::type
2114basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2115{
2116 size_type __ip = static_cast<size_type>(__pos - begin());
2117 size_type __sz = size();
2118 size_type __cap = capacity();
2119 size_type __n = static_cast<size_type>(_STD::distance(__first, __last));
2120 if (__n)
2121 {
2122 pointer __p;
2123 if (__cap - __sz >= __n)
2124 {
2125 __p = __get_pointer();
2126 size_type __n_move = __sz - __ip;
2127 if (__n_move != 0)
2128 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
2129 }
2130 else
2131 {
2132 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
2133 __p = __get_long_pointer();
2134 }
2135 __sz += __n;
2136 __set_size(__sz);
2137 traits_type::assign(__p[__sz], value_type());
2138 for (__p += __ip; __first != __last; ++__p, ++__first)
2139 traits_type::assign(*__p, *__first);
2140 }
2141 return begin() + __ip;
2142}
2143
2144template <class _CharT, class _Traits, class _Allocator>
2145_LIBCPP_INLINE_VISIBILITY inline
2146basic_string<_CharT, _Traits, _Allocator>&
2147basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2148{
2149 return insert(__pos1, __str.data(), __str.size());
2150}
2151
2152template <class _CharT, class _Traits, class _Allocator>
2153basic_string<_CharT, _Traits, _Allocator>&
2154basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2155 size_type __pos2, size_type __n)
2156{
2157 size_type __str_sz = __str.size();
2158 if (__pos2 > __str_sz)
2159 this->__throw_out_of_range();
2160 return insert(__pos1, __str.data() + __pos2, _STD::min(__n, __str_sz - __pos2));
2161}
2162
2163template <class _CharT, class _Traits, class _Allocator>
2164basic_string<_CharT, _Traits, _Allocator>&
2165basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const_pointer __s)
2166{
2167#ifdef _LIBCPP_DEBUG
2168 assert(__s != 0);
2169#endif
2170 return insert(__pos, __s, traits_type::length(__s));
2171}
2172
2173template <class _CharT, class _Traits, class _Allocator>
2174typename basic_string<_CharT, _Traits, _Allocator>::iterator
2175basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2176{
2177 size_type __ip = static_cast<size_type>(__pos - begin());
2178 size_type __sz = size();
2179 size_type __cap = capacity();
2180 pointer __p;
2181 if (__cap == __sz)
2182 {
2183 __grow_by(__cap, 1, __sz, __ip, 0, 1);
2184 __p = __get_long_pointer();
2185 }
2186 else
2187 {
2188 __p = __get_pointer();
2189 size_type __n_move = __sz - __ip;
2190 if (__n_move != 0)
2191 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2192 }
2193 traits_type::assign(__p[__ip], __c);
2194 traits_type::assign(__p[++__sz], value_type());
2195 __set_size(__sz);
2196 return begin() + static_cast<difference_type>(__ip);
2197}
2198
2199template <class _CharT, class _Traits, class _Allocator>
2200_LIBCPP_INLINE_VISIBILITY inline
2201typename basic_string<_CharT, _Traits, _Allocator>::iterator
2202basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2203{
2204 difference_type __p = __pos - begin();
2205 insert(static_cast<size_type>(__p), __n, __c);
2206 return begin() + __p;
2207}
2208
2209// replace
2210
2211template <class _CharT, class _Traits, class _Allocator>
2212basic_string<_CharT, _Traits, _Allocator>&
2213basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const_pointer __s, size_type __n2)
2214{
2215#ifdef _LIBCPP_DEBUG
2216 assert(__s != 0);
2217#endif
2218 size_type __sz = size();
2219 if (__pos > __sz)
2220 this->__throw_out_of_range();
2221 __n1 = _STD::min(__n1, __sz - __pos);
2222 size_type __cap = capacity();
2223 if (__cap - __sz + __n1 >= __n2)
2224 {
2225 pointer __p = __get_pointer();
2226 if (__n1 != __n2)
2227 {
2228 size_type __n_move = __sz - __pos - __n1;
2229 if (__n_move != 0)
2230 {
2231 if (__n1 > __n2)
2232 {
2233 traits_type::move(__p + __pos, __s, __n2);
2234 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2235 goto __finish;
2236 }
2237 if (__p + __pos < __s && __s < __p + __sz)
2238 {
2239 if (__p + __pos + __n1 <= __s)
2240 __s += __n2 - __n1;
2241 else // __p + __pos < __s < __p + __pos + __n1
2242 {
2243 traits_type::move(__p + __pos, __s, __n1);
2244 __pos += __n1;
2245 __s += __n2;
2246 __n2 -= __n1;
2247 __n1 = 0;
2248 }
2249 }
2250 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2251 }
2252 }
2253 traits_type::move(__p + __pos, __s, __n2);
2254__finish:
2255 __sz += __n2 - __n1;
2256 __set_size(__sz);
2257 __invalidate_iterators_past(__sz);
2258 traits_type::assign(__p[__sz], value_type());
2259 }
2260 else
2261 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
2262 return *this;
2263}
2264
2265template <class _CharT, class _Traits, class _Allocator>
2266basic_string<_CharT, _Traits, _Allocator>&
2267basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
2268{
2269 size_type __sz = size();
2270 if (__pos > __sz)
2271 this->__throw_out_of_range();
2272 __n1 = _STD::min(__n1, __sz - __pos);
2273 size_type __cap = capacity();
2274 pointer __p;
2275 if (__cap - __sz + __n1 >= __n2)
2276 {
2277 __p = __get_pointer();
2278 if (__n1 != __n2)
2279 {
2280 size_type __n_move = __sz - __pos - __n1;
2281 if (__n_move != 0)
2282 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2283 }
2284 }
2285 else
2286 {
2287 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
2288 __p = __get_long_pointer();
2289 }
2290 traits_type::assign(__p + __pos, __n2, __c);
2291 __sz += __n2 - __n1;
2292 __set_size(__sz);
2293 __invalidate_iterators_past(__sz);
2294 traits_type::assign(__p[__sz], value_type());
2295 return *this;
2296}
2297
2298template <class _CharT, class _Traits, class _Allocator>
2299template<class _InputIterator>
2300typename enable_if
2301<
2302 __is_input_iterator<_InputIterator>::value,
2303 basic_string<_CharT, _Traits, _Allocator>&
2304>::type
2305basic_string<_CharT, _Traits, _Allocator>::replace(iterator __i1, iterator __i2,
2306 _InputIterator __j1, _InputIterator __j2)
2307{
2308 for (; true; ++__i1, ++__j1)
2309 {
2310 if (__i1 == __i2)
2311 {
2312 if (__j1 != __j2)
2313 insert(__i1, __j1, __j2);
2314 break;
2315 }
2316 if (__j1 == __j2)
2317 {
2318 erase(__i1, __i2);
2319 break;
2320 }
2321 traits_type::assign(*__i1, *__j1);
2322 }
2323 return *this;
2324}
2325
2326template <class _CharT, class _Traits, class _Allocator>
2327_LIBCPP_INLINE_VISIBILITY inline
2328basic_string<_CharT, _Traits, _Allocator>&
2329basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
2330{
2331 return replace(__pos1, __n1, __str.data(), __str.size());
2332}
2333
2334template <class _CharT, class _Traits, class _Allocator>
2335basic_string<_CharT, _Traits, _Allocator>&
2336basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
2337 size_type __pos2, size_type __n2)
2338{
2339 size_type __str_sz = __str.size();
2340 if (__pos2 > __str_sz)
2341 this->__throw_out_of_range();
2342 return replace(__pos1, __n1, __str.data() + __pos2, _STD::min(__n2, __str_sz - __pos2));
2343}
2344
2345template <class _CharT, class _Traits, class _Allocator>
2346basic_string<_CharT, _Traits, _Allocator>&
2347basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const_pointer __s)
2348{
2349#ifdef _LIBCPP_DEBUG
2350 assert(__s != 0);
2351#endif
2352 return replace(__pos, __n1, __s, traits_type::length(__s));
2353}
2354
2355template <class _CharT, class _Traits, class _Allocator>
2356_LIBCPP_INLINE_VISIBILITY inline
2357basic_string<_CharT, _Traits, _Allocator>&
2358basic_string<_CharT, _Traits, _Allocator>::replace(iterator __i1, iterator __i2, const basic_string& __str)
2359{
2360 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
2361 __str.data(), __str.size());
2362}
2363
2364template <class _CharT, class _Traits, class _Allocator>
2365_LIBCPP_INLINE_VISIBILITY inline
2366basic_string<_CharT, _Traits, _Allocator>&
2367basic_string<_CharT, _Traits, _Allocator>::replace(iterator __i1, iterator __i2, const_pointer __s, size_type __n)
2368{
2369 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
2370}
2371
2372template <class _CharT, class _Traits, class _Allocator>
2373_LIBCPP_INLINE_VISIBILITY inline
2374basic_string<_CharT, _Traits, _Allocator>&
2375basic_string<_CharT, _Traits, _Allocator>::replace(iterator __i1, iterator __i2, const_pointer __s)
2376{
2377 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
2378}
2379
2380template <class _CharT, class _Traits, class _Allocator>
2381_LIBCPP_INLINE_VISIBILITY inline
2382basic_string<_CharT, _Traits, _Allocator>&
2383basic_string<_CharT, _Traits, _Allocator>::replace(iterator __i1, iterator __i2, size_type __n, value_type __c)
2384{
2385 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
2386}
2387
2388// erase
2389
2390template <class _CharT, class _Traits, class _Allocator>
2391basic_string<_CharT, _Traits, _Allocator>&
2392basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos, size_type __n)
2393{
2394 size_type __sz = size();
2395 if (__pos > __sz)
2396 this->__throw_out_of_range();
2397 if (__n)
2398 {
2399 pointer __p = __get_pointer();
2400 __n = _STD::min(__n, __sz - __pos);
2401 size_type __n_move = __sz - __pos - __n;
2402 if (__n_move != 0)
2403 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
2404 __sz -= __n;
2405 __set_size(__sz);
2406 __invalidate_iterators_past(__sz);
2407 traits_type::assign(__p[__sz], value_type());
2408 }
2409 return *this;
2410}
2411
2412template <class _CharT, class _Traits, class _Allocator>
2413_LIBCPP_INLINE_VISIBILITY inline
2414typename basic_string<_CharT, _Traits, _Allocator>::iterator
2415basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
2416{
2417 iterator __b = begin();
2418 size_type __r = static_cast<size_type>(__pos - __b);
2419 erase(__r, 1);
2420 return __b + __r;
2421}
2422
2423template <class _CharT, class _Traits, class _Allocator>
2424_LIBCPP_INLINE_VISIBILITY inline
2425typename basic_string<_CharT, _Traits, _Allocator>::iterator
2426basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
2427{
2428 iterator __b = begin();
2429 size_type __r = static_cast<size_type>(__first - __b);
2430 erase(__r, static_cast<size_type>(__last - __first));
2431 return __b + __r;
2432}
2433
2434template <class _CharT, class _Traits, class _Allocator>
2435_LIBCPP_INLINE_VISIBILITY inline
2436void
2437basic_string<_CharT, _Traits, _Allocator>::pop_back()
2438{
2439#ifdef _LIBCPP_DEBUG
2440 assert(!empty());
2441#endif
2442 size_type __sz;
2443 if (__is_long())
2444 {
2445 __sz = __get_long_size() - 1;
2446 __set_long_size(__sz);
2447 traits_type::assign(*(__get_long_pointer() + __sz), value_type());
2448 }
2449 else
2450 {
2451 __sz = __get_short_size() - 1;
2452 __set_short_size(__sz);
2453 traits_type::assign(*(__get_short_pointer() + __sz), value_type());
2454 }
2455 __invalidate_iterators_past(__sz);
2456}
2457
2458template <class _CharT, class _Traits, class _Allocator>
2459_LIBCPP_INLINE_VISIBILITY inline
2460void
2461basic_string<_CharT, _Traits, _Allocator>::clear()
2462{
2463 __invalidate_all_iterators();
2464 if (__is_long())
2465 {
2466 traits_type::assign(*__get_long_pointer(), value_type());
2467 __set_long_size(0);
2468 }
2469 else
2470 {
2471 traits_type::assign(*__get_short_pointer(), value_type());
2472 __set_short_size(0);
2473 }
2474}
2475
2476template <class _CharT, class _Traits, class _Allocator>
2477_LIBCPP_INLINE_VISIBILITY inline
2478void
2479basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
2480{
2481 if (__is_long())
2482 {
2483 traits_type::assign(*(__get_long_pointer() + __pos), value_type());
2484 __set_long_size(__pos);
2485 }
2486 else
2487 {
2488 traits_type::assign(*(__get_short_pointer() + __pos), value_type());
2489 __set_short_size(__pos);
2490 }
2491 __invalidate_iterators_past(__pos);
2492}
2493
2494template <class _CharT, class _Traits, class _Allocator>
2495void
2496basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
2497{
2498 size_type __sz = size();
2499 if (__n > __sz)
2500 append(__n - __sz, __c);
2501 else
2502 __erase_to_end(__n);
2503}
2504
2505template <class _CharT, class _Traits, class _Allocator>
2506_LIBCPP_INLINE_VISIBILITY inline
2507typename basic_string<_CharT, _Traits, _Allocator>::size_type
2508basic_string<_CharT, _Traits, _Allocator>::max_size() const
2509{
2510 size_type __m = __alloc().max_size();
2511#if _LIBCPP_BIG_ENDIAN
2512 return (__m <= ~__long_mask ? __m : __m/2) - 1;
2513#else
2514 return __m - 1;
2515#endif
2516}
2517
2518template <class _CharT, class _Traits, class _Allocator>
2519void
2520basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __res_arg)
2521{
2522 if (__res_arg > max_size())
2523 this->__throw_length_error();
2524 size_type __cap = capacity();
2525 size_type __sz = size();
2526 __res_arg = _STD::max(__res_arg, __sz);
2527 __res_arg = __recommend(__res_arg);
2528 if (__res_arg != __cap)
2529 {
2530 pointer __new_data, __p;
2531 bool __was_long, __now_long;
2532 if (__res_arg == __min_cap - 1)
2533 {
2534 __was_long = true;
2535 __now_long = false;
2536 __new_data = __get_short_pointer();
2537 __p = __get_long_pointer();
2538 }
2539 else
2540 {
2541 if (__res_arg > __cap)
2542 __new_data = __alloc().allocate(__res_arg+1);
2543 else
2544 {
2545 #ifndef _LIBCPP_NO_EXCEPTIONS
2546 try
2547 {
2548 #endif
2549 __new_data = __alloc().allocate(__res_arg+1);
2550 #ifndef _LIBCPP_NO_EXCEPTIONS
2551 }
2552 catch (...)
2553 {
2554 return;
2555 }
2556 #else
2557 if (__new_data == 0)
2558 return;
2559 #endif
2560 }
2561 __now_long = true;
2562 __was_long = __is_long();
2563 __p = __get_pointer();
2564 }
2565 traits_type::copy(__new_data, __p, size()+1);
2566 if (__was_long)
2567 __alloc().deallocate(__p, __cap+1);
2568 if (__now_long)
2569 {
2570 __set_long_cap(__res_arg+1);
2571 __set_long_size(__sz);
2572 __set_long_pointer(__new_data);
2573 }
2574 else
2575 __set_short_size(__sz);
2576 __invalidate_all_iterators();
2577 }
2578}
2579
2580template <class _CharT, class _Traits, class _Allocator>
2581_LIBCPP_INLINE_VISIBILITY inline
2582typename basic_string<_CharT, _Traits, _Allocator>::const_reference
2583basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const
2584{
2585#ifdef __LIBCPP_DEBUG
2586 assert(__pos <= size());
2587#endif
2588 return *(data() + __pos);
2589}
2590
2591template <class _CharT, class _Traits, class _Allocator>
2592_LIBCPP_INLINE_VISIBILITY inline
2593typename basic_string<_CharT, _Traits, _Allocator>::reference
2594basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos)
2595{
2596#ifdef __LIBCPP_DEBUG
2597 assert(__pos < size());
2598#endif
2599 return *(__get_pointer() + __pos);
2600}
2601
2602template <class _CharT, class _Traits, class _Allocator>
2603typename basic_string<_CharT, _Traits, _Allocator>::const_reference
2604basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
2605{
2606 if (__n >= size())
2607 this->__throw_out_of_range();
2608 return (*this)[__n];
2609}
2610
2611template <class _CharT, class _Traits, class _Allocator>
2612typename basic_string<_CharT, _Traits, _Allocator>::reference
2613basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
2614{
2615 if (__n >= size())
2616 this->__throw_out_of_range();
2617 return (*this)[__n];
2618}
2619
2620template <class _CharT, class _Traits, class _Allocator>
2621_LIBCPP_INLINE_VISIBILITY inline
2622typename basic_string<_CharT, _Traits, _Allocator>::reference
2623basic_string<_CharT, _Traits, _Allocator>::front()
2624{
2625#ifdef _LIBCPP_DEBUG
2626 assert(!empty());
2627#endif
2628 return *__get_pointer();
2629}
2630
2631template <class _CharT, class _Traits, class _Allocator>
2632_LIBCPP_INLINE_VISIBILITY inline
2633typename basic_string<_CharT, _Traits, _Allocator>::const_reference
2634basic_string<_CharT, _Traits, _Allocator>::front() const
2635{
2636#ifdef _LIBCPP_DEBUG
2637 assert(!empty());
2638#endif
2639 return *data();
2640}
2641
2642template <class _CharT, class _Traits, class _Allocator>
2643_LIBCPP_INLINE_VISIBILITY inline
2644typename basic_string<_CharT, _Traits, _Allocator>::reference
2645basic_string<_CharT, _Traits, _Allocator>::back()
2646{
2647#ifdef _LIBCPP_DEBUG
2648 assert(!empty());
2649#endif
2650 return *(__get_pointer() + size() - 1);
2651}
2652
2653template <class _CharT, class _Traits, class _Allocator>
2654_LIBCPP_INLINE_VISIBILITY inline
2655typename basic_string<_CharT, _Traits, _Allocator>::const_reference
2656basic_string<_CharT, _Traits, _Allocator>::back() const
2657{
2658#ifdef _LIBCPP_DEBUG
2659 assert(!empty());
2660#endif
2661 return *(data() + size() - 1);
2662}
2663
2664template <class _CharT, class _Traits, class _Allocator>
2665typename basic_string<_CharT, _Traits, _Allocator>::size_type
2666basic_string<_CharT, _Traits, _Allocator>::copy(pointer __s, size_type __n, size_type __pos) const
2667{
2668 size_type __sz = size();
2669 if (__pos > __sz)
2670 this->__throw_out_of_range();
2671 size_type __rlen = _STD::min(__n, __sz - __pos);
2672 traits_type::copy(__s, data() + __pos, __rlen);
2673 return __rlen;
2674}
2675
2676template <class _CharT, class _Traits, class _Allocator>
2677_LIBCPP_INLINE_VISIBILITY inline
2678basic_string<_CharT, _Traits, _Allocator>
2679basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
2680{
2681 return basic_string(*this, __pos, __n, __alloc());
2682}
2683
2684template <class _CharT, class _Traits, class _Allocator>
2685_LIBCPP_INLINE_VISIBILITY inline
2686void
2687basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
2688{
2689 __r_.swap(__str.__r_);
2690#ifdef _LIBCPP_DEBUG
2691 __invalidate_all_iterators();
2692 __str.__invalidate_all_iterators();
2693#endif
2694}
2695
2696// find
2697
2698template <class _Traits>
2699struct _LIBCPP_HIDDEN __traits_eq
2700{
2701 typedef typename _Traits::char_type char_type;
2702 _LIBCPP_INLINE_VISIBILITY bool operator()(const char_type& __x, const char_type& __y) {return _Traits::eq(__x, __y);}
2703};
2704
2705template<class _CharT, class _Traits, class _Allocator>
2706typename basic_string<_CharT, _Traits, _Allocator>::size_type
2707basic_string<_CharT, _Traits, _Allocator>::find(const_pointer __s, size_type __pos, size_type __n) const
2708{
2709#ifdef _LIBCPP_DEBUG
2710 assert(__s != 0);
2711#endif
2712 size_type __sz = size();
2713 if (__pos > __sz || __sz - __pos < __n)
2714 return npos;
2715 if (__n == 0)
2716 return __pos;
2717 const_pointer __p = data();
2718 const_pointer __r = _STD::search(__p + __pos, __p + __sz, __s, __s + __n, __traits_eq<traits_type>());
2719 if (__r == __p + __sz)
2720 return npos;
2721 return static_cast<size_type>(__r - __p);
2722}
2723
2724template<class _CharT, class _Traits, class _Allocator>
2725_LIBCPP_INLINE_VISIBILITY inline
2726typename basic_string<_CharT, _Traits, _Allocator>::size_type
2727basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str, size_type __pos) const
2728{
2729 return find(__str.data(), __pos, __str.size());
2730}
2731
2732template<class _CharT, class _Traits, class _Allocator>
2733_LIBCPP_INLINE_VISIBILITY inline
2734typename basic_string<_CharT, _Traits, _Allocator>::size_type
2735basic_string<_CharT, _Traits, _Allocator>::find(const_pointer __s, size_type __pos) const
2736{
2737#ifdef _LIBCPP_DEBUG
2738 assert(__s != 0);
2739#endif
2740 return find(__s, __pos, traits_type::length(__s));
2741}
2742
2743template<class _CharT, class _Traits, class _Allocator>
2744typename basic_string<_CharT, _Traits, _Allocator>::size_type
2745basic_string<_CharT, _Traits, _Allocator>::find(value_type __c, size_type __pos) const
2746{
2747 size_type __sz = size();
2748 if (__pos >= __sz)
2749 return npos;
2750 const_pointer __p = data();
2751 const_pointer __r = traits_type::find(__p + __pos, __sz - __pos, __c);
2752 if (__r == 0)
2753 return npos;
2754 return static_cast<size_type>(__r - __p);
2755}
2756
2757// rfind
2758
2759template<class _CharT, class _Traits, class _Allocator>
2760typename basic_string<_CharT, _Traits, _Allocator>::size_type
2761basic_string<_CharT, _Traits, _Allocator>::rfind(const_pointer __s, size_type __pos, size_type __n) const
2762{
2763#ifdef _LIBCPP_DEBUG
2764 assert(__s != 0);
2765#endif
2766 size_type __sz = size();
2767 __pos = _STD::min(__pos, __sz);
2768 if (__n < __sz - __pos)
2769 __pos += __n;
2770 else
2771 __pos = __sz;
2772 const_pointer __p = data();
2773 const_pointer __r = _STD::find_end(__p, __p + __pos, __s, __s + __n, __traits_eq<traits_type>());
2774 if (__n > 0 && __r == __p + __pos)
2775 return npos;
2776 return static_cast<size_type>(__r - __p);
2777}
2778
2779template<class _CharT, class _Traits, class _Allocator>
2780_LIBCPP_INLINE_VISIBILITY inline
2781typename basic_string<_CharT, _Traits, _Allocator>::size_type
2782basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str, size_type __pos) const
2783{
2784 return rfind(__str.data(), __pos, __str.size());
2785}
2786
2787template<class _CharT, class _Traits, class _Allocator>
2788_LIBCPP_INLINE_VISIBILITY inline
2789typename basic_string<_CharT, _Traits, _Allocator>::size_type
2790basic_string<_CharT, _Traits, _Allocator>::rfind(const_pointer __s, size_type __pos) const
2791{
2792#ifdef _LIBCPP_DEBUG
2793 assert(__s != 0);
2794#endif
2795 return rfind(__s, __pos, traits_type::length(__s));
2796}
2797
2798template<class _CharT, class _Traits, class _Allocator>
2799typename basic_string<_CharT, _Traits, _Allocator>::size_type
2800basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c, size_type __pos) const
2801{
2802 size_type __sz = size();
2803 if (__sz)
2804 {
2805 if (__pos < __sz)
2806 ++__pos;
2807 else
2808 __pos = __sz;
2809 const_pointer __p = data();
2810 for (const_pointer __ps = __p + __pos; __ps != __p;)
2811 {
2812 if (traits_type::eq(*--__ps, __c))
2813 return static_cast<size_type>(__ps - __p);
2814 }
2815 }
2816 return npos;
2817}
2818
2819// find_first_of
2820
2821template<class _CharT, class _Traits, class _Allocator>
2822typename basic_string<_CharT, _Traits, _Allocator>::size_type
2823basic_string<_CharT, _Traits, _Allocator>::find_first_of(const_pointer __s, size_type __pos, size_type __n) const
2824{
2825#ifdef _LIBCPP_DEBUG
2826 assert(__s != 0);
2827#endif
2828 size_type __sz = size();
2829 if (__pos >= __sz || __n == 0)
2830 return npos;
2831 const_pointer __p = data();
2832 const_pointer __r = _STD::find_first_of(__p + __pos, __p + __sz, __s, __s + __n, __traits_eq<traits_type>());
2833 if (__r == __p + __sz)
2834 return npos;
2835 return static_cast<size_type>(__r - __p);
2836}
2837
2838template<class _CharT, class _Traits, class _Allocator>
2839_LIBCPP_INLINE_VISIBILITY inline
2840typename basic_string<_CharT, _Traits, _Allocator>::size_type
2841basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str, size_type __pos) const
2842{
2843 return find_first_of(__str.data(), __pos, __str.size());
2844}
2845
2846template<class _CharT, class _Traits, class _Allocator>
2847_LIBCPP_INLINE_VISIBILITY inline
2848typename basic_string<_CharT, _Traits, _Allocator>::size_type
2849basic_string<_CharT, _Traits, _Allocator>::find_first_of(const_pointer __s, size_type __pos) const
2850{
2851#ifdef _LIBCPP_DEBUG
2852 assert(__s != 0);
2853#endif
2854 return find_first_of(__s, __pos, traits_type::length(__s));
2855}
2856
2857template<class _CharT, class _Traits, class _Allocator>
2858_LIBCPP_INLINE_VISIBILITY inline
2859typename basic_string<_CharT, _Traits, _Allocator>::size_type
2860basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c, size_type __pos) const
2861{
2862 return find(__c, __pos);
2863}
2864
2865// find_last_of
2866
2867template<class _CharT, class _Traits, class _Allocator>
2868typename basic_string<_CharT, _Traits, _Allocator>::size_type
2869basic_string<_CharT, _Traits, _Allocator>::find_last_of(const_pointer __s, size_type __pos, size_type __n) const
2870{
2871#ifdef _LIBCPP_DEBUG
2872 assert(__s != 0);
2873#endif
2874 if (__n != 0)
2875 {
2876 size_type __sz = size();
2877 if (__pos < __sz)
2878 ++__pos;
2879 else
2880 __pos = __sz;
2881 const_pointer __p = data();
2882 for (const_pointer __ps = __p + __pos; __ps != __p;)
2883 {
2884 const_pointer __r = traits_type::find(__s, __n, *--__ps);
2885 if (__r)
2886 return static_cast<size_type>(__ps - __p);
2887 }
2888 }
2889 return npos;
2890}
2891
2892template<class _CharT, class _Traits, class _Allocator>
2893_LIBCPP_INLINE_VISIBILITY inline
2894typename basic_string<_CharT, _Traits, _Allocator>::size_type
2895basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str, size_type __pos) const
2896{
2897 return find_last_of(__str.data(), __pos, __str.size());
2898}
2899
2900template<class _CharT, class _Traits, class _Allocator>
2901_LIBCPP_INLINE_VISIBILITY inline
2902typename basic_string<_CharT, _Traits, _Allocator>::size_type
2903basic_string<_CharT, _Traits, _Allocator>::find_last_of(const_pointer __s, size_type __pos) const
2904{
2905#ifdef _LIBCPP_DEBUG
2906 assert(__s != 0);
2907#endif
2908 return find_last_of(__s, __pos, traits_type::length(__s));
2909}
2910
2911template<class _CharT, class _Traits, class _Allocator>
2912_LIBCPP_INLINE_VISIBILITY inline
2913typename basic_string<_CharT, _Traits, _Allocator>::size_type
2914basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c, size_type __pos) const
2915{
2916 return rfind(__c, __pos);
2917}
2918
2919// find_first_not_of
2920
2921template<class _CharT, class _Traits, class _Allocator>
2922typename basic_string<_CharT, _Traits, _Allocator>::size_type
2923basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const_pointer __s, size_type __pos, size_type __n) const
2924{
2925#ifdef _LIBCPP_DEBUG
2926 assert(__s != 0);
2927#endif
2928 size_type __sz = size();
2929 if (__pos < __sz)
2930 {
2931 const_pointer __p = data();
2932 const_pointer __pe = __p + __sz;
2933 for (const_pointer __ps = __p + __pos; __ps != __pe; ++__ps)
2934 if (traits_type::find(__s, __n, *__ps) == 0)
2935 return static_cast<size_type>(__ps - __p);
2936 }
2937 return npos;
2938}
2939
2940template<class _CharT, class _Traits, class _Allocator>
2941_LIBCPP_INLINE_VISIBILITY inline
2942typename basic_string<_CharT, _Traits, _Allocator>::size_type
2943basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str, size_type __pos) const
2944{
2945 return find_first_not_of(__str.data(), __pos, __str.size());
2946}
2947
2948template<class _CharT, class _Traits, class _Allocator>
2949_LIBCPP_INLINE_VISIBILITY inline
2950typename basic_string<_CharT, _Traits, _Allocator>::size_type
2951basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const_pointer __s, size_type __pos) const
2952{
2953#ifdef _LIBCPP_DEBUG
2954 assert(__s != 0);
2955#endif
2956 return find_first_not_of(__s, __pos, traits_type::length(__s));
2957}
2958
2959template<class _CharT, class _Traits, class _Allocator>
2960_LIBCPP_INLINE_VISIBILITY inline
2961typename basic_string<_CharT, _Traits, _Allocator>::size_type
2962basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c, size_type __pos) const
2963{
2964 size_type __sz = size();
2965 if (__pos < __sz)
2966 {
2967 const_pointer __p = data();
2968 const_pointer __pe = __p + __sz;
2969 for (const_pointer __ps = __p + __pos; __p != __pe; ++__ps)
2970 if (!traits_type::eq(*__ps, __c))
2971 return static_cast<size_type>(__ps - __p);
2972 }
2973 return npos;
2974}
2975
2976// find_last_not_of
2977
2978template<class _CharT, class _Traits, class _Allocator>
2979typename basic_string<_CharT, _Traits, _Allocator>::size_type
2980basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const_pointer __s, size_type __pos, size_type __n) const
2981{
2982#ifdef _LIBCPP_DEBUG
2983 assert(__s != 0);
2984#endif
2985 size_type __sz = size();
2986 if (__pos < __sz)
2987 ++__pos;
2988 else
2989 __pos = __sz;
2990 const_pointer __p = data();
2991 for (const_pointer __ps = __p + __pos; __ps != __p;)
2992 if (traits_type::find(__s, __n, *--__ps) == 0)
2993 return static_cast<size_type>(__ps - __p);
2994 return npos;
2995}
2996
2997template<class _CharT, class _Traits, class _Allocator>
2998_LIBCPP_INLINE_VISIBILITY inline
2999typename basic_string<_CharT, _Traits, _Allocator>::size_type
3000basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str, size_type __pos) const
3001{
3002 return find_last_not_of(__str.data(), __pos, __str.size());
3003}
3004
3005template<class _CharT, class _Traits, class _Allocator>
3006_LIBCPP_INLINE_VISIBILITY inline
3007typename basic_string<_CharT, _Traits, _Allocator>::size_type
3008basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const_pointer __s, size_type __pos) const
3009{
3010#ifdef _LIBCPP_DEBUG
3011 assert(__s != 0);
3012#endif
3013 return find_last_not_of(__s, __pos, traits_type::length(__s));
3014}
3015
3016template<class _CharT, class _Traits, class _Allocator>
3017_LIBCPP_INLINE_VISIBILITY inline
3018typename basic_string<_CharT, _Traits, _Allocator>::size_type
3019basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c, size_type __pos) const
3020{
3021 size_type __sz = size();
3022 if (__pos < __sz)
3023 ++__pos;
3024 else
3025 __pos = __sz;
3026 const_pointer __p = data();
3027 for (const_pointer __ps = __p + __pos; __ps != __p;)
3028 if (!traits_type::eq(*--__ps, __c))
3029 return static_cast<size_type>(__ps - __p);
3030 return npos;
3031}
3032
3033// compare
3034
3035template <class _CharT, class _Traits, class _Allocator>
3036_LIBCPP_INLINE_VISIBILITY inline
3037int
3038basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const
3039{
3040 return compare(0, npos, __str.data(), __str.size());
3041}
3042
3043template <class _CharT, class _Traits, class _Allocator>
3044_LIBCPP_INLINE_VISIBILITY inline
3045int
3046basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1, size_type __n1, const basic_string& __str) const
3047{
3048 return compare(__pos1, __n1, __str.data(), __str.size());
3049}
3050
3051template <class _CharT, class _Traits, class _Allocator>
3052int
3053basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1, size_type __n1, const basic_string& __str,
3054 size_type __pos2, size_type __n2) const
3055{
3056 size_type __sz = __str.size();
3057 if (__pos2 > __sz)
3058 this->__throw_out_of_range();
3059 return compare(__pos1, __n1, __str.data() + __pos2, _STD::min(__n2, __sz - __pos2));
3060}
3061
3062template <class _CharT, class _Traits, class _Allocator>
3063int
3064basic_string<_CharT, _Traits, _Allocator>::compare(const_pointer __s) const
3065{
3066#ifdef _LIBCPP_DEBUG
3067 assert(__s != 0);
3068#endif
3069 return compare(0, npos, __s, traits_type::length(__s));
3070}
3071
3072template <class _CharT, class _Traits, class _Allocator>
3073int
3074basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1, size_type __n1, const_pointer __s) const
3075{
3076#ifdef _LIBCPP_DEBUG
3077 assert(__s != 0);
3078#endif
3079 return compare(__pos1, __n1, __s, traits_type::length(__s));
3080}
3081
3082template <class _CharT, class _Traits, class _Allocator>
3083int
3084basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1, size_type __n1,
3085 const_pointer __s, size_type __n2) const
3086{
3087#ifdef _LIBCPP_DEBUG
3088 assert(__s != 0);
3089#endif
3090 size_type __sz = size();
3091 if (__pos1 > __sz || __n2 == npos)
3092 this->__throw_out_of_range();
3093 size_type __rlen = _STD::min(__n1, __sz - __pos1);
3094 int __r = traits_type::compare(data() + __pos1, __s, _STD::min(__rlen, __n2));
3095 if (__r == 0)
3096 {
3097 if (__rlen < __n2)
3098 __r = -1;
3099 else if (__rlen > __n2)
3100 __r = 1;
3101 }
3102 return __r;
3103}
3104
3105// __invariants
3106
3107template<class _CharT, class _Traits, class _Allocator>
3108bool
3109basic_string<_CharT, _Traits, _Allocator>::__invariants() const
3110{
3111 if (size() > capacity())
3112 return false;
3113 if (capacity() < __min_cap - 1)
3114 return false;
3115 if (data() == 0)
3116 return false;
3117 if (data()[size()] != value_type(0))
3118 return false;
3119 return true;
3120}
3121
3122// operator==
3123
3124template<class _CharT, class _Traits, class _Allocator>
3125_LIBCPP_INLINE_VISIBILITY inline
3126bool
3127operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3128 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3129{
3130 return __lhs.size() == __rhs.size() && _Traits::compare(__lhs.data(), __rhs.data(), __lhs.size()) == 0;
3131}
3132
3133template<class _CharT, class _Traits, class _Allocator>
3134_LIBCPP_INLINE_VISIBILITY inline
3135bool
3136operator==(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3137{
3138 return __rhs.compare(__lhs) == 0;
3139}
3140
3141template<class _Allocator>
3142_LIBCPP_INLINE_VISIBILITY inline
3143bool
3144operator==(const char* __lhs, const basic_string<char, char_traits<char>, _Allocator>& __rhs)
3145{
3146 return strcmp(__lhs, __rhs.data()) == 0;
3147}
3148
3149template<class _Allocator>
3150_LIBCPP_INLINE_VISIBILITY inline
3151bool
3152operator==(const wchar_t* __lhs, const basic_string<wchar_t, char_traits<wchar_t>, _Allocator>& __rhs)
3153{
3154 return wcscmp(__lhs, __rhs.data()) == 0;
3155}
3156
3157template<class _CharT, class _Traits, class _Allocator>
3158_LIBCPP_INLINE_VISIBILITY inline
3159bool
3160operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs, const _CharT* __rhs)
3161{
3162 return __lhs.compare(__rhs) == 0;
3163}
3164
3165template<class _Allocator>
3166_LIBCPP_INLINE_VISIBILITY inline
3167bool
3168operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs, const char* __rhs)
3169{
3170 return strcmp(__lhs.data(), __rhs) == 0;
3171}
3172
3173template<class _Allocator>
3174_LIBCPP_INLINE_VISIBILITY inline
3175bool
3176operator==(const basic_string<wchar_t, char_traits<wchar_t>, _Allocator>& __lhs, const wchar_t* __rhs)
3177{
3178 return wcscmp(__lhs.data(), __rhs) == 0;
3179}
3180
3181// operator!=
3182
3183template<class _CharT, class _Traits, class _Allocator>
3184_LIBCPP_INLINE_VISIBILITY inline
3185bool
3186operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
3187 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3188{
3189 return !(__lhs == __rhs);
3190}
3191
3192template<class _CharT, class _Traits, class _Allocator>
3193_LIBCPP_INLINE_VISIBILITY inline
3194bool
3195operator!=(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3196{
3197 return !(__lhs == __rhs);
3198}
3199
3200template<class _CharT, class _Traits, class _Allocator>
3201_LIBCPP_INLINE_VISIBILITY inline
3202bool
3203operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
3204{
3205 return !(__lhs == __rhs);
3206}
3207
3208// operator<
3209
3210template<class _CharT, class _Traits, class _Allocator>
3211_LIBCPP_INLINE_VISIBILITY inline
3212bool
3213operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3214 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3215{
3216 return __lhs.cmpare(__rhs) < 0;
3217}
3218
3219template<class _Allocator>
3220_LIBCPP_INLINE_VISIBILITY inline
3221bool
3222operator< (const basic_string<char, char_traits<char>, _Allocator>& __lhs,
3223 const basic_string<char, char_traits<char>, _Allocator>& __rhs)
3224{
3225 return strcmp(__lhs.data(), __rhs.data()) < 0;
3226}
3227
3228template<class _Allocator>
3229_LIBCPP_INLINE_VISIBILITY inline
3230bool
3231operator< (const basic_string<wchar_t, char_traits<wchar_t>, _Allocator>& __lhs,
3232 const basic_string<wchar_t, char_traits<wchar_t>, _Allocator>& __rhs)
3233{
3234 return wcscmp(__lhs.data(), __rhs.data()) < 0;
3235}
3236
3237template<class _CharT, class _Traits, class _Allocator>
3238_LIBCPP_INLINE_VISIBILITY inline
3239bool
3240operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
3241{
3242 return __lhs.compare(__rhs);
3243}
3244
3245template<class _Allocator>
3246_LIBCPP_INLINE_VISIBILITY inline
3247bool
3248operator< (const basic_string<char, char_traits<char>, _Allocator>& __lhs, const char* __rhs)
3249{
3250 return strcmp(__lhs.data(), __rhs) < 0;
3251}
3252
3253template<class _Allocator>
3254_LIBCPP_INLINE_VISIBILITY inline
3255bool
3256operator< (const basic_string<wchar_t, char_traits<wchar_t>, _Allocator>& __lhs, const wchar_t* __rhs)
3257{
3258 return wcscmp(__lhs.data(), __rhs) < 0;
3259}
3260
3261template<class _CharT, class _Traits, class _Allocator>
3262_LIBCPP_INLINE_VISIBILITY inline
3263bool
3264operator< (const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3265{
3266 return __rhs.compare(__lhs) > 0;
3267}
3268
3269template<class _Allocator>
3270_LIBCPP_INLINE_VISIBILITY inline
3271bool
3272operator< (const char* __lhs, const basic_string<char, char_traits<char>, _Allocator>& __rhs)
3273{
3274 return strcmp(__lhs, __rhs.data()) < 0;
3275}
3276
3277template<class _Allocator>
3278_LIBCPP_INLINE_VISIBILITY inline
3279bool
3280operator< (const wchar_t* __lhs, const basic_string<wchar_t, char_traits<wchar_t>, _Allocator>& __rhs)
3281{
3282 return wcscmp(__lhs, __rhs.data()) < 0;
3283}
3284
3285// operator>
3286
3287template<class _CharT, class _Traits, class _Allocator>
3288_LIBCPP_INLINE_VISIBILITY inline
3289bool
3290operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3291 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3292{
3293 return __rhs < __lhs;
3294}
3295
3296template<class _CharT, class _Traits, class _Allocator>
3297_LIBCPP_INLINE_VISIBILITY inline
3298bool
3299operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
3300{
3301 return __rhs < __lhs;
3302}
3303
3304template<class _CharT, class _Traits, class _Allocator>
3305_LIBCPP_INLINE_VISIBILITY inline
3306bool
3307operator> (const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3308{
3309 return __rhs < __lhs;
3310}
3311
3312// operator<=
3313
3314template<class _CharT, class _Traits, class _Allocator>
3315_LIBCPP_INLINE_VISIBILITY inline
3316bool
3317operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3318 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3319{
3320 return !(__rhs < __lhs);
3321}
3322
3323template<class _CharT, class _Traits, class _Allocator>
3324_LIBCPP_INLINE_VISIBILITY inline
3325bool
3326operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
3327{
3328 return !(__rhs < __lhs);
3329}
3330
3331template<class _CharT, class _Traits, class _Allocator>
3332_LIBCPP_INLINE_VISIBILITY inline
3333bool
3334operator<=(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3335{
3336 return !(__rhs < __lhs);
3337}
3338
3339// operator>=
3340
3341template<class _CharT, class _Traits, class _Allocator>
3342_LIBCPP_INLINE_VISIBILITY inline
3343bool
3344operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3345 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3346{
3347 return !(__lhs < __rhs);
3348}
3349
3350template<class _CharT, class _Traits, class _Allocator>
3351_LIBCPP_INLINE_VISIBILITY inline
3352bool
3353operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
3354{
3355 return !(__lhs < __rhs);
3356}
3357
3358template<class _CharT, class _Traits, class _Allocator>
3359_LIBCPP_INLINE_VISIBILITY inline
3360bool
3361operator>=(const _CharT* __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3362{
3363 return !(__lhs < __rhs);
3364}
3365
3366// operator +
3367
3368template<class _CharT, class _Traits, class _Allocator>
3369basic_string<_CharT, _Traits, _Allocator>
3370operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3371 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3372{
3373 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
3374 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
3375 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
3376 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
3377 __r.append(__rhs.data(), __rhs_sz);
3378 return __r;
3379}
3380
3381template<class _CharT, class _Traits, class _Allocator>
3382basic_string<_CharT, _Traits, _Allocator>
3383operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
3384{
3385 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
3386 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = _Traits::length(__lhs);
3387 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
3388 __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz);
3389 __r.append(__rhs.data(), __rhs_sz);
3390 return __r;
3391}
3392
3393template<class _CharT, class _Traits, class _Allocator>
3394basic_string<_CharT, _Traits, _Allocator>
3395operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
3396{
3397 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
3398 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
3399 __r.__init(&__lhs, 1, 1 + __rhs_sz);
3400 __r.append(__rhs.data(), __rhs_sz);
3401 return __r;
3402}
3403
3404template<class _CharT, class _Traits, class _Allocator>
3405basic_string<_CharT, _Traits, _Allocator>
3406operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
3407{
3408 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
3409 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
3410 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = _Traits::length(__rhs);
3411 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
3412 __r.append(__rhs, __rhs_sz);
3413 return __r;
3414}
3415
3416template<class _CharT, class _Traits, class _Allocator>
3417basic_string<_CharT, _Traits, _Allocator>
3418operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
3419{
3420 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
3421 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
3422 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1);
3423 __r.push_back(__rhs);
3424 return __r;
3425}
3426
3427#ifdef _LIBCPP_MOVE
3428
3429template<class _CharT, class _Traits, class _Allocator>
3430_LIBCPP_INLINE_VISIBILITY inline
3431basic_string<_CharT, _Traits, _Allocator>
3432operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3433{
3434 return _STD::move(__lhs.append(__rhs));
3435}
3436
3437template<class _CharT, class _Traits, class _Allocator>
3438_LIBCPP_INLINE_VISIBILITY inline
3439basic_string<_CharT, _Traits, _Allocator>
3440operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
3441{
3442 return _STD::move(__rhs.insert(0, __lhs));
3443}
3444
3445template<class _CharT, class _Traits, class _Allocator>
3446_LIBCPP_INLINE_VISIBILITY inline
3447basic_string<_CharT, _Traits, _Allocator>
3448operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
3449{
3450 return _STD::move(__lhs.append(__rhs));
3451}
3452
3453template<class _CharT, class _Traits, class _Allocator>
3454_LIBCPP_INLINE_VISIBILITY inline
3455basic_string<_CharT, _Traits, _Allocator>
3456operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
3457{
3458 return _STD::move(__rhs.insert(0, __lhs));
3459}
3460
3461template<class _CharT, class _Traits, class _Allocator>
3462_LIBCPP_INLINE_VISIBILITY inline
3463basic_string<_CharT, _Traits, _Allocator>
3464operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
3465{
3466 __rhs.insert(__rhs.begin(), __lhs);
3467 return _STD::move(__rhs);
3468}
3469
3470template<class _CharT, class _Traits, class _Allocator>
3471_LIBCPP_INLINE_VISIBILITY inline
3472basic_string<_CharT, _Traits, _Allocator>
3473operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
3474{
3475 return _STD::move(__lhs.append(__rhs));
3476}
3477
3478template<class _CharT, class _Traits, class _Allocator>
3479_LIBCPP_INLINE_VISIBILITY inline
3480basic_string<_CharT, _Traits, _Allocator>
3481operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
3482{
3483 __lhs.push_back(__rhs);
3484 return _STD::move(__lhs);
3485}
3486
3487#endif // _LIBCPP_MOVE
3488
3489// swap
3490
3491template<class _CharT, class _Traits, class _Allocator>
3492_LIBCPP_INLINE_VISIBILITY inline
3493void
3494swap(basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>& __rhs)
3495{
3496 __lhs.swap(__rhs);
3497}
3498
3499template<class _CharT, class _Traits, class _Allocator>
3500struct __is_zero_default_constructible<basic_string<_CharT, _Traits, _Allocator> >
3501 : public integral_constant<bool, __is_zero_default_constructible<_Allocator>::value> {};
3502
3503#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
3504
3505typedef basic_string<char16_t> u16string;
3506typedef basic_string<char32_t> u32string;
3507
3508#endif
3509
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003510int stoi (const string& __str, size_t* __idx = 0, int __base = 10);
3511long stol (const string& __str, size_t* __idx = 0, int __base = 10);
3512unsigned long stoul (const string& __str, size_t* __idx = 0, int __base = 10);
3513long long stoll (const string& __str, size_t* __idx = 0, int __base = 10);
3514unsigned long long stoull(const string& __str, size_t* __idx = 0, int __base = 10);
3515
3516float stof (const string& __str, size_t* __idx = 0);
3517double stod (const string& __str, size_t* __idx = 0);
3518long double stold(const string& __str, size_t* __idx = 0);
3519
3520string to_string(int __val);
3521string to_string(unsigned __val);
3522string to_string(long __val);
3523string to_string(unsigned long __val);
3524string to_string(long long __val);
3525string to_string(unsigned long long __val);
3526string to_string(float __val);
3527string to_string(double __val);
3528string to_string(long double __val);
3529
3530int stoi (const wstring& __str, size_t* __idx = 0, int __base = 10);
3531long stol (const wstring& __str, size_t* __idx = 0, int __base = 10);
3532unsigned long stoul (const wstring& __str, size_t* __idx = 0, int __base = 10);
3533long long stoll (const wstring& __str, size_t* __idx = 0, int __base = 10);
3534unsigned long long stoull(const wstring& __str, size_t* __idx = 0, int __base = 10);
3535
3536float stof (const wstring& __str, size_t* __idx = 0);
3537double stod (const wstring& __str, size_t* __idx = 0);
3538long double stold(const wstring& __str, size_t* __idx = 0);
3539
3540wstring to_wstring(int __val);
3541wstring to_wstring(unsigned __val);
3542wstring to_wstring(long __val);
3543wstring to_wstring(unsigned long __val);
3544wstring to_wstring(long long __val);
3545wstring to_wstring(unsigned long long __val);
3546wstring to_wstring(float __val);
3547wstring to_wstring(double __val);
3548wstring to_wstring(long double __val);
3549
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003550template<class _CharT, class _Traits, class _Allocator>
3551 const typename basic_string<_CharT, _Traits, _Allocator>::size_type
3552 basic_string<_CharT, _Traits, _Allocator>::npos;
3553
3554template<class _CharT, class _Traits, class _Allocator>
3555struct hash<basic_string<_CharT, _Traits, _Allocator> >
3556 : public unary_function<basic_string<_CharT, _Traits, _Allocator>, size_t>
3557{
3558 size_t
3559 operator()(const basic_string<_CharT, _Traits, _Allocator>& __val) const;
3560};
3561
3562template<class _CharT, class _Traits, class _Allocator>
3563size_t
3564hash<basic_string<_CharT, _Traits, _Allocator> >::operator()(
3565 const basic_string<_CharT, _Traits, _Allocator>& __val) const
3566{
3567 typedef basic_string<_CharT, _Traits, _Allocator> S;
3568 typedef typename S::const_pointer const_pointer;
3569 size_t __r = 0;
3570 const size_t __sr = __CHAR_BIT__ * sizeof(size_t) - 8;
3571 const size_t __m = size_t(0xF) << (__sr + 4);
3572 const_pointer __p = __val.data();
3573 const_pointer __e = __p + __val.size();
3574 for (; __p != __e; ++__p)
3575 {
3576 __r = (__r << 4) + *__p;
3577 size_t __g = __r & __m;
3578 __r ^= __g | (__g >> __sr);
3579 }
3580 return __r;
3581}
3582
3583extern template class basic_string<char>;
3584extern template class basic_string<wchar_t>;
3585
3586extern template
3587 enable_if<__is_forward_iterator<char const*>::value, void>::type
3588 basic_string<char, char_traits<char>, allocator<char> >::
3589 __init<char const*>(char const*, char const*);
3590
3591extern template
3592 enable_if<__is_forward_iterator<wchar_t const*>::value, void>::type
3593 basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> >::
3594 __init<wchar_t const*>(wchar_t const*, wchar_t const*);
3595
3596extern template
3597 enable_if<__is_forward_iterator<char*>::value,
3598 basic_string<char, char_traits<char>, allocator<char> >&>::type
3599 basic_string<char, char_traits<char>, allocator<char> >::
3600 append<char*>(char*, char*);
3601
3602extern template
3603 enable_if<__is_forward_iterator<wchar_t*>::value,
3604 basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> >&>::type
3605 basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> >::
3606 append<wchar_t*>(wchar_t*, wchar_t*);
3607
3608extern template
3609 enable_if<__is_forward_iterator<char const*>::value,
3610 string::iterator>::type
3611 string::
3612 insert<char const*>(string::const_iterator, char const*, char const*);
3613
3614extern template
3615 enable_if<__is_forward_iterator<wchar_t const*>::value,
3616 wstring::iterator>::type
3617 wstring::
3618 insert<wchar_t const*>(wstring::const_iterator, wchar_t const*, wchar_t const*);
3619
3620extern template
3621 enable_if<__is_input_iterator<char const*>::value, string&>::type
3622 string::
3623 replace<char const*>(string::iterator, string::iterator, char const*, char const*);
3624
3625extern template
3626 enable_if<__is_input_iterator<wchar_t const*>::value, wstring&>::type
3627 wstring::
3628 replace<wchar_t const*>(wstring::iterator, wstring::iterator, wchar_t const*, wchar_t const*);
3629
3630extern template
3631 enable_if<__is_forward_iterator<wchar_t*>::value, wstring&>::type
3632 wstring::assign<wchar_t*>(wchar_t*, wchar_t*);
3633
3634extern template
3635 string
3636 operator+<char, char_traits<char>, allocator<char> >(char const*, string const&);
3637
3638_LIBCPP_END_NAMESPACE_STD
3639
3640#endif // _LIBCPP_STRING