blob: 9fd19d2ad9570fcd2e610edef51011a704fbdd18 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
Louis Dionne9bd93882021-11-17 16:25:01 -05002//===----------------------------------------------------------------------===//
Howard Hinnantc51e1022010-05-11 19:42:16 +00003//
Chandler Carruth7642bb12019-01-19 08:50:56 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_STRING
11#define _LIBCPP_STRING
12
13/*
14 string synopsis
15
16namespace std
17{
18
19template <class stateT>
20class fpos
21{
22private:
23 stateT st;
24public:
25 fpos(streamoff = streamoff());
26
27 operator streamoff() const;
28
29 stateT state() const;
30 void state(stateT);
31
32 fpos& operator+=(streamoff);
33 fpos operator+ (streamoff) const;
34 fpos& operator-=(streamoff);
35 fpos operator- (streamoff) const;
36};
37
38template <class stateT> streamoff operator-(const fpos<stateT>& x, const fpos<stateT>& y);
39
40template <class stateT> bool operator==(const fpos<stateT>& x, const fpos<stateT>& y);
41template <class stateT> bool operator!=(const fpos<stateT>& x, const fpos<stateT>& y);
42
43template <class charT>
44struct char_traits
45{
46 typedef charT char_type;
47 typedef ... int_type;
48 typedef streamoff off_type;
49 typedef streampos pos_type;
50 typedef mbstate_t state_type;
51
Howard Hinnantaeb17d82011-05-29 19:57:12 +000052 static void assign(char_type& c1, const char_type& c2) noexcept;
Howard Hinnant270c00e2012-07-20 19:09:12 +000053 static constexpr bool eq(char_type c1, char_type c2) noexcept;
54 static constexpr bool lt(char_type c1, char_type c2) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000055
56 static int compare(const char_type* s1, const char_type* s2, size_t n);
57 static size_t length(const char_type* s);
58 static const char_type* find(const char_type* s, size_t n, const char_type& a);
59 static char_type* move(char_type* s1, const char_type* s2, size_t n);
60 static char_type* copy(char_type* s1, const char_type* s2, size_t n);
61 static char_type* assign(char_type* s, size_t n, char_type a);
62
Howard Hinnant270c00e2012-07-20 19:09:12 +000063 static constexpr int_type not_eof(int_type c) noexcept;
64 static constexpr char_type to_char_type(int_type c) noexcept;
65 static constexpr int_type to_int_type(char_type c) noexcept;
66 static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept;
67 static constexpr int_type eof() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000068};
69
70template <> struct char_traits<char>;
71template <> struct char_traits<wchar_t>;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +010072template <> struct char_traits<char8_t>; // C++20
73template <> struct char_traits<char16_t>;
74template <> struct char_traits<char32_t>;
Howard Hinnantc51e1022010-05-11 19:42:16 +000075
Howard Hinnant3b6579a2010-08-22 00:02:43 +000076template<class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
Howard Hinnantc51e1022010-05-11 19:42:16 +000077class basic_string
78{
Howard Hinnant3b6579a2010-08-22 00:02:43 +000079public:
80// types:
Howard Hinnantc51e1022010-05-11 19:42:16 +000081 typedef traits traits_type;
82 typedef typename traits_type::char_type value_type;
83 typedef Allocator allocator_type;
84 typedef typename allocator_type::size_type size_type;
85 typedef typename allocator_type::difference_type difference_type;
86 typedef typename allocator_type::reference reference;
87 typedef typename allocator_type::const_reference const_reference;
88 typedef typename allocator_type::pointer pointer;
89 typedef typename allocator_type::const_pointer const_pointer;
90 typedef implementation-defined iterator;
91 typedef implementation-defined const_iterator;
92 typedef std::reverse_iterator<iterator> reverse_iterator;
93 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
94
95 static const size_type npos = -1;
96
Howard Hinnant3e276872011-06-03 18:40:47 +000097 basic_string()
98 noexcept(is_nothrow_default_constructible<allocator_type>::value);
99 explicit basic_string(const allocator_type& a);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000100 basic_string(const basic_string& str);
Howard Hinnant3e276872011-06-03 18:40:47 +0000101 basic_string(basic_string&& str)
102 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clow83445802016-04-07 18:13:41 +0000103 basic_string(const basic_string& str, size_type pos,
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000104 const allocator_type& a = allocator_type());
Marshall Clow83445802016-04-07 18:13:41 +0000105 basic_string(const basic_string& str, size_type pos, size_type n,
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000106 const Allocator& a = Allocator());
Marshall Clow78dbe462016-11-14 18:22:19 +0000107 template<class T>
108 basic_string(const T& t, size_type pos, size_type n, const Allocator& a = Allocator()); // C++17
Marshall Clowe46031a2018-07-02 18:41:15 +0000109 template <class T>
110 explicit basic_string(const T& t, const Allocator& a = Allocator()); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000111 basic_string(const value_type* s, const allocator_type& a = allocator_type());
112 basic_string(const value_type* s, size_type n, const allocator_type& a = allocator_type());
Marek Kurdej90d79712021-07-27 16:16:21 +0200113 basic_string(nullptr_t) = delete; // C++2b
Howard Hinnantc51e1022010-05-11 19:42:16 +0000114 basic_string(size_type n, value_type c, const allocator_type& a = allocator_type());
115 template<class InputIterator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000116 basic_string(InputIterator begin, InputIterator end,
117 const allocator_type& a = allocator_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000118 basic_string(initializer_list<value_type>, const Allocator& = Allocator());
119 basic_string(const basic_string&, const Allocator&);
120 basic_string(basic_string&&, const Allocator&);
121
122 ~basic_string();
123
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000124 operator basic_string_view<charT, traits>() const noexcept;
125
Howard Hinnantc51e1022010-05-11 19:42:16 +0000126 basic_string& operator=(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000127 template <class T>
128 basic_string& operator=(const T& t); // C++17
Howard Hinnant3e276872011-06-03 18:40:47 +0000129 basic_string& operator=(basic_string&& str)
130 noexcept(
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000131 allocator_type::propagate_on_container_move_assignment::value ||
132 allocator_type::is_always_equal::value ); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000133 basic_string& operator=(const value_type* s);
Marek Kurdej90d79712021-07-27 16:16:21 +0200134 basic_string& operator=(nullptr_t) = delete; // C++2b
Howard Hinnantc51e1022010-05-11 19:42:16 +0000135 basic_string& operator=(value_type c);
136 basic_string& operator=(initializer_list<value_type>);
137
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000138 iterator begin() noexcept;
139 const_iterator begin() const noexcept;
140 iterator end() noexcept;
141 const_iterator end() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000142
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000143 reverse_iterator rbegin() noexcept;
144 const_reverse_iterator rbegin() const noexcept;
145 reverse_iterator rend() noexcept;
146 const_reverse_iterator rend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000147
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000148 const_iterator cbegin() const noexcept;
149 const_iterator cend() const noexcept;
150 const_reverse_iterator crbegin() const noexcept;
151 const_reverse_iterator crend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000152
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000153 size_type size() const noexcept;
154 size_type length() const noexcept;
155 size_type max_size() const noexcept;
156 size_type capacity() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000157
158 void resize(size_type n, value_type c);
159 void resize(size_type n);
160
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100161 template<class Operation>
162 constexpr void resize_and_overwrite(size_type n, Operation op); // since C++23
163
Marek Kurdejc9848142020-11-26 10:07:16 +0100164 void reserve(size_type res_arg);
165 void reserve(); // deprecated in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000166 void shrink_to_fit();
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000167 void clear() noexcept;
168 bool empty() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000169
170 const_reference operator[](size_type pos) const;
171 reference operator[](size_type pos);
172
173 const_reference at(size_type n) const;
174 reference at(size_type n);
175
176 basic_string& operator+=(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000177 template <class T>
178 basic_string& operator+=(const T& t); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000179 basic_string& operator+=(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000180 basic_string& operator+=(value_type c);
181 basic_string& operator+=(initializer_list<value_type>);
182
183 basic_string& append(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000184 template <class T>
185 basic_string& append(const T& t); // C++17
Marshall Clow8db7fb02014-03-04 19:17:19 +0000186 basic_string& append(const basic_string& str, size_type pos, size_type n=npos); //C++14
Marshall Clow82513342016-09-24 22:45:42 +0000187 template <class T>
188 basic_string& append(const T& t, size_type pos, size_type n=npos); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000189 basic_string& append(const value_type* s, size_type n);
190 basic_string& append(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000191 basic_string& append(size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000192 template<class InputIterator>
193 basic_string& append(InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000194 basic_string& append(initializer_list<value_type>);
195
196 void push_back(value_type c);
197 void pop_back();
198 reference front();
199 const_reference front() const;
200 reference back();
201 const_reference back() const;
202
203 basic_string& assign(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000204 template <class T>
205 basic_string& assign(const T& t); // C++17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000206 basic_string& assign(basic_string&& str);
Marshall Clow8db7fb02014-03-04 19:17:19 +0000207 basic_string& assign(const basic_string& str, size_type pos, size_type n=npos); // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000208 template <class T>
209 basic_string& assign(const T& t, size_type pos, size_type n=npos); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000210 basic_string& assign(const value_type* s, size_type n);
211 basic_string& assign(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000212 basic_string& assign(size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000213 template<class InputIterator>
214 basic_string& assign(InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000215 basic_string& assign(initializer_list<value_type>);
216
217 basic_string& insert(size_type pos1, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000218 template <class T>
219 basic_string& insert(size_type pos1, const T& t);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000220 basic_string& insert(size_type pos1, const basic_string& str,
221 size_type pos2, size_type n);
Marshall Clow82513342016-09-24 22:45:42 +0000222 template <class T>
223 basic_string& insert(size_type pos1, const T& t, size_type pos2, size_type n); // C++17
Marshall Clow8db7fb02014-03-04 19:17:19 +0000224 basic_string& insert(size_type pos, const value_type* s, size_type n=npos); //C++14
Howard Hinnantd17880b2013-06-28 16:59:19 +0000225 basic_string& insert(size_type pos, const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000226 basic_string& insert(size_type pos, size_type n, value_type c);
227 iterator insert(const_iterator p, value_type c);
228 iterator insert(const_iterator p, size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000229 template<class InputIterator>
230 iterator insert(const_iterator p, InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000231 iterator insert(const_iterator p, initializer_list<value_type>);
232
233 basic_string& erase(size_type pos = 0, size_type n = npos);
234 iterator erase(const_iterator position);
235 iterator erase(const_iterator first, const_iterator last);
236
237 basic_string& replace(size_type pos1, size_type n1, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000238 template <class T>
239 basic_string& replace(size_type pos1, size_type n1, const T& t); // C++17
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000240 basic_string& replace(size_type pos1, size_type n1, const basic_string& str,
Marshall Clow8db7fb02014-03-04 19:17:19 +0000241 size_type pos2, size_type n2=npos); // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000242 template <class T>
243 basic_string& replace(size_type pos1, size_type n1, const T& t,
244 size_type pos2, size_type n); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000245 basic_string& replace(size_type pos, size_type n1, const value_type* s, size_type n2);
246 basic_string& replace(size_type pos, size_type n1, const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000247 basic_string& replace(size_type pos, size_type n1, size_type n2, value_type c);
Howard Hinnant990d6e82010-11-17 21:11:40 +0000248 basic_string& replace(const_iterator i1, const_iterator i2, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000249 template <class T>
250 basic_string& replace(const_iterator i1, const_iterator i2, const T& t); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000251 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s, size_type n);
252 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s);
Howard Hinnant990d6e82010-11-17 21:11:40 +0000253 basic_string& replace(const_iterator i1, const_iterator i2, size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000254 template<class InputIterator>
Howard Hinnant990d6e82010-11-17 21:11:40 +0000255 basic_string& replace(const_iterator i1, const_iterator i2, InputIterator j1, InputIterator j2);
256 basic_string& replace(const_iterator i1, const_iterator i2, initializer_list<value_type>);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000257
Howard Hinnantd17880b2013-06-28 16:59:19 +0000258 size_type copy(value_type* s, size_type n, size_type pos = 0) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000259 basic_string substr(size_type pos = 0, size_type n = npos) const;
260
Howard Hinnant3e276872011-06-03 18:40:47 +0000261 void swap(basic_string& str)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000262 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
263 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000264
Howard Hinnantd17880b2013-06-28 16:59:19 +0000265 const value_type* c_str() const noexcept;
266 const value_type* data() const noexcept;
Marshall Clowd3c22392016-03-08 15:44:30 +0000267 value_type* data() noexcept; // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000268
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000269 allocator_type get_allocator() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000270
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000271 size_type find(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000272 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800273 size_type find(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000274 size_type find(const value_type* s, size_type pos, size_type n) const noexcept;
275 size_type find(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000276 size_type find(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000277
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000278 size_type rfind(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000279 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800280 size_type rfind(const T& t, size_type pos = npos) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000281 size_type rfind(const value_type* s, size_type pos, size_type n) const noexcept;
282 size_type rfind(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000283 size_type rfind(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000284
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000285 size_type find_first_of(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000286 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800287 size_type find_first_of(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000288 size_type find_first_of(const value_type* s, size_type pos, size_type n) const noexcept;
289 size_type find_first_of(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000290 size_type find_first_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000291
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000292 size_type find_last_of(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000293 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800294 size_type find_last_of(const T& t, size_type pos = npos) const noexcept noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000295 size_type find_last_of(const value_type* s, size_type pos, size_type n) const noexcept;
296 size_type find_last_of(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000297 size_type find_last_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000298
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000299 size_type find_first_not_of(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000300 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800301 size_type find_first_not_of(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000302 size_type find_first_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
303 size_type find_first_not_of(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000304 size_type find_first_not_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000305
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000306 size_type find_last_not_of(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000307 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800308 size_type find_last_not_of(const T& t, size_type pos = npos) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000309 size_type find_last_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
310 size_type find_last_not_of(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000311 size_type find_last_not_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000312
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000313 int compare(const basic_string& str) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000314 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800315 int compare(const T& t) const noexcept; // C++17, noexcept as an extension
Howard Hinnantc51e1022010-05-11 19:42:16 +0000316 int compare(size_type pos1, size_type n1, const basic_string& str) const;
Marshall Clowe46031a2018-07-02 18:41:15 +0000317 template <class T>
318 int compare(size_type pos1, size_type n1, const T& t) const; // C++17
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000319 int compare(size_type pos1, size_type n1, const basic_string& str,
Marshall Clow8db7fb02014-03-04 19:17:19 +0000320 size_type pos2, size_type n2=npos) const; // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000321 template <class T>
322 int compare(size_type pos1, size_type n1, const T& t,
323 size_type pos2, size_type n2=npos) const; // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000324 int compare(const value_type* s) const noexcept;
325 int compare(size_type pos1, size_type n1, const value_type* s) const;
326 int compare(size_type pos1, size_type n1, const value_type* s, size_type n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000327
Marek Kurdej24b4c512021-01-07 12:29:04 +0100328 bool starts_with(basic_string_view<charT, traits> sv) const noexcept; // C++20
329 bool starts_with(charT c) const noexcept; // C++20
330 bool starts_with(const charT* s) const; // C++20
331 bool ends_with(basic_string_view<charT, traits> sv) const noexcept; // C++20
332 bool ends_with(charT c) const noexcept; // C++20
333 bool ends_with(const charT* s) const; // C++20
Marshall Clow18c293b2017-12-04 20:11:38 +0000334
Wim Leflere023c3542021-01-19 14:33:30 -0500335 constexpr bool contains(basic_string_view<charT, traits> sv) const noexcept; // C++2b
336 constexpr bool contains(charT c) const noexcept; // C++2b
337 constexpr bool contains(const charT* s) const; // C++2b
338
Howard Hinnantc51e1022010-05-11 19:42:16 +0000339 bool __invariants() const;
340};
341
Marshall Clowa0563332018-02-08 06:34:03 +0000342template<class InputIterator,
343 class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
344basic_string(InputIterator, InputIterator, Allocator = Allocator())
345 -> basic_string<typename iterator_traits<InputIterator>::value_type,
346 char_traits<typename iterator_traits<InputIterator>::value_type>,
347 Allocator>; // C++17
348
Howard Hinnantc51e1022010-05-11 19:42:16 +0000349template<class charT, class traits, class Allocator>
350basic_string<charT, traits, Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000351operator+(const basic_string<charT, traits, Allocator>& lhs,
352 const basic_string<charT, traits, Allocator>& rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000353
354template<class charT, class traits, class Allocator>
355basic_string<charT, traits, Allocator>
356operator+(const charT* lhs , const basic_string<charT,traits,Allocator>&rhs);
357
358template<class charT, class traits, class Allocator>
359basic_string<charT, traits, Allocator>
360operator+(charT lhs, const basic_string<charT,traits,Allocator>& rhs);
361
362template<class charT, class traits, class Allocator>
363basic_string<charT, traits, Allocator>
364operator+(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs);
365
366template<class charT, class traits, class Allocator>
367basic_string<charT, traits, Allocator>
368operator+(const basic_string<charT, traits, Allocator>& lhs, charT rhs);
369
370template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000371bool operator==(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000372 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000373
374template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000375bool operator==(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000376
377template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000378bool operator==(const basic_string<charT,traits,Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000379
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000380template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000381bool operator!=(const basic_string<charT,traits,Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000382 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000383
384template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000385bool operator!=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000386
387template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000388bool operator!=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000389
390template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000391bool operator< (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000392 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000393
394template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000395bool operator< (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000396
397template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000398bool operator< (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000399
400template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000401bool operator> (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000402 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000403
404template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000405bool operator> (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000406
407template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000408bool operator> (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000409
410template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000411bool operator<=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000412 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000413
414template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000415bool operator<=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000416
417template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000418bool operator<=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000419
420template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000421bool operator>=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000422 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000423
424template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000425bool operator>=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000426
427template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000428bool operator>=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000429
430template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000431void swap(basic_string<charT, traits, Allocator>& lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +0000432 basic_string<charT, traits, Allocator>& rhs)
433 noexcept(noexcept(lhs.swap(rhs)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000434
435template<class charT, class traits, class Allocator>
436basic_istream<charT, traits>&
437operator>>(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
438
439template<class charT, class traits, class Allocator>
440basic_ostream<charT, traits>&
441operator<<(basic_ostream<charT, traits>& os, const basic_string<charT, traits, Allocator>& str);
442
443template<class charT, class traits, class Allocator>
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000444basic_istream<charT, traits>&
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000445getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str,
446 charT delim);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000447
448template<class charT, class traits, class Allocator>
449basic_istream<charT, traits>&
450getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
451
Marshall Clow29b53f22018-12-14 18:49:35 +0000452template<class charT, class traits, class Allocator, class U>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200453typename basic_string<charT, traits, Allocator>::size_type
454erase(basic_string<charT, traits, Allocator>& c, const U& value); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000455template<class charT, class traits, class Allocator, class Predicate>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200456typename basic_string<charT, traits, Allocator>::size_type
457erase_if(basic_string<charT, traits, Allocator>& c, Predicate pred); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000458
Howard Hinnantc51e1022010-05-11 19:42:16 +0000459typedef basic_string<char> string;
460typedef basic_string<wchar_t> wstring;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100461typedef basic_string<char8_t> u8string; // C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000462typedef basic_string<char16_t> u16string;
463typedef basic_string<char32_t> u32string;
464
Bruce Mitchener170d8972020-11-24 12:53:53 -0500465int stoi (const string& str, size_t* idx = nullptr, int base = 10);
466long stol (const string& str, size_t* idx = nullptr, int base = 10);
467unsigned long stoul (const string& str, size_t* idx = nullptr, int base = 10);
468long long stoll (const string& str, size_t* idx = nullptr, int base = 10);
469unsigned long long stoull(const string& str, size_t* idx = nullptr, int base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000470
Bruce Mitchener170d8972020-11-24 12:53:53 -0500471float stof (const string& str, size_t* idx = nullptr);
472double stod (const string& str, size_t* idx = nullptr);
473long double stold(const string& str, size_t* idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000474
475string to_string(int val);
476string to_string(unsigned val);
477string to_string(long val);
478string to_string(unsigned long val);
479string to_string(long long val);
480string to_string(unsigned long long val);
481string to_string(float val);
482string to_string(double val);
483string to_string(long double val);
484
Bruce Mitchener170d8972020-11-24 12:53:53 -0500485int stoi (const wstring& str, size_t* idx = nullptr, int base = 10);
486long stol (const wstring& str, size_t* idx = nullptr, int base = 10);
487unsigned long stoul (const wstring& str, size_t* idx = nullptr, int base = 10);
488long long stoll (const wstring& str, size_t* idx = nullptr, int base = 10);
489unsigned long long stoull(const wstring& str, size_t* idx = nullptr, int base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000490
Bruce Mitchener170d8972020-11-24 12:53:53 -0500491float stof (const wstring& str, size_t* idx = nullptr);
492double stod (const wstring& str, size_t* idx = nullptr);
493long double stold(const wstring& str, size_t* idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000494
495wstring to_wstring(int val);
496wstring to_wstring(unsigned val);
497wstring to_wstring(long val);
498wstring to_wstring(unsigned long val);
499wstring to_wstring(long long val);
500wstring to_wstring(unsigned long long val);
501wstring to_wstring(float val);
502wstring to_wstring(double val);
503wstring to_wstring(long double val);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000504
505template <> struct hash<string>;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100506template <> struct hash<u8string>; // C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000507template <> struct hash<u16string>;
508template <> struct hash<u32string>;
509template <> struct hash<wstring>;
510
Marshall Clowcba751f2013-07-23 17:05:24 +0000511basic_string<char> operator "" s( const char *str, size_t len ); // C++14
512basic_string<wchar_t> operator "" s( const wchar_t *str, size_t len ); // C++14
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100513basic_string<char8_t> operator "" s( const char8_t *str, size_t len ); // C++20
Marshall Clowcba751f2013-07-23 17:05:24 +0000514basic_string<char16_t> operator "" s( const char16_t *str, size_t len ); // C++14
515basic_string<char32_t> operator "" s( const char32_t *str, size_t len ); // C++14
516
Howard Hinnantc51e1022010-05-11 19:42:16 +0000517} // std
518
519*/
520
Nikolas Klauserf210d8a2022-02-15 18:18:08 +0100521#include <__algorithm/max.h>
522#include <__algorithm/min.h>
523#include <__algorithm/remove.h>
524#include <__algorithm/remove_if.h>
Louis Dionneb4fce352022-03-25 12:55:36 -0400525#include <__assert> // all public C++ headers provide the assertion handler
Howard Hinnantc51e1022010-05-11 19:42:16 +0000526#include <__config>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400527#include <__debug>
Mark de Weverdf3e0d42021-09-26 15:47:42 +0200528#include <__format/enable_insertable.h>
Nikolas Klauser80840272022-02-04 13:04:33 +0100529#include <__ios/fpos.h>
Louis Dionne77249522021-06-11 09:55:11 -0400530#include <__iterator/wrap_iter.h>
Nikolas Klauserc513eba2022-04-09 09:41:19 +0200531#include <__memory/allocate_at_least.h>
Nikolas Klauser8fccd622022-03-05 19:17:07 +0100532#include <__utility/auto_cast.h>
533#include <__utility/move.h>
534#include <__utility/swap.h>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400535#include <compare>
536#include <cstdio> // EOF
Louis Dionned24191c2021-08-19 12:39:16 -0400537#include <cstdlib>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400538#include <cstring>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400539#include <initializer_list>
540#include <iosfwd>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000541#include <iterator>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000542#include <memory>
543#include <stdexcept>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400544#include <string_view>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000545#include <type_traits>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000546#include <version>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000547
Nikolas Klauser4c1bf592022-02-11 19:15:18 +0100548// TODO: remove these headers
549#include <__functional/binary_function.h>
550#include <__functional/invoke.h>
551#include <__functional/operations.h>
552#include <__functional/reference_wrapper.h>
553#include <__functional/unary_function.h>
554#include <__functional/weak_result_type.h>
555#include <new>
556#include <typeinfo>
557
Louis Dionne89258142021-08-23 15:32:36 -0400558#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Nikolas Klauser80840272022-02-04 13:04:33 +0100559# include <cwchar>
Louis Dionne89258142021-08-23 15:32:36 -0400560#endif
561
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400562#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
Nikolas Klauser80840272022-02-04 13:04:33 +0100563# include <cstdint>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400564#endif
Eric Fiselier14b6de92014-08-10 23:53:08 +0000565
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000566#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Arthur O'Dwyer6eeaa002022-02-01 20:16:40 -0500567# pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000568#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000569
Eric Fiselierf4433a32017-05-31 22:07:49 +0000570_LIBCPP_PUSH_MACROS
571#include <__undef_macros>
572
573
Howard Hinnantc51e1022010-05-11 19:42:16 +0000574_LIBCPP_BEGIN_NAMESPACE_STD
575
Howard Hinnantc51e1022010-05-11 19:42:16 +0000576// basic_string
577
578template<class _CharT, class _Traits, class _Allocator>
579basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000580operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
581 const basic_string<_CharT, _Traits, _Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000582
583template<class _CharT, class _Traits, class _Allocator>
584basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000585operator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000586
587template<class _CharT, class _Traits, class _Allocator>
588basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000589operator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000590
591template<class _CharT, class _Traits, class _Allocator>
Louis Dionne8f8d95d2018-11-21 17:31:55 +0000592inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000593basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000594operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000595
596template<class _CharT, class _Traits, class _Allocator>
597basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000598operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000599
Shoaib Meenaiea363712017-07-29 02:54:41 +0000600_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&))
601
Marshall Clow039b2f02016-01-13 21:54:34 +0000602template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400603struct __string_is_trivial_iterator : public false_type {};
604
605template <class _Tp>
606struct __string_is_trivial_iterator<_Tp*>
607 : public is_arithmetic<_Tp> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000608
Louis Dionne173f29e2019-05-29 16:01:36 +0000609template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400610struct __string_is_trivial_iterator<__wrap_iter<_Iter> >
611 : public __string_is_trivial_iterator<_Iter> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000612
Marshall Clow82513342016-09-24 22:45:42 +0000613template <class _CharT, class _Traits, class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500614struct __can_be_converted_to_string_view : public _BoolConstant<
615 is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
616 !is_convertible<const _Tp&, const _CharT*>::value
617 > {};
Marshall Clow82513342016-09-24 22:45:42 +0000618
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400619#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800620typedef basic_string<char8_t> u8string;
621#endif
622
623#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
624typedef basic_string<char16_t> u16string;
625typedef basic_string<char32_t> u32string;
Louis Dionne96fc5f52021-09-09 11:25:10 -0400626#endif
Richard Smith256954d2020-11-11 17:12:18 -0800627
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200628struct __uninitialized_size_tag {};
629
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000630template<class _CharT, class _Traits, class _Allocator>
Richard Smith256954d2020-11-11 17:12:18 -0800631class
632 _LIBCPP_TEMPLATE_VIS
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400633#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800634 _LIBCPP_PREFERRED_NAME(u8string)
635#endif
636#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
637 _LIBCPP_PREFERRED_NAME(u16string)
638 _LIBCPP_PREFERRED_NAME(u32string)
639#endif
640 basic_string
Howard Hinnantc51e1022010-05-11 19:42:16 +0000641{
642public:
643 typedef basic_string __self;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000644 typedef basic_string_view<_CharT, _Traits> __self_view;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000645 typedef _Traits traits_type;
Marshall Clowa3a74e02017-03-15 18:41:11 +0000646 typedef _CharT value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000647 typedef _Allocator allocator_type;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000648 typedef allocator_traits<allocator_type> __alloc_traits;
649 typedef typename __alloc_traits::size_type size_type;
650 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant3e276872011-06-03 18:40:47 +0000651 typedef value_type& reference;
652 typedef const value_type& const_reference;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000653 typedef typename __alloc_traits::pointer pointer;
654 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000655
Marshall Clow79f33542018-03-21 00:36:05 +0000656 static_assert((!is_array<value_type>::value), "Character type of basic_string must not be an array");
657 static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string must be standard-layout");
658 static_assert(( is_trivial<value_type>::value), "Character type of basic_string must be trivial");
659 static_assert(( is_same<_CharT, typename traits_type::char_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000660 "traits_type::char_type must be the same type as CharT");
Marshall Clow79f33542018-03-21 00:36:05 +0000661 static_assert(( is_same<typename allocator_type::value_type, value_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000662 "Allocator::value_type must be same type as value_type");
Marshall Clowe46031a2018-07-02 18:41:15 +0000663
Howard Hinnantc51e1022010-05-11 19:42:16 +0000664 typedef __wrap_iter<pointer> iterator;
665 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000666 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
667 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000668
669private:
Howard Hinnant68bf1812013-04-30 21:44:48 +0000670
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000671#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000672
673 struct __long
674 {
675 pointer __data_;
676 size_type __size_;
677 size_type __cap_;
678 };
679
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000680#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000681 static const size_type __short_mask = 0x01;
682 static const size_type __long_mask = 0x1ul;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000683#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000684 static const size_type __short_mask = 0x80;
685 static const size_type __long_mask = ~(size_type(~0) >> 1);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400686#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +0000687
688 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
689 (sizeof(__long) - 1)/sizeof(value_type) : 2};
690
691 struct __short
692 {
693 value_type __data_[__min_cap];
Azat Khuzhin1995f722022-04-08 16:13:46 -0400694 unsigned char __padding[sizeof(value_type) - 1];
695 unsigned char __size_;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000696 };
697
698#else
699
Howard Hinnantc51e1022010-05-11 19:42:16 +0000700 struct __long
701 {
702 size_type __cap_;
703 size_type __size_;
704 pointer __data_;
705 };
706
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000707#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000708 static const size_type __short_mask = 0x80;
709 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000710#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000711 static const size_type __short_mask = 0x01;
712 static const size_type __long_mask = 0x1ul;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400713#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +0000714
Howard Hinnantc51e1022010-05-11 19:42:16 +0000715 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
716 (sizeof(__long) - 1)/sizeof(value_type) : 2};
717
718 struct __short
719 {
720 union
721 {
722 unsigned char __size_;
Howard Hinnant49e145e2012-10-30 19:06:59 +0000723 value_type __lx;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000724 };
725 value_type __data_[__min_cap];
726 };
727
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400728#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000729
Howard Hinnant8ea98242013-08-23 17:37:05 +0000730 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000731
Howard Hinnant8ea98242013-08-23 17:37:05 +0000732 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000733
734 struct __raw
735 {
736 size_type __words[__n_words];
737 };
738
739 struct __rep
740 {
741 union
742 {
743 __long __l;
744 __short __s;
745 __raw __r;
746 };
747 };
748
749 __compressed_pair<__rep, allocator_type> __r_;
750
Nikolas Klauser55dc8082022-04-09 16:19:45 +0200751 // Construct a string with the given allocator and enough storage to hold `__size` characters, but
752 // don't initialize the characters. The contents of the string, including the null terminator, must be
753 // initialized separately.
754 _LIBCPP_HIDE_FROM_ABI explicit basic_string(__uninitialized_size_tag, size_type __size, const allocator_type& __a)
755 : __r_(__default_init_tag(), __a) {
756 if (__size > max_size())
757 __throw_length_error();
758 if (__fits_in_sso(__size)) {
759 __zero();
760 __set_short_size(__size);
761 } else {
762 auto __capacity = __recommend(__size) + 1;
763 auto __allocation = __alloc_traits::allocate(__alloc(), __capacity);
764 __set_long_cap(__capacity);
765 __set_long_pointer(__allocation);
766 __set_long_size(__size);
767 }
768 std::__debug_db_insert_c(this);
769 }
770
Howard Hinnantc51e1022010-05-11 19:42:16 +0000771public:
Martin Storsjö88890c22021-02-22 01:13:13 +0200772 _LIBCPP_TEMPLATE_DATA_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000773 static const size_type npos = -1;
774
Howard Hinnant3e276872011-06-03 18:40:47 +0000775 _LIBCPP_INLINE_VISIBILITY basic_string()
776 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000777
778 _LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a)
779#if _LIBCPP_STD_VER <= 14
780 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
781#else
782 _NOEXCEPT;
783#endif
784
Howard Hinnantc51e1022010-05-11 19:42:16 +0000785 basic_string(const basic_string& __str);
786 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clowa80abc72015-06-03 19:56:43 +0000787
Eric Fiselierfc92be82017-04-19 00:28:44 +0000788#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000789 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000790 basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +0000791#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +0000792 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000793#else
794 _NOEXCEPT;
795#endif
796
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000797 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000798 basic_string(basic_string&& __str, const allocator_type& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400799#endif // _LIBCPP_CXX03_LANG
Marshall Clowe46031a2018-07-02 18:41:15 +0000800
Louis Dionne9ce598d2021-09-08 09:14:43 -0400801 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Eric Fiseliera8567862018-07-17 05:48:48 +0000802 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5169d1c2019-12-16 19:03:23 -0500803 basic_string(const _CharT* __s) : __r_(__default_init_tag(), __default_init_tag()) {
Eric Fiseliera8567862018-07-17 05:48:48 +0000804 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
805 __init(__s, traits_type::length(__s));
Nikolas Klauserf2807732022-01-11 00:33:35 +0100806 _VSTD::__debug_db_insert_c(this);
Eric Fiseliera8567862018-07-17 05:48:48 +0000807 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000808
Louis Dionne9ce598d2021-09-08 09:14:43 -0400809 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000810 _LIBCPP_INLINE_VISIBILITY
811 basic_string(const _CharT* __s, const _Allocator& __a);
812
Marek Kurdej90d79712021-07-27 16:16:21 +0200813#if _LIBCPP_STD_VER > 20
814 basic_string(nullptr_t) = delete;
815#endif
816
Howard Hinnantcf823322010-12-17 14:46:43 +0000817 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000818 basic_string(const _CharT* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +0000819 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000820 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000821 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000822 basic_string(size_type __n, _CharT __c);
Marshall Clowe46031a2018-07-02 18:41:15 +0000823
Louis Dionne9ce598d2021-09-08 09:14:43 -0400824 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000825 _LIBCPP_INLINE_VISIBILITY
826 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
827
Marshall Clow83445802016-04-07 18:13:41 +0000828 basic_string(const basic_string& __str, size_type __pos, size_type __n,
Eric Fiselier812882b2017-02-17 01:17:10 +0000829 const _Allocator& __a = _Allocator());
Marshall Clow83445802016-04-07 18:13:41 +0000830 _LIBCPP_INLINE_VISIBILITY
831 basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +0000832 const _Allocator& __a = _Allocator());
Marshall Clowe46031a2018-07-02 18:41:15 +0000833
Louis Dionne9ce598d2021-09-08 09:14:43 -0400834 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 +0000835 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselier812882b2017-02-17 01:17:10 +0000836 basic_string(const _Tp& __t, size_type __pos, size_type __n,
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500837 const allocator_type& __a = allocator_type());
Marshall Clowe46031a2018-07-02 18:41:15 +0000838
Louis Dionne9ce598d2021-09-08 09:14:43 -0400839 template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500840 !__is_same_uncvref<_Tp, basic_string>::value> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000841 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
842 explicit basic_string(const _Tp& __t);
843
Louis Dionne9ce598d2021-09-08 09:14:43 -0400844 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 +0000845 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
846 explicit basic_string(const _Tp& __t, const allocator_type& __a);
847
Louis Dionne9ce598d2021-09-08 09:14:43 -0400848 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000849 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000850 basic_string(_InputIterator __first, _InputIterator __last);
Louis Dionne9ce598d2021-09-08 09:14:43 -0400851 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000852 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000853 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000854#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000855 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000856 basic_string(initializer_list<_CharT> __il);
Howard Hinnantcf823322010-12-17 14:46:43 +0000857 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000858 basic_string(initializer_list<_CharT> __il, const _Allocator& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400859#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000860
Eric Fiselierd9a702a2016-10-31 03:42:50 +0000861 inline ~basic_string();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000862
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000863 _LIBCPP_INLINE_VISIBILITY
864 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
865
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000866 basic_string& operator=(const basic_string& __str);
Eric Fiselier5ec13b12017-01-23 21:24:58 +0000867
Louis Dionne9ce598d2021-09-08 09:14:43 -0400868 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 +0000869 basic_string& operator=(const _Tp& __t)
870 {__self_view __sv = __t; return assign(__sv);}
871
Eric Fiselierfc92be82017-04-19 00:28:44 +0000872#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000873 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000874 basic_string& operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000875 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiselierfc92be82017-04-19 00:28:44 +0000876 _LIBCPP_INLINE_VISIBILITY
877 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000878#endif
Howard Hinnantd17880b2013-06-28 16:59:19 +0000879 _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const value_type* __s) {return assign(__s);}
Marek Kurdej90d79712021-07-27 16:16:21 +0200880#if _LIBCPP_STD_VER > 20
881 basic_string& operator=(nullptr_t) = delete;
882#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000883 basic_string& operator=(value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000884
Louis Dionneba400782020-10-02 15:02:52 -0400885#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +0000886 _LIBCPP_INLINE_VISIBILITY
887 iterator begin() _NOEXCEPT
888 {return iterator(this, __get_pointer());}
889 _LIBCPP_INLINE_VISIBILITY
890 const_iterator begin() const _NOEXCEPT
891 {return const_iterator(this, __get_pointer());}
892 _LIBCPP_INLINE_VISIBILITY
893 iterator end() _NOEXCEPT
894 {return iterator(this, __get_pointer() + size());}
895 _LIBCPP_INLINE_VISIBILITY
896 const_iterator end() const _NOEXCEPT
897 {return const_iterator(this, __get_pointer() + size());}
898#else
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000899 _LIBCPP_INLINE_VISIBILITY
900 iterator begin() _NOEXCEPT
901 {return iterator(__get_pointer());}
902 _LIBCPP_INLINE_VISIBILITY
903 const_iterator begin() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000904 {return const_iterator(__get_pointer());}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000905 _LIBCPP_INLINE_VISIBILITY
906 iterator end() _NOEXCEPT
907 {return iterator(__get_pointer() + size());}
908 _LIBCPP_INLINE_VISIBILITY
909 const_iterator end() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000910 {return const_iterator(__get_pointer() + size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400911#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000912 _LIBCPP_INLINE_VISIBILITY
913 reverse_iterator rbegin() _NOEXCEPT
914 {return reverse_iterator(end());}
915 _LIBCPP_INLINE_VISIBILITY
916 const_reverse_iterator rbegin() const _NOEXCEPT
917 {return const_reverse_iterator(end());}
918 _LIBCPP_INLINE_VISIBILITY
919 reverse_iterator rend() _NOEXCEPT
920 {return reverse_iterator(begin());}
921 _LIBCPP_INLINE_VISIBILITY
922 const_reverse_iterator rend() const _NOEXCEPT
923 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000924
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000925 _LIBCPP_INLINE_VISIBILITY
926 const_iterator cbegin() const _NOEXCEPT
927 {return begin();}
928 _LIBCPP_INLINE_VISIBILITY
929 const_iterator cend() const _NOEXCEPT
930 {return end();}
931 _LIBCPP_INLINE_VISIBILITY
932 const_reverse_iterator crbegin() const _NOEXCEPT
933 {return rbegin();}
934 _LIBCPP_INLINE_VISIBILITY
935 const_reverse_iterator crend() const _NOEXCEPT
936 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000937
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000938 _LIBCPP_INLINE_VISIBILITY size_type size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000939 {return __is_long() ? __get_long_size() : __get_short_size();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000940 _LIBCPP_INLINE_VISIBILITY size_type length() const _NOEXCEPT {return size();}
941 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT;
942 _LIBCPP_INLINE_VISIBILITY size_type capacity() const _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +0000943 {return (__is_long() ? __get_long_cap()
944 : static_cast<size_type>(__min_cap)) - 1;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000945
946 void resize(size_type __n, value_type __c);
947 _LIBCPP_INLINE_VISIBILITY void resize(size_type __n) {resize(__n, value_type());}
948
Marek Kurdejc9848142020-11-26 10:07:16 +0100949 void reserve(size_type __requested_capacity);
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100950
951#if _LIBCPP_STD_VER > 20
952 template <class _Op>
953 _LIBCPP_HIDE_FROM_ABI constexpr
954 void resize_and_overwrite(size_type __n, _Op __op) {
955 __resize_default_init(__n);
Nikolas Klausera4a76a12022-01-20 13:53:59 +0100956 __erase_to_end(_VSTD::move(__op)(data(), _LIBCPP_AUTO_CAST(__n)));
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100957 }
958#endif
959
Eric Fiselier451d5582018-11-26 20:15:38 +0000960 _LIBCPP_INLINE_VISIBILITY void __resize_default_init(size_type __n);
961
Marek Kurdejc9848142020-11-26 10:07:16 +0100962 _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_INLINE_VISIBILITY
963 void reserve() _NOEXCEPT {shrink_to_fit();}
Marshall Clow6ae12a82018-11-28 18:18:34 +0000964 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejc9848142020-11-26 10:07:16 +0100965 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000966 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000967 void clear() _NOEXCEPT;
Marshall Clowb7db4972017-11-15 20:02:27 +0000968 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
969 bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000970
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000971 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __pos) const _NOEXCEPT;
972 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000973
974 const_reference at(size_type __n) const;
975 reference at(size_type __n);
976
977 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const basic_string& __str) {return append(__str);}
Marshall Clowe46031a2018-07-02 18:41:15 +0000978
979 template <class _Tp>
980 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -0400981 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +0000982 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500983 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
984 && !__is_same_uncvref<_Tp, basic_string >::value,
Marshall Clowe46031a2018-07-02 18:41:15 +0000985 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500986 >
Marshall Clowe46031a2018-07-02 18:41:15 +0000987 operator+=(const _Tp& __t) {__self_view __sv = __t; return append(__sv);}
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000988 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const value_type* __s) {return append(__s);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000989 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(value_type __c) {push_back(__c); return *this;}
Eric Fiselierfc92be82017-04-19 00:28:44 +0000990#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000991 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(initializer_list<value_type> __il) {return append(__il);}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400992#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000993
Howard Hinnantcf823322010-12-17 14:46:43 +0000994 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000995 basic_string& append(const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000996
997 template <class _Tp>
998 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -0400999 __enable_if_t<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001000 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1001 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001002 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001003 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001004 append(const _Tp& __t) { __self_view __sv = __t; return append(__sv.data(), __sv.size()); }
Marshall Clow8db7fb02014-03-04 19:17:19 +00001005 basic_string& append(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clowe46031a2018-07-02 18:41:15 +00001006
Marshall Clow62953962016-10-03 23:40:48 +00001007 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001008 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001009 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001010 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001011 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1012 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001013 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001014 >
Marshall Clow82513342016-09-24 22:45:42 +00001015 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001016 basic_string& append(const value_type* __s, size_type __n);
1017 basic_string& append(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001018 basic_string& append(size_type __n, value_type __c);
Eric Fiselier451d5582018-11-26 20:15:38 +00001019
1020 _LIBCPP_INLINE_VISIBILITY
1021 void __append_default_init(size_type __n);
1022
Howard Hinnantc51e1022010-05-11 19:42:16 +00001023 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001024 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001025 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001026 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001027 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001028 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001029 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001030 _LIBCPP_INLINE_VISIBILITY
1031 append(_InputIterator __first, _InputIterator __last) {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001032 const basic_string __temp(__first, __last, __alloc());
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001033 append(__temp.data(), __temp.size());
1034 return *this;
1035 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001036 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001037 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001038 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001039 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001040 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001041 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001042 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001043 _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001044 append(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001045
Eric Fiselierfc92be82017-04-19 00:28:44 +00001046#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001047 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001048 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001049#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001050
1051 void push_back(value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001052 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001053 void pop_back();
Marshall Clow05cf6692019-03-19 03:30:07 +00001054 _LIBCPP_INLINE_VISIBILITY reference front() _NOEXCEPT;
1055 _LIBCPP_INLINE_VISIBILITY const_reference front() const _NOEXCEPT;
1056 _LIBCPP_INLINE_VISIBILITY reference back() _NOEXCEPT;
1057 _LIBCPP_INLINE_VISIBILITY const_reference back() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001058
Marshall Clowe46031a2018-07-02 18:41:15 +00001059 template <class _Tp>
1060 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001061 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001062 <
1063 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1064 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001065 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001066 assign(const _Tp & __t) { __self_view __sv = __t; return assign(__sv.data(), __sv.size()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001067 _LIBCPP_INLINE_VISIBILITY
Marshall Clow95d5e9a2016-03-09 18:08:29 +00001068 basic_string& assign(const basic_string& __str) { return *this = __str; }
Eric Fiselierfc92be82017-04-19 00:28:44 +00001069#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001070 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001071 basic_string& assign(basic_string&& __str)
Marshall Clow5aa9e942015-10-05 16:17:34 +00001072 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001073 {*this = _VSTD::move(__str); return *this;}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001074#endif
Marshall Clow8db7fb02014-03-04 19:17:19 +00001075 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow62953962016-10-03 23:40:48 +00001076 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001077 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001078 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001079 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001080 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1081 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001082 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001083 >
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001084 assign(const _Tp & __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001085 basic_string& assign(const value_type* __s, size_type __n);
1086 basic_string& assign(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001087 basic_string& assign(size_type __n, value_type __c);
1088 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001089 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001090 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001091 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001092 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001093 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001094 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001095 assign(_InputIterator __first, _InputIterator __last);
1096 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001097 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001098 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001099 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001100 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001101 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001102 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001103 assign(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001104#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001105 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001106 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001107#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001108
Howard Hinnantcf823322010-12-17 14:46:43 +00001109 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001110 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001111
1112 template <class _Tp>
1113 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001114 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001115 <
1116 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1117 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001118 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001119 insert(size_type __pos1, const _Tp& __t)
1120 { __self_view __sv = __t; return insert(__pos1, __sv.data(), __sv.size()); }
1121
Marshall Clow82513342016-09-24 22:45:42 +00001122 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001123 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001124 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001125 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001126 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001127 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001128 >
Marshall Clow82513342016-09-24 22:45:42 +00001129 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Marshall Clow8db7fb02014-03-04 19:17:19 +00001130 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001131 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1132 basic_string& insert(size_type __pos, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001133 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1134 iterator insert(const_iterator __pos, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001135 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001136 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1137 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001138 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001139 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001140 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001141 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001142 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001143 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001144 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1145 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001146 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001147 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001148 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001149 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001150 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001151 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001152 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001153#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001154 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001155 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1156 {return insert(__pos, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001157#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001158
1159 basic_string& erase(size_type __pos = 0, size_type __n = npos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001160 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001161 iterator erase(const_iterator __pos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001162 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001163 iterator erase(const_iterator __first, const_iterator __last);
1164
Howard Hinnantcf823322010-12-17 14:46:43 +00001165 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001166 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001167
1168 template <class _Tp>
1169 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001170 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001171 <
1172 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1173 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001174 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001175 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 +00001176 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 +00001177 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001178 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001179 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001180 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001181 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001182 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001183 >
Marshall Clow82513342016-09-24 22:45:42 +00001184 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001185 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
1186 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001187 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001188 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001189 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001190
1191 template <class _Tp>
1192 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001193 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001194 <
1195 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1196 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001197 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001198 replace(const_iterator __i1, const_iterator __i2, const _Tp& __t) { __self_view __sv = __t; return replace(__i1 - begin(), __i2 - __i1, __sv); }
1199
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001200 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001201 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +00001202 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001203 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Howard Hinnantcf823322010-12-17 14:46:43 +00001204 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001205 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001206 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001207 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001208 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001209 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001210 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001211 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001212 >
Howard Hinnant990d6e82010-11-17 21:11:40 +00001213 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001214#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001215 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001216 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001217 {return replace(__i1, __i2, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001218#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001219
Howard Hinnantd17880b2013-06-28 16:59:19 +00001220 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
Howard Hinnantcf823322010-12-17 14:46:43 +00001221 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001222 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1223
Howard Hinnantcf823322010-12-17 14:46:43 +00001224 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001225 void swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001226#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00001227 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +00001228#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001229 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001230 __is_nothrow_swappable<allocator_type>::value);
1231#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001232
Howard Hinnantcf823322010-12-17 14:46:43 +00001233 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001234 const value_type* c_str() const _NOEXCEPT {return data();}
Howard Hinnantcf823322010-12-17 14:46:43 +00001235 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001236 const value_type* data() const _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Eric Fiselier4ee2a892017-11-20 20:23:27 +00001237#if _LIBCPP_STD_VER > 14 || defined(_LIBCPP_BUILDING_LIBRARY)
Marshall Clowd3c22392016-03-08 15:44:30 +00001238 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001239 value_type* data() _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Marshall Clowd3c22392016-03-08 15:44:30 +00001240#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001241
Howard Hinnantcf823322010-12-17 14:46:43 +00001242 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001243 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001244
Howard Hinnantcf823322010-12-17 14:46:43 +00001245 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001246 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001247
1248 template <class _Tp>
1249 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001250 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001251 <
1252 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1253 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001254 >
zoecarver1997e0a2021-02-05 11:54:47 -08001255 find(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001256 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001257 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001258 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001259 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
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 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001263
1264 template <class _Tp>
1265 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001266 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001267 <
1268 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1269 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001270 >
zoecarver1997e0a2021-02-05 11:54:47 -08001271 rfind(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001272 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001273 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001274 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001275 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001276
Howard Hinnantcf823322010-12-17 14:46:43 +00001277 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001278 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001279
1280 template <class _Tp>
1281 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001282 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001283 <
1284 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1285 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001286 >
zoecarver1997e0a2021-02-05 11:54:47 -08001287 find_first_of(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001288 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001289 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001290 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001291 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001292 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001293
Howard Hinnantcf823322010-12-17 14:46:43 +00001294 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001295 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001296
1297 template <class _Tp>
1298 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001299 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001300 <
1301 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1302 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001303 >
zoecarver1997e0a2021-02-05 11:54:47 -08001304 find_last_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001305 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001306 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001307 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001308 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001309 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001310
Howard Hinnantcf823322010-12-17 14:46:43 +00001311 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001312 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001313
1314 template <class _Tp>
1315 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001316 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001317 <
1318 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1319 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001320 >
zoecarver1997e0a2021-02-05 11:54:47 -08001321 find_first_not_of(const _Tp &__t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001322 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 +00001323 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001324 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001325 _LIBCPP_INLINE_VISIBILITY
1326 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1327
1328 _LIBCPP_INLINE_VISIBILITY
1329 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001330
1331 template <class _Tp>
1332 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001333 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001334 <
1335 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1336 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001337 >
zoecarver1997e0a2021-02-05 11:54:47 -08001338 find_last_not_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001339 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 +00001340 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001341 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001342 _LIBCPP_INLINE_VISIBILITY
1343 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1344
1345 _LIBCPP_INLINE_VISIBILITY
1346 int compare(const basic_string& __str) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001347
1348 template <class _Tp>
1349 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001350 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001351 <
1352 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1353 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001354 >
zoecarver1997e0a2021-02-05 11:54:47 -08001355 compare(const _Tp &__t) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001356
1357 template <class _Tp>
1358 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001359 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001360 <
1361 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1362 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001363 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001364 compare(size_type __pos1, size_type __n1, const _Tp& __t) const;
1365
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001366 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001367 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Marshall Clow8db7fb02014-03-04 19:17:19 +00001368 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 +00001369
Marshall Clow82513342016-09-24 22:45:42 +00001370 template <class _Tp>
Eric Fiselier7d4aa5c2016-10-14 05:29:46 +00001371 inline _LIBCPP_INLINE_VISIBILITY
Louis Dionne9ce598d2021-09-08 09:14:43 -04001372 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001373 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001374 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001375 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001376 >
Marshall Clow82513342016-09-24 22:45:42 +00001377 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 +00001378 int compare(const value_type* __s) const _NOEXCEPT;
1379 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1380 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001381
Marshall Clow18c293b2017-12-04 20:11:38 +00001382#if _LIBCPP_STD_VER > 17
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001383 constexpr _LIBCPP_INLINE_VISIBILITY
1384 bool starts_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001385 { return __self_view(data(), size()).starts_with(__sv); }
1386
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001387 constexpr _LIBCPP_INLINE_VISIBILITY
1388 bool starts_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001389 { return !empty() && _Traits::eq(front(), __c); }
1390
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001391 constexpr _LIBCPP_INLINE_VISIBILITY
1392 bool starts_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001393 { return starts_with(__self_view(__s)); }
1394
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001395 constexpr _LIBCPP_INLINE_VISIBILITY
1396 bool ends_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001397 { return __self_view(data(), size()).ends_with( __sv); }
1398
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001399 constexpr _LIBCPP_INLINE_VISIBILITY
1400 bool ends_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001401 { return !empty() && _Traits::eq(back(), __c); }
1402
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001403 constexpr _LIBCPP_INLINE_VISIBILITY
1404 bool ends_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001405 { return ends_with(__self_view(__s)); }
1406#endif
1407
Wim Leflere023c3542021-01-19 14:33:30 -05001408#if _LIBCPP_STD_VER > 20
1409 constexpr _LIBCPP_INLINE_VISIBILITY
1410 bool contains(__self_view __sv) const noexcept
1411 { return __self_view(data(), size()).contains(__sv); }
1412
1413 constexpr _LIBCPP_INLINE_VISIBILITY
1414 bool contains(value_type __c) const noexcept
1415 { return __self_view(data(), size()).contains(__c); }
1416
1417 constexpr _LIBCPP_INLINE_VISIBILITY
1418 bool contains(const value_type* __s) const
1419 { return __self_view(data(), size()).contains(__s); }
1420#endif
1421
Howard Hinnantcf823322010-12-17 14:46:43 +00001422 _LIBCPP_INLINE_VISIBILITY bool __invariants() const;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001423
Marshall Clowe60a7182018-05-29 17:04:37 +00001424 _LIBCPP_INLINE_VISIBILITY void __clear_and_shrink() _NOEXCEPT;
Marshall Clow851b9ec2019-05-20 21:56:51 +00001425
Marek Kurdejc9848142020-11-26 10:07:16 +01001426 _LIBCPP_INLINE_VISIBILITY void __shrink_or_extend(size_type __target_capacity);
1427
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001428 _LIBCPP_INLINE_VISIBILITY
1429 bool __is_long() const _NOEXCEPT
1430 {return bool(__r_.first().__s.__size_ & __short_mask);}
1431
Louis Dionneba400782020-10-02 15:02:52 -04001432#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001433
1434 bool __dereferenceable(const const_iterator* __i) const;
1435 bool __decrementable(const const_iterator* __i) const;
1436 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1437 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1438
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001439#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001440
Howard Hinnantc51e1022010-05-11 19:42:16 +00001441private:
Nikolas Klauser93826d12022-01-04 17:24:03 +01001442 _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI static bool __fits_in_sso(size_type __sz) {
1443 // SSO is disabled during constant evaluation because `__is_long` isn't constexpr friendly
1444 return !__libcpp_is_constant_evaluated() && (__sz < __min_cap);
1445 }
1446
Nikolas Klauser48d680d2022-02-26 13:28:33 +01001447 template <class _ForwardIterator>
1448 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
1449 iterator __insert_from_safe_copy(size_type __n, size_type __ip, _ForwardIterator __first, _ForwardIterator __last) {
1450 size_type __sz = size();
1451 size_type __cap = capacity();
1452 value_type* __p;
1453 if (__cap - __sz >= __n)
1454 {
1455 __p = std::__to_address(__get_pointer());
1456 size_type __n_move = __sz - __ip;
1457 if (__n_move != 0)
1458 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
1459 }
1460 else
1461 {
1462 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
1463 __p = std::__to_address(__get_long_pointer());
1464 }
1465 __sz += __n;
1466 __set_size(__sz);
1467 traits_type::assign(__p[__sz], value_type());
1468 for (__p += __ip; __first != __last; ++__p, ++__first)
1469 traits_type::assign(*__p, *__first);
1470
1471 return begin() + __ip;
1472 }
1473
1474 _LIBCPP_HIDE_FROM_ABI allocator_type& __alloc() _NOEXCEPT { return __r_.second(); }
1475 _LIBCPP_HIDE_FROM_ABI const allocator_type& __alloc() const _NOEXCEPT { return __r_.second(); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001476
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001477#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001478
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001479 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001480 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001481# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001482 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001483# else
1484 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1485# endif
1486
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001487 _LIBCPP_INLINE_VISIBILITY
1488 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001489# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001490 {return __r_.first().__s.__size_ >> 1;}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001491# else
1492 {return __r_.first().__s.__size_;}
1493# endif
1494
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001495#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001496
1497 _LIBCPP_INLINE_VISIBILITY
1498 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001499# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001500 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1501# else
1502 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
1503# endif
1504
1505 _LIBCPP_INLINE_VISIBILITY
1506 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001507# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001508 {return __r_.first().__s.__size_;}
1509# else
1510 {return __r_.first().__s.__size_ >> 1;}
1511# endif
1512
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001513#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001514
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001515 _LIBCPP_INLINE_VISIBILITY
1516 void __set_long_size(size_type __s) _NOEXCEPT
1517 {__r_.first().__l.__size_ = __s;}
1518 _LIBCPP_INLINE_VISIBILITY
1519 size_type __get_long_size() const _NOEXCEPT
1520 {return __r_.first().__l.__size_;}
1521 _LIBCPP_INLINE_VISIBILITY
1522 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001523 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1524
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001525 _LIBCPP_INLINE_VISIBILITY
1526 void __set_long_cap(size_type __s) _NOEXCEPT
1527 {__r_.first().__l.__cap_ = __long_mask | __s;}
1528 _LIBCPP_INLINE_VISIBILITY
1529 size_type __get_long_cap() const _NOEXCEPT
Howard Hinnant28b24882011-12-01 20:21:04 +00001530 {return __r_.first().__l.__cap_ & size_type(~__long_mask);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001531
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001532 _LIBCPP_INLINE_VISIBILITY
1533 void __set_long_pointer(pointer __p) _NOEXCEPT
1534 {__r_.first().__l.__data_ = __p;}
1535 _LIBCPP_INLINE_VISIBILITY
1536 pointer __get_long_pointer() _NOEXCEPT
1537 {return __r_.first().__l.__data_;}
1538 _LIBCPP_INLINE_VISIBILITY
1539 const_pointer __get_long_pointer() const _NOEXCEPT
1540 {return __r_.first().__l.__data_;}
1541 _LIBCPP_INLINE_VISIBILITY
1542 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001543 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001544 _LIBCPP_INLINE_VISIBILITY
1545 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001546 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001547 _LIBCPP_INLINE_VISIBILITY
1548 pointer __get_pointer() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001549 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001550 _LIBCPP_INLINE_VISIBILITY
1551 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001552 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1553
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001554 _LIBCPP_INLINE_VISIBILITY
1555 void __zero() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001556 {
1557 size_type (&__a)[__n_words] = __r_.first().__r.__words;
1558 for (unsigned __i = 0; __i < __n_words; ++__i)
1559 __a[__i] = 0;
1560 }
1561
1562 template <size_type __a> static
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001563 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00001564 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +00001565 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001566 enum {__alignment = 16};
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001567 static _LIBCPP_INLINE_VISIBILITY
1568 size_type __recommend(size_type __s) _NOEXCEPT
Marshall Clow80584522018-02-07 21:30:17 +00001569 {
1570 if (__s < __min_cap) return static_cast<size_type>(__min_cap) - 1;
1571 size_type __guess = __align_it<sizeof(value_type) < __alignment ?
1572 __alignment/sizeof(value_type) : 1 > (__s+1) - 1;
1573 if (__guess == __min_cap) ++__guess;
1574 return __guess;
1575 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001576
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001577 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001578 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001579 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001580 void __init(const value_type* __s, size_type __sz);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001581 inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001582 void __init(size_type __n, value_type __c);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001583
Martijn Vels5e7c9752020-03-04 17:52:46 -05001584 // Slow path for the (inlined) copy constructor for 'long' strings.
1585 // Always externally instantiated and not inlined.
1586 // Requires that __s is zero terminated.
1587 // The main reason for this function to exist is because for unstable, we
1588 // want to allow inlining of the copy constructor. However, we don't want
1589 // to call the __init() functions as those are marked as inline which may
1590 // result in over-aggressive inlining by the compiler, where our aim is
1591 // to only inline the fast path code directly in the ctor.
1592 void __init_copy_ctor_external(const value_type* __s, size_type __sz);
1593
Howard Hinnantc51e1022010-05-11 19:42:16 +00001594 template <class _InputIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001595 inline
Louis Dionne9ce598d2021-09-08 09:14:43 -04001596 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001597 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001598 __is_exactly_cpp17_input_iterator<_InputIterator>::value
1599 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001600 __init(_InputIterator __first, _InputIterator __last);
1601
1602 template <class _ForwardIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001603 inline
Louis Dionne9ce598d2021-09-08 09:14:43 -04001604 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001605 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001606 __is_cpp17_forward_iterator<_ForwardIterator>::value
1607 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001608 __init(_ForwardIterator __first, _ForwardIterator __last);
1609
1610 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001611 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001612 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1613 size_type __n_copy, size_type __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00001614 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001615
Martijn Vels596e3de2020-02-26 15:55:49 -05001616 // __assign_no_alias is invoked for assignment operations where we
1617 // have proof that the input does not alias the current instance.
1618 // For example, operator=(basic_string) performs a 'self' check.
1619 template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04001620 basic_string& __assign_no_alias(const value_type* __s, size_type __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05001621
Howard Hinnantcf823322010-12-17 14:46:43 +00001622 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001623 void __erase_to_end(size_type __pos);
1624
Martijn Velsa81fc792020-02-26 13:25:43 -05001625 // __erase_external_with_move is invoked for erase() invocations where
1626 // `n ~= npos`, likely requiring memory moves on the string data.
1627 void __erase_external_with_move(size_type __pos, size_type __n);
1628
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001629 _LIBCPP_INLINE_VISIBILITY
1630 void __copy_assign_alloc(const basic_string& __str)
1631 {__copy_assign_alloc(__str, integral_constant<bool,
1632 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1633
1634 _LIBCPP_INLINE_VISIBILITY
1635 void __copy_assign_alloc(const basic_string& __str, true_type)
1636 {
Marshall Clowf258c202017-01-31 03:40:52 +00001637 if (__alloc() == __str.__alloc())
1638 __alloc() = __str.__alloc();
1639 else
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001640 {
Marshall Clowf258c202017-01-31 03:40:52 +00001641 if (!__str.__is_long())
1642 {
Vedant Kumar55e007e2018-03-08 21:15:26 +00001643 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001644 __alloc() = __str.__alloc();
1645 }
1646 else
1647 {
1648 allocator_type __a = __str.__alloc();
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001649 auto __allocation = std::__allocate_at_least(__a, __str.__get_long_cap());
Vedant Kumar55e007e2018-03-08 21:15:26 +00001650 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001651 __alloc() = _VSTD::move(__a);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001652 __set_long_pointer(__allocation.ptr);
1653 __set_long_cap(__allocation.count);
Marshall Clowf258c202017-01-31 03:40:52 +00001654 __set_long_size(__str.size());
1655 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001656 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001657 }
1658
1659 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001660 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001661 {}
1662
Eric Fiselierfc92be82017-04-19 00:28:44 +00001663#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +00001664 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001665 void __move_assign(basic_string& __str, false_type)
1666 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnantcf823322010-12-17 14:46:43 +00001667 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001668 void __move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001669#if _LIBCPP_STD_VER > 14
1670 _NOEXCEPT;
1671#else
Howard Hinnant3e276872011-06-03 18:40:47 +00001672 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001673#endif
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001674#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001675
1676 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001677 void
Howard Hinnantc2734962011-09-02 20:42:31 +00001678 __move_assign_alloc(basic_string& __str)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001679 _NOEXCEPT_(
1680 !__alloc_traits::propagate_on_container_move_assignment::value ||
1681 is_nothrow_move_assignable<allocator_type>::value)
1682 {__move_assign_alloc(__str, integral_constant<bool,
1683 __alloc_traits::propagate_on_container_move_assignment::value>());}
1684
1685 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00001686 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001687 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1688 {
1689 __alloc() = _VSTD::move(__c.__alloc());
1690 }
1691
1692 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001693 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001694 _NOEXCEPT
1695 {}
1696
Martijn Velsda7d94f2020-06-19 14:24:03 -04001697 basic_string& __assign_external(const value_type* __s);
1698 basic_string& __assign_external(const value_type* __s, size_type __n);
1699
1700 // Assigns the value in __s, guaranteed to be __n < __min_cap in length.
1701 inline basic_string& __assign_short(const value_type* __s, size_type __n) {
1702 pointer __p = __is_long()
1703 ? (__set_long_size(__n), __get_long_pointer())
1704 : (__set_short_size(__n), __get_short_pointer());
1705 traits_type::move(_VSTD::__to_address(__p), __s, __n);
1706 traits_type::assign(__p[__n], value_type());
1707 return *this;
1708 }
1709
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01001710 _LIBCPP_HIDE_FROM_ABI basic_string& __null_terminate_at(value_type* __p, size_type __newsz) {
1711 __set_size(__newsz);
1712 __invalidate_iterators_past(__newsz);
1713 traits_type::assign(__p[__newsz], value_type());
1714 return *this;
1715 }
1716
Howard Hinnantcf823322010-12-17 14:46:43 +00001717 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
1718 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(size_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001719
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001720 template<class _Tp>
1721 _LIBCPP_INLINE_VISIBILITY
1722 bool __addr_in_range(_Tp&& __t) const {
1723 const volatile void *__p = _VSTD::addressof(__t);
1724 return data() <= __p && __p <= data() + size();
1725 }
1726
Louis Dionned24191c2021-08-19 12:39:16 -04001727 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1728 void __throw_length_error() const {
Nikolas Klauserf89bb932022-01-24 19:44:34 +01001729 _VSTD::__throw_length_error("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001730 }
1731
1732 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1733 void __throw_out_of_range() const {
Nikolas Klauserf89bb932022-01-24 19:44:34 +01001734 _VSTD::__throw_out_of_range("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001735 }
1736
Howard Hinnantc51e1022010-05-11 19:42:16 +00001737 friend basic_string operator+<>(const basic_string&, const basic_string&);
1738 friend basic_string operator+<>(const value_type*, const basic_string&);
1739 friend basic_string operator+<>(value_type, const basic_string&);
1740 friend basic_string operator+<>(const basic_string&, const value_type*);
1741 friend basic_string operator+<>(const basic_string&, value_type);
1742};
1743
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001744// These declarations must appear before any functions are implicitly used
1745// so that they have the correct visibility specifier.
1746#ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
Louis Dionne89258142021-08-23 15:32:36 -04001747 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1748# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1749 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1750# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001751#else
Louis Dionne89258142021-08-23 15:32:36 -04001752 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1753# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1754 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1755# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001756#endif
1757
1758
Louis Dionned59f8a52021-08-17 11:59:07 -04001759#if _LIBCPP_STD_VER >= 17
Marshall Clowa0563332018-02-08 06:34:03 +00001760template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -05001761 class _CharT = __iter_value_type<_InputIterator>,
Marshall Clowa0563332018-02-08 06:34:03 +00001762 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001763 class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
1764 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowa0563332018-02-08 06:34:03 +00001765 >
1766basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
1767 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
Marshall Clowe46031a2018-07-02 18:41:15 +00001768
1769template<class _CharT,
1770 class _Traits,
1771 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001772 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowe46031a2018-07-02 18:41:15 +00001773 >
1774explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
1775 -> basic_string<_CharT, _Traits, _Allocator>;
1776
1777template<class _CharT,
1778 class _Traits,
1779 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001780 class = enable_if_t<__is_allocator<_Allocator>::value>,
Marshall Clowe46031a2018-07-02 18:41:15 +00001781 class _Sz = typename allocator_traits<_Allocator>::size_type
1782 >
1783basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
1784 -> basic_string<_CharT, _Traits, _Allocator>;
Marshall Clowa0563332018-02-08 06:34:03 +00001785#endif
1786
Howard Hinnantc51e1022010-05-11 19:42:16 +00001787template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001788inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001789void
1790basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
1791{
Louis Dionneba400782020-10-02 15:02:52 -04001792#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001793 if (!__libcpp_is_constant_evaluated())
1794 __get_db()->__invalidate_all(this);
Louis Dionneba400782020-10-02 15:02:52 -04001795#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001796}
1797
1798template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001799inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001800void
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001801basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type __pos)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001802{
Louis Dionneba400782020-10-02 15:02:52 -04001803#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001804 if (!__libcpp_is_constant_evaluated()) {
1805 __c_node* __c = __get_db()->__find_c_and_lock(this);
1806 if (__c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001807 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001808 const_pointer __new_last = __get_pointer() + __pos;
1809 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001810 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001811 --__p;
1812 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1813 if (__i->base() > __new_last)
1814 {
1815 (*__p)->__c_ = nullptr;
1816 if (--__c->end_ != __p)
1817 _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1818 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001819 }
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001820 __get_db()->unlock();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001821 }
1822 }
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001823#else
1824 (void)__pos;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001825#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001826}
1827
1828template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001829inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001830basic_string<_CharT, _Traits, _Allocator>::basic_string()
Marshall Clowe546cbb2015-06-04 02:05:41 +00001831 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001832 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001833{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001834 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001835 __zero();
1836}
1837
1838template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001839inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001840basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
Eric Fiseliere012bf22015-07-18 20:40:46 +00001841#if _LIBCPP_STD_VER <= 14
1842 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1843#else
1844 _NOEXCEPT
1845#endif
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001846: __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001847{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001848 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001849 __zero();
1850}
1851
1852template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier815ed732016-09-16 00:00:48 +00001853void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1854 size_type __sz,
1855 size_type __reserve)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001856{
1857 if (__reserve > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001858 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001859 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001860 if (__fits_in_sso(__reserve))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001861 {
1862 __set_short_size(__sz);
1863 __p = __get_short_pointer();
1864 }
1865 else
1866 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001867 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__reserve) + 1);
1868 __p = __allocation.ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001869 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001870 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001871 __set_long_size(__sz);
1872 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001873 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001874 traits_type::assign(__p[__sz], value_type());
1875}
1876
1877template <class _CharT, class _Traits, class _Allocator>
1878void
Howard Hinnantd17880b2013-06-28 16:59:19 +00001879basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001880{
1881 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001882 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001883 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001884 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001885 {
1886 __set_short_size(__sz);
1887 __p = __get_short_pointer();
1888 }
1889 else
1890 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001891 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
1892 __p = __allocation.ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001893 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001894 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001895 __set_long_size(__sz);
1896 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001897 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001898 traits_type::assign(__p[__sz], value_type());
1899}
1900
1901template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00001902template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00001903basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001904 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001905{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001906 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001907 __init(__s, traits_type::length(__s));
Nikolas Klauserf2807732022-01-11 00:33:35 +01001908 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001909}
1910
1911template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001912inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001913basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001914 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001915{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001916 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
1917 __init(__s, __n);
1918 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001919}
1920
1921template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001922inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001923basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001924 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001925{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001926 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001927 __init(__s, __n);
Nikolas Klauserf2807732022-01-11 00:33:35 +01001928 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001929}
1930
1931template <class _CharT, class _Traits, class _Allocator>
1932basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001933 : __r_(__default_init_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001934{
1935 if (!__str.__is_long())
1936 __r_.first().__r = __str.__r_.first().__r;
1937 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001938 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1939 __str.__get_long_size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01001940 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001941}
1942
1943template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00001944basic_string<_CharT, _Traits, _Allocator>::basic_string(
1945 const basic_string& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001946 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001947{
1948 if (!__str.__is_long())
1949 __r_.first().__r = __str.__r_.first().__r;
1950 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001951 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1952 __str.__get_long_size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01001953 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001954}
1955
Martijn Vels5e7c9752020-03-04 17:52:46 -05001956template <class _CharT, class _Traits, class _Allocator>
1957void basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(
1958 const value_type* __s, size_type __sz) {
1959 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001960 if (__fits_in_sso(__sz)) {
Martijn Vels5e7c9752020-03-04 17:52:46 -05001961 __p = __get_short_pointer();
1962 __set_short_size(__sz);
1963 } else {
1964 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001965 __throw_length_error();
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001966 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
1967 __p = __allocation.ptr;
Martijn Vels5e7c9752020-03-04 17:52:46 -05001968 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02001969 __set_long_cap(__allocation.count);
Martijn Vels5e7c9752020-03-04 17:52:46 -05001970 __set_long_size(__sz);
1971 }
1972 traits_type::copy(_VSTD::__to_address(__p), __s, __sz + 1);
1973}
1974
Eric Fiselierfc92be82017-04-19 00:28:44 +00001975#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001976
1977template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001978inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001979basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +00001980#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +00001981 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowa80abc72015-06-03 19:56:43 +00001982#else
1983 _NOEXCEPT
1984#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001985 : __r_(_VSTD::move(__str.__r_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001986{
1987 __str.__zero();
Nikolas Klauserf2807732022-01-11 00:33:35 +01001988 _VSTD::__debug_db_insert_c(this);
Louis Dionneba400782020-10-02 15:02:52 -04001989#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauserf2807732022-01-11 00:33:35 +01001990 if (!__libcpp_is_constant_evaluated() && __is_long())
1991 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001992#endif
1993}
1994
1995template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001996inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001997basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001998 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001999{
Marshall Clowd2d24692014-07-17 15:32:20 +00002000 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002001 __init(_VSTD::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd2d24692014-07-17 15:32:20 +00002002 else
2003 {
2004 __r_.first().__r = __str.__r_.first().__r;
2005 __str.__zero();
2006 }
Nikolas Klauserf2807732022-01-11 00:33:35 +01002007 _VSTD::__debug_db_insert_c(this);
Louis Dionneba400782020-10-02 15:02:52 -04002008#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauserf2807732022-01-11 00:33:35 +01002009 if (!__libcpp_is_constant_evaluated() && __is_long())
2010 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002011#endif
2012}
2013
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002014#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002015
2016template <class _CharT, class _Traits, class _Allocator>
2017void
2018basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
2019{
2020 if (__n > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002021 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002022 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002023 if (__fits_in_sso(__n))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002024 {
2025 __set_short_size(__n);
2026 __p = __get_short_pointer();
2027 }
2028 else
2029 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002030 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__n) + 1);
2031 __p = __allocation.ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002032 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002033 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002034 __set_long_size(__n);
2035 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002036 traits_type::assign(_VSTD::__to_address(__p), __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002037 traits_type::assign(__p[__n], value_type());
2038}
2039
2040template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002041inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002042basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002043 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002044{
2045 __init(__n, __c);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002046 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002047}
2048
2049template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002050template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00002051basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002052 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002053{
2054 __init(__n, __c);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002055 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002056}
2057
Howard Hinnantc51e1022010-05-11 19:42:16 +00002058template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00002059basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
2060 size_type __pos, size_type __n,
2061 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002062 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002063{
2064 size_type __str_sz = __str.size();
2065 if (__pos > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002066 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002067 __init(__str.data() + __pos, _VSTD::min(__n, __str_sz - __pos));
Nikolas Klauserf2807732022-01-11 00:33:35 +01002068 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002069}
2070
2071template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002072inline
Marshall Clow83445802016-04-07 18:13:41 +00002073basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +00002074 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002075 : __r_(__default_init_tag(), __a)
Marshall Clow83445802016-04-07 18:13:41 +00002076{
2077 size_type __str_sz = __str.size();
2078 if (__pos > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002079 __throw_out_of_range();
Marshall Clow83445802016-04-07 18:13:41 +00002080 __init(__str.data() + __pos, __str_sz - __pos);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002081 _VSTD::__debug_db_insert_c(this);
Marshall Clow83445802016-04-07 18:13:41 +00002082}
2083
2084template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002085template <class _Tp, class>
Marshall Clow78dbe462016-11-14 18:22:19 +00002086basic_string<_CharT, _Traits, _Allocator>::basic_string(
Marshall Clowe46031a2018-07-02 18:41:15 +00002087 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002088 : __r_(__default_init_tag(), __a)
Marshall Clow78dbe462016-11-14 18:22:19 +00002089{
Marshall Clowe46031a2018-07-02 18:41:15 +00002090 __self_view __sv0 = __t;
2091 __self_view __sv = __sv0.substr(__pos, __n);
Marshall Clow78dbe462016-11-14 18:22:19 +00002092 __init(__sv.data(), __sv.size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002093 _VSTD::__debug_db_insert_c(this);
Marshall Clow78dbe462016-11-14 18:22:19 +00002094}
2095
2096template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002097template <class _Tp, class>
2098basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002099 : __r_(__default_init_tag(), __default_init_tag())
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002100{
Marshall Clowe46031a2018-07-02 18:41:15 +00002101 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002102 __init(__sv.data(), __sv.size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002103 _VSTD::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002104}
2105
2106template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002107template <class _Tp, class>
2108basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002109 : __r_(__default_init_tag(), __a)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002110{
Marshall Clowe46031a2018-07-02 18:41:15 +00002111 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002112 __init(__sv.data(), __sv.size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002113 _VSTD::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002114}
2115
2116template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002117template <class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002118__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002119<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002120 __is_exactly_cpp17_input_iterator<_InputIterator>::value
2121>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002122basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
2123{
2124 __zero();
2125#ifndef _LIBCPP_NO_EXCEPTIONS
2126 try
2127 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002128#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002129 for (; __first != __last; ++__first)
2130 push_back(*__first);
2131#ifndef _LIBCPP_NO_EXCEPTIONS
2132 }
2133 catch (...)
2134 {
2135 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002136 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002137 throw;
2138 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002139#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002140}
2141
2142template <class _CharT, class _Traits, class _Allocator>
2143template <class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002144__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002145<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002146 __is_cpp17_forward_iterator<_ForwardIterator>::value
2147>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002148basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
2149{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002150 size_type __sz = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002151 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002152 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002153 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002154 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002155 {
2156 __set_short_size(__sz);
2157 __p = __get_short_pointer();
2158 }
2159 else
2160 {
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002161 auto __allocation = std::__allocate_at_least(__alloc(), __recommend(__sz) + 1);
2162 __p = __allocation.ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002163 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002164 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002165 __set_long_size(__sz);
2166 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002167
2168#ifndef _LIBCPP_NO_EXCEPTIONS
2169 try
2170 {
2171#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002172 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002173 traits_type::assign(*__p, *__first);
2174 traits_type::assign(*__p, value_type());
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002175#ifndef _LIBCPP_NO_EXCEPTIONS
2176 }
2177 catch (...)
2178 {
2179 if (__is_long())
2180 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
2181 throw;
2182 }
2183#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002184}
2185
2186template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002187template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002188inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002189basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002190 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002191{
2192 __init(__first, __last);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002193 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002194}
2195
2196template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002197template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002198inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002199basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
2200 const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002201 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002202{
2203 __init(__first, __last);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002204 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002205}
2206
Eric Fiselierfc92be82017-04-19 00:28:44 +00002207#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002208
Howard Hinnantc51e1022010-05-11 19:42:16 +00002209template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002210inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002211basic_string<_CharT, _Traits, _Allocator>::basic_string(
2212 initializer_list<_CharT> __il)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002213 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002214{
2215 __init(__il.begin(), __il.end());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002216 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002217}
2218
2219template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002220inline
Eric Fiselier9d355982017-04-12 23:45:53 +00002221
Eric Fiselier812882b2017-02-17 01:17:10 +00002222basic_string<_CharT, _Traits, _Allocator>::basic_string(
2223 initializer_list<_CharT> __il, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002224 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002225{
2226 __init(__il.begin(), __il.end());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002227 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002228}
2229
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002230#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002231
Howard Hinnantc51e1022010-05-11 19:42:16 +00002232template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002233basic_string<_CharT, _Traits, _Allocator>::~basic_string()
2234{
Louis Dionneba400782020-10-02 15:02:52 -04002235#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002236 if (!__libcpp_is_constant_evaluated())
2237 __get_db()->__erase_c(this);
Howard Hinnant8ea98242013-08-23 17:37:05 +00002238#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002239 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002240 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002241}
2242
2243template <class _CharT, class _Traits, class _Allocator>
2244void
2245basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
2246 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002247 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 +00002248{
2249 size_type __ms = max_size();
2250 if (__delta_cap > __ms - __old_cap - 1)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002251 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002252 pointer __old_p = __get_pointer();
2253 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002254 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002255 __ms - 1;
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002256 auto __allocation = std::__allocate_at_least(__alloc(), __cap + 1);
2257 pointer __p = __allocation.ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002258 __invalidate_all_iterators();
2259 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002260 traits_type::copy(_VSTD::__to_address(__p),
2261 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002262 if (__n_add != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002263 traits_type::copy(_VSTD::__to_address(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002264 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2265 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002266 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2267 _VSTD::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002268 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002269 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002270 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002271 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002272 __old_sz = __n_copy + __n_add + __sec_cp_sz;
2273 __set_long_size(__old_sz);
2274 traits_type::assign(__p[__old_sz], value_type());
2275}
2276
2277template <class _CharT, class _Traits, class _Allocator>
2278void
2279basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2280 size_type __n_copy, size_type __n_del, size_type __n_add)
2281{
2282 size_type __ms = max_size();
Marshall Clow2267c5d2013-11-06 14:24:38 +00002283 if (__delta_cap > __ms - __old_cap)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002284 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002285 pointer __old_p = __get_pointer();
2286 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002287 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002288 __ms - 1;
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002289 auto __allocation = std::__allocate_at_least(__alloc(), __cap + 1);
2290 pointer __p = __allocation.ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002291 __invalidate_all_iterators();
2292 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002293 traits_type::copy(_VSTD::__to_address(__p),
2294 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002295 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2296 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002297 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2298 _VSTD::__to_address(__old_p) + __n_copy + __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002299 __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002300 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002301 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002302 __set_long_pointer(__p);
Nikolas Klauserc513eba2022-04-09 09:41:19 +02002303 __set_long_cap(__allocation.count);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002304}
2305
2306// assign
2307
2308template <class _CharT, class _Traits, class _Allocator>
Martijn Vels596e3de2020-02-26 15:55:49 -05002309template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04002310basic_string<_CharT, _Traits, _Allocator>&
2311basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(
Martijn Vels596e3de2020-02-26 15:55:49 -05002312 const value_type* __s, size_type __n) {
Louis Dionne6209e9f2022-03-03 13:39:12 -05002313 size_type __cap = __is_short ? static_cast<size_type>(__min_cap) : __get_long_cap();
Martijn Vels596e3de2020-02-26 15:55:49 -05002314 if (__n < __cap) {
2315 pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
2316 __is_short ? __set_short_size(__n) : __set_long_size(__n);
2317 traits_type::copy(_VSTD::__to_address(__p), __s, __n);
2318 traits_type::assign(__p[__n], value_type());
2319 __invalidate_iterators_past(__n);
2320 } else {
2321 size_type __sz = __is_short ? __get_short_size() : __get_long_size();
2322 __grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
2323 }
Martijn Velsb6a08b62020-04-10 18:36:31 -04002324 return *this;
Martijn Vels596e3de2020-02-26 15:55:49 -05002325}
2326
2327template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002328basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002329basic_string<_CharT, _Traits, _Allocator>::__assign_external(
2330 const value_type* __s, size_type __n) {
2331 size_type __cap = capacity();
2332 if (__cap >= __n) {
2333 value_type* __p = _VSTD::__to_address(__get_pointer());
2334 traits_type::move(__p, __s, __n);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002335 return __null_terminate_at(__p, __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04002336 } else {
2337 size_type __sz = size();
2338 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002339 return *this;
Martijn Velsda7d94f2020-06-19 14:24:03 -04002340 }
Martijn Velsda7d94f2020-06-19 14:24:03 -04002341}
2342
2343template <class _CharT, class _Traits, class _Allocator>
2344basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002345basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002346{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002347 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Nikolas Klauser93826d12022-01-04 17:24:03 +01002348 return (__builtin_constant_p(__n) && __fits_in_sso(__n))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002349 ? __assign_short(__s, __n)
2350 : __assign_external(__s, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002351}
2352
2353template <class _CharT, class _Traits, class _Allocator>
2354basic_string<_CharT, _Traits, _Allocator>&
2355basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2356{
2357 size_type __cap = capacity();
2358 if (__cap < __n)
2359 {
2360 size_type __sz = size();
2361 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2362 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002363 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002364 traits_type::assign(__p, __n, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002365 return __null_terminate_at(__p, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002366}
2367
2368template <class _CharT, class _Traits, class _Allocator>
2369basic_string<_CharT, _Traits, _Allocator>&
2370basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2371{
2372 pointer __p;
2373 if (__is_long())
2374 {
2375 __p = __get_long_pointer();
2376 __set_long_size(1);
2377 }
2378 else
2379 {
2380 __p = __get_short_pointer();
2381 __set_short_size(1);
2382 }
2383 traits_type::assign(*__p, __c);
2384 traits_type::assign(*++__p, value_type());
2385 __invalidate_iterators_past(1);
2386 return *this;
2387}
2388
2389template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002390basic_string<_CharT, _Traits, _Allocator>&
2391basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2392{
Martijn Vels596e3de2020-02-26 15:55:49 -05002393 if (this != &__str) {
2394 __copy_assign_alloc(__str);
2395 if (!__is_long()) {
2396 if (!__str.__is_long()) {
Eric Fiselier03bbc922020-01-15 17:27:10 -05002397 __r_.first().__r = __str.__r_.first().__r;
Martijn Vels596e3de2020-02-26 15:55:49 -05002398 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002399 return __assign_no_alias<true>(__str.data(), __str.size());
Martijn Vels596e3de2020-02-26 15:55:49 -05002400 }
2401 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002402 return __assign_no_alias<false>(__str.data(), __str.size());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002403 }
Martijn Vels596e3de2020-02-26 15:55:49 -05002404 }
2405 return *this;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002406}
2407
Eric Fiselierfc92be82017-04-19 00:28:44 +00002408#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002409
2410template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002411inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002412void
2413basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002414 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002415{
2416 if (__alloc() != __str.__alloc())
2417 assign(__str);
2418 else
2419 __move_assign(__str, true_type());
2420}
2421
2422template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002423inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002424void
2425basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002426#if _LIBCPP_STD_VER > 14
2427 _NOEXCEPT
2428#else
Howard Hinnant3e276872011-06-03 18:40:47 +00002429 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002430#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002431{
marshall2ed41622019-11-27 07:13:00 -08002432 if (__is_long()) {
2433 __alloc_traits::deallocate(__alloc(), __get_long_pointer(),
2434 __get_long_cap());
2435#if _LIBCPP_STD_VER <= 14
2436 if (!is_nothrow_move_assignable<allocator_type>::value) {
2437 __set_short_size(0);
2438 traits_type::assign(__get_short_pointer()[0], value_type());
2439 }
2440#endif
2441 }
2442 __move_assign_alloc(__str);
2443 __r_.first() = __str.__r_.first();
2444 __str.__set_short_size(0);
2445 traits_type::assign(__str.__get_short_pointer()[0], value_type());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002446}
2447
2448template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002449inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002450basic_string<_CharT, _Traits, _Allocator>&
2451basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002452 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002453{
2454 __move_assign(__str, integral_constant<bool,
2455 __alloc_traits::propagate_on_container_move_assignment::value>());
2456 return *this;
2457}
2458
2459#endif
2460
2461template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002462template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002463__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002464<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002465 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002466 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002467>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002468basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2469{
Marshall Clow958362f2016-09-05 01:54:30 +00002470 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002471 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002472 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002473}
2474
2475template <class _CharT, class _Traits, class _Allocator>
2476template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002477__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002478<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002479 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002480 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002481>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002482basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2483{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002484 size_type __cap = capacity();
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002485 size_type __n = __string_is_trivial_iterator<_ForwardIterator>::value ?
2486 static_cast<size_type>(_VSTD::distance(__first, __last)) : 0;
2487
2488 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2489 (__cap >= __n || !__addr_in_range(*__first)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002490 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002491 if (__cap < __n)
2492 {
2493 size_type __sz = size();
2494 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2495 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002496 pointer __p = __get_pointer();
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002497 for (; __first != __last; ++__p, (void) ++__first)
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002498 traits_type::assign(*__p, *__first);
2499 traits_type::assign(*__p, value_type());
2500 __set_size(__n);
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04002501 __invalidate_iterators_past(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002502 }
2503 else
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002504 {
2505 const basic_string __temp(__first, __last, __alloc());
2506 assign(__temp.data(), __temp.size());
2507 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002508 return *this;
2509}
2510
2511template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002512basic_string<_CharT, _Traits, _Allocator>&
2513basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2514{
2515 size_type __sz = __str.size();
2516 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002517 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002518 return assign(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002519}
2520
2521template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002522template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002523__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002524<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002525 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
2526 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00002527 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002528>
Marshall Clow82513342016-09-24 22:45:42 +00002529basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002530{
Marshall Clow82513342016-09-24 22:45:42 +00002531 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002532 size_type __sz = __sv.size();
2533 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002534 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002535 return assign(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2536}
2537
2538
2539template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002540basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002541basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s) {
2542 return __assign_external(__s, traits_type::length(__s));
2543}
2544
2545template <class _CharT, class _Traits, class _Allocator>
2546basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002547basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002548{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002549 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Louis Dionne3e0c4332021-08-31 10:49:06 -04002550 return __builtin_constant_p(*__s)
Nikolas Klauser93826d12022-01-04 17:24:03 +01002551 ? (__fits_in_sso(traits_type::length(__s))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002552 ? __assign_short(__s, traits_type::length(__s))
2553 : __assign_external(__s, traits_type::length(__s)))
2554 : __assign_external(__s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002555}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002556// append
2557
2558template <class _CharT, class _Traits, class _Allocator>
2559basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002560basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002561{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002562 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002563 size_type __cap = capacity();
2564 size_type __sz = size();
2565 if (__cap - __sz >= __n)
2566 {
2567 if (__n)
2568 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002569 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002570 traits_type::copy(__p + __sz, __s, __n);
2571 __sz += __n;
2572 __set_size(__sz);
2573 traits_type::assign(__p[__sz], value_type());
2574 }
2575 }
2576 else
2577 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2578 return *this;
2579}
2580
2581template <class _CharT, class _Traits, class _Allocator>
2582basic_string<_CharT, _Traits, _Allocator>&
2583basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2584{
2585 if (__n)
2586 {
2587 size_type __cap = capacity();
2588 size_type __sz = size();
2589 if (__cap - __sz < __n)
2590 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2591 pointer __p = __get_pointer();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002592 traits_type::assign(_VSTD::__to_address(__p) + __sz, __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002593 __sz += __n;
2594 __set_size(__sz);
2595 traits_type::assign(__p[__sz], value_type());
2596 }
2597 return *this;
2598}
2599
2600template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00002601inline void
2602basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n)
2603{
2604 if (__n)
2605 {
2606 size_type __cap = capacity();
2607 size_type __sz = size();
2608 if (__cap - __sz < __n)
2609 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2610 pointer __p = __get_pointer();
2611 __sz += __n;
2612 __set_size(__sz);
2613 traits_type::assign(__p[__sz], value_type());
2614 }
2615}
2616
2617template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002618void
2619basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2620{
Howard Hinnant68bf1812013-04-30 21:44:48 +00002621 bool __is_short = !__is_long();
2622 size_type __cap;
2623 size_type __sz;
2624 if (__is_short)
2625 {
2626 __cap = __min_cap - 1;
2627 __sz = __get_short_size();
2628 }
2629 else
2630 {
2631 __cap = __get_long_cap() - 1;
2632 __sz = __get_long_size();
2633 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002634 if (__sz == __cap)
Howard Hinnant68bf1812013-04-30 21:44:48 +00002635 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002636 __grow_by(__cap, 1, __sz, __sz, 0);
Louis Dionne82d2b2c2021-11-16 11:26:56 -05002637 __is_short = false; // the string is always long after __grow_by
Howard Hinnant68bf1812013-04-30 21:44:48 +00002638 }
2639 pointer __p;
2640 if (__is_short)
2641 {
2642 __p = __get_short_pointer() + __sz;
2643 __set_short_size(__sz+1);
2644 }
2645 else
2646 {
2647 __p = __get_long_pointer() + __sz;
2648 __set_long_size(__sz+1);
2649 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002650 traits_type::assign(*__p, __c);
2651 traits_type::assign(*++__p, value_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002652}
2653
Howard Hinnantc51e1022010-05-11 19:42:16 +00002654template <class _CharT, class _Traits, class _Allocator>
2655template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002656__enable_if_t
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002657<
2658 __is_cpp17_forward_iterator<_ForwardIterator>::value,
2659 basic_string<_CharT, _Traits, _Allocator>&
2660>
2661basic_string<_CharT, _Traits, _Allocator>::append(
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002662 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002663{
2664 size_type __sz = size();
2665 size_type __cap = capacity();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002666 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002667 if (__n)
2668 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002669 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2670 !__addr_in_range(*__first))
Marshall Clow958362f2016-09-05 01:54:30 +00002671 {
2672 if (__cap - __sz < __n)
2673 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2674 pointer __p = __get_pointer() + __sz;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002675 for (; __first != __last; ++__p, (void) ++__first)
Marshall Clow958362f2016-09-05 01:54:30 +00002676 traits_type::assign(*__p, *__first);
2677 traits_type::assign(*__p, value_type());
2678 __set_size(__sz + __n);
2679 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002680 else
2681 {
2682 const basic_string __temp(__first, __last, __alloc());
2683 append(__temp.data(), __temp.size());
2684 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002685 }
2686 return *this;
2687}
2688
2689template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002690inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002691basic_string<_CharT, _Traits, _Allocator>&
2692basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2693{
2694 return append(__str.data(), __str.size());
2695}
2696
2697template <class _CharT, class _Traits, class _Allocator>
2698basic_string<_CharT, _Traits, _Allocator>&
2699basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2700{
2701 size_type __sz = __str.size();
2702 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002703 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002704 return append(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002705}
2706
2707template <class _CharT, class _Traits, class _Allocator>
Marshall Clow62953962016-10-03 23:40:48 +00002708template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002709 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002710 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002711 __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 +00002712 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002713 >
Marshall Clow82513342016-09-24 22:45:42 +00002714basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002715{
Marshall Clow82513342016-09-24 22:45:42 +00002716 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002717 size_type __sz = __sv.size();
2718 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002719 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002720 return append(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2721}
2722
2723template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002724basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002725basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002726{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002727 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002728 return append(__s, traits_type::length(__s));
2729}
2730
2731// insert
2732
2733template <class _CharT, class _Traits, class _Allocator>
2734basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002735basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002736{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002737 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002738 size_type __sz = size();
2739 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002740 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002741 size_type __cap = capacity();
2742 if (__cap - __sz >= __n)
2743 {
2744 if (__n)
2745 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002746 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002747 size_type __n_move = __sz - __pos;
2748 if (__n_move != 0)
2749 {
2750 if (__p + __pos <= __s && __s < __p + __sz)
2751 __s += __n;
2752 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2753 }
2754 traits_type::move(__p + __pos, __s, __n);
2755 __sz += __n;
2756 __set_size(__sz);
2757 traits_type::assign(__p[__sz], value_type());
2758 }
2759 }
2760 else
2761 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2762 return *this;
2763}
2764
2765template <class _CharT, class _Traits, class _Allocator>
2766basic_string<_CharT, _Traits, _Allocator>&
2767basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2768{
2769 size_type __sz = size();
2770 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002771 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002772 if (__n)
2773 {
2774 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002775 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002776 if (__cap - __sz >= __n)
2777 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002778 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002779 size_type __n_move = __sz - __pos;
2780 if (__n_move != 0)
2781 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2782 }
2783 else
2784 {
2785 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002786 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002787 }
2788 traits_type::assign(__p + __pos, __n, __c);
2789 __sz += __n;
2790 __set_size(__sz);
2791 traits_type::assign(__p[__sz], value_type());
2792 }
2793 return *this;
2794}
2795
2796template <class _CharT, class _Traits, class _Allocator>
2797template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002798__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002799<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002800 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Marshall Clow039b2f02016-01-13 21:54:34 +00002801 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002802>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002803basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2804{
Nikolas Klausereed25832021-12-15 01:32:30 +01002805 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2806 "string::insert(iterator, range) called with an iterator not"
2807 " referring to this string");
2808 const basic_string __temp(__first, __last, __alloc());
2809 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002810}
2811
2812template <class _CharT, class _Traits, class _Allocator>
2813template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002814__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002815<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002816 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002817 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002818>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002819basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2820{
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002821 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2822 "string::insert(iterator, range) called with an iterator not referring to this string");
Nikolas Klausereed25832021-12-15 01:32:30 +01002823
Howard Hinnantc51e1022010-05-11 19:42:16 +00002824 size_type __ip = static_cast<size_type>(__pos - begin());
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002825 size_type __n = static_cast<size_type>(std::distance(__first, __last));
2826 if (__n == 0)
2827 return begin() + __ip;
2828
2829 if (__string_is_trivial_iterator<_ForwardIterator>::value && !__addr_in_range(*__first))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002830 {
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002831 return __insert_from_safe_copy(__n, __ip, __first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002832 }
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002833 else
2834 {
2835 const basic_string __temp(__first, __last, __alloc());
2836 return __insert_from_safe_copy(__n, __ip, __temp.begin(), __temp.end());
2837 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002838}
2839
2840template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002841inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002842basic_string<_CharT, _Traits, _Allocator>&
2843basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2844{
2845 return insert(__pos1, __str.data(), __str.size());
2846}
2847
2848template <class _CharT, class _Traits, class _Allocator>
2849basic_string<_CharT, _Traits, _Allocator>&
2850basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2851 size_type __pos2, size_type __n)
2852{
2853 size_type __str_sz = __str.size();
2854 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002855 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002856 return insert(__pos1, __str.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002857}
2858
2859template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002860template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002861__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002862<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002863 __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 +00002864 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002865>
Marshall Clow82513342016-09-24 22:45:42 +00002866basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002867 size_type __pos2, size_type __n)
2868{
Marshall Clow82513342016-09-24 22:45:42 +00002869 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002870 size_type __str_sz = __sv.size();
2871 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002872 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002873 return insert(__pos1, __sv.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
2874}
2875
2876template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002877basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002878basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002879{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002880 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002881 return insert(__pos, __s, traits_type::length(__s));
2882}
2883
2884template <class _CharT, class _Traits, class _Allocator>
2885typename basic_string<_CharT, _Traits, _Allocator>::iterator
2886basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2887{
Louis Dionnef4cc6fb2022-02-15 15:47:45 -05002888 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2889 "string::insert(iterator, character) called with an iterator not"
2890 " referring to this string");
2891
Howard Hinnantc51e1022010-05-11 19:42:16 +00002892 size_type __ip = static_cast<size_type>(__pos - begin());
2893 size_type __sz = size();
2894 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002895 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002896 if (__cap == __sz)
2897 {
2898 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002899 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002900 }
2901 else
2902 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002903 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002904 size_type __n_move = __sz - __ip;
2905 if (__n_move != 0)
2906 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2907 }
2908 traits_type::assign(__p[__ip], __c);
2909 traits_type::assign(__p[++__sz], value_type());
2910 __set_size(__sz);
2911 return begin() + static_cast<difference_type>(__ip);
2912}
2913
2914template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002915inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002916typename basic_string<_CharT, _Traits, _Allocator>::iterator
2917basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2918{
Nikolas Klausereed25832021-12-15 01:32:30 +01002919 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2920 "string::insert(iterator, n, value) called with an iterator not"
2921 " referring to this string");
2922 difference_type __p = __pos - begin();
2923 insert(static_cast<size_type>(__p), __n, __c);
2924 return begin() + __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002925}
2926
2927// replace
2928
2929template <class _CharT, class _Traits, class _Allocator>
2930basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002931basic_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 +00002932 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00002933{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002934 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002935 size_type __sz = size();
2936 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002937 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002938 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002939 size_type __cap = capacity();
2940 if (__cap - __sz + __n1 >= __n2)
2941 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002942 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002943 if (__n1 != __n2)
2944 {
2945 size_type __n_move = __sz - __pos - __n1;
2946 if (__n_move != 0)
2947 {
2948 if (__n1 > __n2)
2949 {
2950 traits_type::move(__p + __pos, __s, __n2);
2951 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002952 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002953 }
2954 if (__p + __pos < __s && __s < __p + __sz)
2955 {
2956 if (__p + __pos + __n1 <= __s)
2957 __s += __n2 - __n1;
2958 else // __p + __pos < __s < __p + __pos + __n1
2959 {
2960 traits_type::move(__p + __pos, __s, __n1);
2961 __pos += __n1;
2962 __s += __n2;
2963 __n2 -= __n1;
2964 __n1 = 0;
2965 }
2966 }
2967 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2968 }
2969 }
2970 traits_type::move(__p + __pos, __s, __n2);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002971 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002972 }
2973 else
2974 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
2975 return *this;
2976}
2977
2978template <class _CharT, class _Traits, class _Allocator>
2979basic_string<_CharT, _Traits, _Allocator>&
2980basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
2981{
2982 size_type __sz = size();
2983 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002984 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002985 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002986 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002987 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002988 if (__cap - __sz + __n1 >= __n2)
2989 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002990 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002991 if (__n1 != __n2)
2992 {
2993 size_type __n_move = __sz - __pos - __n1;
2994 if (__n_move != 0)
2995 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2996 }
2997 }
2998 else
2999 {
3000 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003001 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003002 }
3003 traits_type::assign(__p + __pos, __n2, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003004 return __null_terminate_at(__p, __sz - (__n1 - __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003005}
3006
3007template <class _CharT, class _Traits, class _Allocator>
3008template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003009__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00003010<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003011 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003012 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003013>
Howard Hinnant990d6e82010-11-17 21:11:40 +00003014basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003015 _InputIterator __j1, _InputIterator __j2)
3016{
Marshall Clow958362f2016-09-05 01:54:30 +00003017 const basic_string __temp(__j1, __j2, __alloc());
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003018 return replace(__i1, __i2, __temp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003019}
3020
3021template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003022inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003023basic_string<_CharT, _Traits, _Allocator>&
3024basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
3025{
3026 return replace(__pos1, __n1, __str.data(), __str.size());
3027}
3028
3029template <class _CharT, class _Traits, class _Allocator>
3030basic_string<_CharT, _Traits, _Allocator>&
3031basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
3032 size_type __pos2, size_type __n2)
3033{
3034 size_type __str_sz = __str.size();
3035 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003036 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003037 return replace(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003038}
3039
3040template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003041template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003042__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003043<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003044 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00003045 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003046>
Marshall Clow82513342016-09-24 22:45:42 +00003047basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003048 size_type __pos2, size_type __n2)
3049{
Marshall Clow82513342016-09-24 22:45:42 +00003050 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003051 size_type __str_sz = __sv.size();
3052 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003053 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003054 return replace(__pos1, __n1, __sv.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
3055}
3056
3057template <class _CharT, class _Traits, class _Allocator>
3058basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003059basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003060{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003061 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003062 return replace(__pos, __n1, __s, traits_type::length(__s));
3063}
3064
3065template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003066inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003067basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003068basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003069{
3070 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
3071 __str.data(), __str.size());
3072}
3073
3074template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003075inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003076basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003077basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003078{
3079 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
3080}
3081
3082template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003083inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003084basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003085basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003086{
3087 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
3088}
3089
3090template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003091inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003092basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003093basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003094{
3095 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
3096}
3097
3098// erase
3099
Martijn Velsa81fc792020-02-26 13:25:43 -05003100// 'externally instantiated' erase() implementation, called when __n != npos.
3101// Does not check __pos against size()
Howard Hinnantc51e1022010-05-11 19:42:16 +00003102template <class _CharT, class _Traits, class _Allocator>
Martijn Velsa81fc792020-02-26 13:25:43 -05003103void
3104basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(
3105 size_type __pos, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003106{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003107 if (__n)
3108 {
Martijn Velsa81fc792020-02-26 13:25:43 -05003109 size_type __sz = size();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003110 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003111 __n = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003112 size_type __n_move = __sz - __pos - __n;
3113 if (__n_move != 0)
3114 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003115 __null_terminate_at(__p, __sz - __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003116 }
Martijn Velsa81fc792020-02-26 13:25:43 -05003117}
3118
3119template <class _CharT, class _Traits, class _Allocator>
3120basic_string<_CharT, _Traits, _Allocator>&
3121basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos,
3122 size_type __n) {
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003123 if (__pos > size())
3124 __throw_out_of_range();
Martijn Velsa81fc792020-02-26 13:25:43 -05003125 if (__n == npos) {
3126 __erase_to_end(__pos);
3127 } else {
3128 __erase_external_with_move(__pos, __n);
3129 }
3130 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003131}
3132
3133template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003134inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003135typename basic_string<_CharT, _Traits, _Allocator>::iterator
3136basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
3137{
Nikolas Klausereed25832021-12-15 01:32:30 +01003138 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3139 "string::erase(iterator) called with an iterator not"
3140 " referring to this string");
3141
3142 _LIBCPP_ASSERT(__pos != end(), "string::erase(iterator) called with a non-dereferenceable iterator");
3143 iterator __b = begin();
3144 size_type __r = static_cast<size_type>(__pos - __b);
3145 erase(__r, 1);
3146 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003147}
3148
3149template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003150inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003151typename basic_string<_CharT, _Traits, _Allocator>::iterator
3152basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
3153{
Nikolas Klausereed25832021-12-15 01:32:30 +01003154 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
3155 "string::erase(iterator, iterator) called with an iterator not"
3156 " referring to this string");
3157
3158 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
3159 iterator __b = begin();
3160 size_type __r = static_cast<size_type>(__first - __b);
3161 erase(__r, static_cast<size_type>(__last - __first));
3162 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003163}
3164
3165template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003166inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003167void
3168basic_string<_CharT, _Traits, _Allocator>::pop_back()
3169{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003170 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003171 __erase_to_end(size() - 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003172}
3173
3174template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003175inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003176void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003177basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003178{
3179 __invalidate_all_iterators();
3180 if (__is_long())
3181 {
3182 traits_type::assign(*__get_long_pointer(), value_type());
3183 __set_long_size(0);
3184 }
3185 else
3186 {
3187 traits_type::assign(*__get_short_pointer(), value_type());
3188 __set_short_size(0);
3189 }
3190}
3191
3192template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003193inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003194void
3195basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
3196{
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003197 __null_terminate_at(_VSTD::__to_address(__get_pointer()), __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003198}
3199
3200template <class _CharT, class _Traits, class _Allocator>
3201void
3202basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
3203{
3204 size_type __sz = size();
3205 if (__n > __sz)
3206 append(__n - __sz, __c);
3207 else
3208 __erase_to_end(__n);
3209}
3210
3211template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00003212inline void
3213basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n)
3214{
3215 size_type __sz = size();
3216 if (__n > __sz) {
3217 __append_default_init(__n - __sz);
3218 } else
3219 __erase_to_end(__n);
3220}
3221
3222template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003223inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003224typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003225basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003226{
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003227 size_type __m = __alloc_traits::max_size(__alloc());
Eric Fiseliere9cc5922017-10-17 13:16:01 +00003228#ifdef _LIBCPP_BIG_ENDIAN
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003229 return (__m <= ~__long_mask ? __m : __m/2) - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003230#else
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003231 return __m - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003232#endif
3233}
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