blob: acd287652f2a0b314bf928e94bc993853bc7676d [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===--------------------------- string -----------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
6// This file is distributed under the University of Illinois Open Source
7// License. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_STRING
12#define _LIBCPP_STRING
13
14/*
15 string synopsis
16
17namespace std
18{
19
20template <class stateT>
21class fpos
22{
23private:
24 stateT st;
25public:
26 fpos(streamoff = streamoff());
27
28 operator streamoff() const;
29
30 stateT state() const;
31 void state(stateT);
32
33 fpos& operator+=(streamoff);
34 fpos operator+ (streamoff) const;
35 fpos& operator-=(streamoff);
36 fpos operator- (streamoff) const;
37};
38
39template <class stateT> streamoff operator-(const fpos<stateT>& x, const fpos<stateT>& y);
40
41template <class stateT> bool operator==(const fpos<stateT>& x, const fpos<stateT>& y);
42template <class stateT> bool operator!=(const fpos<stateT>& x, const fpos<stateT>& y);
43
44template <class charT>
45struct char_traits
46{
47 typedef charT char_type;
48 typedef ... int_type;
49 typedef streamoff off_type;
50 typedef streampos pos_type;
51 typedef mbstate_t state_type;
52
Howard Hinnanta6119a82011-05-29 19:57:12 +000053 static void assign(char_type& c1, const char_type& c2) noexcept;
Howard Hinnant03d71812012-07-20 19:09:12 +000054 static constexpr bool eq(char_type c1, char_type c2) noexcept;
55 static constexpr bool lt(char_type c1, char_type c2) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000056
57 static int compare(const char_type* s1, const char_type* s2, size_t n);
58 static size_t length(const char_type* s);
59 static const char_type* find(const char_type* s, size_t n, const char_type& a);
60 static char_type* move(char_type* s1, const char_type* s2, size_t n);
61 static char_type* copy(char_type* s1, const char_type* s2, size_t n);
62 static char_type* assign(char_type* s, size_t n, char_type a);
63
Howard Hinnant03d71812012-07-20 19:09:12 +000064 static constexpr int_type not_eof(int_type c) noexcept;
65 static constexpr char_type to_char_type(int_type c) noexcept;
66 static constexpr int_type to_int_type(char_type c) noexcept;
67 static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept;
68 static constexpr int_type eof() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000069};
70
71template <> struct char_traits<char>;
72template <> struct char_traits<wchar_t>;
73
Howard Hinnant324bb032010-08-22 00:02:43 +000074template<class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000075class basic_string
76{
Howard Hinnant324bb032010-08-22 00:02:43 +000077public:
78// types:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000079 typedef traits traits_type;
80 typedef typename traits_type::char_type value_type;
81 typedef Allocator allocator_type;
82 typedef typename allocator_type::size_type size_type;
83 typedef typename allocator_type::difference_type difference_type;
84 typedef typename allocator_type::reference reference;
85 typedef typename allocator_type::const_reference const_reference;
86 typedef typename allocator_type::pointer pointer;
87 typedef typename allocator_type::const_pointer const_pointer;
88 typedef implementation-defined iterator;
89 typedef implementation-defined const_iterator;
90 typedef std::reverse_iterator<iterator> reverse_iterator;
91 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
92
93 static const size_type npos = -1;
94
Howard Hinnant53f7d4c2011-06-03 18:40:47 +000095 basic_string()
96 noexcept(is_nothrow_default_constructible<allocator_type>::value);
97 explicit basic_string(const allocator_type& a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000098 basic_string(const basic_string& str);
Howard Hinnant53f7d4c2011-06-03 18:40:47 +000099 basic_string(basic_string&& str)
100 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowf4f7d8f2016-04-07 18:13:41 +0000101 basic_string(const basic_string& str, size_type pos,
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000102 const allocator_type& a = allocator_type());
Marshall Clowf4f7d8f2016-04-07 18:13:41 +0000103 basic_string(const basic_string& str, size_type pos, size_type n,
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000104 const Allocator& a = Allocator());
Marshall Clowdb7fa112016-11-14 18:22:19 +0000105 template<class T>
106 basic_string(const T& t, size_type pos, size_type n, const Allocator& a = Allocator()); // C++17
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000107 explicit basic_string(const basic_string_view<charT, traits> sv, const Allocator& a = Allocator());
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000108 basic_string(const value_type* s, const allocator_type& a = allocator_type());
109 basic_string(const value_type* s, size_type n, const allocator_type& a = allocator_type());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000110 basic_string(size_type n, value_type c, const allocator_type& a = allocator_type());
111 template<class InputIterator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000112 basic_string(InputIterator begin, InputIterator end,
113 const allocator_type& a = allocator_type());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000114 basic_string(initializer_list<value_type>, const Allocator& = Allocator());
115 basic_string(const basic_string&, const Allocator&);
116 basic_string(basic_string&&, const Allocator&);
117
118 ~basic_string();
119
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000120 operator basic_string_view<charT, traits>() const noexcept;
121
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000122 basic_string& operator=(const basic_string& str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000123 basic_string& operator=(basic_string_view<charT, traits> sv);
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000124 basic_string& operator=(basic_string&& str)
125 noexcept(
Marshall Clowaf961ed2015-08-18 18:57:00 +0000126 allocator_type::propagate_on_container_move_assignment::value ||
127 allocator_type::is_always_equal::value ); // C++17
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000128 basic_string& operator=(const value_type* s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000129 basic_string& operator=(value_type c);
130 basic_string& operator=(initializer_list<value_type>);
131
Howard Hinnanta6119a82011-05-29 19:57:12 +0000132 iterator begin() noexcept;
133 const_iterator begin() const noexcept;
134 iterator end() noexcept;
135 const_iterator end() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000136
Howard Hinnanta6119a82011-05-29 19:57:12 +0000137 reverse_iterator rbegin() noexcept;
138 const_reverse_iterator rbegin() const noexcept;
139 reverse_iterator rend() noexcept;
140 const_reverse_iterator rend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000141
Howard Hinnanta6119a82011-05-29 19:57:12 +0000142 const_iterator cbegin() const noexcept;
143 const_iterator cend() const noexcept;
144 const_reverse_iterator crbegin() const noexcept;
145 const_reverse_iterator crend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000146
Howard Hinnanta6119a82011-05-29 19:57:12 +0000147 size_type size() const noexcept;
148 size_type length() const noexcept;
149 size_type max_size() const noexcept;
150 size_type capacity() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000151
152 void resize(size_type n, value_type c);
153 void resize(size_type n);
154
155 void reserve(size_type res_arg = 0);
156 void shrink_to_fit();
Howard Hinnanta6119a82011-05-29 19:57:12 +0000157 void clear() noexcept;
158 bool empty() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000159
160 const_reference operator[](size_type pos) const;
161 reference operator[](size_type pos);
162
163 const_reference at(size_type n) const;
164 reference at(size_type n);
165
166 basic_string& operator+=(const basic_string& str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000167 basic_string& operator+=(basic_string_view<charT, traits> sv);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000168 basic_string& operator+=(const value_type* s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000169 basic_string& operator+=(value_type c);
170 basic_string& operator+=(initializer_list<value_type>);
171
172 basic_string& append(const basic_string& str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000173 basic_string& append(basic_string_view<charT, traits> sv);
Marshall Clowa93b5e22014-03-04 19:17:19 +0000174 basic_string& append(const basic_string& str, size_type pos, size_type n=npos); //C++14
Marshall Clow6ac8de02016-09-24 22:45:42 +0000175 template <class T>
176 basic_string& append(const T& t, size_type pos, size_type n=npos); // C++17
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000177 basic_string& append(const value_type* s, size_type n);
178 basic_string& append(const value_type* s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000179 basic_string& append(size_type n, value_type c);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000180 template<class InputIterator>
181 basic_string& append(InputIterator first, InputIterator last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000182 basic_string& append(initializer_list<value_type>);
183
184 void push_back(value_type c);
185 void pop_back();
186 reference front();
187 const_reference front() const;
188 reference back();
189 const_reference back() const;
190
191 basic_string& assign(const basic_string& str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000192 basic_string& assign(basic_string_view<charT, traits> sv);
Howard Hinnanta6119a82011-05-29 19:57:12 +0000193 basic_string& assign(basic_string&& str);
Marshall Clowa93b5e22014-03-04 19:17:19 +0000194 basic_string& assign(const basic_string& str, size_type pos, size_type n=npos); // C++14
Marshall Clow6ac8de02016-09-24 22:45:42 +0000195 template <class T>
196 basic_string& assign(const T& t, size_type pos, size_type n=npos); // C++17
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000197 basic_string& assign(const value_type* s, size_type n);
198 basic_string& assign(const value_type* s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000199 basic_string& assign(size_type n, value_type c);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000200 template<class InputIterator>
201 basic_string& assign(InputIterator first, InputIterator last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000202 basic_string& assign(initializer_list<value_type>);
203
204 basic_string& insert(size_type pos1, const basic_string& str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000205 basic_string& insert(size_type pos1, basic_string_view<charT, traits> sv);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000206 basic_string& insert(size_type pos1, const basic_string& str,
207 size_type pos2, size_type n);
Marshall Clow6ac8de02016-09-24 22:45:42 +0000208 template <class T>
209 basic_string& insert(size_type pos1, const T& t, size_type pos2, size_type n); // C++17
Marshall Clowa93b5e22014-03-04 19:17:19 +0000210 basic_string& insert(size_type pos, const value_type* s, size_type n=npos); //C++14
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000211 basic_string& insert(size_type pos, const value_type* s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000212 basic_string& insert(size_type pos, size_type n, value_type c);
213 iterator insert(const_iterator p, value_type c);
214 iterator insert(const_iterator p, size_type n, value_type c);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000215 template<class InputIterator>
216 iterator insert(const_iterator p, InputIterator first, InputIterator last);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000217 iterator insert(const_iterator p, initializer_list<value_type>);
218
219 basic_string& erase(size_type pos = 0, size_type n = npos);
220 iterator erase(const_iterator position);
221 iterator erase(const_iterator first, const_iterator last);
222
223 basic_string& replace(size_type pos1, size_type n1, const basic_string& str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000224 basic_string& replace(size_type pos1, size_type n1, basic_string_view<charT, traits> sv);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000225 basic_string& replace(size_type pos1, size_type n1, const basic_string& str,
Marshall Clowa93b5e22014-03-04 19:17:19 +0000226 size_type pos2, size_type n2=npos); // C++14
Marshall Clow6ac8de02016-09-24 22:45:42 +0000227 template <class T>
228 basic_string& replace(size_type pos1, size_type n1, const T& t,
229 size_type pos2, size_type n); // C++17
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000230 basic_string& replace(size_type pos, size_type n1, const value_type* s, size_type n2);
231 basic_string& replace(size_type pos, size_type n1, const value_type* s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000232 basic_string& replace(size_type pos, size_type n1, size_type n2, value_type c);
Howard Hinnant7b2cb482010-11-17 21:11:40 +0000233 basic_string& replace(const_iterator i1, const_iterator i2, const basic_string& str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000234 basic_string& replace(const_iterator i1, const_iterator i2, basic_string_view<charT, traits> sv);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000235 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s, size_type n);
236 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s);
Howard Hinnant7b2cb482010-11-17 21:11:40 +0000237 basic_string& replace(const_iterator i1, const_iterator i2, size_type n, value_type c);
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000238 template<class InputIterator>
Howard Hinnant7b2cb482010-11-17 21:11:40 +0000239 basic_string& replace(const_iterator i1, const_iterator i2, InputIterator j1, InputIterator j2);
240 basic_string& replace(const_iterator i1, const_iterator i2, initializer_list<value_type>);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000241
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000242 size_type copy(value_type* s, size_type n, size_type pos = 0) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000243 basic_string substr(size_type pos = 0, size_type n = npos) const;
244
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000245 void swap(basic_string& str)
Marshall Clow7d914d12015-07-13 20:04:56 +0000246 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
247 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000248
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000249 const value_type* c_str() const noexcept;
250 const value_type* data() const noexcept;
Marshall Clowf532a702016-03-08 15:44:30 +0000251 value_type* data() noexcept; // C++17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000252
Howard Hinnanta6119a82011-05-29 19:57:12 +0000253 allocator_type get_allocator() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000254
Howard Hinnanta6119a82011-05-29 19:57:12 +0000255 size_type find(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000256 size_type find(basic_string_view<charT, traits> sv, size_type pos = 0) const noexcept;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000257 size_type find(const value_type* s, size_type pos, size_type n) const noexcept;
258 size_type find(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnanta6119a82011-05-29 19:57:12 +0000259 size_type find(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000260
Howard Hinnanta6119a82011-05-29 19:57:12 +0000261 size_type rfind(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clow0b9db072017-09-07 04:19:32 +0000262 size_type rfind(basic_string_view<charT, traits> sv, size_type pos = npos) const noexcept;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000263 size_type rfind(const value_type* s, size_type pos, size_type n) const noexcept;
264 size_type rfind(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnanta6119a82011-05-29 19:57:12 +0000265 size_type rfind(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000266
Howard Hinnanta6119a82011-05-29 19:57:12 +0000267 size_type find_first_of(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000268 size_type find_first_of(basic_string_view<charT, traits> sv, size_type pos = 0) const noexcept;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000269 size_type find_first_of(const value_type* s, size_type pos, size_type n) const noexcept;
270 size_type find_first_of(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnanta6119a82011-05-29 19:57:12 +0000271 size_type find_first_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000272
Howard Hinnanta6119a82011-05-29 19:57:12 +0000273 size_type find_last_of(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clow0b9db072017-09-07 04:19:32 +0000274 size_type find_last_of(basic_string_view<charT, traits> sv, size_type pos = npos) const noexcept;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000275 size_type find_last_of(const value_type* s, size_type pos, size_type n) const noexcept;
276 size_type find_last_of(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnanta6119a82011-05-29 19:57:12 +0000277 size_type find_last_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000278
Howard Hinnanta6119a82011-05-29 19:57:12 +0000279 size_type find_first_not_of(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000280 size_type find_first_not_of(basic_string_view<charT, traits> sv, size_type pos = 0) const noexcept;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000281 size_type find_first_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
282 size_type find_first_not_of(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnanta6119a82011-05-29 19:57:12 +0000283 size_type find_first_not_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000284
Howard Hinnanta6119a82011-05-29 19:57:12 +0000285 size_type find_last_not_of(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clow0b9db072017-09-07 04:19:32 +0000286 size_type find_last_not_of(basic_string_view<charT, traits> sv, size_type pos = npos) const noexcept;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000287 size_type find_last_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
288 size_type find_last_not_of(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnanta6119a82011-05-29 19:57:12 +0000289 size_type find_last_not_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000290
Howard Hinnanta6119a82011-05-29 19:57:12 +0000291 int compare(const basic_string& str) const noexcept;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000292 int compare(basic_string_view<charT, traits> sv) const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000293 int compare(size_type pos1, size_type n1, const basic_string& str) const;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000294 int compare(size_type pos1, size_type n1, basic_string_view<charT, traits> sv) const;
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000295 int compare(size_type pos1, size_type n1, const basic_string& str,
Marshall Clowa93b5e22014-03-04 19:17:19 +0000296 size_type pos2, size_type n2=npos) const; // C++14
Marshall Clow6ac8de02016-09-24 22:45:42 +0000297 template <class T>
298 int compare(size_type pos1, size_type n1, const T& t,
299 size_type pos2, size_type n2=npos) const; // C++17
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000300 int compare(const value_type* s) const noexcept;
301 int compare(size_type pos1, size_type n1, const value_type* s) const;
302 int compare(size_type pos1, size_type n1, const value_type* s, size_type n2) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000303
304 bool __invariants() const;
305};
306
307template<class charT, class traits, class Allocator>
308basic_string<charT, traits, Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000309operator+(const basic_string<charT, traits, Allocator>& lhs,
310 const basic_string<charT, traits, Allocator>& rhs);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000311
312template<class charT, class traits, class Allocator>
313basic_string<charT, traits, Allocator>
314operator+(const charT* lhs , const basic_string<charT,traits,Allocator>&rhs);
315
316template<class charT, class traits, class Allocator>
317basic_string<charT, traits, Allocator>
318operator+(charT lhs, const basic_string<charT,traits,Allocator>& rhs);
319
320template<class charT, class traits, class Allocator>
321basic_string<charT, traits, Allocator>
322operator+(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs);
323
324template<class charT, class traits, class Allocator>
325basic_string<charT, traits, Allocator>
326operator+(const basic_string<charT, traits, Allocator>& lhs, charT rhs);
327
328template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000329bool operator==(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +0000330 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000331
332template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000333bool operator==(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000334
335template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000336bool operator==(const basic_string<charT,traits,Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000337
Howard Hinnant324bb032010-08-22 00:02:43 +0000338template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000339bool operator!=(const basic_string<charT,traits,Allocator>& lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +0000340 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000341
342template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000343bool operator!=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000344
345template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000346bool operator!=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000347
348template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000349bool operator< (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +0000350 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000351
352template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000353bool operator< (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000354
355template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000356bool operator< (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000357
358template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000359bool operator> (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +0000360 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000361
362template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000363bool operator> (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000364
365template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000366bool operator> (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000367
368template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000369bool operator<=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +0000370 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000371
372template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000373bool operator<=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000374
375template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000376bool operator<=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000377
378template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000379bool operator>=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +0000380 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000381
382template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000383bool operator>=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000384
385template<class charT, class traits, class Allocator>
Howard Hinnanta6119a82011-05-29 19:57:12 +0000386bool operator>=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000387
388template<class charT, class traits, class Allocator>
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000389void swap(basic_string<charT, traits, Allocator>& lhs,
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000390 basic_string<charT, traits, Allocator>& rhs)
391 noexcept(noexcept(lhs.swap(rhs)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000392
393template<class charT, class traits, class Allocator>
394basic_istream<charT, traits>&
395operator>>(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
396
397template<class charT, class traits, class Allocator>
398basic_ostream<charT, traits>&
399operator<<(basic_ostream<charT, traits>& os, const basic_string<charT, traits, Allocator>& str);
400
401template<class charT, class traits, class Allocator>
Howard Hinnant324bb032010-08-22 00:02:43 +0000402basic_istream<charT, traits>&
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000403getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str,
404 charT delim);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000405
406template<class charT, class traits, class Allocator>
407basic_istream<charT, traits>&
408getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
409
410typedef basic_string<char> string;
411typedef basic_string<wchar_t> wstring;
Howard Hinnanta6a062d2010-06-02 18:20:39 +0000412typedef basic_string<char16_t> u16string;
413typedef basic_string<char32_t> u32string;
414
415int stoi (const string& str, size_t* idx = 0, int base = 10);
416long stol (const string& str, size_t* idx = 0, int base = 10);
417unsigned long stoul (const string& str, size_t* idx = 0, int base = 10);
418long long stoll (const string& str, size_t* idx = 0, int base = 10);
419unsigned long long stoull(const string& str, size_t* idx = 0, int base = 10);
420
421float stof (const string& str, size_t* idx = 0);
422double stod (const string& str, size_t* idx = 0);
423long double stold(const string& str, size_t* idx = 0);
424
425string to_string(int val);
426string to_string(unsigned val);
427string to_string(long val);
428string to_string(unsigned long val);
429string to_string(long long val);
430string to_string(unsigned long long val);
431string to_string(float val);
432string to_string(double val);
433string to_string(long double val);
434
435int stoi (const wstring& str, size_t* idx = 0, int base = 10);
436long stol (const wstring& str, size_t* idx = 0, int base = 10);
437unsigned long stoul (const wstring& str, size_t* idx = 0, int base = 10);
438long long stoll (const wstring& str, size_t* idx = 0, int base = 10);
439unsigned long long stoull(const wstring& str, size_t* idx = 0, int base = 10);
440
441float stof (const wstring& str, size_t* idx = 0);
442double stod (const wstring& str, size_t* idx = 0);
443long double stold(const wstring& str, size_t* idx = 0);
444
445wstring to_wstring(int val);
446wstring to_wstring(unsigned val);
447wstring to_wstring(long val);
448wstring to_wstring(unsigned long val);
449wstring to_wstring(long long val);
450wstring to_wstring(unsigned long long val);
451wstring to_wstring(float val);
452wstring to_wstring(double val);
453wstring to_wstring(long double val);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000454
455template <> struct hash<string>;
456template <> struct hash<u16string>;
457template <> struct hash<u32string>;
458template <> struct hash<wstring>;
459
Marshall Clow15234322013-07-23 17:05:24 +0000460basic_string<char> operator "" s( const char *str, size_t len ); // C++14
461basic_string<wchar_t> operator "" s( const wchar_t *str, size_t len ); // C++14
462basic_string<char16_t> operator "" s( const char16_t *str, size_t len ); // C++14
463basic_string<char32_t> operator "" s( const char32_t *str, size_t len ); // C++14
464
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000465} // std
466
467*/
468
469#include <__config>
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000470#include <string_view>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000471#include <iosfwd>
472#include <cstring>
Howard Hinnantadff4892010-05-24 17:49:41 +0000473#include <cstdio> // For EOF.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000474#include <cwchar>
475#include <algorithm>
476#include <iterator>
477#include <utility>
478#include <memory>
479#include <stdexcept>
480#include <type_traits>
481#include <initializer_list>
482#include <__functional_base>
483#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
484#include <cstdint>
485#endif
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000486
Eric Fiselierb9536102014-08-10 23:53:08 +0000487#include <__debug>
488
Howard Hinnant08e17472011-10-17 20:05:10 +0000489#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000490#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000491#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000492
Eric Fiselier018a3d52017-05-31 22:07:49 +0000493_LIBCPP_PUSH_MACROS
494#include <__undef_macros>
495
496
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000497_LIBCPP_BEGIN_NAMESPACE_STD
498
499// fpos
500
501template <class _StateT>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000502class _LIBCPP_TEMPLATE_VIS fpos
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000503{
504private:
505 _StateT __st_;
506 streamoff __off_;
507public:
508 _LIBCPP_INLINE_VISIBILITY fpos(streamoff __off = streamoff()) : __st_(), __off_(__off) {}
509
510 _LIBCPP_INLINE_VISIBILITY operator streamoff() const {return __off_;}
511
512 _LIBCPP_INLINE_VISIBILITY _StateT state() const {return __st_;}
513 _LIBCPP_INLINE_VISIBILITY void state(_StateT __st) {__st_ = __st;}
514
515 _LIBCPP_INLINE_VISIBILITY fpos& operator+=(streamoff __off) {__off_ += __off; return *this;}
516 _LIBCPP_INLINE_VISIBILITY fpos operator+ (streamoff __off) const {fpos __t(*this); __t += __off; return __t;}
517 _LIBCPP_INLINE_VISIBILITY fpos& operator-=(streamoff __off) {__off_ -= __off; return *this;}
518 _LIBCPP_INLINE_VISIBILITY fpos operator- (streamoff __off) const {fpos __t(*this); __t -= __off; return __t;}
519};
520
521template <class _StateT>
522inline _LIBCPP_INLINE_VISIBILITY
523streamoff operator-(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
524 {return streamoff(__x) - streamoff(__y);}
525
526template <class _StateT>
527inline _LIBCPP_INLINE_VISIBILITY
528bool operator==(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
529 {return streamoff(__x) == streamoff(__y);}
530
531template <class _StateT>
532inline _LIBCPP_INLINE_VISIBILITY
533bool operator!=(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
534 {return streamoff(__x) != streamoff(__y);}
535
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000536// basic_string
537
538template<class _CharT, class _Traits, class _Allocator>
539basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000540operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
541 const basic_string<_CharT, _Traits, _Allocator>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000542
543template<class _CharT, class _Traits, class _Allocator>
544basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000545operator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000546
547template<class _CharT, class _Traits, class _Allocator>
548basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000549operator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000550
551template<class _CharT, class _Traits, class _Allocator>
552basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000553operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000554
555template<class _CharT, class _Traits, class _Allocator>
556basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000557operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000558
Shoaib Meenai487562f2017-07-29 02:54:41 +0000559_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&))
560
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000561template <bool>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000562class _LIBCPP_TEMPLATE_VIS __basic_string_common
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000563{
564protected:
Marshall Clow14c09a22016-08-25 15:09:01 +0000565 _LIBCPP_NORETURN void __throw_length_error() const;
566 _LIBCPP_NORETURN void __throw_out_of_range() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000567};
568
569template <bool __b>
570void
571__basic_string_common<__b>::__throw_length_error() const
572{
Marshall Clow14c09a22016-08-25 15:09:01 +0000573 _VSTD::__throw_length_error("basic_string");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000574}
575
576template <bool __b>
577void
578__basic_string_common<__b>::__throw_out_of_range() const
579{
Marshall Clow14c09a22016-08-25 15:09:01 +0000580 _VSTD::__throw_out_of_range("basic_string");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000581}
582
Eric Fiselier833d6442016-09-15 22:27:07 +0000583_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __basic_string_common<true>)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000584
Marshall Clowdf9db312016-01-13 21:54:34 +0000585#ifdef _LIBCPP_NO_EXCEPTIONS
586template <class _Iter>
587struct __libcpp_string_gets_noexcept_iterator_impl : public true_type {};
588#elif defined(_LIBCPP_HAS_NO_NOEXCEPT)
589template <class _Iter>
590struct __libcpp_string_gets_noexcept_iterator_impl : public false_type {};
591#else
592template <class _Iter, bool = __is_forward_iterator<_Iter>::value>
593struct __libcpp_string_gets_noexcept_iterator_impl : public _LIBCPP_BOOL_CONSTANT((
594 noexcept(++(declval<_Iter&>())) &&
595 is_nothrow_assignable<_Iter&, _Iter>::value &&
596 noexcept(declval<_Iter>() == declval<_Iter>()) &&
597 noexcept(*declval<_Iter>())
598)) {};
599
600template <class _Iter>
601struct __libcpp_string_gets_noexcept_iterator_impl<_Iter, false> : public false_type {};
602#endif
603
604
605template <class _Iter>
606struct __libcpp_string_gets_noexcept_iterator
607 : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value || __libcpp_string_gets_noexcept_iterator_impl<_Iter>::value) {};
608
Marshall Clow6ac8de02016-09-24 22:45:42 +0000609template <class _CharT, class _Traits, class _Tp>
610struct __can_be_converted_to_string_view : public _LIBCPP_BOOL_CONSTANT(
611 ( is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
612 !is_convertible<const _Tp&, const _CharT*>::value)) {};
613
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +0000614#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +0000615
616template <class _CharT, size_t = sizeof(_CharT)>
617struct __padding
618{
619 unsigned char __xx[sizeof(_CharT)-1];
620};
621
622template <class _CharT>
623struct __padding<_CharT, 1>
624{
625};
626
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +0000627#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +0000628
Howard Hinnant324bb032010-08-22 00:02:43 +0000629template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000630class _LIBCPP_TEMPLATE_VIS basic_string
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000631 : private __basic_string_common<true>
632{
633public:
634 typedef basic_string __self;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000635 typedef basic_string_view<_CharT, _Traits> __self_view;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000636 typedef _Traits traits_type;
Marshall Clow2d4c3fa2017-03-15 18:41:11 +0000637 typedef _CharT value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000638 typedef _Allocator allocator_type;
Howard Hinnante32b5e22010-11-17 17:55:08 +0000639 typedef allocator_traits<allocator_type> __alloc_traits;
640 typedef typename __alloc_traits::size_type size_type;
641 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000642 typedef value_type& reference;
643 typedef const value_type& const_reference;
Howard Hinnante32b5e22010-11-17 17:55:08 +0000644 typedef typename __alloc_traits::pointer pointer;
645 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000646
Howard Hinnant499cea12013-08-23 17:37:05 +0000647 static_assert(is_pod<value_type>::value, "Character type of basic_string must be a POD");
Marshall Clow2d4c3fa2017-03-15 18:41:11 +0000648 static_assert((is_same<_CharT, typename traits_type::char_type>::value),
Howard Hinnant499cea12013-08-23 17:37:05 +0000649 "traits_type::char_type must be the same type as CharT");
650 static_assert((is_same<typename allocator_type::value_type, value_type>::value),
651 "Allocator::value_type must be same type as value_type");
652#if defined(_LIBCPP_RAW_ITERATORS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000653 typedef pointer iterator;
654 typedef const_pointer const_iterator;
Howard Hinnant324bb032010-08-22 00:02:43 +0000655#else // defined(_LIBCPP_RAW_ITERATORS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000656 typedef __wrap_iter<pointer> iterator;
657 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnant324bb032010-08-22 00:02:43 +0000658#endif // defined(_LIBCPP_RAW_ITERATORS)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000659 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
660 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000661
662private:
Howard Hinnant15467182013-04-30 21:44:48 +0000663
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +0000664#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +0000665
666 struct __long
667 {
668 pointer __data_;
669 size_type __size_;
670 size_type __cap_;
671 };
672
Eric Fiselier5ccf0432017-10-17 13:16:01 +0000673#ifdef _LIBCPP_BIG_ENDIAN
Ben Craigde79ab62017-07-12 01:45:13 +0000674 static const size_type __short_mask = 0x01;
675 static const size_type __long_mask = 0x1ul;
Howard Hinnant15467182013-04-30 21:44:48 +0000676#else // _LIBCPP_BIG_ENDIAN
Ben Craigde79ab62017-07-12 01:45:13 +0000677 static const size_type __short_mask = 0x80;
678 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant15467182013-04-30 21:44:48 +0000679#endif // _LIBCPP_BIG_ENDIAN
680
681 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
682 (sizeof(__long) - 1)/sizeof(value_type) : 2};
683
684 struct __short
685 {
686 value_type __data_[__min_cap];
687 struct
688 : __padding<value_type>
689 {
690 unsigned char __size_;
691 };
692 };
693
694#else
695
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000696 struct __long
697 {
698 size_type __cap_;
699 size_type __size_;
700 pointer __data_;
701 };
702
Eric Fiselier5ccf0432017-10-17 13:16:01 +0000703#ifdef _LIBCPP_BIG_ENDIAN
Ben Craigde79ab62017-07-12 01:45:13 +0000704 static const size_type __short_mask = 0x80;
705 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant324bb032010-08-22 00:02:43 +0000706#else // _LIBCPP_BIG_ENDIAN
Ben Craigde79ab62017-07-12 01:45:13 +0000707 static const size_type __short_mask = 0x01;
708 static const size_type __long_mask = 0x1ul;
Howard Hinnant324bb032010-08-22 00:02:43 +0000709#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000710
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000711 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
712 (sizeof(__long) - 1)/sizeof(value_type) : 2};
713
714 struct __short
715 {
716 union
717 {
718 unsigned char __size_;
Howard Hinnant9c0df142012-10-30 19:06:59 +0000719 value_type __lx;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000720 };
721 value_type __data_[__min_cap];
722 };
723
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +0000724#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +0000725
Howard Hinnant499cea12013-08-23 17:37:05 +0000726 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000727
Howard Hinnant499cea12013-08-23 17:37:05 +0000728 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000729
730 struct __raw
731 {
732 size_type __words[__n_words];
733 };
734
735 struct __rep
736 {
737 union
738 {
739 __long __l;
740 __short __s;
741 __raw __r;
742 };
743 };
744
745 __compressed_pair<__rep, allocator_type> __r_;
746
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000747public:
748 static const size_type npos = -1;
749
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000750 _LIBCPP_INLINE_VISIBILITY basic_string()
751 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clow7b193f72015-06-03 19:56:43 +0000752
753 _LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a)
754#if _LIBCPP_STD_VER <= 14
755 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
756#else
757 _NOEXCEPT;
758#endif
759
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000760 basic_string(const basic_string& __str);
761 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clow7b193f72015-06-03 19:56:43 +0000762
Eric Fiselier3e928972017-04-19 00:28:44 +0000763#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant9f193f22011-01-26 00:06:59 +0000764 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000765 basic_string(basic_string&& __str)
Marshall Clow7b193f72015-06-03 19:56:43 +0000766#if _LIBCPP_STD_VER <= 14
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000767 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clow7b193f72015-06-03 19:56:43 +0000768#else
769 _NOEXCEPT;
770#endif
771
Howard Hinnant9f193f22011-01-26 00:06:59 +0000772 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000773 basic_string(basic_string&& __str, const allocator_type& __a);
Eric Fiselier3e928972017-04-19 00:28:44 +0000774#endif // _LIBCPP_CXX03_LANG
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000775 _LIBCPP_INLINE_VISIBILITY basic_string(const _CharT* __s);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000776 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000777 basic_string(const _CharT* __s, const _Allocator& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000778 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000779 basic_string(const _CharT* __s, size_type __n);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000780 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000781 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000782 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000783 basic_string(size_type __n, _CharT __c);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000784 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000785 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
Marshall Clowf4f7d8f2016-04-07 18:13:41 +0000786 basic_string(const basic_string& __str, size_type __pos, size_type __n,
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000787 const _Allocator& __a = _Allocator());
Marshall Clowf4f7d8f2016-04-07 18:13:41 +0000788 _LIBCPP_INLINE_VISIBILITY
789 basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000790 const _Allocator& __a = _Allocator());
Marshall Clowdb7fa112016-11-14 18:22:19 +0000791 template<class _Tp>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +0000792 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000793 basic_string(const _Tp& __t, size_type __pos, size_type __n,
Marshall Clowdb7fa112016-11-14 18:22:19 +0000794 const allocator_type& __a = allocator_type(),
795 typename enable_if<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, void>::type* = 0);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000796 _LIBCPP_INLINE_VISIBILITY explicit
797 basic_string(__self_view __sv);
798 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000799 basic_string(__self_view __sv, const _Allocator& __a);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000800 template<class _InputIterator>
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000801 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000802 basic_string(_InputIterator __first, _InputIterator __last);
803 template<class _InputIterator>
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000804 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000805 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
Eric Fiselier3e928972017-04-19 00:28:44 +0000806#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000807 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000808 basic_string(initializer_list<_CharT> __il);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000809 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +0000810 basic_string(initializer_list<_CharT> __il, const _Allocator& __a);
Eric Fiselier3e928972017-04-19 00:28:44 +0000811#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000812
Eric Fiselier51eb1d52016-10-31 03:42:50 +0000813 inline ~basic_string();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000814
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000815 _LIBCPP_INLINE_VISIBILITY
816 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
817
Howard Hinnante32b5e22010-11-17 17:55:08 +0000818 basic_string& operator=(const basic_string& __str);
Eric Fiselierf472d6c2017-01-23 21:24:58 +0000819
820#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier37b2be92017-01-17 22:10:32 +0000821 template <class = void>
Eric Fiselierf472d6c2017-01-23 21:24:58 +0000822#endif
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000823 _LIBCPP_INLINE_VISIBILITY
824 basic_string& operator=(__self_view __sv) {return assign(__sv);}
Eric Fiselier3e928972017-04-19 00:28:44 +0000825#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000826 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000827 basic_string& operator=(basic_string&& __str)
Marshall Clowaf961ed2015-08-18 18:57:00 +0000828 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiselier3e928972017-04-19 00:28:44 +0000829 _LIBCPP_INLINE_VISIBILITY
830 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000831#endif
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000832 _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const value_type* __s) {return assign(__s);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000833 basic_string& operator=(value_type __c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000834
Howard Hinnant499cea12013-08-23 17:37:05 +0000835#if _LIBCPP_DEBUG_LEVEL >= 2
836 _LIBCPP_INLINE_VISIBILITY
837 iterator begin() _NOEXCEPT
838 {return iterator(this, __get_pointer());}
839 _LIBCPP_INLINE_VISIBILITY
840 const_iterator begin() const _NOEXCEPT
841 {return const_iterator(this, __get_pointer());}
842 _LIBCPP_INLINE_VISIBILITY
843 iterator end() _NOEXCEPT
844 {return iterator(this, __get_pointer() + size());}
845 _LIBCPP_INLINE_VISIBILITY
846 const_iterator end() const _NOEXCEPT
847 {return const_iterator(this, __get_pointer() + size());}
848#else
Howard Hinnanta6119a82011-05-29 19:57:12 +0000849 _LIBCPP_INLINE_VISIBILITY
850 iterator begin() _NOEXCEPT
851 {return iterator(__get_pointer());}
852 _LIBCPP_INLINE_VISIBILITY
853 const_iterator begin() const _NOEXCEPT
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000854 {return const_iterator(__get_pointer());}
Howard Hinnanta6119a82011-05-29 19:57:12 +0000855 _LIBCPP_INLINE_VISIBILITY
856 iterator end() _NOEXCEPT
857 {return iterator(__get_pointer() + size());}
858 _LIBCPP_INLINE_VISIBILITY
859 const_iterator end() const _NOEXCEPT
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000860 {return const_iterator(__get_pointer() + size());}
Howard Hinnant499cea12013-08-23 17:37:05 +0000861#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnanta6119a82011-05-29 19:57:12 +0000862 _LIBCPP_INLINE_VISIBILITY
863 reverse_iterator rbegin() _NOEXCEPT
864 {return reverse_iterator(end());}
865 _LIBCPP_INLINE_VISIBILITY
866 const_reverse_iterator rbegin() const _NOEXCEPT
867 {return const_reverse_iterator(end());}
868 _LIBCPP_INLINE_VISIBILITY
869 reverse_iterator rend() _NOEXCEPT
870 {return reverse_iterator(begin());}
871 _LIBCPP_INLINE_VISIBILITY
872 const_reverse_iterator rend() const _NOEXCEPT
873 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000874
Howard Hinnanta6119a82011-05-29 19:57:12 +0000875 _LIBCPP_INLINE_VISIBILITY
876 const_iterator cbegin() const _NOEXCEPT
877 {return begin();}
878 _LIBCPP_INLINE_VISIBILITY
879 const_iterator cend() const _NOEXCEPT
880 {return end();}
881 _LIBCPP_INLINE_VISIBILITY
882 const_reverse_iterator crbegin() const _NOEXCEPT
883 {return rbegin();}
884 _LIBCPP_INLINE_VISIBILITY
885 const_reverse_iterator crend() const _NOEXCEPT
886 {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000887
Howard Hinnanta6119a82011-05-29 19:57:12 +0000888 _LIBCPP_INLINE_VISIBILITY size_type size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000889 {return __is_long() ? __get_long_size() : __get_short_size();}
Howard Hinnanta6119a82011-05-29 19:57:12 +0000890 _LIBCPP_INLINE_VISIBILITY size_type length() const _NOEXCEPT {return size();}
891 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT;
892 _LIBCPP_INLINE_VISIBILITY size_type capacity() const _NOEXCEPT
Eric Fiseliere2d48922015-08-28 07:02:42 +0000893 {return (__is_long() ? __get_long_cap()
894 : static_cast<size_type>(__min_cap)) - 1;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000895
896 void resize(size_type __n, value_type __c);
897 _LIBCPP_INLINE_VISIBILITY void resize(size_type __n) {resize(__n, value_type());}
898
Eric Fiselier59e24fe2017-06-01 02:29:37 +0000899 void reserve(size_type __res_arg = 0);
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000900 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +0000901 void shrink_to_fit() _NOEXCEPT {reserve();}
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000902 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +0000903 void clear() _NOEXCEPT;
904 _LIBCPP_INLINE_VISIBILITY bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000905
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000906 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __pos) const _NOEXCEPT;
907 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000908
909 const_reference at(size_type __n) const;
910 reference at(size_type __n);
911
912 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const basic_string& __str) {return append(__str);}
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000913 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(__self_view __sv) {return append(__sv);}
914 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const value_type* __s) {return append(__s);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000915 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(value_type __c) {push_back(__c); return *this;}
Eric Fiselier3e928972017-04-19 00:28:44 +0000916#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000917 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(initializer_list<value_type> __il) {return append(__il);}
Eric Fiselier3e928972017-04-19 00:28:44 +0000918#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000919
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000920 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000921 basic_string& append(const basic_string& __str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000922 _LIBCPP_INLINE_VISIBILITY
923 basic_string& append(__self_view __sv) { return append(__sv.data(), __sv.size()); }
Marshall Clowa93b5e22014-03-04 19:17:19 +0000924 basic_string& append(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow42a87db2016-10-03 23:40:48 +0000925 template <class _Tp>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +0000926 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
927 typename enable_if
Marshall Clow6ac8de02016-09-24 22:45:42 +0000928 <
929 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
930 basic_string&
931 >::type
932 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000933 basic_string& append(const value_type* __s, size_type __n);
934 basic_string& append(const value_type* __s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000935 basic_string& append(size_type __n, value_type __c);
Eric Fiselier026d38e2016-10-31 02:46:25 +0000936 template <class _ForwardIterator>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +0000937 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
938 basic_string& __append_forward_unsafe(_ForwardIterator, _ForwardIterator);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000939 template<class _InputIterator>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +0000940 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
941 typename enable_if
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000942 <
Marshall Clowdf9db312016-01-13 21:54:34 +0000943 __is_exactly_input_iterator<_InputIterator>::value
944 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000945 basic_string&
946 >::type
Eric Fiselier026d38e2016-10-31 02:46:25 +0000947 _LIBCPP_INLINE_VISIBILITY
948 append(_InputIterator __first, _InputIterator __last) {
949 const basic_string __temp (__first, __last, __alloc());
950 append(__temp.data(), __temp.size());
951 return *this;
952 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000953 template<class _ForwardIterator>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +0000954 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
955 typename enable_if
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000956 <
Marshall Clowdf9db312016-01-13 21:54:34 +0000957 __is_forward_iterator<_ForwardIterator>::value
958 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000959 basic_string&
960 >::type
Eric Fiselier026d38e2016-10-31 02:46:25 +0000961 _LIBCPP_INLINE_VISIBILITY
962 append(_ForwardIterator __first, _ForwardIterator __last) {
963 return __append_forward_unsafe(__first, __last);
964 }
965
Eric Fiselier3e928972017-04-19 00:28:44 +0000966#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant8d7a9552010-09-23 17:31:07 +0000967 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000968 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Eric Fiselier3e928972017-04-19 00:28:44 +0000969#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000970
971 void push_back(value_type __c);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000972 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000973 void pop_back();
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000974 _LIBCPP_INLINE_VISIBILITY reference front();
975 _LIBCPP_INLINE_VISIBILITY const_reference front() const;
976 _LIBCPP_INLINE_VISIBILITY reference back();
977 _LIBCPP_INLINE_VISIBILITY const_reference back() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000978
Howard Hinnant2d72b1e2010-12-17 14:46:43 +0000979 _LIBCPP_INLINE_VISIBILITY
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000980 basic_string& assign(__self_view __sv) { return assign(__sv.data(), __sv.size()); }
981 _LIBCPP_INLINE_VISIBILITY
Marshall Clowf40ec902016-03-09 18:08:29 +0000982 basic_string& assign(const basic_string& __str) { return *this = __str; }
Eric Fiselier3e928972017-04-19 00:28:44 +0000983#ifndef _LIBCPP_CXX03_LANG
Howard Hinnanta6119a82011-05-29 19:57:12 +0000984 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier59e24fe2017-06-01 02:29:37 +0000985 basic_string& assign(basic_string&& __str)
Marshall Clow7ed093b2015-10-05 16:17:34 +0000986 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Eric Fiselier59e24fe2017-06-01 02:29:37 +0000987 {*this = _VSTD::move(__str); return *this;}
Howard Hinnanta6119a82011-05-29 19:57:12 +0000988#endif
Marshall Clowa93b5e22014-03-04 19:17:19 +0000989 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow42a87db2016-10-03 23:40:48 +0000990 template <class _Tp>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +0000991 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
992 typename enable_if
Marshall Clow6ac8de02016-09-24 22:45:42 +0000993 <
994 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
995 basic_string&
996 >::type
Eric Fiselier59e24fe2017-06-01 02:29:37 +0000997 assign(const _Tp & __t, size_type __pos, size_type __n=npos);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000998 basic_string& assign(const value_type* __s, size_type __n);
999 basic_string& assign(const value_type* __s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001000 basic_string& assign(size_type __n, value_type __c);
1001 template<class _InputIterator>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +00001002 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1003 typename enable_if
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001004 <
Marshall Clowdf9db312016-01-13 21:54:34 +00001005 __is_exactly_input_iterator<_InputIterator>::value
1006 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001007 basic_string&
1008 >::type
1009 assign(_InputIterator __first, _InputIterator __last);
1010 template<class _ForwardIterator>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +00001011 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1012 typename enable_if
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001013 <
Marshall Clowdf9db312016-01-13 21:54:34 +00001014 __is_forward_iterator<_ForwardIterator>::value
1015 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001016 basic_string&
1017 >::type
1018 assign(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselier3e928972017-04-19 00:28:44 +00001019#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant8d7a9552010-09-23 17:31:07 +00001020 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001021 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Eric Fiselier3e928972017-04-19 00:28:44 +00001022#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001023
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001024 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001025 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001026 _LIBCPP_INLINE_VISIBILITY
1027 basic_string& insert(size_type __pos1, __self_view __sv) { return insert(__pos1, __sv.data(), __sv.size()); }
Marshall Clow6ac8de02016-09-24 22:45:42 +00001028 template <class _Tp>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +00001029 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1030 typename enable_if
Marshall Clow6ac8de02016-09-24 22:45:42 +00001031 <
1032 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1033 basic_string&
1034 >::type
1035 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Marshall Clowa93b5e22014-03-04 19:17:19 +00001036 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001037 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1038 basic_string& insert(size_type __pos, const value_type* __s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001039 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1040 iterator insert(const_iterator __pos, value_type __c);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001041 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001042 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1043 template<class _InputIterator>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +00001044 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1045 typename enable_if
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001046 <
Marshall Clowdf9db312016-01-13 21:54:34 +00001047 __is_exactly_input_iterator<_InputIterator>::value
1048 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001049 iterator
1050 >::type
1051 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1052 template<class _ForwardIterator>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +00001053 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1054 typename enable_if
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001055 <
Marshall Clowdf9db312016-01-13 21:54:34 +00001056 __is_forward_iterator<_ForwardIterator>::value
1057 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001058 iterator
1059 >::type
1060 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselier3e928972017-04-19 00:28:44 +00001061#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant8d7a9552010-09-23 17:31:07 +00001062 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001063 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1064 {return insert(__pos, __il.begin(), __il.end());}
Eric Fiselier3e928972017-04-19 00:28:44 +00001065#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001066
1067 basic_string& erase(size_type __pos = 0, size_type __n = npos);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001068 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001069 iterator erase(const_iterator __pos);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001070 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001071 iterator erase(const_iterator __first, const_iterator __last);
1072
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001073 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001074 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001075 _LIBCPP_INLINE_VISIBILITY
1076 basic_string& replace(size_type __pos1, size_type __n1, __self_view __sv) { return replace(__pos1, __n1, __sv.data(), __sv.size()); }
Marshall Clowa93b5e22014-03-04 19:17:19 +00001077 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2=npos);
Marshall Clow6ac8de02016-09-24 22:45:42 +00001078 template <class _Tp>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +00001079 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1080 typename enable_if
Marshall Clow6ac8de02016-09-24 22:45:42 +00001081 <
1082 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1083 basic_string&
1084 >::type
1085 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001086 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
1087 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001088 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001089 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7b2cb482010-11-17 21:11:40 +00001090 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001091 _LIBCPP_INLINE_VISIBILITY
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001092 basic_string& replace(const_iterator __i1, const_iterator __i2, __self_view __sv) { return replace(__i1 - begin(), __i2 - __i1, __sv); }
1093 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001094 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001095 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001096 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001097 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7b2cb482010-11-17 21:11:40 +00001098 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001099 template<class _InputIterator>
Shoaib Meenai24e8dbd2017-03-02 03:02:50 +00001100 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1101 typename enable_if
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001102 <
1103 __is_input_iterator<_InputIterator>::value,
1104 basic_string&
1105 >::type
Howard Hinnant7b2cb482010-11-17 21:11:40 +00001106 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Eric Fiselier3e928972017-04-19 00:28:44 +00001107#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant8d7a9552010-09-23 17:31:07 +00001108 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7b2cb482010-11-17 21:11:40 +00001109 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001110 {return replace(__i1, __i2, __il.begin(), __il.end());}
Eric Fiselier3e928972017-04-19 00:28:44 +00001111#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001112
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001113 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001114 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001115 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1116
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001117 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001118 void swap(basic_string& __str)
Marshall Clow7d914d12015-07-13 20:04:56 +00001119#if _LIBCPP_STD_VER >= 14
Eric Fiselier47257c42016-12-28 05:53:01 +00001120 _NOEXCEPT_DEBUG;
Marshall Clow7d914d12015-07-13 20:04:56 +00001121#else
Eric Fiselier47257c42016-12-28 05:53:01 +00001122 _NOEXCEPT_DEBUG_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow7d914d12015-07-13 20:04:56 +00001123 __is_nothrow_swappable<allocator_type>::value);
1124#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001125
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001126 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001127 const value_type* c_str() const _NOEXCEPT {return data();}
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001128 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001129 const value_type* data() const _NOEXCEPT {return _VSTD::__to_raw_pointer(__get_pointer());}
Marshall Clowf532a702016-03-08 15:44:30 +00001130#if _LIBCPP_STD_VER > 14
1131 _LIBCPP_INLINE_VISIBILITY
1132 value_type* data() _NOEXCEPT {return _VSTD::__to_raw_pointer(__get_pointer());}
1133#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001134
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001135 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001136 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001137
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001138 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001139 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001140 _LIBCPP_INLINE_VISIBILITY
1141 size_type find(__self_view __sv, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001142 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001143 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001144 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnanta6119a82011-05-29 19:57:12 +00001145 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001146
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001147 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001148 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001149 _LIBCPP_INLINE_VISIBILITY
Marshall Clow0b9db072017-09-07 04:19:32 +00001150 size_type rfind(__self_view __sv, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001151 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001152 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001153 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnanta6119a82011-05-29 19:57:12 +00001154 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001155
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001156 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001157 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001158 _LIBCPP_INLINE_VISIBILITY
1159 size_type find_first_of(__self_view __sv, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001160 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001161 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001162 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001163 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001164 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001165
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001166 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001167 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001168 _LIBCPP_INLINE_VISIBILITY
Marshall Clow0b9db072017-09-07 04:19:32 +00001169 size_type find_last_of(__self_view __sv, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001170 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001171 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001172 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001173 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001174 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001175
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001176 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001177 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001178 _LIBCPP_INLINE_VISIBILITY
1179 size_type find_first_not_of(__self_view __sv, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001180 size_type find_first_not_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnanta6119a82011-05-29 19:57:12 +00001181 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001182 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnanta6119a82011-05-29 19:57:12 +00001183 _LIBCPP_INLINE_VISIBILITY
1184 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1185
1186 _LIBCPP_INLINE_VISIBILITY
1187 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001188 _LIBCPP_INLINE_VISIBILITY
Marshall Clow0b9db072017-09-07 04:19:32 +00001189 size_type find_last_not_of(__self_view __sv, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001190 size_type find_last_not_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnanta6119a82011-05-29 19:57:12 +00001191 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001192 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnanta6119a82011-05-29 19:57:12 +00001193 _LIBCPP_INLINE_VISIBILITY
1194 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1195
1196 _LIBCPP_INLINE_VISIBILITY
1197 int compare(const basic_string& __str) const _NOEXCEPT;
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001198 _LIBCPP_INLINE_VISIBILITY
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001199 int compare(__self_view __sv) const _NOEXCEPT;
1200 _LIBCPP_INLINE_VISIBILITY
1201 int compare(size_type __pos1, size_type __n1, __self_view __sv) const;
1202 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001203 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Marshall Clowa93b5e22014-03-04 19:17:19 +00001204 int compare(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2=npos) const;
Marshall Clow6ac8de02016-09-24 22:45:42 +00001205 template <class _Tp>
Eric Fiselier9dbc0532016-10-14 05:29:46 +00001206 inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow6ac8de02016-09-24 22:45:42 +00001207 typename enable_if
1208 <
1209 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1210 int
1211 >::type
1212 compare(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos) const;
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001213 int compare(const value_type* __s) const _NOEXCEPT;
1214 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1215 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001216
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001217 _LIBCPP_INLINE_VISIBILITY bool __invariants() const;
Howard Hinnant08dd2532013-04-22 23:55:13 +00001218
1219 _LIBCPP_INLINE_VISIBILITY
1220 bool __is_long() const _NOEXCEPT
1221 {return bool(__r_.first().__s.__size_ & __short_mask);}
1222
Howard Hinnant499cea12013-08-23 17:37:05 +00001223#if _LIBCPP_DEBUG_LEVEL >= 2
1224
1225 bool __dereferenceable(const const_iterator* __i) const;
1226 bool __decrementable(const const_iterator* __i) const;
1227 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1228 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1229
1230#endif // _LIBCPP_DEBUG_LEVEL >= 2
1231
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001232private:
Howard Hinnanta6119a82011-05-29 19:57:12 +00001233 _LIBCPP_INLINE_VISIBILITY
1234 allocator_type& __alloc() _NOEXCEPT
1235 {return __r_.second();}
1236 _LIBCPP_INLINE_VISIBILITY
1237 const allocator_type& __alloc() const _NOEXCEPT
1238 {return __r_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001239
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +00001240#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +00001241
Howard Hinnanta6119a82011-05-29 19:57:12 +00001242 _LIBCPP_INLINE_VISIBILITY
Howard Hinnanta6119a82011-05-29 19:57:12 +00001243 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiselier5ccf0432017-10-17 13:16:01 +00001244# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001245 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
Howard Hinnant15467182013-04-30 21:44:48 +00001246# else
1247 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1248# endif
1249
Howard Hinnanta6119a82011-05-29 19:57:12 +00001250 _LIBCPP_INLINE_VISIBILITY
1251 size_type __get_short_size() const _NOEXCEPT
Eric Fiselier5ccf0432017-10-17 13:16:01 +00001252# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001253 {return __r_.first().__s.__size_ >> 1;}
Howard Hinnant15467182013-04-30 21:44:48 +00001254# else
1255 {return __r_.first().__s.__size_;}
1256# endif
1257
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +00001258#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +00001259
1260 _LIBCPP_INLINE_VISIBILITY
1261 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiselier5ccf0432017-10-17 13:16:01 +00001262# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant15467182013-04-30 21:44:48 +00001263 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1264# else
1265 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
1266# endif
1267
1268 _LIBCPP_INLINE_VISIBILITY
1269 size_type __get_short_size() const _NOEXCEPT
Eric Fiselier5ccf0432017-10-17 13:16:01 +00001270# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant15467182013-04-30 21:44:48 +00001271 {return __r_.first().__s.__size_;}
1272# else
1273 {return __r_.first().__s.__size_ >> 1;}
1274# endif
1275
Evgeniy Stepanov4f01aa82015-10-13 23:48:28 +00001276#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant15467182013-04-30 21:44:48 +00001277
Howard Hinnanta6119a82011-05-29 19:57:12 +00001278 _LIBCPP_INLINE_VISIBILITY
1279 void __set_long_size(size_type __s) _NOEXCEPT
1280 {__r_.first().__l.__size_ = __s;}
1281 _LIBCPP_INLINE_VISIBILITY
1282 size_type __get_long_size() const _NOEXCEPT
1283 {return __r_.first().__l.__size_;}
1284 _LIBCPP_INLINE_VISIBILITY
1285 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001286 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1287
Howard Hinnanta6119a82011-05-29 19:57:12 +00001288 _LIBCPP_INLINE_VISIBILITY
1289 void __set_long_cap(size_type __s) _NOEXCEPT
1290 {__r_.first().__l.__cap_ = __long_mask | __s;}
1291 _LIBCPP_INLINE_VISIBILITY
1292 size_type __get_long_cap() const _NOEXCEPT
Howard Hinnantec3773c2011-12-01 20:21:04 +00001293 {return __r_.first().__l.__cap_ & size_type(~__long_mask);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001294
Howard Hinnanta6119a82011-05-29 19:57:12 +00001295 _LIBCPP_INLINE_VISIBILITY
1296 void __set_long_pointer(pointer __p) _NOEXCEPT
1297 {__r_.first().__l.__data_ = __p;}
1298 _LIBCPP_INLINE_VISIBILITY
1299 pointer __get_long_pointer() _NOEXCEPT
1300 {return __r_.first().__l.__data_;}
1301 _LIBCPP_INLINE_VISIBILITY
1302 const_pointer __get_long_pointer() const _NOEXCEPT
1303 {return __r_.first().__l.__data_;}
1304 _LIBCPP_INLINE_VISIBILITY
1305 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001306 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnanta6119a82011-05-29 19:57:12 +00001307 _LIBCPP_INLINE_VISIBILITY
1308 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001309 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnanta6119a82011-05-29 19:57:12 +00001310 _LIBCPP_INLINE_VISIBILITY
1311 pointer __get_pointer() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001312 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Howard Hinnanta6119a82011-05-29 19:57:12 +00001313 _LIBCPP_INLINE_VISIBILITY
1314 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001315 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1316
Howard Hinnanta6119a82011-05-29 19:57:12 +00001317 _LIBCPP_INLINE_VISIBILITY
1318 void __zero() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001319 {
1320 size_type (&__a)[__n_words] = __r_.first().__r.__words;
1321 for (unsigned __i = 0; __i < __n_words; ++__i)
1322 __a[__i] = 0;
1323 }
1324
1325 template <size_type __a> static
Howard Hinnanta6119a82011-05-29 19:57:12 +00001326 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7f764502013-08-14 18:00:20 +00001327 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiseliere2d48922015-08-28 07:02:42 +00001328 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001329 enum {__alignment = 16};
Howard Hinnanta6119a82011-05-29 19:57:12 +00001330 static _LIBCPP_INLINE_VISIBILITY
1331 size_type __recommend(size_type __s) _NOEXCEPT
Eric Fiseliere2d48922015-08-28 07:02:42 +00001332 {return (__s < __min_cap ? static_cast<size_type>(__min_cap) :
Howard Hinnant7f764502013-08-14 18:00:20 +00001333 __align_it<sizeof(value_type) < __alignment ?
Howard Hinnanta6119a82011-05-29 19:57:12 +00001334 __alignment/sizeof(value_type) : 1 > (__s+1)) - 1;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001335
Duncan P. N. Exon Smithed3c0e62017-04-01 03:20:48 +00001336 inline
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001337 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Duncan P. N. Exon Smithed3c0e62017-04-01 03:20:48 +00001338 inline
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001339 void __init(const value_type* __s, size_type __sz);
Duncan P. N. Exon Smithed3c0e62017-04-01 03:20:48 +00001340 inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001341 void __init(size_type __n, value_type __c);
Howard Hinnant324bb032010-08-22 00:02:43 +00001342
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001343 template <class _InputIterator>
Duncan P. N. Exon Smithed3c0e62017-04-01 03:20:48 +00001344 inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001345 typename enable_if
1346 <
Marshall Clowdf9db312016-01-13 21:54:34 +00001347 __is_exactly_input_iterator<_InputIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001348 void
1349 >::type
1350 __init(_InputIterator __first, _InputIterator __last);
1351
1352 template <class _ForwardIterator>
Duncan P. N. Exon Smithed3c0e62017-04-01 03:20:48 +00001353 inline
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001354 typename enable_if
1355 <
1356 __is_forward_iterator<_ForwardIterator>::value,
1357 void
1358 >::type
1359 __init(_ForwardIterator __first, _ForwardIterator __last);
1360
1361 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant324bb032010-08-22 00:02:43 +00001362 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001363 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1364 size_type __n_copy, size_type __n_del,
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001365 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001366
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001367 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001368 void __erase_to_end(size_type __pos);
1369
Howard Hinnante32b5e22010-11-17 17:55:08 +00001370 _LIBCPP_INLINE_VISIBILITY
1371 void __copy_assign_alloc(const basic_string& __str)
1372 {__copy_assign_alloc(__str, integral_constant<bool,
1373 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1374
1375 _LIBCPP_INLINE_VISIBILITY
1376 void __copy_assign_alloc(const basic_string& __str, true_type)
1377 {
Marshall Clow9247fd22017-01-31 03:40:52 +00001378 if (__alloc() == __str.__alloc())
1379 __alloc() = __str.__alloc();
1380 else
Howard Hinnante32b5e22010-11-17 17:55:08 +00001381 {
Marshall Clow9247fd22017-01-31 03:40:52 +00001382 if (!__str.__is_long())
1383 {
1384 clear();
1385 shrink_to_fit();
1386 __alloc() = __str.__alloc();
1387 }
1388 else
1389 {
1390 allocator_type __a = __str.__alloc();
1391 pointer __p = __alloc_traits::allocate(__a, __str.__get_long_cap());
1392 clear();
1393 shrink_to_fit();
1394 __alloc() = _VSTD::move(__a);
1395 __set_long_pointer(__p);
1396 __set_long_cap(__str.__get_long_cap());
1397 __set_long_size(__str.size());
1398 }
Howard Hinnante32b5e22010-11-17 17:55:08 +00001399 }
Howard Hinnante32b5e22010-11-17 17:55:08 +00001400 }
1401
1402 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00001403 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnante32b5e22010-11-17 17:55:08 +00001404 {}
1405
Eric Fiselier3e928972017-04-19 00:28:44 +00001406#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001407 _LIBCPP_INLINE_VISIBILITY
Marshall Clowaf961ed2015-08-18 18:57:00 +00001408 void __move_assign(basic_string& __str, false_type)
1409 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001410 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001411 void __move_assign(basic_string& __str, true_type)
Marshall Clowaf961ed2015-08-18 18:57:00 +00001412#if _LIBCPP_STD_VER > 14
1413 _NOEXCEPT;
1414#else
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001415 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001416#endif
Marshall Clowaf961ed2015-08-18 18:57:00 +00001417#endif
Howard Hinnante32b5e22010-11-17 17:55:08 +00001418
1419 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00001420 void
Howard Hinnant9cbee432011-09-02 20:42:31 +00001421 __move_assign_alloc(basic_string& __str)
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00001422 _NOEXCEPT_(
1423 !__alloc_traits::propagate_on_container_move_assignment::value ||
1424 is_nothrow_move_assignable<allocator_type>::value)
1425 {__move_assign_alloc(__str, integral_constant<bool,
1426 __alloc_traits::propagate_on_container_move_assignment::value>());}
1427
1428 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant9cbee432011-09-02 20:42:31 +00001429 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00001430 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1431 {
1432 __alloc() = _VSTD::move(__c.__alloc());
1433 }
1434
1435 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantec3773c2011-12-01 20:21:04 +00001436 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00001437 _NOEXCEPT
1438 {}
1439
Howard Hinnant2d72b1e2010-12-17 14:46:43 +00001440 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
1441 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(size_type);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001442
1443 friend basic_string operator+<>(const basic_string&, const basic_string&);
1444 friend basic_string operator+<>(const value_type*, const basic_string&);
1445 friend basic_string operator+<>(value_type, const basic_string&);
1446 friend basic_string operator+<>(const basic_string&, const value_type*);
1447 friend basic_string operator+<>(const basic_string&, value_type);
1448};
1449
1450template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001451inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001452void
1453basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
1454{
Howard Hinnant499cea12013-08-23 17:37:05 +00001455#if _LIBCPP_DEBUG_LEVEL >= 2
1456 __get_db()->__invalidate_all(this);
1457#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001458}
1459
1460template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001461inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001462void
Howard Hinnantec3773c2011-12-01 20:21:04 +00001463basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type
Howard Hinnant499cea12013-08-23 17:37:05 +00001464#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantec3773c2011-12-01 20:21:04 +00001465 __pos
1466#endif
1467 )
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001468{
Howard Hinnant499cea12013-08-23 17:37:05 +00001469#if _LIBCPP_DEBUG_LEVEL >= 2
1470 __c_node* __c = __get_db()->__find_c_and_lock(this);
1471 if (__c)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001472 {
Howard Hinnant499cea12013-08-23 17:37:05 +00001473 const_pointer __new_last = __get_pointer() + __pos;
1474 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001475 {
Howard Hinnant499cea12013-08-23 17:37:05 +00001476 --__p;
1477 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1478 if (__i->base() > __new_last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001479 {
Howard Hinnant499cea12013-08-23 17:37:05 +00001480 (*__p)->__c_ = nullptr;
1481 if (--__c->end_ != __p)
1482 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001483 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001484 }
Howard Hinnant499cea12013-08-23 17:37:05 +00001485 __get_db()->unlock();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001486 }
Howard Hinnant499cea12013-08-23 17:37:05 +00001487#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001488}
1489
1490template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001491inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001492basic_string<_CharT, _Traits, _Allocator>::basic_string()
Marshall Clowc912c0c2015-06-04 02:05:41 +00001493 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001494{
Howard Hinnant499cea12013-08-23 17:37:05 +00001495#if _LIBCPP_DEBUG_LEVEL >= 2
1496 __get_db()->__insert_c(this);
1497#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001498 __zero();
1499}
1500
1501template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001502inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001503basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
Eric Fiselier692177d2015-07-18 20:40:46 +00001504#if _LIBCPP_STD_VER <= 14
1505 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1506#else
1507 _NOEXCEPT
1508#endif
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001509: __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001510{
Howard Hinnant499cea12013-08-23 17:37:05 +00001511#if _LIBCPP_DEBUG_LEVEL >= 2
1512 __get_db()->__insert_c(this);
1513#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001514 __zero();
1515}
1516
1517template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier6dbed462016-09-16 00:00:48 +00001518void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1519 size_type __sz,
1520 size_type __reserve)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001521{
1522 if (__reserve > max_size())
1523 this->__throw_length_error();
1524 pointer __p;
1525 if (__reserve < __min_cap)
1526 {
1527 __set_short_size(__sz);
1528 __p = __get_short_pointer();
1529 }
1530 else
1531 {
1532 size_type __cap = __recommend(__reserve);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001533 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001534 __set_long_pointer(__p);
1535 __set_long_cap(__cap+1);
1536 __set_long_size(__sz);
1537 }
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001538 traits_type::copy(_VSTD::__to_raw_pointer(__p), __s, __sz);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001539 traits_type::assign(__p[__sz], value_type());
1540}
1541
1542template <class _CharT, class _Traits, class _Allocator>
1543void
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001544basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001545{
1546 if (__sz > max_size())
1547 this->__throw_length_error();
1548 pointer __p;
1549 if (__sz < __min_cap)
1550 {
1551 __set_short_size(__sz);
1552 __p = __get_short_pointer();
1553 }
1554 else
1555 {
1556 size_type __cap = __recommend(__sz);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001557 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001558 __set_long_pointer(__p);
1559 __set_long_cap(__cap+1);
1560 __set_long_size(__sz);
1561 }
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001562 traits_type::copy(_VSTD::__to_raw_pointer(__p), __s, __sz);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001563 traits_type::assign(__p[__sz], value_type());
1564}
1565
1566template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001567inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001568basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001569{
Howard Hinnant499cea12013-08-23 17:37:05 +00001570 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001571 __init(__s, traits_type::length(__s));
Howard Hinnant499cea12013-08-23 17:37:05 +00001572#if _LIBCPP_DEBUG_LEVEL >= 2
1573 __get_db()->__insert_c(this);
1574#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001575}
1576
1577template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001578inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001579basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001580 : __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001581{
Howard Hinnant499cea12013-08-23 17:37:05 +00001582 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001583 __init(__s, traits_type::length(__s));
Howard Hinnant499cea12013-08-23 17:37:05 +00001584#if _LIBCPP_DEBUG_LEVEL >= 2
1585 __get_db()->__insert_c(this);
1586#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001587}
1588
1589template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001590inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001591basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001592{
Howard Hinnant499cea12013-08-23 17:37:05 +00001593 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001594 __init(__s, __n);
Howard Hinnant499cea12013-08-23 17:37:05 +00001595#if _LIBCPP_DEBUG_LEVEL >= 2
1596 __get_db()->__insert_c(this);
1597#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001598}
1599
1600template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001601inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001602basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001603 : __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001604{
Howard Hinnant499cea12013-08-23 17:37:05 +00001605 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001606 __init(__s, __n);
Howard Hinnant499cea12013-08-23 17:37:05 +00001607#if _LIBCPP_DEBUG_LEVEL >= 2
1608 __get_db()->__insert_c(this);
1609#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001610}
1611
1612template <class _CharT, class _Traits, class _Allocator>
1613basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001614 : __r_(__second_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001615{
1616 if (!__str.__is_long())
1617 __r_.first().__r = __str.__r_.first().__r;
1618 else
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001619 __init(_VSTD::__to_raw_pointer(__str.__get_long_pointer()), __str.__get_long_size());
Howard Hinnant499cea12013-08-23 17:37:05 +00001620#if _LIBCPP_DEBUG_LEVEL >= 2
1621 __get_db()->__insert_c(this);
1622#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001623}
1624
1625template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001626basic_string<_CharT, _Traits, _Allocator>::basic_string(
1627 const basic_string& __str, const allocator_type& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001628 : __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001629{
1630 if (!__str.__is_long())
1631 __r_.first().__r = __str.__r_.first().__r;
1632 else
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001633 __init(_VSTD::__to_raw_pointer(__str.__get_long_pointer()), __str.__get_long_size());
Howard Hinnant499cea12013-08-23 17:37:05 +00001634#if _LIBCPP_DEBUG_LEVEL >= 2
1635 __get_db()->__insert_c(this);
1636#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001637}
1638
Eric Fiselier3e928972017-04-19 00:28:44 +00001639#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001640
1641template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001642inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001643basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
Marshall Clow7b193f72015-06-03 19:56:43 +00001644#if _LIBCPP_STD_VER <= 14
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00001645 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clow7b193f72015-06-03 19:56:43 +00001646#else
1647 _NOEXCEPT
1648#endif
Howard Hinnant0949eed2011-06-30 21:18:19 +00001649 : __r_(_VSTD::move(__str.__r_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001650{
1651 __str.__zero();
Howard Hinnant499cea12013-08-23 17:37:05 +00001652#if _LIBCPP_DEBUG_LEVEL >= 2
1653 __get_db()->__insert_c(this);
1654 if (__is_long())
1655 __get_db()->swap(this, &__str);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001656#endif
1657}
1658
1659template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001660inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001661basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001662 : __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001663{
Marshall Clowd5549cc2014-07-17 15:32:20 +00001664 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001665 __init(_VSTD::__to_raw_pointer(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd5549cc2014-07-17 15:32:20 +00001666 else
1667 {
1668 __r_.first().__r = __str.__r_.first().__r;
1669 __str.__zero();
1670 }
Howard Hinnant499cea12013-08-23 17:37:05 +00001671#if _LIBCPP_DEBUG_LEVEL >= 2
1672 __get_db()->__insert_c(this);
1673 if (__is_long())
1674 __get_db()->swap(this, &__str);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001675#endif
1676}
1677
Eric Fiselier3e928972017-04-19 00:28:44 +00001678#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001679
1680template <class _CharT, class _Traits, class _Allocator>
1681void
1682basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
1683{
1684 if (__n > max_size())
1685 this->__throw_length_error();
1686 pointer __p;
1687 if (__n < __min_cap)
1688 {
1689 __set_short_size(__n);
1690 __p = __get_short_pointer();
1691 }
1692 else
1693 {
1694 size_type __cap = __recommend(__n);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001695 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001696 __set_long_pointer(__p);
1697 __set_long_cap(__cap+1);
1698 __set_long_size(__n);
1699 }
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001700 traits_type::assign(_VSTD::__to_raw_pointer(__p), __n, __c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001701 traits_type::assign(__p[__n], value_type());
1702}
1703
1704template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001705inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001706basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001707{
1708 __init(__n, __c);
Howard Hinnant499cea12013-08-23 17:37:05 +00001709#if _LIBCPP_DEBUG_LEVEL >= 2
1710 __get_db()->__insert_c(this);
1711#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001712}
1713
1714template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001715inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001716basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001717 : __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001718{
1719 __init(__n, __c);
Howard Hinnant499cea12013-08-23 17:37:05 +00001720#if _LIBCPP_DEBUG_LEVEL >= 2
1721 __get_db()->__insert_c(this);
1722#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001723}
1724
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001725template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001726basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
1727 size_type __pos, size_type __n,
1728 const _Allocator& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001729 : __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001730{
1731 size_type __str_sz = __str.size();
1732 if (__pos > __str_sz)
1733 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001734 __init(__str.data() + __pos, _VSTD::min(__n, __str_sz - __pos));
Howard Hinnant499cea12013-08-23 17:37:05 +00001735#if _LIBCPP_DEBUG_LEVEL >= 2
1736 __get_db()->__insert_c(this);
1737#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001738}
1739
1740template <class _CharT, class _Traits, class _Allocator>
Marshall Clowf4f7d8f2016-04-07 18:13:41 +00001741inline _LIBCPP_INLINE_VISIBILITY
1742basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001743 const _Allocator& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001744 : __r_(__second_tag(), __a)
Marshall Clowf4f7d8f2016-04-07 18:13:41 +00001745{
1746 size_type __str_sz = __str.size();
1747 if (__pos > __str_sz)
1748 this->__throw_out_of_range();
1749 __init(__str.data() + __pos, __str_sz - __pos);
1750#if _LIBCPP_DEBUG_LEVEL >= 2
1751 __get_db()->__insert_c(this);
1752#endif
1753}
1754
1755template <class _CharT, class _Traits, class _Allocator>
Marshall Clowdb7fa112016-11-14 18:22:19 +00001756template <class _Tp>
1757basic_string<_CharT, _Traits, _Allocator>::basic_string(
1758 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a,
1759 typename enable_if<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value, void>::type *)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001760 : __r_(__second_tag(), __a)
Marshall Clowdb7fa112016-11-14 18:22:19 +00001761{
1762 __self_view __sv = __self_view(__t).substr(__pos, __n);
1763 __init(__sv.data(), __sv.size());
1764#if _LIBCPP_DEBUG_LEVEL >= 2
1765 __get_db()->__insert_c(this);
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001766#endif
Marshall Clowdb7fa112016-11-14 18:22:19 +00001767}
1768
1769template <class _CharT, class _Traits, class _Allocator>
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001770inline _LIBCPP_INLINE_VISIBILITY
1771basic_string<_CharT, _Traits, _Allocator>::basic_string(__self_view __sv)
1772{
1773 __init(__sv.data(), __sv.size());
1774#if _LIBCPP_DEBUG_LEVEL >= 2
1775 __get_db()->__insert_c(this);
1776#endif
1777}
1778
1779template <class _CharT, class _Traits, class _Allocator>
1780inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001781basic_string<_CharT, _Traits, _Allocator>::basic_string(__self_view __sv, const _Allocator& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001782 : __r_(__second_tag(), __a)
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001783{
1784 __init(__sv.data(), __sv.size());
1785#if _LIBCPP_DEBUG_LEVEL >= 2
1786 __get_db()->__insert_c(this);
1787#endif
1788}
1789
1790template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001791template <class _InputIterator>
1792typename enable_if
1793<
Marshall Clowdf9db312016-01-13 21:54:34 +00001794 __is_exactly_input_iterator<_InputIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001795 void
1796>::type
1797basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
1798{
1799 __zero();
1800#ifndef _LIBCPP_NO_EXCEPTIONS
1801 try
1802 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001803#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001804 for (; __first != __last; ++__first)
1805 push_back(*__first);
1806#ifndef _LIBCPP_NO_EXCEPTIONS
1807 }
1808 catch (...)
1809 {
1810 if (__is_long())
Howard Hinnante32b5e22010-11-17 17:55:08 +00001811 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001812 throw;
1813 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001814#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001815}
1816
1817template <class _CharT, class _Traits, class _Allocator>
1818template <class _ForwardIterator>
1819typename enable_if
1820<
1821 __is_forward_iterator<_ForwardIterator>::value,
1822 void
1823>::type
1824basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
1825{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001826 size_type __sz = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001827 if (__sz > max_size())
1828 this->__throw_length_error();
1829 pointer __p;
1830 if (__sz < __min_cap)
1831 {
1832 __set_short_size(__sz);
1833 __p = __get_short_pointer();
1834 }
1835 else
1836 {
1837 size_type __cap = __recommend(__sz);
Howard Hinnante32b5e22010-11-17 17:55:08 +00001838 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001839 __set_long_pointer(__p);
1840 __set_long_cap(__cap+1);
1841 __set_long_size(__sz);
1842 }
Eric Fiselierb9919752014-10-27 19:28:20 +00001843 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001844 traits_type::assign(*__p, *__first);
1845 traits_type::assign(*__p, value_type());
1846}
1847
1848template <class _CharT, class _Traits, class _Allocator>
1849template<class _InputIterator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001850inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001851basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
1852{
1853 __init(__first, __last);
Howard Hinnant499cea12013-08-23 17:37:05 +00001854#if _LIBCPP_DEBUG_LEVEL >= 2
1855 __get_db()->__insert_c(this);
1856#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001857}
1858
1859template <class _CharT, class _Traits, class _Allocator>
1860template<class _InputIterator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001861inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001862basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
1863 const allocator_type& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001864 : __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001865{
1866 __init(__first, __last);
Howard Hinnant499cea12013-08-23 17:37:05 +00001867#if _LIBCPP_DEBUG_LEVEL >= 2
1868 __get_db()->__insert_c(this);
1869#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001870}
1871
Eric Fiselier3e928972017-04-19 00:28:44 +00001872#ifndef _LIBCPP_CXX03_LANG
Howard Hinnante3e32912011-08-12 21:56:02 +00001873
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001874template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001875inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001876basic_string<_CharT, _Traits, _Allocator>::basic_string(
1877 initializer_list<_CharT> __il)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001878{
1879 __init(__il.begin(), __il.end());
Howard Hinnant499cea12013-08-23 17:37:05 +00001880#if _LIBCPP_DEBUG_LEVEL >= 2
1881 __get_db()->__insert_c(this);
1882#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001883}
1884
1885template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00001886inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001887
Eric Fiselier0eaf2e82017-02-17 01:17:10 +00001888basic_string<_CharT, _Traits, _Allocator>::basic_string(
1889 initializer_list<_CharT> __il, const _Allocator& __a)
Eric Fiselierdb14bcc2017-04-12 23:45:53 +00001890 : __r_(__second_tag(), __a)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001891{
1892 __init(__il.begin(), __il.end());
Howard Hinnant499cea12013-08-23 17:37:05 +00001893#if _LIBCPP_DEBUG_LEVEL >= 2
1894 __get_db()->__insert_c(this);
1895#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001896}
1897
Eric Fiselier3e928972017-04-19 00:28:44 +00001898#endif // _LIBCPP_CXX03_LANG
Howard Hinnante3e32912011-08-12 21:56:02 +00001899
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001900template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001901basic_string<_CharT, _Traits, _Allocator>::~basic_string()
1902{
Howard Hinnant499cea12013-08-23 17:37:05 +00001903#if _LIBCPP_DEBUG_LEVEL >= 2
1904 __get_db()->__erase_c(this);
1905#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001906 if (__is_long())
Howard Hinnante32b5e22010-11-17 17:55:08 +00001907 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001908}
1909
1910template <class _CharT, class _Traits, class _Allocator>
1911void
1912basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
1913 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001914 size_type __n_copy, size_type __n_del, size_type __n_add, const value_type* __p_new_stuff)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001915{
1916 size_type __ms = max_size();
1917 if (__delta_cap > __ms - __old_cap - 1)
1918 this->__throw_length_error();
1919 pointer __old_p = __get_pointer();
1920 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnant0949eed2011-06-30 21:18:19 +00001921 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001922 __ms - 1;
Howard Hinnante32b5e22010-11-17 17:55:08 +00001923 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001924 __invalidate_all_iterators();
1925 if (__n_copy != 0)
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001926 traits_type::copy(_VSTD::__to_raw_pointer(__p),
1927 _VSTD::__to_raw_pointer(__old_p), __n_copy);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001928 if (__n_add != 0)
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001929 traits_type::copy(_VSTD::__to_raw_pointer(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001930 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
1931 if (__sec_cp_sz != 0)
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001932 traits_type::copy(_VSTD::__to_raw_pointer(__p) + __n_copy + __n_add,
1933 _VSTD::__to_raw_pointer(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001934 if (__old_cap+1 != __min_cap)
Howard Hinnante32b5e22010-11-17 17:55:08 +00001935 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001936 __set_long_pointer(__p);
1937 __set_long_cap(__cap+1);
1938 __old_sz = __n_copy + __n_add + __sec_cp_sz;
1939 __set_long_size(__old_sz);
1940 traits_type::assign(__p[__old_sz], value_type());
1941}
1942
1943template <class _CharT, class _Traits, class _Allocator>
1944void
1945basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1946 size_type __n_copy, size_type __n_del, size_type __n_add)
1947{
1948 size_type __ms = max_size();
Marshall Clowecc8d7b2013-11-06 14:24:38 +00001949 if (__delta_cap > __ms - __old_cap)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001950 this->__throw_length_error();
1951 pointer __old_p = __get_pointer();
1952 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnant0949eed2011-06-30 21:18:19 +00001953 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001954 __ms - 1;
Howard Hinnante32b5e22010-11-17 17:55:08 +00001955 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001956 __invalidate_all_iterators();
1957 if (__n_copy != 0)
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001958 traits_type::copy(_VSTD::__to_raw_pointer(__p),
1959 _VSTD::__to_raw_pointer(__old_p), __n_copy);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001960 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
1961 if (__sec_cp_sz != 0)
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001962 traits_type::copy(_VSTD::__to_raw_pointer(__p) + __n_copy + __n_add,
1963 _VSTD::__to_raw_pointer(__old_p) + __n_copy + __n_del,
1964 __sec_cp_sz);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001965 if (__old_cap+1 != __min_cap)
Howard Hinnante32b5e22010-11-17 17:55:08 +00001966 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001967 __set_long_pointer(__p);
1968 __set_long_cap(__cap+1);
1969}
1970
1971// assign
1972
1973template <class _CharT, class _Traits, class _Allocator>
1974basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001975basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001976{
Alp Tokerec34c482014-05-15 11:27:39 +00001977 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001978 size_type __cap = capacity();
1979 if (__cap >= __n)
1980 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00001981 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001982 traits_type::move(__p, __s, __n);
1983 traits_type::assign(__p[__n], value_type());
1984 __set_size(__n);
1985 __invalidate_iterators_past(__n);
1986 }
1987 else
1988 {
1989 size_type __sz = size();
1990 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
1991 }
1992 return *this;
1993}
1994
1995template <class _CharT, class _Traits, class _Allocator>
1996basic_string<_CharT, _Traits, _Allocator>&
1997basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
1998{
1999 size_type __cap = capacity();
2000 if (__cap < __n)
2001 {
2002 size_type __sz = size();
2003 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2004 }
2005 else
2006 __invalidate_iterators_past(__n);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002007 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002008 traits_type::assign(__p, __n, __c);
2009 traits_type::assign(__p[__n], value_type());
2010 __set_size(__n);
2011 return *this;
2012}
2013
2014template <class _CharT, class _Traits, class _Allocator>
2015basic_string<_CharT, _Traits, _Allocator>&
2016basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2017{
2018 pointer __p;
2019 if (__is_long())
2020 {
2021 __p = __get_long_pointer();
2022 __set_long_size(1);
2023 }
2024 else
2025 {
2026 __p = __get_short_pointer();
2027 __set_short_size(1);
2028 }
2029 traits_type::assign(*__p, __c);
2030 traits_type::assign(*++__p, value_type());
2031 __invalidate_iterators_past(1);
2032 return *this;
2033}
2034
2035template <class _CharT, class _Traits, class _Allocator>
Howard Hinnante32b5e22010-11-17 17:55:08 +00002036basic_string<_CharT, _Traits, _Allocator>&
2037basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2038{
2039 if (this != &__str)
2040 {
2041 __copy_assign_alloc(__str);
Marshall Clowf40ec902016-03-09 18:08:29 +00002042 assign(__str.data(), __str.size());
Howard Hinnante32b5e22010-11-17 17:55:08 +00002043 }
2044 return *this;
2045}
2046
Eric Fiselier3e928972017-04-19 00:28:44 +00002047#ifndef _LIBCPP_CXX03_LANG
Howard Hinnante32b5e22010-11-17 17:55:08 +00002048
2049template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002050inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnante32b5e22010-11-17 17:55:08 +00002051void
2052basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clowaf961ed2015-08-18 18:57:00 +00002053 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnante32b5e22010-11-17 17:55:08 +00002054{
2055 if (__alloc() != __str.__alloc())
2056 assign(__str);
2057 else
2058 __move_assign(__str, true_type());
2059}
2060
2061template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002062inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnante32b5e22010-11-17 17:55:08 +00002063void
2064basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clowaf961ed2015-08-18 18:57:00 +00002065#if _LIBCPP_STD_VER > 14
2066 _NOEXCEPT
2067#else
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00002068 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clowaf961ed2015-08-18 18:57:00 +00002069#endif
Howard Hinnante32b5e22010-11-17 17:55:08 +00002070{
2071 clear();
2072 shrink_to_fit();
Howard Hinnant3fdbbd22011-08-17 20:36:18 +00002073 __r_.first() = __str.__r_.first();
2074 __move_assign_alloc(__str);
Howard Hinnante32b5e22010-11-17 17:55:08 +00002075 __str.__zero();
2076}
2077
2078template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002079inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnante32b5e22010-11-17 17:55:08 +00002080basic_string<_CharT, _Traits, _Allocator>&
2081basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clowaf961ed2015-08-18 18:57:00 +00002082 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnante32b5e22010-11-17 17:55:08 +00002083{
2084 __move_assign(__str, integral_constant<bool,
2085 __alloc_traits::propagate_on_container_move_assignment::value>());
2086 return *this;
2087}
2088
2089#endif
2090
2091template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002092template<class _InputIterator>
2093typename enable_if
2094<
Marshall Clowdf9db312016-01-13 21:54:34 +00002095 __is_exactly_input_iterator <_InputIterator>::value
2096 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002097 basic_string<_CharT, _Traits, _Allocator>&
2098>::type
2099basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2100{
Marshall Clowd979eed2016-09-05 01:54:30 +00002101 const basic_string __temp(__first, __last, __alloc());
Marshall Clowdf9db312016-01-13 21:54:34 +00002102 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidis1dc6f7a2012-10-13 02:03:45 +00002103 return *this;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002104}
2105
2106template <class _CharT, class _Traits, class _Allocator>
2107template<class _ForwardIterator>
2108typename enable_if
2109<
Marshall Clowdf9db312016-01-13 21:54:34 +00002110 __is_forward_iterator<_ForwardIterator>::value
2111 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002112 basic_string<_CharT, _Traits, _Allocator>&
2113>::type
2114basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2115{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002116 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002117 size_type __cap = capacity();
2118 if (__cap < __n)
2119 {
2120 size_type __sz = size();
2121 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2122 }
2123 else
2124 __invalidate_iterators_past(__n);
2125 pointer __p = __get_pointer();
2126 for (; __first != __last; ++__first, ++__p)
2127 traits_type::assign(*__p, *__first);
2128 traits_type::assign(*__p, value_type());
2129 __set_size(__n);
2130 return *this;
2131}
2132
2133template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002134basic_string<_CharT, _Traits, _Allocator>&
2135basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2136{
2137 size_type __sz = __str.size();
2138 if (__pos > __sz)
2139 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002140 return assign(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002141}
2142
2143template <class _CharT, class _Traits, class _Allocator>
Marshall Clow6ac8de02016-09-24 22:45:42 +00002144template <class _Tp>
2145typename enable_if
2146<
2147 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
2148 basic_string<_CharT, _Traits, _Allocator>&
2149>::type
2150basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002151{
Marshall Clow6ac8de02016-09-24 22:45:42 +00002152 __self_view __sv = __t;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002153 size_type __sz = __sv.size();
2154 if (__pos > __sz)
2155 this->__throw_out_of_range();
2156 return assign(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2157}
2158
2159
2160template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002161basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002162basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002163{
Alp Tokerec34c482014-05-15 11:27:39 +00002164 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002165 return assign(__s, traits_type::length(__s));
2166}
2167
2168// append
2169
2170template <class _CharT, class _Traits, class _Allocator>
2171basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002172basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002173{
Alp Tokerec34c482014-05-15 11:27:39 +00002174 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002175 size_type __cap = capacity();
2176 size_type __sz = size();
2177 if (__cap - __sz >= __n)
2178 {
2179 if (__n)
2180 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002181 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002182 traits_type::copy(__p + __sz, __s, __n);
2183 __sz += __n;
2184 __set_size(__sz);
2185 traits_type::assign(__p[__sz], value_type());
2186 }
2187 }
2188 else
2189 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2190 return *this;
2191}
2192
2193template <class _CharT, class _Traits, class _Allocator>
2194basic_string<_CharT, _Traits, _Allocator>&
2195basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2196{
2197 if (__n)
2198 {
2199 size_type __cap = capacity();
2200 size_type __sz = size();
2201 if (__cap - __sz < __n)
2202 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2203 pointer __p = __get_pointer();
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002204 traits_type::assign(_VSTD::__to_raw_pointer(__p) + __sz, __n, __c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002205 __sz += __n;
2206 __set_size(__sz);
2207 traits_type::assign(__p[__sz], value_type());
2208 }
2209 return *this;
2210}
2211
2212template <class _CharT, class _Traits, class _Allocator>
2213void
2214basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2215{
Howard Hinnant15467182013-04-30 21:44:48 +00002216 bool __is_short = !__is_long();
2217 size_type __cap;
2218 size_type __sz;
2219 if (__is_short)
2220 {
2221 __cap = __min_cap - 1;
2222 __sz = __get_short_size();
2223 }
2224 else
2225 {
2226 __cap = __get_long_cap() - 1;
2227 __sz = __get_long_size();
2228 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002229 if (__sz == __cap)
Howard Hinnant15467182013-04-30 21:44:48 +00002230 {
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002231 __grow_by(__cap, 1, __sz, __sz, 0);
Howard Hinnant15467182013-04-30 21:44:48 +00002232 __is_short = !__is_long();
2233 }
2234 pointer __p;
2235 if (__is_short)
2236 {
2237 __p = __get_short_pointer() + __sz;
2238 __set_short_size(__sz+1);
2239 }
2240 else
2241 {
2242 __p = __get_long_pointer() + __sz;
2243 __set_long_size(__sz+1);
2244 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002245 traits_type::assign(*__p, __c);
2246 traits_type::assign(*++__p, value_type());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002247}
2248
Marshall Clowac655ef2016-09-07 03:32:06 +00002249template <class _Tp>
Marshall Clowd979eed2016-09-05 01:54:30 +00002250bool __ptr_in_range (const _Tp* __p, const _Tp* __first, const _Tp* __last)
2251{
2252 return __first <= __p && __p < __last;
2253}
2254
Marshall Clowac655ef2016-09-07 03:32:06 +00002255template <class _Tp1, class _Tp2>
Eric Fiselier0e5ebbc2016-12-23 23:37:52 +00002256bool __ptr_in_range (const _Tp1*, const _Tp2*, const _Tp2*)
Marshall Clowac655ef2016-09-07 03:32:06 +00002257{
2258 return false;
2259}
2260
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002261template <class _CharT, class _Traits, class _Allocator>
2262template<class _ForwardIterator>
Eric Fiselier026d38e2016-10-31 02:46:25 +00002263basic_string<_CharT, _Traits, _Allocator>&
2264basic_string<_CharT, _Traits, _Allocator>::__append_forward_unsafe(
2265 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002266{
Eric Fiselier026d38e2016-10-31 02:46:25 +00002267 static_assert(__is_forward_iterator<_ForwardIterator>::value,
2268 "function requires a ForwardIterator");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002269 size_type __sz = size();
2270 size_type __cap = capacity();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002271 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002272 if (__n)
2273 {
Eric Fiselier622c7d52017-04-15 06:49:02 +00002274 typedef typename iterator_traits<_ForwardIterator>::reference _CharRef;
2275 _CharRef __tmp_ref = *__first;
2276 if (__ptr_in_range(_VSTD::addressof(__tmp_ref), data(), data() + size()))
Marshall Clowd979eed2016-09-05 01:54:30 +00002277 {
2278 const basic_string __temp (__first, __last, __alloc());
2279 append(__temp.data(), __temp.size());
2280 }
2281 else
2282 {
2283 if (__cap - __sz < __n)
2284 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2285 pointer __p = __get_pointer() + __sz;
2286 for (; __first != __last; ++__p, ++__first)
2287 traits_type::assign(*__p, *__first);
2288 traits_type::assign(*__p, value_type());
2289 __set_size(__sz + __n);
2290 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002291 }
2292 return *this;
2293}
2294
2295template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002296inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002297basic_string<_CharT, _Traits, _Allocator>&
2298basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2299{
2300 return append(__str.data(), __str.size());
2301}
2302
2303template <class _CharT, class _Traits, class _Allocator>
2304basic_string<_CharT, _Traits, _Allocator>&
2305basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2306{
2307 size_type __sz = __str.size();
2308 if (__pos > __sz)
2309 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002310 return append(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002311}
2312
2313template <class _CharT, class _Traits, class _Allocator>
Marshall Clow42a87db2016-10-03 23:40:48 +00002314template <class _Tp>
Marshall Clow6ac8de02016-09-24 22:45:42 +00002315 typename enable_if
2316 <
2317 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
2318 basic_string<_CharT, _Traits, _Allocator>&
2319 >::type
2320basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002321{
Marshall Clow6ac8de02016-09-24 22:45:42 +00002322 __self_view __sv = __t;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002323 size_type __sz = __sv.size();
2324 if (__pos > __sz)
2325 this->__throw_out_of_range();
2326 return append(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2327}
2328
2329template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002330basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002331basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002332{
Alp Tokerec34c482014-05-15 11:27:39 +00002333 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002334 return append(__s, traits_type::length(__s));
2335}
2336
2337// insert
2338
2339template <class _CharT, class _Traits, class _Allocator>
2340basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002341basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002342{
Alp Tokerec34c482014-05-15 11:27:39 +00002343 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002344 size_type __sz = size();
2345 if (__pos > __sz)
2346 this->__throw_out_of_range();
2347 size_type __cap = capacity();
2348 if (__cap - __sz >= __n)
2349 {
2350 if (__n)
2351 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002352 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002353 size_type __n_move = __sz - __pos;
2354 if (__n_move != 0)
2355 {
2356 if (__p + __pos <= __s && __s < __p + __sz)
2357 __s += __n;
2358 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2359 }
2360 traits_type::move(__p + __pos, __s, __n);
2361 __sz += __n;
2362 __set_size(__sz);
2363 traits_type::assign(__p[__sz], value_type());
2364 }
2365 }
2366 else
2367 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2368 return *this;
2369}
2370
2371template <class _CharT, class _Traits, class _Allocator>
2372basic_string<_CharT, _Traits, _Allocator>&
2373basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2374{
2375 size_type __sz = size();
2376 if (__pos > __sz)
2377 this->__throw_out_of_range();
2378 if (__n)
2379 {
2380 size_type __cap = capacity();
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002381 value_type* __p;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002382 if (__cap - __sz >= __n)
2383 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002384 __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002385 size_type __n_move = __sz - __pos;
2386 if (__n_move != 0)
2387 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2388 }
2389 else
2390 {
2391 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002392 __p = _VSTD::__to_raw_pointer(__get_long_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002393 }
2394 traits_type::assign(__p + __pos, __n, __c);
2395 __sz += __n;
2396 __set_size(__sz);
2397 traits_type::assign(__p[__sz], value_type());
2398 }
2399 return *this;
2400}
2401
2402template <class _CharT, class _Traits, class _Allocator>
2403template<class _InputIterator>
2404typename enable_if
2405<
Marshall Clowdf9db312016-01-13 21:54:34 +00002406 __is_exactly_input_iterator<_InputIterator>::value
2407 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
2408 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002409>::type
2410basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2411{
Howard Hinnant499cea12013-08-23 17:37:05 +00002412#if _LIBCPP_DEBUG_LEVEL >= 2
2413 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2414 "string::insert(iterator, range) called with an iterator not"
2415 " referring to this string");
2416#endif
Marshall Clowd979eed2016-09-05 01:54:30 +00002417 const basic_string __temp(__first, __last, __alloc());
Marshall Clowdf9db312016-01-13 21:54:34 +00002418 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002419}
2420
2421template <class _CharT, class _Traits, class _Allocator>
2422template<class _ForwardIterator>
2423typename enable_if
2424<
Marshall Clowdf9db312016-01-13 21:54:34 +00002425 __is_forward_iterator<_ForwardIterator>::value
2426 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002427 typename basic_string<_CharT, _Traits, _Allocator>::iterator
2428>::type
2429basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2430{
Howard Hinnant499cea12013-08-23 17:37:05 +00002431#if _LIBCPP_DEBUG_LEVEL >= 2
2432 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2433 "string::insert(iterator, range) called with an iterator not"
2434 " referring to this string");
2435#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002436 size_type __ip = static_cast<size_type>(__pos - begin());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002437 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002438 if (__n)
2439 {
Eric Fiselier622c7d52017-04-15 06:49:02 +00002440 typedef typename iterator_traits<_ForwardIterator>::reference _CharRef;
2441 _CharRef __tmp_char = *__first;
2442 if (__ptr_in_range(_VSTD::addressof(__tmp_char), data(), data() + size()))
Marshall Clowd979eed2016-09-05 01:54:30 +00002443 {
2444 const basic_string __temp(__first, __last, __alloc());
2445 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
2446 }
2447
2448 size_type __sz = size();
2449 size_type __cap = capacity();
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002450 value_type* __p;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002451 if (__cap - __sz >= __n)
2452 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002453 __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002454 size_type __n_move = __sz - __ip;
2455 if (__n_move != 0)
2456 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
2457 }
2458 else
2459 {
2460 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002461 __p = _VSTD::__to_raw_pointer(__get_long_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002462 }
2463 __sz += __n;
2464 __set_size(__sz);
2465 traits_type::assign(__p[__sz], value_type());
2466 for (__p += __ip; __first != __last; ++__p, ++__first)
2467 traits_type::assign(*__p, *__first);
2468 }
2469 return begin() + __ip;
2470}
2471
2472template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002473inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002474basic_string<_CharT, _Traits, _Allocator>&
2475basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2476{
2477 return insert(__pos1, __str.data(), __str.size());
2478}
2479
2480template <class _CharT, class _Traits, class _Allocator>
2481basic_string<_CharT, _Traits, _Allocator>&
2482basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2483 size_type __pos2, size_type __n)
2484{
2485 size_type __str_sz = __str.size();
2486 if (__pos2 > __str_sz)
2487 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002488 return insert(__pos1, __str.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002489}
2490
2491template <class _CharT, class _Traits, class _Allocator>
Marshall Clow6ac8de02016-09-24 22:45:42 +00002492template <class _Tp>
2493typename enable_if
2494<
2495 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
2496 basic_string<_CharT, _Traits, _Allocator>&
2497>::type
2498basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002499 size_type __pos2, size_type __n)
2500{
Marshall Clow6ac8de02016-09-24 22:45:42 +00002501 __self_view __sv = __t;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002502 size_type __str_sz = __sv.size();
2503 if (__pos2 > __str_sz)
2504 this->__throw_out_of_range();
2505 return insert(__pos1, __sv.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
2506}
2507
2508template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002509basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002510basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002511{
Alp Tokerec34c482014-05-15 11:27:39 +00002512 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002513 return insert(__pos, __s, traits_type::length(__s));
2514}
2515
2516template <class _CharT, class _Traits, class _Allocator>
2517typename basic_string<_CharT, _Traits, _Allocator>::iterator
2518basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2519{
2520 size_type __ip = static_cast<size_type>(__pos - begin());
2521 size_type __sz = size();
2522 size_type __cap = capacity();
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002523 value_type* __p;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002524 if (__cap == __sz)
2525 {
2526 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002527 __p = _VSTD::__to_raw_pointer(__get_long_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002528 }
2529 else
2530 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002531 __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002532 size_type __n_move = __sz - __ip;
2533 if (__n_move != 0)
2534 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2535 }
2536 traits_type::assign(__p[__ip], __c);
2537 traits_type::assign(__p[++__sz], value_type());
2538 __set_size(__sz);
2539 return begin() + static_cast<difference_type>(__ip);
2540}
2541
2542template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002543inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002544typename basic_string<_CharT, _Traits, _Allocator>::iterator
2545basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2546{
Howard Hinnant499cea12013-08-23 17:37:05 +00002547#if _LIBCPP_DEBUG_LEVEL >= 2
2548 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2549 "string::insert(iterator, n, value) called with an iterator not"
2550 " referring to this string");
2551#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002552 difference_type __p = __pos - begin();
2553 insert(static_cast<size_type>(__p), __n, __c);
2554 return begin() + __p;
2555}
2556
2557// replace
2558
2559template <class _CharT, class _Traits, class _Allocator>
2560basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002561basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2)
Eric Fiselier3b7c1342017-03-09 01:54:13 +00002562 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002563{
Alp Tokerec34c482014-05-15 11:27:39 +00002564 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002565 size_type __sz = size();
2566 if (__pos > __sz)
2567 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002568 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002569 size_type __cap = capacity();
2570 if (__cap - __sz + __n1 >= __n2)
2571 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002572 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002573 if (__n1 != __n2)
2574 {
2575 size_type __n_move = __sz - __pos - __n1;
2576 if (__n_move != 0)
2577 {
2578 if (__n1 > __n2)
2579 {
2580 traits_type::move(__p + __pos, __s, __n2);
2581 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2582 goto __finish;
2583 }
2584 if (__p + __pos < __s && __s < __p + __sz)
2585 {
2586 if (__p + __pos + __n1 <= __s)
2587 __s += __n2 - __n1;
2588 else // __p + __pos < __s < __p + __pos + __n1
2589 {
2590 traits_type::move(__p + __pos, __s, __n1);
2591 __pos += __n1;
2592 __s += __n2;
2593 __n2 -= __n1;
2594 __n1 = 0;
2595 }
2596 }
2597 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2598 }
2599 }
2600 traits_type::move(__p + __pos, __s, __n2);
2601__finish:
Eric Fiselier3b7c1342017-03-09 01:54:13 +00002602// __sz += __n2 - __n1; in this and the below function below can cause unsigned integer overflow,
2603// but this is a safe operation, so we disable the check.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002604 __sz += __n2 - __n1;
2605 __set_size(__sz);
2606 __invalidate_iterators_past(__sz);
2607 traits_type::assign(__p[__sz], value_type());
2608 }
2609 else
2610 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
2611 return *this;
2612}
2613
2614template <class _CharT, class _Traits, class _Allocator>
2615basic_string<_CharT, _Traits, _Allocator>&
2616basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
Eric Fiselier3b7c1342017-03-09 01:54:13 +00002617 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002618{
2619 size_type __sz = size();
2620 if (__pos > __sz)
2621 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002622 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002623 size_type __cap = capacity();
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002624 value_type* __p;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002625 if (__cap - __sz + __n1 >= __n2)
2626 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002627 __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002628 if (__n1 != __n2)
2629 {
2630 size_type __n_move = __sz - __pos - __n1;
2631 if (__n_move != 0)
2632 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2633 }
2634 }
2635 else
2636 {
2637 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002638 __p = _VSTD::__to_raw_pointer(__get_long_pointer());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002639 }
2640 traits_type::assign(__p + __pos, __n2, __c);
2641 __sz += __n2 - __n1;
2642 __set_size(__sz);
2643 __invalidate_iterators_past(__sz);
2644 traits_type::assign(__p[__sz], value_type());
2645 return *this;
2646}
2647
2648template <class _CharT, class _Traits, class _Allocator>
2649template<class _InputIterator>
2650typename enable_if
2651<
2652 __is_input_iterator<_InputIterator>::value,
2653 basic_string<_CharT, _Traits, _Allocator>&
2654>::type
Howard Hinnant7b2cb482010-11-17 21:11:40 +00002655basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002656 _InputIterator __j1, _InputIterator __j2)
2657{
Marshall Clowd979eed2016-09-05 01:54:30 +00002658 const basic_string __temp(__j1, __j2, __alloc());
Marshall Clowdf9db312016-01-13 21:54:34 +00002659 return this->replace(__i1, __i2, __temp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002660}
2661
2662template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002663inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002664basic_string<_CharT, _Traits, _Allocator>&
2665basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
2666{
2667 return replace(__pos1, __n1, __str.data(), __str.size());
2668}
2669
2670template <class _CharT, class _Traits, class _Allocator>
2671basic_string<_CharT, _Traits, _Allocator>&
2672basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
2673 size_type __pos2, size_type __n2)
2674{
2675 size_type __str_sz = __str.size();
2676 if (__pos2 > __str_sz)
2677 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002678 return replace(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002679}
2680
2681template <class _CharT, class _Traits, class _Allocator>
Marshall Clow6ac8de02016-09-24 22:45:42 +00002682template <class _Tp>
2683typename enable_if
2684<
2685 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
2686 basic_string<_CharT, _Traits, _Allocator>&
2687>::type
2688basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002689 size_type __pos2, size_type __n2)
2690{
Marshall Clow6ac8de02016-09-24 22:45:42 +00002691 __self_view __sv = __t;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002692 size_type __str_sz = __sv.size();
2693 if (__pos2 > __str_sz)
2694 this->__throw_out_of_range();
2695 return replace(__pos1, __n1, __sv.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
2696}
2697
2698template <class _CharT, class _Traits, class _Allocator>
2699basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002700basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002701{
Alp Tokerec34c482014-05-15 11:27:39 +00002702 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002703 return replace(__pos, __n1, __s, traits_type::length(__s));
2704}
2705
2706template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002707inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002708basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant7b2cb482010-11-17 21:11:40 +00002709basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002710{
2711 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
2712 __str.data(), __str.size());
2713}
2714
2715template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002716inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002717basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002718basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002719{
2720 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
2721}
2722
2723template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002724inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002725basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002726basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002727{
2728 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
2729}
2730
2731template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002732inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002733basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant7b2cb482010-11-17 21:11:40 +00002734basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002735{
2736 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
2737}
2738
2739// erase
2740
2741template <class _CharT, class _Traits, class _Allocator>
2742basic_string<_CharT, _Traits, _Allocator>&
2743basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos, size_type __n)
2744{
2745 size_type __sz = size();
2746 if (__pos > __sz)
2747 this->__throw_out_of_range();
2748 if (__n)
2749 {
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002750 value_type* __p = _VSTD::__to_raw_pointer(__get_pointer());
Howard Hinnant0949eed2011-06-30 21:18:19 +00002751 __n = _VSTD::min(__n, __sz - __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002752 size_type __n_move = __sz - __pos - __n;
2753 if (__n_move != 0)
2754 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
2755 __sz -= __n;
2756 __set_size(__sz);
2757 __invalidate_iterators_past(__sz);
2758 traits_type::assign(__p[__sz], value_type());
2759 }
2760 return *this;
2761}
2762
2763template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002764inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002765typename basic_string<_CharT, _Traits, _Allocator>::iterator
2766basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
2767{
Howard Hinnant499cea12013-08-23 17:37:05 +00002768#if _LIBCPP_DEBUG_LEVEL >= 2
2769 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2770 "string::erase(iterator) called with an iterator not"
2771 " referring to this string");
2772#endif
2773 _LIBCPP_ASSERT(__pos != end(),
2774 "string::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002775 iterator __b = begin();
2776 size_type __r = static_cast<size_type>(__pos - __b);
2777 erase(__r, 1);
Howard Hinnantec3773c2011-12-01 20:21:04 +00002778 return __b + static_cast<difference_type>(__r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002779}
2780
2781template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002782inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002783typename basic_string<_CharT, _Traits, _Allocator>::iterator
2784basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
2785{
Howard Hinnant499cea12013-08-23 17:37:05 +00002786#if _LIBCPP_DEBUG_LEVEL >= 2
2787 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
2788 "string::erase(iterator, iterator) called with an iterator not"
2789 " referring to this string");
2790#endif
2791 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002792 iterator __b = begin();
2793 size_type __r = static_cast<size_type>(__first - __b);
2794 erase(__r, static_cast<size_type>(__last - __first));
Howard Hinnantec3773c2011-12-01 20:21:04 +00002795 return __b + static_cast<difference_type>(__r);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002796}
2797
2798template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002799inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002800void
2801basic_string<_CharT, _Traits, _Allocator>::pop_back()
2802{
Howard Hinnant499cea12013-08-23 17:37:05 +00002803 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002804 size_type __sz;
2805 if (__is_long())
2806 {
2807 __sz = __get_long_size() - 1;
2808 __set_long_size(__sz);
2809 traits_type::assign(*(__get_long_pointer() + __sz), value_type());
2810 }
2811 else
2812 {
2813 __sz = __get_short_size() - 1;
2814 __set_short_size(__sz);
2815 traits_type::assign(*(__get_short_pointer() + __sz), value_type());
2816 }
2817 __invalidate_iterators_past(__sz);
2818}
2819
2820template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002821inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002822void
Howard Hinnanta6119a82011-05-29 19:57:12 +00002823basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002824{
2825 __invalidate_all_iterators();
2826 if (__is_long())
2827 {
2828 traits_type::assign(*__get_long_pointer(), value_type());
2829 __set_long_size(0);
2830 }
2831 else
2832 {
2833 traits_type::assign(*__get_short_pointer(), value_type());
2834 __set_short_size(0);
2835 }
2836}
2837
2838template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002839inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002840void
2841basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
2842{
2843 if (__is_long())
2844 {
2845 traits_type::assign(*(__get_long_pointer() + __pos), value_type());
2846 __set_long_size(__pos);
2847 }
2848 else
2849 {
2850 traits_type::assign(*(__get_short_pointer() + __pos), value_type());
2851 __set_short_size(__pos);
2852 }
2853 __invalidate_iterators_past(__pos);
2854}
2855
2856template <class _CharT, class _Traits, class _Allocator>
2857void
2858basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
2859{
2860 size_type __sz = size();
2861 if (__n > __sz)
2862 append(__n - __sz, __c);
2863 else
2864 __erase_to_end(__n);
2865}
2866
2867template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002868inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002869typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00002870basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002871{
Howard Hinnante32b5e22010-11-17 17:55:08 +00002872 size_type __m = __alloc_traits::max_size(__alloc());
Eric Fiselier5ccf0432017-10-17 13:16:01 +00002873#ifdef _LIBCPP_BIG_ENDIAN
Marshall Clow09f85502013-10-31 17:23:08 +00002874 return (__m <= ~__long_mask ? __m : __m/2) - __alignment;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002875#else
Marshall Clow09f85502013-10-31 17:23:08 +00002876 return __m - __alignment;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002877#endif
2878}
2879
2880template <class _CharT, class _Traits, class _Allocator>
2881void
2882basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __res_arg)
2883{
2884 if (__res_arg > max_size())
2885 this->__throw_length_error();
2886 size_type __cap = capacity();
2887 size_type __sz = size();
Howard Hinnant0949eed2011-06-30 21:18:19 +00002888 __res_arg = _VSTD::max(__res_arg, __sz);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002889 __res_arg = __recommend(__res_arg);
2890 if (__res_arg != __cap)
2891 {
2892 pointer __new_data, __p;
2893 bool __was_long, __now_long;
2894 if (__res_arg == __min_cap - 1)
2895 {
2896 __was_long = true;
2897 __now_long = false;
2898 __new_data = __get_short_pointer();
2899 __p = __get_long_pointer();
2900 }
2901 else
2902 {
2903 if (__res_arg > __cap)
Howard Hinnante32b5e22010-11-17 17:55:08 +00002904 __new_data = __alloc_traits::allocate(__alloc(), __res_arg+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002905 else
2906 {
2907 #ifndef _LIBCPP_NO_EXCEPTIONS
2908 try
2909 {
Howard Hinnant324bb032010-08-22 00:02:43 +00002910 #endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnante32b5e22010-11-17 17:55:08 +00002911 __new_data = __alloc_traits::allocate(__alloc(), __res_arg+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002912 #ifndef _LIBCPP_NO_EXCEPTIONS
2913 }
2914 catch (...)
2915 {
2916 return;
2917 }
Howard Hinnant324bb032010-08-22 00:02:43 +00002918 #else // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002919 if (__new_data == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002920 return;
Howard Hinnant324bb032010-08-22 00:02:43 +00002921 #endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002922 }
2923 __now_long = true;
2924 __was_long = __is_long();
2925 __p = __get_pointer();
2926 }
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00002927 traits_type::copy(_VSTD::__to_raw_pointer(__new_data),
2928 _VSTD::__to_raw_pointer(__p), size()+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002929 if (__was_long)
Howard Hinnante32b5e22010-11-17 17:55:08 +00002930 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002931 if (__now_long)
2932 {
2933 __set_long_cap(__res_arg+1);
2934 __set_long_size(__sz);
2935 __set_long_pointer(__new_data);
2936 }
2937 else
2938 __set_short_size(__sz);
2939 __invalidate_all_iterators();
2940 }
2941}
2942
2943template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002944inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002945typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002946basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002947{
Howard Hinnant499cea12013-08-23 17:37:05 +00002948 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002949 return *(data() + __pos);
2950}
2951
2952template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002953inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002954typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow1e00d6d2016-07-21 05:31:24 +00002955basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002956{
Howard Hinnant499cea12013-08-23 17:37:05 +00002957 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002958 return *(__get_pointer() + __pos);
2959}
2960
2961template <class _CharT, class _Traits, class _Allocator>
2962typename basic_string<_CharT, _Traits, _Allocator>::const_reference
2963basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
2964{
2965 if (__n >= size())
2966 this->__throw_out_of_range();
2967 return (*this)[__n];
2968}
2969
2970template <class _CharT, class _Traits, class _Allocator>
2971typename basic_string<_CharT, _Traits, _Allocator>::reference
2972basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
2973{
2974 if (__n >= size())
2975 this->__throw_out_of_range();
2976 return (*this)[__n];
2977}
2978
2979template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002980inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002981typename basic_string<_CharT, _Traits, _Allocator>::reference
2982basic_string<_CharT, _Traits, _Allocator>::front()
2983{
Howard Hinnant499cea12013-08-23 17:37:05 +00002984 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002985 return *__get_pointer();
2986}
2987
2988template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002989inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002990typename basic_string<_CharT, _Traits, _Allocator>::const_reference
2991basic_string<_CharT, _Traits, _Allocator>::front() const
2992{
Howard Hinnant499cea12013-08-23 17:37:05 +00002993 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002994 return *data();
2995}
2996
2997template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00002998inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002999typename basic_string<_CharT, _Traits, _Allocator>::reference
3000basic_string<_CharT, _Traits, _Allocator>::back()
3001{
Howard Hinnant499cea12013-08-23 17:37:05 +00003002 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003003 return *(__get_pointer() + size() - 1);
3004}
3005
3006template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003007inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003008typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3009basic_string<_CharT, _Traits, _Allocator>::back() const
3010{
Howard Hinnant499cea12013-08-23 17:37:05 +00003011 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003012 return *(data() + size() - 1);
3013}
3014
3015template <class _CharT, class _Traits, class _Allocator>
3016typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003017basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003018{
3019 size_type __sz = size();
3020 if (__pos > __sz)
3021 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00003022 size_type __rlen = _VSTD::min(__n, __sz - __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003023 traits_type::copy(__s, data() + __pos, __rlen);
3024 return __rlen;
3025}
3026
3027template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003028inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003029basic_string<_CharT, _Traits, _Allocator>
3030basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3031{
3032 return basic_string(*this, __pos, __n, __alloc());
3033}
3034
3035template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003036inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003037void
3038basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow7d914d12015-07-13 20:04:56 +00003039#if _LIBCPP_STD_VER >= 14
Eric Fiselier47257c42016-12-28 05:53:01 +00003040 _NOEXCEPT_DEBUG
Marshall Clow7d914d12015-07-13 20:04:56 +00003041#else
Eric Fiselier47257c42016-12-28 05:53:01 +00003042 _NOEXCEPT_DEBUG_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow7d914d12015-07-13 20:04:56 +00003043 __is_nothrow_swappable<allocator_type>::value)
3044#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003045{
Howard Hinnant499cea12013-08-23 17:37:05 +00003046#if _LIBCPP_DEBUG_LEVEL >= 2
3047 if (!__is_long())
3048 __get_db()->__invalidate_all(this);
3049 if (!__str.__is_long())
3050 __get_db()->__invalidate_all(&__str);
3051 __get_db()->swap(this, &__str);
3052#endif
Eric Fiselier47257c42016-12-28 05:53:01 +00003053 _LIBCPP_ASSERT(
3054 __alloc_traits::propagate_on_container_swap::value ||
3055 __alloc_traits::is_always_equal::value ||
3056 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Howard Hinnant0949eed2011-06-30 21:18:19 +00003057 _VSTD::swap(__r_.first(), __str.__r_.first());
Marshall Clow7d914d12015-07-13 20:04:56 +00003058 __swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003059}
3060
3061// find
3062
3063template <class _Traits>
3064struct _LIBCPP_HIDDEN __traits_eq
3065{
3066 typedef typename _Traits::char_type char_type;
Howard Hinnanta6119a82011-05-29 19:57:12 +00003067 _LIBCPP_INLINE_VISIBILITY
3068 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3069 {return _Traits::eq(__x, __y);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003070};
3071
3072template<class _CharT, class _Traits, class _Allocator>
3073typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003074basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003075 size_type __pos,
3076 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003077{
Alp Tokerec34c482014-05-15 11:27:39 +00003078 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003079 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003080 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003081}
3082
3083template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003084inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003085typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003086basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3087 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003088{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003089 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003090 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003091}
3092
3093template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003094inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003095typename basic_string<_CharT, _Traits, _Allocator>::size_type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003096basic_string<_CharT, _Traits, _Allocator>::find(__self_view __sv,
3097 size_type __pos) const _NOEXCEPT
3098{
3099 return __str_find<value_type, size_type, traits_type, npos>
3100 (data(), size(), __sv.data(), __pos, __sv.size());
3101}
3102
3103template<class _CharT, class _Traits, class _Allocator>
3104inline _LIBCPP_INLINE_VISIBILITY
3105typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003106basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003107 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003108{
Alp Tokerec34c482014-05-15 11:27:39 +00003109 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003110 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003111 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003112}
3113
3114template<class _CharT, class _Traits, class _Allocator>
3115typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003116basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3117 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003118{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003119 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003120 (data(), size(), __c, __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003121}
3122
3123// rfind
3124
3125template<class _CharT, class _Traits, class _Allocator>
3126typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003127basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003128 size_type __pos,
3129 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003130{
Alp Tokerec34c482014-05-15 11:27:39 +00003131 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003132 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003133 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003134}
3135
3136template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003137inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003138typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003139basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3140 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003141{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003142 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003143 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003144}
3145
3146template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003147inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003148typename basic_string<_CharT, _Traits, _Allocator>::size_type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003149basic_string<_CharT, _Traits, _Allocator>::rfind(__self_view __sv,
3150 size_type __pos) const _NOEXCEPT
3151{
3152 return __str_rfind<value_type, size_type, traits_type, npos>
3153 (data(), size(), __sv.data(), __pos, __sv.size());
3154}
3155
3156template<class _CharT, class _Traits, class _Allocator>
3157inline _LIBCPP_INLINE_VISIBILITY
3158typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003159basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003160 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003161{
Alp Tokerec34c482014-05-15 11:27:39 +00003162 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003163 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003164 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003165}
3166
3167template<class _CharT, class _Traits, class _Allocator>
3168typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003169basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3170 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003171{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003172 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clow360f3192014-06-02 02:22:49 +00003173 (data(), size(), __c, __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003174}
3175
3176// find_first_of
3177
3178template<class _CharT, class _Traits, class _Allocator>
3179typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003180basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003181 size_type __pos,
3182 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003183{
Alp Tokerec34c482014-05-15 11:27:39 +00003184 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003185 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003186 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003187}
3188
3189template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003190inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003191typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003192basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3193 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003194{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003195 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003196 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003197}
3198
3199template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003200inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003201typename basic_string<_CharT, _Traits, _Allocator>::size_type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003202basic_string<_CharT, _Traits, _Allocator>::find_first_of(__self_view __sv,
3203 size_type __pos) const _NOEXCEPT
3204{
3205 return __str_find_first_of<value_type, size_type, traits_type, npos>
3206 (data(), size(), __sv.data(), __pos, __sv.size());
3207}
3208
3209template<class _CharT, class _Traits, class _Allocator>
3210inline _LIBCPP_INLINE_VISIBILITY
3211typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003212basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003213 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003214{
Alp Tokerec34c482014-05-15 11:27:39 +00003215 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003216 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003217 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003218}
3219
3220template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003221inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003222typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003223basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3224 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003225{
3226 return find(__c, __pos);
3227}
3228
3229// find_last_of
3230
3231template<class _CharT, class _Traits, class _Allocator>
3232typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003233basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003234 size_type __pos,
3235 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003236{
Alp Tokerec34c482014-05-15 11:27:39 +00003237 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003238 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003239 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003240}
3241
3242template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003243inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003244typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003245basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3246 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003247{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003248 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003249 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003250}
3251
3252template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003253inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003254typename basic_string<_CharT, _Traits, _Allocator>::size_type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003255basic_string<_CharT, _Traits, _Allocator>::find_last_of(__self_view __sv,
3256 size_type __pos) const _NOEXCEPT
3257{
3258 return __str_find_last_of<value_type, size_type, traits_type, npos>
3259 (data(), size(), __sv.data(), __pos, __sv.size());
3260}
3261
3262template<class _CharT, class _Traits, class _Allocator>
3263inline _LIBCPP_INLINE_VISIBILITY
3264typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003265basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003266 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003267{
Alp Tokerec34c482014-05-15 11:27:39 +00003268 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003269 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003270 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003271}
3272
3273template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003274inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003275typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003276basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3277 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003278{
3279 return rfind(__c, __pos);
3280}
3281
3282// find_first_not_of
3283
3284template<class _CharT, class _Traits, class _Allocator>
3285typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003286basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003287 size_type __pos,
3288 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003289{
Alp Tokerec34c482014-05-15 11:27:39 +00003290 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003291 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003292 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003293}
3294
3295template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003296inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003297typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003298basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3299 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003300{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003301 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003302 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003303}
3304
3305template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003306inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003307typename basic_string<_CharT, _Traits, _Allocator>::size_type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003308basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(__self_view __sv,
3309 size_type __pos) const _NOEXCEPT
3310{
3311 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3312 (data(), size(), __sv.data(), __pos, __sv.size());
3313}
3314
3315template<class _CharT, class _Traits, class _Allocator>
3316inline _LIBCPP_INLINE_VISIBILITY
3317typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003318basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003319 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003320{
Alp Tokerec34c482014-05-15 11:27:39 +00003321 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003322 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003323 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003324}
3325
3326template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003327inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003328typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003329basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3330 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003331{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003332 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003333 (data(), size(), __c, __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003334}
3335
3336// find_last_not_of
3337
3338template<class _CharT, class _Traits, class _Allocator>
3339typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003340basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003341 size_type __pos,
3342 size_type __n) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003343{
Alp Tokerec34c482014-05-15 11:27:39 +00003344 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003345 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003346 (data(), size(), __s, __pos, __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003347}
3348
3349template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003350inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003351typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003352basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3353 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003354{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003355 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003356 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003357}
3358
3359template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003360inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003361typename basic_string<_CharT, _Traits, _Allocator>::size_type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003362basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(__self_view __sv,
3363 size_type __pos) const _NOEXCEPT
3364{
3365 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3366 (data(), size(), __sv.data(), __pos, __sv.size());
3367}
3368
3369template<class _CharT, class _Traits, class _Allocator>
3370inline _LIBCPP_INLINE_VISIBILITY
3371typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003372basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003373 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003374{
Alp Tokerec34c482014-05-15 11:27:39 +00003375 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003376 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003377 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003378}
3379
3380template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003381inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003382typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnanta6119a82011-05-29 19:57:12 +00003383basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3384 size_type __pos) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003385{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003386 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow8eb5acc2014-02-16 01:57:26 +00003387 (data(), size(), __c, __pos);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003388}
3389
3390// compare
3391
3392template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003393inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003394int
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003395basic_string<_CharT, _Traits, _Allocator>::compare(__self_view __sv) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003396{
Howard Hinnantfa06d752011-07-24 21:45:06 +00003397 size_t __lhs_sz = size();
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003398 size_t __rhs_sz = __sv.size();
3399 int __result = traits_type::compare(data(), __sv.data(),
Howard Hinnantfa06d752011-07-24 21:45:06 +00003400 _VSTD::min(__lhs_sz, __rhs_sz));
3401 if (__result != 0)
3402 return __result;
3403 if (__lhs_sz < __rhs_sz)
3404 return -1;
3405 if (__lhs_sz > __rhs_sz)
3406 return 1;
3407 return 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003408}
3409
3410template <class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003411inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003412int
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003413basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003414{
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003415 return compare(__self_view(__str));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003416}
3417
3418template <class _CharT, class _Traits, class _Allocator>
3419int
Howard Hinnanta6119a82011-05-29 19:57:12 +00003420basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3421 size_type __n1,
Howard Hinnant9dcdcde2013-06-28 16:59:19 +00003422 const value_type* __s,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003423 size_type __n2) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003424{
Alp Tokerec34c482014-05-15 11:27:39 +00003425 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003426 size_type __sz = size();
3427 if (__pos1 > __sz || __n2 == npos)
3428 this->__throw_out_of_range();
Howard Hinnant0949eed2011-06-30 21:18:19 +00003429 size_type __rlen = _VSTD::min(__n1, __sz - __pos1);
3430 int __r = traits_type::compare(data() + __pos1, __s, _VSTD::min(__rlen, __n2));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003431 if (__r == 0)
3432 {
3433 if (__rlen < __n2)
3434 __r = -1;
3435 else if (__rlen > __n2)
3436 __r = 1;
3437 }
3438 return __r;
3439}
3440
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003441template <class _CharT, class _Traits, class _Allocator>
3442inline _LIBCPP_INLINE_VISIBILITY
3443int
3444basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3445 size_type __n1,
3446 __self_view __sv) const
3447{
3448 return compare(__pos1, __n1, __sv.data(), __sv.size());
3449}
3450
3451template <class _CharT, class _Traits, class _Allocator>
3452inline _LIBCPP_INLINE_VISIBILITY
3453int
3454basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3455 size_type __n1,
3456 const basic_string& __str) const
3457{
3458 return compare(__pos1, __n1, __str.data(), __str.size());
3459}
3460
3461template <class _CharT, class _Traits, class _Allocator>
Marshall Clow6ac8de02016-09-24 22:45:42 +00003462template <class _Tp>
3463typename enable_if
3464<
3465 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3466 int
3467>::type
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003468basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3469 size_type __n1,
Marshall Clow6ac8de02016-09-24 22:45:42 +00003470 const _Tp& __t,
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003471 size_type __pos2,
3472 size_type __n2) const
3473{
Marshall Clow6ac8de02016-09-24 22:45:42 +00003474 __self_view __sv = __t;
Marshall Clow1e00d6d2016-07-21 05:31:24 +00003475 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3476}
3477
3478template <class _CharT, class _Traits, class _Allocator>
3479int
3480basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3481 size_type __n1,
3482 const basic_string& __str,
3483 size_type __pos2,
3484 size_type __n2) const
3485{
3486 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
3487}
3488
3489template <class _CharT, class _Traits, class _Allocator>
3490int
3491basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
3492{
3493 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3494 return compare(0, npos, __s, traits_type::length(__s));
3495}
3496
3497template <class _CharT, class _Traits, class _Allocator>
3498int
3499basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3500 size_type __n1,
3501 const value_type* __s) const
3502{
3503 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3504 return compare(__pos1, __n1, __s, traits_type::length(__s));
3505}
3506
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003507// __invariants
3508
3509template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003510inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003511bool
3512basic_string<_CharT, _Traits, _Allocator>::__invariants() const
3513{
3514 if (size() > capacity())
3515 return false;
3516 if (capacity() < __min_cap - 1)
3517 return false;
3518 if (data() == 0)
3519 return false;
3520 if (data()[size()] != value_type(0))
3521 return false;
3522 return true;
3523}
3524
3525// operator==
3526
3527template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003528inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003529bool
3530operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003531 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003532{
Howard Hinnant08dd2532013-04-22 23:55:13 +00003533 size_t __lhs_sz = __lhs.size();
3534 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
3535 __rhs.data(),
3536 __lhs_sz) == 0;
3537}
3538
3539template<class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003540inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant08dd2532013-04-22 23:55:13 +00003541bool
3542operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
3543 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
3544{
3545 size_t __lhs_sz = __lhs.size();
3546 if (__lhs_sz != __rhs.size())
3547 return false;
3548 const char* __lp = __lhs.data();
3549 const char* __rp = __rhs.data();
3550 if (__lhs.__is_long())
3551 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
3552 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
3553 if (*__lp != *__rp)
3554 return false;
3555 return true;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003556}
3557
3558template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003559inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003560bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003561operator==(const _CharT* __lhs,
3562 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003563{
Eric Fiselier4f241822015-08-28 03:02:37 +00003564 typedef basic_string<_CharT, _Traits, _Allocator> _String;
3565 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
3566 size_t __lhs_len = _Traits::length(__lhs);
3567 if (__lhs_len != __rhs.size()) return false;
3568 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003569}
3570
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003571template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003572inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003573bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003574operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
3575 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003576{
Eric Fiselier4f241822015-08-28 03:02:37 +00003577 typedef basic_string<_CharT, _Traits, _Allocator> _String;
3578 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
3579 size_t __rhs_len = _Traits::length(__rhs);
3580 if (__rhs_len != __lhs.size()) return false;
3581 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003582}
3583
Howard Hinnant324bb032010-08-22 00:02:43 +00003584template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003585inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003586bool
3587operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003588 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003589{
3590 return !(__lhs == __rhs);
3591}
3592
3593template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003594inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003595bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003596operator!=(const _CharT* __lhs,
3597 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003598{
3599 return !(__lhs == __rhs);
3600}
3601
3602template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003603inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003604bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003605operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3606 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003607{
3608 return !(__lhs == __rhs);
3609}
3610
3611// operator<
3612
3613template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003614inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003615bool
3616operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003617 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003618{
Howard Hinnant2644a7b2011-07-24 15:07:21 +00003619 return __lhs.compare(__rhs) < 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003620}
3621
3622template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003623inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003624bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003625operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3626 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003627{
Howard Hinnant2644a7b2011-07-24 15:07:21 +00003628 return __lhs.compare(__rhs) < 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003629}
3630
3631template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003632inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003633bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003634operator< (const _CharT* __lhs,
3635 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003636{
3637 return __rhs.compare(__lhs) > 0;
3638}
3639
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003640// operator>
3641
3642template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003643inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003644bool
3645operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003646 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003647{
3648 return __rhs < __lhs;
3649}
3650
3651template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003652inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003653bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003654operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3655 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003656{
3657 return __rhs < __lhs;
3658}
3659
3660template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003661inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003662bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003663operator> (const _CharT* __lhs,
3664 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003665{
3666 return __rhs < __lhs;
3667}
3668
3669// operator<=
3670
3671template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003672inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003673bool
3674operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003675 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003676{
3677 return !(__rhs < __lhs);
3678}
3679
3680template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003681inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003682bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003683operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3684 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003685{
3686 return !(__rhs < __lhs);
3687}
3688
3689template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003690inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003691bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003692operator<=(const _CharT* __lhs,
3693 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003694{
3695 return !(__rhs < __lhs);
3696}
3697
3698// operator>=
3699
3700template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003701inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003702bool
3703operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnanta6119a82011-05-29 19:57:12 +00003704 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003705{
3706 return !(__lhs < __rhs);
3707}
3708
3709template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003710inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003711bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003712operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3713 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003714{
3715 return !(__lhs < __rhs);
3716}
3717
3718template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003719inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003720bool
Howard Hinnanta6119a82011-05-29 19:57:12 +00003721operator>=(const _CharT* __lhs,
3722 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003723{
3724 return !(__lhs < __rhs);
3725}
3726
3727// operator +
3728
3729template<class _CharT, class _Traits, class _Allocator>
3730basic_string<_CharT, _Traits, _Allocator>
3731operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
3732 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3733{
3734 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
3735 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
3736 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
3737 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
3738 __r.append(__rhs.data(), __rhs_sz);
3739 return __r;
3740}
3741
3742template<class _CharT, class _Traits, class _Allocator>
3743basic_string<_CharT, _Traits, _Allocator>
3744operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
3745{
3746 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
3747 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = _Traits::length(__lhs);
3748 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
3749 __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz);
3750 __r.append(__rhs.data(), __rhs_sz);
3751 return __r;
3752}
3753
3754template<class _CharT, class _Traits, class _Allocator>
3755basic_string<_CharT, _Traits, _Allocator>
3756operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
3757{
3758 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
3759 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
3760 __r.__init(&__lhs, 1, 1 + __rhs_sz);
3761 __r.append(__rhs.data(), __rhs_sz);
3762 return __r;
3763}
3764
3765template<class _CharT, class _Traits, class _Allocator>
3766basic_string<_CharT, _Traits, _Allocator>
3767operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
3768{
3769 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
3770 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
3771 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = _Traits::length(__rhs);
3772 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
3773 __r.append(__rhs, __rhs_sz);
3774 return __r;
3775}
3776
3777template<class _CharT, class _Traits, class _Allocator>
3778basic_string<_CharT, _Traits, _Allocator>
3779operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
3780{
3781 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
3782 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
3783 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1);
3784 __r.push_back(__rhs);
3785 return __r;
3786}
3787
Eric Fiselier3e928972017-04-19 00:28:44 +00003788#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003789
3790template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003791inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003792basic_string<_CharT, _Traits, _Allocator>
3793operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
3794{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003795 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003796}
3797
3798template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003799inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003800basic_string<_CharT, _Traits, _Allocator>
3801operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
3802{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003803 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003804}
3805
3806template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003807inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003808basic_string<_CharT, _Traits, _Allocator>
3809operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
3810{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003811 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003812}
3813
3814template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003815inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003816basic_string<_CharT, _Traits, _Allocator>
3817operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
3818{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003819 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003820}
3821
3822template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003823inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003824basic_string<_CharT, _Traits, _Allocator>
3825operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
3826{
3827 __rhs.insert(__rhs.begin(), __lhs);
Howard Hinnant0949eed2011-06-30 21:18:19 +00003828 return _VSTD::move(__rhs);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003829}
3830
3831template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003832inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003833basic_string<_CharT, _Traits, _Allocator>
3834operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
3835{
Howard Hinnant0949eed2011-06-30 21:18:19 +00003836 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003837}
3838
3839template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003840inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003841basic_string<_CharT, _Traits, _Allocator>
3842operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
3843{
3844 __lhs.push_back(__rhs);
Howard Hinnant0949eed2011-06-30 21:18:19 +00003845 return _VSTD::move(__lhs);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003846}
3847
Eric Fiselier3e928972017-04-19 00:28:44 +00003848#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003849
3850// swap
3851
3852template<class _CharT, class _Traits, class _Allocator>
Howard Hinnant1e564242013-10-04 22:09:00 +00003853inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003854void
Howard Hinnanta6119a82011-05-29 19:57:12 +00003855swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant53f7d4c2011-06-03 18:40:47 +00003856 basic_string<_CharT, _Traits, _Allocator>& __rhs)
3857 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003858{
3859 __lhs.swap(__rhs);
3860}
3861
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003862#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
3863
3864typedef basic_string<char16_t> u16string;
3865typedef basic_string<char32_t> u32string;
3866
Howard Hinnant324bb032010-08-22 00:02:43 +00003867#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003868
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003869_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = 0, int __base = 10);
3870_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = 0, int __base = 10);
3871_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = 0, int __base = 10);
3872_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = 0, int __base = 10);
3873_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = 0, int __base = 10);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003874
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003875_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = 0);
3876_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = 0);
3877_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = 0);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003878
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003879_LIBCPP_FUNC_VIS string to_string(int __val);
3880_LIBCPP_FUNC_VIS string to_string(unsigned __val);
3881_LIBCPP_FUNC_VIS string to_string(long __val);
3882_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
3883_LIBCPP_FUNC_VIS string to_string(long long __val);
3884_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
3885_LIBCPP_FUNC_VIS string to_string(float __val);
3886_LIBCPP_FUNC_VIS string to_string(double __val);
3887_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003888
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003889_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = 0, int __base = 10);
3890_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = 0, int __base = 10);
3891_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = 0, int __base = 10);
3892_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = 0, int __base = 10);
3893_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = 0, int __base = 10);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003894
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003895_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = 0);
3896_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = 0);
3897_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = 0);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003898
Howard Hinnant0f678bd2013-08-12 18:38:34 +00003899_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
3900_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
3901_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
3902_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
3903_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
3904_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
3905_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
3906_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
3907_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Howard Hinnanta6a062d2010-06-02 18:20:39 +00003908
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003909template<class _CharT, class _Traits, class _Allocator>
3910 const typename basic_string<_CharT, _Traits, _Allocator>::size_type
3911 basic_string<_CharT, _Traits, _Allocator>::npos;
3912
3913template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierc3589a82017-01-04 23:56:00 +00003914struct _LIBCPP_TEMPLATE_VIS hash<basic_string<_CharT, _Traits, _Allocator> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003915 : public unary_function<basic_string<_CharT, _Traits, _Allocator>, size_t>
3916{
3917 size_t
Howard Hinnanta6119a82011-05-29 19:57:12 +00003918 operator()(const basic_string<_CharT, _Traits, _Allocator>& __val) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003919};
3920
3921template<class _CharT, class _Traits, class _Allocator>
3922size_t
3923hash<basic_string<_CharT, _Traits, _Allocator> >::operator()(
Howard Hinnanta6119a82011-05-29 19:57:12 +00003924 const basic_string<_CharT, _Traits, _Allocator>& __val) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003925{
Sean Huntaffd9e52011-07-29 23:31:56 +00003926 return __do_string_hash(__val.data(), __val.data() + __val.size());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003927}
3928
Howard Hinnant464aa5c2011-07-18 15:51:59 +00003929template<class _CharT, class _Traits, class _Allocator>
3930basic_ostream<_CharT, _Traits>&
3931operator<<(basic_ostream<_CharT, _Traits>& __os,
3932 const basic_string<_CharT, _Traits, _Allocator>& __str);
3933
3934template<class _CharT, class _Traits, class _Allocator>
3935basic_istream<_CharT, _Traits>&
3936operator>>(basic_istream<_CharT, _Traits>& __is,
3937 basic_string<_CharT, _Traits, _Allocator>& __str);
3938
3939template<class _CharT, class _Traits, class _Allocator>
3940basic_istream<_CharT, _Traits>&
3941getline(basic_istream<_CharT, _Traits>& __is,
3942 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
3943
3944template<class _CharT, class _Traits, class _Allocator>
3945inline _LIBCPP_INLINE_VISIBILITY
3946basic_istream<_CharT, _Traits>&
3947getline(basic_istream<_CharT, _Traits>& __is,
3948 basic_string<_CharT, _Traits, _Allocator>& __str);
3949
Eric Fiselier3e928972017-04-19 00:28:44 +00003950#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant464aa5c2011-07-18 15:51:59 +00003951
3952template<class _CharT, class _Traits, class _Allocator>
3953inline _LIBCPP_INLINE_VISIBILITY
3954basic_istream<_CharT, _Traits>&
3955getline(basic_istream<_CharT, _Traits>&& __is,
3956 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
3957
3958template<class _CharT, class _Traits, class _Allocator>
3959inline _LIBCPP_INLINE_VISIBILITY
3960basic_istream<_CharT, _Traits>&
3961getline(basic_istream<_CharT, _Traits>&& __is,
3962 basic_string<_CharT, _Traits, _Allocator>& __str);
3963
Eric Fiselier3e928972017-04-19 00:28:44 +00003964#endif // _LIBCPP_CXX03_LANG
Howard Hinnant464aa5c2011-07-18 15:51:59 +00003965
Howard Hinnant499cea12013-08-23 17:37:05 +00003966#if _LIBCPP_DEBUG_LEVEL >= 2
3967
3968template<class _CharT, class _Traits, class _Allocator>
3969bool
3970basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
3971{
3972 return this->data() <= _VSTD::__to_raw_pointer(__i->base()) &&
3973 _VSTD::__to_raw_pointer(__i->base()) < this->data() + this->size();
3974}
3975
3976template<class _CharT, class _Traits, class _Allocator>
3977bool
3978basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
3979{
3980 return this->data() < _VSTD::__to_raw_pointer(__i->base()) &&
3981 _VSTD::__to_raw_pointer(__i->base()) <= this->data() + this->size();
3982}
3983
3984template<class _CharT, class _Traits, class _Allocator>
3985bool
3986basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
3987{
3988 const value_type* __p = _VSTD::__to_raw_pointer(__i->base()) + __n;
3989 return this->data() <= __p && __p <= this->data() + this->size();
3990}
3991
3992template<class _CharT, class _Traits, class _Allocator>
3993bool
3994basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
3995{
3996 const value_type* __p = _VSTD::__to_raw_pointer(__i->base()) + __n;
3997 return this->data() <= __p && __p < this->data() + this->size();
3998}
3999
4000#endif // _LIBCPP_DEBUG_LEVEL >= 2
4001
Shoaib Meenai68506702017-06-29 02:52:46 +00004002_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_string<char>)
4003_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_string<wchar_t>)
Shoaib Meenai68506702017-06-29 02:52:46 +00004004
Marshall Clow15234322013-07-23 17:05:24 +00004005#if _LIBCPP_STD_VER > 11
4006// Literal suffixes for basic_string [basic.string.literals]
Marshall Clow8d9dd7a2013-10-05 21:18:32 +00004007inline namespace literals
Marshall Clow15234322013-07-23 17:05:24 +00004008{
4009 inline namespace string_literals
4010 {
Howard Hinnantab61b2c2013-08-07 19:39:48 +00004011 inline _LIBCPP_INLINE_VISIBILITY
4012 basic_string<char> operator "" s( const char *__str, size_t __len )
4013 {
4014 return basic_string<char> (__str, __len);
4015 }
Marshall Clow15234322013-07-23 17:05:24 +00004016
Howard Hinnantab61b2c2013-08-07 19:39:48 +00004017 inline _LIBCPP_INLINE_VISIBILITY
4018 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4019 {
4020 return basic_string<wchar_t> (__str, __len);
4021 }
Marshall Clow15234322013-07-23 17:05:24 +00004022
Howard Hinnantab61b2c2013-08-07 19:39:48 +00004023 inline _LIBCPP_INLINE_VISIBILITY
4024 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4025 {
4026 return basic_string<char16_t> (__str, __len);
4027 }
Marshall Clow15234322013-07-23 17:05:24 +00004028
Howard Hinnantab61b2c2013-08-07 19:39:48 +00004029 inline _LIBCPP_INLINE_VISIBILITY
4030 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4031 {
4032 return basic_string<char32_t> (__str, __len);
4033 }
Marshall Clow15234322013-07-23 17:05:24 +00004034 }
4035}
4036#endif
4037
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004038_LIBCPP_END_NAMESPACE_STD
4039
Eric Fiselier018a3d52017-05-31 22:07:49 +00004040_LIBCPP_POP_MACROS
4041
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004042#endif // _LIBCPP_STRING