blob: daa6a68b230cb626a5463ad629e3ce7aba7448b6 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
Louis Dionne9bd93882021-11-17 16:25:01 -05002//===----------------------------------------------------------------------===//
Howard Hinnantc51e1022010-05-11 19:42:16 +00003//
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>;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +010072template <> struct char_traits<char8_t>; // C++20
73template <> struct char_traits<char16_t>;
74template <> struct char_traits<char32_t>;
Howard Hinnantc51e1022010-05-11 19:42:16 +000075
Howard Hinnant3b6579a2010-08-22 00:02:43 +000076template<class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
Howard Hinnantc51e1022010-05-11 19:42:16 +000077class basic_string
78{
Howard Hinnant3b6579a2010-08-22 00:02:43 +000079public:
80// types:
Howard Hinnantc51e1022010-05-11 19:42:16 +000081 typedef traits traits_type;
82 typedef typename traits_type::char_type value_type;
83 typedef Allocator allocator_type;
84 typedef typename allocator_type::size_type size_type;
85 typedef typename allocator_type::difference_type difference_type;
86 typedef typename allocator_type::reference reference;
87 typedef typename allocator_type::const_reference const_reference;
88 typedef typename allocator_type::pointer pointer;
89 typedef typename allocator_type::const_pointer const_pointer;
90 typedef implementation-defined iterator;
91 typedef implementation-defined const_iterator;
92 typedef std::reverse_iterator<iterator> reverse_iterator;
93 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
94
95 static const size_type npos = -1;
96
Howard Hinnant3e276872011-06-03 18:40:47 +000097 basic_string()
98 noexcept(is_nothrow_default_constructible<allocator_type>::value);
99 explicit basic_string(const allocator_type& a);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000100 basic_string(const basic_string& str);
Howard Hinnant3e276872011-06-03 18:40:47 +0000101 basic_string(basic_string&& str)
102 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clow83445802016-04-07 18:13:41 +0000103 basic_string(const basic_string& str, size_type pos,
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000104 const allocator_type& a = allocator_type());
Marshall Clow83445802016-04-07 18:13:41 +0000105 basic_string(const basic_string& str, size_type pos, size_type n,
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000106 const Allocator& a = Allocator());
Marshall Clow78dbe462016-11-14 18:22:19 +0000107 template<class T>
108 basic_string(const T& t, size_type pos, size_type n, const Allocator& a = Allocator()); // C++17
Marshall Clowe46031a2018-07-02 18:41:15 +0000109 template <class T>
110 explicit basic_string(const T& t, const Allocator& a = Allocator()); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000111 basic_string(const value_type* s, const allocator_type& a = allocator_type());
112 basic_string(const value_type* s, size_type n, const allocator_type& a = allocator_type());
Marek Kurdej90d79712021-07-27 16:16:21 +0200113 basic_string(nullptr_t) = delete; // C++2b
Howard Hinnantc51e1022010-05-11 19:42:16 +0000114 basic_string(size_type n, value_type c, const allocator_type& a = allocator_type());
115 template<class InputIterator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000116 basic_string(InputIterator begin, InputIterator end,
117 const allocator_type& a = allocator_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000118 basic_string(initializer_list<value_type>, const Allocator& = Allocator());
119 basic_string(const basic_string&, const Allocator&);
120 basic_string(basic_string&&, const Allocator&);
121
122 ~basic_string();
123
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000124 operator basic_string_view<charT, traits>() const noexcept;
125
Howard Hinnantc51e1022010-05-11 19:42:16 +0000126 basic_string& operator=(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000127 template <class T>
128 basic_string& operator=(const T& t); // C++17
Howard Hinnant3e276872011-06-03 18:40:47 +0000129 basic_string& operator=(basic_string&& str)
130 noexcept(
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000131 allocator_type::propagate_on_container_move_assignment::value ||
132 allocator_type::is_always_equal::value ); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000133 basic_string& operator=(const value_type* s);
Marek Kurdej90d79712021-07-27 16:16:21 +0200134 basic_string& operator=(nullptr_t) = delete; // C++2b
Howard Hinnantc51e1022010-05-11 19:42:16 +0000135 basic_string& operator=(value_type c);
136 basic_string& operator=(initializer_list<value_type>);
137
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000138 iterator begin() noexcept;
139 const_iterator begin() const noexcept;
140 iterator end() noexcept;
141 const_iterator end() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000142
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000143 reverse_iterator rbegin() noexcept;
144 const_reverse_iterator rbegin() const noexcept;
145 reverse_iterator rend() noexcept;
146 const_reverse_iterator rend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000147
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000148 const_iterator cbegin() const noexcept;
149 const_iterator cend() const noexcept;
150 const_reverse_iterator crbegin() const noexcept;
151 const_reverse_iterator crend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000152
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000153 size_type size() const noexcept;
154 size_type length() const noexcept;
155 size_type max_size() const noexcept;
156 size_type capacity() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000157
158 void resize(size_type n, value_type c);
159 void resize(size_type n);
160
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100161 template<class Operation>
162 constexpr void resize_and_overwrite(size_type n, Operation op); // since C++23
163
Marek Kurdejc9848142020-11-26 10:07:16 +0100164 void reserve(size_type res_arg);
165 void reserve(); // deprecated in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000166 void shrink_to_fit();
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000167 void clear() noexcept;
168 bool empty() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000169
170 const_reference operator[](size_type pos) const;
171 reference operator[](size_type pos);
172
173 const_reference at(size_type n) const;
174 reference at(size_type n);
175
176 basic_string& operator+=(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000177 template <class T>
178 basic_string& operator+=(const T& t); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000179 basic_string& operator+=(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000180 basic_string& operator+=(value_type c);
181 basic_string& operator+=(initializer_list<value_type>);
182
183 basic_string& append(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000184 template <class T>
185 basic_string& append(const T& t); // C++17
Marshall Clow8db7fb02014-03-04 19:17:19 +0000186 basic_string& append(const basic_string& str, size_type pos, size_type n=npos); //C++14
Marshall Clow82513342016-09-24 22:45:42 +0000187 template <class T>
188 basic_string& append(const T& t, size_type pos, size_type n=npos); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000189 basic_string& append(const value_type* s, size_type n);
190 basic_string& append(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000191 basic_string& append(size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000192 template<class InputIterator>
193 basic_string& append(InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000194 basic_string& append(initializer_list<value_type>);
195
196 void push_back(value_type c);
197 void pop_back();
198 reference front();
199 const_reference front() const;
200 reference back();
201 const_reference back() const;
202
203 basic_string& assign(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000204 template <class T>
205 basic_string& assign(const T& t); // C++17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000206 basic_string& assign(basic_string&& str);
Marshall Clow8db7fb02014-03-04 19:17:19 +0000207 basic_string& assign(const basic_string& str, size_type pos, size_type n=npos); // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000208 template <class T>
209 basic_string& assign(const T& t, size_type pos, size_type n=npos); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000210 basic_string& assign(const value_type* s, size_type n);
211 basic_string& assign(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000212 basic_string& assign(size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000213 template<class InputIterator>
214 basic_string& assign(InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000215 basic_string& assign(initializer_list<value_type>);
216
217 basic_string& insert(size_type pos1, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000218 template <class T>
219 basic_string& insert(size_type pos1, const T& t);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000220 basic_string& insert(size_type pos1, const basic_string& str,
221 size_type pos2, size_type n);
Marshall Clow82513342016-09-24 22:45:42 +0000222 template <class T>
223 basic_string& insert(size_type pos1, const T& t, size_type pos2, size_type n); // C++17
Marshall Clow8db7fb02014-03-04 19:17:19 +0000224 basic_string& insert(size_type pos, const value_type* s, size_type n=npos); //C++14
Howard Hinnantd17880b2013-06-28 16:59:19 +0000225 basic_string& insert(size_type pos, const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000226 basic_string& insert(size_type pos, size_type n, value_type c);
227 iterator insert(const_iterator p, value_type c);
228 iterator insert(const_iterator p, size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000229 template<class InputIterator>
230 iterator insert(const_iterator p, InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000231 iterator insert(const_iterator p, initializer_list<value_type>);
232
233 basic_string& erase(size_type pos = 0, size_type n = npos);
234 iterator erase(const_iterator position);
235 iterator erase(const_iterator first, const_iterator last);
236
237 basic_string& replace(size_type pos1, size_type n1, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000238 template <class T>
239 basic_string& replace(size_type pos1, size_type n1, const T& t); // C++17
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000240 basic_string& replace(size_type pos1, size_type n1, const basic_string& str,
Marshall Clow8db7fb02014-03-04 19:17:19 +0000241 size_type pos2, size_type n2=npos); // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000242 template <class T>
243 basic_string& replace(size_type pos1, size_type n1, const T& t,
244 size_type pos2, size_type n); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000245 basic_string& replace(size_type pos, size_type n1, const value_type* s, size_type n2);
246 basic_string& replace(size_type pos, size_type n1, const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000247 basic_string& replace(size_type pos, size_type n1, size_type n2, value_type c);
Howard Hinnant990d6e82010-11-17 21:11:40 +0000248 basic_string& replace(const_iterator i1, const_iterator i2, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000249 template <class T>
250 basic_string& replace(const_iterator i1, const_iterator i2, const T& t); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000251 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s, size_type n);
252 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s);
Howard Hinnant990d6e82010-11-17 21:11:40 +0000253 basic_string& replace(const_iterator i1, const_iterator i2, size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000254 template<class InputIterator>
Howard Hinnant990d6e82010-11-17 21:11:40 +0000255 basic_string& replace(const_iterator i1, const_iterator i2, InputIterator j1, InputIterator j2);
256 basic_string& replace(const_iterator i1, const_iterator i2, initializer_list<value_type>);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000257
Howard Hinnantd17880b2013-06-28 16:59:19 +0000258 size_type copy(value_type* s, size_type n, size_type pos = 0) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000259 basic_string substr(size_type pos = 0, size_type n = npos) const;
260
Howard Hinnant3e276872011-06-03 18:40:47 +0000261 void swap(basic_string& str)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000262 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
263 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000264
Howard Hinnantd17880b2013-06-28 16:59:19 +0000265 const value_type* c_str() const noexcept;
266 const value_type* data() const noexcept;
Marshall Clowd3c22392016-03-08 15:44:30 +0000267 value_type* data() noexcept; // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000268
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000269 allocator_type get_allocator() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000270
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000271 size_type find(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000272 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800273 size_type find(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000274 size_type find(const value_type* s, size_type pos, size_type n) const noexcept;
275 size_type find(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000276 size_type find(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000277
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000278 size_type rfind(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000279 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800280 size_type rfind(const T& t, size_type pos = npos) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000281 size_type rfind(const value_type* s, size_type pos, size_type n) const noexcept;
282 size_type rfind(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000283 size_type rfind(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000284
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000285 size_type find_first_of(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000286 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800287 size_type find_first_of(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000288 size_type find_first_of(const value_type* s, size_type pos, size_type n) const noexcept;
289 size_type find_first_of(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000290 size_type find_first_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000291
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000292 size_type find_last_of(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000293 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800294 size_type find_last_of(const T& t, size_type pos = npos) const noexcept noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000295 size_type find_last_of(const value_type* s, size_type pos, size_type n) const noexcept;
296 size_type find_last_of(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000297 size_type find_last_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000298
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000299 size_type find_first_not_of(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000300 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800301 size_type find_first_not_of(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000302 size_type find_first_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
303 size_type find_first_not_of(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000304 size_type find_first_not_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000305
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000306 size_type find_last_not_of(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000307 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800308 size_type find_last_not_of(const T& t, size_type pos = npos) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000309 size_type find_last_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
310 size_type find_last_not_of(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000311 size_type find_last_not_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000312
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000313 int compare(const basic_string& str) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000314 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800315 int compare(const T& t) const noexcept; // C++17, noexcept as an extension
Howard Hinnantc51e1022010-05-11 19:42:16 +0000316 int compare(size_type pos1, size_type n1, const basic_string& str) const;
Marshall Clowe46031a2018-07-02 18:41:15 +0000317 template <class T>
318 int compare(size_type pos1, size_type n1, const T& t) const; // C++17
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000319 int compare(size_type pos1, size_type n1, const basic_string& str,
Marshall Clow8db7fb02014-03-04 19:17:19 +0000320 size_type pos2, size_type n2=npos) const; // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000321 template <class T>
322 int compare(size_type pos1, size_type n1, const T& t,
323 size_type pos2, size_type n2=npos) const; // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000324 int compare(const value_type* s) const noexcept;
325 int compare(size_type pos1, size_type n1, const value_type* s) const;
326 int compare(size_type pos1, size_type n1, const value_type* s, size_type n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000327
Marek Kurdej24b4c512021-01-07 12:29:04 +0100328 bool starts_with(basic_string_view<charT, traits> sv) const noexcept; // C++20
329 bool starts_with(charT c) const noexcept; // C++20
330 bool starts_with(const charT* s) const; // C++20
331 bool ends_with(basic_string_view<charT, traits> sv) const noexcept; // C++20
332 bool ends_with(charT c) const noexcept; // C++20
333 bool ends_with(const charT* s) const; // C++20
Marshall Clow18c293b2017-12-04 20:11:38 +0000334
Wim Leflere023c3542021-01-19 14:33:30 -0500335 constexpr bool contains(basic_string_view<charT, traits> sv) const noexcept; // C++2b
336 constexpr bool contains(charT c) const noexcept; // C++2b
337 constexpr bool contains(const charT* s) const; // C++2b
338
Howard Hinnantc51e1022010-05-11 19:42:16 +0000339 bool __invariants() const;
340};
341
Marshall Clowa0563332018-02-08 06:34:03 +0000342template<class InputIterator,
343 class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
344basic_string(InputIterator, InputIterator, Allocator = Allocator())
345 -> basic_string<typename iterator_traits<InputIterator>::value_type,
346 char_traits<typename iterator_traits<InputIterator>::value_type>,
347 Allocator>; // C++17
348
Howard Hinnantc51e1022010-05-11 19:42:16 +0000349template<class charT, class traits, class Allocator>
350basic_string<charT, traits, Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000351operator+(const basic_string<charT, traits, Allocator>& lhs,
352 const basic_string<charT, traits, Allocator>& rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000353
354template<class charT, class traits, class Allocator>
355basic_string<charT, traits, Allocator>
356operator+(const charT* lhs , const basic_string<charT,traits,Allocator>&rhs);
357
358template<class charT, class traits, class Allocator>
359basic_string<charT, traits, Allocator>
360operator+(charT lhs, const basic_string<charT,traits,Allocator>& rhs);
361
362template<class charT, class traits, class Allocator>
363basic_string<charT, traits, Allocator>
364operator+(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs);
365
366template<class charT, class traits, class Allocator>
367basic_string<charT, traits, Allocator>
368operator+(const basic_string<charT, traits, Allocator>& lhs, charT rhs);
369
370template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000371bool operator==(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000372 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000373
374template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000375bool operator==(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000376
377template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000378bool operator==(const basic_string<charT,traits,Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000379
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000380template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000381bool operator!=(const basic_string<charT,traits,Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000382 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000383
384template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000385bool operator!=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000386
387template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000388bool operator!=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000389
390template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000391bool operator< (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000392 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000393
394template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000395bool operator< (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000396
397template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000398bool operator< (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000399
400template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000401bool operator> (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000402 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000403
404template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000405bool operator> (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000406
407template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000408bool operator> (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000409
410template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000411bool operator<=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000412 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000413
414template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000415bool operator<=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000416
417template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000418bool operator<=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000419
420template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000421bool operator>=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000422 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000423
424template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000425bool operator>=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000426
427template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000428bool operator>=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000429
430template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000431void swap(basic_string<charT, traits, Allocator>& lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +0000432 basic_string<charT, traits, Allocator>& rhs)
433 noexcept(noexcept(lhs.swap(rhs)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000434
435template<class charT, class traits, class Allocator>
436basic_istream<charT, traits>&
437operator>>(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
438
439template<class charT, class traits, class Allocator>
440basic_ostream<charT, traits>&
441operator<<(basic_ostream<charT, traits>& os, const basic_string<charT, traits, Allocator>& str);
442
443template<class charT, class traits, class Allocator>
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000444basic_istream<charT, traits>&
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000445getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str,
446 charT delim);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000447
448template<class charT, class traits, class Allocator>
449basic_istream<charT, traits>&
450getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
451
Marshall Clow29b53f22018-12-14 18:49:35 +0000452template<class charT, class traits, class Allocator, class U>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200453typename basic_string<charT, traits, Allocator>::size_type
454erase(basic_string<charT, traits, Allocator>& c, const U& value); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000455template<class charT, class traits, class Allocator, class Predicate>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200456typename basic_string<charT, traits, Allocator>::size_type
457erase_if(basic_string<charT, traits, Allocator>& c, Predicate pred); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000458
Howard Hinnantc51e1022010-05-11 19:42:16 +0000459typedef basic_string<char> string;
460typedef basic_string<wchar_t> wstring;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100461typedef basic_string<char8_t> u8string; // C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000462typedef basic_string<char16_t> u16string;
463typedef basic_string<char32_t> u32string;
464
Bruce Mitchener170d8972020-11-24 12:53:53 -0500465int stoi (const string& str, size_t* idx = nullptr, int base = 10);
466long stol (const string& str, size_t* idx = nullptr, int base = 10);
467unsigned long stoul (const string& str, size_t* idx = nullptr, int base = 10);
468long long stoll (const string& str, size_t* idx = nullptr, int base = 10);
469unsigned long long stoull(const string& str, size_t* idx = nullptr, int base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000470
Bruce Mitchener170d8972020-11-24 12:53:53 -0500471float stof (const string& str, size_t* idx = nullptr);
472double stod (const string& str, size_t* idx = nullptr);
473long double stold(const string& str, size_t* idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000474
475string to_string(int val);
476string to_string(unsigned val);
477string to_string(long val);
478string to_string(unsigned long val);
479string to_string(long long val);
480string to_string(unsigned long long val);
481string to_string(float val);
482string to_string(double val);
483string to_string(long double val);
484
Bruce Mitchener170d8972020-11-24 12:53:53 -0500485int stoi (const wstring& str, size_t* idx = nullptr, int base = 10);
486long stol (const wstring& str, size_t* idx = nullptr, int base = 10);
487unsigned long stoul (const wstring& str, size_t* idx = nullptr, int base = 10);
488long long stoll (const wstring& str, size_t* idx = nullptr, int base = 10);
489unsigned long long stoull(const wstring& str, size_t* idx = nullptr, int base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000490
Bruce Mitchener170d8972020-11-24 12:53:53 -0500491float stof (const wstring& str, size_t* idx = nullptr);
492double stod (const wstring& str, size_t* idx = nullptr);
493long double stold(const wstring& str, size_t* idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000494
495wstring to_wstring(int val);
496wstring to_wstring(unsigned val);
497wstring to_wstring(long val);
498wstring to_wstring(unsigned long val);
499wstring to_wstring(long long val);
500wstring to_wstring(unsigned long long val);
501wstring to_wstring(float val);
502wstring to_wstring(double val);
503wstring to_wstring(long double val);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000504
505template <> struct hash<string>;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100506template <> struct hash<u8string>; // C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000507template <> struct hash<u16string>;
508template <> struct hash<u32string>;
509template <> struct hash<wstring>;
510
Marshall Clowcba751f2013-07-23 17:05:24 +0000511basic_string<char> operator "" s( const char *str, size_t len ); // C++14
512basic_string<wchar_t> operator "" s( const wchar_t *str, size_t len ); // C++14
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100513basic_string<char8_t> operator "" s( const char8_t *str, size_t len ); // C++20
Marshall Clowcba751f2013-07-23 17:05:24 +0000514basic_string<char16_t> operator "" s( const char16_t *str, size_t len ); // C++14
515basic_string<char32_t> operator "" s( const char32_t *str, size_t len ); // C++14
516
Howard Hinnantc51e1022010-05-11 19:42:16 +0000517} // std
518
519*/
520
Nikolas Klauserf210d8a2022-02-15 18:18:08 +0100521#include <__algorithm/max.h>
522#include <__algorithm/min.h>
523#include <__algorithm/remove.h>
524#include <__algorithm/remove_if.h>
Louis Dionneb4fce352022-03-25 12:55:36 -0400525#include <__assert> // all public C++ headers provide the assertion handler
Howard Hinnantc51e1022010-05-11 19:42:16 +0000526#include <__config>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400527#include <__debug>
Mark de Weverdf3e0d42021-09-26 15:47:42 +0200528#include <__format/enable_insertable.h>
Nikolas Klauser80840272022-02-04 13:04:33 +0100529#include <__ios/fpos.h>
Louis Dionne77249522021-06-11 09:55:11 -0400530#include <__iterator/wrap_iter.h>
Nikolas Klauserc513eba2022-04-09 09:41:19 +0200531#include <__memory/allocate_at_least.h>
Nikolas Klauser8fccd622022-03-05 19:17:07 +0100532#include <__utility/auto_cast.h>
533#include <__utility/move.h>
534#include <__utility/swap.h>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400535#include <compare>
536#include <cstdio> // EOF
Louis Dionned24191c2021-08-19 12:39:16 -0400537#include <cstdlib>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400538#include <cstring>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400539#include <initializer_list>
540#include <iosfwd>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000541#include <iterator>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000542#include <memory>
543#include <stdexcept>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400544#include <string_view>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000545#include <type_traits>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000546#include <version>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000547
Nikolas Klauser4c1bf592022-02-11 19:15:18 +0100548// TODO: remove these headers
549#include <__functional/binary_function.h>
550#include <__functional/invoke.h>
551#include <__functional/operations.h>
552#include <__functional/reference_wrapper.h>
553#include <__functional/unary_function.h>
554#include <__functional/weak_result_type.h>
555#include <new>
556#include <typeinfo>
557
Louis Dionne89258142021-08-23 15:32:36 -0400558#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Nikolas Klauser80840272022-02-04 13:04:33 +0100559# include <cwchar>
Louis Dionne89258142021-08-23 15:32:36 -0400560#endif
561
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400562#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
Nikolas Klauser80840272022-02-04 13:04:33 +0100563# include <cstdint>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400564#endif
Eric Fiselier14b6de92014-08-10 23:53:08 +0000565
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000566#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Arthur O'Dwyer6eeaa002022-02-01 20:16:40 -0500567# pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000568#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000569
Eric Fiselierf4433a32017-05-31 22:07:49 +0000570_LIBCPP_PUSH_MACROS
571#include <__undef_macros>
572
573
Howard Hinnantc51e1022010-05-11 19:42:16 +0000574_LIBCPP_BEGIN_NAMESPACE_STD
575
Howard Hinnantc51e1022010-05-11 19:42:16 +0000576// basic_string
577
578template<class _CharT, class _Traits, class _Allocator>
579basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000580operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
581 const basic_string<_CharT, _Traits, _Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000582
583template<class _CharT, class _Traits, class _Allocator>
584basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000585operator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000586
587template<class _CharT, class _Traits, class _Allocator>
588basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000589operator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000590
591template<class _CharT, class _Traits, class _Allocator>
Louis Dionne8f8d95d2018-11-21 17:31:55 +0000592inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000593basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000594operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000595
596template<class _CharT, class _Traits, class _Allocator>
597basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000598operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000599
Shoaib Meenaiea363712017-07-29 02:54:41 +0000600_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&))
601
Marshall Clow039b2f02016-01-13 21:54:34 +0000602template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400603struct __string_is_trivial_iterator : public false_type {};
604
605template <class _Tp>
606struct __string_is_trivial_iterator<_Tp*>
607 : public is_arithmetic<_Tp> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000608
Louis Dionne173f29e2019-05-29 16:01:36 +0000609template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400610struct __string_is_trivial_iterator<__wrap_iter<_Iter> >
611 : public __string_is_trivial_iterator<_Iter> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000612
Marshall Clow82513342016-09-24 22:45:42 +0000613template <class _CharT, class _Traits, class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500614struct __can_be_converted_to_string_view : public _BoolConstant<
615 is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
616 !is_convertible<const _Tp&, const _CharT*>::value
617 > {};
Marshall Clow82513342016-09-24 22:45:42 +0000618
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400619#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800620typedef basic_string<char8_t> u8string;
621#endif
622
623#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
624typedef basic_string<char16_t> u16string;
625typedef basic_string<char32_t> u32string;
Louis Dionne96fc5f52021-09-09 11:25:10 -0400626#endif
Richard Smith256954d2020-11-11 17:12:18 -0800627
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000628template<class _CharT, class _Traits, class _Allocator>
Richard Smith256954d2020-11-11 17:12:18 -0800629class
630 _LIBCPP_TEMPLATE_VIS
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400631#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800632 _LIBCPP_PREFERRED_NAME(u8string)
633#endif
634#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
635 _LIBCPP_PREFERRED_NAME(u16string)
636 _LIBCPP_PREFERRED_NAME(u32string)
637#endif
638 basic_string
Howard Hinnantc51e1022010-05-11 19:42:16 +0000639{
640public:
641 typedef basic_string __self;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000642 typedef basic_string_view<_CharT, _Traits> __self_view;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000643 typedef _Traits traits_type;
Marshall Clowa3a74e02017-03-15 18:41:11 +0000644 typedef _CharT value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000645 typedef _Allocator allocator_type;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000646 typedef allocator_traits<allocator_type> __alloc_traits;
647 typedef typename __alloc_traits::size_type size_type;
648 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant3e276872011-06-03 18:40:47 +0000649 typedef value_type& reference;
650 typedef const value_type& const_reference;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000651 typedef typename __alloc_traits::pointer pointer;
652 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000653
Marshall Clow79f33542018-03-21 00:36:05 +0000654 static_assert((!is_array<value_type>::value), "Character type of basic_string must not be an array");
655 static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string must be standard-layout");
656 static_assert(( is_trivial<value_type>::value), "Character type of basic_string must be trivial");
657 static_assert(( is_same<_CharT, typename traits_type::char_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000658 "traits_type::char_type must be the same type as CharT");
Marshall Clow79f33542018-03-21 00:36:05 +0000659 static_assert(( is_same<typename allocator_type::value_type, value_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000660 "Allocator::value_type must be same type as value_type");
Marshall Clowe46031a2018-07-02 18:41:15 +0000661
Howard Hinnantc51e1022010-05-11 19:42:16 +0000662 typedef __wrap_iter<pointer> iterator;
663 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000664 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
665 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000666
667private:
Howard Hinnant68bf1812013-04-30 21:44:48 +0000668
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000669#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000670
671 struct __long
672 {
673 pointer __data_;
674 size_type __size_;
675 size_type __cap_;
676 };
677
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000678#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000679 static const size_type __short_mask = 0x01;
680 static const size_type __long_mask = 0x1ul;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000681#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000682 static const size_type __short_mask = 0x80;
683 static const size_type __long_mask = ~(size_type(~0) >> 1);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400684#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +0000685
686 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
687 (sizeof(__long) - 1)/sizeof(value_type) : 2};
688
689 struct __short
690 {
691 value_type __data_[__min_cap];
Azat Khuzhin1995f722022-04-08 16:13:46 -0400692 unsigned char __padding[sizeof(value_type) - 1];
693 unsigned char __size_;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000694 };
695
696#else
697
Howard Hinnantc51e1022010-05-11 19:42:16 +0000698 struct __long
699 {
700 size_type __cap_;
701 size_type __size_;
702 pointer __data_;
703 };
704
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000705#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000706 static const size_type __short_mask = 0x80;
707 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000708#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000709 static const size_type __short_mask = 0x01;
710 static const size_type __long_mask = 0x1ul;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400711#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +0000712
Howard Hinnantc51e1022010-05-11 19:42:16 +0000713 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
714 (sizeof(__long) - 1)/sizeof(value_type) : 2};
715
716 struct __short
717 {
718 union
719 {
720 unsigned char __size_;
Howard Hinnant49e145e2012-10-30 19:06:59 +0000721 value_type __lx;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000722 };
723 value_type __data_[__min_cap];
724 };
725
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400726#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000727
Howard Hinnant8ea98242013-08-23 17:37:05 +0000728 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000729
Howard Hinnant8ea98242013-08-23 17:37:05 +0000730 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000731
732 struct __raw
733 {
734 size_type __words[__n_words];
735 };
736
737 struct __rep
738 {
739 union
740 {
741 __long __l;
742 __short __s;
743 __raw __r;
744 };
745 };
746
747 __compressed_pair<__rep, allocator_type> __r_;
748
Howard Hinnantc51e1022010-05-11 19:42:16 +0000749public:
Martin Storsjö88890c22021-02-22 01:13:13 +0200750 _LIBCPP_TEMPLATE_DATA_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000751 static const size_type npos = -1;
752
Howard Hinnant3e276872011-06-03 18:40:47 +0000753 _LIBCPP_INLINE_VISIBILITY basic_string()
754 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000755
756 _LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a)
757#if _LIBCPP_STD_VER <= 14
758 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
759#else
760 _NOEXCEPT;
761#endif
762
Howard Hinnantc51e1022010-05-11 19:42:16 +0000763 basic_string(const basic_string& __str);
764 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clowa80abc72015-06-03 19:56:43 +0000765
Eric Fiselierfc92be82017-04-19 00:28:44 +0000766#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000767 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000768 basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +0000769#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +0000770 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000771#else
772 _NOEXCEPT;
773#endif
774
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000775 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000776 basic_string(basic_string&& __str, const allocator_type& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400777#endif // _LIBCPP_CXX03_LANG
Marshall Clowe46031a2018-07-02 18:41:15 +0000778
Louis Dionne9ce598d2021-09-08 09:14:43 -0400779 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Eric Fiseliera8567862018-07-17 05:48:48 +0000780 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5169d1c2019-12-16 19:03:23 -0500781 basic_string(const _CharT* __s) : __r_(__default_init_tag(), __default_init_tag()) {
Eric Fiseliera8567862018-07-17 05:48:48 +0000782 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
783 __init(__s, traits_type::length(__s));
Nikolas Klauserf2807732022-01-11 00:33:35 +0100784 _VSTD::__debug_db_insert_c(this);
Eric Fiseliera8567862018-07-17 05:48:48 +0000785 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000786
Louis Dionne9ce598d2021-09-08 09:14:43 -0400787 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000788 _LIBCPP_INLINE_VISIBILITY
789 basic_string(const _CharT* __s, const _Allocator& __a);
790
Marek Kurdej90d79712021-07-27 16:16:21 +0200791#if _LIBCPP_STD_VER > 20
792 basic_string(nullptr_t) = delete;
793#endif
794
Howard Hinnantcf823322010-12-17 14:46:43 +0000795 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000796 basic_string(const _CharT* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +0000797 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000798 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000799 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000800 basic_string(size_type __n, _CharT __c);
Marshall Clowe46031a2018-07-02 18:41:15 +0000801
Louis Dionne9ce598d2021-09-08 09:14:43 -0400802 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000803 _LIBCPP_INLINE_VISIBILITY
804 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
805
Marshall Clow83445802016-04-07 18:13:41 +0000806 basic_string(const basic_string& __str, size_type __pos, size_type __n,
Eric Fiselier812882b2017-02-17 01:17:10 +0000807 const _Allocator& __a = _Allocator());
Marshall Clow83445802016-04-07 18:13:41 +0000808 _LIBCPP_INLINE_VISIBILITY
809 basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +0000810 const _Allocator& __a = _Allocator());
Marshall Clowe46031a2018-07-02 18:41:15 +0000811
Louis Dionne9ce598d2021-09-08 09:14:43 -0400812 template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value> >
Shoaib Meenai69c57412017-03-02 03:02:50 +0000813 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselier812882b2017-02-17 01:17:10 +0000814 basic_string(const _Tp& __t, size_type __pos, size_type __n,
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500815 const allocator_type& __a = allocator_type());
Marshall Clowe46031a2018-07-02 18:41:15 +0000816
Louis Dionne9ce598d2021-09-08 09:14:43 -0400817 template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500818 !__is_same_uncvref<_Tp, basic_string>::value> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000819 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
820 explicit basic_string(const _Tp& __t);
821
Louis Dionne9ce598d2021-09-08 09:14:43 -0400822 template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000823 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
824 explicit basic_string(const _Tp& __t, const allocator_type& __a);
825
Louis Dionne9ce598d2021-09-08 09:14:43 -0400826 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000827 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000828 basic_string(_InputIterator __first, _InputIterator __last);
Louis Dionne9ce598d2021-09-08 09:14:43 -0400829 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000830 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000831 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000832#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000833 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000834 basic_string(initializer_list<_CharT> __il);
Howard Hinnantcf823322010-12-17 14:46:43 +0000835 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000836 basic_string(initializer_list<_CharT> __il, const _Allocator& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400837#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000838
Eric Fiselierd9a702a2016-10-31 03:42:50 +0000839 inline ~basic_string();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000840
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000841 _LIBCPP_INLINE_VISIBILITY
842 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
843
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000844 basic_string& operator=(const basic_string& __str);
Eric Fiselier5ec13b12017-01-23 21:24:58 +0000845
Louis Dionne9ce598d2021-09-08 09:14:43 -0400846 template <class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000847 basic_string& operator=(const _Tp& __t)
848 {__self_view __sv = __t; return assign(__sv);}
849
Eric Fiselierfc92be82017-04-19 00:28:44 +0000850#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000851 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000852 basic_string& operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000853 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiselierfc92be82017-04-19 00:28:44 +0000854 _LIBCPP_INLINE_VISIBILITY
855 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000856#endif
Howard Hinnantd17880b2013-06-28 16:59:19 +0000857 _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const value_type* __s) {return assign(__s);}
Marek Kurdej90d79712021-07-27 16:16:21 +0200858#if _LIBCPP_STD_VER > 20
859 basic_string& operator=(nullptr_t) = delete;
860#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000861 basic_string& operator=(value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000862
Louis Dionneba400782020-10-02 15:02:52 -0400863#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +0000864 _LIBCPP_INLINE_VISIBILITY
865 iterator begin() _NOEXCEPT
866 {return iterator(this, __get_pointer());}
867 _LIBCPP_INLINE_VISIBILITY
868 const_iterator begin() const _NOEXCEPT
869 {return const_iterator(this, __get_pointer());}
870 _LIBCPP_INLINE_VISIBILITY
871 iterator end() _NOEXCEPT
872 {return iterator(this, __get_pointer() + size());}
873 _LIBCPP_INLINE_VISIBILITY
874 const_iterator end() const _NOEXCEPT
875 {return const_iterator(this, __get_pointer() + size());}
876#else
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000877 _LIBCPP_INLINE_VISIBILITY
878 iterator begin() _NOEXCEPT
879 {return iterator(__get_pointer());}
880 _LIBCPP_INLINE_VISIBILITY
881 const_iterator begin() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000882 {return const_iterator(__get_pointer());}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000883 _LIBCPP_INLINE_VISIBILITY
884 iterator end() _NOEXCEPT
885 {return iterator(__get_pointer() + size());}
886 _LIBCPP_INLINE_VISIBILITY
887 const_iterator end() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000888 {return const_iterator(__get_pointer() + size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400889#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000890 _LIBCPP_INLINE_VISIBILITY
891 reverse_iterator rbegin() _NOEXCEPT
892 {return reverse_iterator(end());}
893 _LIBCPP_INLINE_VISIBILITY
894 const_reverse_iterator rbegin() const _NOEXCEPT
895 {return const_reverse_iterator(end());}
896 _LIBCPP_INLINE_VISIBILITY
897 reverse_iterator rend() _NOEXCEPT
898 {return reverse_iterator(begin());}
899 _LIBCPP_INLINE_VISIBILITY
900 const_reverse_iterator rend() const _NOEXCEPT
901 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000902
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000903 _LIBCPP_INLINE_VISIBILITY
904 const_iterator cbegin() const _NOEXCEPT
905 {return begin();}
906 _LIBCPP_INLINE_VISIBILITY
907 const_iterator cend() const _NOEXCEPT
908 {return end();}
909 _LIBCPP_INLINE_VISIBILITY
910 const_reverse_iterator crbegin() const _NOEXCEPT
911 {return rbegin();}
912 _LIBCPP_INLINE_VISIBILITY
913 const_reverse_iterator crend() const _NOEXCEPT
914 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000915
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000916 _LIBCPP_INLINE_VISIBILITY size_type size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000917 {return __is_long() ? __get_long_size() : __get_short_size();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000918 _LIBCPP_INLINE_VISIBILITY size_type length() const _NOEXCEPT {return size();}
919 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT;
920 _LIBCPP_INLINE_VISIBILITY size_type capacity() const _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +0000921 {return (__is_long() ? __get_long_cap()
922 : static_cast<size_type>(__min_cap)) - 1;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000923
924 void resize(size_type __n, value_type __c);
925 _LIBCPP_INLINE_VISIBILITY void resize(size_type __n) {resize(__n, value_type());}
926
Marek Kurdejc9848142020-11-26 10:07:16 +0100927 void reserve(size_type __requested_capacity);
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100928
929#if _LIBCPP_STD_VER > 20
930 template <class _Op>
931 _LIBCPP_HIDE_FROM_ABI constexpr
932 void resize_and_overwrite(size_type __n, _Op __op) {
933 __resize_default_init(__n);
Nikolas Klausera4a76a12022-01-20 13:53:59 +0100934 __erase_to_end(_VSTD::move(__op)(data(), _LIBCPP_AUTO_CAST(__n)));
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100935 }
936#endif
937
Eric Fiselier451d5582018-11-26 20:15:38 +0000938 _LIBCPP_INLINE_VISIBILITY void __resize_default_init(size_type __n);
939
Marek Kurdejc9848142020-11-26 10:07:16 +0100940 _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_INLINE_VISIBILITY
941 void reserve() _NOEXCEPT {shrink_to_fit();}
Marshall Clow6ae12a82018-11-28 18:18:34 +0000942 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejc9848142020-11-26 10:07:16 +0100943 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000944 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000945 void clear() _NOEXCEPT;
Marshall Clowb7db4972017-11-15 20:02:27 +0000946 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
947 bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000948
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000949 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __pos) const _NOEXCEPT;
950 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000951
952 const_reference at(size_type __n) const;
953 reference at(size_type __n);
954
955 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const basic_string& __str) {return append(__str);}
Marshall Clowe46031a2018-07-02 18:41:15 +0000956
957 template <class _Tp>
958 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -0400959 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +0000960 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500961 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
962 && !__is_same_uncvref<_Tp, basic_string >::value,
Marshall Clowe46031a2018-07-02 18:41:15 +0000963 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500964 >
Marshall Clowe46031a2018-07-02 18:41:15 +0000965 operator+=(const _Tp& __t) {__self_view __sv = __t; return append(__sv);}
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000966 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const value_type* __s) {return append(__s);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000967 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(value_type __c) {push_back(__c); return *this;}
Eric Fiselierfc92be82017-04-19 00:28:44 +0000968#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000969 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(initializer_list<value_type> __il) {return append(__il);}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400970#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000971
Howard Hinnantcf823322010-12-17 14:46:43 +0000972 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000973 basic_string& append(const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000974
975 template <class _Tp>
976 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -0400977 __enable_if_t<
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500978 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
979 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clowe46031a2018-07-02 18:41:15 +0000980 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500981 >
Marshall Clowe46031a2018-07-02 18:41:15 +0000982 append(const _Tp& __t) { __self_view __sv = __t; return append(__sv.data(), __sv.size()); }
Marshall Clow8db7fb02014-03-04 19:17:19 +0000983 basic_string& append(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clowe46031a2018-07-02 18:41:15 +0000984
Marshall Clow62953962016-10-03 23:40:48 +0000985 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +0000986 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -0400987 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +0000988 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500989 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
990 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +0000991 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500992 >
Marshall Clow82513342016-09-24 22:45:42 +0000993 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +0000994 basic_string& append(const value_type* __s, size_type __n);
995 basic_string& append(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000996 basic_string& append(size_type __n, value_type __c);
Eric Fiselier451d5582018-11-26 20:15:38 +0000997
998 _LIBCPP_INLINE_VISIBILITY
999 void __append_default_init(size_type __n);
1000
Howard Hinnantc51e1022010-05-11 19:42:16 +00001001 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001002 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001003 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001004 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001005 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001006 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001007 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001008 _LIBCPP_INLINE_VISIBILITY
1009 append(_InputIterator __first, _InputIterator __last) {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001010 const basic_string __temp(__first, __last, __alloc());
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001011 append(__temp.data(), __temp.size());
1012 return *this;
1013 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001014 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001015 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001016 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001017 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001018 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001019 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001020 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001021 _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001022 append(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001023
Eric Fiselierfc92be82017-04-19 00:28:44 +00001024#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001025 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001026 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001027#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001028
1029 void push_back(value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001030 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001031 void pop_back();
Marshall Clow05cf6692019-03-19 03:30:07 +00001032 _LIBCPP_INLINE_VISIBILITY reference front() _NOEXCEPT;
1033 _LIBCPP_INLINE_VISIBILITY const_reference front() const _NOEXCEPT;
1034 _LIBCPP_INLINE_VISIBILITY reference back() _NOEXCEPT;
1035 _LIBCPP_INLINE_VISIBILITY const_reference back() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001036
Marshall Clowe46031a2018-07-02 18:41:15 +00001037 template <class _Tp>
1038 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001039 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001040 <
1041 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1042 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001043 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001044 assign(const _Tp & __t) { __self_view __sv = __t; return assign(__sv.data(), __sv.size()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001045 _LIBCPP_INLINE_VISIBILITY
Marshall Clow95d5e9a2016-03-09 18:08:29 +00001046 basic_string& assign(const basic_string& __str) { return *this = __str; }
Eric Fiselierfc92be82017-04-19 00:28:44 +00001047#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001048 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001049 basic_string& assign(basic_string&& __str)
Marshall Clow5aa9e942015-10-05 16:17:34 +00001050 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001051 {*this = _VSTD::move(__str); return *this;}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001052#endif
Marshall Clow8db7fb02014-03-04 19:17:19 +00001053 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow62953962016-10-03 23:40:48 +00001054 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001055 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001056 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001057 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001058 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1059 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001060 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001061 >
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001062 assign(const _Tp & __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001063 basic_string& assign(const value_type* __s, size_type __n);
1064 basic_string& assign(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001065 basic_string& assign(size_type __n, value_type __c);
1066 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001067 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001068 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001069 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001070 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001071 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001072 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001073 assign(_InputIterator __first, _InputIterator __last);
1074 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001075 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001076 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001077 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001078 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001079 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001080 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001081 assign(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001082#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001083 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001084 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001085#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001086
Howard Hinnantcf823322010-12-17 14:46:43 +00001087 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001088 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001089
1090 template <class _Tp>
1091 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001092 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001093 <
1094 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1095 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001096 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001097 insert(size_type __pos1, const _Tp& __t)
1098 { __self_view __sv = __t; return insert(__pos1, __sv.data(), __sv.size()); }
1099
Marshall Clow82513342016-09-24 22:45:42 +00001100 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001101 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001102 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001103 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001104 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001105 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001106 >
Marshall Clow82513342016-09-24 22:45:42 +00001107 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Marshall Clow8db7fb02014-03-04 19:17:19 +00001108 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001109 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1110 basic_string& insert(size_type __pos, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001111 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1112 iterator insert(const_iterator __pos, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001113 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001114 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1115 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001116 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001117 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001118 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001119 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001120 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001121 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001122 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1123 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001124 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001125 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001126 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001127 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001128 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001129 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001130 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001131#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001132 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001133 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1134 {return insert(__pos, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001135#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001136
1137 basic_string& erase(size_type __pos = 0, size_type __n = npos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001138 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001139 iterator erase(const_iterator __pos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001140 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001141 iterator erase(const_iterator __first, const_iterator __last);
1142
Howard Hinnantcf823322010-12-17 14:46:43 +00001143 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001144 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001145
1146 template <class _Tp>
1147 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001148 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001149 <
1150 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1151 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001152 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001153 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 +00001154 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 +00001155 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001156 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001157 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001158 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001159 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001160 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001161 >
Marshall Clow82513342016-09-24 22:45:42 +00001162 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001163 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
1164 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001165 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001166 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001167 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001168
1169 template <class _Tp>
1170 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001171 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001172 <
1173 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1174 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001175 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001176 replace(const_iterator __i1, const_iterator __i2, const _Tp& __t) { __self_view __sv = __t; return replace(__i1 - begin(), __i2 - __i1, __sv); }
1177
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001178 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001179 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +00001180 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001181 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Howard Hinnantcf823322010-12-17 14:46:43 +00001182 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001183 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001184 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001185 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001186 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001187 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001188 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001189 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001190 >
Howard Hinnant990d6e82010-11-17 21:11:40 +00001191 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001192#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001193 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001194 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001195 {return replace(__i1, __i2, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001196#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001197
Howard Hinnantd17880b2013-06-28 16:59:19 +00001198 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
Howard Hinnantcf823322010-12-17 14:46:43 +00001199 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001200 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1201
Howard Hinnantcf823322010-12-17 14:46:43 +00001202 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001203 void swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001204#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00001205 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +00001206#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001207 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001208 __is_nothrow_swappable<allocator_type>::value);
1209#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001210
Howard Hinnantcf823322010-12-17 14:46:43 +00001211 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001212 const value_type* c_str() const _NOEXCEPT {return data();}
Howard Hinnantcf823322010-12-17 14:46:43 +00001213 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001214 const value_type* data() const _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Eric Fiselier4ee2a892017-11-20 20:23:27 +00001215#if _LIBCPP_STD_VER > 14 || defined(_LIBCPP_BUILDING_LIBRARY)
Marshall Clowd3c22392016-03-08 15:44:30 +00001216 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001217 value_type* data() _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Marshall Clowd3c22392016-03-08 15:44:30 +00001218#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001219
Howard Hinnantcf823322010-12-17 14:46:43 +00001220 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001221 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001222
Howard Hinnantcf823322010-12-17 14:46:43 +00001223 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001224 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001225
1226 template <class _Tp>
1227 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001228 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001229 <
1230 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1231 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001232 >
zoecarver1997e0a2021-02-05 11:54:47 -08001233 find(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001234 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001235 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001236 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001237 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001238
Howard Hinnantcf823322010-12-17 14:46:43 +00001239 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001240 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001241
1242 template <class _Tp>
1243 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001244 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001245 <
1246 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1247 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001248 >
zoecarver1997e0a2021-02-05 11:54:47 -08001249 rfind(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001250 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001251 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001252 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001253 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
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_first_of(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
Louis Dionne9ce598d2021-09-08 09:14:43 -04001260 __enable_if_t
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 >
zoecarver1997e0a2021-02-05 11:54:47 -08001265 find_first_of(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001266 size_type find_first_of(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_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001269 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001270 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001271
Howard Hinnantcf823322010-12-17 14:46:43 +00001272 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001273 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001274
1275 template <class _Tp>
1276 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001277 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001278 <
1279 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1280 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001281 >
zoecarver1997e0a2021-02-05 11:54:47 -08001282 find_last_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001283 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001284 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001285 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001286 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001287 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001288
Howard Hinnantcf823322010-12-17 14:46:43 +00001289 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001290 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001291
1292 template <class _Tp>
1293 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001294 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001295 <
1296 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1297 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001298 >
zoecarver1997e0a2021-02-05 11:54:47 -08001299 find_first_not_of(const _Tp &__t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001300 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 +00001301 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001302 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001303 _LIBCPP_INLINE_VISIBILITY
1304 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1305
1306 _LIBCPP_INLINE_VISIBILITY
1307 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001308
1309 template <class _Tp>
1310 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001311 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001312 <
1313 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1314 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001315 >
zoecarver1997e0a2021-02-05 11:54:47 -08001316 find_last_not_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001317 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 +00001318 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001319 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001320 _LIBCPP_INLINE_VISIBILITY
1321 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1322
1323 _LIBCPP_INLINE_VISIBILITY
1324 int compare(const basic_string& __str) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001325
1326 template <class _Tp>
1327 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001328 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001329 <
1330 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1331 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001332 >
zoecarver1997e0a2021-02-05 11:54:47 -08001333 compare(const _Tp &__t) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001334
1335 template <class _Tp>
1336 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001337 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001338 <
1339 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1340 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001341 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001342 compare(size_type __pos1, size_type __n1, const _Tp& __t) const;
1343
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001344 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001345 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Marshall Clow8db7fb02014-03-04 19:17:19 +00001346 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 +00001347
Marshall Clow82513342016-09-24 22:45:42 +00001348 template <class _Tp>
Eric Fiselier7d4aa5c2016-10-14 05:29:46 +00001349 inline _LIBCPP_INLINE_VISIBILITY
Louis Dionne9ce598d2021-09-08 09:14:43 -04001350 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001351 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001352 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001353 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001354 >
Marshall Clow82513342016-09-24 22:45:42 +00001355 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 +00001356 int compare(const value_type* __s) const _NOEXCEPT;
1357 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1358 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001359
Marshall Clow18c293b2017-12-04 20:11:38 +00001360#if _LIBCPP_STD_VER > 17
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001361 constexpr _LIBCPP_INLINE_VISIBILITY
1362 bool starts_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001363 { return __self_view(data(), size()).starts_with(__sv); }
1364
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001365 constexpr _LIBCPP_INLINE_VISIBILITY
1366 bool starts_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001367 { return !empty() && _Traits::eq(front(), __c); }
1368
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001369 constexpr _LIBCPP_INLINE_VISIBILITY
1370 bool starts_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001371 { return starts_with(__self_view(__s)); }
1372
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001373 constexpr _LIBCPP_INLINE_VISIBILITY
1374 bool ends_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001375 { return __self_view(data(), size()).ends_with( __sv); }
1376
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001377 constexpr _LIBCPP_INLINE_VISIBILITY
1378 bool ends_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001379 { return !empty() && _Traits::eq(back(), __c); }
1380
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001381 constexpr _LIBCPP_INLINE_VISIBILITY
1382 bool ends_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001383 { return ends_with(__self_view(__s)); }
1384#endif
1385
Wim Leflere023c3542021-01-19 14:33:30 -05001386#if _LIBCPP_STD_VER > 20
1387 constexpr _LIBCPP_INLINE_VISIBILITY
1388 bool contains(__self_view __sv) const noexcept
1389 { return __self_view(data(), size()).contains(__sv); }
1390
1391 constexpr _LIBCPP_INLINE_VISIBILITY
1392 bool contains(value_type __c) const noexcept
1393 { return __self_view(data(), size()).contains(__c); }
1394
1395 constexpr _LIBCPP_INLINE_VISIBILITY
1396 bool contains(const value_type* __s) const
1397 { return __self_view(data(), size()).contains(__s); }
1398#endif
1399
Howard Hinnantcf823322010-12-17 14:46:43 +00001400 _LIBCPP_INLINE_VISIBILITY bool __invariants() const;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001401
Marshall Clowe60a7182018-05-29 17:04:37 +00001402 _LIBCPP_INLINE_VISIBILITY void __clear_and_shrink() _NOEXCEPT;
Marshall Clow851b9ec2019-05-20 21:56:51 +00001403
Marek Kurdejc9848142020-11-26 10:07:16 +01001404 _LIBCPP_INLINE_VISIBILITY void __shrink_or_extend(size_type __target_capacity);
1405
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001406 _LIBCPP_INLINE_VISIBILITY
1407 bool __is_long() const _NOEXCEPT
1408 {return bool(__r_.first().__s.__size_ & __short_mask);}
1409
Louis Dionneba400782020-10-02 15:02:52 -04001410#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001411
1412 bool __dereferenceable(const const_iterator* __i) const;
1413 bool __decrementable(const const_iterator* __i) const;
1414 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1415 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1416
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001417#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001418
Howard Hinnantc51e1022010-05-11 19:42:16 +00001419private:
Nikolas Klauser93826d12022-01-04 17:24:03 +01001420 _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI static bool __fits_in_sso(size_type __sz) {
1421 // SSO is disabled during constant evaluation because `__is_long` isn't constexpr friendly
1422 return !__libcpp_is_constant_evaluated() && (__sz < __min_cap);
1423 }
1424
Nikolas Klauser48d680d2022-02-26 13:28:33 +01001425 template <class _ForwardIterator>
1426 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
1427 iterator __insert_from_safe_copy(size_type __n, size_type __ip, _ForwardIterator __first, _ForwardIterator __last) {
1428 size_type __sz = size();
1429 size_type __cap = capacity();
1430 value_type* __p;
1431 if (__cap - __sz >= __n)
1432 {
1433 __p = std::__to_address(__get_pointer());
1434 size_type __n_move = __sz - __ip;
1435 if (__n_move != 0)
1436 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
1437 }
1438 else
1439 {
1440 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
1441 __p = std::__to_address(__get_long_pointer());
1442 }
1443 __sz += __n;
1444 __set_size(__sz);
1445 traits_type::assign(__p[__sz], value_type());
1446 for (__p += __ip; __first != __last; ++__p, ++__first)
1447 traits_type::assign(*__p, *__first);
1448
1449 return begin() + __ip;
1450 }
1451
1452 _LIBCPP_HIDE_FROM_ABI allocator_type& __alloc() _NOEXCEPT { return __r_.second(); }
1453 _LIBCPP_HIDE_FROM_ABI const allocator_type& __alloc() const _NOEXCEPT { return __r_.second(); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001454
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001455#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001456
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001457 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001458 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001459# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001460 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001461# else
1462 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1463# endif
1464
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001465 _LIBCPP_INLINE_VISIBILITY
1466 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001467# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001468 {return __r_.first().__s.__size_ >> 1;}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001469# else
1470 {return __r_.first().__s.__size_;}
1471# endif
1472
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001473#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001474
1475 _LIBCPP_INLINE_VISIBILITY
1476 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001477# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001478 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1479# else
1480 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
1481# endif
1482
1483 _LIBCPP_INLINE_VISIBILITY
1484 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001485# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001486 {return __r_.first().__s.__size_;}
1487# else
1488 {return __r_.first().__s.__size_ >> 1;}
1489# endif
1490
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001491#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001492
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001493 _LIBCPP_INLINE_VISIBILITY
1494 void __set_long_size(size_type __s) _NOEXCEPT
1495 {__r_.first().__l.__size_ = __s;}
1496 _LIBCPP_INLINE_VISIBILITY
1497 size_type __get_long_size() const _NOEXCEPT
1498 {return __r_.first().__l.__size_;}
1499 _LIBCPP_INLINE_VISIBILITY
1500 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001501 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1502
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001503 _LIBCPP_INLINE_VISIBILITY
1504 void __set_long_cap(size_type __s) _NOEXCEPT
1505 {__r_.first().__l.__cap_ = __long_mask | __s;}
1506 _LIBCPP_INLINE_VISIBILITY
1507 size_type __get_long_cap() const _NOEXCEPT
Howard Hinnant28b24882011-12-01 20:21:04 +00001508 {return __r_.first().__l.__cap_ & size_type(~__long_mask);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001509
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001510 _LIBCPP_INLINE_VISIBILITY
1511 void __set_long_pointer(pointer __p) _NOEXCEPT
1512 {__r_.first().__l.__data_ = __p;}
1513 _LIBCPP_INLINE_VISIBILITY
1514 pointer __get_long_pointer() _NOEXCEPT
1515 {return __r_.first().__l.__data_;}
1516 _LIBCPP_INLINE_VISIBILITY
1517 const_pointer __get_long_pointer() const _NOEXCEPT
1518 {return __r_.first().__l.__data_;}
1519 _LIBCPP_INLINE_VISIBILITY
1520 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001521 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001522 _LIBCPP_INLINE_VISIBILITY
1523 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001524 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001525 _LIBCPP_INLINE_VISIBILITY
1526 pointer __get_pointer() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001527 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001528 _LIBCPP_INLINE_VISIBILITY
1529 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001530 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1531
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001532 _LIBCPP_INLINE_VISIBILITY
1533 void __zero() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001534 {
1535 size_type (&__a)[__n_words] = __r_.first().__r.__words;
1536 for (unsigned __i = 0; __i < __n_words; ++__i)
1537 __a[__i] = 0;
1538 }
1539
1540 template <size_type __a> static
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001541 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00001542 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +00001543 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001544 enum {__alignment = 16};
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001545 static _LIBCPP_INLINE_VISIBILITY
1546 size_type __recommend(size_type __s) _NOEXCEPT
Marshall Clow80584522018-02-07 21:30:17 +00001547 {
1548 if (__s < __min_cap) return static_cast<size_type>(__min_cap) - 1;
1549 size_type __guess = __align_it<sizeof(value_type) < __alignment ?
1550 __alignment/sizeof(value_type) : 1 > (__s+1) - 1;
1551 if (__guess == __min_cap) ++__guess;
1552 return __guess;
1553 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001554
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001555 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001556 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001557 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001558 void __init(const value_type* __s, size_type __sz);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001559 inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001560 void __init(size_type __n, value_type __c);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001561
Martijn Vels5e7c9752020-03-04 17:52:46 -05001562 // Slow path for the (inlined) copy constructor for 'long' strings.
1563 // Always externally instantiated and not inlined.
1564 // Requires that __s is zero terminated.
1565 // The main reason for this function to exist is because for unstable, we
1566 // want to allow inlining of the copy constructor. However, we don't want
1567 // to call the __init() functions as those are marked as inline which may
1568 // result in over-aggressive inlining by the compiler, where our aim is
1569 // to only inline the fast path code directly in the ctor.
1570 void __init_copy_ctor_external(const value_type* __s, size_type __sz);
1571
Howard Hinnantc51e1022010-05-11 19:42:16 +00001572 template <class _InputIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001573 inline
Louis Dionne9ce598d2021-09-08 09:14:43 -04001574 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001575 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001576 __is_exactly_cpp17_input_iterator<_InputIterator>::value
1577 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001578 __init(_InputIterator __first, _InputIterator __last);
1579
1580 template <class _ForwardIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001581 inline
Louis Dionne9ce598d2021-09-08 09:14:43 -04001582 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001583 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001584 __is_cpp17_forward_iterator<_ForwardIterator>::value
1585 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001586 __init(_ForwardIterator __first, _ForwardIterator __last);
1587
1588 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001589 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001590 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1591 size_type __n_copy, size_type __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00001592 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001593
Martijn Vels596e3de2020-02-26 15:55:49 -05001594 // __assign_no_alias is invoked for assignment operations where we
1595 // have proof that the input does not alias the current instance.
1596 // For example, operator=(basic_string) performs a 'self' check.
1597 template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04001598 basic_string& __assign_no_alias(const value_type* __s, size_type __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05001599
Howard Hinnantcf823322010-12-17 14:46:43 +00001600 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001601 void __erase_to_end(size_type __pos);
1602
Martijn Velsa81fc792020-02-26 13:25:43 -05001603 // __erase_external_with_move is invoked for erase() invocations where
1604 // `n ~= npos`, likely requiring memory moves on the string data.
1605 void __erase_external_with_move(size_type __pos, size_type __n);
1606
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001607 _LIBCPP_INLINE_VISIBILITY
1608 void __copy_assign_alloc(const basic_string& __str)
1609 {__copy_assign_alloc(__str, integral_constant<bool,
1610 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1611
1612 _LIBCPP_INLINE_VISIBILITY
1613 void __copy_assign_alloc(const basic_string& __str, true_type)
1614 {
Marshall Clowf258c202017-01-31 03:40:52 +00001615 if (__alloc() == __str.__alloc())
1616 __alloc() = __str.__alloc();
1617 else
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001618 {
Marshall Clowf258c202017-01-31 03:40:52 +00001619 if (!__str.__is_long())
1620 {
Vedant Kumar55e007e2018-03-08 21:15:26 +00001621 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001622 __alloc() = __str.__alloc();
1623 }
1624 else
1625 {
1626 allocator_type __a = __str.__alloc();
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001627 auto __allocation = std::__allocate_at_least(__a, __str.__get_long_cap());
Vedant Kumar55e007e2018-03-08 21:15:26 +00001628 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001629 __alloc() = _VSTD::move(__a);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001630 __set_long_pointer(__allocation.ptr);
1631 __set_long_cap(__allocation.count);
Marshall Clowf258c202017-01-31 03:40:52 +00001632 __set_long_size(__str.size());
1633 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001634 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001635 }
1636
1637 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001638 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001639 {}
1640
Eric Fiselierfc92be82017-04-19 00:28:44 +00001641#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +00001642 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001643 void __move_assign(basic_string& __str, false_type)
1644 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnantcf823322010-12-17 14:46:43 +00001645 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001646 void __move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001647#if _LIBCPP_STD_VER > 14
1648 _NOEXCEPT;
1649#else
Howard Hinnant3e276872011-06-03 18:40:47 +00001650 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001651#endif
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001652#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001653
1654 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001655 void
Howard Hinnantc2734962011-09-02 20:42:31 +00001656 __move_assign_alloc(basic_string& __str)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001657 _NOEXCEPT_(
1658 !__alloc_traits::propagate_on_container_move_assignment::value ||
1659 is_nothrow_move_assignable<allocator_type>::value)
1660 {__move_assign_alloc(__str, integral_constant<bool,
1661 __alloc_traits::propagate_on_container_move_assignment::value>());}
1662
1663 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00001664 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001665 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1666 {
1667 __alloc() = _VSTD::move(__c.__alloc());
1668 }
1669
1670 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001671 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001672 _NOEXCEPT
1673 {}
1674
Martijn Velsda7d94f2020-06-19 14:24:03 -04001675 basic_string& __assign_external(const value_type* __s);
1676 basic_string& __assign_external(const value_type* __s, size_type __n);
1677
1678 // Assigns the value in __s, guaranteed to be __n < __min_cap in length.
1679 inline basic_string& __assign_short(const value_type* __s, size_type __n) {
1680 pointer __p = __is_long()
1681 ? (__set_long_size(__n), __get_long_pointer())
1682 : (__set_short_size(__n), __get_short_pointer());
1683 traits_type::move(_VSTD::__to_address(__p), __s, __n);
1684 traits_type::assign(__p[__n], value_type());
1685 return *this;
1686 }
1687
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01001688 _LIBCPP_HIDE_FROM_ABI basic_string& __null_terminate_at(value_type* __p, size_type __newsz) {
1689 __set_size(__newsz);
1690 __invalidate_iterators_past(__newsz);
1691 traits_type::assign(__p[__newsz], value_type());
1692 return *this;
1693 }
1694
Howard Hinnantcf823322010-12-17 14:46:43 +00001695 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
1696 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(size_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001697
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001698 template<class _Tp>
1699 _LIBCPP_INLINE_VISIBILITY
1700 bool __addr_in_range(_Tp&& __t) const {
1701 const volatile void *__p = _VSTD::addressof(__t);
1702 return data() <= __p && __p <= data() + size();
1703 }
1704
Louis Dionned24191c2021-08-19 12:39:16 -04001705 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1706 void __throw_length_error() const {
Nikolas Klauserf89bb932022-01-24 19:44:34 +01001707 _VSTD::__throw_length_error("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001708 }
1709
1710 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1711 void __throw_out_of_range() const {
Nikolas Klauserf89bb932022-01-24 19:44:34 +01001712 _VSTD::__throw_out_of_range("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001713 }
1714
Howard Hinnantc51e1022010-05-11 19:42:16 +00001715 friend basic_string operator+<>(const basic_string&, const basic_string&);
1716 friend basic_string operator+<>(const value_type*, const basic_string&);
1717 friend basic_string operator+<>(value_type, const basic_string&);
1718 friend basic_string operator+<>(const basic_string&, const value_type*);
1719 friend basic_string operator+<>(const basic_string&, value_type);
1720};
1721
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001722// These declarations must appear before any functions are implicitly used
1723// so that they have the correct visibility specifier.
1724#ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
Louis Dionne89258142021-08-23 15:32:36 -04001725 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1726# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1727 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1728# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001729#else
Louis Dionne89258142021-08-23 15:32:36 -04001730 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1731# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1732 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1733# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001734#endif
1735
1736
Louis Dionned59f8a52021-08-17 11:59:07 -04001737#if _LIBCPP_STD_VER >= 17
Marshall Clowa0563332018-02-08 06:34:03 +00001738template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -05001739 class _CharT = __iter_value_type<_InputIterator>,
Marshall Clowa0563332018-02-08 06:34:03 +00001740 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001741 class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
1742 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowa0563332018-02-08 06:34:03 +00001743 >
1744basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
1745 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
Marshall Clowe46031a2018-07-02 18:41:15 +00001746
1747template<class _CharT,
1748 class _Traits,
1749 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001750 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowe46031a2018-07-02 18:41:15 +00001751 >
1752explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
1753 -> basic_string<_CharT, _Traits, _Allocator>;
1754
1755template<class _CharT,
1756 class _Traits,
1757 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001758 class = enable_if_t<__is_allocator<_Allocator>::value>,
Marshall Clowe46031a2018-07-02 18:41:15 +00001759 class _Sz = typename allocator_traits<_Allocator>::size_type
1760 >
1761basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
1762 -> basic_string<_CharT, _Traits, _Allocator>;
Marshall Clowa0563332018-02-08 06:34:03 +00001763#endif
1764
Howard Hinnantc51e1022010-05-11 19:42:16 +00001765template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001766inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001767void
1768basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
1769{
Louis Dionneba400782020-10-02 15:02:52 -04001770#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001771 if (!__libcpp_is_constant_evaluated())
1772 __get_db()->__invalidate_all(this);
Louis Dionneba400782020-10-02 15:02:52 -04001773#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001774}
1775
1776template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001777inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001778void
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001779basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type __pos)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001780{
Louis Dionneba400782020-10-02 15:02:52 -04001781#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001782 if (!__libcpp_is_constant_evaluated()) {
1783 __c_node* __c = __get_db()->__find_c_and_lock(this);
1784 if (__c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001785 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001786 const_pointer __new_last = __get_pointer() + __pos;
1787 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001788 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001789 --__p;
1790 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1791 if (__i->base() > __new_last)
1792 {
1793 (*__p)->__c_ = nullptr;
1794 if (--__c->end_ != __p)
1795 _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1796 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001797 }
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001798 __get_db()->unlock();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001799 }
1800 }
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001801#else
1802 (void)__pos;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001803#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001804}
1805
1806template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001807inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001808basic_string<_CharT, _Traits, _Allocator>::basic_string()
Marshall Clowe546cbb2015-06-04 02:05:41 +00001809 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001810 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001811{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001812 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001813 __zero();
1814}
1815
1816template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001817inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001818basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
Eric Fiseliere012bf22015-07-18 20:40:46 +00001819#if _LIBCPP_STD_VER <= 14
1820 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1821#else
1822 _NOEXCEPT
1823#endif
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001824: __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001825{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001826 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001827 __zero();
1828}
1829
1830template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier815ed732016-09-16 00:00:48 +00001831void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1832 size_type __sz,
1833 size_type __reserve)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001834{
1835 if (__reserve > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001836 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001837 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001838 if (__fits_in_sso(__reserve))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001839 {
1840 __set_short_size(__sz);
1841 __p = __get_short_pointer();
1842 }
1843 else
1844 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001845 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__reserve) + 1);
1846 __p = __allocation.ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001847 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001848 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001849 __set_long_size(__sz);
1850 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001851 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001852 traits_type::assign(__p[__sz], value_type());
1853}
1854
1855template <class _CharT, class _Traits, class _Allocator>
1856void
Howard Hinnantd17880b2013-06-28 16:59:19 +00001857basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001858{
1859 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001860 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001861 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001862 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001863 {
1864 __set_short_size(__sz);
1865 __p = __get_short_pointer();
1866 }
1867 else
1868 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001869 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
1870 __p = __allocation.ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001871 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001872 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001873 __set_long_size(__sz);
1874 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001875 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001876 traits_type::assign(__p[__sz], value_type());
1877}
1878
1879template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00001880template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00001881basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001882 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001883{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001884 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001885 __init(__s, traits_type::length(__s));
Nikolas Klauserf2807732022-01-11 00:33:35 +01001886 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001887}
1888
1889template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001890inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001891basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001892 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001893{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001894 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
1895 __init(__s, __n);
1896 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001897}
1898
1899template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001900inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001901basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001902 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001903{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001904 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001905 __init(__s, __n);
Nikolas Klauserf2807732022-01-11 00:33:35 +01001906 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001907}
1908
1909template <class _CharT, class _Traits, class _Allocator>
1910basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001911 : __r_(__default_init_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001912{
1913 if (!__str.__is_long())
1914 __r_.first().__r = __str.__r_.first().__r;
1915 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001916 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1917 __str.__get_long_size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01001918 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001919}
1920
1921template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00001922basic_string<_CharT, _Traits, _Allocator>::basic_string(
1923 const basic_string& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001924 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001925{
1926 if (!__str.__is_long())
1927 __r_.first().__r = __str.__r_.first().__r;
1928 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001929 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1930 __str.__get_long_size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01001931 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001932}
1933
Martijn Vels5e7c9752020-03-04 17:52:46 -05001934template <class _CharT, class _Traits, class _Allocator>
1935void basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(
1936 const value_type* __s, size_type __sz) {
1937 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001938 if (__fits_in_sso(__sz)) {
Martijn Vels5e7c9752020-03-04 17:52:46 -05001939 __p = __get_short_pointer();
1940 __set_short_size(__sz);
1941 } else {
1942 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001943 __throw_length_error();
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001944 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
1945 __p = __allocation.ptr;
Martijn Vels5e7c9752020-03-04 17:52:46 -05001946 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001947 __set_long_cap(__allocation.count);
Martijn Vels5e7c9752020-03-04 17:52:46 -05001948 __set_long_size(__sz);
1949 }
1950 traits_type::copy(_VSTD::__to_address(__p), __s, __sz + 1);
1951}
1952
Eric Fiselierfc92be82017-04-19 00:28:44 +00001953#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001954
1955template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001956inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001957basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +00001958#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +00001959 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowa80abc72015-06-03 19:56:43 +00001960#else
1961 _NOEXCEPT
1962#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001963 : __r_(_VSTD::move(__str.__r_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001964{
1965 __str.__zero();
Nikolas Klauserf2807732022-01-11 00:33:35 +01001966 _VSTD::__debug_db_insert_c(this);
Louis Dionneba400782020-10-02 15:02:52 -04001967#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauserf2807732022-01-11 00:33:35 +01001968 if (!__libcpp_is_constant_evaluated() && __is_long())
1969 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001970#endif
1971}
1972
1973template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001974inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001975basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001976 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001977{
Marshall Clowd2d24692014-07-17 15:32:20 +00001978 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001979 __init(_VSTD::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd2d24692014-07-17 15:32:20 +00001980 else
1981 {
1982 __r_.first().__r = __str.__r_.first().__r;
1983 __str.__zero();
1984 }
Nikolas Klauserf2807732022-01-11 00:33:35 +01001985 _VSTD::__debug_db_insert_c(this);
Louis Dionneba400782020-10-02 15:02:52 -04001986#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauserf2807732022-01-11 00:33:35 +01001987 if (!__libcpp_is_constant_evaluated() && __is_long())
1988 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001989#endif
1990}
1991
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001992#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001993
1994template <class _CharT, class _Traits, class _Allocator>
1995void
1996basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
1997{
1998 if (__n > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001999 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002000 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002001 if (__fits_in_sso(__n))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002002 {
2003 __set_short_size(__n);
2004 __p = __get_short_pointer();
2005 }
2006 else
2007 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002008 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__n) + 1);
2009 __p = __allocation.ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002010 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002011 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002012 __set_long_size(__n);
2013 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002014 traits_type::assign(_VSTD::__to_address(__p), __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002015 traits_type::assign(__p[__n], value_type());
2016}
2017
2018template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002019inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002020basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002021 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002022{
2023 __init(__n, __c);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002024 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002025}
2026
2027template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002028template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00002029basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002030 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002031{
2032 __init(__n, __c);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002033 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002034}
2035
Howard Hinnantc51e1022010-05-11 19:42:16 +00002036template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00002037basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
2038 size_type __pos, size_type __n,
2039 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002040 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002041{
2042 size_type __str_sz = __str.size();
2043 if (__pos > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002044 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002045 __init(__str.data() + __pos, _VSTD::min(__n, __str_sz - __pos));
Nikolas Klauserf2807732022-01-11 00:33:35 +01002046 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002047}
2048
2049template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002050inline
Marshall Clow83445802016-04-07 18:13:41 +00002051basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +00002052 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002053 : __r_(__default_init_tag(), __a)
Marshall Clow83445802016-04-07 18:13:41 +00002054{
2055 size_type __str_sz = __str.size();
2056 if (__pos > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002057 __throw_out_of_range();
Marshall Clow83445802016-04-07 18:13:41 +00002058 __init(__str.data() + __pos, __str_sz - __pos);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002059 _VSTD::__debug_db_insert_c(this);
Marshall Clow83445802016-04-07 18:13:41 +00002060}
2061
2062template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002063template <class _Tp, class>
Marshall Clow78dbe462016-11-14 18:22:19 +00002064basic_string<_CharT, _Traits, _Allocator>::basic_string(
Marshall Clowe46031a2018-07-02 18:41:15 +00002065 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002066 : __r_(__default_init_tag(), __a)
Marshall Clow78dbe462016-11-14 18:22:19 +00002067{
Marshall Clowe46031a2018-07-02 18:41:15 +00002068 __self_view __sv0 = __t;
2069 __self_view __sv = __sv0.substr(__pos, __n);
Marshall Clow78dbe462016-11-14 18:22:19 +00002070 __init(__sv.data(), __sv.size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002071 _VSTD::__debug_db_insert_c(this);
Marshall Clow78dbe462016-11-14 18:22:19 +00002072}
2073
2074template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002075template <class _Tp, class>
2076basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002077 : __r_(__default_init_tag(), __default_init_tag())
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002078{
Marshall Clowe46031a2018-07-02 18:41:15 +00002079 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002080 __init(__sv.data(), __sv.size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002081 _VSTD::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002082}
2083
2084template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002085template <class _Tp, class>
2086basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002087 : __r_(__default_init_tag(), __a)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002088{
Marshall Clowe46031a2018-07-02 18:41:15 +00002089 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002090 __init(__sv.data(), __sv.size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002091 _VSTD::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002092}
2093
2094template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002095template <class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002096__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002097<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002098 __is_exactly_cpp17_input_iterator<_InputIterator>::value
2099>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002100basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
2101{
2102 __zero();
2103#ifndef _LIBCPP_NO_EXCEPTIONS
2104 try
2105 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002106#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002107 for (; __first != __last; ++__first)
2108 push_back(*__first);
2109#ifndef _LIBCPP_NO_EXCEPTIONS
2110 }
2111 catch (...)
2112 {
2113 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002114 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002115 throw;
2116 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002117#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002118}
2119
2120template <class _CharT, class _Traits, class _Allocator>
2121template <class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002122__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002123<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002124 __is_cpp17_forward_iterator<_ForwardIterator>::value
2125>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002126basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
2127{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002128 size_type __sz = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002129 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002130 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002131 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002132 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002133 {
2134 __set_short_size(__sz);
2135 __p = __get_short_pointer();
2136 }
2137 else
2138 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002139 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
2140 __p = __allocation.ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002141 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002142 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002143 __set_long_size(__sz);
2144 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002145
2146#ifndef _LIBCPP_NO_EXCEPTIONS
2147 try
2148 {
2149#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002150 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002151 traits_type::assign(*__p, *__first);
2152 traits_type::assign(*__p, value_type());
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002153#ifndef _LIBCPP_NO_EXCEPTIONS
2154 }
2155 catch (...)
2156 {
2157 if (__is_long())
2158 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
2159 throw;
2160 }
2161#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002162}
2163
2164template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002165template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002166inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002167basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002168 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002169{
2170 __init(__first, __last);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002171 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002172}
2173
2174template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002175template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002176inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002177basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
2178 const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002179 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002180{
2181 __init(__first, __last);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002182 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002183}
2184
Eric Fiselierfc92be82017-04-19 00:28:44 +00002185#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002186
Howard Hinnantc51e1022010-05-11 19:42:16 +00002187template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002188inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002189basic_string<_CharT, _Traits, _Allocator>::basic_string(
2190 initializer_list<_CharT> __il)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002191 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002192{
2193 __init(__il.begin(), __il.end());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002194 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002195}
2196
2197template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002198inline
Eric Fiselier9d355982017-04-12 23:45:53 +00002199
Eric Fiselier812882b2017-02-17 01:17:10 +00002200basic_string<_CharT, _Traits, _Allocator>::basic_string(
2201 initializer_list<_CharT> __il, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002202 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002203{
2204 __init(__il.begin(), __il.end());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002205 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002206}
2207
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002208#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002209
Howard Hinnantc51e1022010-05-11 19:42:16 +00002210template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002211basic_string<_CharT, _Traits, _Allocator>::~basic_string()
2212{
Louis Dionneba400782020-10-02 15:02:52 -04002213#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002214 if (!__libcpp_is_constant_evaluated())
2215 __get_db()->__erase_c(this);
Howard Hinnant8ea98242013-08-23 17:37:05 +00002216#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002217 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002218 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002219}
2220
2221template <class _CharT, class _Traits, class _Allocator>
2222void
2223basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
2224 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002225 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 +00002226{
2227 size_type __ms = max_size();
2228 if (__delta_cap > __ms - __old_cap - 1)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002229 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002230 pointer __old_p = __get_pointer();
2231 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002232 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002233 __ms - 1;
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002234 auto __allocation = std::__allocate_at_least(__alloc(), __cap + 1);
2235 pointer __p = __allocation.ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002236 __invalidate_all_iterators();
2237 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002238 traits_type::copy(_VSTD::__to_address(__p),
2239 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002240 if (__n_add != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002241 traits_type::copy(_VSTD::__to_address(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002242 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2243 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002244 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2245 _VSTD::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002246 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002247 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002248 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002249 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002250 __old_sz = __n_copy + __n_add + __sec_cp_sz;
2251 __set_long_size(__old_sz);
2252 traits_type::assign(__p[__old_sz], value_type());
2253}
2254
2255template <class _CharT, class _Traits, class _Allocator>
2256void
2257basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2258 size_type __n_copy, size_type __n_del, size_type __n_add)
2259{
2260 size_type __ms = max_size();
Marshall Clow2267c5d2013-11-06 14:24:38 +00002261 if (__delta_cap > __ms - __old_cap)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002262 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002263 pointer __old_p = __get_pointer();
2264 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002265 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002266 __ms - 1;
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002267 auto __allocation = std::__allocate_at_least(__alloc(), __cap + 1);
2268 pointer __p = __allocation.ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002269 __invalidate_all_iterators();
2270 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002271 traits_type::copy(_VSTD::__to_address(__p),
2272 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002273 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2274 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002275 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2276 _VSTD::__to_address(__old_p) + __n_copy + __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002277 __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002278 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002279 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002280 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002281 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002282}
2283
2284// assign
2285
2286template <class _CharT, class _Traits, class _Allocator>
Martijn Vels596e3de2020-02-26 15:55:49 -05002287template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04002288basic_string<_CharT, _Traits, _Allocator>&
2289basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(
Martijn Vels596e3de2020-02-26 15:55:49 -05002290 const value_type* __s, size_type __n) {
Louis Dionne6209e9f2022-03-03 13:39:12 -05002291 size_type __cap = __is_short ? static_cast<size_type>(__min_cap) : __get_long_cap();
Martijn Vels596e3de2020-02-26 15:55:49 -05002292 if (__n < __cap) {
2293 pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
2294 __is_short ? __set_short_size(__n) : __set_long_size(__n);
2295 traits_type::copy(_VSTD::__to_address(__p), __s, __n);
2296 traits_type::assign(__p[__n], value_type());
2297 __invalidate_iterators_past(__n);
2298 } else {
2299 size_type __sz = __is_short ? __get_short_size() : __get_long_size();
2300 __grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
2301 }
Martijn Velsb6a08b62020-04-10 18:36:31 -04002302 return *this;
Martijn Vels596e3de2020-02-26 15:55:49 -05002303}
2304
2305template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002306basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002307basic_string<_CharT, _Traits, _Allocator>::__assign_external(
2308 const value_type* __s, size_type __n) {
2309 size_type __cap = capacity();
2310 if (__cap >= __n) {
2311 value_type* __p = _VSTD::__to_address(__get_pointer());
2312 traits_type::move(__p, __s, __n);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002313 return __null_terminate_at(__p, __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04002314 } else {
2315 size_type __sz = size();
2316 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002317 return *this;
Martijn Velsda7d94f2020-06-19 14:24:03 -04002318 }
Martijn Velsda7d94f2020-06-19 14:24:03 -04002319}
2320
2321template <class _CharT, class _Traits, class _Allocator>
2322basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002323basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002324{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002325 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Nikolas Klauser93826d12022-01-04 17:24:03 +01002326 return (__builtin_constant_p(__n) && __fits_in_sso(__n))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002327 ? __assign_short(__s, __n)
2328 : __assign_external(__s, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002329}
2330
2331template <class _CharT, class _Traits, class _Allocator>
2332basic_string<_CharT, _Traits, _Allocator>&
2333basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2334{
2335 size_type __cap = capacity();
2336 if (__cap < __n)
2337 {
2338 size_type __sz = size();
2339 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2340 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002341 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002342 traits_type::assign(__p, __n, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002343 return __null_terminate_at(__p, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002344}
2345
2346template <class _CharT, class _Traits, class _Allocator>
2347basic_string<_CharT, _Traits, _Allocator>&
2348basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2349{
2350 pointer __p;
2351 if (__is_long())
2352 {
2353 __p = __get_long_pointer();
2354 __set_long_size(1);
2355 }
2356 else
2357 {
2358 __p = __get_short_pointer();
2359 __set_short_size(1);
2360 }
2361 traits_type::assign(*__p, __c);
2362 traits_type::assign(*++__p, value_type());
2363 __invalidate_iterators_past(1);
2364 return *this;
2365}
2366
2367template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002368basic_string<_CharT, _Traits, _Allocator>&
2369basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2370{
Martijn Vels596e3de2020-02-26 15:55:49 -05002371 if (this != &__str) {
2372 __copy_assign_alloc(__str);
2373 if (!__is_long()) {
2374 if (!__str.__is_long()) {
Eric Fiselier03bbc922020-01-15 17:27:10 -05002375 __r_.first().__r = __str.__r_.first().__r;
Martijn Vels596e3de2020-02-26 15:55:49 -05002376 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002377 return __assign_no_alias<true>(__str.data(), __str.size());
Martijn Vels596e3de2020-02-26 15:55:49 -05002378 }
2379 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002380 return __assign_no_alias<false>(__str.data(), __str.size());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002381 }
Martijn Vels596e3de2020-02-26 15:55:49 -05002382 }
2383 return *this;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002384}
2385
Eric Fiselierfc92be82017-04-19 00:28:44 +00002386#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002387
2388template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002389inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002390void
2391basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002392 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002393{
2394 if (__alloc() != __str.__alloc())
2395 assign(__str);
2396 else
2397 __move_assign(__str, true_type());
2398}
2399
2400template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002401inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002402void
2403basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002404#if _LIBCPP_STD_VER > 14
2405 _NOEXCEPT
2406#else
Howard Hinnant3e276872011-06-03 18:40:47 +00002407 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002408#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002409{
marshall2ed41622019-11-27 07:13:00 -08002410 if (__is_long()) {
2411 __alloc_traits::deallocate(__alloc(), __get_long_pointer(),
2412 __get_long_cap());
2413#if _LIBCPP_STD_VER <= 14
2414 if (!is_nothrow_move_assignable<allocator_type>::value) {
2415 __set_short_size(0);
2416 traits_type::assign(__get_short_pointer()[0], value_type());
2417 }
2418#endif
2419 }
2420 __move_assign_alloc(__str);
2421 __r_.first() = __str.__r_.first();
2422 __str.__set_short_size(0);
2423 traits_type::assign(__str.__get_short_pointer()[0], value_type());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002424}
2425
2426template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002427inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002428basic_string<_CharT, _Traits, _Allocator>&
2429basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002430 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002431{
2432 __move_assign(__str, integral_constant<bool,
2433 __alloc_traits::propagate_on_container_move_assignment::value>());
2434 return *this;
2435}
2436
2437#endif
2438
2439template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002440template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002441__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002442<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002443 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002444 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002445>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002446basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2447{
Marshall Clow958362f2016-09-05 01:54:30 +00002448 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002449 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002450 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002451}
2452
2453template <class _CharT, class _Traits, class _Allocator>
2454template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002455__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002456<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002457 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002458 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002459>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002460basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2461{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002462 size_type __cap = capacity();
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002463 size_type __n = __string_is_trivial_iterator<_ForwardIterator>::value ?
2464 static_cast<size_type>(_VSTD::distance(__first, __last)) : 0;
2465
2466 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2467 (__cap >= __n || !__addr_in_range(*__first)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002468 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002469 if (__cap < __n)
2470 {
2471 size_type __sz = size();
2472 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2473 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002474 pointer __p = __get_pointer();
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002475 for (; __first != __last; ++__p, (void) ++__first)
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002476 traits_type::assign(*__p, *__first);
2477 traits_type::assign(*__p, value_type());
2478 __set_size(__n);
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04002479 __invalidate_iterators_past(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002480 }
2481 else
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002482 {
2483 const basic_string __temp(__first, __last, __alloc());
2484 assign(__temp.data(), __temp.size());
2485 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002486 return *this;
2487}
2488
2489template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002490basic_string<_CharT, _Traits, _Allocator>&
2491basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2492{
2493 size_type __sz = __str.size();
2494 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002495 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002496 return assign(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002497}
2498
2499template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002500template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002501__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002502<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002503 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
2504 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00002505 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002506>
Marshall Clow82513342016-09-24 22:45:42 +00002507basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002508{
Marshall Clow82513342016-09-24 22:45:42 +00002509 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002510 size_type __sz = __sv.size();
2511 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002512 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002513 return assign(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2514}
2515
2516
2517template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002518basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002519basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s) {
2520 return __assign_external(__s, traits_type::length(__s));
2521}
2522
2523template <class _CharT, class _Traits, class _Allocator>
2524basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002525basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002526{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002527 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Louis Dionne3e0c4332021-08-31 10:49:06 -04002528 return __builtin_constant_p(*__s)
Nikolas Klauser93826d12022-01-04 17:24:03 +01002529 ? (__fits_in_sso(traits_type::length(__s))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002530 ? __assign_short(__s, traits_type::length(__s))
2531 : __assign_external(__s, traits_type::length(__s)))
2532 : __assign_external(__s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002533}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002534// append
2535
2536template <class _CharT, class _Traits, class _Allocator>
2537basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002538basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002539{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002540 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002541 size_type __cap = capacity();
2542 size_type __sz = size();
2543 if (__cap - __sz >= __n)
2544 {
2545 if (__n)
2546 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002547 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002548 traits_type::copy(__p + __sz, __s, __n);
2549 __sz += __n;
2550 __set_size(__sz);
2551 traits_type::assign(__p[__sz], value_type());
2552 }
2553 }
2554 else
2555 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2556 return *this;
2557}
2558
2559template <class _CharT, class _Traits, class _Allocator>
2560basic_string<_CharT, _Traits, _Allocator>&
2561basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2562{
2563 if (__n)
2564 {
2565 size_type __cap = capacity();
2566 size_type __sz = size();
2567 if (__cap - __sz < __n)
2568 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2569 pointer __p = __get_pointer();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002570 traits_type::assign(_VSTD::__to_address(__p) + __sz, __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002571 __sz += __n;
2572 __set_size(__sz);
2573 traits_type::assign(__p[__sz], value_type());
2574 }
2575 return *this;
2576}
2577
2578template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00002579inline void
2580basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n)
2581{
2582 if (__n)
2583 {
2584 size_type __cap = capacity();
2585 size_type __sz = size();
2586 if (__cap - __sz < __n)
2587 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2588 pointer __p = __get_pointer();
2589 __sz += __n;
2590 __set_size(__sz);
2591 traits_type::assign(__p[__sz], value_type());
2592 }
2593}
2594
2595template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002596void
2597basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2598{
Howard Hinnant68bf1812013-04-30 21:44:48 +00002599 bool __is_short = !__is_long();
2600 size_type __cap;
2601 size_type __sz;
2602 if (__is_short)
2603 {
2604 __cap = __min_cap - 1;
2605 __sz = __get_short_size();
2606 }
2607 else
2608 {
2609 __cap = __get_long_cap() - 1;
2610 __sz = __get_long_size();
2611 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002612 if (__sz == __cap)
Howard Hinnant68bf1812013-04-30 21:44:48 +00002613 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002614 __grow_by(__cap, 1, __sz, __sz, 0);
Louis Dionne82d2b2c2021-11-16 11:26:56 -05002615 __is_short = false; // the string is always long after __grow_by
Howard Hinnant68bf1812013-04-30 21:44:48 +00002616 }
2617 pointer __p;
2618 if (__is_short)
2619 {
2620 __p = __get_short_pointer() + __sz;
2621 __set_short_size(__sz+1);
2622 }
2623 else
2624 {
2625 __p = __get_long_pointer() + __sz;
2626 __set_long_size(__sz+1);
2627 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002628 traits_type::assign(*__p, __c);
2629 traits_type::assign(*++__p, value_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002630}
2631
Howard Hinnantc51e1022010-05-11 19:42:16 +00002632template <class _CharT, class _Traits, class _Allocator>
2633template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002634__enable_if_t
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002635<
2636 __is_cpp17_forward_iterator<_ForwardIterator>::value,
2637 basic_string<_CharT, _Traits, _Allocator>&
2638>
2639basic_string<_CharT, _Traits, _Allocator>::append(
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002640 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002641{
2642 size_type __sz = size();
2643 size_type __cap = capacity();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002644 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002645 if (__n)
2646 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002647 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2648 !__addr_in_range(*__first))
Marshall Clow958362f2016-09-05 01:54:30 +00002649 {
2650 if (__cap - __sz < __n)
2651 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2652 pointer __p = __get_pointer() + __sz;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002653 for (; __first != __last; ++__p, (void) ++__first)
Marshall Clow958362f2016-09-05 01:54:30 +00002654 traits_type::assign(*__p, *__first);
2655 traits_type::assign(*__p, value_type());
2656 __set_size(__sz + __n);
2657 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002658 else
2659 {
2660 const basic_string __temp(__first, __last, __alloc());
2661 append(__temp.data(), __temp.size());
2662 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002663 }
2664 return *this;
2665}
2666
2667template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002668inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002669basic_string<_CharT, _Traits, _Allocator>&
2670basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2671{
2672 return append(__str.data(), __str.size());
2673}
2674
2675template <class _CharT, class _Traits, class _Allocator>
2676basic_string<_CharT, _Traits, _Allocator>&
2677basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2678{
2679 size_type __sz = __str.size();
2680 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002681 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002682 return append(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002683}
2684
2685template <class _CharT, class _Traits, class _Allocator>
Marshall Clow62953962016-10-03 23:40:48 +00002686template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002687 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002688 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002689 __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 +00002690 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002691 >
Marshall Clow82513342016-09-24 22:45:42 +00002692basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002693{
Marshall Clow82513342016-09-24 22:45:42 +00002694 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002695 size_type __sz = __sv.size();
2696 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002697 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002698 return append(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2699}
2700
2701template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002702basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002703basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002704{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002705 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002706 return append(__s, traits_type::length(__s));
2707}
2708
2709// insert
2710
2711template <class _CharT, class _Traits, class _Allocator>
2712basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002713basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002714{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002715 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002716 size_type __sz = size();
2717 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002718 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002719 size_type __cap = capacity();
2720 if (__cap - __sz >= __n)
2721 {
2722 if (__n)
2723 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002724 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002725 size_type __n_move = __sz - __pos;
2726 if (__n_move != 0)
2727 {
2728 if (__p + __pos <= __s && __s < __p + __sz)
2729 __s += __n;
2730 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2731 }
2732 traits_type::move(__p + __pos, __s, __n);
2733 __sz += __n;
2734 __set_size(__sz);
2735 traits_type::assign(__p[__sz], value_type());
2736 }
2737 }
2738 else
2739 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2740 return *this;
2741}
2742
2743template <class _CharT, class _Traits, class _Allocator>
2744basic_string<_CharT, _Traits, _Allocator>&
2745basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2746{
2747 size_type __sz = size();
2748 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002749 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002750 if (__n)
2751 {
2752 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002753 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002754 if (__cap - __sz >= __n)
2755 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002756 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002757 size_type __n_move = __sz - __pos;
2758 if (__n_move != 0)
2759 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2760 }
2761 else
2762 {
2763 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002764 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002765 }
2766 traits_type::assign(__p + __pos, __n, __c);
2767 __sz += __n;
2768 __set_size(__sz);
2769 traits_type::assign(__p[__sz], value_type());
2770 }
2771 return *this;
2772}
2773
2774template <class _CharT, class _Traits, class _Allocator>
2775template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002776__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002777<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002778 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Marshall Clow039b2f02016-01-13 21:54:34 +00002779 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002780>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002781basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2782{
Nikolas Klausereed25832021-12-15 01:32:30 +01002783 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2784 "string::insert(iterator, range) called with an iterator not"
2785 " referring to this string");
2786 const basic_string __temp(__first, __last, __alloc());
2787 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002788}
2789
2790template <class _CharT, class _Traits, class _Allocator>
2791template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002792__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002793<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002794 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002795 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002796>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002797basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2798{
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002799 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2800 "string::insert(iterator, range) called with an iterator not referring to this string");
Nikolas Klausereed25832021-12-15 01:32:30 +01002801
Howard Hinnantc51e1022010-05-11 19:42:16 +00002802 size_type __ip = static_cast<size_type>(__pos - begin());
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002803 size_type __n = static_cast<size_type>(std::distance(__first, __last));
2804 if (__n == 0)
2805 return begin() + __ip;
2806
2807 if (__string_is_trivial_iterator<_ForwardIterator>::value && !__addr_in_range(*__first))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002808 {
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002809 return __insert_from_safe_copy(__n, __ip, __first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002810 }
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002811 else
2812 {
2813 const basic_string __temp(__first, __last, __alloc());
2814 return __insert_from_safe_copy(__n, __ip, __temp.begin(), __temp.end());
2815 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002816}
2817
2818template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002819inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002820basic_string<_CharT, _Traits, _Allocator>&
2821basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2822{
2823 return insert(__pos1, __str.data(), __str.size());
2824}
2825
2826template <class _CharT, class _Traits, class _Allocator>
2827basic_string<_CharT, _Traits, _Allocator>&
2828basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2829 size_type __pos2, size_type __n)
2830{
2831 size_type __str_sz = __str.size();
2832 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002833 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002834 return insert(__pos1, __str.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002835}
2836
2837template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002838template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002839__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002840<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002841 __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 +00002842 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002843>
Marshall Clow82513342016-09-24 22:45:42 +00002844basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002845 size_type __pos2, size_type __n)
2846{
Marshall Clow82513342016-09-24 22:45:42 +00002847 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002848 size_type __str_sz = __sv.size();
2849 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002850 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002851 return insert(__pos1, __sv.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
2852}
2853
2854template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002855basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002856basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002857{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002858 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002859 return insert(__pos, __s, traits_type::length(__s));
2860}
2861
2862template <class _CharT, class _Traits, class _Allocator>
2863typename basic_string<_CharT, _Traits, _Allocator>::iterator
2864basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2865{
Louis Dionnef4cc6fb2022-02-15 15:47:45 -05002866 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2867 "string::insert(iterator, character) called with an iterator not"
2868 " referring to this string");
2869
Howard Hinnantc51e1022010-05-11 19:42:16 +00002870 size_type __ip = static_cast<size_type>(__pos - begin());
2871 size_type __sz = size();
2872 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002873 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002874 if (__cap == __sz)
2875 {
2876 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002877 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002878 }
2879 else
2880 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002881 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002882 size_type __n_move = __sz - __ip;
2883 if (__n_move != 0)
2884 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2885 }
2886 traits_type::assign(__p[__ip], __c);
2887 traits_type::assign(__p[++__sz], value_type());
2888 __set_size(__sz);
2889 return begin() + static_cast<difference_type>(__ip);
2890}
2891
2892template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002893inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002894typename basic_string<_CharT, _Traits, _Allocator>::iterator
2895basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2896{
Nikolas Klausereed25832021-12-15 01:32:30 +01002897 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2898 "string::insert(iterator, n, value) called with an iterator not"
2899 " referring to this string");
2900 difference_type __p = __pos - begin();
2901 insert(static_cast<size_type>(__p), __n, __c);
2902 return begin() + __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002903}
2904
2905// replace
2906
2907template <class _CharT, class _Traits, class _Allocator>
2908basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002909basic_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 +00002910 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00002911{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002912 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002913 size_type __sz = size();
2914 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002915 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002916 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002917 size_type __cap = capacity();
2918 if (__cap - __sz + __n1 >= __n2)
2919 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002920 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002921 if (__n1 != __n2)
2922 {
2923 size_type __n_move = __sz - __pos - __n1;
2924 if (__n_move != 0)
2925 {
2926 if (__n1 > __n2)
2927 {
2928 traits_type::move(__p + __pos, __s, __n2);
2929 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002930 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002931 }
2932 if (__p + __pos < __s && __s < __p + __sz)
2933 {
2934 if (__p + __pos + __n1 <= __s)
2935 __s += __n2 - __n1;
2936 else // __p + __pos < __s < __p + __pos + __n1
2937 {
2938 traits_type::move(__p + __pos, __s, __n1);
2939 __pos += __n1;
2940 __s += __n2;
2941 __n2 -= __n1;
2942 __n1 = 0;
2943 }
2944 }
2945 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2946 }
2947 }
2948 traits_type::move(__p + __pos, __s, __n2);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002949 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002950 }
2951 else
2952 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
2953 return *this;
2954}
2955
2956template <class _CharT, class _Traits, class _Allocator>
2957basic_string<_CharT, _Traits, _Allocator>&
2958basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
2959{
2960 size_type __sz = size();
2961 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002962 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002963 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002964 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002965 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002966 if (__cap - __sz + __n1 >= __n2)
2967 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002968 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002969 if (__n1 != __n2)
2970 {
2971 size_type __n_move = __sz - __pos - __n1;
2972 if (__n_move != 0)
2973 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2974 }
2975 }
2976 else
2977 {
2978 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002979 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002980 }
2981 traits_type::assign(__p + __pos, __n2, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002982 return __null_terminate_at(__p, __sz - (__n1 - __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002983}
2984
2985template <class _CharT, class _Traits, class _Allocator>
2986template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002987__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002988<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002989 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002990 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002991>
Howard Hinnant990d6e82010-11-17 21:11:40 +00002992basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002993 _InputIterator __j1, _InputIterator __j2)
2994{
Marshall Clow958362f2016-09-05 01:54:30 +00002995 const basic_string __temp(__j1, __j2, __alloc());
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002996 return replace(__i1, __i2, __temp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002997}
2998
2999template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003000inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003001basic_string<_CharT, _Traits, _Allocator>&
3002basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
3003{
3004 return replace(__pos1, __n1, __str.data(), __str.size());
3005}
3006
3007template <class _CharT, class _Traits, class _Allocator>
3008basic_string<_CharT, _Traits, _Allocator>&
3009basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
3010 size_type __pos2, size_type __n2)
3011{
3012 size_type __str_sz = __str.size();
3013 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003014 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003015 return replace(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003016}
3017
3018template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003019template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003020__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003021<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003022 __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 +00003023 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003024>
Marshall Clow82513342016-09-24 22:45:42 +00003025basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003026 size_type __pos2, size_type __n2)
3027{
Marshall Clow82513342016-09-24 22:45:42 +00003028 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003029 size_type __str_sz = __sv.size();
3030 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003031 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003032 return replace(__pos1, __n1, __sv.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
3033}
3034
3035template <class _CharT, class _Traits, class _Allocator>
3036basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003037basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003038{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003039 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003040 return replace(__pos, __n1, __s, traits_type::length(__s));
3041}
3042
3043template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003044inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003045basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003046basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003047{
3048 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
3049 __str.data(), __str.size());
3050}
3051
3052template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003053inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003054basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003055basic_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 +00003056{
3057 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
3058}
3059
3060template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003061inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003062basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003063basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003064{
3065 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __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, size_type __n, value_type __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003072{
3073 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
3074}
3075
3076// erase
3077
Martijn Velsa81fc792020-02-26 13:25:43 -05003078// 'externally instantiated' erase() implementation, called when __n != npos.
3079// Does not check __pos against size()
Howard Hinnantc51e1022010-05-11 19:42:16 +00003080template <class _CharT, class _Traits, class _Allocator>
Martijn Velsa81fc792020-02-26 13:25:43 -05003081void
3082basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(
3083 size_type __pos, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003084{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003085 if (__n)
3086 {
Martijn Velsa81fc792020-02-26 13:25:43 -05003087 size_type __sz = size();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003088 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003089 __n = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003090 size_type __n_move = __sz - __pos - __n;
3091 if (__n_move != 0)
3092 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003093 __null_terminate_at(__p, __sz - __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003094 }
Martijn Velsa81fc792020-02-26 13:25:43 -05003095}
3096
3097template <class _CharT, class _Traits, class _Allocator>
3098basic_string<_CharT, _Traits, _Allocator>&
3099basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos,
3100 size_type __n) {
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003101 if (__pos > size())
3102 __throw_out_of_range();
Martijn Velsa81fc792020-02-26 13:25:43 -05003103 if (__n == npos) {
3104 __erase_to_end(__pos);
3105 } else {
3106 __erase_external_with_move(__pos, __n);
3107 }
3108 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003109}
3110
3111template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003112inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003113typename basic_string<_CharT, _Traits, _Allocator>::iterator
3114basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
3115{
Nikolas Klausereed25832021-12-15 01:32:30 +01003116 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3117 "string::erase(iterator) called with an iterator not"
3118 " referring to this string");
3119
3120 _LIBCPP_ASSERT(__pos != end(), "string::erase(iterator) called with a non-dereferenceable iterator");
3121 iterator __b = begin();
3122 size_type __r = static_cast<size_type>(__pos - __b);
3123 erase(__r, 1);
3124 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003125}
3126
3127template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003128inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003129typename basic_string<_CharT, _Traits, _Allocator>::iterator
3130basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
3131{
Nikolas Klausereed25832021-12-15 01:32:30 +01003132 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
3133 "string::erase(iterator, iterator) called with an iterator not"
3134 " referring to this string");
3135
3136 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
3137 iterator __b = begin();
3138 size_type __r = static_cast<size_type>(__first - __b);
3139 erase(__r, static_cast<size_type>(__last - __first));
3140 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003141}
3142
3143template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003144inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003145void
3146basic_string<_CharT, _Traits, _Allocator>::pop_back()
3147{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003148 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003149 __erase_to_end(size() - 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003150}
3151
3152template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003153inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003154void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003155basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003156{
3157 __invalidate_all_iterators();
3158 if (__is_long())
3159 {
3160 traits_type::assign(*__get_long_pointer(), value_type());
3161 __set_long_size(0);
3162 }
3163 else
3164 {
3165 traits_type::assign(*__get_short_pointer(), value_type());
3166 __set_short_size(0);
3167 }
3168}
3169
3170template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003171inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003172void
3173basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
3174{
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003175 __null_terminate_at(_VSTD::__to_address(__get_pointer()), __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003176}
3177
3178template <class _CharT, class _Traits, class _Allocator>
3179void
3180basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
3181{
3182 size_type __sz = size();
3183 if (__n > __sz)
3184 append(__n - __sz, __c);
3185 else
3186 __erase_to_end(__n);
3187}
3188
3189template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00003190inline void
3191basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n)
3192{
3193 size_type __sz = size();
3194 if (__n > __sz) {
3195 __append_default_init(__n - __sz);
3196 } else
3197 __erase_to_end(__n);
3198}
3199
3200template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003201inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003202typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003203basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003204{
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003205 size_type __m = __alloc_traits::max_size(__alloc());
Eric Fiseliere9cc5922017-10-17 13:16:01 +00003206#ifdef _LIBCPP_BIG_ENDIAN
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003207 return (__m <= ~__long_mask ? __m : __m/2) - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003208#else
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003209 return __m - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003210#endif
3211}
3212
3213template <class _CharT, class _Traits, class _Allocator>
3214void
Marek Kurdejc9848142020-11-26 10:07:16 +01003215basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __requested_capacity)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003216{
Marek Kurdejc9848142020-11-26 10:07:16 +01003217 if (__requested_capacity > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003218 __throw_length_error();
Marek Kurdejc9848142020-11-26 10:07:16 +01003219
Louis Dionne05b6d3e2022-01-14 12:30:22 -05003220 // Make sure reserve(n) never shrinks. This is technically only required in C++20
3221 // and later (since P0966R1), however we provide consistent behavior in all Standard
3222 // modes because this function is instantiated in the shared library.
3223 if (__requested_capacity <= capacity())
3224 return;
Marek Kurdejc9848142020-11-26 10:07:16 +01003225
3226 size_type __target_capacity = _VSTD::max(__requested_capacity, size());
3227 __target_capacity = __recommend(__target_capacity);
3228 if (__target_capacity == capacity()) return;
3229
3230 __shrink_or_extend(__target_capacity);
3231}
3232
3233template <class _CharT, class _Traits, class _Allocator>
Louis Dionnee38b8642021-12-13 14:15:21 -05003234inline
Marek Kurdejc9848142020-11-26 10:07:16 +01003235void
3236basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT
3237{
3238 size_type __target_capacity = __recommend(size());
3239 if (__target_capacity == capacity()) return;
3240
3241 __shrink_or_extend(__target_capacity);
3242}
3243
3244template <class _CharT, class _Traits, class _Allocator>
Louis Dionnee38b8642021-12-13 14:15:21 -05003245inline
Marek Kurdejc9848142020-11-26 10:07:16 +01003246void
3247basic_string<_CharT, _Traits, _Allocator>::__shrink_or_extend(size_type __target_capacity)
3248{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003249 size_type __cap = capacity();
3250 size_type __sz = size();
Marek Kurdejc9848142020-11-26 10:07:16 +01003251
3252 pointer __new_data, __p;
3253 bool __was_long, __now_long;
3254 if (__target_capacity == __min_cap - 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003255 {
Marek Kurdejc9848142020-11-26 10:07:16 +01003256 __was_long = true;
3257 __now_long = false;
3258 __new_data = __get_short_pointer();
3259 __p = __get_long_pointer();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003260 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003261 else
3262 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02003263 if (__target_capacity > __cap) {
3264 auto __allocation = std::__allocate_at_least(__alloc(), __target_capacity + 1);
3265 __new_data = __allocation.ptr;
3266 __target_capacity = __allocation.count - 1;
3267 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003268 else
3269 {
3270 #ifndef _LIBCPP_NO_EXCEPTIONS
3271 try
3272 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003273 #endif // _LIBCPP_NO_EXCEPTIONS
Nikolas Klauserc513eba2022-04-09 09:41:19 +02003274 auto __allocation = std::__allocate_at_least(__alloc(), __target_capacity + 1);
3275 __new_data = __allocation.ptr;
3276 __target_capacity = __allocation.count - 1;
Marek Kurdejc9848142020-11-26 10:07:16 +01003277 #ifndef _LIBCPP_NO_EXCEPTIONS
3278 }
3279 catch (...)
3280 {
3281 return;
3282 }
3283 #else // _LIBCPP_NO_EXCEPTIONS
3284 if (__new_data == nullptr)
3285 return;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003286 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003287 }
3288 __now_long = true;
3289 __was_long = __is_long();
3290 __p = __get_pointer();
3291 }
3292 traits_type::copy(_VSTD::__to_address(__new_data),
3293 _VSTD::__to_address(__p), size()+1);
3294 if (__was_long)
3295 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
3296 if (__now_long)
3297 {
3298 __set_long_cap(__target_capacity+1);
3299 __set_long_size(__sz);
3300 __set_long_pointer(__new_data);
3301 }
3302 else
3303 __set_short_size(__sz);
3304 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003305}
3306
3307template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003308inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003309typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003310basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003311{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003312 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003313 return *(data() + __pos);
3314}
3315
3316template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003317inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003318typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003319basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003320{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003321 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003322 return *(__get_pointer() + __pos);
3323}
3324
3325template <class _CharT, class _Traits, class _Allocator>
3326typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3327basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3328{
3329 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003330 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003331 return (*this)[__n];
3332}
3333
3334template <class _CharT, class _Traits, class _Allocator>
3335typename basic_string<_CharT, _Traits, _Allocator>::reference
3336basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
3337{
3338 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003339 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003340 return (*this)[__n];
3341}
3342
3343template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003344inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003345typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003346basic_string<_CharT, _Traits, _Allocator>::front() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003347{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003348 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003349 return *__get_pointer();
3350}
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 Clow05cf6692019-03-19 03:30:07 +00003355basic_string<_CharT, _Traits, _Allocator>::front() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003356{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003357 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003358 return *data();
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 Clow05cf6692019-03-19 03:30:07 +00003364basic_string<_CharT, _Traits, _Allocator>::back() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003365{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003366 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003367 return *(__get_pointer() + size() - 1);
3368}
3369
3370template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003371inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003372typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003373basic_string<_CharT, _Traits, _Allocator>::back() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003374{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003375 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003376 return *(data() + size() - 1);
3377}
3378
3379template <class _CharT, class _Traits, class _Allocator>
3380typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003381basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003382{
3383 size_type __sz = size();
3384 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003385 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003386 size_type __rlen = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003387 traits_type::copy(__s, data() + __pos, __rlen);
3388 return __rlen;
3389}
3390
3391template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003392inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003393basic_string<_CharT, _Traits, _Allocator>
3394basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3395{
3396 return basic_string(*this, __pos, __n, __alloc());
3397}
3398
3399template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003400inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003401void
3402basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003403#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00003404 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00003405#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00003406 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003407 __is_nothrow_swappable<allocator_type>::value)
3408#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003409{
Louis Dionneba400782020-10-02 15:02:52 -04003410#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01003411 if (!__libcpp_is_constant_evaluated()) {
3412 if (!__is_long())
3413 __get_db()->__invalidate_all(this);
3414 if (!__str.__is_long())
3415 __get_db()->__invalidate_all(&__str);
3416 __get_db()->swap(this, &__str);
3417 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00003418#endif
Eric Fiselier9bf691f2016-12-28 05:53:01 +00003419 _LIBCPP_ASSERT(
3420 __alloc_traits::propagate_on_container_swap::value ||
3421 __alloc_traits::is_always_equal::value ||
3422 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003423 _VSTD::swap(__r_.first(), __str.__r_.first());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05003424 _VSTD::__swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003425}
3426
3427// find
3428
3429template <class _Traits>
3430struct _LIBCPP_HIDDEN __traits_eq
3431{
3432 typedef typename _Traits::char_type char_type;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003433 _LIBCPP_INLINE_VISIBILITY
3434 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3435 {return _Traits::eq(__x, __y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003436};
3437
3438template<class _CharT, class _Traits, class _Allocator>
3439typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003440basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003441 size_type __pos,
3442 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003443{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003444 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003445 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003446 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003447}
3448
3449template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003450inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003451typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003452basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3453 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003454{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003455 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003456 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003457}
3458
3459template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003460template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003461__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003462<
3463 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3464 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003465>
Marshall Clowe46031a2018-07-02 18:41:15 +00003466basic_string<_CharT, _Traits, _Allocator>::find(const _Tp &__t,
zoecarver1997e0a2021-02-05 11:54:47 -08003467 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003468{
Marshall Clowe46031a2018-07-02 18:41:15 +00003469 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003470 return __str_find<value_type, size_type, traits_type, npos>
3471 (data(), size(), __sv.data(), __pos, __sv.size());
3472}
3473
3474template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003475inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003476typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003477basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003478 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003479{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003480 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003481 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003482 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003483}
3484
3485template<class _CharT, class _Traits, class _Allocator>
3486typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003487basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3488 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003489{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003490 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003491 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003492}
3493
3494// rfind
3495
3496template<class _CharT, class _Traits, class _Allocator>
3497typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003498basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003499 size_type __pos,
3500 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003501{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003502 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003503 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003504 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003505}
3506
3507template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003508inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003509typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003510basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3511 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003512{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003513 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003514 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003515}
3516
3517template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003518template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003519__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003520<
3521 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3522 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003523>
Marshall Clowe46031a2018-07-02 18:41:15 +00003524basic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003525 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003526{
Marshall Clowe46031a2018-07-02 18:41:15 +00003527 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003528 return __str_rfind<value_type, size_type, traits_type, npos>
3529 (data(), size(), __sv.data(), __pos, __sv.size());
3530}
3531
3532template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003533inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003534typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003535basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003536 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003537{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003538 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003539 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003540 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003541}
3542
3543template<class _CharT, class _Traits, class _Allocator>
3544typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003545basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3546 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003547{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003548 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003549 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003550}
3551
3552// find_first_of
3553
3554template<class _CharT, class _Traits, class _Allocator>
3555typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003556basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003557 size_type __pos,
3558 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003559{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003560 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003561 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003562 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003563}
3564
3565template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003566inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003567typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003568basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3569 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003570{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003571 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003572 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003573}
3574
3575template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003576template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003577__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003578<
3579 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3580 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003581>
Marshall Clowe46031a2018-07-02 18:41:15 +00003582basic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003583 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003584{
Marshall Clowe46031a2018-07-02 18:41:15 +00003585 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003586 return __str_find_first_of<value_type, size_type, traits_type, npos>
3587 (data(), size(), __sv.data(), __pos, __sv.size());
3588}
3589
3590template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003591inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003592typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003593basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003594 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003595{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003596 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003597 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003598 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003599}
3600
3601template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003602inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003603typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003604basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3605 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003606{
3607 return find(__c, __pos);
3608}
3609
3610// find_last_of
3611
3612template<class _CharT, class _Traits, class _Allocator>
3613typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003614basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003615 size_type __pos,
3616 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003617{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003618 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003619 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003620 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003621}
3622
3623template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003624inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003625typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003626basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3627 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003628{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003629 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003630 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003631}
3632
3633template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003634template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003635__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003636<
3637 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3638 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003639>
Marshall Clowe46031a2018-07-02 18:41:15 +00003640basic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003641 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003642{
Marshall Clowe46031a2018-07-02 18:41:15 +00003643 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003644 return __str_find_last_of<value_type, size_type, traits_type, npos>
3645 (data(), size(), __sv.data(), __pos, __sv.size());
3646}
3647
3648template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003649inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003650typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003651basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003652 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003653{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003654 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003655 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003656 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003657}
3658
3659template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003660inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003661typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003662basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3663 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003664{
3665 return rfind(__c, __pos);
3666}
3667
3668// find_first_not_of
3669
3670template<class _CharT, class _Traits, class _Allocator>
3671typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003672basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003673 size_type __pos,
3674 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003675{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003676 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003677 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003678 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003679}
3680
3681template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003682inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003683typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003684basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3685 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003686{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003687 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003688 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003689}
3690
3691template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003692template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003693__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003694<
3695 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3696 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003697>
Marshall Clowe46031a2018-07-02 18:41:15 +00003698basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003699 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003700{
Marshall Clowe46031a2018-07-02 18:41:15 +00003701 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003702 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3703 (data(), size(), __sv.data(), __pos, __sv.size());
3704}
3705
3706template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003707inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003708typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003709basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003710 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003711{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003712 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003713 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003714 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003715}
3716
3717template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003718inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003719typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003720basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3721 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003722{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003723 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003724 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003725}
3726
3727// find_last_not_of
3728
3729template<class _CharT, class _Traits, class _Allocator>
3730typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003731basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003732 size_type __pos,
3733 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003734{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003735 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003736 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003737 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003738}
3739
3740template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003741inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003742typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003743basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3744 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003745{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003746 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003747 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003748}
3749
3750template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003751template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003752__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003753<
3754 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3755 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003756>
Marshall Clowe46031a2018-07-02 18:41:15 +00003757basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003758 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003759{
Marshall Clowe46031a2018-07-02 18:41:15 +00003760 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003761 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3762 (data(), size(), __sv.data(), __pos, __sv.size());
3763}
3764
3765template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003766inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003767typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003768basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003769 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003770{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003771 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003772 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003773 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003774}
3775
3776template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003777inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003778typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003779basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3780 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003781{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003782 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003783 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003784}
3785
3786// compare
3787
3788template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003789template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003790__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003791<
3792 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3793 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003794>
zoecarver1997e0a2021-02-05 11:54:47 -08003795basic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003796{
Marshall Clowe46031a2018-07-02 18:41:15 +00003797 __self_view __sv = __t;
Howard Hinnantb0485532011-07-24 21:45:06 +00003798 size_t __lhs_sz = size();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003799 size_t __rhs_sz = __sv.size();
3800 int __result = traits_type::compare(data(), __sv.data(),
Howard Hinnantb0485532011-07-24 21:45:06 +00003801 _VSTD::min(__lhs_sz, __rhs_sz));
3802 if (__result != 0)
3803 return __result;
3804 if (__lhs_sz < __rhs_sz)
3805 return -1;
3806 if (__lhs_sz > __rhs_sz)
3807 return 1;
3808 return 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003809}
3810
3811template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003812inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003813int
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003814basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003815{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003816 return compare(__self_view(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003817}
3818
3819template <class _CharT, class _Traits, class _Allocator>
3820int
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003821basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3822 size_type __n1,
Howard Hinnantd17880b2013-06-28 16:59:19 +00003823 const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003824 size_type __n2) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003825{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003826 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003827 size_type __sz = size();
3828 if (__pos1 > __sz || __n2 == npos)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003829 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003830 size_type __rlen = _VSTD::min(__n1, __sz - __pos1);
3831 int __r = traits_type::compare(data() + __pos1, __s, _VSTD::min(__rlen, __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003832 if (__r == 0)
3833 {
3834 if (__rlen < __n2)
3835 __r = -1;
3836 else if (__rlen > __n2)
3837 __r = 1;
3838 }
3839 return __r;
3840}
3841
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003842template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003843template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003844__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003845<
3846 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3847 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003848>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003849basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3850 size_type __n1,
Marshall Clowe46031a2018-07-02 18:41:15 +00003851 const _Tp& __t) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003852{
Marshall Clowe46031a2018-07-02 18:41:15 +00003853 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003854 return compare(__pos1, __n1, __sv.data(), __sv.size());
3855}
3856
3857template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003858inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003859int
3860basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3861 size_type __n1,
3862 const basic_string& __str) const
3863{
3864 return compare(__pos1, __n1, __str.data(), __str.size());
3865}
3866
3867template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003868template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003869__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003870<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003871 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
3872 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00003873 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003874>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003875basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3876 size_type __n1,
Marshall Clow82513342016-09-24 22:45:42 +00003877 const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003878 size_type __pos2,
3879 size_type __n2) const
3880{
Marshall Clow82513342016-09-24 22:45:42 +00003881 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003882 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3883}
3884
3885template <class _CharT, class _Traits, class _Allocator>
3886int
3887basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3888 size_type __n1,
3889 const basic_string& __str,
3890 size_type __pos2,
3891 size_type __n2) const
3892{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003893 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003894}
3895
3896template <class _CharT, class _Traits, class _Allocator>
3897int
3898basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
3899{
3900 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3901 return compare(0, npos, __s, traits_type::length(__s));
3902}
3903
3904template <class _CharT, class _Traits, class _Allocator>
3905int
3906basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3907 size_type __n1,
3908 const value_type* __s) const
3909{
3910 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3911 return compare(__pos1, __n1, __s, traits_type::length(__s));
3912}
3913
Howard Hinnantc51e1022010-05-11 19:42:16 +00003914// __invariants
3915
3916template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003917inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003918bool
3919basic_string<_CharT, _Traits, _Allocator>::__invariants() const
3920{
3921 if (size() > capacity())
3922 return false;
3923 if (capacity() < __min_cap - 1)
3924 return false;
Bruce Mitchener170d8972020-11-24 12:53:53 -05003925 if (data() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003926 return false;
Louis Dionne663415f2020-10-05 16:16:13 -04003927 if (data()[size()] != value_type())
Howard Hinnantc51e1022010-05-11 19:42:16 +00003928 return false;
3929 return true;
3930}
3931
Vedant Kumar55e007e2018-03-08 21:15:26 +00003932// __clear_and_shrink
3933
3934template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003935inline
Louis Dionne173f29e2019-05-29 16:01:36 +00003936void
Marshall Clowe60a7182018-05-29 17:04:37 +00003937basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
Vedant Kumar55e007e2018-03-08 21:15:26 +00003938{
3939 clear();
3940 if(__is_long())
3941 {
3942 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
3943 __set_long_cap(0);
3944 __set_short_size(0);
Louis Dionne663415f2020-10-05 16:16:13 -04003945 traits_type::assign(*__get_short_pointer(), value_type());
Vedant Kumar55e007e2018-03-08 21:15:26 +00003946 }
Louis Dionne173f29e2019-05-29 16:01:36 +00003947}
Vedant Kumar55e007e2018-03-08 21:15:26 +00003948
Howard Hinnantc51e1022010-05-11 19:42:16 +00003949// operator==
3950
3951template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003952inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003953bool
3954operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003955 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003956{
Howard Hinnantaaeb1132013-04-22 23:55:13 +00003957 size_t __lhs_sz = __lhs.size();
3958 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
3959 __rhs.data(),
3960 __lhs_sz) == 0;
3961}
3962
3963template<class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003964inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaaeb1132013-04-22 23:55:13 +00003965bool
3966operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
3967 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
3968{
3969 size_t __lhs_sz = __lhs.size();
3970 if (__lhs_sz != __rhs.size())
3971 return false;
3972 const char* __lp = __lhs.data();
3973 const char* __rp = __rhs.data();
3974 if (__lhs.__is_long())
3975 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
3976 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
3977 if (*__lp != *__rp)
3978 return false;
3979 return true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003980}
3981
3982template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003983inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003984bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003985operator==(const _CharT* __lhs,
3986 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003987{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00003988 typedef basic_string<_CharT, _Traits, _Allocator> _String;
3989 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
3990 size_t __lhs_len = _Traits::length(__lhs);
3991 if (__lhs_len != __rhs.size()) return false;
3992 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003993}
3994
Howard Hinnantc51e1022010-05-11 19:42:16 +00003995template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003996inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003997bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003998operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
3999 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004000{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004001 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4002 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
4003 size_t __rhs_len = _Traits::length(__rhs);
4004 if (__rhs_len != __lhs.size()) return false;
4005 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004006}
4007
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004008template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004009inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004010bool
4011operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004012 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004013{
4014 return !(__lhs == __rhs);
4015}
4016
4017template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004018inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004019bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004020operator!=(const _CharT* __lhs,
4021 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004022{
4023 return !(__lhs == __rhs);
4024}
4025
4026template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004027inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004028bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004029operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4030 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004031{
4032 return !(__lhs == __rhs);
4033}
4034
4035// operator<
4036
4037template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004038inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004039bool
4040operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004041 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004042{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004043 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004044}
4045
4046template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004047inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004048bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004049operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4050 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004051{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004052 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004053}
4054
4055template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004056inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004057bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004058operator< (const _CharT* __lhs,
4059 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004060{
4061 return __rhs.compare(__lhs) > 0;
4062}
4063
Howard Hinnantc51e1022010-05-11 19:42:16 +00004064// operator>
4065
4066template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004067inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004068bool
4069operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004070 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004071{
4072 return __rhs < __lhs;
4073}
4074
4075template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004076inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004077bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004078operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4079 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004080{
4081 return __rhs < __lhs;
4082}
4083
4084template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004085inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004086bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004087operator> (const _CharT* __lhs,
4088 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004089{
4090 return __rhs < __lhs;
4091}
4092
4093// operator<=
4094
4095template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004096inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004097bool
4098operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004099 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004100{
4101 return !(__rhs < __lhs);
4102}
4103
4104template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004105inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004106bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004107operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4108 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004109{
4110 return !(__rhs < __lhs);
4111}
4112
4113template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004114inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004115bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004116operator<=(const _CharT* __lhs,
4117 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004118{
4119 return !(__rhs < __lhs);
4120}
4121
4122// operator>=
4123
4124template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004125inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004126bool
4127operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004128 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004129{
4130 return !(__lhs < __rhs);
4131}
4132
4133template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004134inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004135bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004136operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4137 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004138{
4139 return !(__lhs < __rhs);
4140}
4141
4142template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004143inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004144bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004145operator>=(const _CharT* __lhs,
4146 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004147{
4148 return !(__lhs < __rhs);
4149}
4150
4151// operator +
4152
4153template<class _CharT, class _Traits, class _Allocator>
4154basic_string<_CharT, _Traits, _Allocator>
4155operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4156 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4157{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004158 using _String = basic_string<_CharT, _Traits, _Allocator>;
4159 _String __r(_String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4160 typename _String::size_type __lhs_sz = __lhs.size();
4161 typename _String::size_type __rhs_sz = __rhs.size();
Howard Hinnantc51e1022010-05-11 19:42:16 +00004162 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4163 __r.append(__rhs.data(), __rhs_sz);
4164 return __r;
4165}
4166
4167template<class _CharT, class _Traits, class _Allocator>
4168basic_string<_CharT, _Traits, _Allocator>
4169operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4170{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004171 using _String = basic_string<_CharT, _Traits, _Allocator>;
4172 _String __r(_String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator()));
4173 typename _String::size_type __lhs_sz = _Traits::length(__lhs);
4174 typename _String::size_type __rhs_sz = __rhs.size();
Howard Hinnantc51e1022010-05-11 19:42:16 +00004175 __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz);
4176 __r.append(__rhs.data(), __rhs_sz);
4177 return __r;
4178}
4179
4180template<class _CharT, class _Traits, class _Allocator>
4181basic_string<_CharT, _Traits, _Allocator>
4182operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4183{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004184 using _String = basic_string<_CharT, _Traits, _Allocator>;
4185 _String __r(_String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator()));
4186 typename _String::size_type __rhs_sz = __rhs.size();
Howard Hinnantc51e1022010-05-11 19:42:16 +00004187 __r.__init(&__lhs, 1, 1 + __rhs_sz);
4188 __r.append(__rhs.data(), __rhs_sz);
4189 return __r;
4190}
4191
4192template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004193inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004194basic_string<_CharT, _Traits, _Allocator>
4195operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
4196{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004197 using _String = basic_string<_CharT, _Traits, _Allocator>;
4198 _String __r(_String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4199 typename _String::size_type __lhs_sz = __lhs.size();
4200 typename _String::size_type __rhs_sz = _Traits::length(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004201 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4202 __r.append(__rhs, __rhs_sz);
4203 return __r;
4204}
4205
4206template<class _CharT, class _Traits, class _Allocator>
4207basic_string<_CharT, _Traits, _Allocator>
4208operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
4209{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004210 using _String = basic_string<_CharT, _Traits, _Allocator>;
4211 _String __r(_String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4212 typename _String::size_type __lhs_sz = __lhs.size();
Howard Hinnantc51e1022010-05-11 19:42:16 +00004213 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1);
4214 __r.push_back(__rhs);
4215 return __r;
4216}
4217
Eric Fiselierfc92be82017-04-19 00:28:44 +00004218#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004219
4220template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004221inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004222basic_string<_CharT, _Traits, _Allocator>
4223operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4224{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004225 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004226}
4227
4228template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004229inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004230basic_string<_CharT, _Traits, _Allocator>
4231operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4232{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004233 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004234}
4235
4236template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004237inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004238basic_string<_CharT, _Traits, _Allocator>
4239operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4240{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004241 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004242}
4243
4244template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004245inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004246basic_string<_CharT, _Traits, _Allocator>
4247operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4248{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004249 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004250}
4251
4252template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004253inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004254basic_string<_CharT, _Traits, _Allocator>
4255operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4256{
4257 __rhs.insert(__rhs.begin(), __lhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004258 return _VSTD::move(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004259}
4260
4261template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004262inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004263basic_string<_CharT, _Traits, _Allocator>
4264operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
4265{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004266 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004267}
4268
4269template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004270inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004271basic_string<_CharT, _Traits, _Allocator>
4272operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
4273{
4274 __lhs.push_back(__rhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004275 return _VSTD::move(__lhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004276}
4277
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004278#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004279
4280// swap
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 +00004284void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004285swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +00004286 basic_string<_CharT, _Traits, _Allocator>& __rhs)
4287 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004288{
4289 __lhs.swap(__rhs);
4290}
4291
Bruce Mitchener170d8972020-11-24 12:53:53 -05004292_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = nullptr, int __base = 10);
4293_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = nullptr, int __base = 10);
4294_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = nullptr, int __base = 10);
4295_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = nullptr, int __base = 10);
4296_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004297
Bruce Mitchener170d8972020-11-24 12:53:53 -05004298_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = nullptr);
4299_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = nullptr);
4300_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004301
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004302_LIBCPP_FUNC_VIS string to_string(int __val);
4303_LIBCPP_FUNC_VIS string to_string(unsigned __val);
4304_LIBCPP_FUNC_VIS string to_string(long __val);
4305_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
4306_LIBCPP_FUNC_VIS string to_string(long long __val);
4307_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
4308_LIBCPP_FUNC_VIS string to_string(float __val);
4309_LIBCPP_FUNC_VIS string to_string(double __val);
4310_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004311
Louis Dionne89258142021-08-23 15:32:36 -04004312#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Bruce Mitchener170d8972020-11-24 12:53:53 -05004313_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4314_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4315_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4316_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4317_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004318
Bruce Mitchener170d8972020-11-24 12:53:53 -05004319_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = nullptr);
4320_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = nullptr);
4321_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004322
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004323_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
4324_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
4325_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
4326_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
4327_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
4328_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
4329_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
4330_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
4331_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Louis Dionne89258142021-08-23 15:32:36 -04004332#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004333
Howard Hinnantc51e1022010-05-11 19:42:16 +00004334template<class _CharT, class _Traits, class _Allocator>
Martin Storsjö88890c22021-02-22 01:13:13 +02004335_LIBCPP_TEMPLATE_DATA_VIS
Eric Fiselierc9fdaf12020-01-15 17:02:17 -05004336const typename basic_string<_CharT, _Traits, _Allocator>::size_type
4337 basic_string<_CharT, _Traits, _Allocator>::npos;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004338
Marshall Clow851b9ec2019-05-20 21:56:51 +00004339template <class _CharT, class _Allocator>
4340struct _LIBCPP_TEMPLATE_VIS
4341 hash<basic_string<_CharT, char_traits<_CharT>, _Allocator> >
4342 : public unary_function<
4343 basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004344{
4345 size_t
Marshall Clow851b9ec2019-05-20 21:56:51 +00004346 operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT
4347 { return __do_string_hash(__val.data(), __val.data() + __val.size()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004348};
4349
Howard Hinnantc51e1022010-05-11 19:42:16 +00004350
Howard Hinnantdc095972011-07-18 15:51:59 +00004351template<class _CharT, class _Traits, class _Allocator>
4352basic_ostream<_CharT, _Traits>&
4353operator<<(basic_ostream<_CharT, _Traits>& __os,
4354 const basic_string<_CharT, _Traits, _Allocator>& __str);
4355
4356template<class _CharT, class _Traits, class _Allocator>
4357basic_istream<_CharT, _Traits>&
4358operator>>(basic_istream<_CharT, _Traits>& __is,
4359 basic_string<_CharT, _Traits, _Allocator>& __str);
4360
4361template<class _CharT, class _Traits, class _Allocator>
4362basic_istream<_CharT, _Traits>&
4363getline(basic_istream<_CharT, _Traits>& __is,
4364 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4365
4366template<class _CharT, class _Traits, class _Allocator>
4367inline _LIBCPP_INLINE_VISIBILITY
4368basic_istream<_CharT, _Traits>&
4369getline(basic_istream<_CharT, _Traits>& __is,
4370 basic_string<_CharT, _Traits, _Allocator>& __str);
4371
Howard Hinnantdc095972011-07-18 15:51:59 +00004372template<class _CharT, class _Traits, class _Allocator>
4373inline _LIBCPP_INLINE_VISIBILITY
4374basic_istream<_CharT, _Traits>&
4375getline(basic_istream<_CharT, _Traits>&& __is,
4376 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4377
4378template<class _CharT, class _Traits, class _Allocator>
4379inline _LIBCPP_INLINE_VISIBILITY
4380basic_istream<_CharT, _Traits>&
4381getline(basic_istream<_CharT, _Traits>&& __is,
4382 basic_string<_CharT, _Traits, _Allocator>& __str);
4383
Marshall Clow29b53f22018-12-14 18:49:35 +00004384#if _LIBCPP_STD_VER > 17
Marek Kurdeja98b1412020-05-02 13:58:03 +02004385template <class _CharT, class _Traits, class _Allocator, class _Up>
Marshall Clow29b53f22018-12-14 18:49:35 +00004386inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004387 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4388 erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
4389 auto __old_size = __str.size();
4390 __str.erase(_VSTD::remove(__str.begin(), __str.end(), __v), __str.end());
4391 return __old_size - __str.size();
4392}
Marshall Clow29b53f22018-12-14 18:49:35 +00004393
Marek Kurdeja98b1412020-05-02 13:58:03 +02004394template <class _CharT, class _Traits, class _Allocator, class _Predicate>
Marshall Clow29b53f22018-12-14 18:49:35 +00004395inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004396 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4397 erase_if(basic_string<_CharT, _Traits, _Allocator>& __str,
4398 _Predicate __pred) {
4399 auto __old_size = __str.size();
4400 __str.erase(_VSTD::remove_if(__str.begin(), __str.end(), __pred),
4401 __str.end());
4402 return __old_size - __str.size();
4403}
Marshall Clow29b53f22018-12-14 18:49:35 +00004404#endif
4405
Louis Dionneba400782020-10-02 15:02:52 -04004406#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004407
4408template<class _CharT, class _Traits, class _Allocator>
4409bool
4410basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4411{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004412 return data() <= _VSTD::__to_address(__i->base()) &&
4413 _VSTD::__to_address(__i->base()) < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004414}
4415
4416template<class _CharT, class _Traits, class _Allocator>
4417bool
4418basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4419{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004420 return data() < _VSTD::__to_address(__i->base()) &&
4421 _VSTD::__to_address(__i->base()) <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004422}
4423
4424template<class _CharT, class _Traits, class _Allocator>
4425bool
4426basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4427{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004428 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004429 return data() <= __p && __p <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004430}
4431
4432template<class _CharT, class _Traits, class _Allocator>
4433bool
4434basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4435{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004436 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004437 return data() <= __p && __p < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004438}
4439
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004440#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004441
Louis Dionne173f29e2019-05-29 16:01:36 +00004442#if _LIBCPP_STD_VER > 11
Marshall Clowcba751f2013-07-23 17:05:24 +00004443// Literal suffixes for basic_string [basic.string.literals]
Marshall Clowac868372013-10-05 21:18:32 +00004444inline namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004445{
4446 inline namespace string_literals
4447 {
Howard Hinnant5c167562013-08-07 19:39:48 +00004448 inline _LIBCPP_INLINE_VISIBILITY
4449 basic_string<char> operator "" s( const char *__str, size_t __len )
4450 {
4451 return basic_string<char> (__str, __len);
4452 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004453
Louis Dionne89258142021-08-23 15:32:36 -04004454#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnant5c167562013-08-07 19:39:48 +00004455 inline _LIBCPP_INLINE_VISIBILITY
4456 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4457 {
4458 return basic_string<wchar_t> (__str, __len);
4459 }
Louis Dionne89258142021-08-23 15:32:36 -04004460#endif
Marshall Clowcba751f2013-07-23 17:05:24 +00004461
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -04004462#ifndef _LIBCPP_HAS_NO_CHAR8_T
Marshall Clow8732fed2018-12-11 04:35:44 +00004463 inline _LIBCPP_INLINE_VISIBILITY
4464 basic_string<char8_t> operator "" s(const char8_t *__str, size_t __len) _NOEXCEPT
4465 {
4466 return basic_string<char8_t> (__str, __len);
4467 }
4468#endif
4469
Howard Hinnant5c167562013-08-07 19:39:48 +00004470 inline _LIBCPP_INLINE_VISIBILITY
4471 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4472 {
4473 return basic_string<char16_t> (__str, __len);
4474 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004475
Howard Hinnant5c167562013-08-07 19:39:48 +00004476 inline _LIBCPP_INLINE_VISIBILITY
4477 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4478 {
4479 return basic_string<char32_t> (__str, __len);
4480 }
Nikolas Klauserd26407a2021-12-02 14:12:51 +01004481 } // namespace string_literals
4482} // namespace literals
Mark de Weverdf3e0d42021-09-26 15:47:42 +02004483
4484#if _LIBCPP_STD_VER > 17
4485template <>
4486inline constexpr bool __format::__enable_insertable<std::basic_string<char>> = true;
4487#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
4488template <>
4489inline constexpr bool __format::__enable_insertable<std::basic_string<wchar_t>> = true;
4490#endif
4491#endif
4492
Marshall Clowcba751f2013-07-23 17:05:24 +00004493#endif
4494
Howard Hinnantc51e1022010-05-11 19:42:16 +00004495_LIBCPP_END_NAMESPACE_STD
4496
Eric Fiselierf4433a32017-05-31 22:07:49 +00004497_LIBCPP_POP_MACROS
4498
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004499#endif // _LIBCPP_STRING