blob: bd1c4303e8a4074c5645ded674d15bbe59ce4572 [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>
Nikolas Klauser62060be2022-04-20 10:52:04 +0200535#include <__utility/unreachable.h>
536#include <climits>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400537#include <compare>
538#include <cstdio> // EOF
Louis Dionned24191c2021-08-19 12:39:16 -0400539#include <cstdlib>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400540#include <cstring>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400541#include <initializer_list>
542#include <iosfwd>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000543#include <iterator>
Nikolas Klauser62060be2022-04-20 10:52:04 +0200544#include <limits>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000545#include <memory>
546#include <stdexcept>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400547#include <string_view>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000548#include <type_traits>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000549#include <version>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000550
Nikolas Klauser4c1bf592022-02-11 19:15:18 +0100551// TODO: remove these headers
552#include <__functional/binary_function.h>
553#include <__functional/invoke.h>
554#include <__functional/operations.h>
555#include <__functional/reference_wrapper.h>
556#include <__functional/unary_function.h>
557#include <__functional/weak_result_type.h>
558#include <new>
559#include <typeinfo>
560
Louis Dionne89258142021-08-23 15:32:36 -0400561#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Nikolas Klauser80840272022-02-04 13:04:33 +0100562# include <cwchar>
Louis Dionne89258142021-08-23 15:32:36 -0400563#endif
564
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400565#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
Nikolas Klauser80840272022-02-04 13:04:33 +0100566# include <cstdint>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400567#endif
Eric Fiselier14b6de92014-08-10 23:53:08 +0000568
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000569#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Arthur O'Dwyer6eeaa002022-02-01 20:16:40 -0500570# pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000571#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000572
Eric Fiselierf4433a32017-05-31 22:07:49 +0000573_LIBCPP_PUSH_MACROS
574#include <__undef_macros>
575
576
Howard Hinnantc51e1022010-05-11 19:42:16 +0000577_LIBCPP_BEGIN_NAMESPACE_STD
578
Howard Hinnantc51e1022010-05-11 19:42:16 +0000579// basic_string
580
581template<class _CharT, class _Traits, class _Allocator>
582basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000583operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
584 const basic_string<_CharT, _Traits, _Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000585
586template<class _CharT, class _Traits, class _Allocator>
587basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000588operator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000589
590template<class _CharT, class _Traits, class _Allocator>
591basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000592operator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000593
594template<class _CharT, class _Traits, class _Allocator>
Louis Dionne8f8d95d2018-11-21 17:31:55 +0000595inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000596basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000597operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000598
599template<class _CharT, class _Traits, class _Allocator>
600basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000601operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000602
Shoaib Meenaiea363712017-07-29 02:54:41 +0000603_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&))
604
Marshall Clow039b2f02016-01-13 21:54:34 +0000605template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400606struct __string_is_trivial_iterator : public false_type {};
607
608template <class _Tp>
609struct __string_is_trivial_iterator<_Tp*>
610 : public is_arithmetic<_Tp> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000611
Louis Dionne173f29e2019-05-29 16:01:36 +0000612template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400613struct __string_is_trivial_iterator<__wrap_iter<_Iter> >
614 : public __string_is_trivial_iterator<_Iter> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000615
Marshall Clow82513342016-09-24 22:45:42 +0000616template <class _CharT, class _Traits, class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500617struct __can_be_converted_to_string_view : public _BoolConstant<
618 is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
619 !is_convertible<const _Tp&, const _CharT*>::value
620 > {};
Marshall Clow82513342016-09-24 22:45:42 +0000621
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400622#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800623typedef basic_string<char8_t> u8string;
624#endif
625
626#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
627typedef basic_string<char16_t> u16string;
628typedef basic_string<char32_t> u32string;
Louis Dionne96fc5f52021-09-09 11:25:10 -0400629#endif
Richard Smith256954d2020-11-11 17:12:18 -0800630
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200631struct __uninitialized_size_tag {};
632
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000633template<class _CharT, class _Traits, class _Allocator>
Richard Smith256954d2020-11-11 17:12:18 -0800634class
635 _LIBCPP_TEMPLATE_VIS
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400636#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800637 _LIBCPP_PREFERRED_NAME(u8string)
638#endif
639#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
640 _LIBCPP_PREFERRED_NAME(u16string)
641 _LIBCPP_PREFERRED_NAME(u32string)
642#endif
643 basic_string
Howard Hinnantc51e1022010-05-11 19:42:16 +0000644{
645public:
646 typedef basic_string __self;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000647 typedef basic_string_view<_CharT, _Traits> __self_view;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000648 typedef _Traits traits_type;
Marshall Clowa3a74e02017-03-15 18:41:11 +0000649 typedef _CharT value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000650 typedef _Allocator allocator_type;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000651 typedef allocator_traits<allocator_type> __alloc_traits;
652 typedef typename __alloc_traits::size_type size_type;
653 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant3e276872011-06-03 18:40:47 +0000654 typedef value_type& reference;
655 typedef const value_type& const_reference;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000656 typedef typename __alloc_traits::pointer pointer;
657 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000658
Marshall Clow79f33542018-03-21 00:36:05 +0000659 static_assert((!is_array<value_type>::value), "Character type of basic_string must not be an array");
660 static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string must be standard-layout");
661 static_assert(( is_trivial<value_type>::value), "Character type of basic_string must be trivial");
662 static_assert(( is_same<_CharT, typename traits_type::char_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000663 "traits_type::char_type must be the same type as CharT");
Marshall Clow79f33542018-03-21 00:36:05 +0000664 static_assert(( is_same<typename allocator_type::value_type, value_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000665 "Allocator::value_type must be same type as value_type");
Marshall Clowe46031a2018-07-02 18:41:15 +0000666
Howard Hinnantc51e1022010-05-11 19:42:16 +0000667 typedef __wrap_iter<pointer> iterator;
668 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000669 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
670 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000671
672private:
Howard Hinnant68bf1812013-04-30 21:44:48 +0000673
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000674#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000675
676 struct __long
677 {
678 pointer __data_;
679 size_type __size_;
Nikolas Klauser62060be2022-04-20 10:52:04 +0200680 size_type __cap_ : sizeof(size_type) * CHAR_BIT - 1;
681 size_type __is_long_ : 1;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000682 };
683
Howard Hinnant68bf1812013-04-30 21:44:48 +0000684 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
685 (sizeof(__long) - 1)/sizeof(value_type) : 2};
686
687 struct __short
688 {
689 value_type __data_[__min_cap];
Azat Khuzhin1995f722022-04-08 16:13:46 -0400690 unsigned char __padding[sizeof(value_type) - 1];
Nikolas Klauser62060be2022-04-20 10:52:04 +0200691 unsigned char __size_ : 7;
692 unsigned char __is_long_ : 1;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000693 };
694
Nikolas Klauser62060be2022-04-20 10:52:04 +0200695// The __endian_factor is required because the field we use to store the size
696// (either size_type or unsigned char depending on long/short) has one fewer
697// bit than it would if it were not a bitfield.
698//
699// If the LSB is used to store the short-flag in the short string representation,
700// we have to multiply the size by two when it is stored and divide it by two when
701// it is loaded to make sure that we always store an even number. In the long string
702// representation, we can ignore this because we can assume that we always allocate
703// an even amount of value_types.
704//
705// If the MSB is used for the short-flag, the max_size() is numeric_limits<size_type>::max() / 2.
706// This does not impact the short string representation, since we never need the MSB
707// for representing the size of a short string anyway.
708
709#ifdef _LIBCPP_BIG_ENDIAN
710 static const size_type __endian_factor = 2;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000711#else
Nikolas Klauser62060be2022-04-20 10:52:04 +0200712 static const size_type __endian_factor = 1;
713#endif
714
715#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
716
717#ifdef _LIBCPP_BIG_ENDIAN
718 static const size_type __endian_factor = 1;
719#else
720 static const size_type __endian_factor = 2;
721#endif
Howard Hinnant68bf1812013-04-30 21:44:48 +0000722
Howard Hinnantc51e1022010-05-11 19:42:16 +0000723 struct __long
724 {
Nikolas Klauser62060be2022-04-20 10:52:04 +0200725 size_type __is_long_ : 1;
726 size_type __cap_ : sizeof(size_type) * CHAR_BIT - 1;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000727 size_type __size_;
728 pointer __data_;
729 };
730
Howard Hinnantc51e1022010-05-11 19:42:16 +0000731 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
732 (sizeof(__long) - 1)/sizeof(value_type) : 2};
733
734 struct __short
735 {
736 union
737 {
Nikolas Klauser62060be2022-04-20 10:52:04 +0200738 struct {
739 unsigned char __is_long_ : 1;
740 unsigned char __size_ : 7;
741 };
Howard Hinnant49e145e2012-10-30 19:06:59 +0000742 value_type __lx;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000743 };
744 value_type __data_[__min_cap];
745 };
746
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400747#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000748
Howard Hinnant8ea98242013-08-23 17:37:05 +0000749 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000750
Howard Hinnant8ea98242013-08-23 17:37:05 +0000751 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000752
753 struct __raw
754 {
755 size_type __words[__n_words];
756 };
757
758 struct __rep
759 {
760 union
761 {
762 __long __l;
763 __short __s;
764 __raw __r;
765 };
766 };
767
768 __compressed_pair<__rep, allocator_type> __r_;
769
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200770 // Construct a string with the given allocator and enough storage to hold `__size` characters, but
771 // don't initialize the characters. The contents of the string, including the null terminator, must be
772 // initialized separately.
773 _LIBCPP_HIDE_FROM_ABI explicit basic_string(__uninitialized_size_tag, size_type __size, const allocator_type& __a)
774 : __r_(__default_init_tag(), __a) {
775 if (__size > max_size())
776 __throw_length_error();
777 if (__fits_in_sso(__size)) {
778 __zero();
779 __set_short_size(__size);
780 } else {
781 auto __capacity = __recommend(__size) + 1;
782 auto __allocation = __alloc_traits::allocate(__alloc(), __capacity);
783 __set_long_cap(__capacity);
784 __set_long_pointer(__allocation);
785 __set_long_size(__size);
786 }
787 std::__debug_db_insert_c(this);
788 }
789
Howard Hinnantc51e1022010-05-11 19:42:16 +0000790public:
Martin Storsjö88890c22021-02-22 01:13:13 +0200791 _LIBCPP_TEMPLATE_DATA_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000792 static const size_type npos = -1;
793
Howard Hinnant3e276872011-06-03 18:40:47 +0000794 _LIBCPP_INLINE_VISIBILITY basic_string()
795 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000796
797 _LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a)
798#if _LIBCPP_STD_VER <= 14
799 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
800#else
801 _NOEXCEPT;
802#endif
803
Howard Hinnantc51e1022010-05-11 19:42:16 +0000804 basic_string(const basic_string& __str);
805 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clowa80abc72015-06-03 19:56:43 +0000806
Eric Fiselierfc92be82017-04-19 00:28:44 +0000807#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000808 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000809 basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +0000810#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +0000811 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000812#else
813 _NOEXCEPT;
814#endif
815
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000816 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000817 basic_string(basic_string&& __str, const allocator_type& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400818#endif // _LIBCPP_CXX03_LANG
Marshall Clowe46031a2018-07-02 18:41:15 +0000819
Louis Dionne9ce598d2021-09-08 09:14:43 -0400820 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Eric Fiseliera8567862018-07-17 05:48:48 +0000821 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5169d1c2019-12-16 19:03:23 -0500822 basic_string(const _CharT* __s) : __r_(__default_init_tag(), __default_init_tag()) {
Eric Fiseliera8567862018-07-17 05:48:48 +0000823 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
824 __init(__s, traits_type::length(__s));
Nikolas Klauserf2807732022-01-11 00:33:35 +0100825 _VSTD::__debug_db_insert_c(this);
Eric Fiseliera8567862018-07-17 05:48:48 +0000826 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000827
Louis Dionne9ce598d2021-09-08 09:14:43 -0400828 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000829 _LIBCPP_INLINE_VISIBILITY
830 basic_string(const _CharT* __s, const _Allocator& __a);
831
Marek Kurdej90d79712021-07-27 16:16:21 +0200832#if _LIBCPP_STD_VER > 20
833 basic_string(nullptr_t) = delete;
834#endif
835
Howard Hinnantcf823322010-12-17 14:46:43 +0000836 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000837 basic_string(const _CharT* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +0000838 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000839 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000840 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000841 basic_string(size_type __n, _CharT __c);
Marshall Clowe46031a2018-07-02 18:41:15 +0000842
Louis Dionne9ce598d2021-09-08 09:14:43 -0400843 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000844 _LIBCPP_INLINE_VISIBILITY
845 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
846
Marshall Clow83445802016-04-07 18:13:41 +0000847 basic_string(const basic_string& __str, size_type __pos, size_type __n,
Eric Fiselier812882b2017-02-17 01:17:10 +0000848 const _Allocator& __a = _Allocator());
Marshall Clow83445802016-04-07 18:13:41 +0000849 _LIBCPP_INLINE_VISIBILITY
850 basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +0000851 const _Allocator& __a = _Allocator());
Marshall Clowe46031a2018-07-02 18:41:15 +0000852
Louis Dionne9ce598d2021-09-08 09:14:43 -0400853 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 +0000854 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselier812882b2017-02-17 01:17:10 +0000855 basic_string(const _Tp& __t, size_type __pos, size_type __n,
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500856 const allocator_type& __a = allocator_type());
Marshall Clowe46031a2018-07-02 18:41:15 +0000857
Louis Dionne9ce598d2021-09-08 09:14:43 -0400858 template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500859 !__is_same_uncvref<_Tp, basic_string>::value> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000860 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
861 explicit basic_string(const _Tp& __t);
862
Louis Dionne9ce598d2021-09-08 09:14:43 -0400863 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 +0000864 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
865 explicit basic_string(const _Tp& __t, const allocator_type& __a);
866
Louis Dionne9ce598d2021-09-08 09:14:43 -0400867 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000868 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000869 basic_string(_InputIterator __first, _InputIterator __last);
Louis Dionne9ce598d2021-09-08 09:14:43 -0400870 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000871 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000872 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000873#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000874 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000875 basic_string(initializer_list<_CharT> __il);
Howard Hinnantcf823322010-12-17 14:46:43 +0000876 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000877 basic_string(initializer_list<_CharT> __il, const _Allocator& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400878#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000879
Eric Fiselierd9a702a2016-10-31 03:42:50 +0000880 inline ~basic_string();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000881
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000882 _LIBCPP_INLINE_VISIBILITY
883 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
884
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000885 basic_string& operator=(const basic_string& __str);
Eric Fiselier5ec13b12017-01-23 21:24:58 +0000886
Louis Dionne9ce598d2021-09-08 09:14:43 -0400887 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 +0000888 basic_string& operator=(const _Tp& __t)
889 {__self_view __sv = __t; return assign(__sv);}
890
Eric Fiselierfc92be82017-04-19 00:28:44 +0000891#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000892 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000893 basic_string& operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000894 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiselierfc92be82017-04-19 00:28:44 +0000895 _LIBCPP_INLINE_VISIBILITY
896 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000897#endif
Howard Hinnantd17880b2013-06-28 16:59:19 +0000898 _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const value_type* __s) {return assign(__s);}
Marek Kurdej90d79712021-07-27 16:16:21 +0200899#if _LIBCPP_STD_VER > 20
900 basic_string& operator=(nullptr_t) = delete;
901#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000902 basic_string& operator=(value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000903
Louis Dionneba400782020-10-02 15:02:52 -0400904#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +0000905 _LIBCPP_INLINE_VISIBILITY
906 iterator begin() _NOEXCEPT
907 {return iterator(this, __get_pointer());}
908 _LIBCPP_INLINE_VISIBILITY
909 const_iterator begin() const _NOEXCEPT
910 {return const_iterator(this, __get_pointer());}
911 _LIBCPP_INLINE_VISIBILITY
912 iterator end() _NOEXCEPT
913 {return iterator(this, __get_pointer() + size());}
914 _LIBCPP_INLINE_VISIBILITY
915 const_iterator end() const _NOEXCEPT
916 {return const_iterator(this, __get_pointer() + size());}
917#else
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000918 _LIBCPP_INLINE_VISIBILITY
919 iterator begin() _NOEXCEPT
920 {return iterator(__get_pointer());}
921 _LIBCPP_INLINE_VISIBILITY
922 const_iterator begin() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000923 {return const_iterator(__get_pointer());}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000924 _LIBCPP_INLINE_VISIBILITY
925 iterator end() _NOEXCEPT
926 {return iterator(__get_pointer() + size());}
927 _LIBCPP_INLINE_VISIBILITY
928 const_iterator end() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000929 {return const_iterator(__get_pointer() + size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400930#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000931 _LIBCPP_INLINE_VISIBILITY
932 reverse_iterator rbegin() _NOEXCEPT
933 {return reverse_iterator(end());}
934 _LIBCPP_INLINE_VISIBILITY
935 const_reverse_iterator rbegin() const _NOEXCEPT
936 {return const_reverse_iterator(end());}
937 _LIBCPP_INLINE_VISIBILITY
938 reverse_iterator rend() _NOEXCEPT
939 {return reverse_iterator(begin());}
940 _LIBCPP_INLINE_VISIBILITY
941 const_reverse_iterator rend() const _NOEXCEPT
942 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000943
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000944 _LIBCPP_INLINE_VISIBILITY
945 const_iterator cbegin() const _NOEXCEPT
946 {return begin();}
947 _LIBCPP_INLINE_VISIBILITY
948 const_iterator cend() const _NOEXCEPT
949 {return end();}
950 _LIBCPP_INLINE_VISIBILITY
951 const_reverse_iterator crbegin() const _NOEXCEPT
952 {return rbegin();}
953 _LIBCPP_INLINE_VISIBILITY
954 const_reverse_iterator crend() const _NOEXCEPT
955 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000956
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000957 _LIBCPP_INLINE_VISIBILITY size_type size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000958 {return __is_long() ? __get_long_size() : __get_short_size();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000959 _LIBCPP_INLINE_VISIBILITY size_type length() const _NOEXCEPT {return size();}
960 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT;
961 _LIBCPP_INLINE_VISIBILITY size_type capacity() const _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +0000962 {return (__is_long() ? __get_long_cap()
963 : static_cast<size_type>(__min_cap)) - 1;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000964
965 void resize(size_type __n, value_type __c);
966 _LIBCPP_INLINE_VISIBILITY void resize(size_type __n) {resize(__n, value_type());}
967
Marek Kurdejc9848142020-11-26 10:07:16 +0100968 void reserve(size_type __requested_capacity);
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100969
970#if _LIBCPP_STD_VER > 20
971 template <class _Op>
972 _LIBCPP_HIDE_FROM_ABI constexpr
973 void resize_and_overwrite(size_type __n, _Op __op) {
974 __resize_default_init(__n);
Nikolas Klausera4a76a12022-01-20 13:53:59 +0100975 __erase_to_end(_VSTD::move(__op)(data(), _LIBCPP_AUTO_CAST(__n)));
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100976 }
977#endif
978
Eric Fiselier451d5582018-11-26 20:15:38 +0000979 _LIBCPP_INLINE_VISIBILITY void __resize_default_init(size_type __n);
980
Marek Kurdejc9848142020-11-26 10:07:16 +0100981 _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_INLINE_VISIBILITY
982 void reserve() _NOEXCEPT {shrink_to_fit();}
Marshall Clow6ae12a82018-11-28 18:18:34 +0000983 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejc9848142020-11-26 10:07:16 +0100984 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000985 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000986 void clear() _NOEXCEPT;
Marshall Clowb7db4972017-11-15 20:02:27 +0000987 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
988 bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000989
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000990 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __pos) const _NOEXCEPT;
991 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000992
993 const_reference at(size_type __n) const;
994 reference at(size_type __n);
995
996 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const basic_string& __str) {return append(__str);}
Marshall Clowe46031a2018-07-02 18:41:15 +0000997
998 template <class _Tp>
999 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001000 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001001 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001002 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1003 && !__is_same_uncvref<_Tp, basic_string >::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001004 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001005 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001006 operator+=(const _Tp& __t) {__self_view __sv = __t; return append(__sv);}
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001007 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const value_type* __s) {return append(__s);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001008 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(value_type __c) {push_back(__c); return *this;}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001009#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001010 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(initializer_list<value_type> __il) {return append(__il);}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001011#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001012
Howard Hinnantcf823322010-12-17 14:46:43 +00001013 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001014 basic_string& append(const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001015
1016 template <class _Tp>
1017 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001018 __enable_if_t<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001019 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1020 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001021 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001022 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001023 append(const _Tp& __t) { __self_view __sv = __t; return append(__sv.data(), __sv.size()); }
Marshall Clow8db7fb02014-03-04 19:17:19 +00001024 basic_string& append(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clowe46031a2018-07-02 18:41:15 +00001025
Marshall Clow62953962016-10-03 23:40:48 +00001026 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001027 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001028 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001029 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001030 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1031 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001032 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001033 >
Marshall Clow82513342016-09-24 22:45:42 +00001034 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001035 basic_string& append(const value_type* __s, size_type __n);
1036 basic_string& append(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001037 basic_string& append(size_type __n, value_type __c);
Eric Fiselier451d5582018-11-26 20:15:38 +00001038
1039 _LIBCPP_INLINE_VISIBILITY
1040 void __append_default_init(size_type __n);
1041
Howard Hinnantc51e1022010-05-11 19:42:16 +00001042 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001043 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001044 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001045 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001046 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001047 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001048 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001049 _LIBCPP_INLINE_VISIBILITY
1050 append(_InputIterator __first, _InputIterator __last) {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001051 const basic_string __temp(__first, __last, __alloc());
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001052 append(__temp.data(), __temp.size());
1053 return *this;
1054 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001055 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001056 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001057 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001058 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001059 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001060 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001061 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001062 _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001063 append(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001064
Eric Fiselierfc92be82017-04-19 00:28:44 +00001065#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001066 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001067 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001068#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001069
1070 void push_back(value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001071 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001072 void pop_back();
Marshall Clow05cf6692019-03-19 03:30:07 +00001073 _LIBCPP_INLINE_VISIBILITY reference front() _NOEXCEPT;
1074 _LIBCPP_INLINE_VISIBILITY const_reference front() const _NOEXCEPT;
1075 _LIBCPP_INLINE_VISIBILITY reference back() _NOEXCEPT;
1076 _LIBCPP_INLINE_VISIBILITY const_reference back() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001077
Marshall Clowe46031a2018-07-02 18:41:15 +00001078 template <class _Tp>
1079 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001080 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001081 <
1082 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1083 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001084 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001085 assign(const _Tp & __t) { __self_view __sv = __t; return assign(__sv.data(), __sv.size()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001086 _LIBCPP_INLINE_VISIBILITY
Marshall Clow95d5e9a2016-03-09 18:08:29 +00001087 basic_string& assign(const basic_string& __str) { return *this = __str; }
Eric Fiselierfc92be82017-04-19 00:28:44 +00001088#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001089 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001090 basic_string& assign(basic_string&& __str)
Marshall Clow5aa9e942015-10-05 16:17:34 +00001091 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001092 {*this = _VSTD::move(__str); return *this;}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001093#endif
Marshall Clow8db7fb02014-03-04 19:17:19 +00001094 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow62953962016-10-03 23:40:48 +00001095 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001096 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001097 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001098 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001099 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1100 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001101 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001102 >
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001103 assign(const _Tp & __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001104 basic_string& assign(const value_type* __s, size_type __n);
1105 basic_string& assign(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001106 basic_string& assign(size_type __n, value_type __c);
1107 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001108 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001109 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001110 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001111 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001112 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001113 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001114 assign(_InputIterator __first, _InputIterator __last);
1115 template<class _ForwardIterator>
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_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001120 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001121 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001122 assign(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001123#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001124 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001125 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001126#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001127
Howard Hinnantcf823322010-12-17 14:46:43 +00001128 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001129 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001130
1131 template <class _Tp>
1132 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001133 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001134 <
1135 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1136 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001137 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001138 insert(size_type __pos1, const _Tp& __t)
1139 { __self_view __sv = __t; return insert(__pos1, __sv.data(), __sv.size()); }
1140
Marshall Clow82513342016-09-24 22:45:42 +00001141 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001142 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001143 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001144 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001145 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001146 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001147 >
Marshall Clow82513342016-09-24 22:45:42 +00001148 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Marshall Clow8db7fb02014-03-04 19:17:19 +00001149 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001150 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1151 basic_string& insert(size_type __pos, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001152 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1153 iterator insert(const_iterator __pos, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001154 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001155 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1156 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001157 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001158 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001159 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001160 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001161 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001162 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001163 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1164 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001165 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001166 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001167 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001168 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001169 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001170 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001171 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001172#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001173 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001174 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1175 {return insert(__pos, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001176#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001177
1178 basic_string& erase(size_type __pos = 0, size_type __n = npos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001179 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001180 iterator erase(const_iterator __pos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001181 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001182 iterator erase(const_iterator __first, const_iterator __last);
1183
Howard Hinnantcf823322010-12-17 14:46:43 +00001184 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001185 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001186
1187 template <class _Tp>
1188 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001189 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001190 <
1191 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1192 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001193 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001194 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 +00001195 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 +00001196 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001197 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001198 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001199 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001200 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001201 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001202 >
Marshall Clow82513342016-09-24 22:45:42 +00001203 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001204 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
1205 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001206 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001207 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001208 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001209
1210 template <class _Tp>
1211 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001212 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001213 <
1214 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1215 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001216 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001217 replace(const_iterator __i1, const_iterator __i2, const _Tp& __t) { __self_view __sv = __t; return replace(__i1 - begin(), __i2 - __i1, __sv); }
1218
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001219 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001220 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +00001221 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001222 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Howard Hinnantcf823322010-12-17 14:46:43 +00001223 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001224 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001225 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001226 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001227 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001228 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001229 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001230 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001231 >
Howard Hinnant990d6e82010-11-17 21:11:40 +00001232 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001233#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001234 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001235 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001236 {return replace(__i1, __i2, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001237#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001238
Howard Hinnantd17880b2013-06-28 16:59:19 +00001239 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
Howard Hinnantcf823322010-12-17 14:46:43 +00001240 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001241 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1242
Howard Hinnantcf823322010-12-17 14:46:43 +00001243 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001244 void swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001245#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00001246 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +00001247#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001248 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001249 __is_nothrow_swappable<allocator_type>::value);
1250#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001251
Howard Hinnantcf823322010-12-17 14:46:43 +00001252 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001253 const value_type* c_str() const _NOEXCEPT {return data();}
Howard Hinnantcf823322010-12-17 14:46:43 +00001254 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001255 const value_type* data() const _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Eric Fiselier4ee2a892017-11-20 20:23:27 +00001256#if _LIBCPP_STD_VER > 14 || defined(_LIBCPP_BUILDING_LIBRARY)
Marshall Clowd3c22392016-03-08 15:44:30 +00001257 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001258 value_type* data() _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Marshall Clowd3c22392016-03-08 15:44:30 +00001259#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001260
Howard Hinnantcf823322010-12-17 14:46:43 +00001261 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001262 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001263
Howard Hinnantcf823322010-12-17 14:46:43 +00001264 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001265 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001266
1267 template <class _Tp>
1268 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001269 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001270 <
1271 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1272 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001273 >
zoecarver1997e0a2021-02-05 11:54:47 -08001274 find(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001275 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001276 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001277 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001278 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001279
Howard Hinnantcf823322010-12-17 14:46:43 +00001280 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001281 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001282
1283 template <class _Tp>
1284 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001285 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001286 <
1287 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1288 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001289 >
zoecarver1997e0a2021-02-05 11:54:47 -08001290 rfind(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001291 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001292 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001293 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001294 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001295
Howard Hinnantcf823322010-12-17 14:46:43 +00001296 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001297 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001298
1299 template <class _Tp>
1300 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001301 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001302 <
1303 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1304 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001305 >
zoecarver1997e0a2021-02-05 11:54:47 -08001306 find_first_of(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001307 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001308 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001309 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001310 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001311 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001312
Howard Hinnantcf823322010-12-17 14:46:43 +00001313 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001314 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001315
1316 template <class _Tp>
1317 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001318 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001319 <
1320 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1321 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001322 >
zoecarver1997e0a2021-02-05 11:54:47 -08001323 find_last_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001324 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001325 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001326 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001327 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001328 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001329
Howard Hinnantcf823322010-12-17 14:46:43 +00001330 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001331 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001332
1333 template <class _Tp>
1334 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001335 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001336 <
1337 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1338 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001339 >
zoecarver1997e0a2021-02-05 11:54:47 -08001340 find_first_not_of(const _Tp &__t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001341 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 +00001342 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001343 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001344 _LIBCPP_INLINE_VISIBILITY
1345 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1346
1347 _LIBCPP_INLINE_VISIBILITY
1348 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001349
1350 template <class _Tp>
1351 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001352 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001353 <
1354 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1355 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001356 >
zoecarver1997e0a2021-02-05 11:54:47 -08001357 find_last_not_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001358 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 +00001359 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001360 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001361 _LIBCPP_INLINE_VISIBILITY
1362 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1363
1364 _LIBCPP_INLINE_VISIBILITY
1365 int compare(const basic_string& __str) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001366
1367 template <class _Tp>
1368 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001369 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001370 <
1371 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1372 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001373 >
zoecarver1997e0a2021-02-05 11:54:47 -08001374 compare(const _Tp &__t) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001375
1376 template <class _Tp>
1377 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001378 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001379 <
1380 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1381 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001382 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001383 compare(size_type __pos1, size_type __n1, const _Tp& __t) const;
1384
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001385 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001386 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Marshall Clow8db7fb02014-03-04 19:17:19 +00001387 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 +00001388
Marshall Clow82513342016-09-24 22:45:42 +00001389 template <class _Tp>
Eric Fiselier7d4aa5c2016-10-14 05:29:46 +00001390 inline _LIBCPP_INLINE_VISIBILITY
Louis Dionne9ce598d2021-09-08 09:14:43 -04001391 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001392 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001393 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001394 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001395 >
Marshall Clow82513342016-09-24 22:45:42 +00001396 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 +00001397 int compare(const value_type* __s) const _NOEXCEPT;
1398 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1399 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001400
Marshall Clow18c293b2017-12-04 20:11:38 +00001401#if _LIBCPP_STD_VER > 17
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001402 constexpr _LIBCPP_INLINE_VISIBILITY
1403 bool starts_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001404 { return __self_view(data(), size()).starts_with(__sv); }
1405
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001406 constexpr _LIBCPP_INLINE_VISIBILITY
1407 bool starts_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001408 { return !empty() && _Traits::eq(front(), __c); }
1409
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001410 constexpr _LIBCPP_INLINE_VISIBILITY
1411 bool starts_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001412 { return starts_with(__self_view(__s)); }
1413
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001414 constexpr _LIBCPP_INLINE_VISIBILITY
1415 bool ends_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001416 { return __self_view(data(), size()).ends_with( __sv); }
1417
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001418 constexpr _LIBCPP_INLINE_VISIBILITY
1419 bool ends_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001420 { return !empty() && _Traits::eq(back(), __c); }
1421
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001422 constexpr _LIBCPP_INLINE_VISIBILITY
1423 bool ends_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001424 { return ends_with(__self_view(__s)); }
1425#endif
1426
Wim Leflere023c3542021-01-19 14:33:30 -05001427#if _LIBCPP_STD_VER > 20
1428 constexpr _LIBCPP_INLINE_VISIBILITY
1429 bool contains(__self_view __sv) const noexcept
1430 { return __self_view(data(), size()).contains(__sv); }
1431
1432 constexpr _LIBCPP_INLINE_VISIBILITY
1433 bool contains(value_type __c) const noexcept
1434 { return __self_view(data(), size()).contains(__c); }
1435
1436 constexpr _LIBCPP_INLINE_VISIBILITY
1437 bool contains(const value_type* __s) const
1438 { return __self_view(data(), size()).contains(__s); }
1439#endif
1440
Howard Hinnantcf823322010-12-17 14:46:43 +00001441 _LIBCPP_INLINE_VISIBILITY bool __invariants() const;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001442
Marshall Clowe60a7182018-05-29 17:04:37 +00001443 _LIBCPP_INLINE_VISIBILITY void __clear_and_shrink() _NOEXCEPT;
Marshall Clow851b9ec2019-05-20 21:56:51 +00001444
Marek Kurdejc9848142020-11-26 10:07:16 +01001445 _LIBCPP_INLINE_VISIBILITY void __shrink_or_extend(size_type __target_capacity);
1446
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001447 _LIBCPP_INLINE_VISIBILITY
Nikolas Klauser62060be2022-04-20 10:52:04 +02001448 bool __is_long() const _NOEXCEPT {
1449 return __r_.first().__s.__is_long_;
1450 }
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001451
Louis Dionneba400782020-10-02 15:02:52 -04001452#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001453
1454 bool __dereferenceable(const const_iterator* __i) const;
1455 bool __decrementable(const const_iterator* __i) const;
1456 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1457 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1458
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001459#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001460
Howard Hinnantc51e1022010-05-11 19:42:16 +00001461private:
Nikolas Klauser93826d12022-01-04 17:24:03 +01001462 _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI static bool __fits_in_sso(size_type __sz) {
1463 // SSO is disabled during constant evaluation because `__is_long` isn't constexpr friendly
1464 return !__libcpp_is_constant_evaluated() && (__sz < __min_cap);
1465 }
1466
Nikolas Klauser48d680d2022-02-26 13:28:33 +01001467 template <class _ForwardIterator>
1468 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
1469 iterator __insert_from_safe_copy(size_type __n, size_type __ip, _ForwardIterator __first, _ForwardIterator __last) {
1470 size_type __sz = size();
1471 size_type __cap = capacity();
1472 value_type* __p;
1473 if (__cap - __sz >= __n)
1474 {
1475 __p = std::__to_address(__get_pointer());
1476 size_type __n_move = __sz - __ip;
1477 if (__n_move != 0)
1478 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
1479 }
1480 else
1481 {
1482 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
1483 __p = std::__to_address(__get_long_pointer());
1484 }
1485 __sz += __n;
1486 __set_size(__sz);
1487 traits_type::assign(__p[__sz], value_type());
1488 for (__p += __ip; __first != __last; ++__p, ++__first)
1489 traits_type::assign(*__p, *__first);
1490
1491 return begin() + __ip;
1492 }
1493
1494 _LIBCPP_HIDE_FROM_ABI allocator_type& __alloc() _NOEXCEPT { return __r_.second(); }
1495 _LIBCPP_HIDE_FROM_ABI const allocator_type& __alloc() const _NOEXCEPT { return __r_.second(); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001496
Nikolas Klauser62060be2022-04-20 10:52:04 +02001497 _LIBCPP_INLINE_VISIBILITY
1498 void __set_short_size(size_type __s) _NOEXCEPT {
1499 _LIBCPP_ASSERT(__s < __min_cap, "__s should never be greater than or equal to the short string capacity");
1500 __r_.first().__s.__size_ = __s;
1501 __r_.first().__s.__is_long_ = false;
1502 }
Howard Hinnant68bf1812013-04-30 21:44:48 +00001503
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001504 _LIBCPP_INLINE_VISIBILITY
Nikolas Klauser62060be2022-04-20 10:52:04 +02001505 size_type __get_short_size() const _NOEXCEPT {
1506 _LIBCPP_ASSERT(!__r_.first().__s.__is_long_, "String has to be short when trying to get the short size");
1507 return __r_.first().__s.__size_;
1508 }
Howard Hinnant68bf1812013-04-30 21:44:48 +00001509
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001510 _LIBCPP_INLINE_VISIBILITY
1511 void __set_long_size(size_type __s) _NOEXCEPT
1512 {__r_.first().__l.__size_ = __s;}
1513 _LIBCPP_INLINE_VISIBILITY
1514 size_type __get_long_size() const _NOEXCEPT
1515 {return __r_.first().__l.__size_;}
1516 _LIBCPP_INLINE_VISIBILITY
1517 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001518 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1519
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001520 _LIBCPP_INLINE_VISIBILITY
Nikolas Klauser62060be2022-04-20 10:52:04 +02001521 void __set_long_cap(size_type __s) _NOEXCEPT {
1522 __r_.first().__l.__cap_ = __s / __endian_factor;
1523 __r_.first().__l.__is_long_ = true;
1524 }
1525
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001526 _LIBCPP_INLINE_VISIBILITY
Nikolas Klauser62060be2022-04-20 10:52:04 +02001527 size_type __get_long_cap() const _NOEXCEPT {
1528 return __r_.first().__l.__cap_ * __endian_factor;
1529 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001530
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001531 _LIBCPP_INLINE_VISIBILITY
1532 void __set_long_pointer(pointer __p) _NOEXCEPT
1533 {__r_.first().__l.__data_ = __p;}
1534 _LIBCPP_INLINE_VISIBILITY
1535 pointer __get_long_pointer() _NOEXCEPT
1536 {return __r_.first().__l.__data_;}
1537 _LIBCPP_INLINE_VISIBILITY
1538 const_pointer __get_long_pointer() const _NOEXCEPT
1539 {return __r_.first().__l.__data_;}
1540 _LIBCPP_INLINE_VISIBILITY
1541 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001542 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001543 _LIBCPP_INLINE_VISIBILITY
1544 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001545 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001546 _LIBCPP_INLINE_VISIBILITY
1547 pointer __get_pointer() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001548 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001549 _LIBCPP_INLINE_VISIBILITY
1550 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001551 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1552
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001553 _LIBCPP_INLINE_VISIBILITY
1554 void __zero() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001555 {
1556 size_type (&__a)[__n_words] = __r_.first().__r.__words;
1557 for (unsigned __i = 0; __i < __n_words; ++__i)
1558 __a[__i] = 0;
1559 }
1560
1561 template <size_type __a> static
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001562 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00001563 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +00001564 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001565 enum {__alignment = 16};
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001566 static _LIBCPP_INLINE_VISIBILITY
1567 size_type __recommend(size_type __s) _NOEXCEPT
Marshall Clow80584522018-02-07 21:30:17 +00001568 {
1569 if (__s < __min_cap) return static_cast<size_type>(__min_cap) - 1;
1570 size_type __guess = __align_it<sizeof(value_type) < __alignment ?
1571 __alignment/sizeof(value_type) : 1 > (__s+1) - 1;
1572 if (__guess == __min_cap) ++__guess;
1573 return __guess;
1574 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001575
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001576 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001577 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001578 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001579 void __init(const value_type* __s, size_type __sz);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001580 inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001581 void __init(size_type __n, value_type __c);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001582
Martijn Vels5e7c9752020-03-04 17:52:46 -05001583 // Slow path for the (inlined) copy constructor for 'long' strings.
1584 // Always externally instantiated and not inlined.
1585 // Requires that __s is zero terminated.
1586 // The main reason for this function to exist is because for unstable, we
1587 // want to allow inlining of the copy constructor. However, we don't want
1588 // to call the __init() functions as those are marked as inline which may
1589 // result in over-aggressive inlining by the compiler, where our aim is
1590 // to only inline the fast path code directly in the ctor.
1591 void __init_copy_ctor_external(const value_type* __s, size_type __sz);
1592
Howard Hinnantc51e1022010-05-11 19:42:16 +00001593 template <class _InputIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001594 inline
Louis Dionne9ce598d2021-09-08 09:14:43 -04001595 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001596 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001597 __is_exactly_cpp17_input_iterator<_InputIterator>::value
1598 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001599 __init(_InputIterator __first, _InputIterator __last);
1600
1601 template <class _ForwardIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001602 inline
Louis Dionne9ce598d2021-09-08 09:14:43 -04001603 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001604 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001605 __is_cpp17_forward_iterator<_ForwardIterator>::value
1606 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001607 __init(_ForwardIterator __first, _ForwardIterator __last);
1608
1609 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001610 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001611 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1612 size_type __n_copy, size_type __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00001613 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001614
Martijn Vels596e3de2020-02-26 15:55:49 -05001615 // __assign_no_alias is invoked for assignment operations where we
1616 // have proof that the input does not alias the current instance.
1617 // For example, operator=(basic_string) performs a 'self' check.
1618 template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04001619 basic_string& __assign_no_alias(const value_type* __s, size_type __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05001620
Howard Hinnantcf823322010-12-17 14:46:43 +00001621 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001622 void __erase_to_end(size_type __pos);
1623
Martijn Velsa81fc792020-02-26 13:25:43 -05001624 // __erase_external_with_move is invoked for erase() invocations where
1625 // `n ~= npos`, likely requiring memory moves on the string data.
1626 void __erase_external_with_move(size_type __pos, size_type __n);
1627
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001628 _LIBCPP_INLINE_VISIBILITY
1629 void __copy_assign_alloc(const basic_string& __str)
1630 {__copy_assign_alloc(__str, integral_constant<bool,
1631 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1632
1633 _LIBCPP_INLINE_VISIBILITY
1634 void __copy_assign_alloc(const basic_string& __str, true_type)
1635 {
Marshall Clowf258c202017-01-31 03:40:52 +00001636 if (__alloc() == __str.__alloc())
1637 __alloc() = __str.__alloc();
1638 else
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001639 {
Marshall Clowf258c202017-01-31 03:40:52 +00001640 if (!__str.__is_long())
1641 {
Vedant Kumar55e007e2018-03-08 21:15:26 +00001642 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001643 __alloc() = __str.__alloc();
1644 }
1645 else
1646 {
1647 allocator_type __a = __str.__alloc();
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001648 auto __allocation = std::__allocate_at_least(__a, __str.__get_long_cap());
Vedant Kumar55e007e2018-03-08 21:15:26 +00001649 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001650 __alloc() = _VSTD::move(__a);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001651 __set_long_pointer(__allocation.ptr);
1652 __set_long_cap(__allocation.count);
Marshall Clowf258c202017-01-31 03:40:52 +00001653 __set_long_size(__str.size());
1654 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001655 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001656 }
1657
1658 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001659 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001660 {}
1661
Eric Fiselierfc92be82017-04-19 00:28:44 +00001662#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +00001663 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001664 void __move_assign(basic_string& __str, false_type)
1665 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnantcf823322010-12-17 14:46:43 +00001666 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001667 void __move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001668#if _LIBCPP_STD_VER > 14
1669 _NOEXCEPT;
1670#else
Howard Hinnant3e276872011-06-03 18:40:47 +00001671 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001672#endif
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001673#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001674
1675 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001676 void
Howard Hinnantc2734962011-09-02 20:42:31 +00001677 __move_assign_alloc(basic_string& __str)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001678 _NOEXCEPT_(
1679 !__alloc_traits::propagate_on_container_move_assignment::value ||
1680 is_nothrow_move_assignable<allocator_type>::value)
1681 {__move_assign_alloc(__str, integral_constant<bool,
1682 __alloc_traits::propagate_on_container_move_assignment::value>());}
1683
1684 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00001685 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001686 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1687 {
1688 __alloc() = _VSTD::move(__c.__alloc());
1689 }
1690
1691 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001692 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001693 _NOEXCEPT
1694 {}
1695
Martijn Velsda7d94f2020-06-19 14:24:03 -04001696 basic_string& __assign_external(const value_type* __s);
1697 basic_string& __assign_external(const value_type* __s, size_type __n);
1698
1699 // Assigns the value in __s, guaranteed to be __n < __min_cap in length.
1700 inline basic_string& __assign_short(const value_type* __s, size_type __n) {
1701 pointer __p = __is_long()
1702 ? (__set_long_size(__n), __get_long_pointer())
1703 : (__set_short_size(__n), __get_short_pointer());
1704 traits_type::move(_VSTD::__to_address(__p), __s, __n);
1705 traits_type::assign(__p[__n], value_type());
1706 return *this;
1707 }
1708
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01001709 _LIBCPP_HIDE_FROM_ABI basic_string& __null_terminate_at(value_type* __p, size_type __newsz) {
1710 __set_size(__newsz);
1711 __invalidate_iterators_past(__newsz);
1712 traits_type::assign(__p[__newsz], value_type());
1713 return *this;
1714 }
1715
Howard Hinnantcf823322010-12-17 14:46:43 +00001716 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
1717 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(size_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001718
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001719 template<class _Tp>
1720 _LIBCPP_INLINE_VISIBILITY
1721 bool __addr_in_range(_Tp&& __t) const {
1722 const volatile void *__p = _VSTD::addressof(__t);
1723 return data() <= __p && __p <= data() + size();
1724 }
1725
Louis Dionned24191c2021-08-19 12:39:16 -04001726 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1727 void __throw_length_error() const {
Nikolas Klauserf89bb932022-01-24 19:44:34 +01001728 _VSTD::__throw_length_error("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001729 }
1730
1731 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1732 void __throw_out_of_range() const {
Nikolas Klauserf89bb932022-01-24 19:44:34 +01001733 _VSTD::__throw_out_of_range("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001734 }
1735
Howard Hinnantc51e1022010-05-11 19:42:16 +00001736 friend basic_string operator+<>(const basic_string&, const basic_string&);
1737 friend basic_string operator+<>(const value_type*, const basic_string&);
1738 friend basic_string operator+<>(value_type, const basic_string&);
1739 friend basic_string operator+<>(const basic_string&, const value_type*);
1740 friend basic_string operator+<>(const basic_string&, value_type);
1741};
1742
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001743// These declarations must appear before any functions are implicitly used
1744// so that they have the correct visibility specifier.
1745#ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
Louis Dionne89258142021-08-23 15:32:36 -04001746 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1747# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1748 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1749# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001750#else
Louis Dionne89258142021-08-23 15:32:36 -04001751 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1752# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1753 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1754# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001755#endif
1756
1757
Louis Dionned59f8a52021-08-17 11:59:07 -04001758#if _LIBCPP_STD_VER >= 17
Marshall Clowa0563332018-02-08 06:34:03 +00001759template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -05001760 class _CharT = __iter_value_type<_InputIterator>,
Marshall Clowa0563332018-02-08 06:34:03 +00001761 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001762 class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
1763 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowa0563332018-02-08 06:34:03 +00001764 >
1765basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
1766 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
Marshall Clowe46031a2018-07-02 18:41:15 +00001767
1768template<class _CharT,
1769 class _Traits,
1770 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001771 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowe46031a2018-07-02 18:41:15 +00001772 >
1773explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
1774 -> basic_string<_CharT, _Traits, _Allocator>;
1775
1776template<class _CharT,
1777 class _Traits,
1778 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001779 class = enable_if_t<__is_allocator<_Allocator>::value>,
Marshall Clowe46031a2018-07-02 18:41:15 +00001780 class _Sz = typename allocator_traits<_Allocator>::size_type
1781 >
1782basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
1783 -> basic_string<_CharT, _Traits, _Allocator>;
Marshall Clowa0563332018-02-08 06:34:03 +00001784#endif
1785
Howard Hinnantc51e1022010-05-11 19:42:16 +00001786template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001787inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001788void
1789basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
1790{
Louis Dionneba400782020-10-02 15:02:52 -04001791#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001792 if (!__libcpp_is_constant_evaluated())
1793 __get_db()->__invalidate_all(this);
Louis Dionneba400782020-10-02 15:02:52 -04001794#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001795}
1796
1797template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001798inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001799void
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001800basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type __pos)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001801{
Louis Dionneba400782020-10-02 15:02:52 -04001802#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001803 if (!__libcpp_is_constant_evaluated()) {
1804 __c_node* __c = __get_db()->__find_c_and_lock(this);
1805 if (__c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001806 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001807 const_pointer __new_last = __get_pointer() + __pos;
1808 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001809 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001810 --__p;
1811 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1812 if (__i->base() > __new_last)
1813 {
1814 (*__p)->__c_ = nullptr;
1815 if (--__c->end_ != __p)
1816 _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1817 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001818 }
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001819 __get_db()->unlock();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001820 }
1821 }
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001822#else
1823 (void)__pos;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001824#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001825}
1826
1827template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001828inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001829basic_string<_CharT, _Traits, _Allocator>::basic_string()
Marshall Clowe546cbb2015-06-04 02:05:41 +00001830 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001831 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001832{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001833 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001834 __zero();
1835}
1836
1837template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001838inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001839basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
Eric Fiseliere012bf22015-07-18 20:40:46 +00001840#if _LIBCPP_STD_VER <= 14
1841 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1842#else
1843 _NOEXCEPT
1844#endif
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001845: __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001846{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001847 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001848 __zero();
1849}
1850
1851template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier815ed732016-09-16 00:00:48 +00001852void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1853 size_type __sz,
1854 size_type __reserve)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001855{
1856 if (__reserve > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001857 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001858 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001859 if (__fits_in_sso(__reserve))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001860 {
1861 __set_short_size(__sz);
1862 __p = __get_short_pointer();
1863 }
1864 else
1865 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001866 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__reserve) + 1);
1867 __p = __allocation.ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001868 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001869 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001870 __set_long_size(__sz);
1871 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001872 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001873 traits_type::assign(__p[__sz], value_type());
1874}
1875
1876template <class _CharT, class _Traits, class _Allocator>
1877void
Howard Hinnantd17880b2013-06-28 16:59:19 +00001878basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001879{
1880 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001881 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001882 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001883 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001884 {
1885 __set_short_size(__sz);
1886 __p = __get_short_pointer();
1887 }
1888 else
1889 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001890 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
1891 __p = __allocation.ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001892 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001893 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001894 __set_long_size(__sz);
1895 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001896 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001897 traits_type::assign(__p[__sz], value_type());
1898}
1899
1900template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00001901template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00001902basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001903 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001904{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001905 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001906 __init(__s, traits_type::length(__s));
Nikolas Klauserf2807732022-01-11 00:33:35 +01001907 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001908}
1909
1910template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001911inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001912basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001913 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001914{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001915 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
1916 __init(__s, __n);
1917 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001918}
1919
1920template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001921inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001922basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001923 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001924{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001925 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001926 __init(__s, __n);
Nikolas Klauserf2807732022-01-11 00:33:35 +01001927 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001928}
1929
1930template <class _CharT, class _Traits, class _Allocator>
1931basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001932 : __r_(__default_init_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001933{
1934 if (!__str.__is_long())
1935 __r_.first().__r = __str.__r_.first().__r;
1936 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001937 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1938 __str.__get_long_size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01001939 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001940}
1941
1942template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00001943basic_string<_CharT, _Traits, _Allocator>::basic_string(
1944 const basic_string& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001945 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001946{
1947 if (!__str.__is_long())
1948 __r_.first().__r = __str.__r_.first().__r;
1949 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001950 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1951 __str.__get_long_size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01001952 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001953}
1954
Martijn Vels5e7c9752020-03-04 17:52:46 -05001955template <class _CharT, class _Traits, class _Allocator>
1956void basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(
1957 const value_type* __s, size_type __sz) {
1958 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001959 if (__fits_in_sso(__sz)) {
Martijn Vels5e7c9752020-03-04 17:52:46 -05001960 __p = __get_short_pointer();
1961 __set_short_size(__sz);
1962 } else {
1963 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001964 __throw_length_error();
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001965 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
1966 __p = __allocation.ptr;
Martijn Vels5e7c9752020-03-04 17:52:46 -05001967 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001968 __set_long_cap(__allocation.count);
Martijn Vels5e7c9752020-03-04 17:52:46 -05001969 __set_long_size(__sz);
1970 }
1971 traits_type::copy(_VSTD::__to_address(__p), __s, __sz + 1);
1972}
1973
Eric Fiselierfc92be82017-04-19 00:28:44 +00001974#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001975
1976template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001977inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001978basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +00001979#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +00001980 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowa80abc72015-06-03 19:56:43 +00001981#else
1982 _NOEXCEPT
1983#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001984 : __r_(_VSTD::move(__str.__r_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001985{
1986 __str.__zero();
Nikolas Klauserf2807732022-01-11 00:33:35 +01001987 _VSTD::__debug_db_insert_c(this);
Louis Dionneba400782020-10-02 15:02:52 -04001988#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauserf2807732022-01-11 00:33:35 +01001989 if (!__libcpp_is_constant_evaluated() && __is_long())
1990 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001991#endif
1992}
1993
1994template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001995inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001996basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001997 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001998{
Marshall Clowd2d24692014-07-17 15:32:20 +00001999 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002000 __init(_VSTD::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd2d24692014-07-17 15:32:20 +00002001 else
2002 {
2003 __r_.first().__r = __str.__r_.first().__r;
2004 __str.__zero();
2005 }
Nikolas Klauserf2807732022-01-11 00:33:35 +01002006 _VSTD::__debug_db_insert_c(this);
Louis Dionneba400782020-10-02 15:02:52 -04002007#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauserf2807732022-01-11 00:33:35 +01002008 if (!__libcpp_is_constant_evaluated() && __is_long())
2009 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002010#endif
2011}
2012
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002013#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002014
2015template <class _CharT, class _Traits, class _Allocator>
2016void
2017basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
2018{
2019 if (__n > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002020 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002021 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002022 if (__fits_in_sso(__n))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002023 {
2024 __set_short_size(__n);
2025 __p = __get_short_pointer();
2026 }
2027 else
2028 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002029 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__n) + 1);
2030 __p = __allocation.ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002031 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002032 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002033 __set_long_size(__n);
2034 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002035 traits_type::assign(_VSTD::__to_address(__p), __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002036 traits_type::assign(__p[__n], value_type());
2037}
2038
2039template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002040inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002041basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002042 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002043{
2044 __init(__n, __c);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002045 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002046}
2047
2048template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002049template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00002050basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002051 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002052{
2053 __init(__n, __c);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002054 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002055}
2056
Howard Hinnantc51e1022010-05-11 19:42:16 +00002057template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00002058basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
2059 size_type __pos, size_type __n,
2060 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002061 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002062{
2063 size_type __str_sz = __str.size();
2064 if (__pos > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002065 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002066 __init(__str.data() + __pos, _VSTD::min(__n, __str_sz - __pos));
Nikolas Klauserf2807732022-01-11 00:33:35 +01002067 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002068}
2069
2070template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002071inline
Marshall Clow83445802016-04-07 18:13:41 +00002072basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +00002073 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002074 : __r_(__default_init_tag(), __a)
Marshall Clow83445802016-04-07 18:13:41 +00002075{
2076 size_type __str_sz = __str.size();
2077 if (__pos > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002078 __throw_out_of_range();
Marshall Clow83445802016-04-07 18:13:41 +00002079 __init(__str.data() + __pos, __str_sz - __pos);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002080 _VSTD::__debug_db_insert_c(this);
Marshall Clow83445802016-04-07 18:13:41 +00002081}
2082
2083template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002084template <class _Tp, class>
Marshall Clow78dbe462016-11-14 18:22:19 +00002085basic_string<_CharT, _Traits, _Allocator>::basic_string(
Marshall Clowe46031a2018-07-02 18:41:15 +00002086 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002087 : __r_(__default_init_tag(), __a)
Marshall Clow78dbe462016-11-14 18:22:19 +00002088{
Marshall Clowe46031a2018-07-02 18:41:15 +00002089 __self_view __sv0 = __t;
2090 __self_view __sv = __sv0.substr(__pos, __n);
Marshall Clow78dbe462016-11-14 18:22:19 +00002091 __init(__sv.data(), __sv.size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002092 _VSTD::__debug_db_insert_c(this);
Marshall Clow78dbe462016-11-14 18:22:19 +00002093}
2094
2095template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002096template <class _Tp, class>
2097basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002098 : __r_(__default_init_tag(), __default_init_tag())
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002099{
Marshall Clowe46031a2018-07-02 18:41:15 +00002100 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002101 __init(__sv.data(), __sv.size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002102 _VSTD::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002103}
2104
2105template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002106template <class _Tp, class>
2107basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002108 : __r_(__default_init_tag(), __a)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002109{
Marshall Clowe46031a2018-07-02 18:41:15 +00002110 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002111 __init(__sv.data(), __sv.size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002112 _VSTD::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002113}
2114
2115template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002116template <class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002117__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002118<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002119 __is_exactly_cpp17_input_iterator<_InputIterator>::value
2120>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002121basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
2122{
2123 __zero();
2124#ifndef _LIBCPP_NO_EXCEPTIONS
2125 try
2126 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002127#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002128 for (; __first != __last; ++__first)
2129 push_back(*__first);
2130#ifndef _LIBCPP_NO_EXCEPTIONS
2131 }
2132 catch (...)
2133 {
2134 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002135 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002136 throw;
2137 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002138#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002139}
2140
2141template <class _CharT, class _Traits, class _Allocator>
2142template <class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002143__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002144<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002145 __is_cpp17_forward_iterator<_ForwardIterator>::value
2146>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002147basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
2148{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002149 size_type __sz = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002150 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002151 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002152 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002153 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002154 {
2155 __set_short_size(__sz);
2156 __p = __get_short_pointer();
2157 }
2158 else
2159 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002160 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
2161 __p = __allocation.ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002162 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002163 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002164 __set_long_size(__sz);
2165 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002166
2167#ifndef _LIBCPP_NO_EXCEPTIONS
2168 try
2169 {
2170#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002171 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002172 traits_type::assign(*__p, *__first);
2173 traits_type::assign(*__p, value_type());
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002174#ifndef _LIBCPP_NO_EXCEPTIONS
2175 }
2176 catch (...)
2177 {
2178 if (__is_long())
2179 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
2180 throw;
2181 }
2182#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002183}
2184
2185template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002186template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002187inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002188basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002189 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002190{
2191 __init(__first, __last);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002192 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002193}
2194
2195template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002196template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002197inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002198basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
2199 const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002200 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002201{
2202 __init(__first, __last);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002203 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002204}
2205
Eric Fiselierfc92be82017-04-19 00:28:44 +00002206#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002207
Howard Hinnantc51e1022010-05-11 19:42:16 +00002208template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002209inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002210basic_string<_CharT, _Traits, _Allocator>::basic_string(
2211 initializer_list<_CharT> __il)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002212 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002213{
2214 __init(__il.begin(), __il.end());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002215 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002216}
2217
2218template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002219inline
Eric Fiselier9d355982017-04-12 23:45:53 +00002220
Eric Fiselier812882b2017-02-17 01:17:10 +00002221basic_string<_CharT, _Traits, _Allocator>::basic_string(
2222 initializer_list<_CharT> __il, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002223 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002224{
2225 __init(__il.begin(), __il.end());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002226 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002227}
2228
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002229#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002230
Howard Hinnantc51e1022010-05-11 19:42:16 +00002231template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002232basic_string<_CharT, _Traits, _Allocator>::~basic_string()
2233{
Louis Dionneba400782020-10-02 15:02:52 -04002234#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002235 if (!__libcpp_is_constant_evaluated())
2236 __get_db()->__erase_c(this);
Howard Hinnant8ea98242013-08-23 17:37:05 +00002237#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002238 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002239 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002240}
2241
2242template <class _CharT, class _Traits, class _Allocator>
2243void
2244basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
2245 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002246 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 +00002247{
2248 size_type __ms = max_size();
2249 if (__delta_cap > __ms - __old_cap - 1)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002250 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002251 pointer __old_p = __get_pointer();
2252 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002253 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002254 __ms - 1;
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002255 auto __allocation = std::__allocate_at_least(__alloc(), __cap + 1);
2256 pointer __p = __allocation.ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002257 __invalidate_all_iterators();
2258 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002259 traits_type::copy(_VSTD::__to_address(__p),
2260 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002261 if (__n_add != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002262 traits_type::copy(_VSTD::__to_address(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002263 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2264 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002265 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2266 _VSTD::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002267 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002268 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002269 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002270 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002271 __old_sz = __n_copy + __n_add + __sec_cp_sz;
2272 __set_long_size(__old_sz);
2273 traits_type::assign(__p[__old_sz], value_type());
2274}
2275
2276template <class _CharT, class _Traits, class _Allocator>
2277void
2278basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2279 size_type __n_copy, size_type __n_del, size_type __n_add)
2280{
2281 size_type __ms = max_size();
Marshall Clow2267c5d2013-11-06 14:24:38 +00002282 if (__delta_cap > __ms - __old_cap)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002283 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002284 pointer __old_p = __get_pointer();
2285 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002286 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002287 __ms - 1;
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002288 auto __allocation = std::__allocate_at_least(__alloc(), __cap + 1);
2289 pointer __p = __allocation.ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002290 __invalidate_all_iterators();
2291 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002292 traits_type::copy(_VSTD::__to_address(__p),
2293 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002294 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2295 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002296 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2297 _VSTD::__to_address(__old_p) + __n_copy + __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002298 __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002299 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002300 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002301 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002302 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002303}
2304
2305// assign
2306
2307template <class _CharT, class _Traits, class _Allocator>
Martijn Vels596e3de2020-02-26 15:55:49 -05002308template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04002309basic_string<_CharT, _Traits, _Allocator>&
2310basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(
Martijn Vels596e3de2020-02-26 15:55:49 -05002311 const value_type* __s, size_type __n) {
Louis Dionne6209e9f2022-03-03 13:39:12 -05002312 size_type __cap = __is_short ? static_cast<size_type>(__min_cap) : __get_long_cap();
Martijn Vels596e3de2020-02-26 15:55:49 -05002313 if (__n < __cap) {
2314 pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
2315 __is_short ? __set_short_size(__n) : __set_long_size(__n);
2316 traits_type::copy(_VSTD::__to_address(__p), __s, __n);
2317 traits_type::assign(__p[__n], value_type());
2318 __invalidate_iterators_past(__n);
2319 } else {
2320 size_type __sz = __is_short ? __get_short_size() : __get_long_size();
2321 __grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
2322 }
Martijn Velsb6a08b62020-04-10 18:36:31 -04002323 return *this;
Martijn Vels596e3de2020-02-26 15:55:49 -05002324}
2325
2326template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002327basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002328basic_string<_CharT, _Traits, _Allocator>::__assign_external(
2329 const value_type* __s, size_type __n) {
2330 size_type __cap = capacity();
2331 if (__cap >= __n) {
2332 value_type* __p = _VSTD::__to_address(__get_pointer());
2333 traits_type::move(__p, __s, __n);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002334 return __null_terminate_at(__p, __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04002335 } else {
2336 size_type __sz = size();
2337 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002338 return *this;
Martijn Velsda7d94f2020-06-19 14:24:03 -04002339 }
Martijn Velsda7d94f2020-06-19 14:24:03 -04002340}
2341
2342template <class _CharT, class _Traits, class _Allocator>
2343basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002344basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002345{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002346 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Nikolas Klauser93826d12022-01-04 17:24:03 +01002347 return (__builtin_constant_p(__n) && __fits_in_sso(__n))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002348 ? __assign_short(__s, __n)
2349 : __assign_external(__s, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002350}
2351
2352template <class _CharT, class _Traits, class _Allocator>
2353basic_string<_CharT, _Traits, _Allocator>&
2354basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2355{
2356 size_type __cap = capacity();
2357 if (__cap < __n)
2358 {
2359 size_type __sz = size();
2360 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2361 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002362 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002363 traits_type::assign(__p, __n, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002364 return __null_terminate_at(__p, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002365}
2366
2367template <class _CharT, class _Traits, class _Allocator>
2368basic_string<_CharT, _Traits, _Allocator>&
2369basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2370{
2371 pointer __p;
2372 if (__is_long())
2373 {
2374 __p = __get_long_pointer();
2375 __set_long_size(1);
2376 }
2377 else
2378 {
2379 __p = __get_short_pointer();
2380 __set_short_size(1);
2381 }
2382 traits_type::assign(*__p, __c);
2383 traits_type::assign(*++__p, value_type());
2384 __invalidate_iterators_past(1);
2385 return *this;
2386}
2387
2388template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002389basic_string<_CharT, _Traits, _Allocator>&
2390basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2391{
Martijn Vels596e3de2020-02-26 15:55:49 -05002392 if (this != &__str) {
2393 __copy_assign_alloc(__str);
2394 if (!__is_long()) {
2395 if (!__str.__is_long()) {
Eric Fiselier03bbc922020-01-15 17:27:10 -05002396 __r_.first().__r = __str.__r_.first().__r;
Martijn Vels596e3de2020-02-26 15:55:49 -05002397 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002398 return __assign_no_alias<true>(__str.data(), __str.size());
Martijn Vels596e3de2020-02-26 15:55:49 -05002399 }
2400 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002401 return __assign_no_alias<false>(__str.data(), __str.size());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002402 }
Martijn Vels596e3de2020-02-26 15:55:49 -05002403 }
2404 return *this;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002405}
2406
Eric Fiselierfc92be82017-04-19 00:28:44 +00002407#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002408
2409template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002410inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002411void
2412basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002413 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002414{
2415 if (__alloc() != __str.__alloc())
2416 assign(__str);
2417 else
2418 __move_assign(__str, true_type());
2419}
2420
2421template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002422inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002423void
2424basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002425#if _LIBCPP_STD_VER > 14
2426 _NOEXCEPT
2427#else
Howard Hinnant3e276872011-06-03 18:40:47 +00002428 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002429#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002430{
marshall2ed41622019-11-27 07:13:00 -08002431 if (__is_long()) {
2432 __alloc_traits::deallocate(__alloc(), __get_long_pointer(),
2433 __get_long_cap());
2434#if _LIBCPP_STD_VER <= 14
2435 if (!is_nothrow_move_assignable<allocator_type>::value) {
2436 __set_short_size(0);
2437 traits_type::assign(__get_short_pointer()[0], value_type());
2438 }
2439#endif
2440 }
2441 __move_assign_alloc(__str);
2442 __r_.first() = __str.__r_.first();
2443 __str.__set_short_size(0);
2444 traits_type::assign(__str.__get_short_pointer()[0], value_type());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002445}
2446
2447template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002448inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002449basic_string<_CharT, _Traits, _Allocator>&
2450basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002451 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002452{
2453 __move_assign(__str, integral_constant<bool,
2454 __alloc_traits::propagate_on_container_move_assignment::value>());
2455 return *this;
2456}
2457
2458#endif
2459
2460template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002461template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002462__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002463<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002464 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002465 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002466>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002467basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2468{
Marshall Clow958362f2016-09-05 01:54:30 +00002469 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002470 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002471 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002472}
2473
2474template <class _CharT, class _Traits, class _Allocator>
2475template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002476__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002477<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002478 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002479 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002480>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002481basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2482{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002483 size_type __cap = capacity();
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002484 size_type __n = __string_is_trivial_iterator<_ForwardIterator>::value ?
2485 static_cast<size_type>(_VSTD::distance(__first, __last)) : 0;
2486
2487 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2488 (__cap >= __n || !__addr_in_range(*__first)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002489 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002490 if (__cap < __n)
2491 {
2492 size_type __sz = size();
2493 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2494 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002495 pointer __p = __get_pointer();
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002496 for (; __first != __last; ++__p, (void) ++__first)
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002497 traits_type::assign(*__p, *__first);
2498 traits_type::assign(*__p, value_type());
2499 __set_size(__n);
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04002500 __invalidate_iterators_past(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002501 }
2502 else
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002503 {
2504 const basic_string __temp(__first, __last, __alloc());
2505 assign(__temp.data(), __temp.size());
2506 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002507 return *this;
2508}
2509
2510template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002511basic_string<_CharT, _Traits, _Allocator>&
2512basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2513{
2514 size_type __sz = __str.size();
2515 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002516 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002517 return assign(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002518}
2519
2520template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002521template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002522__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002523<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002524 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
2525 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00002526 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002527>
Marshall Clow82513342016-09-24 22:45:42 +00002528basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002529{
Marshall Clow82513342016-09-24 22:45:42 +00002530 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002531 size_type __sz = __sv.size();
2532 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002533 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002534 return assign(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2535}
2536
2537
2538template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002539basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002540basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s) {
2541 return __assign_external(__s, traits_type::length(__s));
2542}
2543
2544template <class _CharT, class _Traits, class _Allocator>
2545basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002546basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002547{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002548 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Louis Dionne3e0c4332021-08-31 10:49:06 -04002549 return __builtin_constant_p(*__s)
Nikolas Klauser93826d12022-01-04 17:24:03 +01002550 ? (__fits_in_sso(traits_type::length(__s))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002551 ? __assign_short(__s, traits_type::length(__s))
2552 : __assign_external(__s, traits_type::length(__s)))
2553 : __assign_external(__s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002554}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002555// append
2556
2557template <class _CharT, class _Traits, class _Allocator>
2558basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002559basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002560{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002561 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002562 size_type __cap = capacity();
2563 size_type __sz = size();
2564 if (__cap - __sz >= __n)
2565 {
2566 if (__n)
2567 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002568 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002569 traits_type::copy(__p + __sz, __s, __n);
2570 __sz += __n;
2571 __set_size(__sz);
2572 traits_type::assign(__p[__sz], value_type());
2573 }
2574 }
2575 else
2576 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2577 return *this;
2578}
2579
2580template <class _CharT, class _Traits, class _Allocator>
2581basic_string<_CharT, _Traits, _Allocator>&
2582basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2583{
2584 if (__n)
2585 {
2586 size_type __cap = capacity();
2587 size_type __sz = size();
2588 if (__cap - __sz < __n)
2589 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2590 pointer __p = __get_pointer();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002591 traits_type::assign(_VSTD::__to_address(__p) + __sz, __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002592 __sz += __n;
2593 __set_size(__sz);
2594 traits_type::assign(__p[__sz], value_type());
2595 }
2596 return *this;
2597}
2598
2599template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00002600inline void
2601basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n)
2602{
2603 if (__n)
2604 {
2605 size_type __cap = capacity();
2606 size_type __sz = size();
2607 if (__cap - __sz < __n)
2608 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2609 pointer __p = __get_pointer();
2610 __sz += __n;
2611 __set_size(__sz);
2612 traits_type::assign(__p[__sz], value_type());
2613 }
2614}
2615
2616template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002617void
2618basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2619{
Howard Hinnant68bf1812013-04-30 21:44:48 +00002620 bool __is_short = !__is_long();
2621 size_type __cap;
2622 size_type __sz;
2623 if (__is_short)
2624 {
2625 __cap = __min_cap - 1;
2626 __sz = __get_short_size();
2627 }
2628 else
2629 {
2630 __cap = __get_long_cap() - 1;
2631 __sz = __get_long_size();
2632 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002633 if (__sz == __cap)
Howard Hinnant68bf1812013-04-30 21:44:48 +00002634 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002635 __grow_by(__cap, 1, __sz, __sz, 0);
Louis Dionne82d2b2c2021-11-16 11:26:56 -05002636 __is_short = false; // the string is always long after __grow_by
Howard Hinnant68bf1812013-04-30 21:44:48 +00002637 }
2638 pointer __p;
2639 if (__is_short)
2640 {
2641 __p = __get_short_pointer() + __sz;
2642 __set_short_size(__sz+1);
2643 }
2644 else
2645 {
2646 __p = __get_long_pointer() + __sz;
2647 __set_long_size(__sz+1);
2648 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002649 traits_type::assign(*__p, __c);
2650 traits_type::assign(*++__p, value_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002651}
2652
Howard Hinnantc51e1022010-05-11 19:42:16 +00002653template <class _CharT, class _Traits, class _Allocator>
2654template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002655__enable_if_t
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002656<
2657 __is_cpp17_forward_iterator<_ForwardIterator>::value,
2658 basic_string<_CharT, _Traits, _Allocator>&
2659>
2660basic_string<_CharT, _Traits, _Allocator>::append(
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002661 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002662{
2663 size_type __sz = size();
2664 size_type __cap = capacity();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002665 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002666 if (__n)
2667 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002668 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2669 !__addr_in_range(*__first))
Marshall Clow958362f2016-09-05 01:54:30 +00002670 {
2671 if (__cap - __sz < __n)
2672 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2673 pointer __p = __get_pointer() + __sz;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002674 for (; __first != __last; ++__p, (void) ++__first)
Marshall Clow958362f2016-09-05 01:54:30 +00002675 traits_type::assign(*__p, *__first);
2676 traits_type::assign(*__p, value_type());
2677 __set_size(__sz + __n);
2678 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002679 else
2680 {
2681 const basic_string __temp(__first, __last, __alloc());
2682 append(__temp.data(), __temp.size());
2683 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002684 }
2685 return *this;
2686}
2687
2688template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002689inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002690basic_string<_CharT, _Traits, _Allocator>&
2691basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2692{
2693 return append(__str.data(), __str.size());
2694}
2695
2696template <class _CharT, class _Traits, class _Allocator>
2697basic_string<_CharT, _Traits, _Allocator>&
2698basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2699{
2700 size_type __sz = __str.size();
2701 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002702 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002703 return append(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002704}
2705
2706template <class _CharT, class _Traits, class _Allocator>
Marshall Clow62953962016-10-03 23:40:48 +00002707template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002708 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002709 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002710 __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 +00002711 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002712 >
Marshall Clow82513342016-09-24 22:45:42 +00002713basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002714{
Marshall Clow82513342016-09-24 22:45:42 +00002715 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002716 size_type __sz = __sv.size();
2717 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002718 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002719 return append(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2720}
2721
2722template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002723basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002724basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002725{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002726 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002727 return append(__s, traits_type::length(__s));
2728}
2729
2730// insert
2731
2732template <class _CharT, class _Traits, class _Allocator>
2733basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002734basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002735{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002736 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002737 size_type __sz = size();
2738 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002739 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002740 size_type __cap = capacity();
2741 if (__cap - __sz >= __n)
2742 {
2743 if (__n)
2744 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002745 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002746 size_type __n_move = __sz - __pos;
2747 if (__n_move != 0)
2748 {
2749 if (__p + __pos <= __s && __s < __p + __sz)
2750 __s += __n;
2751 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2752 }
2753 traits_type::move(__p + __pos, __s, __n);
2754 __sz += __n;
2755 __set_size(__sz);
2756 traits_type::assign(__p[__sz], value_type());
2757 }
2758 }
2759 else
2760 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2761 return *this;
2762}
2763
2764template <class _CharT, class _Traits, class _Allocator>
2765basic_string<_CharT, _Traits, _Allocator>&
2766basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2767{
2768 size_type __sz = size();
2769 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002770 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002771 if (__n)
2772 {
2773 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002774 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002775 if (__cap - __sz >= __n)
2776 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002777 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002778 size_type __n_move = __sz - __pos;
2779 if (__n_move != 0)
2780 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2781 }
2782 else
2783 {
2784 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002785 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002786 }
2787 traits_type::assign(__p + __pos, __n, __c);
2788 __sz += __n;
2789 __set_size(__sz);
2790 traits_type::assign(__p[__sz], value_type());
2791 }
2792 return *this;
2793}
2794
2795template <class _CharT, class _Traits, class _Allocator>
2796template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002797__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002798<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002799 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Marshall Clow039b2f02016-01-13 21:54:34 +00002800 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002801>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002802basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2803{
Nikolas Klausereed25832021-12-15 01:32:30 +01002804 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2805 "string::insert(iterator, range) called with an iterator not"
2806 " referring to this string");
2807 const basic_string __temp(__first, __last, __alloc());
2808 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002809}
2810
2811template <class _CharT, class _Traits, class _Allocator>
2812template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002813__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002814<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002815 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002816 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002817>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002818basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2819{
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002820 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2821 "string::insert(iterator, range) called with an iterator not referring to this string");
Nikolas Klausereed25832021-12-15 01:32:30 +01002822
Howard Hinnantc51e1022010-05-11 19:42:16 +00002823 size_type __ip = static_cast<size_type>(__pos - begin());
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002824 size_type __n = static_cast<size_type>(std::distance(__first, __last));
2825 if (__n == 0)
2826 return begin() + __ip;
2827
2828 if (__string_is_trivial_iterator<_ForwardIterator>::value && !__addr_in_range(*__first))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002829 {
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002830 return __insert_from_safe_copy(__n, __ip, __first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002831 }
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002832 else
2833 {
2834 const basic_string __temp(__first, __last, __alloc());
2835 return __insert_from_safe_copy(__n, __ip, __temp.begin(), __temp.end());
2836 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002837}
2838
2839template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002840inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002841basic_string<_CharT, _Traits, _Allocator>&
2842basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2843{
2844 return insert(__pos1, __str.data(), __str.size());
2845}
2846
2847template <class _CharT, class _Traits, class _Allocator>
2848basic_string<_CharT, _Traits, _Allocator>&
2849basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2850 size_type __pos2, size_type __n)
2851{
2852 size_type __str_sz = __str.size();
2853 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002854 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002855 return insert(__pos1, __str.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002856}
2857
2858template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002859template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002860__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002861<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002862 __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 +00002863 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002864>
Marshall Clow82513342016-09-24 22:45:42 +00002865basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002866 size_type __pos2, size_type __n)
2867{
Marshall Clow82513342016-09-24 22:45:42 +00002868 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002869 size_type __str_sz = __sv.size();
2870 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002871 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002872 return insert(__pos1, __sv.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
2873}
2874
2875template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002876basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002877basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002878{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002879 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002880 return insert(__pos, __s, traits_type::length(__s));
2881}
2882
2883template <class _CharT, class _Traits, class _Allocator>
2884typename basic_string<_CharT, _Traits, _Allocator>::iterator
2885basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2886{
Louis Dionnef4cc6fb2022-02-15 15:47:45 -05002887 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2888 "string::insert(iterator, character) called with an iterator not"
2889 " referring to this string");
2890
Howard Hinnantc51e1022010-05-11 19:42:16 +00002891 size_type __ip = static_cast<size_type>(__pos - begin());
2892 size_type __sz = size();
2893 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002894 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002895 if (__cap == __sz)
2896 {
2897 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002898 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002899 }
2900 else
2901 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002902 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002903 size_type __n_move = __sz - __ip;
2904 if (__n_move != 0)
2905 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2906 }
2907 traits_type::assign(__p[__ip], __c);
2908 traits_type::assign(__p[++__sz], value_type());
2909 __set_size(__sz);
2910 return begin() + static_cast<difference_type>(__ip);
2911}
2912
2913template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002914inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002915typename basic_string<_CharT, _Traits, _Allocator>::iterator
2916basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2917{
Nikolas Klausereed25832021-12-15 01:32:30 +01002918 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2919 "string::insert(iterator, n, value) called with an iterator not"
2920 " referring to this string");
2921 difference_type __p = __pos - begin();
2922 insert(static_cast<size_type>(__p), __n, __c);
2923 return begin() + __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002924}
2925
2926// replace
2927
2928template <class _CharT, class _Traits, class _Allocator>
2929basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002930basic_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 +00002931 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00002932{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002933 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002934 size_type __sz = size();
2935 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002936 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002937 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002938 size_type __cap = capacity();
2939 if (__cap - __sz + __n1 >= __n2)
2940 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002941 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002942 if (__n1 != __n2)
2943 {
2944 size_type __n_move = __sz - __pos - __n1;
2945 if (__n_move != 0)
2946 {
2947 if (__n1 > __n2)
2948 {
2949 traits_type::move(__p + __pos, __s, __n2);
2950 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002951 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002952 }
2953 if (__p + __pos < __s && __s < __p + __sz)
2954 {
2955 if (__p + __pos + __n1 <= __s)
2956 __s += __n2 - __n1;
2957 else // __p + __pos < __s < __p + __pos + __n1
2958 {
2959 traits_type::move(__p + __pos, __s, __n1);
2960 __pos += __n1;
2961 __s += __n2;
2962 __n2 -= __n1;
2963 __n1 = 0;
2964 }
2965 }
2966 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2967 }
2968 }
2969 traits_type::move(__p + __pos, __s, __n2);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002970 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002971 }
2972 else
2973 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
2974 return *this;
2975}
2976
2977template <class _CharT, class _Traits, class _Allocator>
2978basic_string<_CharT, _Traits, _Allocator>&
2979basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
2980{
2981 size_type __sz = size();
2982 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002983 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002984 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002985 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002986 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002987 if (__cap - __sz + __n1 >= __n2)
2988 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002989 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002990 if (__n1 != __n2)
2991 {
2992 size_type __n_move = __sz - __pos - __n1;
2993 if (__n_move != 0)
2994 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2995 }
2996 }
2997 else
2998 {
2999 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003000 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003001 }
3002 traits_type::assign(__p + __pos, __n2, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003003 return __null_terminate_at(__p, __sz - (__n1 - __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003004}
3005
3006template <class _CharT, class _Traits, class _Allocator>
3007template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003008__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00003009<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003010 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003011 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003012>
Howard Hinnant990d6e82010-11-17 21:11:40 +00003013basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003014 _InputIterator __j1, _InputIterator __j2)
3015{
Marshall Clow958362f2016-09-05 01:54:30 +00003016 const basic_string __temp(__j1, __j2, __alloc());
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003017 return replace(__i1, __i2, __temp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003018}
3019
3020template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003021inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003022basic_string<_CharT, _Traits, _Allocator>&
3023basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
3024{
3025 return replace(__pos1, __n1, __str.data(), __str.size());
3026}
3027
3028template <class _CharT, class _Traits, class _Allocator>
3029basic_string<_CharT, _Traits, _Allocator>&
3030basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
3031 size_type __pos2, size_type __n2)
3032{
3033 size_type __str_sz = __str.size();
3034 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003035 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003036 return replace(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003037}
3038
3039template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003040template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003041__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003042<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003043 __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 +00003044 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003045>
Marshall Clow82513342016-09-24 22:45:42 +00003046basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003047 size_type __pos2, size_type __n2)
3048{
Marshall Clow82513342016-09-24 22:45:42 +00003049 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003050 size_type __str_sz = __sv.size();
3051 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003052 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003053 return replace(__pos1, __n1, __sv.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
3054}
3055
3056template <class _CharT, class _Traits, class _Allocator>
3057basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003058basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003059{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003060 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003061 return replace(__pos, __n1, __s, traits_type::length(__s));
3062}
3063
3064template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003065inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003066basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003067basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003068{
3069 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
3070 __str.data(), __str.size());
3071}
3072
3073template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003074inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003075basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003076basic_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 +00003077{
3078 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
3079}
3080
3081template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003082inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003083basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003084basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003085{
3086 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
3087}
3088
3089template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003090inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003091basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003092basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003093{
3094 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
3095}
3096
3097// erase
3098
Martijn Velsa81fc792020-02-26 13:25:43 -05003099// 'externally instantiated' erase() implementation, called when __n != npos.
3100// Does not check __pos against size()
Howard Hinnantc51e1022010-05-11 19:42:16 +00003101template <class _CharT, class _Traits, class _Allocator>
Martijn Velsa81fc792020-02-26 13:25:43 -05003102void
3103basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(
3104 size_type __pos, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003105{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003106 if (__n)
3107 {
Martijn Velsa81fc792020-02-26 13:25:43 -05003108 size_type __sz = size();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003109 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003110 __n = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003111 size_type __n_move = __sz - __pos - __n;
3112 if (__n_move != 0)
3113 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003114 __null_terminate_at(__p, __sz - __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003115 }
Martijn Velsa81fc792020-02-26 13:25:43 -05003116}
3117
3118template <class _CharT, class _Traits, class _Allocator>
3119basic_string<_CharT, _Traits, _Allocator>&
3120basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos,
3121 size_type __n) {
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003122 if (__pos > size())
3123 __throw_out_of_range();
Martijn Velsa81fc792020-02-26 13:25:43 -05003124 if (__n == npos) {
3125 __erase_to_end(__pos);
3126 } else {
3127 __erase_external_with_move(__pos, __n);
3128 }
3129 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003130}
3131
3132template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003133inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003134typename basic_string<_CharT, _Traits, _Allocator>::iterator
3135basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
3136{
Nikolas Klausereed25832021-12-15 01:32:30 +01003137 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3138 "string::erase(iterator) called with an iterator not"
3139 " referring to this string");
3140
3141 _LIBCPP_ASSERT(__pos != end(), "string::erase(iterator) called with a non-dereferenceable iterator");
3142 iterator __b = begin();
3143 size_type __r = static_cast<size_type>(__pos - __b);
3144 erase(__r, 1);
3145 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003146}
3147
3148template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003149inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003150typename basic_string<_CharT, _Traits, _Allocator>::iterator
3151basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
3152{
Nikolas Klausereed25832021-12-15 01:32:30 +01003153 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
3154 "string::erase(iterator, iterator) called with an iterator not"
3155 " referring to this string");
3156
3157 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
3158 iterator __b = begin();
3159 size_type __r = static_cast<size_type>(__first - __b);
3160 erase(__r, static_cast<size_type>(__last - __first));
3161 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003162}
3163
3164template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003165inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003166void
3167basic_string<_CharT, _Traits, _Allocator>::pop_back()
3168{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003169 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003170 __erase_to_end(size() - 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003171}
3172
3173template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003174inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003175void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003176basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003177{
3178 __invalidate_all_iterators();
3179 if (__is_long())
3180 {
3181 traits_type::assign(*__get_long_pointer(), value_type());
3182 __set_long_size(0);
3183 }
3184 else
3185 {
3186 traits_type::assign(*__get_short_pointer(), value_type());
3187 __set_short_size(0);
3188 }
3189}
3190
3191template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003192inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003193void
3194basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
3195{
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003196 __null_terminate_at(_VSTD::__to_address(__get_pointer()), __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003197}
3198
3199template <class _CharT, class _Traits, class _Allocator>
3200void
3201basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
3202{
3203 size_type __sz = size();
3204 if (__n > __sz)
3205 append(__n - __sz, __c);
3206 else
3207 __erase_to_end(__n);
3208}
3209
3210template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00003211inline void
3212basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n)
3213{
3214 size_type __sz = size();
3215 if (__n > __sz) {
3216 __append_default_init(__n - __sz);
3217 } else
3218 __erase_to_end(__n);
3219}
3220
3221template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003222inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003223typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003224basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003225{
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003226 size_type __m = __alloc_traits::max_size(__alloc());
Nikolas Klauser62060be2022-04-20 10:52:04 +02003227 if (__m <= std::numeric_limits<size_type>::max() / 2) {
3228 return __m - __alignment;
3229 } else {
3230 bool __uses_lsb = __endian_factor == 2;
3231 return __uses_lsb ? __m - __alignment : (__m / 2) - __alignment;
3232 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00003233}
3234
3235template <class _CharT, class _Traits, class _Allocator>
3236void
Marek Kurdejc9848142020-11-26 10:07:16 +01003237basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __requested_capacity)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003238{
Marek Kurdejc9848142020-11-26 10:07:16 +01003239 if (__requested_capacity > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003240 __throw_length_error();
Marek Kurdejc9848142020-11-26 10:07:16 +01003241
Louis Dionne05b6d3e2022-01-14 12:30:22 -05003242 // Make sure reserve(n) never shrinks. This is technically only required in C++20
3243 // and later (since P0966R1), however we provide consistent behavior in all Standard
3244 // modes because this function is instantiated in the shared library.
3245 if (__requested_capacity <= capacity())
3246 return;
Marek Kurdejc9848142020-11-26 10:07:16 +01003247
3248 size_type __target_capacity = _VSTD::max(__requested_capacity, size());
3249 __target_capacity = __recommend(__target_capacity);
3250 if (__target_capacity == capacity()) return;
3251
3252 __shrink_or_extend(__target_capacity);
3253}
3254
3255template <class _CharT, class _Traits, class _Allocator>
Louis Dionnee38b8642021-12-13 14:15:21 -05003256inline
Marek Kurdejc9848142020-11-26 10:07:16 +01003257void
3258basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT
3259{
3260 size_type __target_capacity = __recommend(size());
3261 if (__target_capacity == capacity()) return;
3262
3263 __shrink_or_extend(__target_capacity);
3264}
3265
3266template <class _CharT, class _Traits, class _Allocator>
Louis Dionnee38b8642021-12-13 14:15:21 -05003267inline
Marek Kurdejc9848142020-11-26 10:07:16 +01003268void
3269basic_string<_CharT, _Traits, _Allocator>::__shrink_or_extend(size_type __target_capacity)
3270{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003271 size_type __cap = capacity();
3272 size_type __sz = size();
Marek Kurdejc9848142020-11-26 10:07:16 +01003273
3274 pointer __new_data, __p;
3275 bool __was_long, __now_long;
3276 if (__target_capacity == __min_cap - 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003277 {
Marek Kurdejc9848142020-11-26 10:07:16 +01003278 __was_long = true;
3279 __now_long = false;
3280 __new_data = __get_short_pointer();
3281 __p = __get_long_pointer();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003282 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003283 else
3284 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02003285 if (__target_capacity > __cap) {
3286 auto __allocation = std::__allocate_at_least(__alloc(), __target_capacity + 1);
3287 __new_data = __allocation.ptr;
3288 __target_capacity = __allocation.count - 1;
3289 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003290 else
3291 {
3292 #ifndef _LIBCPP_NO_EXCEPTIONS
3293 try
3294 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003295 #endif // _LIBCPP_NO_EXCEPTIONS
Nikolas Klauserc513eba2022-04-09 09:41:19 +02003296 auto __allocation = std::__allocate_at_least(__alloc(), __target_capacity + 1);
3297 __new_data = __allocation.ptr;
3298 __target_capacity = __allocation.count - 1;
Marek Kurdejc9848142020-11-26 10:07:16 +01003299 #ifndef _LIBCPP_NO_EXCEPTIONS
3300 }
3301 catch (...)
3302 {
3303 return;
3304 }
3305 #else // _LIBCPP_NO_EXCEPTIONS
3306 if (__new_data == nullptr)
3307 return;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003308 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003309 }
3310 __now_long = true;
3311 __was_long = __is_long();
3312 __p = __get_pointer();
3313 }
3314 traits_type::copy(_VSTD::__to_address(__new_data),
3315 _VSTD::__to_address(__p), size()+1);
3316 if (__was_long)
3317 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
3318 if (__now_long)
3319 {
3320 __set_long_cap(__target_capacity+1);
3321 __set_long_size(__sz);
3322 __set_long_pointer(__new_data);
3323 }
3324 else
3325 __set_short_size(__sz);
3326 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003327}
3328
3329template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003330inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003331typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003332basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003333{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003334 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003335 return *(data() + __pos);
3336}
3337
3338template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003339inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003340typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003341basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003342{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003343 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003344 return *(__get_pointer() + __pos);
3345}
3346
3347template <class _CharT, class _Traits, class _Allocator>
3348typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3349basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3350{
3351 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003352 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003353 return (*this)[__n];
3354}
3355
3356template <class _CharT, class _Traits, class _Allocator>
3357typename basic_string<_CharT, _Traits, _Allocator>::reference
3358basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
3359{
3360 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003361 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003362 return (*this)[__n];
3363}
3364
3365template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003366inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003367typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003368basic_string<_CharT, _Traits, _Allocator>::front() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003369{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003370 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003371 return *__get_pointer();
3372}
3373
3374template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003375inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003376typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003377basic_string<_CharT, _Traits, _Allocator>::front() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003378{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003379 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003380 return *data();
3381}
3382
3383template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003384inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003385typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003386basic_string<_CharT, _Traits, _Allocator>::back() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003387{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003388 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003389 return *(__get_pointer() + size() - 1);
3390}
3391
3392template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003393inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003394typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003395basic_string<_CharT, _Traits, _Allocator>::back() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003396{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003397 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003398 return *(data() + size() - 1);
3399}
3400
3401template <class _CharT, class _Traits, class _Allocator>
3402typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003403basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003404{
3405 size_type __sz = size();
3406 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003407 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003408 size_type __rlen = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003409 traits_type::copy(__s, data() + __pos, __rlen);
3410 return __rlen;
3411}
3412
3413template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003414inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003415basic_string<_CharT, _Traits, _Allocator>
3416basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3417{
3418 return basic_string(*this, __pos, __n, __alloc());
3419}
3420
3421template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003422inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003423void
3424basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003425#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00003426 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00003427#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00003428 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003429 __is_nothrow_swappable<allocator_type>::value)
3430#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003431{
Louis Dionneba400782020-10-02 15:02:52 -04003432#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01003433 if (!__libcpp_is_constant_evaluated()) {
3434 if (!__is_long())
3435 __get_db()->__invalidate_all(this);
3436 if (!__str.__is_long())
3437 __get_db()->__invalidate_all(&__str);
3438 __get_db()->swap(this, &__str);
3439 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00003440#endif
Eric Fiselier9bf691f2016-12-28 05:53:01 +00003441 _LIBCPP_ASSERT(
3442 __alloc_traits::propagate_on_container_swap::value ||
3443 __alloc_traits::is_always_equal::value ||
3444 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003445 _VSTD::swap(__r_.first(), __str.__r_.first());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05003446 _VSTD::__swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003447}
3448
3449// find
3450
3451template <class _Traits>
3452struct _LIBCPP_HIDDEN __traits_eq
3453{
3454 typedef typename _Traits::char_type char_type;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003455 _LIBCPP_INLINE_VISIBILITY
3456 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3457 {return _Traits::eq(__x, __y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003458};
3459
3460template<class _CharT, class _Traits, class _Allocator>
3461typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003462basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003463 size_type __pos,
3464 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003465{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003466 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003467 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003468 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003469}
3470
3471template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003472inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003473typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003474basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3475 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003476{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003477 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003478 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003479}
3480
3481template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003482template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003483__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003484<
3485 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3486 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003487>
Marshall Clowe46031a2018-07-02 18:41:15 +00003488basic_string<_CharT, _Traits, _Allocator>::find(const _Tp &__t,
zoecarver1997e0a2021-02-05 11:54:47 -08003489 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003490{
Marshall Clowe46031a2018-07-02 18:41:15 +00003491 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003492 return __str_find<value_type, size_type, traits_type, npos>
3493 (data(), size(), __sv.data(), __pos, __sv.size());
3494}
3495
3496template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003497inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003498typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003499basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003500 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003501{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003502 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003503 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003504 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003505}
3506
3507template<class _CharT, class _Traits, class _Allocator>
3508typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003509basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3510 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003511{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003512 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003513 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003514}
3515
3516// rfind
3517
3518template<class _CharT, class _Traits, class _Allocator>
3519typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003520basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003521 size_type __pos,
3522 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003523{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003524 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003525 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003526 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003527}
3528
3529template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003530inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003531typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003532basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3533 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003534{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003535 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003536 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003537}
3538
3539template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003540template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003541__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003542<
3543 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3544 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003545>
Marshall Clowe46031a2018-07-02 18:41:15 +00003546basic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003547 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003548{
Marshall Clowe46031a2018-07-02 18:41:15 +00003549 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003550 return __str_rfind<value_type, size_type, traits_type, npos>
3551 (data(), size(), __sv.data(), __pos, __sv.size());
3552}
3553
3554template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003555inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003556typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003557basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003558 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003559{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003560 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003561 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003562 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003563}
3564
3565template<class _CharT, class _Traits, class _Allocator>
3566typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003567basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3568 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003569{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003570 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003571 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003572}
3573
3574// find_first_of
3575
3576template<class _CharT, class _Traits, class _Allocator>
3577typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003578basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003579 size_type __pos,
3580 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003581{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003582 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003583 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003584 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003585}
3586
3587template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003588inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003589typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003590basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3591 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003592{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003593 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003594 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003595}
3596
3597template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003598template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003599__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003600<
3601 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3602 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003603>
Marshall Clowe46031a2018-07-02 18:41:15 +00003604basic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003605 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003606{
Marshall Clowe46031a2018-07-02 18:41:15 +00003607 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003608 return __str_find_first_of<value_type, size_type, traits_type, npos>
3609 (data(), size(), __sv.data(), __pos, __sv.size());
3610}
3611
3612template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003613inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003614typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003615basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003616 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003617{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003618 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003619 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003620 (data(), size(), __s, __pos, traits_type::length(__s));
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_first_of(value_type __c,
3627 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003628{
3629 return find(__c, __pos);
3630}
3631
3632// find_last_of
3633
3634template<class _CharT, class _Traits, class _Allocator>
3635typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003636basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003637 size_type __pos,
3638 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003639{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003640 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003641 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003642 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003643}
3644
3645template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003646inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003647typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003648basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3649 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003650{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003651 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003652 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003653}
3654
3655template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003656template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003657__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003658<
3659 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3660 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003661>
Marshall Clowe46031a2018-07-02 18:41:15 +00003662basic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003663 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003664{
Marshall Clowe46031a2018-07-02 18:41:15 +00003665 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003666 return __str_find_last_of<value_type, size_type, traits_type, npos>
3667 (data(), size(), __sv.data(), __pos, __sv.size());
3668}
3669
3670template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003671inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003672typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003673basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003674 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003675{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003676 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003677 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003678 (data(), size(), __s, __pos, traits_type::length(__s));
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_last_of(value_type __c,
3685 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003686{
3687 return rfind(__c, __pos);
3688}
3689
3690// find_first_not_of
3691
3692template<class _CharT, class _Traits, class _Allocator>
3693typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003694basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003695 size_type __pos,
3696 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003697{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003698 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003699 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003700 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003701}
3702
3703template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003704inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003705typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003706basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3707 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003708{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003709 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003710 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003711}
3712
3713template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003714template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003715__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003716<
3717 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3718 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003719>
Marshall Clowe46031a2018-07-02 18:41:15 +00003720basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003721 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003722{
Marshall Clowe46031a2018-07-02 18:41:15 +00003723 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003724 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3725 (data(), size(), __sv.data(), __pos, __sv.size());
3726}
3727
3728template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003729inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003730typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003731basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003732 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003733{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003734 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003735 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003736 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003737}
3738
3739template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003740inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003741typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003742basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3743 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003744{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003745 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003746 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003747}
3748
3749// find_last_not_of
3750
3751template<class _CharT, class _Traits, class _Allocator>
3752typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003753basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003754 size_type __pos,
3755 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003756{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003757 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003758 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003759 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003760}
3761
3762template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003763inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003764typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003765basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3766 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003767{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003768 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003769 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003770}
3771
3772template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003773template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003774__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003775<
3776 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3777 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003778>
Marshall Clowe46031a2018-07-02 18:41:15 +00003779basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003780 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003781{
Marshall Clowe46031a2018-07-02 18:41:15 +00003782 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003783 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3784 (data(), size(), __sv.data(), __pos, __sv.size());
3785}
3786
3787template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003788inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003789typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003790basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003791 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003792{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003793 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003794 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003795 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003796}
3797
3798template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003799inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003800typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003801basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3802 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003803{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003804 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003805 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003806}
3807
3808// compare
3809
3810template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003811template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003812__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003813<
3814 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3815 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003816>
zoecarver1997e0a2021-02-05 11:54:47 -08003817basic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003818{
Marshall Clowe46031a2018-07-02 18:41:15 +00003819 __self_view __sv = __t;
Howard Hinnantb0485532011-07-24 21:45:06 +00003820 size_t __lhs_sz = size();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003821 size_t __rhs_sz = __sv.size();
3822 int __result = traits_type::compare(data(), __sv.data(),
Howard Hinnantb0485532011-07-24 21:45:06 +00003823 _VSTD::min(__lhs_sz, __rhs_sz));
3824 if (__result != 0)
3825 return __result;
3826 if (__lhs_sz < __rhs_sz)
3827 return -1;
3828 if (__lhs_sz > __rhs_sz)
3829 return 1;
3830 return 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003831}
3832
3833template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003834inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003835int
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003836basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003837{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003838 return compare(__self_view(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003839}
3840
3841template <class _CharT, class _Traits, class _Allocator>
3842int
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003843basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3844 size_type __n1,
Howard Hinnantd17880b2013-06-28 16:59:19 +00003845 const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003846 size_type __n2) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003847{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003848 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003849 size_type __sz = size();
3850 if (__pos1 > __sz || __n2 == npos)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003851 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003852 size_type __rlen = _VSTD::min(__n1, __sz - __pos1);
3853 int __r = traits_type::compare(data() + __pos1, __s, _VSTD::min(__rlen, __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003854 if (__r == 0)
3855 {
3856 if (__rlen < __n2)
3857 __r = -1;
3858 else if (__rlen > __n2)
3859 __r = 1;
3860 }
3861 return __r;
3862}
3863
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003864template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003865template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003866__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003867<
3868 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3869 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003870>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003871basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3872 size_type __n1,
Marshall Clowe46031a2018-07-02 18:41:15 +00003873 const _Tp& __t) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003874{
Marshall Clowe46031a2018-07-02 18:41:15 +00003875 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003876 return compare(__pos1, __n1, __sv.data(), __sv.size());
3877}
3878
3879template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003880inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003881int
3882basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3883 size_type __n1,
3884 const basic_string& __str) const
3885{
3886 return compare(__pos1, __n1, __str.data(), __str.size());
3887}
3888
3889template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003890template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003891__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003892<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003893 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
3894 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00003895 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003896>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003897basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3898 size_type __n1,
Marshall Clow82513342016-09-24 22:45:42 +00003899 const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003900 size_type __pos2,
3901 size_type __n2) const
3902{
Marshall Clow82513342016-09-24 22:45:42 +00003903 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003904 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3905}
3906
3907template <class _CharT, class _Traits, class _Allocator>
3908int
3909basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3910 size_type __n1,
3911 const basic_string& __str,
3912 size_type __pos2,
3913 size_type __n2) const
3914{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003915 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003916}
3917
3918template <class _CharT, class _Traits, class _Allocator>
3919int
3920basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
3921{
3922 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3923 return compare(0, npos, __s, traits_type::length(__s));
3924}
3925
3926template <class _CharT, class _Traits, class _Allocator>
3927int
3928basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3929 size_type __n1,
3930 const value_type* __s) const
3931{
3932 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3933 return compare(__pos1, __n1, __s, traits_type::length(__s));
3934}
3935
Howard Hinnantc51e1022010-05-11 19:42:16 +00003936// __invariants
3937
3938template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003939inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003940bool
3941basic_string<_CharT, _Traits, _Allocator>::__invariants() const
3942{
3943 if (size() > capacity())
3944 return false;
3945 if (capacity() < __min_cap - 1)
3946 return false;
Bruce Mitchener170d8972020-11-24 12:53:53 -05003947 if (data() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003948 return false;
Louis Dionne663415f2020-10-05 16:16:13 -04003949 if (data()[size()] != value_type())
Howard Hinnantc51e1022010-05-11 19:42:16 +00003950 return false;
3951 return true;
3952}
3953
Vedant Kumar55e007e2018-03-08 21:15:26 +00003954// __clear_and_shrink
3955
3956template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003957inline
Louis Dionne173f29e2019-05-29 16:01:36 +00003958void
Marshall Clowe60a7182018-05-29 17:04:37 +00003959basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
Vedant Kumar55e007e2018-03-08 21:15:26 +00003960{
3961 clear();
3962 if(__is_long())
3963 {
3964 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
3965 __set_long_cap(0);
3966 __set_short_size(0);
Louis Dionne663415f2020-10-05 16:16:13 -04003967 traits_type::assign(*__get_short_pointer(), value_type());
Vedant Kumar55e007e2018-03-08 21:15:26 +00003968 }
Louis Dionne173f29e2019-05-29 16:01:36 +00003969}
Vedant Kumar55e007e2018-03-08 21:15:26 +00003970
Howard Hinnantc51e1022010-05-11 19:42:16 +00003971// operator==
3972
3973template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003974inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003975bool
3976operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003977 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003978{
Howard Hinnantaaeb1132013-04-22 23:55:13 +00003979 size_t __lhs_sz = __lhs.size();
3980 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
3981 __rhs.data(),
3982 __lhs_sz) == 0;
3983}
3984
3985template<class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003986inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaaeb1132013-04-22 23:55:13 +00003987bool
3988operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
3989 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
3990{
3991 size_t __lhs_sz = __lhs.size();
3992 if (__lhs_sz != __rhs.size())
3993 return false;
3994 const char* __lp = __lhs.data();
3995 const char* __rp = __rhs.data();
3996 if (__lhs.__is_long())
3997 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
3998 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
3999 if (*__lp != *__rp)
4000 return false;
4001 return true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004002}
4003
4004template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004005inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004006bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004007operator==(const _CharT* __lhs,
4008 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004009{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004010 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4011 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
4012 size_t __lhs_len = _Traits::length(__lhs);
4013 if (__lhs_len != __rhs.size()) return false;
4014 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004015}
4016
Howard Hinnantc51e1022010-05-11 19:42:16 +00004017template<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 basic_string<_CharT,_Traits,_Allocator>& __lhs,
4021 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004022{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004023 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4024 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
4025 size_t __rhs_len = _Traits::length(__rhs);
4026 if (__rhs_len != __lhs.size()) return false;
4027 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004028}
4029
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004030template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004031inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004032bool
4033operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004034 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004035{
4036 return !(__lhs == __rhs);
4037}
4038
4039template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004040inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004041bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004042operator!=(const _CharT* __lhs,
4043 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004044{
4045 return !(__lhs == __rhs);
4046}
4047
4048template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004049inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004050bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004051operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4052 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004053{
4054 return !(__lhs == __rhs);
4055}
4056
4057// operator<
4058
4059template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004060inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004061bool
4062operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004063 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004064{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004065 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004066}
4067
4068template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004069inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004070bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004071operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4072 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004073{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004074 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004075}
4076
4077template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004078inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004079bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004080operator< (const _CharT* __lhs,
4081 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004082{
4083 return __rhs.compare(__lhs) > 0;
4084}
4085
Howard Hinnantc51e1022010-05-11 19:42:16 +00004086// operator>
4087
4088template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004089inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004090bool
4091operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004092 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004093{
4094 return __rhs < __lhs;
4095}
4096
4097template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004098inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004099bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004100operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4101 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004102{
4103 return __rhs < __lhs;
4104}
4105
4106template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004107inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004108bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004109operator> (const _CharT* __lhs,
4110 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004111{
4112 return __rhs < __lhs;
4113}
4114
4115// operator<=
4116
4117template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004118inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004119bool
4120operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004121 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004122{
4123 return !(__rhs < __lhs);
4124}
4125
4126template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004127inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004128bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004129operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4130 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004131{
4132 return !(__rhs < __lhs);
4133}
4134
4135template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004136inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004137bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004138operator<=(const _CharT* __lhs,
4139 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004140{
4141 return !(__rhs < __lhs);
4142}
4143
4144// operator>=
4145
4146template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004147inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004148bool
4149operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004150 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004151{
4152 return !(__lhs < __rhs);
4153}
4154
4155template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004156inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004157bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004158operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4159 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004160{
4161 return !(__lhs < __rhs);
4162}
4163
4164template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004165inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004166bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004167operator>=(const _CharT* __lhs,
4168 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004169{
4170 return !(__lhs < __rhs);
4171}
4172
4173// operator +
4174
4175template<class _CharT, class _Traits, class _Allocator>
4176basic_string<_CharT, _Traits, _Allocator>
4177operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4178 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4179{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004180 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004181 auto __lhs_sz = __lhs.size();
4182 auto __rhs_sz = __rhs.size();
4183 _String __r(__uninitialized_size_tag(),
4184 __lhs_sz + __rhs_sz,
4185 _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4186 auto __ptr = std::__to_address(__r.__get_pointer());
4187 _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
4188 _Traits::copy(__ptr + __lhs_sz, __rhs.data(), __rhs_sz);
4189 _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004190 return __r;
4191}
4192
4193template<class _CharT, class _Traits, class _Allocator>
4194basic_string<_CharT, _Traits, _Allocator>
4195operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4196{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004197 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004198 auto __lhs_sz = _Traits::length(__lhs);
4199 auto __rhs_sz = __rhs.size();
4200 _String __r(__uninitialized_size_tag(),
4201 __lhs_sz + __rhs_sz,
4202 _String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator()));
4203 auto __ptr = std::__to_address(__r.__get_pointer());
4204 _Traits::copy(__ptr, __lhs, __lhs_sz);
4205 _Traits::copy(__ptr + __lhs_sz, __rhs.data(), __rhs_sz);
4206 _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004207 return __r;
4208}
4209
4210template<class _CharT, class _Traits, class _Allocator>
4211basic_string<_CharT, _Traits, _Allocator>
4212operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4213{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004214 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004215 typename _String::size_type __rhs_sz = __rhs.size();
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004216 _String __r(__uninitialized_size_tag(),
4217 __rhs_sz + 1,
4218 _String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator()));
4219 auto __ptr = std::__to_address(__r.__get_pointer());
4220 _Traits::assign(__ptr, 1, __lhs);
4221 _Traits::copy(__ptr + 1, __rhs.data(), __rhs_sz);
4222 _Traits::assign(__ptr + 1 + __rhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004223 return __r;
4224}
4225
4226template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004227inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004228basic_string<_CharT, _Traits, _Allocator>
4229operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
4230{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004231 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004232 typename _String::size_type __lhs_sz = __lhs.size();
4233 typename _String::size_type __rhs_sz = _Traits::length(__rhs);
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004234 _String __r(__uninitialized_size_tag(),
4235 __lhs_sz + __rhs_sz,
4236 _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4237 auto __ptr = std::__to_address(__r.__get_pointer());
4238 _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
4239 _Traits::copy(__ptr + __lhs_sz, __rhs, __rhs_sz);
4240 _Traits::assign(__ptr + __lhs_sz + __rhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004241 return __r;
4242}
4243
4244template<class _CharT, class _Traits, class _Allocator>
4245basic_string<_CharT, _Traits, _Allocator>
4246operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
4247{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004248 using _String = basic_string<_CharT, _Traits, _Allocator>;
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004249 typename _String::size_type __lhs_sz = __lhs.size();
Nikolas Klauser55dc8082022-04-09 16:19:45 +02004250 _String __r(__uninitialized_size_tag(),
4251 __lhs_sz + 1,
4252 _String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4253 auto __ptr = std::__to_address(__r.__get_pointer());
4254 _Traits::copy(__ptr, __lhs.data(), __lhs_sz);
4255 _Traits::assign(__ptr + __lhs_sz, 1, __rhs);
4256 _Traits::assign(__ptr + 1 + __lhs_sz, 1, _CharT());
Howard Hinnantc51e1022010-05-11 19:42:16 +00004257 return __r;
4258}
4259
Eric Fiselierfc92be82017-04-19 00:28:44 +00004260#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004261
4262template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004263inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004264basic_string<_CharT, _Traits, _Allocator>
4265operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4266{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004267 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004268}
4269
4270template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004271inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004272basic_string<_CharT, _Traits, _Allocator>
4273operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4274{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004275 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004276}
4277
4278template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004279inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004280basic_string<_CharT, _Traits, _Allocator>
4281operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4282{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004283 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004284}
4285
4286template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004287inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004288basic_string<_CharT, _Traits, _Allocator>
4289operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4290{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004291 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004292}
4293
4294template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004295inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004296basic_string<_CharT, _Traits, _Allocator>
4297operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4298{
4299 __rhs.insert(__rhs.begin(), __lhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004300 return _VSTD::move(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004301}
4302
4303template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004304inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004305basic_string<_CharT, _Traits, _Allocator>
4306operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
4307{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004308 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004309}
4310
4311template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004312inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004313basic_string<_CharT, _Traits, _Allocator>
4314operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
4315{
4316 __lhs.push_back(__rhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004317 return _VSTD::move(__lhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004318}
4319
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004320#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004321
4322// swap
4323
4324template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004325inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004326void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004327swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +00004328 basic_string<_CharT, _Traits, _Allocator>& __rhs)
4329 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004330{
4331 __lhs.swap(__rhs);
4332}
4333
Bruce Mitchener170d8972020-11-24 12:53:53 -05004334_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = nullptr, int __base = 10);
4335_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = nullptr, int __base = 10);
4336_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = nullptr, int __base = 10);
4337_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = nullptr, int __base = 10);
4338_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004339
Bruce Mitchener170d8972020-11-24 12:53:53 -05004340_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = nullptr);
4341_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = nullptr);
4342_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004343
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004344_LIBCPP_FUNC_VIS string to_string(int __val);
4345_LIBCPP_FUNC_VIS string to_string(unsigned __val);
4346_LIBCPP_FUNC_VIS string to_string(long __val);
4347_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
4348_LIBCPP_FUNC_VIS string to_string(long long __val);
4349_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
4350_LIBCPP_FUNC_VIS string to_string(float __val);
4351_LIBCPP_FUNC_VIS string to_string(double __val);
4352_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004353
Louis Dionne89258142021-08-23 15:32:36 -04004354#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Bruce Mitchener170d8972020-11-24 12:53:53 -05004355_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4356_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4357_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4358_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4359_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004360
Bruce Mitchener170d8972020-11-24 12:53:53 -05004361_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = nullptr);
4362_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = nullptr);
4363_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004364
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004365_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
4366_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
4367_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
4368_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
4369_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
4370_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
4371_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
4372_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
4373_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Louis Dionne89258142021-08-23 15:32:36 -04004374#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004375
Howard Hinnantc51e1022010-05-11 19:42:16 +00004376template<class _CharT, class _Traits, class _Allocator>
Martin Storsjö88890c22021-02-22 01:13:13 +02004377_LIBCPP_TEMPLATE_DATA_VIS
Eric Fiselierc9fdaf12020-01-15 17:02:17 -05004378const typename basic_string<_CharT, _Traits, _Allocator>::size_type
4379 basic_string<_CharT, _Traits, _Allocator>::npos;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004380
Marshall Clow851b9ec2019-05-20 21:56:51 +00004381template <class _CharT, class _Allocator>
4382struct _LIBCPP_TEMPLATE_VIS
4383 hash<basic_string<_CharT, char_traits<_CharT>, _Allocator> >
4384 : public unary_function<
4385 basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004386{
4387 size_t
Marshall Clow851b9ec2019-05-20 21:56:51 +00004388 operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT
4389 { return __do_string_hash(__val.data(), __val.data() + __val.size()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004390};
4391
Howard Hinnantc51e1022010-05-11 19:42:16 +00004392
Howard Hinnantdc095972011-07-18 15:51:59 +00004393template<class _CharT, class _Traits, class _Allocator>
4394basic_ostream<_CharT, _Traits>&
4395operator<<(basic_ostream<_CharT, _Traits>& __os,
4396 const basic_string<_CharT, _Traits, _Allocator>& __str);
4397
4398template<class _CharT, class _Traits, class _Allocator>
4399basic_istream<_CharT, _Traits>&
4400operator>>(basic_istream<_CharT, _Traits>& __is,
4401 basic_string<_CharT, _Traits, _Allocator>& __str);
4402
4403template<class _CharT, class _Traits, class _Allocator>
4404basic_istream<_CharT, _Traits>&
4405getline(basic_istream<_CharT, _Traits>& __is,
4406 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4407
4408template<class _CharT, class _Traits, class _Allocator>
4409inline _LIBCPP_INLINE_VISIBILITY
4410basic_istream<_CharT, _Traits>&
4411getline(basic_istream<_CharT, _Traits>& __is,
4412 basic_string<_CharT, _Traits, _Allocator>& __str);
4413
Howard Hinnantdc095972011-07-18 15:51:59 +00004414template<class _CharT, class _Traits, class _Allocator>
4415inline _LIBCPP_INLINE_VISIBILITY
4416basic_istream<_CharT, _Traits>&
4417getline(basic_istream<_CharT, _Traits>&& __is,
4418 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4419
4420template<class _CharT, class _Traits, class _Allocator>
4421inline _LIBCPP_INLINE_VISIBILITY
4422basic_istream<_CharT, _Traits>&
4423getline(basic_istream<_CharT, _Traits>&& __is,
4424 basic_string<_CharT, _Traits, _Allocator>& __str);
4425
Marshall Clow29b53f22018-12-14 18:49:35 +00004426#if _LIBCPP_STD_VER > 17
Marek Kurdeja98b1412020-05-02 13:58:03 +02004427template <class _CharT, class _Traits, class _Allocator, class _Up>
Marshall Clow29b53f22018-12-14 18:49:35 +00004428inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004429 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4430 erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
4431 auto __old_size = __str.size();
4432 __str.erase(_VSTD::remove(__str.begin(), __str.end(), __v), __str.end());
4433 return __old_size - __str.size();
4434}
Marshall Clow29b53f22018-12-14 18:49:35 +00004435
Marek Kurdeja98b1412020-05-02 13:58:03 +02004436template <class _CharT, class _Traits, class _Allocator, class _Predicate>
Marshall Clow29b53f22018-12-14 18:49:35 +00004437inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004438 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4439 erase_if(basic_string<_CharT, _Traits, _Allocator>& __str,
4440 _Predicate __pred) {
4441 auto __old_size = __str.size();
4442 __str.erase(_VSTD::remove_if(__str.begin(), __str.end(), __pred),
4443 __str.end());
4444 return __old_size - __str.size();
4445}
Marshall Clow29b53f22018-12-14 18:49:35 +00004446#endif
4447
Louis Dionneba400782020-10-02 15:02:52 -04004448#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004449
4450template<class _CharT, class _Traits, class _Allocator>
4451bool
4452basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4453{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004454 return data() <= _VSTD::__to_address(__i->base()) &&
4455 _VSTD::__to_address(__i->base()) < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004456}
4457
4458template<class _CharT, class _Traits, class _Allocator>
4459bool
4460basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4461{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004462 return data() < _VSTD::__to_address(__i->base()) &&
4463 _VSTD::__to_address(__i->base()) <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004464}
4465
4466template<class _CharT, class _Traits, class _Allocator>
4467bool
4468basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4469{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004470 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004471 return data() <= __p && __p <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004472}
4473
4474template<class _CharT, class _Traits, class _Allocator>
4475bool
4476basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4477{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004478 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004479 return data() <= __p && __p < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004480}
4481
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004482#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004483
Louis Dionne173f29e2019-05-29 16:01:36 +00004484#if _LIBCPP_STD_VER > 11
Marshall Clowcba751f2013-07-23 17:05:24 +00004485// Literal suffixes for basic_string [basic.string.literals]
Marshall Clowac868372013-10-05 21:18:32 +00004486inline namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004487{
4488 inline namespace string_literals
4489 {
Howard Hinnant5c167562013-08-07 19:39:48 +00004490 inline _LIBCPP_INLINE_VISIBILITY
4491 basic_string<char> operator "" s( const char *__str, size_t __len )
4492 {
4493 return basic_string<char> (__str, __len);
4494 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004495
Louis Dionne89258142021-08-23 15:32:36 -04004496#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnant5c167562013-08-07 19:39:48 +00004497 inline _LIBCPP_INLINE_VISIBILITY
4498 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4499 {
4500 return basic_string<wchar_t> (__str, __len);
4501 }
Louis Dionne89258142021-08-23 15:32:36 -04004502#endif
Marshall Clowcba751f2013-07-23 17:05:24 +00004503
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -04004504#ifndef _LIBCPP_HAS_NO_CHAR8_T
Marshall Clow8732fed2018-12-11 04:35:44 +00004505 inline _LIBCPP_INLINE_VISIBILITY
4506 basic_string<char8_t> operator "" s(const char8_t *__str, size_t __len) _NOEXCEPT
4507 {
4508 return basic_string<char8_t> (__str, __len);
4509 }
4510#endif
4511
Howard Hinnant5c167562013-08-07 19:39:48 +00004512 inline _LIBCPP_INLINE_VISIBILITY
4513 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4514 {
4515 return basic_string<char16_t> (__str, __len);
4516 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004517
Howard Hinnant5c167562013-08-07 19:39:48 +00004518 inline _LIBCPP_INLINE_VISIBILITY
4519 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4520 {
4521 return basic_string<char32_t> (__str, __len);
4522 }
Nikolas Klauserd26407a2021-12-02 14:12:51 +01004523 } // namespace string_literals
4524} // namespace literals
Mark de Weverdf3e0d42021-09-26 15:47:42 +02004525
4526#if _LIBCPP_STD_VER > 17
4527template <>
4528inline constexpr bool __format::__enable_insertable<std::basic_string<char>> = true;
4529#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
4530template <>
4531inline constexpr bool __format::__enable_insertable<std::basic_string<wchar_t>> = true;
4532#endif
4533#endif
4534
Marshall Clowcba751f2013-07-23 17:05:24 +00004535#endif
4536
Howard Hinnantc51e1022010-05-11 19:42:16 +00004537_LIBCPP_END_NAMESPACE_STD
4538
Eric Fiselierf4433a32017-05-31 22:07:49 +00004539_LIBCPP_POP_MACROS
4540
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004541#endif // _LIBCPP_STRING