blob: 1f3f444727fd774e443ca8860501e0efee491981 [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
Marek Kurdejc9848142020-11-26 10:07:16 +0100156 void reserve(size_type res_arg);
157 void reserve(); // deprecated in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000158 void shrink_to_fit();
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000159 void clear() noexcept;
160 bool empty() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000161
162 const_reference operator[](size_type pos) const;
163 reference operator[](size_type pos);
164
165 const_reference at(size_type n) const;
166 reference at(size_type n);
167
168 basic_string& operator+=(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000169 template <class T>
170 basic_string& operator+=(const T& t); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000171 basic_string& operator+=(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000172 basic_string& operator+=(value_type c);
173 basic_string& operator+=(initializer_list<value_type>);
174
175 basic_string& append(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000176 template <class T>
177 basic_string& append(const T& t); // C++17
Marshall Clow8db7fb02014-03-04 19:17:19 +0000178 basic_string& append(const basic_string& str, size_type pos, size_type n=npos); //C++14
Marshall Clow82513342016-09-24 22:45:42 +0000179 template <class T>
180 basic_string& append(const T& t, size_type pos, size_type n=npos); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000181 basic_string& append(const value_type* s, size_type n);
182 basic_string& append(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000183 basic_string& append(size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000184 template<class InputIterator>
185 basic_string& append(InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000186 basic_string& append(initializer_list<value_type>);
187
188 void push_back(value_type c);
189 void pop_back();
190 reference front();
191 const_reference front() const;
192 reference back();
193 const_reference back() const;
194
195 basic_string& assign(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000196 template <class T>
197 basic_string& assign(const T& t); // C++17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000198 basic_string& assign(basic_string&& str);
Marshall Clow8db7fb02014-03-04 19:17:19 +0000199 basic_string& assign(const basic_string& str, size_type pos, size_type n=npos); // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000200 template <class T>
201 basic_string& assign(const T& t, size_type pos, size_type n=npos); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000202 basic_string& assign(const value_type* s, size_type n);
203 basic_string& assign(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000204 basic_string& assign(size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000205 template<class InputIterator>
206 basic_string& assign(InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000207 basic_string& assign(initializer_list<value_type>);
208
209 basic_string& insert(size_type pos1, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000210 template <class T>
211 basic_string& insert(size_type pos1, const T& t);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000212 basic_string& insert(size_type pos1, const basic_string& str,
213 size_type pos2, size_type n);
Marshall Clow82513342016-09-24 22:45:42 +0000214 template <class T>
215 basic_string& insert(size_type pos1, const T& t, size_type pos2, size_type n); // C++17
Marshall Clow8db7fb02014-03-04 19:17:19 +0000216 basic_string& insert(size_type pos, const value_type* s, size_type n=npos); //C++14
Howard Hinnantd17880b2013-06-28 16:59:19 +0000217 basic_string& insert(size_type pos, const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000218 basic_string& insert(size_type pos, size_type n, value_type c);
219 iterator insert(const_iterator p, value_type c);
220 iterator insert(const_iterator p, size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000221 template<class InputIterator>
222 iterator insert(const_iterator p, InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000223 iterator insert(const_iterator p, initializer_list<value_type>);
224
225 basic_string& erase(size_type pos = 0, size_type n = npos);
226 iterator erase(const_iterator position);
227 iterator erase(const_iterator first, const_iterator last);
228
229 basic_string& replace(size_type pos1, size_type n1, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000230 template <class T>
231 basic_string& replace(size_type pos1, size_type n1, const T& t); // C++17
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000232 basic_string& replace(size_type pos1, size_type n1, const basic_string& str,
Marshall Clow8db7fb02014-03-04 19:17:19 +0000233 size_type pos2, size_type n2=npos); // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000234 template <class T>
235 basic_string& replace(size_type pos1, size_type n1, const T& t,
236 size_type pos2, size_type n); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000237 basic_string& replace(size_type pos, size_type n1, const value_type* s, size_type n2);
238 basic_string& replace(size_type pos, size_type n1, const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000239 basic_string& replace(size_type pos, size_type n1, size_type n2, value_type c);
Howard Hinnant990d6e82010-11-17 21:11:40 +0000240 basic_string& replace(const_iterator i1, const_iterator i2, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000241 template <class T>
242 basic_string& replace(const_iterator i1, const_iterator i2, const T& t); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000243 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s, size_type n);
244 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s);
Howard Hinnant990d6e82010-11-17 21:11:40 +0000245 basic_string& replace(const_iterator i1, const_iterator i2, size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000246 template<class InputIterator>
Howard Hinnant990d6e82010-11-17 21:11:40 +0000247 basic_string& replace(const_iterator i1, const_iterator i2, InputIterator j1, InputIterator j2);
248 basic_string& replace(const_iterator i1, const_iterator i2, initializer_list<value_type>);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000249
Howard Hinnantd17880b2013-06-28 16:59:19 +0000250 size_type copy(value_type* s, size_type n, size_type pos = 0) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000251 basic_string substr(size_type pos = 0, size_type n = npos) const;
252
Howard Hinnant3e276872011-06-03 18:40:47 +0000253 void swap(basic_string& str)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000254 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
255 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000256
Howard Hinnantd17880b2013-06-28 16:59:19 +0000257 const value_type* c_str() const noexcept;
258 const value_type* data() const noexcept;
Marshall Clowd3c22392016-03-08 15:44:30 +0000259 value_type* data() noexcept; // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000260
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000261 allocator_type get_allocator() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000262
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000263 size_type find(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000264 template <class T>
265 size_type find(const T& t, size_type pos = 0) const; // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000266 size_type find(const value_type* s, size_type pos, size_type n) const noexcept;
267 size_type find(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000268 size_type find(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000269
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000270 size_type rfind(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000271 template <class T>
272 size_type rfind(const T& t, size_type pos = npos) const; // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000273 size_type rfind(const value_type* s, size_type pos, size_type n) const noexcept;
274 size_type rfind(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000275 size_type rfind(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000276
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000277 size_type find_first_of(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000278 template <class T>
279 size_type find_first_of(const T& t, size_type pos = 0) const; // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000280 size_type find_first_of(const value_type* s, size_type pos, size_type n) const noexcept;
281 size_type find_first_of(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000282 size_type find_first_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000283
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000284 size_type find_last_of(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000285 template <class T>
286 size_type find_last_of(const T& t, size_type pos = npos) const noexcept; // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000287 size_type find_last_of(const value_type* s, size_type pos, size_type n) const noexcept;
288 size_type find_last_of(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000289 size_type find_last_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000290
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000291 size_type find_first_not_of(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000292 template <class T>
293 size_type find_first_not_of(const T& t, size_type pos = 0) const; // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000294 size_type find_first_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
295 size_type find_first_not_of(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000296 size_type find_first_not_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000297
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000298 size_type find_last_not_of(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000299 template <class T>
300 size_type find_last_not_of(const T& t, size_type pos = npos) const; // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000301 size_type find_last_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
302 size_type find_last_not_of(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000303 size_type find_last_not_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000304
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000305 int compare(const basic_string& str) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000306 template <class T>
307 int compare(const T& t) const noexcept; // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000308 int compare(size_type pos1, size_type n1, const basic_string& str) const;
Marshall Clowe46031a2018-07-02 18:41:15 +0000309 template <class T>
310 int compare(size_type pos1, size_type n1, const T& t) const; // C++17
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000311 int compare(size_type pos1, size_type n1, const basic_string& str,
Marshall Clow8db7fb02014-03-04 19:17:19 +0000312 size_type pos2, size_type n2=npos) const; // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000313 template <class T>
314 int compare(size_type pos1, size_type n1, const T& t,
315 size_type pos2, size_type n2=npos) const; // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000316 int compare(const value_type* s) const noexcept;
317 int compare(size_type pos1, size_type n1, const value_type* s) const;
318 int compare(size_type pos1, size_type n1, const value_type* s, size_type n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000319
Marshall Clow18c293b2017-12-04 20:11:38 +0000320 bool starts_with(basic_string_view<charT, traits> sv) const noexcept; // C++2a
321 bool starts_with(charT c) const noexcept; // C++2a
322 bool starts_with(const charT* s) const; // C++2a
323 bool ends_with(basic_string_view<charT, traits> sv) const noexcept; // C++2a
324 bool ends_with(charT c) const noexcept; // C++2a
325 bool ends_with(const charT* s) const; // C++2a
326
Howard Hinnantc51e1022010-05-11 19:42:16 +0000327 bool __invariants() const;
328};
329
Marshall Clowa0563332018-02-08 06:34:03 +0000330template<class InputIterator,
331 class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
332basic_string(InputIterator, InputIterator, Allocator = Allocator())
333 -> basic_string<typename iterator_traits<InputIterator>::value_type,
334 char_traits<typename iterator_traits<InputIterator>::value_type>,
335 Allocator>; // C++17
336
Howard Hinnantc51e1022010-05-11 19:42:16 +0000337template<class charT, class traits, class Allocator>
338basic_string<charT, traits, Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000339operator+(const basic_string<charT, traits, Allocator>& lhs,
340 const basic_string<charT, traits, Allocator>& rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000341
342template<class charT, class traits, class Allocator>
343basic_string<charT, traits, Allocator>
344operator+(const charT* lhs , const basic_string<charT,traits,Allocator>&rhs);
345
346template<class charT, class traits, class Allocator>
347basic_string<charT, traits, Allocator>
348operator+(charT lhs, const basic_string<charT,traits,Allocator>& rhs);
349
350template<class charT, class traits, class Allocator>
351basic_string<charT, traits, Allocator>
352operator+(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs);
353
354template<class charT, class traits, class Allocator>
355basic_string<charT, traits, Allocator>
356operator+(const basic_string<charT, traits, Allocator>& lhs, charT rhs);
357
358template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000359bool operator==(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000360 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000361
362template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000363bool operator==(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000364
365template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000366bool operator==(const basic_string<charT,traits,Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000367
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000368template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000369bool operator!=(const basic_string<charT,traits,Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000370 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000371
372template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000373bool operator!=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000374
375template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000376bool operator!=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000377
378template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000379bool operator< (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000380 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000381
382template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000383bool operator< (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000384
385template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000386bool operator< (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000387
388template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000389bool operator> (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000390 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000391
392template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000393bool operator> (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000394
395template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000396bool operator> (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000397
398template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000399bool operator<=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000400 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000401
402template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000403bool operator<=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000404
405template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000406bool operator<=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000407
408template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000409bool operator>=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000410 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000411
412template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000413bool operator>=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000414
415template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000416bool operator>=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000417
418template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000419void swap(basic_string<charT, traits, Allocator>& lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +0000420 basic_string<charT, traits, Allocator>& rhs)
421 noexcept(noexcept(lhs.swap(rhs)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000422
423template<class charT, class traits, class Allocator>
424basic_istream<charT, traits>&
425operator>>(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
426
427template<class charT, class traits, class Allocator>
428basic_ostream<charT, traits>&
429operator<<(basic_ostream<charT, traits>& os, const basic_string<charT, traits, Allocator>& str);
430
431template<class charT, class traits, class Allocator>
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000432basic_istream<charT, traits>&
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000433getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str,
434 charT delim);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000435
436template<class charT, class traits, class Allocator>
437basic_istream<charT, traits>&
438getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
439
Marshall Clow29b53f22018-12-14 18:49:35 +0000440template<class charT, class traits, class Allocator, class U>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200441typename basic_string<charT, traits, Allocator>::size_type
442erase(basic_string<charT, traits, Allocator>& c, const U& value); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000443template<class charT, class traits, class Allocator, class Predicate>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200444typename basic_string<charT, traits, Allocator>::size_type
445erase_if(basic_string<charT, traits, Allocator>& c, Predicate pred); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000446
Howard Hinnantc51e1022010-05-11 19:42:16 +0000447typedef basic_string<char> string;
448typedef basic_string<wchar_t> wstring;
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000449typedef basic_string<char16_t> u16string;
450typedef basic_string<char32_t> u32string;
451
Bruce Mitchener170d8972020-11-24 12:53:53 -0500452int stoi (const string& str, size_t* idx = nullptr, int base = 10);
453long stol (const string& str, size_t* idx = nullptr, int base = 10);
454unsigned long stoul (const string& str, size_t* idx = nullptr, int base = 10);
455long long stoll (const string& str, size_t* idx = nullptr, int base = 10);
456unsigned long long stoull(const string& str, size_t* idx = nullptr, int base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000457
Bruce Mitchener170d8972020-11-24 12:53:53 -0500458float stof (const string& str, size_t* idx = nullptr);
459double stod (const string& str, size_t* idx = nullptr);
460long double stold(const string& str, size_t* idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000461
462string to_string(int val);
463string to_string(unsigned val);
464string to_string(long val);
465string to_string(unsigned long val);
466string to_string(long long val);
467string to_string(unsigned long long val);
468string to_string(float val);
469string to_string(double val);
470string to_string(long double val);
471
Bruce Mitchener170d8972020-11-24 12:53:53 -0500472int stoi (const wstring& str, size_t* idx = nullptr, int base = 10);
473long stol (const wstring& str, size_t* idx = nullptr, int base = 10);
474unsigned long stoul (const wstring& str, size_t* idx = nullptr, int base = 10);
475long long stoll (const wstring& str, size_t* idx = nullptr, int base = 10);
476unsigned long long stoull(const wstring& str, size_t* idx = nullptr, int base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000477
Bruce Mitchener170d8972020-11-24 12:53:53 -0500478float stof (const wstring& str, size_t* idx = nullptr);
479double stod (const wstring& str, size_t* idx = nullptr);
480long double stold(const wstring& str, size_t* idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000481
482wstring to_wstring(int val);
483wstring to_wstring(unsigned val);
484wstring to_wstring(long val);
485wstring to_wstring(unsigned long val);
486wstring to_wstring(long long val);
487wstring to_wstring(unsigned long long val);
488wstring to_wstring(float val);
489wstring to_wstring(double val);
490wstring to_wstring(long double val);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000491
492template <> struct hash<string>;
493template <> struct hash<u16string>;
494template <> struct hash<u32string>;
495template <> struct hash<wstring>;
496
Marshall Clowcba751f2013-07-23 17:05:24 +0000497basic_string<char> operator "" s( const char *str, size_t len ); // C++14
498basic_string<wchar_t> operator "" s( const wchar_t *str, size_t len ); // C++14
499basic_string<char16_t> operator "" s( const char16_t *str, size_t len ); // C++14
500basic_string<char32_t> operator "" s( const char32_t *str, size_t len ); // C++14
501
Howard Hinnantc51e1022010-05-11 19:42:16 +0000502} // std
503
504*/
505
506#include <__config>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000507#include <string_view>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000508#include <iosfwd>
509#include <cstring>
Howard Hinnant155c2af2010-05-24 17:49:41 +0000510#include <cstdio> // For EOF.
Howard Hinnantc51e1022010-05-11 19:42:16 +0000511#include <cwchar>
512#include <algorithm>
513#include <iterator>
514#include <utility>
515#include <memory>
516#include <stdexcept>
517#include <type_traits>
518#include <initializer_list>
519#include <__functional_base>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000520#include <version>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000521#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
522#include <cstdint>
523#endif
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000524
Eric Fiselier14b6de92014-08-10 23:53:08 +0000525#include <__debug>
526
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000527#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000528#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000529#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000530
Eric Fiselierf4433a32017-05-31 22:07:49 +0000531_LIBCPP_PUSH_MACROS
532#include <__undef_macros>
533
534
Howard Hinnantc51e1022010-05-11 19:42:16 +0000535_LIBCPP_BEGIN_NAMESPACE_STD
536
537// fpos
538
539template <class _StateT>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000540class _LIBCPP_TEMPLATE_VIS fpos
Howard Hinnantc51e1022010-05-11 19:42:16 +0000541{
542private:
543 _StateT __st_;
544 streamoff __off_;
545public:
546 _LIBCPP_INLINE_VISIBILITY fpos(streamoff __off = streamoff()) : __st_(), __off_(__off) {}
547
548 _LIBCPP_INLINE_VISIBILITY operator streamoff() const {return __off_;}
549
550 _LIBCPP_INLINE_VISIBILITY _StateT state() const {return __st_;}
551 _LIBCPP_INLINE_VISIBILITY void state(_StateT __st) {__st_ = __st;}
552
553 _LIBCPP_INLINE_VISIBILITY fpos& operator+=(streamoff __off) {__off_ += __off; return *this;}
554 _LIBCPP_INLINE_VISIBILITY fpos operator+ (streamoff __off) const {fpos __t(*this); __t += __off; return __t;}
555 _LIBCPP_INLINE_VISIBILITY fpos& operator-=(streamoff __off) {__off_ -= __off; return *this;}
556 _LIBCPP_INLINE_VISIBILITY fpos operator- (streamoff __off) const {fpos __t(*this); __t -= __off; return __t;}
557};
558
559template <class _StateT>
560inline _LIBCPP_INLINE_VISIBILITY
561streamoff operator-(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
562 {return streamoff(__x) - streamoff(__y);}
563
564template <class _StateT>
565inline _LIBCPP_INLINE_VISIBILITY
566bool operator==(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
567 {return streamoff(__x) == streamoff(__y);}
568
569template <class _StateT>
570inline _LIBCPP_INLINE_VISIBILITY
571bool operator!=(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
572 {return streamoff(__x) != streamoff(__y);}
573
Howard Hinnantc51e1022010-05-11 19:42:16 +0000574// basic_string
575
576template<class _CharT, class _Traits, class _Allocator>
577basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000578operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
579 const basic_string<_CharT, _Traits, _Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000580
581template<class _CharT, class _Traits, class _Allocator>
582basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000583operator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000584
585template<class _CharT, class _Traits, class _Allocator>
586basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000587operator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000588
589template<class _CharT, class _Traits, class _Allocator>
Louis Dionne8f8d95d2018-11-21 17:31:55 +0000590inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000591basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000592operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000593
594template<class _CharT, class _Traits, class _Allocator>
595basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000596operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000597
Shoaib Meenaiea363712017-07-29 02:54:41 +0000598_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&))
599
Howard Hinnantc51e1022010-05-11 19:42:16 +0000600template <bool>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000601class _LIBCPP_TEMPLATE_VIS __basic_string_common
Howard Hinnantc51e1022010-05-11 19:42:16 +0000602{
603protected:
Marshall Clow8fea1612016-08-25 15:09:01 +0000604 _LIBCPP_NORETURN void __throw_length_error() const;
605 _LIBCPP_NORETURN void __throw_out_of_range() const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000606};
607
608template <bool __b>
609void
610__basic_string_common<__b>::__throw_length_error() const
611{
Marshall Clow8fea1612016-08-25 15:09:01 +0000612 _VSTD::__throw_length_error("basic_string");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000613}
614
615template <bool __b>
616void
617__basic_string_common<__b>::__throw_out_of_range() const
618{
Marshall Clow8fea1612016-08-25 15:09:01 +0000619 _VSTD::__throw_out_of_range("basic_string");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000620}
621
Eric Fiselier1b57fa82016-09-15 22:27:07 +0000622_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __basic_string_common<true>)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000623
Marshall Clow039b2f02016-01-13 21:54:34 +0000624#ifdef _LIBCPP_NO_EXCEPTIONS
625template <class _Iter>
626struct __libcpp_string_gets_noexcept_iterator_impl : public true_type {};
627#elif defined(_LIBCPP_HAS_NO_NOEXCEPT)
628template <class _Iter>
629struct __libcpp_string_gets_noexcept_iterator_impl : public false_type {};
630#else
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500631template <class _Iter, bool = __is_cpp17_forward_iterator<_Iter>::value>
Marshall Clow039b2f02016-01-13 21:54:34 +0000632struct __libcpp_string_gets_noexcept_iterator_impl : public _LIBCPP_BOOL_CONSTANT((
Louis Dionne173f29e2019-05-29 16:01:36 +0000633 noexcept(++(declval<_Iter&>())) &&
634 is_nothrow_assignable<_Iter&, _Iter>::value &&
635 noexcept(declval<_Iter>() == declval<_Iter>()) &&
Marshall Clow039b2f02016-01-13 21:54:34 +0000636 noexcept(*declval<_Iter>())
637)) {};
638
Louis Dionne173f29e2019-05-29 16:01:36 +0000639template <class _Iter>
Marshall Clow039b2f02016-01-13 21:54:34 +0000640struct __libcpp_string_gets_noexcept_iterator_impl<_Iter, false> : public false_type {};
641#endif
642
643
644template <class _Iter>
645struct __libcpp_string_gets_noexcept_iterator
646 : public _LIBCPP_BOOL_CONSTANT(__libcpp_is_trivial_iterator<_Iter>::value || __libcpp_string_gets_noexcept_iterator_impl<_Iter>::value) {};
647
Marshall Clow82513342016-09-24 22:45:42 +0000648template <class _CharT, class _Traits, class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500649struct __can_be_converted_to_string_view : public _BoolConstant<
650 is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
651 !is_convertible<const _Tp&, const _CharT*>::value
652 > {};
Marshall Clow82513342016-09-24 22:45:42 +0000653
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000654#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000655
656template <class _CharT, size_t = sizeof(_CharT)>
657struct __padding
658{
659 unsigned char __xx[sizeof(_CharT)-1];
660};
661
662template <class _CharT>
663struct __padding<_CharT, 1>
664{
665};
666
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000667#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000668
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000669template<class _CharT, class _Traits, class _Allocator>
Richard Smithbf0d2662020-12-08 00:42:26 -0800670class _LIBCPP_TEMPLATE_VIS basic_string
Howard Hinnantc51e1022010-05-11 19:42:16 +0000671 : private __basic_string_common<true>
672{
673public:
674 typedef basic_string __self;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000675 typedef basic_string_view<_CharT, _Traits> __self_view;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000676 typedef _Traits traits_type;
Marshall Clowa3a74e02017-03-15 18:41:11 +0000677 typedef _CharT value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000678 typedef _Allocator allocator_type;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000679 typedef allocator_traits<allocator_type> __alloc_traits;
680 typedef typename __alloc_traits::size_type size_type;
681 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant3e276872011-06-03 18:40:47 +0000682 typedef value_type& reference;
683 typedef const value_type& const_reference;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000684 typedef typename __alloc_traits::pointer pointer;
685 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000686
Marshall Clow79f33542018-03-21 00:36:05 +0000687 static_assert((!is_array<value_type>::value), "Character type of basic_string must not be an array");
688 static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string must be standard-layout");
689 static_assert(( is_trivial<value_type>::value), "Character type of basic_string must be trivial");
690 static_assert(( is_same<_CharT, typename traits_type::char_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000691 "traits_type::char_type must be the same type as CharT");
Marshall Clow79f33542018-03-21 00:36:05 +0000692 static_assert(( is_same<typename allocator_type::value_type, value_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000693 "Allocator::value_type must be same type as value_type");
Marshall Clowe46031a2018-07-02 18:41:15 +0000694
Howard Hinnantc51e1022010-05-11 19:42:16 +0000695 typedef __wrap_iter<pointer> iterator;
696 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000697 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
698 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000699
700private:
Howard Hinnant68bf1812013-04-30 21:44:48 +0000701
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000702#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000703
704 struct __long
705 {
706 pointer __data_;
707 size_type __size_;
708 size_type __cap_;
709 };
710
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000711#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000712 static const size_type __short_mask = 0x01;
713 static const size_type __long_mask = 0x1ul;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000714#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000715 static const size_type __short_mask = 0x80;
716 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant68bf1812013-04-30 21:44:48 +0000717#endif // _LIBCPP_BIG_ENDIAN
718
719 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
720 (sizeof(__long) - 1)/sizeof(value_type) : 2};
721
722 struct __short
723 {
724 value_type __data_[__min_cap];
725 struct
726 : __padding<value_type>
727 {
728 unsigned char __size_;
729 };
730 };
731
732#else
733
Howard Hinnantc51e1022010-05-11 19:42:16 +0000734 struct __long
735 {
736 size_type __cap_;
737 size_type __size_;
738 pointer __data_;
739 };
740
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000741#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000742 static const size_type __short_mask = 0x80;
743 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000744#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000745 static const size_type __short_mask = 0x01;
746 static const size_type __long_mask = 0x1ul;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000747#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +0000748
Howard Hinnantc51e1022010-05-11 19:42:16 +0000749 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
750 (sizeof(__long) - 1)/sizeof(value_type) : 2};
751
752 struct __short
753 {
754 union
755 {
756 unsigned char __size_;
Howard Hinnant49e145e2012-10-30 19:06:59 +0000757 value_type __lx;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000758 };
759 value_type __data_[__min_cap];
760 };
761
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000762#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000763
Howard Hinnant8ea98242013-08-23 17:37:05 +0000764 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000765
Howard Hinnant8ea98242013-08-23 17:37:05 +0000766 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000767
768 struct __raw
769 {
770 size_type __words[__n_words];
771 };
772
773 struct __rep
774 {
775 union
776 {
777 __long __l;
778 __short __s;
779 __raw __r;
780 };
781 };
782
783 __compressed_pair<__rep, allocator_type> __r_;
784
Howard Hinnantc51e1022010-05-11 19:42:16 +0000785public:
Eric Fiselier2ae9e062020-01-16 15:00:34 -0500786 _LIBCPP_FUNC_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000787 static const size_type npos = -1;
788
Howard Hinnant3e276872011-06-03 18:40:47 +0000789 _LIBCPP_INLINE_VISIBILITY basic_string()
790 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000791
792 _LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a)
793#if _LIBCPP_STD_VER <= 14
794 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
795#else
796 _NOEXCEPT;
797#endif
798
Howard Hinnantc51e1022010-05-11 19:42:16 +0000799 basic_string(const basic_string& __str);
800 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clowa80abc72015-06-03 19:56:43 +0000801
Eric Fiselierfc92be82017-04-19 00:28:44 +0000802#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000803 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000804 basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +0000805#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +0000806 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000807#else
808 _NOEXCEPT;
809#endif
810
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000811 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000812 basic_string(basic_string&& __str, const allocator_type& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000813#endif // _LIBCPP_CXX03_LANG
Marshall Clowe46031a2018-07-02 18:41:15 +0000814
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500815 template <class = _EnableIf<__is_allocator<_Allocator>::value, nullptr_t> >
Eric Fiseliera8567862018-07-17 05:48:48 +0000816 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5169d1c2019-12-16 19:03:23 -0500817 basic_string(const _CharT* __s) : __r_(__default_init_tag(), __default_init_tag()) {
Eric Fiseliera8567862018-07-17 05:48:48 +0000818 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
819 __init(__s, traits_type::length(__s));
Louis Dionneba400782020-10-02 15:02:52 -0400820# if _LIBCPP_DEBUG_LEVEL == 2
Eric Fiseliera8567862018-07-17 05:48:48 +0000821 __get_db()->__insert_c(this);
822# endif
823 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000824
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500825 template <class = _EnableIf<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000826 _LIBCPP_INLINE_VISIBILITY
827 basic_string(const _CharT* __s, const _Allocator& __a);
828
Howard Hinnantcf823322010-12-17 14:46:43 +0000829 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000830 basic_string(const _CharT* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +0000831 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000832 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000833 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000834 basic_string(size_type __n, _CharT __c);
Marshall Clowe46031a2018-07-02 18:41:15 +0000835
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500836 template <class = _EnableIf<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000837 _LIBCPP_INLINE_VISIBILITY
838 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
839
Marshall Clow83445802016-04-07 18:13:41 +0000840 basic_string(const basic_string& __str, size_type __pos, size_type __n,
Eric Fiselier812882b2017-02-17 01:17:10 +0000841 const _Allocator& __a = _Allocator());
Marshall Clow83445802016-04-07 18:13:41 +0000842 _LIBCPP_INLINE_VISIBILITY
843 basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +0000844 const _Allocator& __a = _Allocator());
Marshall Clowe46031a2018-07-02 18:41:15 +0000845
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500846 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 +0000847 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselier812882b2017-02-17 01:17:10 +0000848 basic_string(const _Tp& __t, size_type __pos, size_type __n,
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500849 const allocator_type& __a = allocator_type());
Marshall Clowe46031a2018-07-02 18:41:15 +0000850
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500851 template<class _Tp, class = _EnableIf<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
852 !__is_same_uncvref<_Tp, basic_string>::value> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000853 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
854 explicit basic_string(const _Tp& __t);
855
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500856 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 +0000857 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
858 explicit basic_string(const _Tp& __t, const allocator_type& __a);
859
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500860 template<class _InputIterator, class = _EnableIf<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000861 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000862 basic_string(_InputIterator __first, _InputIterator __last);
Eric Fiselierfe7fe0a2020-01-15 17:29:55 -0500863 template<class _InputIterator, class = _EnableIf<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000864 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000865 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000866#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000867 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000868 basic_string(initializer_list<_CharT> __il);
Howard Hinnantcf823322010-12-17 14:46:43 +0000869 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000870 basic_string(initializer_list<_CharT> __il, const _Allocator& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000871#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000872
Eric Fiselierd9a702a2016-10-31 03:42:50 +0000873 inline ~basic_string();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000874
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000875 _LIBCPP_INLINE_VISIBILITY
876 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
877
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000878 basic_string& operator=(const basic_string& __str);
Eric Fiselier5ec13b12017-01-23 21:24:58 +0000879
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500880 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 +0000881 basic_string& operator=(const _Tp& __t)
882 {__self_view __sv = __t; return assign(__sv);}
883
Eric Fiselierfc92be82017-04-19 00:28:44 +0000884#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000885 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000886 basic_string& operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000887 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiselierfc92be82017-04-19 00:28:44 +0000888 _LIBCPP_INLINE_VISIBILITY
889 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000890#endif
Howard Hinnantd17880b2013-06-28 16:59:19 +0000891 _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const value_type* __s) {return assign(__s);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000892 basic_string& operator=(value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000893
Louis Dionneba400782020-10-02 15:02:52 -0400894#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +0000895 _LIBCPP_INLINE_VISIBILITY
896 iterator begin() _NOEXCEPT
897 {return iterator(this, __get_pointer());}
898 _LIBCPP_INLINE_VISIBILITY
899 const_iterator begin() const _NOEXCEPT
900 {return const_iterator(this, __get_pointer());}
901 _LIBCPP_INLINE_VISIBILITY
902 iterator end() _NOEXCEPT
903 {return iterator(this, __get_pointer() + size());}
904 _LIBCPP_INLINE_VISIBILITY
905 const_iterator end() const _NOEXCEPT
906 {return const_iterator(this, __get_pointer() + size());}
907#else
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000908 _LIBCPP_INLINE_VISIBILITY
909 iterator begin() _NOEXCEPT
910 {return iterator(__get_pointer());}
911 _LIBCPP_INLINE_VISIBILITY
912 const_iterator begin() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000913 {return const_iterator(__get_pointer());}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000914 _LIBCPP_INLINE_VISIBILITY
915 iterator end() _NOEXCEPT
916 {return iterator(__get_pointer() + size());}
917 _LIBCPP_INLINE_VISIBILITY
918 const_iterator end() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000919 {return const_iterator(__get_pointer() + size());}
Louis Dionneba400782020-10-02 15:02:52 -0400920#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000921 _LIBCPP_INLINE_VISIBILITY
922 reverse_iterator rbegin() _NOEXCEPT
923 {return reverse_iterator(end());}
924 _LIBCPP_INLINE_VISIBILITY
925 const_reverse_iterator rbegin() const _NOEXCEPT
926 {return const_reverse_iterator(end());}
927 _LIBCPP_INLINE_VISIBILITY
928 reverse_iterator rend() _NOEXCEPT
929 {return reverse_iterator(begin());}
930 _LIBCPP_INLINE_VISIBILITY
931 const_reverse_iterator rend() const _NOEXCEPT
932 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000933
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000934 _LIBCPP_INLINE_VISIBILITY
935 const_iterator cbegin() const _NOEXCEPT
936 {return begin();}
937 _LIBCPP_INLINE_VISIBILITY
938 const_iterator cend() const _NOEXCEPT
939 {return end();}
940 _LIBCPP_INLINE_VISIBILITY
941 const_reverse_iterator crbegin() const _NOEXCEPT
942 {return rbegin();}
943 _LIBCPP_INLINE_VISIBILITY
944 const_reverse_iterator crend() const _NOEXCEPT
945 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000946
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000947 _LIBCPP_INLINE_VISIBILITY size_type size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000948 {return __is_long() ? __get_long_size() : __get_short_size();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000949 _LIBCPP_INLINE_VISIBILITY size_type length() const _NOEXCEPT {return size();}
950 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT;
951 _LIBCPP_INLINE_VISIBILITY size_type capacity() const _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +0000952 {return (__is_long() ? __get_long_cap()
953 : static_cast<size_type>(__min_cap)) - 1;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000954
955 void resize(size_type __n, value_type __c);
956 _LIBCPP_INLINE_VISIBILITY void resize(size_type __n) {resize(__n, value_type());}
957
Marek Kurdejc9848142020-11-26 10:07:16 +0100958 void reserve(size_type __requested_capacity);
Eric Fiselier451d5582018-11-26 20:15:38 +0000959 _LIBCPP_INLINE_VISIBILITY void __resize_default_init(size_type __n);
960
Marek Kurdejc9848142020-11-26 10:07:16 +0100961 _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_INLINE_VISIBILITY
962 void reserve() _NOEXCEPT {shrink_to_fit();}
Marshall Clow6ae12a82018-11-28 18:18:34 +0000963 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejc9848142020-11-26 10:07:16 +0100964 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000965 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000966 void clear() _NOEXCEPT;
Marshall Clowb7db4972017-11-15 20:02:27 +0000967 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
968 bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000969
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000970 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __pos) const _NOEXCEPT;
971 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000972
973 const_reference at(size_type __n) const;
974 reference at(size_type __n);
975
976 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const basic_string& __str) {return append(__str);}
Marshall Clowe46031a2018-07-02 18:41:15 +0000977
978 template <class _Tp>
979 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500980 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +0000981 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500982 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
983 && !__is_same_uncvref<_Tp, basic_string >::value,
Marshall Clowe46031a2018-07-02 18:41:15 +0000984 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500985 >
Marshall Clowe46031a2018-07-02 18:41:15 +0000986 operator+=(const _Tp& __t) {__self_view __sv = __t; return append(__sv);}
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000987 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const value_type* __s) {return append(__s);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000988 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(value_type __c) {push_back(__c); return *this;}
Eric Fiselierfc92be82017-04-19 00:28:44 +0000989#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000990 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(initializer_list<value_type> __il) {return append(__il);}
Eric Fiselierfc92be82017-04-19 00:28:44 +0000991#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000992
Howard Hinnantcf823322010-12-17 14:46:43 +0000993 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000994 basic_string& append(const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000995
996 template <class _Tp>
997 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500998 _EnableIf<
999 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1000 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001001 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001002 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001003 append(const _Tp& __t) { __self_view __sv = __t; return append(__sv.data(), __sv.size()); }
Marshall Clow8db7fb02014-03-04 19:17:19 +00001004 basic_string& append(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clowe46031a2018-07-02 18:41:15 +00001005
Marshall Clow62953962016-10-03 23:40:48 +00001006 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001007 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001008 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001009 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001010 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1011 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001012 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001013 >
Marshall Clow82513342016-09-24 22:45:42 +00001014 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001015 basic_string& append(const value_type* __s, size_type __n);
1016 basic_string& append(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001017 basic_string& append(size_type __n, value_type __c);
Eric Fiselier451d5582018-11-26 20:15:38 +00001018
1019 _LIBCPP_INLINE_VISIBILITY
1020 void __append_default_init(size_type __n);
1021
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001022 template <class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001023 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
1024 basic_string& __append_forward_unsafe(_ForwardIterator, _ForwardIterator);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001025 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001026 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001027 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001028 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001029 __is_exactly_cpp17_input_iterator<_InputIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00001030 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001031 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001032 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001033 _LIBCPP_INLINE_VISIBILITY
1034 append(_InputIterator __first, _InputIterator __last) {
1035 const basic_string __temp (__first, __last, __alloc());
1036 append(__temp.data(), __temp.size());
1037 return *this;
1038 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001039 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001040 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001041 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001042 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001043 __is_cpp17_forward_iterator<_ForwardIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00001044 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001045 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001046 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001047 _LIBCPP_INLINE_VISIBILITY
1048 append(_ForwardIterator __first, _ForwardIterator __last) {
1049 return __append_forward_unsafe(__first, __last);
1050 }
1051
Eric Fiselierfc92be82017-04-19 00:28:44 +00001052#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001053 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001054 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001055#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001056
1057 void push_back(value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001058 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001059 void pop_back();
Marshall Clow05cf6692019-03-19 03:30:07 +00001060 _LIBCPP_INLINE_VISIBILITY reference front() _NOEXCEPT;
1061 _LIBCPP_INLINE_VISIBILITY const_reference front() const _NOEXCEPT;
1062 _LIBCPP_INLINE_VISIBILITY reference back() _NOEXCEPT;
1063 _LIBCPP_INLINE_VISIBILITY const_reference back() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001064
Marshall Clowe46031a2018-07-02 18:41:15 +00001065 template <class _Tp>
1066 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001067 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001068 <
1069 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1070 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001071 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001072 assign(const _Tp & __t) { __self_view __sv = __t; return assign(__sv.data(), __sv.size()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001073 _LIBCPP_INLINE_VISIBILITY
Marshall Clow95d5e9a2016-03-09 18:08:29 +00001074 basic_string& assign(const basic_string& __str) { return *this = __str; }
Eric Fiselierfc92be82017-04-19 00:28:44 +00001075#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001076 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001077 basic_string& assign(basic_string&& __str)
Marshall Clow5aa9e942015-10-05 16:17:34 +00001078 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001079 {*this = _VSTD::move(__str); return *this;}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001080#endif
Marshall Clow8db7fb02014-03-04 19:17:19 +00001081 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow62953962016-10-03 23:40:48 +00001082 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001083 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001084 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001085 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001086 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1087 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001088 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001089 >
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001090 assign(const _Tp & __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001091 basic_string& assign(const value_type* __s, size_type __n);
1092 basic_string& assign(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001093 basic_string& assign(size_type __n, value_type __c);
1094 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001095 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001096 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001097 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001098 __is_exactly_cpp17_input_iterator<_InputIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00001099 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001100 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001101 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001102 assign(_InputIterator __first, _InputIterator __last);
1103 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001104 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001105 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001106 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001107 __is_cpp17_forward_iterator<_ForwardIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00001108 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001109 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001110 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001111 assign(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001112#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001113 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001114 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001115#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001116
Howard Hinnantcf823322010-12-17 14:46:43 +00001117 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001118 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001119
1120 template <class _Tp>
1121 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001122 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001123 <
1124 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1125 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001126 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001127 insert(size_type __pos1, const _Tp& __t)
1128 { __self_view __sv = __t; return insert(__pos1, __sv.data(), __sv.size()); }
1129
Marshall Clow82513342016-09-24 22:45:42 +00001130 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001131 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001132 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001133 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001134 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001135 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001136 >
Marshall Clow82513342016-09-24 22:45:42 +00001137 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Marshall Clow8db7fb02014-03-04 19:17:19 +00001138 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001139 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1140 basic_string& insert(size_type __pos, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001141 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1142 iterator insert(const_iterator __pos, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001143 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001144 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1145 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001146 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001147 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001148 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001149 __is_exactly_cpp17_input_iterator<_InputIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00001150 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001151 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001152 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001153 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1154 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001155 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001156 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001157 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001158 __is_cpp17_forward_iterator<_ForwardIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00001159 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001160 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001161 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001162 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001163#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001164 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001165 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1166 {return insert(__pos, __il.begin(), __il.end());}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001167#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001168
1169 basic_string& erase(size_type __pos = 0, size_type __n = npos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001170 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001171 iterator erase(const_iterator __pos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001172 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001173 iterator erase(const_iterator __first, const_iterator __last);
1174
Howard Hinnantcf823322010-12-17 14:46:43 +00001175 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001176 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001177
1178 template <class _Tp>
1179 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001180 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001181 <
1182 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1183 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001184 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001185 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 +00001186 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 +00001187 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001188 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001189 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001190 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001191 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001192 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001193 >
Marshall Clow82513342016-09-24 22:45:42 +00001194 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001195 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
1196 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001197 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001198 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001199 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001200
1201 template <class _Tp>
1202 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001203 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001204 <
1205 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1206 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001207 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001208 replace(const_iterator __i1, const_iterator __i2, const _Tp& __t) { __self_view __sv = __t; return replace(__i1 - begin(), __i2 - __i1, __sv); }
1209
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001210 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001211 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +00001212 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001213 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Howard Hinnantcf823322010-12-17 14:46:43 +00001214 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001215 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001216 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001217 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001218 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001219 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001220 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001221 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001222 >
Howard Hinnant990d6e82010-11-17 21:11:40 +00001223 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001224#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001225 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001226 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001227 {return replace(__i1, __i2, __il.begin(), __il.end());}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001228#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001229
Howard Hinnantd17880b2013-06-28 16:59:19 +00001230 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
Howard Hinnantcf823322010-12-17 14:46:43 +00001231 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001232 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1233
Howard Hinnantcf823322010-12-17 14:46:43 +00001234 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001235 void swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001236#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00001237 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +00001238#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001239 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001240 __is_nothrow_swappable<allocator_type>::value);
1241#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001242
Howard Hinnantcf823322010-12-17 14:46:43 +00001243 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001244 const value_type* c_str() const _NOEXCEPT {return data();}
Howard Hinnantcf823322010-12-17 14:46:43 +00001245 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001246 const value_type* data() const _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Eric Fiselier4ee2a892017-11-20 20:23:27 +00001247#if _LIBCPP_STD_VER > 14 || defined(_LIBCPP_BUILDING_LIBRARY)
Marshall Clowd3c22392016-03-08 15:44:30 +00001248 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001249 value_type* data() _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Marshall Clowd3c22392016-03-08 15:44:30 +00001250#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001251
Howard Hinnantcf823322010-12-17 14:46:43 +00001252 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001253 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001254
Howard Hinnantcf823322010-12-17 14:46:43 +00001255 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001256 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001257
1258 template <class _Tp>
1259 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001260 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001261 <
1262 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1263 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001264 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001265 find(const _Tp& __t, size_type __pos = 0) const;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001266 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001267 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001268 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001269 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001270
Howard Hinnantcf823322010-12-17 14:46:43 +00001271 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001272 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001273
1274 template <class _Tp>
1275 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001276 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001277 <
1278 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1279 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001280 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001281 rfind(const _Tp& __t, size_type __pos = npos) const;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001282 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001283 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001284 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001285 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001286
Howard Hinnantcf823322010-12-17 14:46:43 +00001287 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001288 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001289
1290 template <class _Tp>
1291 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001292 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001293 <
1294 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1295 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001296 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001297 find_first_of(const _Tp& __t, size_type __pos = 0) const;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001298 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001299 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001300 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001301 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001302 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001303
Howard Hinnantcf823322010-12-17 14:46:43 +00001304 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001305 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001306
1307 template <class _Tp>
1308 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001309 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001310 <
1311 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1312 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001313 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001314 find_last_of(const _Tp& __t, size_type __pos = npos) const;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001315 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001316 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001317 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001318 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001319 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001320
Howard Hinnantcf823322010-12-17 14:46:43 +00001321 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001322 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001323
1324 template <class _Tp>
1325 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001326 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001327 <
1328 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1329 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001330 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001331 find_first_not_of(const _Tp &__t, size_type __pos = 0) const;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001332 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 +00001333 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001334 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001335 _LIBCPP_INLINE_VISIBILITY
1336 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1337
1338 _LIBCPP_INLINE_VISIBILITY
1339 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001340
1341 template <class _Tp>
1342 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001343 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001344 <
1345 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1346 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001347 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001348 find_last_not_of(const _Tp& __t, size_type __pos = npos) const;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001349 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 +00001350 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001351 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001352 _LIBCPP_INLINE_VISIBILITY
1353 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1354
1355 _LIBCPP_INLINE_VISIBILITY
1356 int compare(const basic_string& __str) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001357
1358 template <class _Tp>
1359 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001360 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001361 <
1362 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1363 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001364 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001365 compare(const _Tp &__t) const;
1366
1367 template <class _Tp>
1368 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001369 _EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00001370 <
1371 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1372 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001373 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001374 compare(size_type __pos1, size_type __n1, const _Tp& __t) const;
1375
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001376 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001377 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Marshall Clow8db7fb02014-03-04 19:17:19 +00001378 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 +00001379
Marshall Clow82513342016-09-24 22:45:42 +00001380 template <class _Tp>
Eric Fiselier7d4aa5c2016-10-14 05:29:46 +00001381 inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001382 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00001383 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001384 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001385 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001386 >
Marshall Clow82513342016-09-24 22:45:42 +00001387 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 +00001388 int compare(const value_type* __s) const _NOEXCEPT;
1389 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1390 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001391
Marshall Clow18c293b2017-12-04 20:11:38 +00001392#if _LIBCPP_STD_VER > 17
1393 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1394 bool starts_with(__self_view __sv) const _NOEXCEPT
1395 { return __self_view(data(), size()).starts_with(__sv); }
1396
1397 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1398 bool starts_with(value_type __c) const _NOEXCEPT
1399 { return !empty() && _Traits::eq(front(), __c); }
1400
1401 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1402 bool starts_with(const value_type* __s) const _NOEXCEPT
1403 { return starts_with(__self_view(__s)); }
1404
1405 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1406 bool ends_with(__self_view __sv) const _NOEXCEPT
1407 { return __self_view(data(), size()).ends_with( __sv); }
1408
1409 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1410 bool ends_with(value_type __c) const _NOEXCEPT
1411 { return !empty() && _Traits::eq(back(), __c); }
1412
1413 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
1414 bool ends_with(const value_type* __s) const _NOEXCEPT
1415 { return ends_with(__self_view(__s)); }
1416#endif
1417
Howard Hinnantcf823322010-12-17 14:46:43 +00001418 _LIBCPP_INLINE_VISIBILITY bool __invariants() const;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001419
Marshall Clowe60a7182018-05-29 17:04:37 +00001420 _LIBCPP_INLINE_VISIBILITY void __clear_and_shrink() _NOEXCEPT;
Marshall Clow851b9ec2019-05-20 21:56:51 +00001421
Marek Kurdejc9848142020-11-26 10:07:16 +01001422 _LIBCPP_INLINE_VISIBILITY void __shrink_or_extend(size_type __target_capacity);
1423
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001424 _LIBCPP_INLINE_VISIBILITY
1425 bool __is_long() const _NOEXCEPT
1426 {return bool(__r_.first().__s.__size_ & __short_mask);}
1427
Louis Dionneba400782020-10-02 15:02:52 -04001428#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001429
1430 bool __dereferenceable(const const_iterator* __i) const;
1431 bool __decrementable(const const_iterator* __i) const;
1432 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1433 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1434
Louis Dionneba400782020-10-02 15:02:52 -04001435#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001436
Howard Hinnantc51e1022010-05-11 19:42:16 +00001437private:
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001438 _LIBCPP_INLINE_VISIBILITY
1439 allocator_type& __alloc() _NOEXCEPT
1440 {return __r_.second();}
1441 _LIBCPP_INLINE_VISIBILITY
1442 const allocator_type& __alloc() const _NOEXCEPT
1443 {return __r_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001444
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001445#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001446
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001447 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001448 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001449# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001450 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001451# else
1452 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1453# endif
1454
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001455 _LIBCPP_INLINE_VISIBILITY
1456 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001457# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001458 {return __r_.first().__s.__size_ >> 1;}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001459# else
1460 {return __r_.first().__s.__size_;}
1461# endif
1462
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001463#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001464
1465 _LIBCPP_INLINE_VISIBILITY
1466 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001467# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001468 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1469# else
1470 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
1471# endif
1472
1473 _LIBCPP_INLINE_VISIBILITY
1474 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001475# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001476 {return __r_.first().__s.__size_;}
1477# else
1478 {return __r_.first().__s.__size_ >> 1;}
1479# endif
1480
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001481#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001482
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001483 _LIBCPP_INLINE_VISIBILITY
1484 void __set_long_size(size_type __s) _NOEXCEPT
1485 {__r_.first().__l.__size_ = __s;}
1486 _LIBCPP_INLINE_VISIBILITY
1487 size_type __get_long_size() const _NOEXCEPT
1488 {return __r_.first().__l.__size_;}
1489 _LIBCPP_INLINE_VISIBILITY
1490 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001491 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1492
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001493 _LIBCPP_INLINE_VISIBILITY
1494 void __set_long_cap(size_type __s) _NOEXCEPT
1495 {__r_.first().__l.__cap_ = __long_mask | __s;}
1496 _LIBCPP_INLINE_VISIBILITY
1497 size_type __get_long_cap() const _NOEXCEPT
Howard Hinnant28b24882011-12-01 20:21:04 +00001498 {return __r_.first().__l.__cap_ & size_type(~__long_mask);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001499
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001500 _LIBCPP_INLINE_VISIBILITY
1501 void __set_long_pointer(pointer __p) _NOEXCEPT
1502 {__r_.first().__l.__data_ = __p;}
1503 _LIBCPP_INLINE_VISIBILITY
1504 pointer __get_long_pointer() _NOEXCEPT
1505 {return __r_.first().__l.__data_;}
1506 _LIBCPP_INLINE_VISIBILITY
1507 const_pointer __get_long_pointer() const _NOEXCEPT
1508 {return __r_.first().__l.__data_;}
1509 _LIBCPP_INLINE_VISIBILITY
1510 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001511 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001512 _LIBCPP_INLINE_VISIBILITY
1513 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001514 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001515 _LIBCPP_INLINE_VISIBILITY
1516 pointer __get_pointer() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001517 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001518 _LIBCPP_INLINE_VISIBILITY
1519 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001520 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1521
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001522 _LIBCPP_INLINE_VISIBILITY
1523 void __zero() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001524 {
1525 size_type (&__a)[__n_words] = __r_.first().__r.__words;
1526 for (unsigned __i = 0; __i < __n_words; ++__i)
1527 __a[__i] = 0;
1528 }
1529
1530 template <size_type __a> static
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001531 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00001532 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +00001533 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001534 enum {__alignment = 16};
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001535 static _LIBCPP_INLINE_VISIBILITY
1536 size_type __recommend(size_type __s) _NOEXCEPT
Marshall Clow80584522018-02-07 21:30:17 +00001537 {
1538 if (__s < __min_cap) return static_cast<size_type>(__min_cap) - 1;
1539 size_type __guess = __align_it<sizeof(value_type) < __alignment ?
1540 __alignment/sizeof(value_type) : 1 > (__s+1) - 1;
1541 if (__guess == __min_cap) ++__guess;
1542 return __guess;
1543 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001544
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001545 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001546 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001547 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001548 void __init(const value_type* __s, size_type __sz);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001549 inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001550 void __init(size_type __n, value_type __c);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001551
Martijn Vels5e7c9752020-03-04 17:52:46 -05001552 // Slow path for the (inlined) copy constructor for 'long' strings.
1553 // Always externally instantiated and not inlined.
1554 // Requires that __s is zero terminated.
1555 // The main reason for this function to exist is because for unstable, we
1556 // want to allow inlining of the copy constructor. However, we don't want
1557 // to call the __init() functions as those are marked as inline which may
1558 // result in over-aggressive inlining by the compiler, where our aim is
1559 // to only inline the fast path code directly in the ctor.
1560 void __init_copy_ctor_external(const value_type* __s, size_type __sz);
1561
Howard Hinnantc51e1022010-05-11 19:42:16 +00001562 template <class _InputIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001563 inline
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001564 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001565 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001566 __is_exactly_cpp17_input_iterator<_InputIterator>::value
1567 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001568 __init(_InputIterator __first, _InputIterator __last);
1569
1570 template <class _ForwardIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001571 inline
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001572 _EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00001573 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001574 __is_cpp17_forward_iterator<_ForwardIterator>::value
1575 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001576 __init(_ForwardIterator __first, _ForwardIterator __last);
1577
1578 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001579 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001580 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1581 size_type __n_copy, size_type __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00001582 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001583
Martijn Vels596e3de2020-02-26 15:55:49 -05001584 // __assign_no_alias is invoked for assignment operations where we
1585 // have proof that the input does not alias the current instance.
1586 // For example, operator=(basic_string) performs a 'self' check.
1587 template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04001588 basic_string& __assign_no_alias(const value_type* __s, size_type __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05001589
Howard Hinnantcf823322010-12-17 14:46:43 +00001590 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001591 void __erase_to_end(size_type __pos);
1592
Martijn Velsa81fc792020-02-26 13:25:43 -05001593 // __erase_external_with_move is invoked for erase() invocations where
1594 // `n ~= npos`, likely requiring memory moves on the string data.
1595 void __erase_external_with_move(size_type __pos, size_type __n);
1596
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001597 _LIBCPP_INLINE_VISIBILITY
1598 void __copy_assign_alloc(const basic_string& __str)
1599 {__copy_assign_alloc(__str, integral_constant<bool,
1600 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1601
1602 _LIBCPP_INLINE_VISIBILITY
1603 void __copy_assign_alloc(const basic_string& __str, true_type)
1604 {
Marshall Clowf258c202017-01-31 03:40:52 +00001605 if (__alloc() == __str.__alloc())
1606 __alloc() = __str.__alloc();
1607 else
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001608 {
Marshall Clowf258c202017-01-31 03:40:52 +00001609 if (!__str.__is_long())
1610 {
Vedant Kumar55e007e2018-03-08 21:15:26 +00001611 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001612 __alloc() = __str.__alloc();
1613 }
1614 else
1615 {
1616 allocator_type __a = __str.__alloc();
1617 pointer __p = __alloc_traits::allocate(__a, __str.__get_long_cap());
Vedant Kumar55e007e2018-03-08 21:15:26 +00001618 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001619 __alloc() = _VSTD::move(__a);
1620 __set_long_pointer(__p);
1621 __set_long_cap(__str.__get_long_cap());
1622 __set_long_size(__str.size());
1623 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001624 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001625 }
1626
1627 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001628 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001629 {}
1630
Eric Fiselierfc92be82017-04-19 00:28:44 +00001631#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +00001632 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001633 void __move_assign(basic_string& __str, false_type)
1634 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnantcf823322010-12-17 14:46:43 +00001635 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001636 void __move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001637#if _LIBCPP_STD_VER > 14
1638 _NOEXCEPT;
1639#else
Howard Hinnant3e276872011-06-03 18:40:47 +00001640 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001641#endif
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001642#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001643
1644 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001645 void
Howard Hinnantc2734962011-09-02 20:42:31 +00001646 __move_assign_alloc(basic_string& __str)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001647 _NOEXCEPT_(
1648 !__alloc_traits::propagate_on_container_move_assignment::value ||
1649 is_nothrow_move_assignable<allocator_type>::value)
1650 {__move_assign_alloc(__str, integral_constant<bool,
1651 __alloc_traits::propagate_on_container_move_assignment::value>());}
1652
1653 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00001654 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001655 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1656 {
1657 __alloc() = _VSTD::move(__c.__alloc());
1658 }
1659
1660 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001661 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001662 _NOEXCEPT
1663 {}
1664
Martijn Velsda7d94f2020-06-19 14:24:03 -04001665 basic_string& __assign_external(const value_type* __s);
1666 basic_string& __assign_external(const value_type* __s, size_type __n);
1667
1668 // Assigns the value in __s, guaranteed to be __n < __min_cap in length.
1669 inline basic_string& __assign_short(const value_type* __s, size_type __n) {
1670 pointer __p = __is_long()
1671 ? (__set_long_size(__n), __get_long_pointer())
1672 : (__set_short_size(__n), __get_short_pointer());
1673 traits_type::move(_VSTD::__to_address(__p), __s, __n);
1674 traits_type::assign(__p[__n], value_type());
1675 return *this;
1676 }
1677
Howard Hinnantcf823322010-12-17 14:46:43 +00001678 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
1679 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(size_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001680
1681 friend basic_string operator+<>(const basic_string&, const basic_string&);
1682 friend basic_string operator+<>(const value_type*, const basic_string&);
1683 friend basic_string operator+<>(value_type, const basic_string&);
1684 friend basic_string operator+<>(const basic_string&, const value_type*);
1685 friend basic_string operator+<>(const basic_string&, value_type);
1686};
1687
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001688// These declarations must appear before any functions are implicitly used
1689// so that they have the correct visibility specifier.
1690#ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
1691_LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1692_LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1693#else
1694_LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1695_LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1696#endif
1697
1698
Marshall Clowa0563332018-02-08 06:34:03 +00001699#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
1700template<class _InputIterator,
1701 class _CharT = typename iterator_traits<_InputIterator>::value_type,
1702 class _Allocator = allocator<_CharT>,
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001703 class = _EnableIf<__is_cpp17_input_iterator<_InputIterator>::value>,
1704 class = _EnableIf<__is_allocator<_Allocator>::value>
Marshall Clowa0563332018-02-08 06:34:03 +00001705 >
1706basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
1707 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
Marshall Clowe46031a2018-07-02 18:41:15 +00001708
1709template<class _CharT,
1710 class _Traits,
1711 class _Allocator = allocator<_CharT>,
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001712 class = _EnableIf<__is_allocator<_Allocator>::value>
Marshall Clowe46031a2018-07-02 18:41:15 +00001713 >
1714explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
1715 -> basic_string<_CharT, _Traits, _Allocator>;
1716
1717template<class _CharT,
1718 class _Traits,
1719 class _Allocator = allocator<_CharT>,
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001720 class = _EnableIf<__is_allocator<_Allocator>::value>,
Marshall Clowe46031a2018-07-02 18:41:15 +00001721 class _Sz = typename allocator_traits<_Allocator>::size_type
1722 >
1723basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
1724 -> basic_string<_CharT, _Traits, _Allocator>;
Marshall Clowa0563332018-02-08 06:34:03 +00001725#endif
1726
Howard Hinnantc51e1022010-05-11 19:42:16 +00001727template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001728inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001729void
1730basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
1731{
Louis Dionneba400782020-10-02 15:02:52 -04001732#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001733 __get_db()->__invalidate_all(this);
Louis Dionneba400782020-10-02 15:02:52 -04001734#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001735}
1736
1737template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001738inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001739void
Howard Hinnant28b24882011-12-01 20:21:04 +00001740basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type
Louis Dionneba400782020-10-02 15:02:52 -04001741#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant28b24882011-12-01 20:21:04 +00001742 __pos
1743#endif
1744 )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001745{
Louis Dionneba400782020-10-02 15:02:52 -04001746#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001747 __c_node* __c = __get_db()->__find_c_and_lock(this);
1748 if (__c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001749 {
Howard Hinnant8ea98242013-08-23 17:37:05 +00001750 const_pointer __new_last = __get_pointer() + __pos;
1751 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001752 {
Howard Hinnant8ea98242013-08-23 17:37:05 +00001753 --__p;
1754 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1755 if (__i->base() > __new_last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001756 {
Howard Hinnant8ea98242013-08-23 17:37:05 +00001757 (*__p)->__c_ = nullptr;
1758 if (--__c->end_ != __p)
1759 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001760 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001761 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00001762 __get_db()->unlock();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001763 }
Louis Dionneba400782020-10-02 15:02:52 -04001764#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001765}
1766
1767template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001768inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001769basic_string<_CharT, _Traits, _Allocator>::basic_string()
Marshall Clowe546cbb2015-06-04 02:05:41 +00001770 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001771 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001772{
Louis Dionneba400782020-10-02 15:02:52 -04001773#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001774 __get_db()->__insert_c(this);
1775#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001776 __zero();
1777}
1778
1779template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001780inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001781basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
Eric Fiseliere012bf22015-07-18 20:40:46 +00001782#if _LIBCPP_STD_VER <= 14
1783 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1784#else
1785 _NOEXCEPT
1786#endif
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001787: __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001788{
Louis Dionneba400782020-10-02 15:02:52 -04001789#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001790 __get_db()->__insert_c(this);
1791#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001792 __zero();
1793}
1794
1795template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier815ed732016-09-16 00:00:48 +00001796void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1797 size_type __sz,
1798 size_type __reserve)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001799{
1800 if (__reserve > max_size())
1801 this->__throw_length_error();
1802 pointer __p;
1803 if (__reserve < __min_cap)
1804 {
1805 __set_short_size(__sz);
1806 __p = __get_short_pointer();
1807 }
1808 else
1809 {
1810 size_type __cap = __recommend(__reserve);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001811 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001812 __set_long_pointer(__p);
1813 __set_long_cap(__cap+1);
1814 __set_long_size(__sz);
1815 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001816 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001817 traits_type::assign(__p[__sz], value_type());
1818}
1819
1820template <class _CharT, class _Traits, class _Allocator>
1821void
Howard Hinnantd17880b2013-06-28 16:59:19 +00001822basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001823{
1824 if (__sz > max_size())
1825 this->__throw_length_error();
1826 pointer __p;
1827 if (__sz < __min_cap)
1828 {
1829 __set_short_size(__sz);
1830 __p = __get_short_pointer();
1831 }
1832 else
1833 {
1834 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001835 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001836 __set_long_pointer(__p);
1837 __set_long_cap(__cap+1);
1838 __set_long_size(__sz);
1839 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001840 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001841 traits_type::assign(__p[__sz], value_type());
1842}
1843
1844template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00001845template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00001846basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001847 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001848{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001849 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001850 __init(__s, traits_type::length(__s));
Louis Dionneba400782020-10-02 15:02:52 -04001851#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001852 __get_db()->__insert_c(this);
1853#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001854}
1855
1856template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001857inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001858basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001859 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001860{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001861 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001862 __init(__s, __n);
Louis Dionneba400782020-10-02 15:02:52 -04001863#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001864 __get_db()->__insert_c(this);
1865#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001866}
1867
1868template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001869inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001870basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001871 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001872{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001873 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001874 __init(__s, __n);
Louis Dionneba400782020-10-02 15:02:52 -04001875#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001876 __get_db()->__insert_c(this);
1877#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001878}
1879
1880template <class _CharT, class _Traits, class _Allocator>
1881basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001882 : __r_(__default_init_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001883{
1884 if (!__str.__is_long())
1885 __r_.first().__r = __str.__r_.first().__r;
1886 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001887 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1888 __str.__get_long_size());
1889
Louis Dionneba400782020-10-02 15:02:52 -04001890#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001891 __get_db()->__insert_c(this);
1892#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001893}
1894
1895template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00001896basic_string<_CharT, _Traits, _Allocator>::basic_string(
1897 const basic_string& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001898 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001899{
1900 if (!__str.__is_long())
1901 __r_.first().__r = __str.__r_.first().__r;
1902 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001903 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1904 __str.__get_long_size());
Louis Dionneba400782020-10-02 15:02:52 -04001905#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001906 __get_db()->__insert_c(this);
1907#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001908}
1909
Martijn Vels5e7c9752020-03-04 17:52:46 -05001910template <class _CharT, class _Traits, class _Allocator>
1911void basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(
1912 const value_type* __s, size_type __sz) {
1913 pointer __p;
1914 if (__sz < __min_cap) {
1915 __p = __get_short_pointer();
1916 __set_short_size(__sz);
1917 } else {
1918 if (__sz > max_size())
1919 this->__throw_length_error();
1920 size_t __cap = __recommend(__sz);
1921 __p = __alloc_traits::allocate(__alloc(), __cap + 1);
1922 __set_long_pointer(__p);
1923 __set_long_cap(__cap + 1);
1924 __set_long_size(__sz);
1925 }
1926 traits_type::copy(_VSTD::__to_address(__p), __s, __sz + 1);
1927}
1928
Eric Fiselierfc92be82017-04-19 00:28:44 +00001929#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001930
1931template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001932inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001933basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +00001934#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +00001935 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowa80abc72015-06-03 19:56:43 +00001936#else
1937 _NOEXCEPT
1938#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001939 : __r_(_VSTD::move(__str.__r_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001940{
1941 __str.__zero();
Louis Dionneba400782020-10-02 15:02:52 -04001942#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001943 __get_db()->__insert_c(this);
1944 if (__is_long())
1945 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001946#endif
1947}
1948
1949template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001950inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001951basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001952 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001953{
Marshall Clowd2d24692014-07-17 15:32:20 +00001954 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001955 __init(_VSTD::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd2d24692014-07-17 15:32:20 +00001956 else
1957 {
1958 __r_.first().__r = __str.__r_.first().__r;
1959 __str.__zero();
1960 }
Louis Dionneba400782020-10-02 15:02:52 -04001961#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001962 __get_db()->__insert_c(this);
1963 if (__is_long())
1964 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001965#endif
1966}
1967
Eric Fiselierfc92be82017-04-19 00:28:44 +00001968#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001969
1970template <class _CharT, class _Traits, class _Allocator>
1971void
1972basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
1973{
1974 if (__n > max_size())
1975 this->__throw_length_error();
1976 pointer __p;
1977 if (__n < __min_cap)
1978 {
1979 __set_short_size(__n);
1980 __p = __get_short_pointer();
1981 }
1982 else
1983 {
1984 size_type __cap = __recommend(__n);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001985 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001986 __set_long_pointer(__p);
1987 __set_long_cap(__cap+1);
1988 __set_long_size(__n);
1989 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001990 traits_type::assign(_VSTD::__to_address(__p), __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001991 traits_type::assign(__p[__n], value_type());
1992}
1993
1994template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001995inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001996basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001997 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001998{
1999 __init(__n, __c);
Louis Dionneba400782020-10-02 15:02:52 -04002000#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002001 __get_db()->__insert_c(this);
2002#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002003}
2004
2005template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002006template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00002007basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002008 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002009{
2010 __init(__n, __c);
Louis Dionneba400782020-10-02 15:02:52 -04002011#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002012 __get_db()->__insert_c(this);
2013#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002014}
2015
Howard Hinnantc51e1022010-05-11 19:42:16 +00002016template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00002017basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
2018 size_type __pos, size_type __n,
2019 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002020 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002021{
2022 size_type __str_sz = __str.size();
2023 if (__pos > __str_sz)
2024 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002025 __init(__str.data() + __pos, _VSTD::min(__n, __str_sz - __pos));
Louis Dionneba400782020-10-02 15:02:52 -04002026#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002027 __get_db()->__insert_c(this);
2028#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002029}
2030
2031template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002032inline
Marshall Clow83445802016-04-07 18:13:41 +00002033basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +00002034 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002035 : __r_(__default_init_tag(), __a)
Marshall Clow83445802016-04-07 18:13:41 +00002036{
2037 size_type __str_sz = __str.size();
2038 if (__pos > __str_sz)
2039 this->__throw_out_of_range();
2040 __init(__str.data() + __pos, __str_sz - __pos);
Louis Dionneba400782020-10-02 15:02:52 -04002041#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clow83445802016-04-07 18:13:41 +00002042 __get_db()->__insert_c(this);
2043#endif
2044}
2045
2046template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002047template <class _Tp, class>
Marshall Clow78dbe462016-11-14 18:22:19 +00002048basic_string<_CharT, _Traits, _Allocator>::basic_string(
Marshall Clowe46031a2018-07-02 18:41:15 +00002049 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002050 : __r_(__default_init_tag(), __a)
Marshall Clow78dbe462016-11-14 18:22:19 +00002051{
Marshall Clowe46031a2018-07-02 18:41:15 +00002052 __self_view __sv0 = __t;
2053 __self_view __sv = __sv0.substr(__pos, __n);
Marshall Clow78dbe462016-11-14 18:22:19 +00002054 __init(__sv.data(), __sv.size());
Louis Dionneba400782020-10-02 15:02:52 -04002055#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clow78dbe462016-11-14 18:22:19 +00002056 __get_db()->__insert_c(this);
Eric Fiselier812882b2017-02-17 01:17:10 +00002057#endif
Marshall Clow78dbe462016-11-14 18:22:19 +00002058}
2059
2060template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002061template <class _Tp, class>
2062basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002063 : __r_(__default_init_tag(), __default_init_tag())
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002064{
Marshall Clowe46031a2018-07-02 18:41:15 +00002065 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002066 __init(__sv.data(), __sv.size());
Louis Dionneba400782020-10-02 15:02:52 -04002067#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002068 __get_db()->__insert_c(this);
2069#endif
2070}
2071
2072template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002073template <class _Tp, class>
2074basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002075 : __r_(__default_init_tag(), __a)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002076{
Marshall Clowe46031a2018-07-02 18:41:15 +00002077 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002078 __init(__sv.data(), __sv.size());
Louis Dionneba400782020-10-02 15:02:52 -04002079#if _LIBCPP_DEBUG_LEVEL == 2
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002080 __get_db()->__insert_c(this);
2081#endif
2082}
2083
2084template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002085template <class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002086_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002087<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002088 __is_exactly_cpp17_input_iterator<_InputIterator>::value
2089>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002090basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
2091{
2092 __zero();
2093#ifndef _LIBCPP_NO_EXCEPTIONS
2094 try
2095 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002096#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002097 for (; __first != __last; ++__first)
2098 push_back(*__first);
2099#ifndef _LIBCPP_NO_EXCEPTIONS
2100 }
2101 catch (...)
2102 {
2103 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002104 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002105 throw;
2106 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00002107#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002108}
2109
2110template <class _CharT, class _Traits, class _Allocator>
2111template <class _ForwardIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002112_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002113<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002114 __is_cpp17_forward_iterator<_ForwardIterator>::value
2115>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002116basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
2117{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002118 size_type __sz = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002119 if (__sz > max_size())
2120 this->__throw_length_error();
2121 pointer __p;
2122 if (__sz < __min_cap)
2123 {
2124 __set_short_size(__sz);
2125 __p = __get_short_pointer();
2126 }
2127 else
2128 {
2129 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002130 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002131 __set_long_pointer(__p);
2132 __set_long_cap(__cap+1);
2133 __set_long_size(__sz);
2134 }
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002135 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002136 traits_type::assign(*__p, *__first);
2137 traits_type::assign(*__p, value_type());
2138}
2139
2140template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002141template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002142inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002143basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002144 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002145{
2146 __init(__first, __last);
Louis Dionneba400782020-10-02 15:02:52 -04002147#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002148 __get_db()->__insert_c(this);
2149#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002150}
2151
2152template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002153template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002154inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002155basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
2156 const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002157 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002158{
2159 __init(__first, __last);
Louis Dionneba400782020-10-02 15:02:52 -04002160#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002161 __get_db()->__insert_c(this);
2162#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002163}
2164
Eric Fiselierfc92be82017-04-19 00:28:44 +00002165#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002166
Howard Hinnantc51e1022010-05-11 19:42:16 +00002167template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002168inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002169basic_string<_CharT, _Traits, _Allocator>::basic_string(
2170 initializer_list<_CharT> __il)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002171 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002172{
2173 __init(__il.begin(), __il.end());
Louis Dionneba400782020-10-02 15:02:52 -04002174#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002175 __get_db()->__insert_c(this);
2176#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002177}
2178
2179template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002180inline
Eric Fiselier9d355982017-04-12 23:45:53 +00002181
Eric Fiselier812882b2017-02-17 01:17:10 +00002182basic_string<_CharT, _Traits, _Allocator>::basic_string(
2183 initializer_list<_CharT> __il, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002184 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002185{
2186 __init(__il.begin(), __il.end());
Louis Dionneba400782020-10-02 15:02:52 -04002187#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002188 __get_db()->__insert_c(this);
2189#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002190}
2191
Eric Fiselierfc92be82017-04-19 00:28:44 +00002192#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002193
Howard Hinnantc51e1022010-05-11 19:42:16 +00002194template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002195basic_string<_CharT, _Traits, _Allocator>::~basic_string()
2196{
Louis Dionneba400782020-10-02 15:02:52 -04002197#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002198 __get_db()->__erase_c(this);
2199#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002200 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002201 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002202}
2203
2204template <class _CharT, class _Traits, class _Allocator>
2205void
2206basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
2207 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002208 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 +00002209{
2210 size_type __ms = max_size();
2211 if (__delta_cap > __ms - __old_cap - 1)
2212 this->__throw_length_error();
2213 pointer __old_p = __get_pointer();
2214 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002215 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002216 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002217 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002218 __invalidate_all_iterators();
2219 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002220 traits_type::copy(_VSTD::__to_address(__p),
2221 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002222 if (__n_add != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002223 traits_type::copy(_VSTD::__to_address(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002224 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2225 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002226 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2227 _VSTD::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002228 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002229 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002230 __set_long_pointer(__p);
2231 __set_long_cap(__cap+1);
2232 __old_sz = __n_copy + __n_add + __sec_cp_sz;
2233 __set_long_size(__old_sz);
2234 traits_type::assign(__p[__old_sz], value_type());
2235}
2236
2237template <class _CharT, class _Traits, class _Allocator>
2238void
2239basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2240 size_type __n_copy, size_type __n_del, size_type __n_add)
2241{
2242 size_type __ms = max_size();
Marshall Clow2267c5d2013-11-06 14:24:38 +00002243 if (__delta_cap > __ms - __old_cap)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002244 this->__throw_length_error();
2245 pointer __old_p = __get_pointer();
2246 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002247 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002248 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002249 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002250 __invalidate_all_iterators();
2251 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002252 traits_type::copy(_VSTD::__to_address(__p),
2253 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002254 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2255 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002256 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2257 _VSTD::__to_address(__old_p) + __n_copy + __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002258 __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002259 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002260 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002261 __set_long_pointer(__p);
2262 __set_long_cap(__cap+1);
2263}
2264
2265// assign
2266
2267template <class _CharT, class _Traits, class _Allocator>
Martijn Vels596e3de2020-02-26 15:55:49 -05002268template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04002269basic_string<_CharT, _Traits, _Allocator>&
2270basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(
Martijn Vels596e3de2020-02-26 15:55:49 -05002271 const value_type* __s, size_type __n) {
2272 size_type __cap = __is_short ? __min_cap : __get_long_cap();
2273 if (__n < __cap) {
2274 pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
2275 __is_short ? __set_short_size(__n) : __set_long_size(__n);
2276 traits_type::copy(_VSTD::__to_address(__p), __s, __n);
2277 traits_type::assign(__p[__n], value_type());
2278 __invalidate_iterators_past(__n);
2279 } else {
2280 size_type __sz = __is_short ? __get_short_size() : __get_long_size();
2281 __grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
2282 }
Martijn Velsb6a08b62020-04-10 18:36:31 -04002283 return *this;
Martijn Vels596e3de2020-02-26 15:55:49 -05002284}
2285
2286template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002287basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002288basic_string<_CharT, _Traits, _Allocator>::__assign_external(
2289 const value_type* __s, size_type __n) {
2290 size_type __cap = capacity();
2291 if (__cap >= __n) {
2292 value_type* __p = _VSTD::__to_address(__get_pointer());
2293 traits_type::move(__p, __s, __n);
2294 traits_type::assign(__p[__n], value_type());
2295 __set_size(__n);
2296 __invalidate_iterators_past(__n);
2297 } else {
2298 size_type __sz = size();
2299 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
2300 }
2301 return *this;
2302}
2303
2304template <class _CharT, class _Traits, class _Allocator>
2305basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002306basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002307{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002308 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Martijn Velsda7d94f2020-06-19 14:24:03 -04002309 return (_LIBCPP_BUILTIN_CONSTANT_P(__n) && __n < __min_cap)
2310 ? __assign_short(__s, __n)
2311 : __assign_external(__s, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002312}
2313
2314template <class _CharT, class _Traits, class _Allocator>
2315basic_string<_CharT, _Traits, _Allocator>&
2316basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2317{
2318 size_type __cap = capacity();
2319 if (__cap < __n)
2320 {
2321 size_type __sz = size();
2322 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2323 }
2324 else
2325 __invalidate_iterators_past(__n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002326 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002327 traits_type::assign(__p, __n, __c);
2328 traits_type::assign(__p[__n], value_type());
2329 __set_size(__n);
2330 return *this;
2331}
2332
2333template <class _CharT, class _Traits, class _Allocator>
2334basic_string<_CharT, _Traits, _Allocator>&
2335basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2336{
2337 pointer __p;
2338 if (__is_long())
2339 {
2340 __p = __get_long_pointer();
2341 __set_long_size(1);
2342 }
2343 else
2344 {
2345 __p = __get_short_pointer();
2346 __set_short_size(1);
2347 }
2348 traits_type::assign(*__p, __c);
2349 traits_type::assign(*++__p, value_type());
2350 __invalidate_iterators_past(1);
2351 return *this;
2352}
2353
2354template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002355basic_string<_CharT, _Traits, _Allocator>&
2356basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2357{
Martijn Vels596e3de2020-02-26 15:55:49 -05002358 if (this != &__str) {
2359 __copy_assign_alloc(__str);
2360 if (!__is_long()) {
2361 if (!__str.__is_long()) {
Eric Fiselier03bbc922020-01-15 17:27:10 -05002362 __r_.first().__r = __str.__r_.first().__r;
Martijn Vels596e3de2020-02-26 15:55:49 -05002363 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002364 return __assign_no_alias<true>(__str.data(), __str.size());
Martijn Vels596e3de2020-02-26 15:55:49 -05002365 }
2366 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002367 return __assign_no_alias<false>(__str.data(), __str.size());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002368 }
Martijn Vels596e3de2020-02-26 15:55:49 -05002369 }
2370 return *this;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002371}
2372
Eric Fiselierfc92be82017-04-19 00:28:44 +00002373#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002374
2375template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002376inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002377void
2378basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002379 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002380{
2381 if (__alloc() != __str.__alloc())
2382 assign(__str);
2383 else
2384 __move_assign(__str, true_type());
2385}
2386
2387template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002388inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002389void
2390basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002391#if _LIBCPP_STD_VER > 14
2392 _NOEXCEPT
2393#else
Howard Hinnant3e276872011-06-03 18:40:47 +00002394 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002395#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002396{
marshall2ed41622019-11-27 07:13:00 -08002397 if (__is_long()) {
2398 __alloc_traits::deallocate(__alloc(), __get_long_pointer(),
2399 __get_long_cap());
2400#if _LIBCPP_STD_VER <= 14
2401 if (!is_nothrow_move_assignable<allocator_type>::value) {
2402 __set_short_size(0);
2403 traits_type::assign(__get_short_pointer()[0], value_type());
2404 }
2405#endif
2406 }
2407 __move_assign_alloc(__str);
2408 __r_.first() = __str.__r_.first();
2409 __str.__set_short_size(0);
2410 traits_type::assign(__str.__get_short_pointer()[0], value_type());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002411}
2412
2413template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002414inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002415basic_string<_CharT, _Traits, _Allocator>&
2416basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002417 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002418{
2419 __move_assign(__str, integral_constant<bool,
2420 __alloc_traits::propagate_on_container_move_assignment::value>());
2421 return *this;
2422}
2423
2424#endif
2425
2426template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002427template<class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002428_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002429<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002430 __is_exactly_cpp17_input_iterator <_InputIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00002431 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002432 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002433>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002434basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2435{
Marshall Clow958362f2016-09-05 01:54:30 +00002436 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002437 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002438 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002439}
2440
2441template <class _CharT, class _Traits, class _Allocator>
2442template<class _ForwardIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002443_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002444<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002445 __is_cpp17_forward_iterator<_ForwardIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00002446 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002447 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002448>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002449basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2450{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002451 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002452 size_type __cap = capacity();
2453 if (__cap < __n)
2454 {
2455 size_type __sz = size();
2456 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2457 }
2458 else
2459 __invalidate_iterators_past(__n);
2460 pointer __p = __get_pointer();
2461 for (; __first != __last; ++__first, ++__p)
2462 traits_type::assign(*__p, *__first);
2463 traits_type::assign(*__p, value_type());
2464 __set_size(__n);
2465 return *this;
2466}
2467
2468template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002469basic_string<_CharT, _Traits, _Allocator>&
2470basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2471{
2472 size_type __sz = __str.size();
2473 if (__pos > __sz)
2474 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002475 return assign(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002476}
2477
2478template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002479template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002480_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00002481<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002482 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
2483 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00002484 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002485>
Marshall Clow82513342016-09-24 22:45:42 +00002486basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002487{
Marshall Clow82513342016-09-24 22:45:42 +00002488 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002489 size_type __sz = __sv.size();
2490 if (__pos > __sz)
2491 this->__throw_out_of_range();
2492 return assign(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2493}
2494
2495
2496template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002497basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002498basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s) {
2499 return __assign_external(__s, traits_type::length(__s));
2500}
2501
2502template <class _CharT, class _Traits, class _Allocator>
2503basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002504basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002505{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002506 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Martijn Velsda7d94f2020-06-19 14:24:03 -04002507 return _LIBCPP_BUILTIN_CONSTANT_P(*__s)
2508 ? (traits_type::length(__s) < __min_cap
2509 ? __assign_short(__s, traits_type::length(__s))
2510 : __assign_external(__s, traits_type::length(__s)))
2511 : __assign_external(__s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002512}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002513// append
2514
2515template <class _CharT, class _Traits, class _Allocator>
2516basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002517basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002518{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002519 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002520 size_type __cap = capacity();
2521 size_type __sz = size();
2522 if (__cap - __sz >= __n)
2523 {
2524 if (__n)
2525 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002526 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002527 traits_type::copy(__p + __sz, __s, __n);
2528 __sz += __n;
2529 __set_size(__sz);
2530 traits_type::assign(__p[__sz], value_type());
2531 }
2532 }
2533 else
2534 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2535 return *this;
2536}
2537
2538template <class _CharT, class _Traits, class _Allocator>
2539basic_string<_CharT, _Traits, _Allocator>&
2540basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2541{
2542 if (__n)
2543 {
2544 size_type __cap = capacity();
2545 size_type __sz = size();
2546 if (__cap - __sz < __n)
2547 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2548 pointer __p = __get_pointer();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002549 traits_type::assign(_VSTD::__to_address(__p) + __sz, __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002550 __sz += __n;
2551 __set_size(__sz);
2552 traits_type::assign(__p[__sz], value_type());
2553 }
2554 return *this;
2555}
2556
2557template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00002558inline void
2559basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n)
2560{
2561 if (__n)
2562 {
2563 size_type __cap = capacity();
2564 size_type __sz = size();
2565 if (__cap - __sz < __n)
2566 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2567 pointer __p = __get_pointer();
2568 __sz += __n;
2569 __set_size(__sz);
2570 traits_type::assign(__p[__sz], value_type());
2571 }
2572}
2573
2574template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002575void
2576basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2577{
Howard Hinnant68bf1812013-04-30 21:44:48 +00002578 bool __is_short = !__is_long();
2579 size_type __cap;
2580 size_type __sz;
2581 if (__is_short)
2582 {
2583 __cap = __min_cap - 1;
2584 __sz = __get_short_size();
2585 }
2586 else
2587 {
2588 __cap = __get_long_cap() - 1;
2589 __sz = __get_long_size();
2590 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002591 if (__sz == __cap)
Howard Hinnant68bf1812013-04-30 21:44:48 +00002592 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002593 __grow_by(__cap, 1, __sz, __sz, 0);
Howard Hinnant68bf1812013-04-30 21:44:48 +00002594 __is_short = !__is_long();
2595 }
2596 pointer __p;
2597 if (__is_short)
2598 {
2599 __p = __get_short_pointer() + __sz;
2600 __set_short_size(__sz+1);
2601 }
2602 else
2603 {
2604 __p = __get_long_pointer() + __sz;
2605 __set_long_size(__sz+1);
2606 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002607 traits_type::assign(*__p, __c);
2608 traits_type::assign(*++__p, value_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002609}
2610
Marshall Clow6ebbf662016-09-07 03:32:06 +00002611template <class _Tp>
Marshall Clow958362f2016-09-05 01:54:30 +00002612bool __ptr_in_range (const _Tp* __p, const _Tp* __first, const _Tp* __last)
2613{
2614 return __first <= __p && __p < __last;
2615}
2616
Marshall Clow6ebbf662016-09-07 03:32:06 +00002617template <class _Tp1, class _Tp2>
Eric Fiselier6003c772016-12-23 23:37:52 +00002618bool __ptr_in_range (const _Tp1*, const _Tp2*, const _Tp2*)
Marshall Clow6ebbf662016-09-07 03:32:06 +00002619{
2620 return false;
2621}
2622
Howard Hinnantc51e1022010-05-11 19:42:16 +00002623template <class _CharT, class _Traits, class _Allocator>
2624template<class _ForwardIterator>
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002625basic_string<_CharT, _Traits, _Allocator>&
2626basic_string<_CharT, _Traits, _Allocator>::__append_forward_unsafe(
2627 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002628{
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002629 static_assert(__is_cpp17_forward_iterator<_ForwardIterator>::value,
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002630 "function requires a ForwardIterator");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002631 size_type __sz = size();
2632 size_type __cap = capacity();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002633 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002634 if (__n)
2635 {
Eric Fiselier96866b52017-04-15 06:49:02 +00002636 typedef typename iterator_traits<_ForwardIterator>::reference _CharRef;
2637 _CharRef __tmp_ref = *__first;
2638 if (__ptr_in_range(_VSTD::addressof(__tmp_ref), data(), data() + size()))
Marshall Clow958362f2016-09-05 01:54:30 +00002639 {
2640 const basic_string __temp (__first, __last, __alloc());
2641 append(__temp.data(), __temp.size());
2642 }
Louis Dionne173f29e2019-05-29 16:01:36 +00002643 else
Marshall Clow958362f2016-09-05 01:54:30 +00002644 {
2645 if (__cap - __sz < __n)
2646 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2647 pointer __p = __get_pointer() + __sz;
2648 for (; __first != __last; ++__p, ++__first)
2649 traits_type::assign(*__p, *__first);
2650 traits_type::assign(*__p, value_type());
2651 __set_size(__sz + __n);
2652 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002653 }
2654 return *this;
2655}
2656
2657template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002658inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002659basic_string<_CharT, _Traits, _Allocator>&
2660basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2661{
2662 return append(__str.data(), __str.size());
2663}
2664
2665template <class _CharT, class _Traits, class _Allocator>
2666basic_string<_CharT, _Traits, _Allocator>&
2667basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2668{
2669 size_type __sz = __str.size();
2670 if (__pos > __sz)
2671 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002672 return append(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002673}
2674
2675template <class _CharT, class _Traits, class _Allocator>
Marshall Clow62953962016-10-03 23:40:48 +00002676template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002677 _EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00002678 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002679 __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 +00002680 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002681 >
Marshall Clow82513342016-09-24 22:45:42 +00002682basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002683{
Marshall Clow82513342016-09-24 22:45:42 +00002684 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002685 size_type __sz = __sv.size();
2686 if (__pos > __sz)
2687 this->__throw_out_of_range();
2688 return append(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2689}
2690
2691template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002692basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002693basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002694{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002695 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002696 return append(__s, traits_type::length(__s));
2697}
2698
2699// insert
2700
2701template <class _CharT, class _Traits, class _Allocator>
2702basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002703basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002704{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002705 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002706 size_type __sz = size();
2707 if (__pos > __sz)
2708 this->__throw_out_of_range();
2709 size_type __cap = capacity();
2710 if (__cap - __sz >= __n)
2711 {
2712 if (__n)
2713 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002714 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002715 size_type __n_move = __sz - __pos;
2716 if (__n_move != 0)
2717 {
2718 if (__p + __pos <= __s && __s < __p + __sz)
2719 __s += __n;
2720 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2721 }
2722 traits_type::move(__p + __pos, __s, __n);
2723 __sz += __n;
2724 __set_size(__sz);
2725 traits_type::assign(__p[__sz], value_type());
2726 }
2727 }
2728 else
2729 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2730 return *this;
2731}
2732
2733template <class _CharT, class _Traits, class _Allocator>
2734basic_string<_CharT, _Traits, _Allocator>&
2735basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2736{
2737 size_type __sz = size();
2738 if (__pos > __sz)
2739 this->__throw_out_of_range();
2740 if (__n)
2741 {
2742 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002743 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002744 if (__cap - __sz >= __n)
2745 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002746 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002747 size_type __n_move = __sz - __pos;
2748 if (__n_move != 0)
2749 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2750 }
2751 else
2752 {
2753 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002754 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002755 }
2756 traits_type::assign(__p + __pos, __n, __c);
2757 __sz += __n;
2758 __set_size(__sz);
2759 traits_type::assign(__p[__sz], value_type());
2760 }
2761 return *this;
2762}
2763
2764template <class _CharT, class _Traits, class _Allocator>
2765template<class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002766_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002767<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002768 __is_exactly_cpp17_input_iterator<_InputIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00002769 || !__libcpp_string_gets_noexcept_iterator<_InputIterator>::value,
2770 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002771>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002772basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2773{
Louis Dionneba400782020-10-02 15:02:52 -04002774#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002775 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2776 "string::insert(iterator, range) called with an iterator not"
2777 " referring to this string");
2778#endif
Marshall Clow958362f2016-09-05 01:54:30 +00002779 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002780 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002781}
2782
2783template <class _CharT, class _Traits, class _Allocator>
2784template<class _ForwardIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002785_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00002786<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002787 __is_cpp17_forward_iterator<_ForwardIterator>::value
Marshall Clow039b2f02016-01-13 21:54:34 +00002788 && __libcpp_string_gets_noexcept_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002789 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002790>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002791basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2792{
Louis Dionneba400782020-10-02 15:02:52 -04002793#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002794 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2795 "string::insert(iterator, range) called with an iterator not"
2796 " referring to this string");
2797#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002798 size_type __ip = static_cast<size_type>(__pos - begin());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002799 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002800 if (__n)
2801 {
Eric Fiselier96866b52017-04-15 06:49:02 +00002802 typedef typename iterator_traits<_ForwardIterator>::reference _CharRef;
2803 _CharRef __tmp_char = *__first;
2804 if (__ptr_in_range(_VSTD::addressof(__tmp_char), data(), data() + size()))
Marshall Clow958362f2016-09-05 01:54:30 +00002805 {
2806 const basic_string __temp(__first, __last, __alloc());
2807 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
2808 }
2809
2810 size_type __sz = size();
2811 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002812 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002813 if (__cap - __sz >= __n)
2814 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002815 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002816 size_type __n_move = __sz - __ip;
2817 if (__n_move != 0)
2818 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
2819 }
2820 else
2821 {
2822 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002823 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002824 }
2825 __sz += __n;
2826 __set_size(__sz);
2827 traits_type::assign(__p[__sz], value_type());
2828 for (__p += __ip; __first != __last; ++__p, ++__first)
2829 traits_type::assign(*__p, *__first);
2830 }
2831 return begin() + __ip;
2832}
2833
2834template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002835inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002836basic_string<_CharT, _Traits, _Allocator>&
2837basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2838{
2839 return insert(__pos1, __str.data(), __str.size());
2840}
2841
2842template <class _CharT, class _Traits, class _Allocator>
2843basic_string<_CharT, _Traits, _Allocator>&
2844basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2845 size_type __pos2, size_type __n)
2846{
2847 size_type __str_sz = __str.size();
2848 if (__pos2 > __str_sz)
2849 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002850 return insert(__pos1, __str.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002851}
2852
2853template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002854template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002855_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00002856<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002857 __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 +00002858 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002859>
Marshall Clow82513342016-09-24 22:45:42 +00002860basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002861 size_type __pos2, size_type __n)
2862{
Marshall Clow82513342016-09-24 22:45:42 +00002863 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002864 size_type __str_sz = __sv.size();
2865 if (__pos2 > __str_sz)
2866 this->__throw_out_of_range();
2867 return insert(__pos1, __sv.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
2868}
2869
2870template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002871basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002872basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002873{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002874 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002875 return insert(__pos, __s, traits_type::length(__s));
2876}
2877
2878template <class _CharT, class _Traits, class _Allocator>
2879typename basic_string<_CharT, _Traits, _Allocator>::iterator
2880basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2881{
2882 size_type __ip = static_cast<size_type>(__pos - begin());
2883 size_type __sz = size();
2884 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002885 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002886 if (__cap == __sz)
2887 {
2888 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002889 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002890 }
2891 else
2892 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002893 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002894 size_type __n_move = __sz - __ip;
2895 if (__n_move != 0)
2896 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2897 }
2898 traits_type::assign(__p[__ip], __c);
2899 traits_type::assign(__p[++__sz], value_type());
2900 __set_size(__sz);
2901 return begin() + static_cast<difference_type>(__ip);
2902}
2903
2904template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002905inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002906typename basic_string<_CharT, _Traits, _Allocator>::iterator
2907basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2908{
Louis Dionneba400782020-10-02 15:02:52 -04002909#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00002910 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2911 "string::insert(iterator, n, value) called with an iterator not"
2912 " referring to this string");
2913#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002914 difference_type __p = __pos - begin();
2915 insert(static_cast<size_type>(__p), __n, __c);
2916 return begin() + __p;
2917}
2918
2919// replace
2920
2921template <class _CharT, class _Traits, class _Allocator>
2922basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002923basic_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 +00002924 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00002925{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002926 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002927 size_type __sz = size();
2928 if (__pos > __sz)
2929 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002930 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002931 size_type __cap = capacity();
2932 if (__cap - __sz + __n1 >= __n2)
2933 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002934 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002935 if (__n1 != __n2)
2936 {
2937 size_type __n_move = __sz - __pos - __n1;
2938 if (__n_move != 0)
2939 {
2940 if (__n1 > __n2)
2941 {
2942 traits_type::move(__p + __pos, __s, __n2);
2943 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2944 goto __finish;
2945 }
2946 if (__p + __pos < __s && __s < __p + __sz)
2947 {
2948 if (__p + __pos + __n1 <= __s)
2949 __s += __n2 - __n1;
2950 else // __p + __pos < __s < __p + __pos + __n1
2951 {
2952 traits_type::move(__p + __pos, __s, __n1);
2953 __pos += __n1;
2954 __s += __n2;
2955 __n2 -= __n1;
2956 __n1 = 0;
2957 }
2958 }
2959 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2960 }
2961 }
2962 traits_type::move(__p + __pos, __s, __n2);
2963__finish:
Martijn Vels1c48afb2020-01-28 13:43:19 -05002964// __sz += __n2 - __n1; in this and the below function below can cause unsigned
2965// integer overflow, but this is a safe operation, so we disable the check.
Howard Hinnantc51e1022010-05-11 19:42:16 +00002966 __sz += __n2 - __n1;
2967 __set_size(__sz);
2968 __invalidate_iterators_past(__sz);
2969 traits_type::assign(__p[__sz], value_type());
2970 }
2971 else
2972 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
2973 return *this;
2974}
2975
2976template <class _CharT, class _Traits, class _Allocator>
2977basic_string<_CharT, _Traits, _Allocator>&
2978basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
Eric Fiseliere5b21842017-03-09 01:54:13 +00002979 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00002980{
2981 size_type __sz = size();
2982 if (__pos > __sz)
2983 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002984 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002985 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002986 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002987 if (__cap - __sz + __n1 >= __n2)
2988 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002989 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002990 if (__n1 != __n2)
2991 {
2992 size_type __n_move = __sz - __pos - __n1;
2993 if (__n_move != 0)
2994 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2995 }
2996 }
2997 else
2998 {
2999 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003000 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003001 }
3002 traits_type::assign(__p + __pos, __n2, __c);
3003 __sz += __n2 - __n1;
3004 __set_size(__sz);
3005 __invalidate_iterators_past(__sz);
3006 traits_type::assign(__p[__sz], value_type());
3007 return *this;
3008}
3009
3010template <class _CharT, class _Traits, class _Allocator>
3011template<class _InputIterator>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003012_EnableIf
Howard Hinnantc51e1022010-05-11 19:42:16 +00003013<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003014 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003015 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003016>
Howard Hinnant990d6e82010-11-17 21:11:40 +00003017basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003018 _InputIterator __j1, _InputIterator __j2)
3019{
Marshall Clow958362f2016-09-05 01:54:30 +00003020 const basic_string __temp(__j1, __j2, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00003021 return this->replace(__i1, __i2, __temp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003022}
3023
3024template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003025inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003026basic_string<_CharT, _Traits, _Allocator>&
3027basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
3028{
3029 return replace(__pos1, __n1, __str.data(), __str.size());
3030}
3031
3032template <class _CharT, class _Traits, class _Allocator>
3033basic_string<_CharT, _Traits, _Allocator>&
3034basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
3035 size_type __pos2, size_type __n2)
3036{
3037 size_type __str_sz = __str.size();
3038 if (__pos2 > __str_sz)
3039 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003040 return replace(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003041}
3042
3043template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003044template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003045_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00003046<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003047 __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 +00003048 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003049>
Marshall Clow82513342016-09-24 22:45:42 +00003050basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003051 size_type __pos2, size_type __n2)
3052{
Marshall Clow82513342016-09-24 22:45:42 +00003053 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003054 size_type __str_sz = __sv.size();
3055 if (__pos2 > __str_sz)
3056 this->__throw_out_of_range();
3057 return replace(__pos1, __n1, __sv.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
3058}
3059
3060template <class _CharT, class _Traits, class _Allocator>
3061basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003062basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003063{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003064 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003065 return replace(__pos, __n1, __s, traits_type::length(__s));
3066}
3067
3068template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003069inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003070basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003071basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003072{
3073 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
3074 __str.data(), __str.size());
3075}
3076
3077template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003078inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003079basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003080basic_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 +00003081{
3082 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
3083}
3084
3085template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003086inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003087basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003088basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003089{
3090 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
3091}
3092
3093template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003094inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003095basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003096basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003097{
3098 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
3099}
3100
3101// erase
3102
Martijn Velsa81fc792020-02-26 13:25:43 -05003103// 'externally instantiated' erase() implementation, called when __n != npos.
3104// Does not check __pos against size()
Howard Hinnantc51e1022010-05-11 19:42:16 +00003105template <class _CharT, class _Traits, class _Allocator>
Martijn Velsa81fc792020-02-26 13:25:43 -05003106void
3107basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(
3108 size_type __pos, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003109{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003110 if (__n)
3111 {
Martijn Velsa81fc792020-02-26 13:25:43 -05003112 size_type __sz = size();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003113 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003114 __n = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003115 size_type __n_move = __sz - __pos - __n;
3116 if (__n_move != 0)
3117 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
3118 __sz -= __n;
3119 __set_size(__sz);
3120 __invalidate_iterators_past(__sz);
3121 traits_type::assign(__p[__sz], value_type());
3122 }
Martijn Velsa81fc792020-02-26 13:25:43 -05003123}
3124
3125template <class _CharT, class _Traits, class _Allocator>
3126basic_string<_CharT, _Traits, _Allocator>&
3127basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos,
3128 size_type __n) {
3129 if (__pos > size()) this->__throw_out_of_range();
3130 if (__n == npos) {
3131 __erase_to_end(__pos);
3132 } else {
3133 __erase_external_with_move(__pos, __n);
3134 }
3135 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003136}
3137
3138template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003139inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003140typename basic_string<_CharT, _Traits, _Allocator>::iterator
3141basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
3142{
Louis Dionneba400782020-10-02 15:02:52 -04003143#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00003144 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3145 "string::erase(iterator) called with an iterator not"
3146 " referring to this string");
3147#endif
3148 _LIBCPP_ASSERT(__pos != end(),
3149 "string::erase(iterator) called with a non-dereferenceable iterator");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003150 iterator __b = begin();
3151 size_type __r = static_cast<size_type>(__pos - __b);
3152 erase(__r, 1);
Howard Hinnant28b24882011-12-01 20:21:04 +00003153 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003154}
3155
3156template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003157inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003158typename basic_string<_CharT, _Traits, _Allocator>::iterator
3159basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
3160{
Louis Dionneba400782020-10-02 15:02:52 -04003161#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00003162 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
3163 "string::erase(iterator, iterator) called with an iterator not"
3164 " referring to this string");
3165#endif
3166 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003167 iterator __b = begin();
3168 size_type __r = static_cast<size_type>(__first - __b);
3169 erase(__r, static_cast<size_type>(__last - __first));
Howard Hinnant28b24882011-12-01 20:21:04 +00003170 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003171}
3172
3173template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003174inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003175void
3176basic_string<_CharT, _Traits, _Allocator>::pop_back()
3177{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003178 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003179 size_type __sz;
3180 if (__is_long())
3181 {
3182 __sz = __get_long_size() - 1;
3183 __set_long_size(__sz);
3184 traits_type::assign(*(__get_long_pointer() + __sz), value_type());
3185 }
3186 else
3187 {
3188 __sz = __get_short_size() - 1;
3189 __set_short_size(__sz);
3190 traits_type::assign(*(__get_short_pointer() + __sz), value_type());
3191 }
3192 __invalidate_iterators_past(__sz);
3193}
3194
3195template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003196inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003197void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003198basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003199{
3200 __invalidate_all_iterators();
3201 if (__is_long())
3202 {
3203 traits_type::assign(*__get_long_pointer(), value_type());
3204 __set_long_size(0);
3205 }
3206 else
3207 {
3208 traits_type::assign(*__get_short_pointer(), value_type());
3209 __set_short_size(0);
3210 }
3211}
3212
3213template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003214inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003215void
3216basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
3217{
3218 if (__is_long())
3219 {
3220 traits_type::assign(*(__get_long_pointer() + __pos), value_type());
3221 __set_long_size(__pos);
3222 }
3223 else
3224 {
3225 traits_type::assign(*(__get_short_pointer() + __pos), value_type());
3226 __set_short_size(__pos);
3227 }
3228 __invalidate_iterators_past(__pos);
3229}
3230
3231template <class _CharT, class _Traits, class _Allocator>
3232void
3233basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
3234{
3235 size_type __sz = size();
3236 if (__n > __sz)
3237 append(__n - __sz, __c);
3238 else
3239 __erase_to_end(__n);
3240}
3241
3242template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00003243inline void
3244basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n)
3245{
3246 size_type __sz = size();
3247 if (__n > __sz) {
3248 __append_default_init(__n - __sz);
3249 } else
3250 __erase_to_end(__n);
3251}
3252
3253template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003254inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003255typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003256basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003257{
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003258 size_type __m = __alloc_traits::max_size(__alloc());
Eric Fiseliere9cc5922017-10-17 13:16:01 +00003259#ifdef _LIBCPP_BIG_ENDIAN
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003260 return (__m <= ~__long_mask ? __m : __m/2) - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003261#else
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003262 return __m - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003263#endif
3264}
3265
3266template <class _CharT, class _Traits, class _Allocator>
3267void
Marek Kurdejc9848142020-11-26 10:07:16 +01003268basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __requested_capacity)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003269{
Marek Kurdejc9848142020-11-26 10:07:16 +01003270 if (__requested_capacity > max_size())
Howard Hinnantc51e1022010-05-11 19:42:16 +00003271 this->__throw_length_error();
Marek Kurdejc9848142020-11-26 10:07:16 +01003272
3273#if _LIBCPP_STD_VER > 17
3274 // Reserve never shrinks as of C++20.
3275 if (__requested_capacity <= capacity()) return;
3276#endif
3277
3278 size_type __target_capacity = _VSTD::max(__requested_capacity, size());
3279 __target_capacity = __recommend(__target_capacity);
3280 if (__target_capacity == capacity()) return;
3281
3282 __shrink_or_extend(__target_capacity);
3283}
3284
3285template <class _CharT, class _Traits, class _Allocator>
3286void
3287basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT
3288{
3289 size_type __target_capacity = __recommend(size());
3290 if (__target_capacity == capacity()) return;
3291
3292 __shrink_or_extend(__target_capacity);
3293}
3294
3295template <class _CharT, class _Traits, class _Allocator>
3296void
3297basic_string<_CharT, _Traits, _Allocator>::__shrink_or_extend(size_type __target_capacity)
3298{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003299 size_type __cap = capacity();
3300 size_type __sz = size();
Marek Kurdejc9848142020-11-26 10:07:16 +01003301
3302 pointer __new_data, __p;
3303 bool __was_long, __now_long;
3304 if (__target_capacity == __min_cap - 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003305 {
Marek Kurdejc9848142020-11-26 10:07:16 +01003306 __was_long = true;
3307 __now_long = false;
3308 __new_data = __get_short_pointer();
3309 __p = __get_long_pointer();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003310 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003311 else
3312 {
3313 if (__target_capacity > __cap)
3314 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3315 else
3316 {
3317 #ifndef _LIBCPP_NO_EXCEPTIONS
3318 try
3319 {
3320 #endif // _LIBCPP_NO_EXCEPTIONS
3321 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3322 #ifndef _LIBCPP_NO_EXCEPTIONS
3323 }
3324 catch (...)
3325 {
3326 return;
3327 }
3328 #else // _LIBCPP_NO_EXCEPTIONS
3329 if (__new_data == nullptr)
3330 return;
3331 #endif // _LIBCPP_NO_EXCEPTIONS
3332 }
3333 __now_long = true;
3334 __was_long = __is_long();
3335 __p = __get_pointer();
3336 }
3337 traits_type::copy(_VSTD::__to_address(__new_data),
3338 _VSTD::__to_address(__p), size()+1);
3339 if (__was_long)
3340 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
3341 if (__now_long)
3342 {
3343 __set_long_cap(__target_capacity+1);
3344 __set_long_size(__sz);
3345 __set_long_pointer(__new_data);
3346 }
3347 else
3348 __set_short_size(__sz);
3349 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003350}
3351
3352template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003353inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003354typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003355basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003356{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003357 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003358 return *(data() + __pos);
3359}
3360
3361template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003362inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003363typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003364basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003365{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003366 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003367 return *(__get_pointer() + __pos);
3368}
3369
3370template <class _CharT, class _Traits, class _Allocator>
3371typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3372basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3373{
3374 if (__n >= size())
3375 this->__throw_out_of_range();
3376 return (*this)[__n];
3377}
3378
3379template <class _CharT, class _Traits, class _Allocator>
3380typename basic_string<_CharT, _Traits, _Allocator>::reference
3381basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
3382{
3383 if (__n >= size())
3384 this->__throw_out_of_range();
3385 return (*this)[__n];
3386}
3387
3388template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003389inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003390typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003391basic_string<_CharT, _Traits, _Allocator>::front() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003392{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003393 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003394 return *__get_pointer();
3395}
3396
3397template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003398inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003399typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003400basic_string<_CharT, _Traits, _Allocator>::front() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003401{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003402 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003403 return *data();
3404}
3405
3406template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003407inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003408typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003409basic_string<_CharT, _Traits, _Allocator>::back() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003410{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003411 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003412 return *(__get_pointer() + size() - 1);
3413}
3414
3415template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003416inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003417typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003418basic_string<_CharT, _Traits, _Allocator>::back() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003419{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003420 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003421 return *(data() + size() - 1);
3422}
3423
3424template <class _CharT, class _Traits, class _Allocator>
3425typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003426basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003427{
3428 size_type __sz = size();
3429 if (__pos > __sz)
3430 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003431 size_type __rlen = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003432 traits_type::copy(__s, data() + __pos, __rlen);
3433 return __rlen;
3434}
3435
3436template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003437inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003438basic_string<_CharT, _Traits, _Allocator>
3439basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3440{
3441 return basic_string(*this, __pos, __n, __alloc());
3442}
3443
3444template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003445inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003446void
3447basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003448#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00003449 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00003450#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00003451 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003452 __is_nothrow_swappable<allocator_type>::value)
3453#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003454{
Louis Dionneba400782020-10-02 15:02:52 -04003455#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00003456 if (!__is_long())
3457 __get_db()->__invalidate_all(this);
3458 if (!__str.__is_long())
3459 __get_db()->__invalidate_all(&__str);
3460 __get_db()->swap(this, &__str);
3461#endif
Eric Fiselier9bf691f2016-12-28 05:53:01 +00003462 _LIBCPP_ASSERT(
3463 __alloc_traits::propagate_on_container_swap::value ||
3464 __alloc_traits::is_always_equal::value ||
3465 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003466 _VSTD::swap(__r_.first(), __str.__r_.first());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05003467 _VSTD::__swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003468}
3469
3470// find
3471
3472template <class _Traits>
3473struct _LIBCPP_HIDDEN __traits_eq
3474{
3475 typedef typename _Traits::char_type char_type;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003476 _LIBCPP_INLINE_VISIBILITY
3477 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3478 {return _Traits::eq(__x, __y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003479};
3480
3481template<class _CharT, class _Traits, class _Allocator>
3482typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003483basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003484 size_type __pos,
3485 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003486{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003487 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003488 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003489 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003490}
3491
3492template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003493inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003494typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003495basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3496 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003497{
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(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003500}
3501
3502template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003503template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003504_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003505<
3506 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3507 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003508>
Marshall Clowe46031a2018-07-02 18:41:15 +00003509basic_string<_CharT, _Traits, _Allocator>::find(const _Tp &__t,
3510 size_type __pos) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003511{
Marshall Clowe46031a2018-07-02 18:41:15 +00003512 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003513 return __str_find<value_type, size_type, traits_type, npos>
3514 (data(), size(), __sv.data(), __pos, __sv.size());
3515}
3516
3517template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003518inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003519typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003520basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003521 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003522{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003523 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003524 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003525 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003526}
3527
3528template<class _CharT, class _Traits, class _Allocator>
3529typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003530basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3531 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003532{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003533 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003534 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003535}
3536
3537// rfind
3538
3539template<class _CharT, class _Traits, class _Allocator>
3540typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003541basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003542 size_type __pos,
3543 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003544{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003545 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003546 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003547 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003548}
3549
3550template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003551inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003552typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003553basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3554 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003555{
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(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003558}
3559
3560template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003561template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003562_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003563<
3564 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3565 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003566>
Marshall Clowe46031a2018-07-02 18:41:15 +00003567basic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t,
3568 size_type __pos) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003569{
Marshall Clowe46031a2018-07-02 18:41:15 +00003570 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003571 return __str_rfind<value_type, size_type, traits_type, npos>
3572 (data(), size(), __sv.data(), __pos, __sv.size());
3573}
3574
3575template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003576inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003577typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003578basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003579 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003580{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003581 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003582 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003583 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003584}
3585
3586template<class _CharT, class _Traits, class _Allocator>
3587typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003588basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3589 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003590{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003591 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003592 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003593}
3594
3595// find_first_of
3596
3597template<class _CharT, class _Traits, class _Allocator>
3598typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003599basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003600 size_type __pos,
3601 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003602{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003603 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003604 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003605 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003606}
3607
3608template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003609inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003610typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003611basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3612 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003613{
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(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003616}
3617
3618template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003619template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003620_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003621<
3622 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3623 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003624>
Marshall Clowe46031a2018-07-02 18:41:15 +00003625basic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t,
3626 size_type __pos) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003627{
Marshall Clowe46031a2018-07-02 18:41:15 +00003628 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003629 return __str_find_first_of<value_type, size_type, traits_type, npos>
3630 (data(), size(), __sv.data(), __pos, __sv.size());
3631}
3632
3633template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003634inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003635typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003636basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003637 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003638{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003639 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003640 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003641 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003642}
3643
3644template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003645inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003646typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003647basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3648 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003649{
3650 return find(__c, __pos);
3651}
3652
3653// find_last_of
3654
3655template<class _CharT, class _Traits, class _Allocator>
3656typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003657basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003658 size_type __pos,
3659 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003660{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003661 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003662 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003663 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003664}
3665
3666template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003667inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003668typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003669basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3670 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003671{
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(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003674}
3675
3676template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003677template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003678_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003679<
3680 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3681 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003682>
Marshall Clowe46031a2018-07-02 18:41:15 +00003683basic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t,
3684 size_type __pos) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003685{
Marshall Clowe46031a2018-07-02 18:41:15 +00003686 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003687 return __str_find_last_of<value_type, size_type, traits_type, npos>
3688 (data(), size(), __sv.data(), __pos, __sv.size());
3689}
3690
3691template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003692inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003693typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003694basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003695 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003696{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003697 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003698 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003699 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003700}
3701
3702template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003703inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003704typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003705basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3706 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003707{
3708 return rfind(__c, __pos);
3709}
3710
3711// find_first_not_of
3712
3713template<class _CharT, class _Traits, class _Allocator>
3714typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003715basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003716 size_type __pos,
3717 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003718{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003719 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003720 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003721 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003722}
3723
3724template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003725inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003726typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003727basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3728 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003729{
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(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003732}
3733
3734template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003735template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003736_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003737<
3738 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3739 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003740>
Marshall Clowe46031a2018-07-02 18:41:15 +00003741basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t,
3742 size_type __pos) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003743{
Marshall Clowe46031a2018-07-02 18:41:15 +00003744 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003745 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3746 (data(), size(), __sv.data(), __pos, __sv.size());
3747}
3748
3749template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003750inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003751typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003752basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003753 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003754{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003755 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003756 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003757 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003758}
3759
3760template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003761inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003762typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003763basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3764 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003765{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003766 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003767 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003768}
3769
3770// find_last_not_of
3771
3772template<class _CharT, class _Traits, class _Allocator>
3773typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003774basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003775 size_type __pos,
3776 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003777{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003778 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003779 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003780 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003781}
3782
3783template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003784inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003785typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003786basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3787 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003788{
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(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003791}
3792
3793template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003794template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003795_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003796<
3797 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3798 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003799>
Marshall Clowe46031a2018-07-02 18:41:15 +00003800basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t,
3801 size_type __pos) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003802{
Marshall Clowe46031a2018-07-02 18:41:15 +00003803 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003804 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3805 (data(), size(), __sv.data(), __pos, __sv.size());
3806}
3807
3808template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003809inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003810typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003811basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003812 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003813{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003814 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003815 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003816 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003817}
3818
3819template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003820inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003821typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003822basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3823 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003824{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003825 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003826 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003827}
3828
3829// compare
3830
3831template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003832template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003833_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003834<
3835 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3836 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003837>
Marshall Clowe46031a2018-07-02 18:41:15 +00003838basic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003839{
Marshall Clowe46031a2018-07-02 18:41:15 +00003840 __self_view __sv = __t;
Howard Hinnantb0485532011-07-24 21:45:06 +00003841 size_t __lhs_sz = size();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003842 size_t __rhs_sz = __sv.size();
3843 int __result = traits_type::compare(data(), __sv.data(),
Howard Hinnantb0485532011-07-24 21:45:06 +00003844 _VSTD::min(__lhs_sz, __rhs_sz));
3845 if (__result != 0)
3846 return __result;
3847 if (__lhs_sz < __rhs_sz)
3848 return -1;
3849 if (__lhs_sz > __rhs_sz)
3850 return 1;
3851 return 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003852}
3853
3854template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003855inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003856int
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003857basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003858{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003859 return compare(__self_view(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003860}
3861
3862template <class _CharT, class _Traits, class _Allocator>
3863int
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003864basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3865 size_type __n1,
Howard Hinnantd17880b2013-06-28 16:59:19 +00003866 const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003867 size_type __n2) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003868{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003869 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003870 size_type __sz = size();
3871 if (__pos1 > __sz || __n2 == npos)
3872 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003873 size_type __rlen = _VSTD::min(__n1, __sz - __pos1);
3874 int __r = traits_type::compare(data() + __pos1, __s, _VSTD::min(__rlen, __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003875 if (__r == 0)
3876 {
3877 if (__rlen < __n2)
3878 __r = -1;
3879 else if (__rlen > __n2)
3880 __r = 1;
3881 }
3882 return __r;
3883}
3884
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003885template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003886template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003887_EnableIf
Marshall Clowe46031a2018-07-02 18:41:15 +00003888<
3889 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3890 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 Clowe46031a2018-07-02 18:41:15 +00003894 const _Tp& __t) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003895{
Marshall Clowe46031a2018-07-02 18:41:15 +00003896 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003897 return compare(__pos1, __n1, __sv.data(), __sv.size());
3898}
3899
3900template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003901inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003902int
3903basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3904 size_type __n1,
3905 const basic_string& __str) const
3906{
3907 return compare(__pos1, __n1, __str.data(), __str.size());
3908}
3909
3910template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003911template <class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003912_EnableIf
Marshall Clow82513342016-09-24 22:45:42 +00003913<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003914 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
3915 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00003916 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003917>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003918basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3919 size_type __n1,
Marshall Clow82513342016-09-24 22:45:42 +00003920 const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003921 size_type __pos2,
3922 size_type __n2) const
3923{
Marshall Clow82513342016-09-24 22:45:42 +00003924 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003925 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3926}
3927
3928template <class _CharT, class _Traits, class _Allocator>
3929int
3930basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3931 size_type __n1,
3932 const basic_string& __str,
3933 size_type __pos2,
3934 size_type __n2) const
3935{
3936 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
3937}
3938
3939template <class _CharT, class _Traits, class _Allocator>
3940int
3941basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
3942{
3943 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3944 return compare(0, npos, __s, traits_type::length(__s));
3945}
3946
3947template <class _CharT, class _Traits, class _Allocator>
3948int
3949basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3950 size_type __n1,
3951 const value_type* __s) const
3952{
3953 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3954 return compare(__pos1, __n1, __s, traits_type::length(__s));
3955}
3956
Howard Hinnantc51e1022010-05-11 19:42:16 +00003957// __invariants
3958
3959template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003960inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003961bool
3962basic_string<_CharT, _Traits, _Allocator>::__invariants() const
3963{
3964 if (size() > capacity())
3965 return false;
3966 if (capacity() < __min_cap - 1)
3967 return false;
Bruce Mitchener170d8972020-11-24 12:53:53 -05003968 if (data() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003969 return false;
Louis Dionne663415f2020-10-05 16:16:13 -04003970 if (data()[size()] != value_type())
Howard Hinnantc51e1022010-05-11 19:42:16 +00003971 return false;
3972 return true;
3973}
3974
Vedant Kumar55e007e2018-03-08 21:15:26 +00003975// __clear_and_shrink
3976
3977template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003978inline
Louis Dionne173f29e2019-05-29 16:01:36 +00003979void
Marshall Clowe60a7182018-05-29 17:04:37 +00003980basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
Vedant Kumar55e007e2018-03-08 21:15:26 +00003981{
3982 clear();
3983 if(__is_long())
3984 {
3985 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
3986 __set_long_cap(0);
3987 __set_short_size(0);
Louis Dionne663415f2020-10-05 16:16:13 -04003988 traits_type::assign(*__get_short_pointer(), value_type());
Vedant Kumar55e007e2018-03-08 21:15:26 +00003989 }
Louis Dionne173f29e2019-05-29 16:01:36 +00003990}
Vedant Kumar55e007e2018-03-08 21:15:26 +00003991
Howard Hinnantc51e1022010-05-11 19:42:16 +00003992// operator==
3993
3994template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003995inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003996bool
3997operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003998 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003999{
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004000 size_t __lhs_sz = __lhs.size();
4001 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
4002 __rhs.data(),
4003 __lhs_sz) == 0;
4004}
4005
4006template<class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004007inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004008bool
4009operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
4010 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
4011{
4012 size_t __lhs_sz = __lhs.size();
4013 if (__lhs_sz != __rhs.size())
4014 return false;
4015 const char* __lp = __lhs.data();
4016 const char* __rp = __rhs.data();
4017 if (__lhs.__is_long())
4018 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
4019 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
4020 if (*__lp != *__rp)
4021 return false;
4022 return true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004023}
4024
4025template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004026inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004027bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004028operator==(const _CharT* __lhs,
4029 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004030{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004031 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4032 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
4033 size_t __lhs_len = _Traits::length(__lhs);
4034 if (__lhs_len != __rhs.size()) return false;
4035 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004036}
4037
Howard Hinnantc51e1022010-05-11 19:42:16 +00004038template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004039inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004040bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004041operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
4042 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004043{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004044 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4045 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
4046 size_t __rhs_len = _Traits::length(__rhs);
4047 if (__rhs_len != __lhs.size()) return false;
4048 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004049}
4050
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004051template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004052inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004053bool
4054operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004055 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004056{
4057 return !(__lhs == __rhs);
4058}
4059
4060template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004061inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004062bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004063operator!=(const _CharT* __lhs,
4064 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004065{
4066 return !(__lhs == __rhs);
4067}
4068
4069template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004070inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004071bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004072operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4073 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004074{
4075 return !(__lhs == __rhs);
4076}
4077
4078// operator<
4079
4080template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004081inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004082bool
4083operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004084 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004085{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004086 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004087}
4088
4089template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004090inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004091bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004092operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4093 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004094{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004095 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004096}
4097
4098template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004099inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004100bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004101operator< (const _CharT* __lhs,
4102 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004103{
4104 return __rhs.compare(__lhs) > 0;
4105}
4106
Howard Hinnantc51e1022010-05-11 19:42:16 +00004107// operator>
4108
4109template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004110inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004111bool
4112operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004113 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004114{
4115 return __rhs < __lhs;
4116}
4117
4118template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004119inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004120bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004121operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4122 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004123{
4124 return __rhs < __lhs;
4125}
4126
4127template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004128inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004129bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004130operator> (const _CharT* __lhs,
4131 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004132{
4133 return __rhs < __lhs;
4134}
4135
4136// operator<=
4137
4138template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004139inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004140bool
4141operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004142 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004143{
4144 return !(__rhs < __lhs);
4145}
4146
4147template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004148inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004149bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004150operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4151 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004152{
4153 return !(__rhs < __lhs);
4154}
4155
4156template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004157inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004158bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004159operator<=(const _CharT* __lhs,
4160 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004161{
4162 return !(__rhs < __lhs);
4163}
4164
4165// operator>=
4166
4167template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004168inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004169bool
4170operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004171 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004172{
4173 return !(__lhs < __rhs);
4174}
4175
4176template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004177inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004178bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004179operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4180 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004181{
4182 return !(__lhs < __rhs);
4183}
4184
4185template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004186inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004187bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004188operator>=(const _CharT* __lhs,
4189 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004190{
4191 return !(__lhs < __rhs);
4192}
4193
4194// operator +
4195
4196template<class _CharT, class _Traits, class _Allocator>
4197basic_string<_CharT, _Traits, _Allocator>
4198operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4199 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4200{
4201 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4202 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4203 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4204 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4205 __r.append(__rhs.data(), __rhs_sz);
4206 return __r;
4207}
4208
4209template<class _CharT, class _Traits, class _Allocator>
4210basic_string<_CharT, _Traits, _Allocator>
4211operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4212{
4213 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4214 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = _Traits::length(__lhs);
4215 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4216 __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz);
4217 __r.append(__rhs.data(), __rhs_sz);
4218 return __r;
4219}
4220
4221template<class _CharT, class _Traits, class _Allocator>
4222basic_string<_CharT, _Traits, _Allocator>
4223operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4224{
4225 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4226 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4227 __r.__init(&__lhs, 1, 1 + __rhs_sz);
4228 __r.append(__rhs.data(), __rhs_sz);
4229 return __r;
4230}
4231
4232template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004233inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004234basic_string<_CharT, _Traits, _Allocator>
4235operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
4236{
4237 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4238 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4239 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = _Traits::length(__rhs);
4240 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4241 __r.append(__rhs, __rhs_sz);
4242 return __r;
4243}
4244
4245template<class _CharT, class _Traits, class _Allocator>
4246basic_string<_CharT, _Traits, _Allocator>
4247operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
4248{
4249 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4250 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4251 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1);
4252 __r.push_back(__rhs);
4253 return __r;
4254}
4255
Eric Fiselierfc92be82017-04-19 00:28:44 +00004256#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004257
4258template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004259inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004260basic_string<_CharT, _Traits, _Allocator>
4261operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4262{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004263 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004264}
4265
4266template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004267inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004268basic_string<_CharT, _Traits, _Allocator>
4269operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4270{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004271 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004272}
4273
4274template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004275inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004276basic_string<_CharT, _Traits, _Allocator>
4277operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4278{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004279 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004280}
4281
4282template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004283inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004284basic_string<_CharT, _Traits, _Allocator>
4285operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4286{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004287 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004288}
4289
4290template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004291inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004292basic_string<_CharT, _Traits, _Allocator>
4293operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4294{
4295 __rhs.insert(__rhs.begin(), __lhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004296 return _VSTD::move(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004297}
4298
4299template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004300inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004301basic_string<_CharT, _Traits, _Allocator>
4302operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
4303{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004304 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004305}
4306
4307template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004308inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004309basic_string<_CharT, _Traits, _Allocator>
4310operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
4311{
4312 __lhs.push_back(__rhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004313 return _VSTD::move(__lhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004314}
4315
Eric Fiselierfc92be82017-04-19 00:28:44 +00004316#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004317
4318// swap
4319
4320template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004321inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004322void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004323swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +00004324 basic_string<_CharT, _Traits, _Allocator>& __rhs)
4325 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004326{
4327 __lhs.swap(__rhs);
4328}
4329
Richard Smithbf0d2662020-12-08 00:42:26 -08004330#ifndef _LIBCPP_NO_HAS_CHAR8_T
4331typedef basic_string<char8_t> u8string;
4332#endif
4333
4334#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
4335typedef basic_string<char16_t> u16string;
4336typedef basic_string<char32_t> u32string;
4337#endif // _LIBCPP_HAS_NO_UNICODE_CHARS
4338
Bruce Mitchener170d8972020-11-24 12:53:53 -05004339_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = nullptr, int __base = 10);
4340_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = nullptr, int __base = 10);
4341_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = nullptr, int __base = 10);
4342_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = nullptr, int __base = 10);
4343_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004344
Bruce Mitchener170d8972020-11-24 12:53:53 -05004345_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = nullptr);
4346_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = nullptr);
4347_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004348
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004349_LIBCPP_FUNC_VIS string to_string(int __val);
4350_LIBCPP_FUNC_VIS string to_string(unsigned __val);
4351_LIBCPP_FUNC_VIS string to_string(long __val);
4352_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
4353_LIBCPP_FUNC_VIS string to_string(long long __val);
4354_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
4355_LIBCPP_FUNC_VIS string to_string(float __val);
4356_LIBCPP_FUNC_VIS string to_string(double __val);
4357_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004358
Bruce Mitchener170d8972020-11-24 12:53:53 -05004359_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4360_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4361_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4362_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4363_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004364
Bruce Mitchener170d8972020-11-24 12:53:53 -05004365_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = nullptr);
4366_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = nullptr);
4367_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004368
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004369_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
4370_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
4371_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
4372_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
4373_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
4374_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
4375_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
4376_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
4377_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004378
Howard Hinnantc51e1022010-05-11 19:42:16 +00004379template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierc9fdaf12020-01-15 17:02:17 -05004380_LIBCPP_FUNC_VIS
4381const typename basic_string<_CharT, _Traits, _Allocator>::size_type
4382 basic_string<_CharT, _Traits, _Allocator>::npos;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004383
Marshall Clow851b9ec2019-05-20 21:56:51 +00004384template <class _CharT, class _Allocator>
4385struct _LIBCPP_TEMPLATE_VIS
4386 hash<basic_string<_CharT, char_traits<_CharT>, _Allocator> >
4387 : public unary_function<
4388 basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004389{
4390 size_t
Marshall Clow851b9ec2019-05-20 21:56:51 +00004391 operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT
4392 { return __do_string_hash(__val.data(), __val.data() + __val.size()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004393};
4394
Howard Hinnantc51e1022010-05-11 19:42:16 +00004395
Howard Hinnantdc095972011-07-18 15:51:59 +00004396template<class _CharT, class _Traits, class _Allocator>
4397basic_ostream<_CharT, _Traits>&
4398operator<<(basic_ostream<_CharT, _Traits>& __os,
4399 const basic_string<_CharT, _Traits, _Allocator>& __str);
4400
4401template<class _CharT, class _Traits, class _Allocator>
4402basic_istream<_CharT, _Traits>&
4403operator>>(basic_istream<_CharT, _Traits>& __is,
4404 basic_string<_CharT, _Traits, _Allocator>& __str);
4405
4406template<class _CharT, class _Traits, class _Allocator>
4407basic_istream<_CharT, _Traits>&
4408getline(basic_istream<_CharT, _Traits>& __is,
4409 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4410
4411template<class _CharT, class _Traits, class _Allocator>
4412inline _LIBCPP_INLINE_VISIBILITY
4413basic_istream<_CharT, _Traits>&
4414getline(basic_istream<_CharT, _Traits>& __is,
4415 basic_string<_CharT, _Traits, _Allocator>& __str);
4416
Eric Fiselierfc92be82017-04-19 00:28:44 +00004417#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantdc095972011-07-18 15:51:59 +00004418
4419template<class _CharT, class _Traits, class _Allocator>
4420inline _LIBCPP_INLINE_VISIBILITY
4421basic_istream<_CharT, _Traits>&
4422getline(basic_istream<_CharT, _Traits>&& __is,
4423 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4424
4425template<class _CharT, class _Traits, class _Allocator>
4426inline _LIBCPP_INLINE_VISIBILITY
4427basic_istream<_CharT, _Traits>&
4428getline(basic_istream<_CharT, _Traits>&& __is,
4429 basic_string<_CharT, _Traits, _Allocator>& __str);
4430
Eric Fiselierfc92be82017-04-19 00:28:44 +00004431#endif // _LIBCPP_CXX03_LANG
Howard Hinnantdc095972011-07-18 15:51:59 +00004432
Marshall Clow29b53f22018-12-14 18:49:35 +00004433#if _LIBCPP_STD_VER > 17
Marek Kurdeja98b1412020-05-02 13:58:03 +02004434template <class _CharT, class _Traits, class _Allocator, class _Up>
Marshall Clow29b53f22018-12-14 18:49:35 +00004435inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004436 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4437 erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
4438 auto __old_size = __str.size();
4439 __str.erase(_VSTD::remove(__str.begin(), __str.end(), __v), __str.end());
4440 return __old_size - __str.size();
4441}
Marshall Clow29b53f22018-12-14 18:49:35 +00004442
Marek Kurdeja98b1412020-05-02 13:58:03 +02004443template <class _CharT, class _Traits, class _Allocator, class _Predicate>
Marshall Clow29b53f22018-12-14 18:49:35 +00004444inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004445 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4446 erase_if(basic_string<_CharT, _Traits, _Allocator>& __str,
4447 _Predicate __pred) {
4448 auto __old_size = __str.size();
4449 __str.erase(_VSTD::remove_if(__str.begin(), __str.end(), __pred),
4450 __str.end());
4451 return __old_size - __str.size();
4452}
Marshall Clow29b53f22018-12-14 18:49:35 +00004453#endif
4454
Louis Dionneba400782020-10-02 15:02:52 -04004455#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004456
4457template<class _CharT, class _Traits, class _Allocator>
4458bool
4459basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4460{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004461 return this->data() <= _VSTD::__to_address(__i->base()) &&
4462 _VSTD::__to_address(__i->base()) < this->data() + this->size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004463}
4464
4465template<class _CharT, class _Traits, class _Allocator>
4466bool
4467basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4468{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004469 return this->data() < _VSTD::__to_address(__i->base()) &&
4470 _VSTD::__to_address(__i->base()) <= this->data() + this->size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004471}
4472
4473template<class _CharT, class _Traits, class _Allocator>
4474bool
4475basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4476{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004477 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Howard Hinnant8ea98242013-08-23 17:37:05 +00004478 return this->data() <= __p && __p <= this->data() + this->size();
4479}
4480
4481template<class _CharT, class _Traits, class _Allocator>
4482bool
4483basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4484{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004485 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Howard Hinnant8ea98242013-08-23 17:37:05 +00004486 return this->data() <= __p && __p < this->data() + this->size();
4487}
4488
Louis Dionneba400782020-10-02 15:02:52 -04004489#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004490
Louis Dionne173f29e2019-05-29 16:01:36 +00004491#if _LIBCPP_STD_VER > 11
Marshall Clowcba751f2013-07-23 17:05:24 +00004492// Literal suffixes for basic_string [basic.string.literals]
Marshall Clowac868372013-10-05 21:18:32 +00004493inline namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004494{
4495 inline namespace string_literals
4496 {
Howard Hinnant5c167562013-08-07 19:39:48 +00004497 inline _LIBCPP_INLINE_VISIBILITY
4498 basic_string<char> operator "" s( const char *__str, size_t __len )
4499 {
4500 return basic_string<char> (__str, __len);
4501 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004502
Howard Hinnant5c167562013-08-07 19:39:48 +00004503 inline _LIBCPP_INLINE_VISIBILITY
4504 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4505 {
4506 return basic_string<wchar_t> (__str, __len);
4507 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004508
Marshall Clow8732fed2018-12-11 04:35:44 +00004509#ifndef _LIBCPP_NO_HAS_CHAR8_T
4510 inline _LIBCPP_INLINE_VISIBILITY
4511 basic_string<char8_t> operator "" s(const char8_t *__str, size_t __len) _NOEXCEPT
4512 {
4513 return basic_string<char8_t> (__str, __len);
4514 }
4515#endif
4516
Howard Hinnant5c167562013-08-07 19:39:48 +00004517 inline _LIBCPP_INLINE_VISIBILITY
4518 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4519 {
4520 return basic_string<char16_t> (__str, __len);
4521 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004522
Howard Hinnant5c167562013-08-07 19:39:48 +00004523 inline _LIBCPP_INLINE_VISIBILITY
4524 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4525 {
4526 return basic_string<char32_t> (__str, __len);
4527 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004528 }
4529}
4530#endif
4531
Howard Hinnantc51e1022010-05-11 19:42:16 +00004532_LIBCPP_END_NAMESPACE_STD
4533
Eric Fiselierf4433a32017-05-31 22:07:49 +00004534_LIBCPP_POP_MACROS
4535
Howard Hinnantc51e1022010-05-11 19:42:16 +00004536#endif // _LIBCPP_STRING