blob: 2f846eda06c580ed6f98f5172a950918300f67c6 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===--------------------------- string -----------------------------------===//
3//
Chandler Carruth7642bb12019-01-19 08:50:56 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_STRING
11#define _LIBCPP_STRING
12
13/*
14 string synopsis
15
16namespace std
17{
18
19template <class stateT>
20class fpos
21{
22private:
23 stateT st;
24public:
25 fpos(streamoff = streamoff());
26
27 operator streamoff() const;
28
29 stateT state() const;
30 void state(stateT);
31
32 fpos& operator+=(streamoff);
33 fpos operator+ (streamoff) const;
34 fpos& operator-=(streamoff);
35 fpos operator- (streamoff) const;
36};
37
38template <class stateT> streamoff operator-(const fpos<stateT>& x, const fpos<stateT>& y);
39
40template <class stateT> bool operator==(const fpos<stateT>& x, const fpos<stateT>& y);
41template <class stateT> bool operator!=(const fpos<stateT>& x, const fpos<stateT>& y);
42
43template <class charT>
44struct char_traits
45{
46 typedef charT char_type;
47 typedef ... int_type;
48 typedef streamoff off_type;
49 typedef streampos pos_type;
50 typedef mbstate_t state_type;
51
Howard Hinnantaeb17d82011-05-29 19:57:12 +000052 static void assign(char_type& c1, const char_type& c2) noexcept;
Howard Hinnant270c00e2012-07-20 19:09:12 +000053 static constexpr bool eq(char_type c1, char_type c2) noexcept;
54 static constexpr bool lt(char_type c1, char_type c2) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000055
56 static int compare(const char_type* s1, const char_type* s2, size_t n);
57 static size_t length(const char_type* s);
58 static const char_type* find(const char_type* s, size_t n, const char_type& a);
59 static char_type* move(char_type* s1, const char_type* s2, size_t n);
60 static char_type* copy(char_type* s1, const char_type* s2, size_t n);
61 static char_type* assign(char_type* s, size_t n, char_type a);
62
Howard Hinnant270c00e2012-07-20 19:09:12 +000063 static constexpr int_type not_eof(int_type c) noexcept;
64 static constexpr char_type to_char_type(int_type c) noexcept;
65 static constexpr int_type to_int_type(char_type c) noexcept;
66 static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept;
67 static constexpr int_type eof() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000068};
69
70template <> struct char_traits<char>;
71template <> struct char_traits<wchar_t>;
72
Howard Hinnant3b6579a2010-08-22 00:02:43 +000073template<class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
Howard Hinnantc51e1022010-05-11 19:42:16 +000074class basic_string
75{
Howard Hinnant3b6579a2010-08-22 00:02:43 +000076public:
77// types:
Howard Hinnantc51e1022010-05-11 19:42:16 +000078 typedef traits traits_type;
79 typedef typename traits_type::char_type value_type;
80 typedef Allocator allocator_type;
81 typedef typename allocator_type::size_type size_type;
82 typedef typename allocator_type::difference_type difference_type;
83 typedef typename allocator_type::reference reference;
84 typedef typename allocator_type::const_reference const_reference;
85 typedef typename allocator_type::pointer pointer;
86 typedef typename allocator_type::const_pointer const_pointer;
87 typedef implementation-defined iterator;
88 typedef implementation-defined const_iterator;
89 typedef std::reverse_iterator<iterator> reverse_iterator;
90 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
91
92 static const size_type npos = -1;
93
Howard Hinnant3e276872011-06-03 18:40:47 +000094 basic_string()
95 noexcept(is_nothrow_default_constructible<allocator_type>::value);
96 explicit basic_string(const allocator_type& a);
Howard Hinnantc51e1022010-05-11 19:42:16 +000097 basic_string(const basic_string& str);
Howard Hinnant3e276872011-06-03 18:40:47 +000098 basic_string(basic_string&& str)
99 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clow83445802016-04-07 18:13:41 +0000100 basic_string(const basic_string& str, size_type pos,
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000101 const allocator_type& a = allocator_type());
Marshall Clow83445802016-04-07 18:13:41 +0000102 basic_string(const basic_string& str, size_type pos, size_type n,
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000103 const Allocator& a = Allocator());
Marshall Clow78dbe462016-11-14 18:22:19 +0000104 template<class T>
105 basic_string(const T& t, size_type pos, size_type n, const Allocator& a = Allocator()); // C++17
Marshall Clowe46031a2018-07-02 18:41:15 +0000106 template <class T>
107 explicit basic_string(const T& t, const Allocator& a = Allocator()); // C++17
Howard Hinnantd17880b2013-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 Hinnantc51e1022010-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 Hinnanta5f4f8e2010-06-02 18:20:39 +0000112 basic_string(InputIterator begin, InputIterator end,
113 const allocator_type& a = allocator_type());
Howard Hinnantc51e1022010-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 Clowdf63a6d2016-07-21 05:31:24 +0000120 operator basic_string_view<charT, traits>() const noexcept;
121
Howard Hinnantc51e1022010-05-11 19:42:16 +0000122 basic_string& operator=(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000123 template <class T>
124 basic_string& operator=(const T& t); // C++17
Howard Hinnant3e276872011-06-03 18:40:47 +0000125 basic_string& operator=(basic_string&& str)
126 noexcept(
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000127 allocator_type::propagate_on_container_move_assignment::value ||
128 allocator_type::is_always_equal::value ); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000129 basic_string& operator=(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000130 basic_string& operator=(value_type c);
131 basic_string& operator=(initializer_list<value_type>);
132
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000133 iterator begin() noexcept;
134 const_iterator begin() const noexcept;
135 iterator end() noexcept;
136 const_iterator end() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000137
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000138 reverse_iterator rbegin() noexcept;
139 const_reverse_iterator rbegin() const noexcept;
140 reverse_iterator rend() noexcept;
141 const_reverse_iterator rend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000142
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000143 const_iterator cbegin() const noexcept;
144 const_iterator cend() const noexcept;
145 const_reverse_iterator crbegin() const noexcept;
146 const_reverse_iterator crend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000147
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000148 size_type size() const noexcept;
149 size_type length() const noexcept;
150 size_type max_size() const noexcept;
151 size_type capacity() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000152
153 void resize(size_type n, value_type c);
154 void resize(size_type n);
155
156 void reserve(size_type res_arg = 0);
157 void shrink_to_fit();
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000158 void clear() noexcept;
159 bool empty() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000160
161 const_reference operator[](size_type pos) const;
162 reference operator[](size_type pos);
163
164 const_reference at(size_type n) const;
165 reference at(size_type n);
166
167 basic_string& operator+=(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000168 template <class T>
169 basic_string& operator+=(const T& t); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000170 basic_string& operator+=(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000171 basic_string& operator+=(value_type c);
172 basic_string& operator+=(initializer_list<value_type>);
173
174 basic_string& append(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000175 template <class T>
176 basic_string& append(const T& t); // C++17
Marshall Clow8db7fb02014-03-04 19:17:19 +0000177 basic_string& append(const basic_string& str, size_type pos, size_type n=npos); //C++14
Marshall Clow82513342016-09-24 22:45:42 +0000178 template <class T>
179 basic_string& append(const T& t, size_type pos, size_type n=npos); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000180 basic_string& append(const value_type* s, size_type n);
181 basic_string& append(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000182 basic_string& append(size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000183 template<class InputIterator>
184 basic_string& append(InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000185 basic_string& append(initializer_list<value_type>);
186
187 void push_back(value_type c);
188 void pop_back();
189 reference front();
190 const_reference front() const;
191 reference back();
192 const_reference back() const;
193
194 basic_string& assign(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000195 template <class T>
196 basic_string& assign(const T& t); // C++17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000197 basic_string& assign(basic_string&& str);
Marshall Clow8db7fb02014-03-04 19:17:19 +0000198 basic_string& assign(const basic_string& str, size_type pos, size_type n=npos); // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000199 template <class T>
200 basic_string& assign(const T& t, size_type pos, size_type n=npos); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000201 basic_string& assign(const value_type* s, size_type n);
202 basic_string& assign(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000203 basic_string& assign(size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000204 template<class InputIterator>
205 basic_string& assign(InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000206 basic_string& assign(initializer_list<value_type>);
207
208 basic_string& insert(size_type pos1, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000209 template <class T>
210 basic_string& insert(size_type pos1, const T& t);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000211 basic_string& insert(size_type pos1, const basic_string& str,
212 size_type pos2, size_type n);
Marshall Clow82513342016-09-24 22:45:42 +0000213 template <class T>
214 basic_string& insert(size_type pos1, const T& t, size_type pos2, size_type n); // C++17
Marshall Clow8db7fb02014-03-04 19:17:19 +0000215 basic_string& insert(size_type pos, const value_type* s, size_type n=npos); //C++14
Howard Hinnantd17880b2013-06-28 16:59:19 +0000216 basic_string& insert(size_type pos, const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000217 basic_string& insert(size_type pos, size_type n, value_type c);
218 iterator insert(const_iterator p, value_type c);
219 iterator insert(const_iterator p, size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000220 template<class InputIterator>
221 iterator insert(const_iterator p, InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000222 iterator insert(const_iterator p, initializer_list<value_type>);
223
224 basic_string& erase(size_type pos = 0, size_type n = npos);
225 iterator erase(const_iterator position);
226 iterator erase(const_iterator first, const_iterator last);
227
228 basic_string& replace(size_type pos1, size_type n1, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000229 template <class T>
230 basic_string& replace(size_type pos1, size_type n1, const T& t); // C++17
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000231 basic_string& replace(size_type pos1, size_type n1, const basic_string& str,
Marshall Clow8db7fb02014-03-04 19:17:19 +0000232 size_type pos2, size_type n2=npos); // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000233 template <class T>
234 basic_string& replace(size_type pos1, size_type n1, const T& t,
235 size_type pos2, size_type n); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000236 basic_string& replace(size_type pos, size_type n1, const value_type* s, size_type n2);
237 basic_string& replace(size_type pos, size_type n1, const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000238 basic_string& replace(size_type pos, size_type n1, size_type n2, value_type c);
Howard Hinnant990d6e82010-11-17 21:11:40 +0000239 basic_string& replace(const_iterator i1, const_iterator i2, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000240 template <class T>
241 basic_string& replace(const_iterator i1, const_iterator i2, const T& t); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000242 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s, size_type n);
243 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s);
Howard Hinnant990d6e82010-11-17 21:11:40 +0000244 basic_string& replace(const_iterator i1, const_iterator i2, size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000245 template<class InputIterator>
Howard Hinnant990d6e82010-11-17 21:11:40 +0000246 basic_string& replace(const_iterator i1, const_iterator i2, InputIterator j1, InputIterator j2);
247 basic_string& replace(const_iterator i1, const_iterator i2, initializer_list<value_type>);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000248
Howard Hinnantd17880b2013-06-28 16:59:19 +0000249 size_type copy(value_type* s, size_type n, size_type pos = 0) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000250 basic_string substr(size_type pos = 0, size_type n = npos) const;
251
Howard Hinnant3e276872011-06-03 18:40:47 +0000252 void swap(basic_string& str)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000253 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
254 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000255
Howard Hinnantd17880b2013-06-28 16:59:19 +0000256 const value_type* c_str() const noexcept;
257 const value_type* data() const noexcept;
Marshall Clowd3c22392016-03-08 15:44:30 +0000258 value_type* data() noexcept; // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000259
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000260 allocator_type get_allocator() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000261
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000262 size_type find(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000263 template <class T>
264 size_type find(const T& t, size_type pos = 0) const; // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000265 size_type find(const value_type* s, size_type pos, size_type n) const noexcept;
266 size_type find(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000267 size_type find(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000268
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000269 size_type rfind(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000270 template <class T>
271 size_type rfind(const T& t, size_type pos = npos) const; // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000272 size_type rfind(const value_type* s, size_type pos, size_type n) const noexcept;
273 size_type rfind(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000274 size_type rfind(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000275
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000276 size_type find_first_of(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000277 template <class T>
278 size_type find_first_of(const T& t, size_type pos = 0) const; // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000279 size_type find_first_of(const value_type* s, size_type pos, size_type n) const noexcept;
280 size_type find_first_of(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000281 size_type find_first_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000282
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000283 size_type find_last_of(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000284 template <class T>
285 size_type find_last_of(const T& t, size_type pos = npos) const noexcept; // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000286 size_type find_last_of(const value_type* s, size_type pos, size_type n) const noexcept;
287 size_type find_last_of(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000288 size_type find_last_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000289
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000290 size_type find_first_not_of(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000291 template <class T>
292 size_type find_first_not_of(const T& t, size_type pos = 0) const; // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000293 size_type find_first_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
294 size_type find_first_not_of(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000295 size_type find_first_not_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000296
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000297 size_type find_last_not_of(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000298 template <class T>
299 size_type find_last_not_of(const T& t, size_type pos = npos) const; // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000300 size_type find_last_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
301 size_type find_last_not_of(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000302 size_type find_last_not_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000303
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000304 int compare(const basic_string& str) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000305 template <class T>
306 int compare(const T& t) const noexcept; // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000307 int compare(size_type pos1, size_type n1, const basic_string& str) const;
Marshall Clowe46031a2018-07-02 18:41:15 +0000308 template <class T>
309 int compare(size_type pos1, size_type n1, const T& t) const; // C++17
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000310 int compare(size_type pos1, size_type n1, const basic_string& str,
Marshall Clow8db7fb02014-03-04 19:17:19 +0000311 size_type pos2, size_type n2=npos) const; // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000312 template <class T>
313 int compare(size_type pos1, size_type n1, const T& t,
314 size_type pos2, size_type n2=npos) const; // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000315 int compare(const value_type* s) const noexcept;
316 int compare(size_type pos1, size_type n1, const value_type* s) const;
317 int compare(size_type pos1, size_type n1, const value_type* s, size_type n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000318
Marshall Clow18c293b2017-12-04 20:11:38 +0000319 bool starts_with(basic_string_view<charT, traits> sv) const noexcept; // C++2a
320 bool starts_with(charT c) const noexcept; // C++2a
321 bool starts_with(const charT* s) const; // C++2a
322 bool ends_with(basic_string_view<charT, traits> sv) const noexcept; // C++2a
323 bool ends_with(charT c) const noexcept; // C++2a
324 bool ends_with(const charT* s) const; // C++2a
325
Howard Hinnantc51e1022010-05-11 19:42:16 +0000326 bool __invariants() const;
327};
328
Marshall Clowa0563332018-02-08 06:34:03 +0000329template<class InputIterator,
330 class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
331basic_string(InputIterator, InputIterator, Allocator = Allocator())
332 -> basic_string<typename iterator_traits<InputIterator>::value_type,
333 char_traits<typename iterator_traits<InputIterator>::value_type>,
334 Allocator>; // C++17
335
Howard Hinnantc51e1022010-05-11 19:42:16 +0000336template<class charT, class traits, class Allocator>
337basic_string<charT, traits, Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000338operator+(const basic_string<charT, traits, Allocator>& lhs,
339 const basic_string<charT, traits, Allocator>& rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000340
341template<class charT, class traits, class Allocator>
342basic_string<charT, traits, Allocator>
343operator+(const charT* lhs , const basic_string<charT,traits,Allocator>&rhs);
344
345template<class charT, class traits, class Allocator>
346basic_string<charT, traits, Allocator>
347operator+(charT lhs, const basic_string<charT,traits,Allocator>& rhs);
348
349template<class charT, class traits, class Allocator>
350basic_string<charT, traits, Allocator>
351operator+(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs);
352
353template<class charT, class traits, class Allocator>
354basic_string<charT, traits, Allocator>
355operator+(const basic_string<charT, traits, Allocator>& lhs, charT rhs);
356
357template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000358bool operator==(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000359 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000360
361template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000362bool operator==(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000363
364template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000365bool operator==(const basic_string<charT,traits,Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000366
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000367template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000368bool operator!=(const basic_string<charT,traits,Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000369 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000370
371template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000372bool operator!=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000373
374template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000375bool operator!=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000376
377template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000378bool operator< (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000379 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000380
381template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000382bool operator< (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000383
384template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000385bool operator< (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000386
387template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000388bool operator> (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000389 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000390
391template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000392bool operator> (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000393
394template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000395bool operator> (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000396
397template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000398bool operator<=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000399 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000400
401template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000402bool operator<=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000403
404template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000405bool operator<=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000406
407template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000408bool operator>=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000409 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000410
411template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000412bool operator>=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000413
414template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000415bool operator>=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000416
417template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000418void swap(basic_string<charT, traits, Allocator>& lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +0000419 basic_string<charT, traits, Allocator>& rhs)
420 noexcept(noexcept(lhs.swap(rhs)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000421
422template<class charT, class traits, class Allocator>
423basic_istream<charT, traits>&
424operator>>(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
425
426template<class charT, class traits, class Allocator>
427basic_ostream<charT, traits>&
428operator<<(basic_ostream<charT, traits>& os, const basic_string<charT, traits, Allocator>& str);
429
430template<class charT, class traits, class Allocator>
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000431basic_istream<charT, traits>&
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000432getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str,
433 charT delim);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000434
435template<class charT, class traits, class Allocator>
436basic_istream<charT, traits>&
437getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
438
Marshall Clow29b53f22018-12-14 18:49:35 +0000439template<class charT, class traits, class Allocator, class U>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200440typename basic_string<charT, traits, Allocator>::size_type
441erase(basic_string<charT, traits, Allocator>& c, const U& value); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000442template<class charT, class traits, class Allocator, class Predicate>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200443typename basic_string<charT, traits, Allocator>::size_type
444erase_if(basic_string<charT, traits, Allocator>& c, Predicate pred); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000445
Howard Hinnantc51e1022010-05-11 19:42:16 +0000446typedef basic_string<char> string;
447typedef basic_string<wchar_t> wstring;
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000448typedef basic_string<char16_t> u16string;
449typedef basic_string<char32_t> u32string;
450
451int stoi (const string& str, size_t* idx = 0, int base = 10);
452long stol (const string& str, size_t* idx = 0, int base = 10);
453unsigned long stoul (const string& str, size_t* idx = 0, int base = 10);
454long long stoll (const string& str, size_t* idx = 0, int base = 10);
455unsigned long long stoull(const string& str, size_t* idx = 0, int base = 10);
456
457float stof (const string& str, size_t* idx = 0);
458double stod (const string& str, size_t* idx = 0);
459long double stold(const string& str, size_t* idx = 0);
460
461string to_string(int val);
462string to_string(unsigned val);
463string to_string(long val);
464string to_string(unsigned long val);
465string to_string(long long val);
466string to_string(unsigned long long val);
467string to_string(float val);
468string to_string(double val);
469string to_string(long double val);
470
471int stoi (const wstring& str, size_t* idx = 0, int base = 10);
472long stol (const wstring& str, size_t* idx = 0, int base = 10);
473unsigned long stoul (const wstring& str, size_t* idx = 0, int base = 10);
474long long stoll (const wstring& str, size_t* idx = 0, int base = 10);
475unsigned long long stoull(const wstring& str, size_t* idx = 0, int base = 10);
476
477float stof (const wstring& str, size_t* idx = 0);
478double stod (const wstring& str, size_t* idx = 0);
479long double stold(const wstring& str, size_t* idx = 0);
480
481wstring to_wstring(int val);
482wstring to_wstring(unsigned val);
483wstring to_wstring(long val);
484wstring to_wstring(unsigned long val);
485wstring to_wstring(long long val);
486wstring to_wstring(unsigned long long val);
487wstring to_wstring(float val);
488wstring to_wstring(double val);
489wstring to_wstring(long double val);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000490
491template <> struct hash<string>;
492template <> struct hash<u16string>;
493template <> struct hash<u32string>;
494template <> struct hash<wstring>;
495
Marshall Clowcba751f2013-07-23 17:05:24 +0000496basic_string<char> operator "" s( const char *str, size_t len ); // C++14
497basic_string<wchar_t> operator "" s( const wchar_t *str, size_t len ); // C++14
498basic_string<char16_t> operator "" s( const char16_t *str, size_t len ); // C++14
499basic_string<char32_t> operator "" s( const char32_t *str, size_t len ); // C++14
500
Howard Hinnantc51e1022010-05-11 19:42:16 +0000501} // std
502
503*/
504
505#include <__config>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000506#include <string_view>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000507#include <iosfwd>
508#include <cstring>
Howard Hinnant155c2af2010-05-24 17:49:41 +0000509#include <cstdio> // For EOF.
Howard Hinnantc51e1022010-05-11 19:42:16 +0000510#include <cwchar>
511#include <algorithm>
512#include <iterator>
513#include <utility>
514#include <memory>
515#include <stdexcept>
516#include <type_traits>
517#include <initializer_list>
518#include <__functional_base>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000519#include <version>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000520#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
521#include <cstdint>
522#endif
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000523
Eric Fiselier14b6de92014-08-10 23:53:08 +0000524#include <__debug>
525
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000526#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000527#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000528#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000529
Eric Fiselierf4433a32017-05-31 22:07:49 +0000530_LIBCPP_PUSH_MACROS
531#include <__undef_macros>
532
533
Howard Hinnantc51e1022010-05-11 19:42:16 +0000534_LIBCPP_BEGIN_NAMESPACE_STD
535
536// fpos
537
538template <class _StateT>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000539class _LIBCPP_TEMPLATE_VIS fpos
Howard Hinnantc51e1022010-05-11 19:42:16 +0000540{
541private:
542 _StateT __st_;
543 streamoff __off_;
544public:
545 _LIBCPP_INLINE_VISIBILITY fpos(streamoff __off = streamoff()) : __st_(), __off_(__off) {}
546
547 _LIBCPP_INLINE_VISIBILITY operator streamoff() const {return __off_;}
548
549 _LIBCPP_INLINE_VISIBILITY _StateT state() const {return __st_;}
550 _LIBCPP_INLINE_VISIBILITY void state(_StateT __st) {__st_ = __st;}
551
552 _LIBCPP_INLINE_VISIBILITY fpos& operator+=(streamoff __off) {__off_ += __off; return *this;}
553 _LIBCPP_INLINE_VISIBILITY fpos operator+ (streamoff __off) const {fpos __t(*this); __t += __off; return __t;}
554 _LIBCPP_INLINE_VISIBILITY fpos& operator-=(streamoff __off) {__off_ -= __off; return *this;}
555 _LIBCPP_INLINE_VISIBILITY fpos operator- (streamoff __off) const {fpos __t(*this); __t -= __off; return __t;}
556};
557
558template <class _StateT>
559inline _LIBCPP_INLINE_VISIBILITY
560streamoff operator-(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
561 {return streamoff(__x) - streamoff(__y);}
562
563template <class _StateT>
564inline _LIBCPP_INLINE_VISIBILITY
565bool operator==(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
566 {return streamoff(__x) == streamoff(__y);}
567
568template <class _StateT>
569inline _LIBCPP_INLINE_VISIBILITY
570bool operator!=(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
571 {return streamoff(__x) != streamoff(__y);}
572
Howard Hinnantc51e1022010-05-11 19:42:16 +0000573// basic_string
574
575template<class _CharT, class _Traits, class _Allocator>
576basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000577operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
578 const basic_string<_CharT, _Traits, _Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000579
580template<class _CharT, class _Traits, class _Allocator>
581basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000582operator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000583
584template<class _CharT, class _Traits, class _Allocator>
585basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000586operator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000587
588template<class _CharT, class _Traits, class _Allocator>
Louis Dionne8f8d95d2018-11-21 17:31:55 +0000589inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000590basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000591operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000592
593template<class _CharT, class _Traits, class _Allocator>
594basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000595operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000596
Shoaib Meenaiea363712017-07-29 02:54:41 +0000597_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&))
598
Howard Hinnantc51e1022010-05-11 19:42:16 +0000599template <bool>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000600class _LIBCPP_TEMPLATE_VIS __basic_string_common
Howard Hinnantc51e1022010-05-11 19:42:16 +0000601{
602protected:
Marshall Clow8fea1612016-08-25 15:09:01 +0000603 _LIBCPP_NORETURN void __throw_length_error() const;
604 _LIBCPP_NORETURN void __throw_out_of_range() const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000605};
606
607template <bool __b>
608void
609__basic_string_common<__b>::__throw_length_error() const
610{
Marshall Clow8fea1612016-08-25 15:09:01 +0000611 _VSTD::__throw_length_error("basic_string");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000612}
613
614template <bool __b>
615void
616__basic_string_common<__b>::__throw_out_of_range() const
617{
Marshall Clow8fea1612016-08-25 15:09:01 +0000618 _VSTD::__throw_out_of_range("basic_string");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000619}
620
Eric Fiselier1b57fa82016-09-15 22:27:07 +0000621_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __basic_string_common<true>)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000622
Marshall Clow039b2f02016-01-13 21:54:34 +0000623#ifdef _LIBCPP_NO_EXCEPTIONS
624template <class _Iter>
625struct __libcpp_string_gets_noexcept_iterator_impl : public true_type {};
626#elif defined(_LIBCPP_HAS_NO_NOEXCEPT)
627template <class _Iter>
628struct __libcpp_string_gets_noexcept_iterator_impl : public false_type {};
629#else
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500630template <class _Iter, bool = __is_cpp17_forward_iterator<_Iter>::value>
Marshall Clow039b2f02016-01-13 21:54:34 +0000631struct __libcpp_string_gets_noexcept_iterator_impl : public _LIBCPP_BOOL_CONSTANT((
Louis Dionne173f29e2019-05-29 16:01:36 +0000632 noexcept(++(declval<_Iter&>())) &&
633 is_nothrow_assignable<_Iter&, _Iter>::value &&
634 noexcept(declval<_Iter>() == declval<_Iter>()) &&
Marshall Clow039b2f02016-01-13 21:54:34 +0000635 noexcept(*declval<_Iter>())
636)) {};
637
Louis Dionne173f29e2019-05-29 16:01:36 +0000638template <class _Iter>
Marshall Clow039b2f02016-01-13 21:54:34 +0000639struct __libcpp_string_gets_noexcept_iterator_impl<_Iter, false> : public false_type {};
640#endif
641
642
643template <class _Iter>
644struct __libcpp_string_gets_noexcept_iterator
645 : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value || __libcpp_string_gets_noexcept_iterator_impl<_Iter>::value) {};
646
Marshall Clow82513342016-09-24 22:45:42 +0000647template <class _CharT, class _Traits, class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500648struct __can_be_converted_to_string_view : public _BoolConstant<
649 is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
650 !is_convertible<const _Tp&, const _CharT*>::value
651 > {};
Marshall Clow82513342016-09-24 22:45:42 +0000652
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000653#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000654
655template <class _CharT, size_t = sizeof(_CharT)>
656struct __padding
657{
658 unsigned char __xx[sizeof(_CharT)-1];
659};
660
661template <class _CharT>
662struct __padding<_CharT, 1>
663{
664};
665
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000666#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000667
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000668template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000669class _LIBCPP_TEMPLATE_VIS basic_string
Howard Hinnantc51e1022010-05-11 19:42:16 +0000670 : private __basic_string_common<true>
671{
672public:
673 typedef basic_string __self;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000674 typedef basic_string_view<_CharT, _Traits> __self_view;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000675 typedef _Traits traits_type;
Marshall Clowa3a74e02017-03-15 18:41:11 +0000676 typedef _CharT value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000677 typedef _Allocator allocator_type;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000678 typedef allocator_traits<allocator_type> __alloc_traits;
679 typedef typename __alloc_traits::size_type size_type;
680 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant3e276872011-06-03 18:40:47 +0000681 typedef value_type& reference;
682 typedef const value_type& const_reference;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000683 typedef typename __alloc_traits::pointer pointer;
684 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000685
Marshall Clow79f33542018-03-21 00:36:05 +0000686 static_assert((!is_array<value_type>::value), "Character type of basic_string must not be an array");
687 static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string must be standard-layout");
688 static_assert(( is_trivial<value_type>::value), "Character type of basic_string must be trivial");
689 static_assert(( is_same<_CharT, typename traits_type::char_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000690 "traits_type::char_type must be the same type as CharT");
Marshall Clow79f33542018-03-21 00:36:05 +0000691 static_assert(( is_same<typename allocator_type::value_type, value_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000692 "Allocator::value_type must be same type as value_type");
Marshall Clowe46031a2018-07-02 18:41:15 +0000693
Howard Hinnantc51e1022010-05-11 19:42:16 +0000694 typedef __wrap_iter<pointer> iterator;
695 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000696 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
697 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000698
699private:
Howard Hinnant68bf1812013-04-30 21:44:48 +0000700
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000701#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000702
703 struct __long
704 {
705 pointer __data_;
706 size_type __size_;
707 size_type __cap_;
708 };
709
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000710#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000711 static const size_type __short_mask = 0x01;
712 static const size_type __long_mask = 0x1ul;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000713#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000714 static const size_type __short_mask = 0x80;
715 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant68bf1812013-04-30 21:44:48 +0000716#endif // _LIBCPP_BIG_ENDIAN
717
718 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
719 (sizeof(__long) - 1)/sizeof(value_type) : 2};
720
721 struct __short
722 {
723 value_type __data_[__min_cap];
724 struct
725 : __padding<value_type>
726 {
727 unsigned char __size_;
728 };
729 };
730
731#else
732
Howard Hinnantc51e1022010-05-11 19:42:16 +0000733 struct __long
734 {
735 size_type __cap_;
736 size_type __size_;
737 pointer __data_;
738 };
739
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000740#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000741 static const size_type __short_mask = 0x80;
742 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000743#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000744 static const size_type __short_mask = 0x01;
745 static const size_type __long_mask = 0x1ul;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000746#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +0000747
Howard Hinnantc51e1022010-05-11 19:42:16 +0000748 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
749 (sizeof(__long) - 1)/sizeof(value_type) : 2};
750
751 struct __short
752 {
753 union
754 {
755 unsigned char __size_;
Howard Hinnant49e145e2012-10-30 19:06:59 +0000756 value_type __lx;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000757 };
758 value_type __data_[__min_cap];
759 };
760
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000761#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000762
Howard Hinnant8ea98242013-08-23 17:37:05 +0000763 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000764
Howard Hinnant8ea98242013-08-23 17:37:05 +0000765 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000766
767 struct __raw
768 {
769 size_type __words[__n_words];
770 };
771
772 struct __rep
773 {
774 union
775 {
776 __long __l;
777 __short __s;
778 __raw __r;
779 };
780 };
781
782 __compressed_pair<__rep, allocator_type> __r_;
783
Howard Hinnantc51e1022010-05-11 19:42:16 +0000784public:
Eric Fiselier2ae9e062020-01-16 15:00:34 -0500785 _LIBCPP_FUNC_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000786 static const size_type npos = -1;
787
Howard Hinnant3e276872011-06-03 18:40:47 +0000788 _LIBCPP_INLINE_VISIBILITY basic_string()
789 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000790
791 _LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a)
792#if _LIBCPP_STD_VER <= 14
793 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
794#else
795 _NOEXCEPT;
796#endif
797
Howard Hinnantc51e1022010-05-11 19:42:16 +0000798 basic_string(const basic_string& __str);
799 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clowa80abc72015-06-03 19:56:43 +0000800
Eric Fiselierfc92be82017-04-19 00:28:44 +0000801#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000802 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000803 basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +0000804#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +0000805 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000806#else
807 _NOEXCEPT;
808#endif
809
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000810 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000811 basic_string(basic_string&& __str, const allocator_type& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000812#endif // _LIBCPP_CXX03_LANG
Marshall Clowe46031a2018-07-02 18:41:15 +0000813
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500814 template <class = _EnableIf<__is_allocator<_Allocator>::value, nullptr_t> >
Eric Fiseliera8567862018-07-17 05:48:48 +0000815 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5169d1c2019-12-16 19:03:23 -0500816 basic_string(const _CharT* __s) : __r_(__default_init_tag(), __default_init_tag()) {
Eric Fiseliera8567862018-07-17 05:48:48 +0000817 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
818 __init(__s, traits_type::length(__s));
819# if _LIBCPP_DEBUG_LEVEL >= 2
820 __get_db()->__insert_c(this);
821# endif
822 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000823
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500824 template <class = _EnableIf<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000825 _LIBCPP_INLINE_VISIBILITY
826 basic_string(const _CharT* __s, const _Allocator& __a);
827
Howard Hinnantcf823322010-12-17 14:46:43 +0000828 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000829 basic_string(const _CharT* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +0000830 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000831 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000832 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000833 basic_string(size_type __n, _CharT __c);
Marshall Clowe46031a2018-07-02 18:41:15 +0000834
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500835 template <class = _EnableIf<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000836 _LIBCPP_INLINE_VISIBILITY
837 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
838
Marshall Clow83445802016-04-07 18:13:41 +0000839 basic_string(const basic_string& __str, size_type __pos, size_type __n,
Eric Fiselier812882b2017-02-17 01:17:10 +0000840 const _Allocator& __a = _Allocator());
Marshall Clow83445802016-04-07 18:13:41 +0000841 _LIBCPP_INLINE_VISIBILITY
842 basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +0000843 const _Allocator& __a = _Allocator());
Marshall Clowe46031a2018-07-02 18:41:15 +0000844
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500845 template<class _Tp, class = _EnableIf<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value> >
Shoaib Meenai69c57412017-03-02 03:02:50 +0000846 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselier812882b2017-02-17 01:17:10 +0000847 basic_string(const _Tp& __t, size_type __pos, size_type __n,
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500848 const allocator_type& __a = allocator_type());
Marshall Clowe46031a2018-07-02 18:41:15 +0000849
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500850 template<class _Tp, class = _EnableIf<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
851 !__is_same_uncvref<_Tp, basic_string>::value> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000852 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
853 explicit basic_string(const _Tp& __t);
854
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500855 template<class _Tp, class = _EnableIf<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000856 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
857 explicit basic_string(const _Tp& __t, const allocator_type& __a);
858
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500859 template<class _InputIterator, class = _EnableIf<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000860 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000861 basic_string(_InputIterator __first, _InputIterator __last);
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500862 template<class _InputIterator, class = _EnableIf<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000863 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000864 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000865#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000866 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000867 basic_string(initializer_list<_CharT> __il);
Howard Hinnantcf823322010-12-17 14:46:43 +0000868 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000869 basic_string(initializer_list<_CharT> __il, const _Allocator& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000870#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000871
Eric Fiselierd9a702a2016-10-31 03:42:50 +0000872 inline ~basic_string();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000873
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000874 _LIBCPP_INLINE_VISIBILITY
875 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
876
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000877 basic_string& operator=(const basic_string& __str);
Eric Fiselier5ec13b12017-01-23 21:24:58 +0000878
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500879 template <class _Tp, class = _EnableIf<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000880 basic_string& operator=(const _Tp& __t)
881 {__self_view __sv = __t; return assign(__sv);}
882
Eric Fiselierfc92be82017-04-19 00:28:44 +0000883#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000884 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000885 basic_string& operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000886 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiselierfc92be82017-04-19 00:28:44 +0000887 _LIBCPP_INLINE_VISIBILITY
888 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000889#endif
Howard Hinnantd17880b2013-06-28 16:59:19 +0000890 _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const value_type* __s) {return assign(__s);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000891 basic_string& operator=(value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000892
Howard Hinnant8ea98242013-08-23 17:37:05 +0000893#if _LIBCPP_DEBUG_LEVEL >= 2
894 _LIBCPP_INLINE_VISIBILITY
895 iterator begin() _NOEXCEPT
896 {return iterator(this, __get_pointer());}
897 _LIBCPP_INLINE_VISIBILITY
898 const_iterator begin() const _NOEXCEPT
899 {return const_iterator(this, __get_pointer());}
900 _LIBCPP_INLINE_VISIBILITY
901 iterator end() _NOEXCEPT
902 {return iterator(this, __get_pointer() + size());}
903 _LIBCPP_INLINE_VISIBILITY
904 const_iterator end() const _NOEXCEPT
905 {return const_iterator(this, __get_pointer() + size());}
906#else
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000907 _LIBCPP_INLINE_VISIBILITY
908 iterator begin() _NOEXCEPT
909 {return iterator(__get_pointer());}
910 _LIBCPP_INLINE_VISIBILITY
911 const_iterator begin() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000912 {return const_iterator(__get_pointer());}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000913 _LIBCPP_INLINE_VISIBILITY
914 iterator end() _NOEXCEPT
915 {return iterator(__get_pointer() + size());}
916 _LIBCPP_INLINE_VISIBILITY
917 const_iterator end() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000918 {return const_iterator(__get_pointer() + size());}
Howard Hinnant8ea98242013-08-23 17:37:05 +0000919#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000920 _LIBCPP_INLINE_VISIBILITY
921 reverse_iterator rbegin() _NOEXCEPT
922 {return reverse_iterator(end());}
923 _LIBCPP_INLINE_VISIBILITY
924 const_reverse_iterator rbegin() const _NOEXCEPT
925 {return const_reverse_iterator(end());}
926 _LIBCPP_INLINE_VISIBILITY
927 reverse_iterator rend() _NOEXCEPT
928 {return reverse_iterator(begin());}
929 _LIBCPP_INLINE_VISIBILITY
930 const_reverse_iterator rend() const _NOEXCEPT
931 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000932
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000933 _LIBCPP_INLINE_VISIBILITY
934 const_iterator cbegin() const _NOEXCEPT
935 {return begin();}
936 _LIBCPP_INLINE_VISIBILITY
937 const_iterator cend() const _NOEXCEPT
938 {return end();}
939 _LIBCPP_INLINE_VISIBILITY
940 const_reverse_iterator crbegin() const _NOEXCEPT
941 {return rbegin();}
942 _LIBCPP_INLINE_VISIBILITY
943 const_reverse_iterator crend() const _NOEXCEPT
944 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000945
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000946 _LIBCPP_INLINE_VISIBILITY size_type size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000947 {return __is_long() ? __get_long_size() : __get_short_size();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000948 _LIBCPP_INLINE_VISIBILITY size_type length() const _NOEXCEPT {return size();}
949 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT;
950 _LIBCPP_INLINE_VISIBILITY size_type capacity() const _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +0000951 {return (__is_long() ? __get_long_cap()
952 : static_cast<size_type>(__min_cap)) - 1;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000953
954 void resize(size_type __n, value_type __c);
955 _LIBCPP_INLINE_VISIBILITY void resize(size_type __n) {resize(__n, value_type());}
956
Marshall Clow6ae12a82018-11-28 18:18:34 +0000957 void reserve(size_type __res_arg);
Eric Fiselier451d5582018-11-26 20:15:38 +0000958 _LIBCPP_INLINE_VISIBILITY void __resize_default_init(size_type __n);
959
Marshall Clow6ae12a82018-11-28 18:18:34 +0000960 _LIBCPP_INLINE_VISIBILITY
961 void reserve() _NOEXCEPT {reserve(0);}
Howard Hinnant186dca82010-09-23 17:31:07 +0000962 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000963 void shrink_to_fit() _NOEXCEPT {reserve();}
Howard Hinnantcf823322010-12-17 14:46:43 +0000964 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000965 void clear() _NOEXCEPT;
Marshall Clowb7db4972017-11-15 20:02:27 +0000966 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
967 bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000968
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000969 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __pos) const _NOEXCEPT;
970 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000971
972 const_reference at(size_type __n) const;
973 reference at(size_type __n);
974
975 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const basic_string& __str) {return append(__str);}
Marshall Clowe46031a2018-07-02 18:41:15 +0000976
977 template <class _Tp>
978 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500979 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +0000980 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500981 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
982 && !__is_same_uncvref<_Tp, basic_string >::value,
Marshall Clowe46031a2018-07-02 18:41:15 +0000983 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500984 >
Marshall Clowe46031a2018-07-02 18:41:15 +0000985 operator+=(const _Tp& __t) {__self_view __sv = __t; return append(__sv);}
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000986 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const value_type* __s) {return append(__s);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000987 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(value_type __c) {push_back(__c); return *this;}
Eric Fiselierfc92be82017-04-19 00:28:44 +0000988#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000989 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(initializer_list<value_type> __il) {return append(__il);}
Eric Fiselierfc92be82017-04-19 00:28:44 +0000990#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000991
Howard Hinnantcf823322010-12-17 14:46:43 +0000992 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000993 basic_string& append(const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000994
995 template <class _Tp>
996 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500997 _EnableIf<
998 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
999 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001000 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001001 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001002 append(const _Tp& __t) { __self_view __sv = __t; return append(__sv.data(), __sv.size()); }
Marshall Clow8db7fb02014-03-04 19:17:19 +00001003 basic_string& append(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clowe46031a2018-07-02 18:41:15 +00001004
Marshall Clow62953962016-10-03 23:40:48 +00001005 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001006 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001007 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001008 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001009 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1010 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001011 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001012 >
Marshall Clow82513342016-09-24 22:45:42 +00001013 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001014 basic_string& append(const value_type* __s, size_type __n);
1015 basic_string& append(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001016 basic_string& append(size_type __n, value_type __c);
Eric Fiselier451d5582018-11-26 20:15:38 +00001017
1018 _LIBCPP_INLINE_VISIBILITY
1019 void __append_default_init(size_type __n);
1020
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001021 template <class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001022 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1023 basic_string& __append_forward_unsafe(_ForwardIterator, _ForwardIterator);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001024 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001025 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001026 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001027 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001028 __is_exactly_cpp17_input_iterator<_InputIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00001029 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001030 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001031 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001032 _LIBCPP_INLINE_VISIBILITY
1033 append(_InputIterator __first, _InputIterator __last) {
1034 const basic_string __temp (__first, __last, __alloc());
1035 append(__temp.data(), __temp.size());
1036 return *this;
1037 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001038 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001039 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001040 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001041 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001042 __is_cpp17_forward_iterator<_ForwardIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00001043 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001044 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001045 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001046 _LIBCPP_INLINE_VISIBILITY
1047 append(_ForwardIterator __first, _ForwardIterator __last) {
1048 return __append_forward_unsafe(__first, __last);
1049 }
1050
Eric Fiselierfc92be82017-04-19 00:28:44 +00001051#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001052 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001053 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001054#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001055
1056 void push_back(value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001057 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001058 void pop_back();
Marshall Clow05cf6692019-03-19 03:30:07 +00001059 _LIBCPP_INLINE_VISIBILITY reference front() _NOEXCEPT;
1060 _LIBCPP_INLINE_VISIBILITY const_reference front() const _NOEXCEPT;
1061 _LIBCPP_INLINE_VISIBILITY reference back() _NOEXCEPT;
1062 _LIBCPP_INLINE_VISIBILITY const_reference back() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001063
Marshall Clowe46031a2018-07-02 18:41:15 +00001064 template <class _Tp>
1065 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001066 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001067 <
1068 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1069 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001070 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001071 assign(const _Tp & __t) { __self_view __sv = __t; return assign(__sv.data(), __sv.size()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001072 _LIBCPP_INLINE_VISIBILITY
Marshall Clow95d5e9a2016-03-09 18:08:29 +00001073 basic_string& assign(const basic_string& __str) { return *this = __str; }
Eric Fiselierfc92be82017-04-19 00:28:44 +00001074#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001075 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001076 basic_string& assign(basic_string&& __str)
Marshall Clow5aa9e942015-10-05 16:17:34 +00001077 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001078 {*this = _VSTD::move(__str); return *this;}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001079#endif
Marshall Clow8db7fb02014-03-04 19:17:19 +00001080 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow62953962016-10-03 23:40:48 +00001081 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001082 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001083 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001084 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001085 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1086 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001087 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001088 >
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001089 assign(const _Tp & __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001090 basic_string& assign(const value_type* __s, size_type __n);
1091 basic_string& assign(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001092 basic_string& assign(size_type __n, value_type __c);
1093 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001094 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001095 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001096 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001097 __is_exactly_cpp17_input_iterator<_InputIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00001098 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001099 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001100 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001101 assign(_InputIterator __first, _InputIterator __last);
1102 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001103 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001104 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001105 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001106 __is_cpp17_forward_iterator<_ForwardIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00001107 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001108 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001109 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001110 assign(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001111#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001112 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001113 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001114#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001115
Howard Hinnantcf823322010-12-17 14:46:43 +00001116 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001117 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001118
1119 template <class _Tp>
1120 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001121 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001122 <
1123 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1124 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001125 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001126 insert(size_type __pos1, const _Tp& __t)
1127 { __self_view __sv = __t; return insert(__pos1, __sv.data(), __sv.size()); }
1128
Marshall Clow82513342016-09-24 22:45:42 +00001129 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001130 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001131 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001132 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001133 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001134 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001135 >
Marshall Clow82513342016-09-24 22:45:42 +00001136 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Marshall Clow8db7fb02014-03-04 19:17:19 +00001137 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001138 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1139 basic_string& insert(size_type __pos, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001140 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1141 iterator insert(const_iterator __pos, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001142 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001143 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1144 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001145 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001146 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001147 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001148 __is_exactly_cpp17_input_iterator<_InputIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00001149 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001150 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001151 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001152 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1153 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001154 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001155 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001156 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001157 __is_cpp17_forward_iterator<_ForwardIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00001158 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001159 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001160 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001161 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001162#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001163 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001164 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1165 {return insert(__pos, __il.begin(), __il.end());}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001166#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001167
1168 basic_string& erase(size_type __pos = 0, size_type __n = npos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001169 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001170 iterator erase(const_iterator __pos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001171 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001172 iterator erase(const_iterator __first, const_iterator __last);
1173
Howard Hinnantcf823322010-12-17 14:46:43 +00001174 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001175 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001176
1177 template <class _Tp>
1178 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001179 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001180 <
1181 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1182 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001183 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001184 replace(size_type __pos1, size_type __n1, const _Tp& __t) { __self_view __sv = __t; return replace(__pos1, __n1, __sv.data(), __sv.size()); }
Marshall Clow8db7fb02014-03-04 19:17:19 +00001185 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2=npos);
Marshall Clow82513342016-09-24 22:45:42 +00001186 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001187 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001188 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001189 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001190 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001191 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001192 >
Marshall Clow82513342016-09-24 22:45:42 +00001193 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001194 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
1195 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001196 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001197 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001198 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001199
1200 template <class _Tp>
1201 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001202 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001203 <
1204 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1205 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001206 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001207 replace(const_iterator __i1, const_iterator __i2, const _Tp& __t) { __self_view __sv = __t; return replace(__i1 - begin(), __i2 - __i1, __sv); }
1208
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001209 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001210 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +00001211 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001212 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Howard Hinnantcf823322010-12-17 14:46:43 +00001213 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001214 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001215 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001216 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001217 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001218 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001219 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001220 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001221 >
Howard Hinnant990d6e82010-11-17 21:11:40 +00001222 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001223#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001224 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001225 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001226 {return replace(__i1, __i2, __il.begin(), __il.end());}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001227#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001228
Howard Hinnantd17880b2013-06-28 16:59:19 +00001229 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
Howard Hinnantcf823322010-12-17 14:46:43 +00001230 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001231 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1232
Howard Hinnantcf823322010-12-17 14:46:43 +00001233 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001234 void swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001235#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00001236 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +00001237#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001238 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001239 __is_nothrow_swappable<allocator_type>::value);
1240#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001241
Howard Hinnantcf823322010-12-17 14:46:43 +00001242 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001243 const value_type* c_str() const _NOEXCEPT {return data();}
Howard Hinnantcf823322010-12-17 14:46:43 +00001244 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001245 const value_type* data() const _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Eric Fiselier4ee2a892017-11-20 20:23:27 +00001246#if _LIBCPP_STD_VER > 14 || defined(_LIBCPP_BUILDING_LIBRARY)
Marshall Clowd3c22392016-03-08 15:44:30 +00001247 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001248 value_type* data() _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Marshall Clowd3c22392016-03-08 15:44:30 +00001249#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001250
Howard Hinnantcf823322010-12-17 14:46:43 +00001251 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001252 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001253
Howard Hinnantcf823322010-12-17 14:46:43 +00001254 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001255 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001256
1257 template <class _Tp>
1258 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001259 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001260 <
1261 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1262 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001263 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001264 find(const _Tp& __t, size_type __pos = 0) const;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001265 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001266 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001267 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001268 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001269
Howard Hinnantcf823322010-12-17 14:46:43 +00001270 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001271 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001272
1273 template <class _Tp>
1274 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001275 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001276 <
1277 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1278 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001279 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001280 rfind(const _Tp& __t, size_type __pos = npos) const;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001281 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001282 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001283 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001284 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001285
Howard Hinnantcf823322010-12-17 14:46:43 +00001286 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001287 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001288
1289 template <class _Tp>
1290 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001291 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001292 <
1293 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1294 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001295 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001296 find_first_of(const _Tp& __t, size_type __pos = 0) const;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001297 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001298 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001299 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001300 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001301 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001302
Howard Hinnantcf823322010-12-17 14:46:43 +00001303 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001304 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001305
1306 template <class _Tp>
1307 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001308 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001309 <
1310 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1311 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001312 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001313 find_last_of(const _Tp& __t, size_type __pos = npos) const;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001314 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001315 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001316 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001317 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001318 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001319
Howard Hinnantcf823322010-12-17 14:46:43 +00001320 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001321 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001322
1323 template <class _Tp>
1324 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001325 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001326 <
1327 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1328 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001329 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001330 find_first_not_of(const _Tp &__t, size_type __pos = 0) const;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001331 size_type find_first_not_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001332 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001333 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001334 _LIBCPP_INLINE_VISIBILITY
1335 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1336
1337 _LIBCPP_INLINE_VISIBILITY
1338 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001339
1340 template <class _Tp>
1341 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001342 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001343 <
1344 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1345 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001346 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001347 find_last_not_of(const _Tp& __t, size_type __pos = npos) const;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001348 size_type find_last_not_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001349 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001350 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001351 _LIBCPP_INLINE_VISIBILITY
1352 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1353
1354 _LIBCPP_INLINE_VISIBILITY
1355 int compare(const basic_string& __str) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001356
1357 template <class _Tp>
1358 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001359 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001360 <
1361 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1362 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001363 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001364 compare(const _Tp &__t) const;
1365
1366 template <class _Tp>
1367 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001368 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001369 <
1370 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1371 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001372 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001373 compare(size_type __pos1, size_type __n1, const _Tp& __t) const;
1374
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001375 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001376 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Marshall Clow8db7fb02014-03-04 19:17:19 +00001377 int compare(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2=npos) const;
Marshall Clowe46031a2018-07-02 18:41:15 +00001378
Marshall Clow82513342016-09-24 22:45:42 +00001379 template <class _Tp>
Eric Fiselier7d4aa5c2016-10-14 05:29:46 +00001380 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001381 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001382 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001383 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001384 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001385 >
Marshall Clow82513342016-09-24 22:45:42 +00001386 compare(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos) const;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001387 int compare(const value_type* __s) const _NOEXCEPT;
1388 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1389 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001390
Marshall Clow18c293b2017-12-04 20:11:38 +00001391#if _LIBCPP_STD_VER > 17
1392 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1393 bool starts_with(__self_view __sv) const _NOEXCEPT
1394 { return __self_view(data(), size()).starts_with(__sv); }
1395
1396 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1397 bool starts_with(value_type __c) const _NOEXCEPT
1398 { return !empty() && _Traits::eq(front(), __c); }
1399
1400 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1401 bool starts_with(const value_type* __s) const _NOEXCEPT
1402 { return starts_with(__self_view(__s)); }
1403
1404 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1405 bool ends_with(__self_view __sv) const _NOEXCEPT
1406 { return __self_view(data(), size()).ends_with( __sv); }
1407
1408 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1409 bool ends_with(value_type __c) const _NOEXCEPT
1410 { return !empty() && _Traits::eq(back(), __c); }
1411
1412 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1413 bool ends_with(const value_type* __s) const _NOEXCEPT
1414 { return ends_with(__self_view(__s)); }
1415#endif
1416
Howard Hinnantcf823322010-12-17 14:46:43 +00001417 _LIBCPP_INLINE_VISIBILITY bool __invariants() const;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001418
Marshall Clowe60a7182018-05-29 17:04:37 +00001419 _LIBCPP_INLINE_VISIBILITY void __clear_and_shrink() _NOEXCEPT;
Marshall Clow851b9ec2019-05-20 21:56:51 +00001420
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001421 _LIBCPP_INLINE_VISIBILITY
1422 bool __is_long() const _NOEXCEPT
1423 {return bool(__r_.first().__s.__size_ & __short_mask);}
1424
Howard Hinnant8ea98242013-08-23 17:37:05 +00001425#if _LIBCPP_DEBUG_LEVEL >= 2
1426
1427 bool __dereferenceable(const const_iterator* __i) const;
1428 bool __decrementable(const const_iterator* __i) const;
1429 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1430 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1431
1432#endif // _LIBCPP_DEBUG_LEVEL >= 2
1433
Howard Hinnantc51e1022010-05-11 19:42:16 +00001434private:
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001435 _LIBCPP_INLINE_VISIBILITY
1436 allocator_type& __alloc() _NOEXCEPT
1437 {return __r_.second();}
1438 _LIBCPP_INLINE_VISIBILITY
1439 const allocator_type& __alloc() const _NOEXCEPT
1440 {return __r_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001441
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001442#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001443
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001444 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001445 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001446# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001447 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001448# else
1449 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1450# endif
1451
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001452 _LIBCPP_INLINE_VISIBILITY
1453 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001454# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001455 {return __r_.first().__s.__size_ >> 1;}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001456# else
1457 {return __r_.first().__s.__size_;}
1458# endif
1459
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001460#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001461
1462 _LIBCPP_INLINE_VISIBILITY
1463 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001464# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001465 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1466# else
1467 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
1468# endif
1469
1470 _LIBCPP_INLINE_VISIBILITY
1471 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001472# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001473 {return __r_.first().__s.__size_;}
1474# else
1475 {return __r_.first().__s.__size_ >> 1;}
1476# endif
1477
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001478#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001479
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001480 _LIBCPP_INLINE_VISIBILITY
1481 void __set_long_size(size_type __s) _NOEXCEPT
1482 {__r_.first().__l.__size_ = __s;}
1483 _LIBCPP_INLINE_VISIBILITY
1484 size_type __get_long_size() const _NOEXCEPT
1485 {return __r_.first().__l.__size_;}
1486 _LIBCPP_INLINE_VISIBILITY
1487 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001488 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1489
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001490 _LIBCPP_INLINE_VISIBILITY
1491 void __set_long_cap(size_type __s) _NOEXCEPT
1492 {__r_.first().__l.__cap_ = __long_mask | __s;}
1493 _LIBCPP_INLINE_VISIBILITY
1494 size_type __get_long_cap() const _NOEXCEPT
Howard Hinnant28b24882011-12-01 20:21:04 +00001495 {return __r_.first().__l.__cap_ & size_type(~__long_mask);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001496
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001497 _LIBCPP_INLINE_VISIBILITY
1498 void __set_long_pointer(pointer __p) _NOEXCEPT
1499 {__r_.first().__l.__data_ = __p;}
1500 _LIBCPP_INLINE_VISIBILITY
1501 pointer __get_long_pointer() _NOEXCEPT
1502 {return __r_.first().__l.__data_;}
1503 _LIBCPP_INLINE_VISIBILITY
1504 const_pointer __get_long_pointer() const _NOEXCEPT
1505 {return __r_.first().__l.__data_;}
1506 _LIBCPP_INLINE_VISIBILITY
1507 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001508 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001509 _LIBCPP_INLINE_VISIBILITY
1510 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001511 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001512 _LIBCPP_INLINE_VISIBILITY
1513 pointer __get_pointer() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001514 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001515 _LIBCPP_INLINE_VISIBILITY
1516 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001517 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1518
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001519 _LIBCPP_INLINE_VISIBILITY
1520 void __zero() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001521 {
1522 size_type (&__a)[__n_words] = __r_.first().__r.__words;
1523 for (unsigned __i = 0; __i < __n_words; ++__i)
1524 __a[__i] = 0;
1525 }
1526
1527 template <size_type __a> static
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001528 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00001529 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +00001530 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001531 enum {__alignment = 16};
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001532 static _LIBCPP_INLINE_VISIBILITY
1533 size_type __recommend(size_type __s) _NOEXCEPT
Marshall Clow80584522018-02-07 21:30:17 +00001534 {
1535 if (__s < __min_cap) return static_cast<size_type>(__min_cap) - 1;
1536 size_type __guess = __align_it<sizeof(value_type) < __alignment ?
1537 __alignment/sizeof(value_type) : 1 > (__s+1) - 1;
1538 if (__guess == __min_cap) ++__guess;
1539 return __guess;
1540 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001541
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001542 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001543 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001544 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001545 void __init(const value_type* __s, size_type __sz);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001546 inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001547 void __init(size_type __n, value_type __c);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001548
Martijn Vels5e7c9752020-03-04 17:52:46 -05001549 // Slow path for the (inlined) copy constructor for 'long' strings.
1550 // Always externally instantiated and not inlined.
1551 // Requires that __s is zero terminated.
1552 // The main reason for this function to exist is because for unstable, we
1553 // want to allow inlining of the copy constructor. However, we don't want
1554 // to call the __init() functions as those are marked as inline which may
1555 // result in over-aggressive inlining by the compiler, where our aim is
1556 // to only inline the fast path code directly in the ctor.
1557 void __init_copy_ctor_external(const value_type* __s, size_type __sz);
1558
Howard Hinnantc51e1022010-05-11 19:42:16 +00001559 template <class _InputIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001560 inline
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001561 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001562 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001563 __is_exactly_cpp17_input_iterator<_InputIterator>::value
1564 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001565 __init(_InputIterator __first, _InputIterator __last);
1566
1567 template <class _ForwardIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001568 inline
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001569 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001570 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001571 __is_cpp17_forward_iterator<_ForwardIterator>::value
1572 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001573 __init(_ForwardIterator __first, _ForwardIterator __last);
1574
1575 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001576 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001577 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1578 size_type __n_copy, size_type __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00001579 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001580
Martijn Vels596e3de2020-02-26 15:55:49 -05001581 // __assign_no_alias is invoked for assignment operations where we
1582 // have proof that the input does not alias the current instance.
1583 // For example, operator=(basic_string) performs a 'self' check.
1584 template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04001585 basic_string& __assign_no_alias(const value_type* __s, size_type __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05001586
Howard Hinnantcf823322010-12-17 14:46:43 +00001587 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001588 void __erase_to_end(size_type __pos);
1589
Martijn Velsa81fc792020-02-26 13:25:43 -05001590 // __erase_external_with_move is invoked for erase() invocations where
1591 // `n ~= npos`, likely requiring memory moves on the string data.
1592 void __erase_external_with_move(size_type __pos, size_type __n);
1593
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001594 _LIBCPP_INLINE_VISIBILITY
1595 void __copy_assign_alloc(const basic_string& __str)
1596 {__copy_assign_alloc(__str, integral_constant<bool,
1597 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1598
1599 _LIBCPP_INLINE_VISIBILITY
1600 void __copy_assign_alloc(const basic_string& __str, true_type)
1601 {
Marshall Clowf258c202017-01-31 03:40:52 +00001602 if (__alloc() == __str.__alloc())
1603 __alloc() = __str.__alloc();
1604 else
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001605 {
Marshall Clowf258c202017-01-31 03:40:52 +00001606 if (!__str.__is_long())
1607 {
Vedant Kumar55e007e2018-03-08 21:15:26 +00001608 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001609 __alloc() = __str.__alloc();
1610 }
1611 else
1612 {
1613 allocator_type __a = __str.__alloc();
1614 pointer __p = __alloc_traits::allocate(__a, __str.__get_long_cap());
Vedant Kumar55e007e2018-03-08 21:15:26 +00001615 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001616 __alloc() = _VSTD::move(__a);
1617 __set_long_pointer(__p);
1618 __set_long_cap(__str.__get_long_cap());
1619 __set_long_size(__str.size());
1620 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001621 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001622 }
1623
1624 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001625 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001626 {}
1627
Eric Fiselierfc92be82017-04-19 00:28:44 +00001628#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +00001629 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001630 void __move_assign(basic_string& __str, false_type)
1631 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnantcf823322010-12-17 14:46:43 +00001632 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001633 void __move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001634#if _LIBCPP_STD_VER > 14
1635 _NOEXCEPT;
1636#else
Howard Hinnant3e276872011-06-03 18:40:47 +00001637 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001638#endif
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001639#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001640
1641 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001642 void
Howard Hinnantc2734962011-09-02 20:42:31 +00001643 __move_assign_alloc(basic_string& __str)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001644 _NOEXCEPT_(
1645 !__alloc_traits::propagate_on_container_move_assignment::value ||
1646 is_nothrow_move_assignable<allocator_type>::value)
1647 {__move_assign_alloc(__str, integral_constant<bool,
1648 __alloc_traits::propagate_on_container_move_assignment::value>());}
1649
1650 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00001651 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001652 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1653 {
1654 __alloc() = _VSTD::move(__c.__alloc());
1655 }
1656
1657 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001658 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001659 _NOEXCEPT
1660 {}
1661
Martijn Velsda7d94f2020-06-19 14:24:03 -04001662 basic_string& __assign_external(const value_type* __s);
1663 basic_string& __assign_external(const value_type* __s, size_type __n);
1664
1665 // Assigns the value in __s, guaranteed to be __n < __min_cap in length.
1666 inline basic_string& __assign_short(const value_type* __s, size_type __n) {
1667 pointer __p = __is_long()
1668 ? (__set_long_size(__n), __get_long_pointer())
1669 : (__set_short_size(__n), __get_short_pointer());
1670 traits_type::move(_VSTD::__to_address(__p), __s, __n);
1671 traits_type::assign(__p[__n], value_type());
1672 return *this;
1673 }
1674
Howard Hinnantcf823322010-12-17 14:46:43 +00001675 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
1676 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(size_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001677
1678 friend basic_string operator+<>(const basic_string&, const basic_string&);
1679 friend basic_string operator+<>(const value_type*, const basic_string&);
1680 friend basic_string operator+<>(value_type, const basic_string&);
1681 friend basic_string operator+<>(const basic_string&, const value_type*);
1682 friend basic_string operator+<>(const basic_string&, value_type);
1683};
1684
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001685// These declarations must appear before any functions are implicitly used
1686// so that they have the correct visibility specifier.
1687#ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
1688_LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1689_LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1690#else
1691_LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1692_LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1693#endif
1694
1695
Marshall Clowa0563332018-02-08 06:34:03 +00001696#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
1697template<class _InputIterator,
1698 class _CharT = typename iterator_traits<_InputIterator>::value_type,
1699 class _Allocator = allocator<_CharT>,
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001700 class = _EnableIf<__is_cpp17_input_iterator<_InputIterator>::value>,
1701 class = _EnableIf<__is_allocator<_Allocator>::value>
Marshall Clowa0563332018-02-08 06:34:03 +00001702 >
1703basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
1704 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
Marshall Clowe46031a2018-07-02 18:41:15 +00001705
1706template<class _CharT,
1707 class _Traits,
1708 class _Allocator = allocator<_CharT>,
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001709 class = _EnableIf<__is_allocator<_Allocator>::value>
Marshall Clowe46031a2018-07-02 18:41:15 +00001710 >
1711explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
1712 -> basic_string<_CharT, _Traits, _Allocator>;
1713
1714template<class _CharT,
1715 class _Traits,
1716 class _Allocator = allocator<_CharT>,
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001717 class = _EnableIf<__is_allocator<_Allocator>::value>,
Marshall Clowe46031a2018-07-02 18:41:15 +00001718 class _Sz = typename allocator_traits<_Allocator>::size_type
1719 >
1720basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
1721 -> basic_string<_CharT, _Traits, _Allocator>;
Marshall Clowa0563332018-02-08 06:34:03 +00001722#endif
1723
Howard Hinnantc51e1022010-05-11 19:42:16 +00001724template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001725inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001726void
1727basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
1728{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001729#if _LIBCPP_DEBUG_LEVEL >= 2
1730 __get_db()->__invalidate_all(this);
1731#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001732}
1733
1734template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001735inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001736void
Howard Hinnant28b24882011-12-01 20:21:04 +00001737basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type
Howard Hinnant8ea98242013-08-23 17:37:05 +00001738#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant28b24882011-12-01 20:21:04 +00001739 __pos
1740#endif
1741 )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001742{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001743#if _LIBCPP_DEBUG_LEVEL >= 2
1744 __c_node* __c = __get_db()->__find_c_and_lock(this);
1745 if (__c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001746 {
Howard Hinnant8ea98242013-08-23 17:37:05 +00001747 const_pointer __new_last = __get_pointer() + __pos;
1748 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001749 {
Howard Hinnant8ea98242013-08-23 17:37:05 +00001750 --__p;
1751 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1752 if (__i->base() > __new_last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001753 {
Howard Hinnant8ea98242013-08-23 17:37:05 +00001754 (*__p)->__c_ = nullptr;
1755 if (--__c->end_ != __p)
1756 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001757 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001758 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00001759 __get_db()->unlock();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001760 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00001761#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001762}
1763
1764template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001765inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001766basic_string<_CharT, _Traits, _Allocator>::basic_string()
Marshall Clowe546cbb2015-06-04 02:05:41 +00001767 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001768 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001769{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001770#if _LIBCPP_DEBUG_LEVEL >= 2
1771 __get_db()->__insert_c(this);
1772#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001773 __zero();
1774}
1775
1776template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001777inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001778basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
Eric Fiseliere012bf22015-07-18 20:40:46 +00001779#if _LIBCPP_STD_VER <= 14
1780 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1781#else
1782 _NOEXCEPT
1783#endif
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001784: __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001785{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001786#if _LIBCPP_DEBUG_LEVEL >= 2
1787 __get_db()->__insert_c(this);
1788#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001789 __zero();
1790}
1791
1792template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier815ed732016-09-16 00:00:48 +00001793void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1794 size_type __sz,
1795 size_type __reserve)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001796{
1797 if (__reserve > max_size())
1798 this->__throw_length_error();
1799 pointer __p;
1800 if (__reserve < __min_cap)
1801 {
1802 __set_short_size(__sz);
1803 __p = __get_short_pointer();
1804 }
1805 else
1806 {
1807 size_type __cap = __recommend(__reserve);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001808 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001809 __set_long_pointer(__p);
1810 __set_long_cap(__cap+1);
1811 __set_long_size(__sz);
1812 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001813 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001814 traits_type::assign(__p[__sz], value_type());
1815}
1816
1817template <class _CharT, class _Traits, class _Allocator>
1818void
Howard Hinnantd17880b2013-06-28 16:59:19 +00001819basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001820{
1821 if (__sz > max_size())
1822 this->__throw_length_error();
1823 pointer __p;
1824 if (__sz < __min_cap)
1825 {
1826 __set_short_size(__sz);
1827 __p = __get_short_pointer();
1828 }
1829 else
1830 {
1831 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001832 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001833 __set_long_pointer(__p);
1834 __set_long_cap(__cap+1);
1835 __set_long_size(__sz);
1836 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001837 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001838 traits_type::assign(__p[__sz], value_type());
1839}
1840
1841template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00001842template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00001843basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001844 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001845{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001846 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001847 __init(__s, traits_type::length(__s));
Howard Hinnant8ea98242013-08-23 17:37:05 +00001848#if _LIBCPP_DEBUG_LEVEL >= 2
1849 __get_db()->__insert_c(this);
1850#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001851}
1852
1853template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001854inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001855basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001856 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001857{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001858 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001859 __init(__s, __n);
Howard Hinnant8ea98242013-08-23 17:37:05 +00001860#if _LIBCPP_DEBUG_LEVEL >= 2
1861 __get_db()->__insert_c(this);
1862#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001863}
1864
1865template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001866inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001867basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001868 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001869{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001870 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001871 __init(__s, __n);
Howard Hinnant8ea98242013-08-23 17:37:05 +00001872#if _LIBCPP_DEBUG_LEVEL >= 2
1873 __get_db()->__insert_c(this);
1874#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001875}
1876
1877template <class _CharT, class _Traits, class _Allocator>
1878basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001879 : __r_(__default_init_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001880{
1881 if (!__str.__is_long())
1882 __r_.first().__r = __str.__r_.first().__r;
1883 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001884 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1885 __str.__get_long_size());
1886
Howard Hinnant8ea98242013-08-23 17:37:05 +00001887#if _LIBCPP_DEBUG_LEVEL >= 2
1888 __get_db()->__insert_c(this);
1889#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001890}
1891
1892template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00001893basic_string<_CharT, _Traits, _Allocator>::basic_string(
1894 const basic_string& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001895 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001896{
1897 if (!__str.__is_long())
1898 __r_.first().__r = __str.__r_.first().__r;
1899 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001900 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1901 __str.__get_long_size());
Howard Hinnant8ea98242013-08-23 17:37:05 +00001902#if _LIBCPP_DEBUG_LEVEL >= 2
1903 __get_db()->__insert_c(this);
1904#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001905}
1906
Martijn Vels5e7c9752020-03-04 17:52:46 -05001907template <class _CharT, class _Traits, class _Allocator>
1908void basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(
1909 const value_type* __s, size_type __sz) {
1910 pointer __p;
1911 if (__sz < __min_cap) {
1912 __p = __get_short_pointer();
1913 __set_short_size(__sz);
1914 } else {
1915 if (__sz > max_size())
1916 this->__throw_length_error();
1917 size_t __cap = __recommend(__sz);
1918 __p = __alloc_traits::allocate(__alloc(), __cap + 1);
1919 __set_long_pointer(__p);
1920 __set_long_cap(__cap + 1);
1921 __set_long_size(__sz);
1922 }
1923 traits_type::copy(_VSTD::__to_address(__p), __s, __sz + 1);
1924}
1925
Eric Fiselierfc92be82017-04-19 00:28:44 +00001926#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001927
1928template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001929inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001930basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +00001931#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +00001932 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowa80abc72015-06-03 19:56:43 +00001933#else
1934 _NOEXCEPT
1935#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001936 : __r_(_VSTD::move(__str.__r_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001937{
1938 __str.__zero();
Howard Hinnant8ea98242013-08-23 17:37:05 +00001939#if _LIBCPP_DEBUG_LEVEL >= 2
1940 __get_db()->__insert_c(this);
1941 if (__is_long())
1942 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001943#endif
1944}
1945
1946template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001947inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001948basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001949 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001950{
Marshall Clowd2d24692014-07-17 15:32:20 +00001951 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001952 __init(_VSTD::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd2d24692014-07-17 15:32:20 +00001953 else
1954 {
1955 __r_.first().__r = __str.__r_.first().__r;
1956 __str.__zero();
1957 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00001958#if _LIBCPP_DEBUG_LEVEL >= 2
1959 __get_db()->__insert_c(this);
1960 if (__is_long())
1961 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001962#endif
1963}
1964
Eric Fiselierfc92be82017-04-19 00:28:44 +00001965#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001966
1967template <class _CharT, class _Traits, class _Allocator>
1968void
1969basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
1970{
1971 if (__n > max_size())
1972 this->__throw_length_error();
1973 pointer __p;
1974 if (__n < __min_cap)
1975 {
1976 __set_short_size(__n);
1977 __p = __get_short_pointer();
1978 }
1979 else
1980 {
1981 size_type __cap = __recommend(__n);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001982 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001983 __set_long_pointer(__p);
1984 __set_long_cap(__cap+1);
1985 __set_long_size(__n);
1986 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001987 traits_type::assign(_VSTD::__to_address(__p), __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001988 traits_type::assign(__p[__n], value_type());
1989}
1990
1991template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001992inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001993basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001994 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001995{
1996 __init(__n, __c);
Howard Hinnant8ea98242013-08-23 17:37:05 +00001997#if _LIBCPP_DEBUG_LEVEL >= 2
1998 __get_db()->__insert_c(this);
1999#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002000}
2001
2002template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002003template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00002004basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002005 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002006{
2007 __init(__n, __c);
Howard Hinnant8ea98242013-08-23 17:37:05 +00002008#if _LIBCPP_DEBUG_LEVEL >= 2
2009 __get_db()->__insert_c(this);
2010#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002011}
2012
Howard Hinnantc51e1022010-05-11 19:42:16 +00002013template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00002014basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
2015 size_type __pos, size_type __n,
2016 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002017 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002018{
2019 size_type __str_sz = __str.size();
2020 if (__pos > __str_sz)
2021 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002022 __init(__str.data() + __pos, _VSTD::min(__n, __str_sz - __pos));
Howard Hinnant8ea98242013-08-23 17:37:05 +00002023#if _LIBCPP_DEBUG_LEVEL >= 2
2024 __get_db()->__insert_c(this);
2025#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002026}
2027
2028template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002029inline
Marshall Clow83445802016-04-07 18:13:41 +00002030basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +00002031 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002032 : __r_(__default_init_tag(), __a)
Marshall Clow83445802016-04-07 18:13:41 +00002033{
2034 size_type __str_sz = __str.size();
2035 if (__pos > __str_sz)
2036 this->__throw_out_of_range();
2037 __init(__str.data() + __pos, __str_sz - __pos);
2038#if _LIBCPP_DEBUG_LEVEL >= 2
2039 __get_db()->__insert_c(this);
2040#endif
2041}
2042
2043template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002044template <class _Tp, class>
Marshall Clow78dbe462016-11-14 18:22:19 +00002045basic_string<_CharT, _Traits, _Allocator>::basic_string(
Marshall Clowe46031a2018-07-02 18:41:15 +00002046 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002047 : __r_(__default_init_tag(), __a)
Marshall Clow78dbe462016-11-14 18:22:19 +00002048{
Marshall Clowe46031a2018-07-02 18:41:15 +00002049 __self_view __sv0 = __t;
2050 __self_view __sv = __sv0.substr(__pos, __n);
Marshall Clow78dbe462016-11-14 18:22:19 +00002051 __init(__sv.data(), __sv.size());
2052#if _LIBCPP_DEBUG_LEVEL >= 2
2053 __get_db()->__insert_c(this);
Eric Fiselier812882b2017-02-17 01:17:10 +00002054#endif
Marshall Clow78dbe462016-11-14 18:22:19 +00002055}
2056
2057template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002058template <class _Tp, class>
2059basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002060 : __r_(__default_init_tag(), __default_init_tag())
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002061{
Marshall Clowe46031a2018-07-02 18:41:15 +00002062 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002063 __init(__sv.data(), __sv.size());
2064#if _LIBCPP_DEBUG_LEVEL >= 2
2065 __get_db()->__insert_c(this);
2066#endif
2067}
2068
2069template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002070template <class _Tp, class>
2071basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002072 : __r_(__default_init_tag(), __a)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002073{
Marshall Clowe46031a2018-07-02 18:41:15 +00002074 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002075 __init(__sv.data(), __sv.size());
2076#if _LIBCPP_DEBUG_LEVEL >= 2
2077 __get_db()->__insert_c(this);
2078#endif
2079}
2080
2081template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002082template <class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002083_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002084<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002085 __is_exactly_cpp17_input_iterator<_InputIterator>::value
2086>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002087basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
2088{
2089 __zero();
2090#ifndef _LIBCPP_NO_EXCEPTIONS
2091 try
2092 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002093#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002094 for (; __first != __last; ++__first)
2095 push_back(*__first);
2096#ifndef _LIBCPP_NO_EXCEPTIONS
2097 }
2098 catch (...)
2099 {
2100 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002101 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002102 throw;
2103 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002104#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002105}
2106
2107template <class _CharT, class _Traits, class _Allocator>
2108template <class _ForwardIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002109_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002110<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002111 __is_cpp17_forward_iterator<_ForwardIterator>::value
2112>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002113basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
2114{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002115 size_type __sz = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002116 if (__sz > max_size())
2117 this->__throw_length_error();
2118 pointer __p;
2119 if (__sz < __min_cap)
2120 {
2121 __set_short_size(__sz);
2122 __p = __get_short_pointer();
2123 }
2124 else
2125 {
2126 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002127 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002128 __set_long_pointer(__p);
2129 __set_long_cap(__cap+1);
2130 __set_long_size(__sz);
2131 }
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002132 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002133 traits_type::assign(*__p, *__first);
2134 traits_type::assign(*__p, value_type());
2135}
2136
2137template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002138template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002139inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002140basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002141 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002142{
2143 __init(__first, __last);
Howard Hinnant8ea98242013-08-23 17:37:05 +00002144#if _LIBCPP_DEBUG_LEVEL >= 2
2145 __get_db()->__insert_c(this);
2146#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002147}
2148
2149template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002150template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002151inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002152basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
2153 const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002154 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002155{
2156 __init(__first, __last);
Howard Hinnant8ea98242013-08-23 17:37:05 +00002157#if _LIBCPP_DEBUG_LEVEL >= 2
2158 __get_db()->__insert_c(this);
2159#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002160}
2161
Eric Fiselierfc92be82017-04-19 00:28:44 +00002162#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002163
Howard Hinnantc51e1022010-05-11 19:42:16 +00002164template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002165inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002166basic_string<_CharT, _Traits, _Allocator>::basic_string(
2167 initializer_list<_CharT> __il)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002168 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002169{
2170 __init(__il.begin(), __il.end());
Howard Hinnant8ea98242013-08-23 17:37:05 +00002171#if _LIBCPP_DEBUG_LEVEL >= 2
2172 __get_db()->__insert_c(this);
2173#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002174}
2175
2176template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002177inline
Eric Fiselier9d355982017-04-12 23:45:53 +00002178
Eric Fiselier812882b2017-02-17 01:17:10 +00002179basic_string<_CharT, _Traits, _Allocator>::basic_string(
2180 initializer_list<_CharT> __il, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002181 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002182{
2183 __init(__il.begin(), __il.end());
Howard Hinnant8ea98242013-08-23 17:37:05 +00002184#if _LIBCPP_DEBUG_LEVEL >= 2
2185 __get_db()->__insert_c(this);
2186#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002187}
2188
Eric Fiselierfc92be82017-04-19 00:28:44 +00002189#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002190
Howard Hinnantc51e1022010-05-11 19:42:16 +00002191template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002192basic_string<_CharT, _Traits, _Allocator>::~basic_string()
2193{
Howard Hinnant8ea98242013-08-23 17:37:05 +00002194#if _LIBCPP_DEBUG_LEVEL >= 2
2195 __get_db()->__erase_c(this);
2196#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002197 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002198 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002199}
2200
2201template <class _CharT, class _Traits, class _Allocator>
2202void
2203basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
2204 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002205 size_type __n_copy, size_type __n_del, size_type __n_add, const value_type* __p_new_stuff)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002206{
2207 size_type __ms = max_size();
2208 if (__delta_cap > __ms - __old_cap - 1)
2209 this->__throw_length_error();
2210 pointer __old_p = __get_pointer();
2211 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002212 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002213 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002214 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002215 __invalidate_all_iterators();
2216 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002217 traits_type::copy(_VSTD::__to_address(__p),
2218 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002219 if (__n_add != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002220 traits_type::copy(_VSTD::__to_address(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002221 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2222 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002223 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2224 _VSTD::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002225 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002226 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002227 __set_long_pointer(__p);
2228 __set_long_cap(__cap+1);
2229 __old_sz = __n_copy + __n_add + __sec_cp_sz;
2230 __set_long_size(__old_sz);
2231 traits_type::assign(__p[__old_sz], value_type());
2232}
2233
2234template <class _CharT, class _Traits, class _Allocator>
2235void
2236basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2237 size_type __n_copy, size_type __n_del, size_type __n_add)
2238{
2239 size_type __ms = max_size();
Marshall Clow2267c5d2013-11-06 14:24:38 +00002240 if (__delta_cap > __ms - __old_cap)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002241 this->__throw_length_error();
2242 pointer __old_p = __get_pointer();
2243 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002244 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002245 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002246 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002247 __invalidate_all_iterators();
2248 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002249 traits_type::copy(_VSTD::__to_address(__p),
2250 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002251 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2252 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002253 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2254 _VSTD::__to_address(__old_p) + __n_copy + __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002255 __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002256 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002257 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002258 __set_long_pointer(__p);
2259 __set_long_cap(__cap+1);
2260}
2261
2262// assign
2263
2264template <class _CharT, class _Traits, class _Allocator>
Martijn Vels596e3de2020-02-26 15:55:49 -05002265template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04002266basic_string<_CharT, _Traits, _Allocator>&
2267basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(
Martijn Vels596e3de2020-02-26 15:55:49 -05002268 const value_type* __s, size_type __n) {
2269 size_type __cap = __is_short ? __min_cap : __get_long_cap();
2270 if (__n < __cap) {
2271 pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
2272 __is_short ? __set_short_size(__n) : __set_long_size(__n);
2273 traits_type::copy(_VSTD::__to_address(__p), __s, __n);
2274 traits_type::assign(__p[__n], value_type());
2275 __invalidate_iterators_past(__n);
2276 } else {
2277 size_type __sz = __is_short ? __get_short_size() : __get_long_size();
2278 __grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
2279 }
Martijn Velsb6a08b62020-04-10 18:36:31 -04002280 return *this;
Martijn Vels596e3de2020-02-26 15:55:49 -05002281}
2282
2283template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002284basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002285basic_string<_CharT, _Traits, _Allocator>::__assign_external(
2286 const value_type* __s, size_type __n) {
2287 size_type __cap = capacity();
2288 if (__cap >= __n) {
2289 value_type* __p = _VSTD::__to_address(__get_pointer());
2290 traits_type::move(__p, __s, __n);
2291 traits_type::assign(__p[__n], value_type());
2292 __set_size(__n);
2293 __invalidate_iterators_past(__n);
2294 } else {
2295 size_type __sz = size();
2296 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
2297 }
2298 return *this;
2299}
2300
2301template <class _CharT, class _Traits, class _Allocator>
2302basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002303basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002304{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002305 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Martijn Velsda7d94f2020-06-19 14:24:03 -04002306 return (_LIBCPP_BUILTIN_CONSTANT_P(__n) && __n < __min_cap)
2307 ? __assign_short(__s, __n)
2308 : __assign_external(__s, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002309}
2310
2311template <class _CharT, class _Traits, class _Allocator>
2312basic_string<_CharT, _Traits, _Allocator>&
2313basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2314{
2315 size_type __cap = capacity();
2316 if (__cap < __n)
2317 {
2318 size_type __sz = size();
2319 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2320 }
2321 else
2322 __invalidate_iterators_past(__n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002323 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002324 traits_type::assign(__p, __n, __c);
2325 traits_type::assign(__p[__n], value_type());
2326 __set_size(__n);
2327 return *this;
2328}
2329
2330template <class _CharT, class _Traits, class _Allocator>
2331basic_string<_CharT, _Traits, _Allocator>&
2332basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2333{
2334 pointer __p;
2335 if (__is_long())
2336 {
2337 __p = __get_long_pointer();
2338 __set_long_size(1);
2339 }
2340 else
2341 {
2342 __p = __get_short_pointer();
2343 __set_short_size(1);
2344 }
2345 traits_type::assign(*__p, __c);
2346 traits_type::assign(*++__p, value_type());
2347 __invalidate_iterators_past(1);
2348 return *this;
2349}
2350
2351template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002352basic_string<_CharT, _Traits, _Allocator>&
2353basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2354{
Martijn Vels596e3de2020-02-26 15:55:49 -05002355 if (this != &__str) {
2356 __copy_assign_alloc(__str);
2357 if (!__is_long()) {
2358 if (!__str.__is_long()) {
Eric Fiselier03bbc922020-01-15 17:27:10 -05002359 __r_.first().__r = __str.__r_.first().__r;
Martijn Vels596e3de2020-02-26 15:55:49 -05002360 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002361 return __assign_no_alias<true>(__str.data(), __str.size());
Martijn Vels596e3de2020-02-26 15:55:49 -05002362 }
2363 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002364 return __assign_no_alias<false>(__str.data(), __str.size());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002365 }
Martijn Vels596e3de2020-02-26 15:55:49 -05002366 }
2367 return *this;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002368}
2369
Eric Fiselierfc92be82017-04-19 00:28:44 +00002370#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002371
2372template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002373inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002374void
2375basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002376 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002377{
2378 if (__alloc() != __str.__alloc())
2379 assign(__str);
2380 else
2381 __move_assign(__str, true_type());
2382}
2383
2384template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002385inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002386void
2387basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002388#if _LIBCPP_STD_VER > 14
2389 _NOEXCEPT
2390#else
Howard Hinnant3e276872011-06-03 18:40:47 +00002391 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002392#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002393{
marshall2ed41622019-11-27 07:13:00 -08002394 if (__is_long()) {
2395 __alloc_traits::deallocate(__alloc(), __get_long_pointer(),
2396 __get_long_cap());
2397#if _LIBCPP_STD_VER <= 14
2398 if (!is_nothrow_move_assignable<allocator_type>::value) {
2399 __set_short_size(0);
2400 traits_type::assign(__get_short_pointer()[0], value_type());
2401 }
2402#endif
2403 }
2404 __move_assign_alloc(__str);
2405 __r_.first() = __str.__r_.first();
2406 __str.__set_short_size(0);
2407 traits_type::assign(__str.__get_short_pointer()[0], value_type());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002408}
2409
2410template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002411inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002412basic_string<_CharT, _Traits, _Allocator>&
2413basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002414 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002415{
2416 __move_assign(__str, integral_constant<bool,
2417 __alloc_traits::propagate_on_container_move_assignment::value>());
2418 return *this;
2419}
2420
2421#endif
2422
2423template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002424template<class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002425_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002426<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002427 __is_exactly_cpp17_input_iterator <_InputIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00002428 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002429 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002430>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002431basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2432{
Marshall Clow958362f2016-09-05 01:54:30 +00002433 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002434 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002435 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002436}
2437
2438template <class _CharT, class _Traits, class _Allocator>
2439template<class _ForwardIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002440_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002441<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002442 __is_cpp17_forward_iterator<_ForwardIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00002443 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002444 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002445>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002446basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2447{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002448 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002449 size_type __cap = capacity();
2450 if (__cap < __n)
2451 {
2452 size_type __sz = size();
2453 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2454 }
2455 else
2456 __invalidate_iterators_past(__n);
2457 pointer __p = __get_pointer();
2458 for (; __first != __last; ++__first, ++__p)
2459 traits_type::assign(*__p, *__first);
2460 traits_type::assign(*__p, value_type());
2461 __set_size(__n);
2462 return *this;
2463}
2464
2465template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002466basic_string<_CharT, _Traits, _Allocator>&
2467basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2468{
2469 size_type __sz = __str.size();
2470 if (__pos > __sz)
2471 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002472 return assign(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002473}
2474
2475template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002476template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002477_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00002478<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002479 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
2480 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00002481 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002482>
Marshall Clow82513342016-09-24 22:45:42 +00002483basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002484{
Marshall Clow82513342016-09-24 22:45:42 +00002485 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002486 size_type __sz = __sv.size();
2487 if (__pos > __sz)
2488 this->__throw_out_of_range();
2489 return assign(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2490}
2491
2492
2493template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002494basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002495basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s) {
2496 return __assign_external(__s, traits_type::length(__s));
2497}
2498
2499template <class _CharT, class _Traits, class _Allocator>
2500basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002501basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002502{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002503 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Martijn Velsda7d94f2020-06-19 14:24:03 -04002504 return _LIBCPP_BUILTIN_CONSTANT_P(*__s)
2505 ? (traits_type::length(__s) < __min_cap
2506 ? __assign_short(__s, traits_type::length(__s))
2507 : __assign_external(__s, traits_type::length(__s)))
2508 : __assign_external(__s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002509}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002510// append
2511
2512template <class _CharT, class _Traits, class _Allocator>
2513basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002514basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002515{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002516 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002517 size_type __cap = capacity();
2518 size_type __sz = size();
2519 if (__cap - __sz >= __n)
2520 {
2521 if (__n)
2522 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002523 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002524 traits_type::copy(__p + __sz, __s, __n);
2525 __sz += __n;
2526 __set_size(__sz);
2527 traits_type::assign(__p[__sz], value_type());
2528 }
2529 }
2530 else
2531 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2532 return *this;
2533}
2534
2535template <class _CharT, class _Traits, class _Allocator>
2536basic_string<_CharT, _Traits, _Allocator>&
2537basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2538{
2539 if (__n)
2540 {
2541 size_type __cap = capacity();
2542 size_type __sz = size();
2543 if (__cap - __sz < __n)
2544 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2545 pointer __p = __get_pointer();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002546 traits_type::assign(_VSTD::__to_address(__p) + __sz, __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002547 __sz += __n;
2548 __set_size(__sz);
2549 traits_type::assign(__p[__sz], value_type());
2550 }
2551 return *this;
2552}
2553
2554template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00002555inline void
2556basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n)
2557{
2558 if (__n)
2559 {
2560 size_type __cap = capacity();
2561 size_type __sz = size();
2562 if (__cap - __sz < __n)
2563 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2564 pointer __p = __get_pointer();
2565 __sz += __n;
2566 __set_size(__sz);
2567 traits_type::assign(__p[__sz], value_type());
2568 }
2569}
2570
2571template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002572void
2573basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2574{
Howard Hinnant68bf1812013-04-30 21:44:48 +00002575 bool __is_short = !__is_long();
2576 size_type __cap;
2577 size_type __sz;
2578 if (__is_short)
2579 {
2580 __cap = __min_cap - 1;
2581 __sz = __get_short_size();
2582 }
2583 else
2584 {
2585 __cap = __get_long_cap() - 1;
2586 __sz = __get_long_size();
2587 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002588 if (__sz == __cap)
Howard Hinnant68bf1812013-04-30 21:44:48 +00002589 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002590 __grow_by(__cap, 1, __sz, __sz, 0);
Howard Hinnant68bf1812013-04-30 21:44:48 +00002591 __is_short = !__is_long();
2592 }
2593 pointer __p;
2594 if (__is_short)
2595 {
2596 __p = __get_short_pointer() + __sz;
2597 __set_short_size(__sz+1);
2598 }
2599 else
2600 {
2601 __p = __get_long_pointer() + __sz;
2602 __set_long_size(__sz+1);
2603 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002604 traits_type::assign(*__p, __c);
2605 traits_type::assign(*++__p, value_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002606}
2607
Marshall Clow6ebbf662016-09-07 03:32:06 +00002608template <class _Tp>
Marshall Clow958362f2016-09-05 01:54:30 +00002609bool __ptr_in_range (const _Tp* __p, const _Tp* __first, const _Tp* __last)
2610{
2611 return __first <= __p && __p < __last;
2612}
2613
Marshall Clow6ebbf662016-09-07 03:32:06 +00002614template <class _Tp1, class _Tp2>
Eric Fiselier6003c772016-12-23 23:37:52 +00002615bool __ptr_in_range (const _Tp1*, const _Tp2*, const _Tp2*)
Marshall Clow6ebbf662016-09-07 03:32:06 +00002616{
2617 return false;
2618}
2619
Howard Hinnantc51e1022010-05-11 19:42:16 +00002620template <class _CharT, class _Traits, class _Allocator>
2621template<class _ForwardIterator>
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002622basic_string<_CharT, _Traits, _Allocator>&
2623basic_string<_CharT, _Traits, _Allocator>::__append_forward_unsafe(
2624 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002625{
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002626 static_assert(__is_cpp17_forward_iterator<_ForwardIterator>::value,
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002627 "function requires a ForwardIterator");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002628 size_type __sz = size();
2629 size_type __cap = capacity();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002630 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002631 if (__n)
2632 {
Eric Fiselier96866b52017-04-15 06:49:02 +00002633 typedef typename iterator_traits<_ForwardIterator>::reference _CharRef;
2634 _CharRef __tmp_ref = *__first;
2635 if (__ptr_in_range(_VSTD::addressof(__tmp_ref), data(), data() + size()))
Marshall Clow958362f2016-09-05 01:54:30 +00002636 {
2637 const basic_string __temp (__first, __last, __alloc());
2638 append(__temp.data(), __temp.size());
2639 }
Louis Dionne173f29e2019-05-29 16:01:36 +00002640 else
Marshall Clow958362f2016-09-05 01:54:30 +00002641 {
2642 if (__cap - __sz < __n)
2643 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2644 pointer __p = __get_pointer() + __sz;
2645 for (; __first != __last; ++__p, ++__first)
2646 traits_type::assign(*__p, *__first);
2647 traits_type::assign(*__p, value_type());
2648 __set_size(__sz + __n);
2649 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002650 }
2651 return *this;
2652}
2653
2654template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002655inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002656basic_string<_CharT, _Traits, _Allocator>&
2657basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2658{
2659 return append(__str.data(), __str.size());
2660}
2661
2662template <class _CharT, class _Traits, class _Allocator>
2663basic_string<_CharT, _Traits, _Allocator>&
2664basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2665{
2666 size_type __sz = __str.size();
2667 if (__pos > __sz)
2668 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002669 return append(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002670}
2671
2672template <class _CharT, class _Traits, class _Allocator>
Marshall Clow62953962016-10-03 23:40:48 +00002673template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002674 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00002675 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002676 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clow82513342016-09-24 22:45:42 +00002677 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002678 >
Marshall Clow82513342016-09-24 22:45:42 +00002679basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002680{
Marshall Clow82513342016-09-24 22:45:42 +00002681 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002682 size_type __sz = __sv.size();
2683 if (__pos > __sz)
2684 this->__throw_out_of_range();
2685 return append(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2686}
2687
2688template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002689basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002690basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002691{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002692 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002693 return append(__s, traits_type::length(__s));
2694}
2695
2696// insert
2697
2698template <class _CharT, class _Traits, class _Allocator>
2699basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002700basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002701{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002702 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002703 size_type __sz = size();
2704 if (__pos > __sz)
2705 this->__throw_out_of_range();
2706 size_type __cap = capacity();
2707 if (__cap - __sz >= __n)
2708 {
2709 if (__n)
2710 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002711 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002712 size_type __n_move = __sz - __pos;
2713 if (__n_move != 0)
2714 {
2715 if (__p + __pos <= __s && __s < __p + __sz)
2716 __s += __n;
2717 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2718 }
2719 traits_type::move(__p + __pos, __s, __n);
2720 __sz += __n;
2721 __set_size(__sz);
2722 traits_type::assign(__p[__sz], value_type());
2723 }
2724 }
2725 else
2726 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2727 return *this;
2728}
2729
2730template <class _CharT, class _Traits, class _Allocator>
2731basic_string<_CharT, _Traits, _Allocator>&
2732basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2733{
2734 size_type __sz = size();
2735 if (__pos > __sz)
2736 this->__throw_out_of_range();
2737 if (__n)
2738 {
2739 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002740 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002741 if (__cap - __sz >= __n)
2742 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002743 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002744 size_type __n_move = __sz - __pos;
2745 if (__n_move != 0)
2746 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2747 }
2748 else
2749 {
2750 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002751 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002752 }
2753 traits_type::assign(__p + __pos, __n, __c);
2754 __sz += __n;
2755 __set_size(__sz);
2756 traits_type::assign(__p[__sz], value_type());
2757 }
2758 return *this;
2759}
2760
2761template <class _CharT, class _Traits, class _Allocator>
2762template<class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002763_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002764<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002765 __is_exactly_cpp17_input_iterator<_InputIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00002766 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
2767 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002768>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002769basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2770{
Howard Hinnant8ea98242013-08-23 17:37:05 +00002771#if _LIBCPP_DEBUG_LEVEL >= 2
2772 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2773 "string::insert(iterator, range) called with an iterator not"
2774 " referring to this string");
2775#endif
Marshall Clow958362f2016-09-05 01:54:30 +00002776 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002777 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002778}
2779
2780template <class _CharT, class _Traits, class _Allocator>
2781template<class _ForwardIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002782_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002783<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002784 __is_cpp17_forward_iterator<_ForwardIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00002785 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002786 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002787>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002788basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2789{
Howard Hinnant8ea98242013-08-23 17:37:05 +00002790#if _LIBCPP_DEBUG_LEVEL >= 2
2791 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2792 "string::insert(iterator, range) called with an iterator not"
2793 " referring to this string");
2794#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002795 size_type __ip = static_cast<size_type>(__pos - begin());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002796 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002797 if (__n)
2798 {
Eric Fiselier96866b52017-04-15 06:49:02 +00002799 typedef typename iterator_traits<_ForwardIterator>::reference _CharRef;
2800 _CharRef __tmp_char = *__first;
2801 if (__ptr_in_range(_VSTD::addressof(__tmp_char), data(), data() + size()))
Marshall Clow958362f2016-09-05 01:54:30 +00002802 {
2803 const basic_string __temp(__first, __last, __alloc());
2804 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
2805 }
2806
2807 size_type __sz = size();
2808 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002809 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002810 if (__cap - __sz >= __n)
2811 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002812 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002813 size_type __n_move = __sz - __ip;
2814 if (__n_move != 0)
2815 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
2816 }
2817 else
2818 {
2819 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002820 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002821 }
2822 __sz += __n;
2823 __set_size(__sz);
2824 traits_type::assign(__p[__sz], value_type());
2825 for (__p += __ip; __first != __last; ++__p, ++__first)
2826 traits_type::assign(*__p, *__first);
2827 }
2828 return begin() + __ip;
2829}
2830
2831template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002832inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002833basic_string<_CharT, _Traits, _Allocator>&
2834basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2835{
2836 return insert(__pos1, __str.data(), __str.size());
2837}
2838
2839template <class _CharT, class _Traits, class _Allocator>
2840basic_string<_CharT, _Traits, _Allocator>&
2841basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2842 size_type __pos2, size_type __n)
2843{
2844 size_type __str_sz = __str.size();
2845 if (__pos2 > __str_sz)
2846 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002847 return insert(__pos1, __str.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002848}
2849
2850template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002851template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002852_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00002853<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002854 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00002855 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002856>
Marshall Clow82513342016-09-24 22:45:42 +00002857basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002858 size_type __pos2, size_type __n)
2859{
Marshall Clow82513342016-09-24 22:45:42 +00002860 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002861 size_type __str_sz = __sv.size();
2862 if (__pos2 > __str_sz)
2863 this->__throw_out_of_range();
2864 return insert(__pos1, __sv.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
2865}
2866
2867template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002868basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002869basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002870{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002871 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002872 return insert(__pos, __s, traits_type::length(__s));
2873}
2874
2875template <class _CharT, class _Traits, class _Allocator>
2876typename basic_string<_CharT, _Traits, _Allocator>::iterator
2877basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2878{
2879 size_type __ip = static_cast<size_type>(__pos - begin());
2880 size_type __sz = size();
2881 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002882 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002883 if (__cap == __sz)
2884 {
2885 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002886 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002887 }
2888 else
2889 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002890 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002891 size_type __n_move = __sz - __ip;
2892 if (__n_move != 0)
2893 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2894 }
2895 traits_type::assign(__p[__ip], __c);
2896 traits_type::assign(__p[++__sz], value_type());
2897 __set_size(__sz);
2898 return begin() + static_cast<difference_type>(__ip);
2899}
2900
2901template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002902inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002903typename basic_string<_CharT, _Traits, _Allocator>::iterator
2904basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2905{
Howard Hinnant8ea98242013-08-23 17:37:05 +00002906#if _LIBCPP_DEBUG_LEVEL >= 2
2907 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2908 "string::insert(iterator, n, value) called with an iterator not"
2909 " referring to this string");
2910#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002911 difference_type __p = __pos - begin();
2912 insert(static_cast<size_type>(__p), __n, __c);
2913 return begin() + __p;
2914}
2915
2916// replace
2917
2918template <class _CharT, class _Traits, class _Allocator>
2919basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002920basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2)
Eric Fiseliere5b21842017-03-09 01:54:13 +00002921 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00002922{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002923 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002924 size_type __sz = size();
2925 if (__pos > __sz)
2926 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002927 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002928 size_type __cap = capacity();
2929 if (__cap - __sz + __n1 >= __n2)
2930 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002931 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002932 if (__n1 != __n2)
2933 {
2934 size_type __n_move = __sz - __pos - __n1;
2935 if (__n_move != 0)
2936 {
2937 if (__n1 > __n2)
2938 {
2939 traits_type::move(__p + __pos, __s, __n2);
2940 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2941 goto __finish;
2942 }
2943 if (__p + __pos < __s && __s < __p + __sz)
2944 {
2945 if (__p + __pos + __n1 <= __s)
2946 __s += __n2 - __n1;
2947 else // __p + __pos < __s < __p + __pos + __n1
2948 {
2949 traits_type::move(__p + __pos, __s, __n1);
2950 __pos += __n1;
2951 __s += __n2;
2952 __n2 -= __n1;
2953 __n1 = 0;
2954 }
2955 }
2956 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2957 }
2958 }
2959 traits_type::move(__p + __pos, __s, __n2);
2960__finish:
Martijn Vels1c48afb2020-01-28 13:43:19 -05002961// __sz += __n2 - __n1; in this and the below function below can cause unsigned
2962// integer overflow, but this is a safe operation, so we disable the check.
Howard Hinnantc51e1022010-05-11 19:42:16 +00002963 __sz += __n2 - __n1;
2964 __set_size(__sz);
2965 __invalidate_iterators_past(__sz);
2966 traits_type::assign(__p[__sz], value_type());
2967 }
2968 else
2969 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
2970 return *this;
2971}
2972
2973template <class _CharT, class _Traits, class _Allocator>
2974basic_string<_CharT, _Traits, _Allocator>&
2975basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
Eric Fiseliere5b21842017-03-09 01:54:13 +00002976 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00002977{
2978 size_type __sz = size();
2979 if (__pos > __sz)
2980 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002981 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002982 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002983 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002984 if (__cap - __sz + __n1 >= __n2)
2985 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002986 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002987 if (__n1 != __n2)
2988 {
2989 size_type __n_move = __sz - __pos - __n1;
2990 if (__n_move != 0)
2991 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2992 }
2993 }
2994 else
2995 {
2996 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002997 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002998 }
2999 traits_type::assign(__p + __pos, __n2, __c);
3000 __sz += __n2 - __n1;
3001 __set_size(__sz);
3002 __invalidate_iterators_past(__sz);
3003 traits_type::assign(__p[__sz], value_type());
3004 return *this;
3005}
3006
3007template <class _CharT, class _Traits, class _Allocator>
3008template<class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003009_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00003010<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003011 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003012 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003013>
Howard Hinnant990d6e82010-11-17 21:11:40 +00003014basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003015 _InputIterator __j1, _InputIterator __j2)
3016{
Marshall Clow958362f2016-09-05 01:54:30 +00003017 const basic_string __temp(__j1, __j2, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00003018 return this->replace(__i1, __i2, __temp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003019}
3020
3021template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003022inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003023basic_string<_CharT, _Traits, _Allocator>&
3024basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
3025{
3026 return replace(__pos1, __n1, __str.data(), __str.size());
3027}
3028
3029template <class _CharT, class _Traits, class _Allocator>
3030basic_string<_CharT, _Traits, _Allocator>&
3031basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
3032 size_type __pos2, size_type __n2)
3033{
3034 size_type __str_sz = __str.size();
3035 if (__pos2 > __str_sz)
3036 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003037 return replace(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003038}
3039
3040template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003041template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003042_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00003043<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003044 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00003045 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003046>
Marshall Clow82513342016-09-24 22:45:42 +00003047basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003048 size_type __pos2, size_type __n2)
3049{
Marshall Clow82513342016-09-24 22:45:42 +00003050 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003051 size_type __str_sz = __sv.size();
3052 if (__pos2 > __str_sz)
3053 this->__throw_out_of_range();
3054 return replace(__pos1, __n1, __sv.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
3055}
3056
3057template <class _CharT, class _Traits, class _Allocator>
3058basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003059basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003060{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003061 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003062 return replace(__pos, __n1, __s, traits_type::length(__s));
3063}
3064
3065template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003066inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003067basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003068basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003069{
3070 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
3071 __str.data(), __str.size());
3072}
3073
3074template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003075inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003076basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003077basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003078{
3079 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
3080}
3081
3082template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003083inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003084basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003085basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003086{
3087 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
3088}
3089
3090template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003091inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003092basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003093basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003094{
3095 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
3096}
3097
3098// erase
3099
Martijn Velsa81fc792020-02-26 13:25:43 -05003100// 'externally instantiated' erase() implementation, called when __n != npos.
3101// Does not check __pos against size()
Howard Hinnantc51e1022010-05-11 19:42:16 +00003102template <class _CharT, class _Traits, class _Allocator>
Martijn Velsa81fc792020-02-26 13:25:43 -05003103void
3104basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(
3105 size_type __pos, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003106{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003107 if (__n)
3108 {
Martijn Velsa81fc792020-02-26 13:25:43 -05003109 size_type __sz = size();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003110 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003111 __n = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003112 size_type __n_move = __sz - __pos - __n;
3113 if (__n_move != 0)
3114 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
3115 __sz -= __n;
3116 __set_size(__sz);
3117 __invalidate_iterators_past(__sz);
3118 traits_type::assign(__p[__sz], value_type());
3119 }
Martijn Velsa81fc792020-02-26 13:25:43 -05003120}
3121
3122template <class _CharT, class _Traits, class _Allocator>
3123basic_string<_CharT, _Traits, _Allocator>&
3124basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos,
3125 size_type __n) {
3126 if (__pos > size()) this->__throw_out_of_range();
3127 if (__n == npos) {
3128 __erase_to_end(__pos);
3129 } else {
3130 __erase_external_with_move(__pos, __n);
3131 }
3132 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003133}
3134
3135template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003136inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003137typename basic_string<_CharT, _Traits, _Allocator>::iterator
3138basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
3139{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003140#if _LIBCPP_DEBUG_LEVEL >= 2
3141 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3142 "string::erase(iterator) called with an iterator not"
3143 " referring to this string");
3144#endif
3145 _LIBCPP_ASSERT(__pos != end(),
3146 "string::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003147 iterator __b = begin();
3148 size_type __r = static_cast<size_type>(__pos - __b);
3149 erase(__r, 1);
Howard Hinnant28b24882011-12-01 20:21:04 +00003150 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003151}
3152
3153template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003154inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003155typename basic_string<_CharT, _Traits, _Allocator>::iterator
3156basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
3157{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003158#if _LIBCPP_DEBUG_LEVEL >= 2
3159 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
3160 "string::erase(iterator, iterator) called with an iterator not"
3161 " referring to this string");
3162#endif
3163 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003164 iterator __b = begin();
3165 size_type __r = static_cast<size_type>(__first - __b);
3166 erase(__r, static_cast<size_type>(__last - __first));
Howard Hinnant28b24882011-12-01 20:21:04 +00003167 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003168}
3169
3170template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003171inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003172void
3173basic_string<_CharT, _Traits, _Allocator>::pop_back()
3174{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003175 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003176 size_type __sz;
3177 if (__is_long())
3178 {
3179 __sz = __get_long_size() - 1;
3180 __set_long_size(__sz);
3181 traits_type::assign(*(__get_long_pointer() + __sz), value_type());
3182 }
3183 else
3184 {
3185 __sz = __get_short_size() - 1;
3186 __set_short_size(__sz);
3187 traits_type::assign(*(__get_short_pointer() + __sz), value_type());
3188 }
3189 __invalidate_iterators_past(__sz);
3190}
3191
3192template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003193inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003194void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003195basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003196{
3197 __invalidate_all_iterators();
3198 if (__is_long())
3199 {
3200 traits_type::assign(*__get_long_pointer(), value_type());
3201 __set_long_size(0);
3202 }
3203 else
3204 {
3205 traits_type::assign(*__get_short_pointer(), value_type());
3206 __set_short_size(0);
3207 }
3208}
3209
3210template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003211inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003212void
3213basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
3214{
3215 if (__is_long())
3216 {
3217 traits_type::assign(*(__get_long_pointer() + __pos), value_type());
3218 __set_long_size(__pos);
3219 }
3220 else
3221 {
3222 traits_type::assign(*(__get_short_pointer() + __pos), value_type());
3223 __set_short_size(__pos);
3224 }
3225 __invalidate_iterators_past(__pos);
3226}
3227
3228template <class _CharT, class _Traits, class _Allocator>
3229void
3230basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
3231{
3232 size_type __sz = size();
3233 if (__n > __sz)
3234 append(__n - __sz, __c);
3235 else
3236 __erase_to_end(__n);
3237}
3238
3239template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00003240inline void
3241basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n)
3242{
3243 size_type __sz = size();
3244 if (__n > __sz) {
3245 __append_default_init(__n - __sz);
3246 } else
3247 __erase_to_end(__n);
3248}
3249
3250template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003251inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003252typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003253basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003254{
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003255 size_type __m = __alloc_traits::max_size(__alloc());
Eric Fiseliere9cc5922017-10-17 13:16:01 +00003256#ifdef _LIBCPP_BIG_ENDIAN
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003257 return (__m <= ~__long_mask ? __m : __m/2) - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003258#else
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003259 return __m - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003260#endif
3261}
3262
3263template <class _CharT, class _Traits, class _Allocator>
3264void
3265basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __res_arg)
3266{
3267 if (__res_arg > max_size())
3268 this->__throw_length_error();
3269 size_type __cap = capacity();
3270 size_type __sz = size();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003271 __res_arg = _VSTD::max(__res_arg, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003272 __res_arg = __recommend(__res_arg);
3273 if (__res_arg != __cap)
3274 {
3275 pointer __new_data, __p;
3276 bool __was_long, __now_long;
3277 if (__res_arg == __min_cap - 1)
3278 {
3279 __was_long = true;
3280 __now_long = false;
3281 __new_data = __get_short_pointer();
3282 __p = __get_long_pointer();
3283 }
3284 else
3285 {
3286 if (__res_arg > __cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003287 __new_data = __alloc_traits::allocate(__alloc(), __res_arg+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003288 else
3289 {
3290 #ifndef _LIBCPP_NO_EXCEPTIONS
3291 try
3292 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003293 #endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003294 __new_data = __alloc_traits::allocate(__alloc(), __res_arg+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003295 #ifndef _LIBCPP_NO_EXCEPTIONS
3296 }
3297 catch (...)
3298 {
3299 return;
3300 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003301 #else // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantd17880b2013-06-28 16:59:19 +00003302 if (__new_data == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003303 return;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003304 #endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00003305 }
3306 __now_long = true;
3307 __was_long = __is_long();
3308 __p = __get_pointer();
3309 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003310 traits_type::copy(_VSTD::__to_address(__new_data),
3311 _VSTD::__to_address(__p), size()+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003312 if (__was_long)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003313 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003314 if (__now_long)
3315 {
3316 __set_long_cap(__res_arg+1);
3317 __set_long_size(__sz);
3318 __set_long_pointer(__new_data);
3319 }
3320 else
3321 __set_short_size(__sz);
3322 __invalidate_all_iterators();
3323 }
3324}
3325
3326template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003327inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003328typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003329basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003330{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003331 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003332 return *(data() + __pos);
3333}
3334
3335template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003336inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003337typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003338basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003339{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003340 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003341 return *(__get_pointer() + __pos);
3342}
3343
3344template <class _CharT, class _Traits, class _Allocator>
3345typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3346basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3347{
3348 if (__n >= size())
3349 this->__throw_out_of_range();
3350 return (*this)[__n];
3351}
3352
3353template <class _CharT, class _Traits, class _Allocator>
3354typename basic_string<_CharT, _Traits, _Allocator>::reference
3355basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
3356{
3357 if (__n >= size())
3358 this->__throw_out_of_range();
3359 return (*this)[__n];
3360}
3361
3362template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003363inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003364typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003365basic_string<_CharT, _Traits, _Allocator>::front() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003366{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003367 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003368 return *__get_pointer();
3369}
3370
3371template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003372inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003373typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003374basic_string<_CharT, _Traits, _Allocator>::front() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003375{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003376 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003377 return *data();
3378}
3379
3380template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003381inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003382typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003383basic_string<_CharT, _Traits, _Allocator>::back() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003384{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003385 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003386 return *(__get_pointer() + size() - 1);
3387}
3388
3389template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003390inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003391typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003392basic_string<_CharT, _Traits, _Allocator>::back() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003393{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003394 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003395 return *(data() + size() - 1);
3396}
3397
3398template <class _CharT, class _Traits, class _Allocator>
3399typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003400basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003401{
3402 size_type __sz = size();
3403 if (__pos > __sz)
3404 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003405 size_type __rlen = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003406 traits_type::copy(__s, data() + __pos, __rlen);
3407 return __rlen;
3408}
3409
3410template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003411inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003412basic_string<_CharT, _Traits, _Allocator>
3413basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3414{
3415 return basic_string(*this, __pos, __n, __alloc());
3416}
3417
3418template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003419inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003420void
3421basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003422#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00003423 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00003424#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00003425 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003426 __is_nothrow_swappable<allocator_type>::value)
3427#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003428{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003429#if _LIBCPP_DEBUG_LEVEL >= 2
3430 if (!__is_long())
3431 __get_db()->__invalidate_all(this);
3432 if (!__str.__is_long())
3433 __get_db()->__invalidate_all(&__str);
3434 __get_db()->swap(this, &__str);
3435#endif
Eric Fiselier9bf691f2016-12-28 05:53:01 +00003436 _LIBCPP_ASSERT(
3437 __alloc_traits::propagate_on_container_swap::value ||
3438 __alloc_traits::is_always_equal::value ||
3439 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003440 _VSTD::swap(__r_.first(), __str.__r_.first());
Marshall Clow8982dcd2015-07-13 20:04:56 +00003441 __swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003442}
3443
3444// find
3445
3446template <class _Traits>
3447struct _LIBCPP_HIDDEN __traits_eq
3448{
3449 typedef typename _Traits::char_type char_type;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003450 _LIBCPP_INLINE_VISIBILITY
3451 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3452 {return _Traits::eq(__x, __y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003453};
3454
3455template<class _CharT, class _Traits, class _Allocator>
3456typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003457basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003458 size_type __pos,
3459 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003460{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003461 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003462 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003463 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003464}
3465
3466template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003467inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003468typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003469basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3470 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003471{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003472 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003473 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003474}
3475
3476template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003477template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003478_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003479<
3480 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3481 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003482>
Marshall Clowe46031a2018-07-02 18:41:15 +00003483basic_string<_CharT, _Traits, _Allocator>::find(const _Tp &__t,
3484 size_type __pos) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003485{
Marshall Clowe46031a2018-07-02 18:41:15 +00003486 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003487 return __str_find<value_type, size_type, traits_type, npos>
3488 (data(), size(), __sv.data(), __pos, __sv.size());
3489}
3490
3491template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003492inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003493typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003494basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003495 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003496{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003497 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003498 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003499 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003500}
3501
3502template<class _CharT, class _Traits, class _Allocator>
3503typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003504basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3505 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003506{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003507 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003508 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003509}
3510
3511// rfind
3512
3513template<class _CharT, class _Traits, class _Allocator>
3514typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003515basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003516 size_type __pos,
3517 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003518{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003519 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003520 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003521 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003522}
3523
3524template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003525inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003526typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003527basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3528 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003529{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003530 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003531 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003532}
3533
3534template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003535template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003536_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003537<
3538 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3539 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003540>
Marshall Clowe46031a2018-07-02 18:41:15 +00003541basic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t,
3542 size_type __pos) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003543{
Marshall Clowe46031a2018-07-02 18:41:15 +00003544 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003545 return __str_rfind<value_type, size_type, traits_type, npos>
3546 (data(), size(), __sv.data(), __pos, __sv.size());
3547}
3548
3549template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003550inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003551typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003552basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003553 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003554{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003555 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003556 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003557 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003558}
3559
3560template<class _CharT, class _Traits, class _Allocator>
3561typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003562basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3563 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003564{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003565 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003566 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003567}
3568
3569// find_first_of
3570
3571template<class _CharT, class _Traits, class _Allocator>
3572typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003573basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003574 size_type __pos,
3575 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003576{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003577 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003578 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003579 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003580}
3581
3582template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003583inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003584typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003585basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3586 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003587{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003588 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003589 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003590}
3591
3592template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003593template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003594_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003595<
3596 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3597 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003598>
Marshall Clowe46031a2018-07-02 18:41:15 +00003599basic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t,
3600 size_type __pos) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003601{
Marshall Clowe46031a2018-07-02 18:41:15 +00003602 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003603 return __str_find_first_of<value_type, size_type, traits_type, npos>
3604 (data(), size(), __sv.data(), __pos, __sv.size());
3605}
3606
3607template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003608inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003609typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003610basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003611 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003612{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003613 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003614 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003615 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003616}
3617
3618template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003619inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003620typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003621basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3622 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003623{
3624 return find(__c, __pos);
3625}
3626
3627// find_last_of
3628
3629template<class _CharT, class _Traits, class _Allocator>
3630typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003631basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003632 size_type __pos,
3633 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003634{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003635 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003636 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003637 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003638}
3639
3640template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003641inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003642typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003643basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3644 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003645{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003646 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003647 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003648}
3649
3650template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003651template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003652_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003653<
3654 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3655 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003656>
Marshall Clowe46031a2018-07-02 18:41:15 +00003657basic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t,
3658 size_type __pos) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003659{
Marshall Clowe46031a2018-07-02 18:41:15 +00003660 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003661 return __str_find_last_of<value_type, size_type, traits_type, npos>
3662 (data(), size(), __sv.data(), __pos, __sv.size());
3663}
3664
3665template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003666inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003667typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003668basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003669 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003670{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003671 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003672 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003673 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003674}
3675
3676template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003677inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003678typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003679basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3680 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003681{
3682 return rfind(__c, __pos);
3683}
3684
3685// find_first_not_of
3686
3687template<class _CharT, class _Traits, class _Allocator>
3688typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003689basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003690 size_type __pos,
3691 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003692{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003693 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003694 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003695 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003696}
3697
3698template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003699inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003700typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003701basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3702 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003703{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003704 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003705 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003706}
3707
3708template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003709template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003710_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003711<
3712 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3713 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003714>
Marshall Clowe46031a2018-07-02 18:41:15 +00003715basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t,
3716 size_type __pos) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003717{
Marshall Clowe46031a2018-07-02 18:41:15 +00003718 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003719 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3720 (data(), size(), __sv.data(), __pos, __sv.size());
3721}
3722
3723template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003724inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003725typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003726basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003727 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003728{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003729 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003730 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003731 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003732}
3733
3734template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003735inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003736typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003737basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3738 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003739{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003740 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003741 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003742}
3743
3744// find_last_not_of
3745
3746template<class _CharT, class _Traits, class _Allocator>
3747typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003748basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003749 size_type __pos,
3750 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003751{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003752 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003753 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003754 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003755}
3756
3757template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003758inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003759typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003760basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3761 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003762{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003763 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003764 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003765}
3766
3767template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003768template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003769_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003770<
3771 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3772 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003773>
Marshall Clowe46031a2018-07-02 18:41:15 +00003774basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t,
3775 size_type __pos) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003776{
Marshall Clowe46031a2018-07-02 18:41:15 +00003777 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003778 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3779 (data(), size(), __sv.data(), __pos, __sv.size());
3780}
3781
3782template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003783inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003784typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003785basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003786 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003787{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003788 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003789 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003790 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003791}
3792
3793template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003794inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003795typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003796basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3797 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003798{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003799 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003800 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003801}
3802
3803// compare
3804
3805template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003806template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003807_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003808<
3809 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3810 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003811>
Marshall Clowe46031a2018-07-02 18:41:15 +00003812basic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003813{
Marshall Clowe46031a2018-07-02 18:41:15 +00003814 __self_view __sv = __t;
Howard Hinnantb0485532011-07-24 21:45:06 +00003815 size_t __lhs_sz = size();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003816 size_t __rhs_sz = __sv.size();
3817 int __result = traits_type::compare(data(), __sv.data(),
Howard Hinnantb0485532011-07-24 21:45:06 +00003818 _VSTD::min(__lhs_sz, __rhs_sz));
3819 if (__result != 0)
3820 return __result;
3821 if (__lhs_sz < __rhs_sz)
3822 return -1;
3823 if (__lhs_sz > __rhs_sz)
3824 return 1;
3825 return 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003826}
3827
3828template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003829inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003830int
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003831basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003832{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003833 return compare(__self_view(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003834}
3835
3836template <class _CharT, class _Traits, class _Allocator>
3837int
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003838basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3839 size_type __n1,
Howard Hinnantd17880b2013-06-28 16:59:19 +00003840 const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003841 size_type __n2) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003842{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003843 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003844 size_type __sz = size();
3845 if (__pos1 > __sz || __n2 == npos)
3846 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003847 size_type __rlen = _VSTD::min(__n1, __sz - __pos1);
3848 int __r = traits_type::compare(data() + __pos1, __s, _VSTD::min(__rlen, __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003849 if (__r == 0)
3850 {
3851 if (__rlen < __n2)
3852 __r = -1;
3853 else if (__rlen > __n2)
3854 __r = 1;
3855 }
3856 return __r;
3857}
3858
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003859template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003860template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003861_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003862<
3863 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3864 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003865>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003866basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3867 size_type __n1,
Marshall Clowe46031a2018-07-02 18:41:15 +00003868 const _Tp& __t) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003869{
Marshall Clowe46031a2018-07-02 18:41:15 +00003870 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003871 return compare(__pos1, __n1, __sv.data(), __sv.size());
3872}
3873
3874template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003875inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003876int
3877basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3878 size_type __n1,
3879 const basic_string& __str) const
3880{
3881 return compare(__pos1, __n1, __str.data(), __str.size());
3882}
3883
3884template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003885template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003886_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00003887<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003888 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
3889 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00003890 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003891>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003892basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3893 size_type __n1,
Marshall Clow82513342016-09-24 22:45:42 +00003894 const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003895 size_type __pos2,
3896 size_type __n2) const
3897{
Marshall Clow82513342016-09-24 22:45:42 +00003898 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003899 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3900}
3901
3902template <class _CharT, class _Traits, class _Allocator>
3903int
3904basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3905 size_type __n1,
3906 const basic_string& __str,
3907 size_type __pos2,
3908 size_type __n2) const
3909{
3910 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
3911}
3912
3913template <class _CharT, class _Traits, class _Allocator>
3914int
3915basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
3916{
3917 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3918 return compare(0, npos, __s, traits_type::length(__s));
3919}
3920
3921template <class _CharT, class _Traits, class _Allocator>
3922int
3923basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3924 size_type __n1,
3925 const value_type* __s) const
3926{
3927 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3928 return compare(__pos1, __n1, __s, traits_type::length(__s));
3929}
3930
Howard Hinnantc51e1022010-05-11 19:42:16 +00003931// __invariants
3932
3933template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003934inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003935bool
3936basic_string<_CharT, _Traits, _Allocator>::__invariants() const
3937{
3938 if (size() > capacity())
3939 return false;
3940 if (capacity() < __min_cap - 1)
3941 return false;
3942 if (data() == 0)
3943 return false;
3944 if (data()[size()] != value_type(0))
3945 return false;
3946 return true;
3947}
3948
Vedant Kumar55e007e2018-03-08 21:15:26 +00003949// __clear_and_shrink
3950
3951template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003952inline
Louis Dionne173f29e2019-05-29 16:01:36 +00003953void
Marshall Clowe60a7182018-05-29 17:04:37 +00003954basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
Vedant Kumar55e007e2018-03-08 21:15:26 +00003955{
3956 clear();
3957 if(__is_long())
3958 {
3959 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
3960 __set_long_cap(0);
3961 __set_short_size(0);
3962 }
Louis Dionne173f29e2019-05-29 16:01:36 +00003963}
Vedant Kumar55e007e2018-03-08 21:15:26 +00003964
Howard Hinnantc51e1022010-05-11 19:42:16 +00003965// operator==
3966
3967template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003968inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003969bool
3970operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003971 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003972{
Howard Hinnantaaeb1132013-04-22 23:55:13 +00003973 size_t __lhs_sz = __lhs.size();
3974 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
3975 __rhs.data(),
3976 __lhs_sz) == 0;
3977}
3978
3979template<class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003980inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaaeb1132013-04-22 23:55:13 +00003981bool
3982operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
3983 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
3984{
3985 size_t __lhs_sz = __lhs.size();
3986 if (__lhs_sz != __rhs.size())
3987 return false;
3988 const char* __lp = __lhs.data();
3989 const char* __rp = __rhs.data();
3990 if (__lhs.__is_long())
3991 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
3992 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
3993 if (*__lp != *__rp)
3994 return false;
3995 return true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003996}
3997
3998template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003999inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004000bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004001operator==(const _CharT* __lhs,
4002 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004003{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004004 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4005 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
4006 size_t __lhs_len = _Traits::length(__lhs);
4007 if (__lhs_len != __rhs.size()) return false;
4008 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004009}
4010
Howard Hinnantc51e1022010-05-11 19:42:16 +00004011template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004012inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004013bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004014operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
4015 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004016{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004017 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4018 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
4019 size_t __rhs_len = _Traits::length(__rhs);
4020 if (__rhs_len != __lhs.size()) return false;
4021 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004022}
4023
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004024template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004025inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004026bool
4027operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004028 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004029{
4030 return !(__lhs == __rhs);
4031}
4032
4033template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004034inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004035bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004036operator!=(const _CharT* __lhs,
4037 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004038{
4039 return !(__lhs == __rhs);
4040}
4041
4042template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004043inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004044bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004045operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4046 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004047{
4048 return !(__lhs == __rhs);
4049}
4050
4051// operator<
4052
4053template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004054inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004055bool
4056operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004057 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004058{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004059 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004060}
4061
4062template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004063inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004064bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004065operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4066 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004067{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004068 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004069}
4070
4071template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004072inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004073bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004074operator< (const _CharT* __lhs,
4075 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004076{
4077 return __rhs.compare(__lhs) > 0;
4078}
4079
Howard Hinnantc51e1022010-05-11 19:42:16 +00004080// operator>
4081
4082template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004083inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004084bool
4085operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004086 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004087{
4088 return __rhs < __lhs;
4089}
4090
4091template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004092inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004093bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004094operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4095 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004096{
4097 return __rhs < __lhs;
4098}
4099
4100template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004101inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004102bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004103operator> (const _CharT* __lhs,
4104 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004105{
4106 return __rhs < __lhs;
4107}
4108
4109// operator<=
4110
4111template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004112inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004113bool
4114operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004115 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004116{
4117 return !(__rhs < __lhs);
4118}
4119
4120template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004121inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004122bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004123operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4124 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004125{
4126 return !(__rhs < __lhs);
4127}
4128
4129template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004130inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004131bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004132operator<=(const _CharT* __lhs,
4133 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004134{
4135 return !(__rhs < __lhs);
4136}
4137
4138// operator>=
4139
4140template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004141inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004142bool
4143operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004144 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004145{
4146 return !(__lhs < __rhs);
4147}
4148
4149template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004150inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004151bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004152operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4153 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004154{
4155 return !(__lhs < __rhs);
4156}
4157
4158template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004159inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004160bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004161operator>=(const _CharT* __lhs,
4162 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004163{
4164 return !(__lhs < __rhs);
4165}
4166
4167// operator +
4168
4169template<class _CharT, class _Traits, class _Allocator>
4170basic_string<_CharT, _Traits, _Allocator>
4171operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4172 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4173{
4174 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4175 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4176 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4177 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4178 __r.append(__rhs.data(), __rhs_sz);
4179 return __r;
4180}
4181
4182template<class _CharT, class _Traits, class _Allocator>
4183basic_string<_CharT, _Traits, _Allocator>
4184operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4185{
4186 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4187 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = _Traits::length(__lhs);
4188 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4189 __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz);
4190 __r.append(__rhs.data(), __rhs_sz);
4191 return __r;
4192}
4193
4194template<class _CharT, class _Traits, class _Allocator>
4195basic_string<_CharT, _Traits, _Allocator>
4196operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4197{
4198 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4199 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4200 __r.__init(&__lhs, 1, 1 + __rhs_sz);
4201 __r.append(__rhs.data(), __rhs_sz);
4202 return __r;
4203}
4204
4205template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004206inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004207basic_string<_CharT, _Traits, _Allocator>
4208operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
4209{
4210 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4211 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4212 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = _Traits::length(__rhs);
4213 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4214 __r.append(__rhs, __rhs_sz);
4215 return __r;
4216}
4217
4218template<class _CharT, class _Traits, class _Allocator>
4219basic_string<_CharT, _Traits, _Allocator>
4220operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
4221{
4222 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4223 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4224 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1);
4225 __r.push_back(__rhs);
4226 return __r;
4227}
4228
Eric Fiselierfc92be82017-04-19 00:28:44 +00004229#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004230
4231template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004232inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004233basic_string<_CharT, _Traits, _Allocator>
4234operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4235{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004236 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004237}
4238
4239template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004240inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004241basic_string<_CharT, _Traits, _Allocator>
4242operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4243{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004244 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004245}
4246
4247template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004248inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004249basic_string<_CharT, _Traits, _Allocator>
4250operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4251{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004252 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004253}
4254
4255template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004256inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004257basic_string<_CharT, _Traits, _Allocator>
4258operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4259{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004260 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004261}
4262
4263template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004264inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004265basic_string<_CharT, _Traits, _Allocator>
4266operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4267{
4268 __rhs.insert(__rhs.begin(), __lhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004269 return _VSTD::move(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004270}
4271
4272template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004273inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004274basic_string<_CharT, _Traits, _Allocator>
4275operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
4276{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004277 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004278}
4279
4280template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004281inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004282basic_string<_CharT, _Traits, _Allocator>
4283operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
4284{
4285 __lhs.push_back(__rhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004286 return _VSTD::move(__lhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004287}
4288
Eric Fiselierfc92be82017-04-19 00:28:44 +00004289#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004290
4291// swap
4292
4293template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004294inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004295void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004296swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +00004297 basic_string<_CharT, _Traits, _Allocator>& __rhs)
4298 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004299{
4300 __lhs.swap(__rhs);
4301}
4302
Marshall Clow8732fed2018-12-11 04:35:44 +00004303#ifndef _LIBCPP_NO_HAS_CHAR8_T
4304typedef basic_string<char8_t> u8string;
4305#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00004306
Marshall Clow8732fed2018-12-11 04:35:44 +00004307#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004308typedef basic_string<char16_t> u16string;
4309typedef basic_string<char32_t> u32string;
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004310#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
Howard Hinnantc51e1022010-05-11 19:42:16 +00004311
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004312_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = 0, int __base = 10);
4313_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = 0, int __base = 10);
4314_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = 0, int __base = 10);
4315_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = 0, int __base = 10);
4316_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = 0, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004317
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004318_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = 0);
4319_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = 0);
4320_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = 0);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004321
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004322_LIBCPP_FUNC_VIS string to_string(int __val);
4323_LIBCPP_FUNC_VIS string to_string(unsigned __val);
4324_LIBCPP_FUNC_VIS string to_string(long __val);
4325_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
4326_LIBCPP_FUNC_VIS string to_string(long long __val);
4327_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
4328_LIBCPP_FUNC_VIS string to_string(float __val);
4329_LIBCPP_FUNC_VIS string to_string(double __val);
4330_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004331
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004332_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = 0, int __base = 10);
4333_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = 0, int __base = 10);
4334_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = 0, int __base = 10);
4335_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = 0, int __base = 10);
4336_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = 0, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004337
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004338_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = 0);
4339_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = 0);
4340_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = 0);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004341
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004342_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
4343_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
4344_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
4345_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
4346_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
4347_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
4348_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
4349_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
4350_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004351
Howard Hinnantc51e1022010-05-11 19:42:16 +00004352template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierc9fdaf12020-01-15 17:02:17 -05004353_LIBCPP_FUNC_VIS
4354const typename basic_string<_CharT, _Traits, _Allocator>::size_type
4355 basic_string<_CharT, _Traits, _Allocator>::npos;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004356
Marshall Clow851b9ec2019-05-20 21:56:51 +00004357template <class _CharT, class _Allocator>
4358struct _LIBCPP_TEMPLATE_VIS
4359 hash<basic_string<_CharT, char_traits<_CharT>, _Allocator> >
4360 : public unary_function<
4361 basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004362{
4363 size_t
Marshall Clow851b9ec2019-05-20 21:56:51 +00004364 operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT
4365 { return __do_string_hash(__val.data(), __val.data() + __val.size()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004366};
4367
Howard Hinnantc51e1022010-05-11 19:42:16 +00004368
Howard Hinnantdc095972011-07-18 15:51:59 +00004369template<class _CharT, class _Traits, class _Allocator>
4370basic_ostream<_CharT, _Traits>&
4371operator<<(basic_ostream<_CharT, _Traits>& __os,
4372 const basic_string<_CharT, _Traits, _Allocator>& __str);
4373
4374template<class _CharT, class _Traits, class _Allocator>
4375basic_istream<_CharT, _Traits>&
4376operator>>(basic_istream<_CharT, _Traits>& __is,
4377 basic_string<_CharT, _Traits, _Allocator>& __str);
4378
4379template<class _CharT, class _Traits, class _Allocator>
4380basic_istream<_CharT, _Traits>&
4381getline(basic_istream<_CharT, _Traits>& __is,
4382 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4383
4384template<class _CharT, class _Traits, class _Allocator>
4385inline _LIBCPP_INLINE_VISIBILITY
4386basic_istream<_CharT, _Traits>&
4387getline(basic_istream<_CharT, _Traits>& __is,
4388 basic_string<_CharT, _Traits, _Allocator>& __str);
4389
Eric Fiselierfc92be82017-04-19 00:28:44 +00004390#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantdc095972011-07-18 15:51:59 +00004391
4392template<class _CharT, class _Traits, class _Allocator>
4393inline _LIBCPP_INLINE_VISIBILITY
4394basic_istream<_CharT, _Traits>&
4395getline(basic_istream<_CharT, _Traits>&& __is,
4396 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4397
4398template<class _CharT, class _Traits, class _Allocator>
4399inline _LIBCPP_INLINE_VISIBILITY
4400basic_istream<_CharT, _Traits>&
4401getline(basic_istream<_CharT, _Traits>&& __is,
4402 basic_string<_CharT, _Traits, _Allocator>& __str);
4403
Eric Fiselierfc92be82017-04-19 00:28:44 +00004404#endif // _LIBCPP_CXX03_LANG
Howard Hinnantdc095972011-07-18 15:51:59 +00004405
Marshall Clow29b53f22018-12-14 18:49:35 +00004406#if _LIBCPP_STD_VER > 17
Marek Kurdeja98b1412020-05-02 13:58:03 +02004407template <class _CharT, class _Traits, class _Allocator, class _Up>
Marshall Clow29b53f22018-12-14 18:49:35 +00004408inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004409 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4410 erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
4411 auto __old_size = __str.size();
4412 __str.erase(_VSTD::remove(__str.begin(), __str.end(), __v), __str.end());
4413 return __old_size - __str.size();
4414}
Marshall Clow29b53f22018-12-14 18:49:35 +00004415
Marek Kurdeja98b1412020-05-02 13:58:03 +02004416template <class _CharT, class _Traits, class _Allocator, class _Predicate>
Marshall Clow29b53f22018-12-14 18:49:35 +00004417inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004418 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4419 erase_if(basic_string<_CharT, _Traits, _Allocator>& __str,
4420 _Predicate __pred) {
4421 auto __old_size = __str.size();
4422 __str.erase(_VSTD::remove_if(__str.begin(), __str.end(), __pred),
4423 __str.end());
4424 return __old_size - __str.size();
4425}
Marshall Clow29b53f22018-12-14 18:49:35 +00004426#endif
4427
Howard Hinnant8ea98242013-08-23 17:37:05 +00004428#if _LIBCPP_DEBUG_LEVEL >= 2
4429
4430template<class _CharT, class _Traits, class _Allocator>
4431bool
4432basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4433{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004434 return this->data() <= _VSTD::__to_address(__i->base()) &&
4435 _VSTD::__to_address(__i->base()) < this->data() + this->size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004436}
4437
4438template<class _CharT, class _Traits, class _Allocator>
4439bool
4440basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4441{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004442 return this->data() < _VSTD::__to_address(__i->base()) &&
4443 _VSTD::__to_address(__i->base()) <= this->data() + this->size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004444}
4445
4446template<class _CharT, class _Traits, class _Allocator>
4447bool
4448basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4449{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004450 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Howard Hinnant8ea98242013-08-23 17:37:05 +00004451 return this->data() <= __p && __p <= this->data() + this->size();
4452}
4453
4454template<class _CharT, class _Traits, class _Allocator>
4455bool
4456basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4457{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004458 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Howard Hinnant8ea98242013-08-23 17:37:05 +00004459 return this->data() <= __p && __p < this->data() + this->size();
4460}
4461
4462#endif // _LIBCPP_DEBUG_LEVEL >= 2
4463
Louis Dionne173f29e2019-05-29 16:01:36 +00004464#if _LIBCPP_STD_VER > 11
Marshall Clowcba751f2013-07-23 17:05:24 +00004465// Literal suffixes for basic_string [basic.string.literals]
Marshall Clowac868372013-10-05 21:18:32 +00004466inline namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004467{
4468 inline namespace string_literals
4469 {
Howard Hinnant5c167562013-08-07 19:39:48 +00004470 inline _LIBCPP_INLINE_VISIBILITY
4471 basic_string<char> operator "" s( const char *__str, size_t __len )
4472 {
4473 return basic_string<char> (__str, __len);
4474 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004475
Howard Hinnant5c167562013-08-07 19:39:48 +00004476 inline _LIBCPP_INLINE_VISIBILITY
4477 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4478 {
4479 return basic_string<wchar_t> (__str, __len);
4480 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004481
Marshall Clow8732fed2018-12-11 04:35:44 +00004482#ifndef _LIBCPP_NO_HAS_CHAR8_T
4483 inline _LIBCPP_INLINE_VISIBILITY
4484 basic_string<char8_t> operator "" s(const char8_t *__str, size_t __len) _NOEXCEPT
4485 {
4486 return basic_string<char8_t> (__str, __len);
4487 }
4488#endif
4489
Howard Hinnant5c167562013-08-07 19:39:48 +00004490 inline _LIBCPP_INLINE_VISIBILITY
4491 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4492 {
4493 return basic_string<char16_t> (__str, __len);
4494 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004495
Howard Hinnant5c167562013-08-07 19:39:48 +00004496 inline _LIBCPP_INLINE_VISIBILITY
4497 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4498 {
4499 return basic_string<char32_t> (__str, __len);
4500 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004501 }
4502}
4503#endif
4504
Howard Hinnantc51e1022010-05-11 19:42:16 +00004505_LIBCPP_END_NAMESPACE_STD
4506
Eric Fiselierf4433a32017-05-31 22:07:49 +00004507_LIBCPP_POP_MACROS
4508
Howard Hinnantc51e1022010-05-11 19:42:16 +00004509#endif // _LIBCPP_STRING