blob: 88b18be41cf66c6271bc56a0b055ea12f858d605 [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 Klauser8fccd622022-03-05 19:17:07 +0100531#include <__utility/auto_cast.h>
532#include <__utility/move.h>
533#include <__utility/swap.h>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400534#include <compare>
535#include <cstdio> // EOF
Louis Dionned24191c2021-08-19 12:39:16 -0400536#include <cstdlib>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400537#include <cstring>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400538#include <initializer_list>
539#include <iosfwd>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000540#include <iterator>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000541#include <memory>
542#include <stdexcept>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400543#include <string_view>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000544#include <type_traits>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000545#include <version>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000546
Nikolas Klauser4c1bf592022-02-11 19:15:18 +0100547// TODO: remove these headers
548#include <__functional/binary_function.h>
549#include <__functional/invoke.h>
550#include <__functional/operations.h>
551#include <__functional/reference_wrapper.h>
552#include <__functional/unary_function.h>
553#include <__functional/weak_result_type.h>
554#include <new>
555#include <typeinfo>
556
Louis Dionne89258142021-08-23 15:32:36 -0400557#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Nikolas Klauser80840272022-02-04 13:04:33 +0100558# include <cwchar>
Louis Dionne89258142021-08-23 15:32:36 -0400559#endif
560
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400561#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
Nikolas Klauser80840272022-02-04 13:04:33 +0100562# include <cstdint>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400563#endif
Eric Fiselier14b6de92014-08-10 23:53:08 +0000564
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000565#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Arthur O'Dwyer6eeaa002022-02-01 20:16:40 -0500566# pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000567#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000568
Eric Fiselierf4433a32017-05-31 22:07:49 +0000569_LIBCPP_PUSH_MACROS
570#include <__undef_macros>
571
572
Howard Hinnantc51e1022010-05-11 19:42:16 +0000573_LIBCPP_BEGIN_NAMESPACE_STD
574
Howard Hinnantc51e1022010-05-11 19:42:16 +0000575// basic_string
576
577template<class _CharT, class _Traits, class _Allocator>
578basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000579operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
580 const basic_string<_CharT, _Traits, _Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000581
582template<class _CharT, class _Traits, class _Allocator>
583basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000584operator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000585
586template<class _CharT, class _Traits, class _Allocator>
587basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000588operator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000589
590template<class _CharT, class _Traits, class _Allocator>
Louis Dionne8f8d95d2018-11-21 17:31:55 +0000591inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000592basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000593operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000594
595template<class _CharT, class _Traits, class _Allocator>
596basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000597operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000598
Shoaib Meenaiea363712017-07-29 02:54:41 +0000599_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&))
600
Marshall Clow039b2f02016-01-13 21:54:34 +0000601template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400602struct __string_is_trivial_iterator : public false_type {};
603
604template <class _Tp>
605struct __string_is_trivial_iterator<_Tp*>
606 : public is_arithmetic<_Tp> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000607
Louis Dionne173f29e2019-05-29 16:01:36 +0000608template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400609struct __string_is_trivial_iterator<__wrap_iter<_Iter> >
610 : public __string_is_trivial_iterator<_Iter> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000611
Marshall Clow82513342016-09-24 22:45:42 +0000612template <class _CharT, class _Traits, class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500613struct __can_be_converted_to_string_view : public _BoolConstant<
614 is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
615 !is_convertible<const _Tp&, const _CharT*>::value
616 > {};
Marshall Clow82513342016-09-24 22:45:42 +0000617
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400618#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800619typedef basic_string<char8_t> u8string;
620#endif
621
622#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
623typedef basic_string<char16_t> u16string;
624typedef basic_string<char32_t> u32string;
Louis Dionne96fc5f52021-09-09 11:25:10 -0400625#endif
Richard Smith256954d2020-11-11 17:12:18 -0800626
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000627template<class _CharT, class _Traits, class _Allocator>
Richard Smith256954d2020-11-11 17:12:18 -0800628class
629 _LIBCPP_TEMPLATE_VIS
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400630#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800631 _LIBCPP_PREFERRED_NAME(u8string)
632#endif
633#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
634 _LIBCPP_PREFERRED_NAME(u16string)
635 _LIBCPP_PREFERRED_NAME(u32string)
636#endif
637 basic_string
Howard Hinnantc51e1022010-05-11 19:42:16 +0000638{
639public:
640 typedef basic_string __self;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000641 typedef basic_string_view<_CharT, _Traits> __self_view;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000642 typedef _Traits traits_type;
Marshall Clowa3a74e02017-03-15 18:41:11 +0000643 typedef _CharT value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000644 typedef _Allocator allocator_type;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000645 typedef allocator_traits<allocator_type> __alloc_traits;
646 typedef typename __alloc_traits::size_type size_type;
647 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant3e276872011-06-03 18:40:47 +0000648 typedef value_type& reference;
649 typedef const value_type& const_reference;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000650 typedef typename __alloc_traits::pointer pointer;
651 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000652
Marshall Clow79f33542018-03-21 00:36:05 +0000653 static_assert((!is_array<value_type>::value), "Character type of basic_string must not be an array");
654 static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string must be standard-layout");
655 static_assert(( is_trivial<value_type>::value), "Character type of basic_string must be trivial");
656 static_assert(( is_same<_CharT, typename traits_type::char_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000657 "traits_type::char_type must be the same type as CharT");
Marshall Clow79f33542018-03-21 00:36:05 +0000658 static_assert(( is_same<typename allocator_type::value_type, value_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000659 "Allocator::value_type must be same type as value_type");
Marshall Clowe46031a2018-07-02 18:41:15 +0000660
Howard Hinnantc51e1022010-05-11 19:42:16 +0000661 typedef __wrap_iter<pointer> iterator;
662 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000663 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
664 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000665
666private:
Howard Hinnant68bf1812013-04-30 21:44:48 +0000667
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000668#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000669
670 struct __long
671 {
672 pointer __data_;
673 size_type __size_;
674 size_type __cap_;
675 };
676
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000677#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000678 static const size_type __short_mask = 0x01;
679 static const size_type __long_mask = 0x1ul;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000680#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000681 static const size_type __short_mask = 0x80;
682 static const size_type __long_mask = ~(size_type(~0) >> 1);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400683#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +0000684
685 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
686 (sizeof(__long) - 1)/sizeof(value_type) : 2};
687
688 struct __short
689 {
690 value_type __data_[__min_cap];
Azat Khuzhin1995f722022-04-08 16:13:46 -0400691 unsigned char __padding[sizeof(value_type) - 1];
692 unsigned char __size_;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000693 };
694
695#else
696
Howard Hinnantc51e1022010-05-11 19:42:16 +0000697 struct __long
698 {
699 size_type __cap_;
700 size_type __size_;
701 pointer __data_;
702 };
703
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000704#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000705 static const size_type __short_mask = 0x80;
706 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000707#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000708 static const size_type __short_mask = 0x01;
709 static const size_type __long_mask = 0x1ul;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400710#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +0000711
Howard Hinnantc51e1022010-05-11 19:42:16 +0000712 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
713 (sizeof(__long) - 1)/sizeof(value_type) : 2};
714
715 struct __short
716 {
717 union
718 {
719 unsigned char __size_;
Howard Hinnant49e145e2012-10-30 19:06:59 +0000720 value_type __lx;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000721 };
722 value_type __data_[__min_cap];
723 };
724
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400725#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000726
Howard Hinnant8ea98242013-08-23 17:37:05 +0000727 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000728
Howard Hinnant8ea98242013-08-23 17:37:05 +0000729 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000730
731 struct __raw
732 {
733 size_type __words[__n_words];
734 };
735
736 struct __rep
737 {
738 union
739 {
740 __long __l;
741 __short __s;
742 __raw __r;
743 };
744 };
745
746 __compressed_pair<__rep, allocator_type> __r_;
747
Howard Hinnantc51e1022010-05-11 19:42:16 +0000748public:
Martin Storsjö88890c22021-02-22 01:13:13 +0200749 _LIBCPP_TEMPLATE_DATA_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000750 static const size_type npos = -1;
751
Howard Hinnant3e276872011-06-03 18:40:47 +0000752 _LIBCPP_INLINE_VISIBILITY basic_string()
753 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000754
755 _LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a)
756#if _LIBCPP_STD_VER <= 14
757 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
758#else
759 _NOEXCEPT;
760#endif
761
Howard Hinnantc51e1022010-05-11 19:42:16 +0000762 basic_string(const basic_string& __str);
763 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clowa80abc72015-06-03 19:56:43 +0000764
Eric Fiselierfc92be82017-04-19 00:28:44 +0000765#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000766 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000767 basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +0000768#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +0000769 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000770#else
771 _NOEXCEPT;
772#endif
773
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000774 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000775 basic_string(basic_string&& __str, const allocator_type& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400776#endif // _LIBCPP_CXX03_LANG
Marshall Clowe46031a2018-07-02 18:41:15 +0000777
Louis Dionne9ce598d2021-09-08 09:14:43 -0400778 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Eric Fiseliera8567862018-07-17 05:48:48 +0000779 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5169d1c2019-12-16 19:03:23 -0500780 basic_string(const _CharT* __s) : __r_(__default_init_tag(), __default_init_tag()) {
Eric Fiseliera8567862018-07-17 05:48:48 +0000781 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
782 __init(__s, traits_type::length(__s));
Nikolas Klauserf2807732022-01-11 00:33:35 +0100783 _VSTD::__debug_db_insert_c(this);
Eric Fiseliera8567862018-07-17 05:48:48 +0000784 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000785
Louis Dionne9ce598d2021-09-08 09:14:43 -0400786 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000787 _LIBCPP_INLINE_VISIBILITY
788 basic_string(const _CharT* __s, const _Allocator& __a);
789
Marek Kurdej90d79712021-07-27 16:16:21 +0200790#if _LIBCPP_STD_VER > 20
791 basic_string(nullptr_t) = delete;
792#endif
793
Howard Hinnantcf823322010-12-17 14:46:43 +0000794 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000795 basic_string(const _CharT* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +0000796 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000797 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000798 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000799 basic_string(size_type __n, _CharT __c);
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> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000802 _LIBCPP_INLINE_VISIBILITY
803 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
804
Marshall Clow83445802016-04-07 18:13:41 +0000805 basic_string(const basic_string& __str, size_type __pos, size_type __n,
Eric Fiselier812882b2017-02-17 01:17:10 +0000806 const _Allocator& __a = _Allocator());
Marshall Clow83445802016-04-07 18:13:41 +0000807 _LIBCPP_INLINE_VISIBILITY
808 basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +0000809 const _Allocator& __a = _Allocator());
Marshall Clowe46031a2018-07-02 18:41:15 +0000810
Louis Dionne9ce598d2021-09-08 09:14:43 -0400811 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 +0000812 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselier812882b2017-02-17 01:17:10 +0000813 basic_string(const _Tp& __t, size_type __pos, size_type __n,
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500814 const allocator_type& __a = allocator_type());
Marshall Clowe46031a2018-07-02 18:41:15 +0000815
Louis Dionne9ce598d2021-09-08 09:14:43 -0400816 template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500817 !__is_same_uncvref<_Tp, basic_string>::value> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000818 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
819 explicit basic_string(const _Tp& __t);
820
Louis Dionne9ce598d2021-09-08 09:14:43 -0400821 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 +0000822 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
823 explicit basic_string(const _Tp& __t, const allocator_type& __a);
824
Louis Dionne9ce598d2021-09-08 09:14:43 -0400825 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000826 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000827 basic_string(_InputIterator __first, _InputIterator __last);
Louis Dionne9ce598d2021-09-08 09:14:43 -0400828 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000829 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000830 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000831#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000832 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000833 basic_string(initializer_list<_CharT> __il);
Howard Hinnantcf823322010-12-17 14:46:43 +0000834 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000835 basic_string(initializer_list<_CharT> __il, const _Allocator& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400836#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000837
Eric Fiselierd9a702a2016-10-31 03:42:50 +0000838 inline ~basic_string();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000839
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000840 _LIBCPP_INLINE_VISIBILITY
841 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
842
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000843 basic_string& operator=(const basic_string& __str);
Eric Fiselier5ec13b12017-01-23 21:24:58 +0000844
Louis Dionne9ce598d2021-09-08 09:14:43 -0400845 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 +0000846 basic_string& operator=(const _Tp& __t)
847 {__self_view __sv = __t; return assign(__sv);}
848
Eric Fiselierfc92be82017-04-19 00:28:44 +0000849#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000850 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000851 basic_string& operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000852 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiselierfc92be82017-04-19 00:28:44 +0000853 _LIBCPP_INLINE_VISIBILITY
854 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000855#endif
Howard Hinnantd17880b2013-06-28 16:59:19 +0000856 _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const value_type* __s) {return assign(__s);}
Marek Kurdej90d79712021-07-27 16:16:21 +0200857#if _LIBCPP_STD_VER > 20
858 basic_string& operator=(nullptr_t) = delete;
859#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000860 basic_string& operator=(value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000861
Louis Dionneba400782020-10-02 15:02:52 -0400862#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +0000863 _LIBCPP_INLINE_VISIBILITY
864 iterator begin() _NOEXCEPT
865 {return iterator(this, __get_pointer());}
866 _LIBCPP_INLINE_VISIBILITY
867 const_iterator begin() const _NOEXCEPT
868 {return const_iterator(this, __get_pointer());}
869 _LIBCPP_INLINE_VISIBILITY
870 iterator end() _NOEXCEPT
871 {return iterator(this, __get_pointer() + size());}
872 _LIBCPP_INLINE_VISIBILITY
873 const_iterator end() const _NOEXCEPT
874 {return const_iterator(this, __get_pointer() + size());}
875#else
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000876 _LIBCPP_INLINE_VISIBILITY
877 iterator begin() _NOEXCEPT
878 {return iterator(__get_pointer());}
879 _LIBCPP_INLINE_VISIBILITY
880 const_iterator begin() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000881 {return const_iterator(__get_pointer());}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000882 _LIBCPP_INLINE_VISIBILITY
883 iterator end() _NOEXCEPT
884 {return iterator(__get_pointer() + size());}
885 _LIBCPP_INLINE_VISIBILITY
886 const_iterator end() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000887 {return const_iterator(__get_pointer() + size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400888#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000889 _LIBCPP_INLINE_VISIBILITY
890 reverse_iterator rbegin() _NOEXCEPT
891 {return reverse_iterator(end());}
892 _LIBCPP_INLINE_VISIBILITY
893 const_reverse_iterator rbegin() const _NOEXCEPT
894 {return const_reverse_iterator(end());}
895 _LIBCPP_INLINE_VISIBILITY
896 reverse_iterator rend() _NOEXCEPT
897 {return reverse_iterator(begin());}
898 _LIBCPP_INLINE_VISIBILITY
899 const_reverse_iterator rend() const _NOEXCEPT
900 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000901
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000902 _LIBCPP_INLINE_VISIBILITY
903 const_iterator cbegin() const _NOEXCEPT
904 {return begin();}
905 _LIBCPP_INLINE_VISIBILITY
906 const_iterator cend() const _NOEXCEPT
907 {return end();}
908 _LIBCPP_INLINE_VISIBILITY
909 const_reverse_iterator crbegin() const _NOEXCEPT
910 {return rbegin();}
911 _LIBCPP_INLINE_VISIBILITY
912 const_reverse_iterator crend() const _NOEXCEPT
913 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000914
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000915 _LIBCPP_INLINE_VISIBILITY size_type size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000916 {return __is_long() ? __get_long_size() : __get_short_size();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000917 _LIBCPP_INLINE_VISIBILITY size_type length() const _NOEXCEPT {return size();}
918 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT;
919 _LIBCPP_INLINE_VISIBILITY size_type capacity() const _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +0000920 {return (__is_long() ? __get_long_cap()
921 : static_cast<size_type>(__min_cap)) - 1;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000922
923 void resize(size_type __n, value_type __c);
924 _LIBCPP_INLINE_VISIBILITY void resize(size_type __n) {resize(__n, value_type());}
925
Marek Kurdejc9848142020-11-26 10:07:16 +0100926 void reserve(size_type __requested_capacity);
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100927
928#if _LIBCPP_STD_VER > 20
929 template <class _Op>
930 _LIBCPP_HIDE_FROM_ABI constexpr
931 void resize_and_overwrite(size_type __n, _Op __op) {
932 __resize_default_init(__n);
Nikolas Klausera4a76a12022-01-20 13:53:59 +0100933 __erase_to_end(_VSTD::move(__op)(data(), _LIBCPP_AUTO_CAST(__n)));
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100934 }
935#endif
936
Eric Fiselier451d5582018-11-26 20:15:38 +0000937 _LIBCPP_INLINE_VISIBILITY void __resize_default_init(size_type __n);
938
Marek Kurdejc9848142020-11-26 10:07:16 +0100939 _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_INLINE_VISIBILITY
940 void reserve() _NOEXCEPT {shrink_to_fit();}
Marshall Clow6ae12a82018-11-28 18:18:34 +0000941 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejc9848142020-11-26 10:07:16 +0100942 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000943 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000944 void clear() _NOEXCEPT;
Marshall Clowb7db4972017-11-15 20:02:27 +0000945 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
946 bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000947
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000948 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __pos) const _NOEXCEPT;
949 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000950
951 const_reference at(size_type __n) const;
952 reference at(size_type __n);
953
954 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const basic_string& __str) {return append(__str);}
Marshall Clowe46031a2018-07-02 18:41:15 +0000955
956 template <class _Tp>
957 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -0400958 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +0000959 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500960 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
961 && !__is_same_uncvref<_Tp, basic_string >::value,
Marshall Clowe46031a2018-07-02 18:41:15 +0000962 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500963 >
Marshall Clowe46031a2018-07-02 18:41:15 +0000964 operator+=(const _Tp& __t) {__self_view __sv = __t; return append(__sv);}
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000965 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const value_type* __s) {return append(__s);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000966 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(value_type __c) {push_back(__c); return *this;}
Eric Fiselierfc92be82017-04-19 00:28:44 +0000967#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000968 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(initializer_list<value_type> __il) {return append(__il);}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400969#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000970
Howard Hinnantcf823322010-12-17 14:46:43 +0000971 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000972 basic_string& append(const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000973
974 template <class _Tp>
975 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -0400976 __enable_if_t<
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500977 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
978 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clowe46031a2018-07-02 18:41:15 +0000979 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500980 >
Marshall Clowe46031a2018-07-02 18:41:15 +0000981 append(const _Tp& __t) { __self_view __sv = __t; return append(__sv.data(), __sv.size()); }
Marshall Clow8db7fb02014-03-04 19:17:19 +0000982 basic_string& append(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clowe46031a2018-07-02 18:41:15 +0000983
Marshall Clow62953962016-10-03 23:40:48 +0000984 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +0000985 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -0400986 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +0000987 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500988 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
989 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +0000990 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500991 >
Marshall Clow82513342016-09-24 22:45:42 +0000992 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +0000993 basic_string& append(const value_type* __s, size_type __n);
994 basic_string& append(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000995 basic_string& append(size_type __n, value_type __c);
Eric Fiselier451d5582018-11-26 20:15:38 +0000996
997 _LIBCPP_INLINE_VISIBILITY
998 void __append_default_init(size_type __n);
999
Howard Hinnantc51e1022010-05-11 19:42:16 +00001000 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001001 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001002 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001003 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001004 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001005 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001006 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001007 _LIBCPP_INLINE_VISIBILITY
1008 append(_InputIterator __first, _InputIterator __last) {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001009 const basic_string __temp(__first, __last, __alloc());
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001010 append(__temp.data(), __temp.size());
1011 return *this;
1012 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001013 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001014 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001015 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001016 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001017 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001018 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001019 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001020 _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001021 append(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001022
Eric Fiselierfc92be82017-04-19 00:28:44 +00001023#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001024 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001025 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001026#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001027
1028 void push_back(value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001029 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001030 void pop_back();
Marshall Clow05cf6692019-03-19 03:30:07 +00001031 _LIBCPP_INLINE_VISIBILITY reference front() _NOEXCEPT;
1032 _LIBCPP_INLINE_VISIBILITY const_reference front() const _NOEXCEPT;
1033 _LIBCPP_INLINE_VISIBILITY reference back() _NOEXCEPT;
1034 _LIBCPP_INLINE_VISIBILITY const_reference back() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001035
Marshall Clowe46031a2018-07-02 18:41:15 +00001036 template <class _Tp>
1037 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001038 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001039 <
1040 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1041 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001042 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001043 assign(const _Tp & __t) { __self_view __sv = __t; return assign(__sv.data(), __sv.size()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001044 _LIBCPP_INLINE_VISIBILITY
Marshall Clow95d5e9a2016-03-09 18:08:29 +00001045 basic_string& assign(const basic_string& __str) { return *this = __str; }
Eric Fiselierfc92be82017-04-19 00:28:44 +00001046#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001047 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001048 basic_string& assign(basic_string&& __str)
Marshall Clow5aa9e942015-10-05 16:17:34 +00001049 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001050 {*this = _VSTD::move(__str); return *this;}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001051#endif
Marshall Clow8db7fb02014-03-04 19:17:19 +00001052 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow62953962016-10-03 23:40:48 +00001053 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001054 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001055 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001056 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001057 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1058 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001059 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001060 >
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001061 assign(const _Tp & __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001062 basic_string& assign(const value_type* __s, size_type __n);
1063 basic_string& assign(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001064 basic_string& assign(size_type __n, value_type __c);
1065 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001066 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001067 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001068 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001069 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001070 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001071 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001072 assign(_InputIterator __first, _InputIterator __last);
1073 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001074 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001075 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001076 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001077 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001078 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001079 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001080 assign(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001081#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001082 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001083 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001084#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001085
Howard Hinnantcf823322010-12-17 14:46:43 +00001086 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001087 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001088
1089 template <class _Tp>
1090 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001091 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001092 <
1093 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1094 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001095 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001096 insert(size_type __pos1, const _Tp& __t)
1097 { __self_view __sv = __t; return insert(__pos1, __sv.data(), __sv.size()); }
1098
Marshall Clow82513342016-09-24 22:45:42 +00001099 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001100 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001101 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001102 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001103 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001104 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001105 >
Marshall Clow82513342016-09-24 22:45:42 +00001106 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Marshall Clow8db7fb02014-03-04 19:17:19 +00001107 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001108 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1109 basic_string& insert(size_type __pos, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001110 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1111 iterator insert(const_iterator __pos, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001112 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001113 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1114 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001115 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001116 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001117 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001118 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001119 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001120 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001121 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1122 template<class _ForwardIterator>
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
Howard Hinnantc51e1022010-05-11 19:42:16 +00001125 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001126 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001127 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001128 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001129 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001130#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001131 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001132 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1133 {return insert(__pos, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001134#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001135
1136 basic_string& erase(size_type __pos = 0, size_type __n = npos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001137 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001138 iterator erase(const_iterator __pos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001139 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001140 iterator erase(const_iterator __first, const_iterator __last);
1141
Howard Hinnantcf823322010-12-17 14:46:43 +00001142 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001143 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001144
1145 template <class _Tp>
1146 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001147 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001148 <
1149 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1150 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001151 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001152 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 +00001153 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 +00001154 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001155 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001156 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001157 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001158 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001159 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001160 >
Marshall Clow82513342016-09-24 22:45:42 +00001161 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001162 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
1163 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001164 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001165 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001166 basic_string& replace(const_iterator __i1, const_iterator __i2, 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(const_iterator __i1, const_iterator __i2, const _Tp& __t) { __self_view __sv = __t; return replace(__i1 - begin(), __i2 - __i1, __sv); }
1176
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001177 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001178 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +00001179 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001180 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Howard Hinnantcf823322010-12-17 14:46:43 +00001181 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001182 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001183 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001184 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001185 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001186 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001187 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001188 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001189 >
Howard Hinnant990d6e82010-11-17 21:11:40 +00001190 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001191#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001192 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001193 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001194 {return replace(__i1, __i2, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001195#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001196
Howard Hinnantd17880b2013-06-28 16:59:19 +00001197 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
Howard Hinnantcf823322010-12-17 14:46:43 +00001198 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001199 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1200
Howard Hinnantcf823322010-12-17 14:46:43 +00001201 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001202 void swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001203#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00001204 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +00001205#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001206 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001207 __is_nothrow_swappable<allocator_type>::value);
1208#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001209
Howard Hinnantcf823322010-12-17 14:46:43 +00001210 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001211 const value_type* c_str() const _NOEXCEPT {return data();}
Howard Hinnantcf823322010-12-17 14:46:43 +00001212 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001213 const value_type* data() const _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Eric Fiselier4ee2a892017-11-20 20:23:27 +00001214#if _LIBCPP_STD_VER > 14 || defined(_LIBCPP_BUILDING_LIBRARY)
Marshall Clowd3c22392016-03-08 15:44:30 +00001215 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001216 value_type* data() _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Marshall Clowd3c22392016-03-08 15:44:30 +00001217#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001218
Howard Hinnantcf823322010-12-17 14:46:43 +00001219 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001220 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001221
Howard Hinnantcf823322010-12-17 14:46:43 +00001222 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001223 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001224
1225 template <class _Tp>
1226 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001227 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001228 <
1229 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1230 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001231 >
zoecarver1997e0a2021-02-05 11:54:47 -08001232 find(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001233 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001234 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001235 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001236 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001237
Howard Hinnantcf823322010-12-17 14:46:43 +00001238 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001239 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001240
1241 template <class _Tp>
1242 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001243 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001244 <
1245 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1246 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001247 >
zoecarver1997e0a2021-02-05 11:54:47 -08001248 rfind(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001249 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001250 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001251 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001252 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001253
Howard Hinnantcf823322010-12-17 14:46:43 +00001254 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001255 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001256
1257 template <class _Tp>
1258 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001259 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001260 <
1261 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1262 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001263 >
zoecarver1997e0a2021-02-05 11:54:47 -08001264 find_first_of(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001265 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001266 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001267 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001268 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001269 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001270
Howard Hinnantcf823322010-12-17 14:46:43 +00001271 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001272 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001273
1274 template <class _Tp>
1275 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001276 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001277 <
1278 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1279 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001280 >
zoecarver1997e0a2021-02-05 11:54:47 -08001281 find_last_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001282 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001283 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001284 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001285 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001286 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001287
Howard Hinnantcf823322010-12-17 14:46:43 +00001288 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001289 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001290
1291 template <class _Tp>
1292 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001293 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001294 <
1295 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1296 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001297 >
zoecarver1997e0a2021-02-05 11:54:47 -08001298 find_first_not_of(const _Tp &__t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001299 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 +00001300 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001301 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001302 _LIBCPP_INLINE_VISIBILITY
1303 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1304
1305 _LIBCPP_INLINE_VISIBILITY
1306 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001307
1308 template <class _Tp>
1309 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001310 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001311 <
1312 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1313 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001314 >
zoecarver1997e0a2021-02-05 11:54:47 -08001315 find_last_not_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001316 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 +00001317 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001318 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001319 _LIBCPP_INLINE_VISIBILITY
1320 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1321
1322 _LIBCPP_INLINE_VISIBILITY
1323 int compare(const basic_string& __str) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001324
1325 template <class _Tp>
1326 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001327 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001328 <
1329 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1330 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001331 >
zoecarver1997e0a2021-02-05 11:54:47 -08001332 compare(const _Tp &__t) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001333
1334 template <class _Tp>
1335 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001336 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001337 <
1338 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1339 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001340 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001341 compare(size_type __pos1, size_type __n1, const _Tp& __t) const;
1342
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001343 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001344 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Marshall Clow8db7fb02014-03-04 19:17:19 +00001345 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 +00001346
Marshall Clow82513342016-09-24 22:45:42 +00001347 template <class _Tp>
Eric Fiselier7d4aa5c2016-10-14 05:29:46 +00001348 inline _LIBCPP_INLINE_VISIBILITY
Louis Dionne9ce598d2021-09-08 09:14:43 -04001349 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001350 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001351 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001352 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001353 >
Marshall Clow82513342016-09-24 22:45:42 +00001354 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 +00001355 int compare(const value_type* __s) const _NOEXCEPT;
1356 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1357 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001358
Marshall Clow18c293b2017-12-04 20:11:38 +00001359#if _LIBCPP_STD_VER > 17
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001360 constexpr _LIBCPP_INLINE_VISIBILITY
1361 bool starts_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001362 { return __self_view(data(), size()).starts_with(__sv); }
1363
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001364 constexpr _LIBCPP_INLINE_VISIBILITY
1365 bool starts_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001366 { return !empty() && _Traits::eq(front(), __c); }
1367
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001368 constexpr _LIBCPP_INLINE_VISIBILITY
1369 bool starts_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001370 { return starts_with(__self_view(__s)); }
1371
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001372 constexpr _LIBCPP_INLINE_VISIBILITY
1373 bool ends_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001374 { return __self_view(data(), size()).ends_with( __sv); }
1375
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001376 constexpr _LIBCPP_INLINE_VISIBILITY
1377 bool ends_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001378 { return !empty() && _Traits::eq(back(), __c); }
1379
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001380 constexpr _LIBCPP_INLINE_VISIBILITY
1381 bool ends_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001382 { return ends_with(__self_view(__s)); }
1383#endif
1384
Wim Leflere023c3542021-01-19 14:33:30 -05001385#if _LIBCPP_STD_VER > 20
1386 constexpr _LIBCPP_INLINE_VISIBILITY
1387 bool contains(__self_view __sv) const noexcept
1388 { return __self_view(data(), size()).contains(__sv); }
1389
1390 constexpr _LIBCPP_INLINE_VISIBILITY
1391 bool contains(value_type __c) const noexcept
1392 { return __self_view(data(), size()).contains(__c); }
1393
1394 constexpr _LIBCPP_INLINE_VISIBILITY
1395 bool contains(const value_type* __s) const
1396 { return __self_view(data(), size()).contains(__s); }
1397#endif
1398
Howard Hinnantcf823322010-12-17 14:46:43 +00001399 _LIBCPP_INLINE_VISIBILITY bool __invariants() const;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001400
Marshall Clowe60a7182018-05-29 17:04:37 +00001401 _LIBCPP_INLINE_VISIBILITY void __clear_and_shrink() _NOEXCEPT;
Marshall Clow851b9ec2019-05-20 21:56:51 +00001402
Marek Kurdejc9848142020-11-26 10:07:16 +01001403 _LIBCPP_INLINE_VISIBILITY void __shrink_or_extend(size_type __target_capacity);
1404
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001405 _LIBCPP_INLINE_VISIBILITY
1406 bool __is_long() const _NOEXCEPT
1407 {return bool(__r_.first().__s.__size_ & __short_mask);}
1408
Louis Dionneba400782020-10-02 15:02:52 -04001409#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001410
1411 bool __dereferenceable(const const_iterator* __i) const;
1412 bool __decrementable(const const_iterator* __i) const;
1413 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1414 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1415
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001416#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001417
Howard Hinnantc51e1022010-05-11 19:42:16 +00001418private:
Nikolas Klauser93826d12022-01-04 17:24:03 +01001419 _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI static bool __fits_in_sso(size_type __sz) {
1420 // SSO is disabled during constant evaluation because `__is_long` isn't constexpr friendly
1421 return !__libcpp_is_constant_evaluated() && (__sz < __min_cap);
1422 }
1423
Nikolas Klauser48d680d2022-02-26 13:28:33 +01001424 template <class _ForwardIterator>
1425 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
1426 iterator __insert_from_safe_copy(size_type __n, size_type __ip, _ForwardIterator __first, _ForwardIterator __last) {
1427 size_type __sz = size();
1428 size_type __cap = capacity();
1429 value_type* __p;
1430 if (__cap - __sz >= __n)
1431 {
1432 __p = std::__to_address(__get_pointer());
1433 size_type __n_move = __sz - __ip;
1434 if (__n_move != 0)
1435 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
1436 }
1437 else
1438 {
1439 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
1440 __p = std::__to_address(__get_long_pointer());
1441 }
1442 __sz += __n;
1443 __set_size(__sz);
1444 traits_type::assign(__p[__sz], value_type());
1445 for (__p += __ip; __first != __last; ++__p, ++__first)
1446 traits_type::assign(*__p, *__first);
1447
1448 return begin() + __ip;
1449 }
1450
1451 _LIBCPP_HIDE_FROM_ABI allocator_type& __alloc() _NOEXCEPT { return __r_.second(); }
1452 _LIBCPP_HIDE_FROM_ABI const allocator_type& __alloc() const _NOEXCEPT { return __r_.second(); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001453
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001454#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001455
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001456 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001457 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001458# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001459 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001460# else
1461 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1462# endif
1463
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001464 _LIBCPP_INLINE_VISIBILITY
1465 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001466# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001467 {return __r_.first().__s.__size_ >> 1;}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001468# else
1469 {return __r_.first().__s.__size_;}
1470# endif
1471
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001472#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001473
1474 _LIBCPP_INLINE_VISIBILITY
1475 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001476# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001477 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1478# else
1479 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
1480# endif
1481
1482 _LIBCPP_INLINE_VISIBILITY
1483 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001484# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001485 {return __r_.first().__s.__size_;}
1486# else
1487 {return __r_.first().__s.__size_ >> 1;}
1488# endif
1489
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001490#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001491
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001492 _LIBCPP_INLINE_VISIBILITY
1493 void __set_long_size(size_type __s) _NOEXCEPT
1494 {__r_.first().__l.__size_ = __s;}
1495 _LIBCPP_INLINE_VISIBILITY
1496 size_type __get_long_size() const _NOEXCEPT
1497 {return __r_.first().__l.__size_;}
1498 _LIBCPP_INLINE_VISIBILITY
1499 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001500 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1501
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001502 _LIBCPP_INLINE_VISIBILITY
1503 void __set_long_cap(size_type __s) _NOEXCEPT
1504 {__r_.first().__l.__cap_ = __long_mask | __s;}
1505 _LIBCPP_INLINE_VISIBILITY
1506 size_type __get_long_cap() const _NOEXCEPT
Howard Hinnant28b24882011-12-01 20:21:04 +00001507 {return __r_.first().__l.__cap_ & size_type(~__long_mask);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001508
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001509 _LIBCPP_INLINE_VISIBILITY
1510 void __set_long_pointer(pointer __p) _NOEXCEPT
1511 {__r_.first().__l.__data_ = __p;}
1512 _LIBCPP_INLINE_VISIBILITY
1513 pointer __get_long_pointer() _NOEXCEPT
1514 {return __r_.first().__l.__data_;}
1515 _LIBCPP_INLINE_VISIBILITY
1516 const_pointer __get_long_pointer() const _NOEXCEPT
1517 {return __r_.first().__l.__data_;}
1518 _LIBCPP_INLINE_VISIBILITY
1519 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001520 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001521 _LIBCPP_INLINE_VISIBILITY
1522 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001523 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001524 _LIBCPP_INLINE_VISIBILITY
1525 pointer __get_pointer() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001526 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001527 _LIBCPP_INLINE_VISIBILITY
1528 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001529 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1530
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001531 _LIBCPP_INLINE_VISIBILITY
1532 void __zero() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001533 {
1534 size_type (&__a)[__n_words] = __r_.first().__r.__words;
1535 for (unsigned __i = 0; __i < __n_words; ++__i)
1536 __a[__i] = 0;
1537 }
1538
1539 template <size_type __a> static
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001540 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00001541 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +00001542 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001543 enum {__alignment = 16};
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001544 static _LIBCPP_INLINE_VISIBILITY
1545 size_type __recommend(size_type __s) _NOEXCEPT
Marshall Clow80584522018-02-07 21:30:17 +00001546 {
1547 if (__s < __min_cap) return static_cast<size_type>(__min_cap) - 1;
1548 size_type __guess = __align_it<sizeof(value_type) < __alignment ?
1549 __alignment/sizeof(value_type) : 1 > (__s+1) - 1;
1550 if (__guess == __min_cap) ++__guess;
1551 return __guess;
1552 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001553
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001554 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001555 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001556 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001557 void __init(const value_type* __s, size_type __sz);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001558 inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001559 void __init(size_type __n, value_type __c);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001560
Martijn Vels5e7c9752020-03-04 17:52:46 -05001561 // Slow path for the (inlined) copy constructor for 'long' strings.
1562 // Always externally instantiated and not inlined.
1563 // Requires that __s is zero terminated.
1564 // The main reason for this function to exist is because for unstable, we
1565 // want to allow inlining of the copy constructor. However, we don't want
1566 // to call the __init() functions as those are marked as inline which may
1567 // result in over-aggressive inlining by the compiler, where our aim is
1568 // to only inline the fast path code directly in the ctor.
1569 void __init_copy_ctor_external(const value_type* __s, size_type __sz);
1570
Howard Hinnantc51e1022010-05-11 19:42:16 +00001571 template <class _InputIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001572 inline
Louis Dionne9ce598d2021-09-08 09:14:43 -04001573 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001574 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001575 __is_exactly_cpp17_input_iterator<_InputIterator>::value
1576 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001577 __init(_InputIterator __first, _InputIterator __last);
1578
1579 template <class _ForwardIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001580 inline
Louis Dionne9ce598d2021-09-08 09:14:43 -04001581 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001582 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001583 __is_cpp17_forward_iterator<_ForwardIterator>::value
1584 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001585 __init(_ForwardIterator __first, _ForwardIterator __last);
1586
1587 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001588 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001589 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1590 size_type __n_copy, size_type __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00001591 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001592
Martijn Vels596e3de2020-02-26 15:55:49 -05001593 // __assign_no_alias is invoked for assignment operations where we
1594 // have proof that the input does not alias the current instance.
1595 // For example, operator=(basic_string) performs a 'self' check.
1596 template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04001597 basic_string& __assign_no_alias(const value_type* __s, size_type __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05001598
Howard Hinnantcf823322010-12-17 14:46:43 +00001599 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001600 void __erase_to_end(size_type __pos);
1601
Martijn Velsa81fc792020-02-26 13:25:43 -05001602 // __erase_external_with_move is invoked for erase() invocations where
1603 // `n ~= npos`, likely requiring memory moves on the string data.
1604 void __erase_external_with_move(size_type __pos, size_type __n);
1605
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001606 _LIBCPP_INLINE_VISIBILITY
1607 void __copy_assign_alloc(const basic_string& __str)
1608 {__copy_assign_alloc(__str, integral_constant<bool,
1609 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1610
1611 _LIBCPP_INLINE_VISIBILITY
1612 void __copy_assign_alloc(const basic_string& __str, true_type)
1613 {
Marshall Clowf258c202017-01-31 03:40:52 +00001614 if (__alloc() == __str.__alloc())
1615 __alloc() = __str.__alloc();
1616 else
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001617 {
Marshall Clowf258c202017-01-31 03:40:52 +00001618 if (!__str.__is_long())
1619 {
Vedant Kumar55e007e2018-03-08 21:15:26 +00001620 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001621 __alloc() = __str.__alloc();
1622 }
1623 else
1624 {
1625 allocator_type __a = __str.__alloc();
1626 pointer __p = __alloc_traits::allocate(__a, __str.__get_long_cap());
Vedant Kumar55e007e2018-03-08 21:15:26 +00001627 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001628 __alloc() = _VSTD::move(__a);
1629 __set_long_pointer(__p);
1630 __set_long_cap(__str.__get_long_cap());
1631 __set_long_size(__str.size());
1632 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001633 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001634 }
1635
1636 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001637 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001638 {}
1639
Eric Fiselierfc92be82017-04-19 00:28:44 +00001640#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +00001641 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001642 void __move_assign(basic_string& __str, false_type)
1643 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnantcf823322010-12-17 14:46:43 +00001644 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001645 void __move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001646#if _LIBCPP_STD_VER > 14
1647 _NOEXCEPT;
1648#else
Howard Hinnant3e276872011-06-03 18:40:47 +00001649 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001650#endif
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001651#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001652
1653 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001654 void
Howard Hinnantc2734962011-09-02 20:42:31 +00001655 __move_assign_alloc(basic_string& __str)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001656 _NOEXCEPT_(
1657 !__alloc_traits::propagate_on_container_move_assignment::value ||
1658 is_nothrow_move_assignable<allocator_type>::value)
1659 {__move_assign_alloc(__str, integral_constant<bool,
1660 __alloc_traits::propagate_on_container_move_assignment::value>());}
1661
1662 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00001663 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001664 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1665 {
1666 __alloc() = _VSTD::move(__c.__alloc());
1667 }
1668
1669 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001670 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001671 _NOEXCEPT
1672 {}
1673
Martijn Velsda7d94f2020-06-19 14:24:03 -04001674 basic_string& __assign_external(const value_type* __s);
1675 basic_string& __assign_external(const value_type* __s, size_type __n);
1676
1677 // Assigns the value in __s, guaranteed to be __n < __min_cap in length.
1678 inline basic_string& __assign_short(const value_type* __s, size_type __n) {
1679 pointer __p = __is_long()
1680 ? (__set_long_size(__n), __get_long_pointer())
1681 : (__set_short_size(__n), __get_short_pointer());
1682 traits_type::move(_VSTD::__to_address(__p), __s, __n);
1683 traits_type::assign(__p[__n], value_type());
1684 return *this;
1685 }
1686
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01001687 _LIBCPP_HIDE_FROM_ABI basic_string& __null_terminate_at(value_type* __p, size_type __newsz) {
1688 __set_size(__newsz);
1689 __invalidate_iterators_past(__newsz);
1690 traits_type::assign(__p[__newsz], value_type());
1691 return *this;
1692 }
1693
Howard Hinnantcf823322010-12-17 14:46:43 +00001694 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
1695 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(size_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001696
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001697 template<class _Tp>
1698 _LIBCPP_INLINE_VISIBILITY
1699 bool __addr_in_range(_Tp&& __t) const {
1700 const volatile void *__p = _VSTD::addressof(__t);
1701 return data() <= __p && __p <= data() + size();
1702 }
1703
Louis Dionned24191c2021-08-19 12:39:16 -04001704 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1705 void __throw_length_error() const {
Nikolas Klauserf89bb932022-01-24 19:44:34 +01001706 _VSTD::__throw_length_error("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001707 }
1708
1709 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1710 void __throw_out_of_range() const {
Nikolas Klauserf89bb932022-01-24 19:44:34 +01001711 _VSTD::__throw_out_of_range("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001712 }
1713
Howard Hinnantc51e1022010-05-11 19:42:16 +00001714 friend basic_string operator+<>(const basic_string&, const basic_string&);
1715 friend basic_string operator+<>(const value_type*, const basic_string&);
1716 friend basic_string operator+<>(value_type, const basic_string&);
1717 friend basic_string operator+<>(const basic_string&, const value_type*);
1718 friend basic_string operator+<>(const basic_string&, value_type);
1719};
1720
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001721// These declarations must appear before any functions are implicitly used
1722// so that they have the correct visibility specifier.
1723#ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
Louis Dionne89258142021-08-23 15:32:36 -04001724 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1725# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1726 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1727# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001728#else
Louis Dionne89258142021-08-23 15:32:36 -04001729 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1730# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1731 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1732# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001733#endif
1734
1735
Louis Dionned59f8a52021-08-17 11:59:07 -04001736#if _LIBCPP_STD_VER >= 17
Marshall Clowa0563332018-02-08 06:34:03 +00001737template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -05001738 class _CharT = __iter_value_type<_InputIterator>,
Marshall Clowa0563332018-02-08 06:34:03 +00001739 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001740 class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
1741 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowa0563332018-02-08 06:34:03 +00001742 >
1743basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
1744 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
Marshall Clowe46031a2018-07-02 18:41:15 +00001745
1746template<class _CharT,
1747 class _Traits,
1748 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001749 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowe46031a2018-07-02 18:41:15 +00001750 >
1751explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
1752 -> basic_string<_CharT, _Traits, _Allocator>;
1753
1754template<class _CharT,
1755 class _Traits,
1756 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001757 class = enable_if_t<__is_allocator<_Allocator>::value>,
Marshall Clowe46031a2018-07-02 18:41:15 +00001758 class _Sz = typename allocator_traits<_Allocator>::size_type
1759 >
1760basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
1761 -> basic_string<_CharT, _Traits, _Allocator>;
Marshall Clowa0563332018-02-08 06:34:03 +00001762#endif
1763
Howard Hinnantc51e1022010-05-11 19:42:16 +00001764template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001765inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001766void
1767basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
1768{
Louis Dionneba400782020-10-02 15:02:52 -04001769#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001770 if (!__libcpp_is_constant_evaluated())
1771 __get_db()->__invalidate_all(this);
Louis Dionneba400782020-10-02 15:02:52 -04001772#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001773}
1774
1775template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001776inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001777void
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001778basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type __pos)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001779{
Louis Dionneba400782020-10-02 15:02:52 -04001780#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001781 if (!__libcpp_is_constant_evaluated()) {
1782 __c_node* __c = __get_db()->__find_c_and_lock(this);
1783 if (__c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001784 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001785 const_pointer __new_last = __get_pointer() + __pos;
1786 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001787 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001788 --__p;
1789 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1790 if (__i->base() > __new_last)
1791 {
1792 (*__p)->__c_ = nullptr;
1793 if (--__c->end_ != __p)
1794 _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1795 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001796 }
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001797 __get_db()->unlock();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001798 }
1799 }
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001800#else
1801 (void)__pos;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001802#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001803}
1804
1805template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001806inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001807basic_string<_CharT, _Traits, _Allocator>::basic_string()
Marshall Clowe546cbb2015-06-04 02:05:41 +00001808 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001809 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001810{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001811 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001812 __zero();
1813}
1814
1815template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001816inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001817basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
Eric Fiseliere012bf22015-07-18 20:40:46 +00001818#if _LIBCPP_STD_VER <= 14
1819 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1820#else
1821 _NOEXCEPT
1822#endif
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001823: __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001824{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001825 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001826 __zero();
1827}
1828
1829template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier815ed732016-09-16 00:00:48 +00001830void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1831 size_type __sz,
1832 size_type __reserve)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001833{
1834 if (__reserve > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001835 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001836 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001837 if (__fits_in_sso(__reserve))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001838 {
1839 __set_short_size(__sz);
1840 __p = __get_short_pointer();
1841 }
1842 else
1843 {
1844 size_type __cap = __recommend(__reserve);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001845 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001846 __set_long_pointer(__p);
1847 __set_long_cap(__cap+1);
1848 __set_long_size(__sz);
1849 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001850 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001851 traits_type::assign(__p[__sz], value_type());
1852}
1853
1854template <class _CharT, class _Traits, class _Allocator>
1855void
Howard Hinnantd17880b2013-06-28 16:59:19 +00001856basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001857{
1858 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001859 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001860 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001861 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001862 {
1863 __set_short_size(__sz);
1864 __p = __get_short_pointer();
1865 }
1866 else
1867 {
1868 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001869 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001870 __set_long_pointer(__p);
1871 __set_long_cap(__cap+1);
1872 __set_long_size(__sz);
1873 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001874 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001875 traits_type::assign(__p[__sz], value_type());
1876}
1877
1878template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00001879template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00001880basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001881 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001882{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001883 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001884 __init(__s, traits_type::length(__s));
Nikolas Klauserf2807732022-01-11 00:33:35 +01001885 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001886}
1887
1888template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001889inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001890basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001891 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001892{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001893 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
1894 __init(__s, __n);
1895 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001896}
1897
1898template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001899inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001900basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001901 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001902{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001903 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001904 __init(__s, __n);
Nikolas Klauserf2807732022-01-11 00:33:35 +01001905 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001906}
1907
1908template <class _CharT, class _Traits, class _Allocator>
1909basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001910 : __r_(__default_init_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001911{
1912 if (!__str.__is_long())
1913 __r_.first().__r = __str.__r_.first().__r;
1914 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001915 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1916 __str.__get_long_size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01001917 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001918}
1919
1920template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00001921basic_string<_CharT, _Traits, _Allocator>::basic_string(
1922 const basic_string& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001923 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001924{
1925 if (!__str.__is_long())
1926 __r_.first().__r = __str.__r_.first().__r;
1927 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001928 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1929 __str.__get_long_size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01001930 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001931}
1932
Martijn Vels5e7c9752020-03-04 17:52:46 -05001933template <class _CharT, class _Traits, class _Allocator>
1934void basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(
1935 const value_type* __s, size_type __sz) {
1936 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001937 if (__fits_in_sso(__sz)) {
Martijn Vels5e7c9752020-03-04 17:52:46 -05001938 __p = __get_short_pointer();
1939 __set_short_size(__sz);
1940 } else {
1941 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001942 __throw_length_error();
Martijn Vels5e7c9752020-03-04 17:52:46 -05001943 size_t __cap = __recommend(__sz);
1944 __p = __alloc_traits::allocate(__alloc(), __cap + 1);
1945 __set_long_pointer(__p);
1946 __set_long_cap(__cap + 1);
1947 __set_long_size(__sz);
1948 }
1949 traits_type::copy(_VSTD::__to_address(__p), __s, __sz + 1);
1950}
1951
Eric Fiselierfc92be82017-04-19 00:28:44 +00001952#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001953
1954template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001955inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001956basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +00001957#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +00001958 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowa80abc72015-06-03 19:56:43 +00001959#else
1960 _NOEXCEPT
1961#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001962 : __r_(_VSTD::move(__str.__r_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001963{
1964 __str.__zero();
Nikolas Klauserf2807732022-01-11 00:33:35 +01001965 _VSTD::__debug_db_insert_c(this);
Louis Dionneba400782020-10-02 15:02:52 -04001966#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauserf2807732022-01-11 00:33:35 +01001967 if (!__libcpp_is_constant_evaluated() && __is_long())
1968 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001969#endif
1970}
1971
1972template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001973inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001974basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001975 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001976{
Marshall Clowd2d24692014-07-17 15:32:20 +00001977 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001978 __init(_VSTD::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd2d24692014-07-17 15:32:20 +00001979 else
1980 {
1981 __r_.first().__r = __str.__r_.first().__r;
1982 __str.__zero();
1983 }
Nikolas Klauserf2807732022-01-11 00:33:35 +01001984 _VSTD::__debug_db_insert_c(this);
Louis Dionneba400782020-10-02 15:02:52 -04001985#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauserf2807732022-01-11 00:33:35 +01001986 if (!__libcpp_is_constant_evaluated() && __is_long())
1987 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001988#endif
1989}
1990
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001991#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001992
1993template <class _CharT, class _Traits, class _Allocator>
1994void
1995basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
1996{
1997 if (__n > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001998 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001999 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002000 if (__fits_in_sso(__n))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002001 {
2002 __set_short_size(__n);
2003 __p = __get_short_pointer();
2004 }
2005 else
2006 {
2007 size_type __cap = __recommend(__n);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002008 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002009 __set_long_pointer(__p);
2010 __set_long_cap(__cap+1);
2011 __set_long_size(__n);
2012 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002013 traits_type::assign(_VSTD::__to_address(__p), __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002014 traits_type::assign(__p[__n], value_type());
2015}
2016
2017template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002018inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002019basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002020 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002021{
2022 __init(__n, __c);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002023 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002024}
2025
2026template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002027template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00002028basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002029 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002030{
2031 __init(__n, __c);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002032 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002033}
2034
Howard Hinnantc51e1022010-05-11 19:42:16 +00002035template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00002036basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
2037 size_type __pos, size_type __n,
2038 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002039 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002040{
2041 size_type __str_sz = __str.size();
2042 if (__pos > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002043 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002044 __init(__str.data() + __pos, _VSTD::min(__n, __str_sz - __pos));
Nikolas Klauserf2807732022-01-11 00:33:35 +01002045 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002046}
2047
2048template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002049inline
Marshall Clow83445802016-04-07 18:13:41 +00002050basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +00002051 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002052 : __r_(__default_init_tag(), __a)
Marshall Clow83445802016-04-07 18:13:41 +00002053{
2054 size_type __str_sz = __str.size();
2055 if (__pos > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002056 __throw_out_of_range();
Marshall Clow83445802016-04-07 18:13:41 +00002057 __init(__str.data() + __pos, __str_sz - __pos);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002058 _VSTD::__debug_db_insert_c(this);
Marshall Clow83445802016-04-07 18:13:41 +00002059}
2060
2061template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002062template <class _Tp, class>
Marshall Clow78dbe462016-11-14 18:22:19 +00002063basic_string<_CharT, _Traits, _Allocator>::basic_string(
Marshall Clowe46031a2018-07-02 18:41:15 +00002064 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002065 : __r_(__default_init_tag(), __a)
Marshall Clow78dbe462016-11-14 18:22:19 +00002066{
Marshall Clowe46031a2018-07-02 18:41:15 +00002067 __self_view __sv0 = __t;
2068 __self_view __sv = __sv0.substr(__pos, __n);
Marshall Clow78dbe462016-11-14 18:22:19 +00002069 __init(__sv.data(), __sv.size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002070 _VSTD::__debug_db_insert_c(this);
Marshall Clow78dbe462016-11-14 18:22:19 +00002071}
2072
2073template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002074template <class _Tp, class>
2075basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002076 : __r_(__default_init_tag(), __default_init_tag())
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002077{
Marshall Clowe46031a2018-07-02 18:41:15 +00002078 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002079 __init(__sv.data(), __sv.size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002080 _VSTD::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002081}
2082
2083template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002084template <class _Tp, class>
2085basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002086 : __r_(__default_init_tag(), __a)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002087{
Marshall Clowe46031a2018-07-02 18:41:15 +00002088 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002089 __init(__sv.data(), __sv.size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002090 _VSTD::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002091}
2092
2093template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002094template <class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002095__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002096<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002097 __is_exactly_cpp17_input_iterator<_InputIterator>::value
2098>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002099basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
2100{
2101 __zero();
2102#ifndef _LIBCPP_NO_EXCEPTIONS
2103 try
2104 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002105#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002106 for (; __first != __last; ++__first)
2107 push_back(*__first);
2108#ifndef _LIBCPP_NO_EXCEPTIONS
2109 }
2110 catch (...)
2111 {
2112 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002113 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002114 throw;
2115 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002116#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002117}
2118
2119template <class _CharT, class _Traits, class _Allocator>
2120template <class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002121__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002122<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002123 __is_cpp17_forward_iterator<_ForwardIterator>::value
2124>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002125basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
2126{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002127 size_type __sz = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002128 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002129 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002130 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002131 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002132 {
2133 __set_short_size(__sz);
2134 __p = __get_short_pointer();
2135 }
2136 else
2137 {
2138 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002139 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002140 __set_long_pointer(__p);
2141 __set_long_cap(__cap+1);
2142 __set_long_size(__sz);
2143 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002144
2145#ifndef _LIBCPP_NO_EXCEPTIONS
2146 try
2147 {
2148#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002149 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002150 traits_type::assign(*__p, *__first);
2151 traits_type::assign(*__p, value_type());
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002152#ifndef _LIBCPP_NO_EXCEPTIONS
2153 }
2154 catch (...)
2155 {
2156 if (__is_long())
2157 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
2158 throw;
2159 }
2160#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002161}
2162
2163template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002164template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002165inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002166basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002167 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002168{
2169 __init(__first, __last);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002170 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002171}
2172
2173template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002174template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002175inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002176basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
2177 const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002178 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002179{
2180 __init(__first, __last);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002181 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002182}
2183
Eric Fiselierfc92be82017-04-19 00:28:44 +00002184#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002185
Howard Hinnantc51e1022010-05-11 19:42:16 +00002186template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002187inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002188basic_string<_CharT, _Traits, _Allocator>::basic_string(
2189 initializer_list<_CharT> __il)
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(__il.begin(), __il.end());
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 Fiselierbea70602018-11-26 22:51:35 +00002197inline
Eric Fiselier9d355982017-04-12 23:45:53 +00002198
Eric Fiselier812882b2017-02-17 01:17:10 +00002199basic_string<_CharT, _Traits, _Allocator>::basic_string(
2200 initializer_list<_CharT> __il, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002201 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002202{
2203 __init(__il.begin(), __il.end());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002204 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002205}
2206
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002207#endif // _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>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002210basic_string<_CharT, _Traits, _Allocator>::~basic_string()
2211{
Louis Dionneba400782020-10-02 15:02:52 -04002212#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002213 if (!__libcpp_is_constant_evaluated())
2214 __get_db()->__erase_c(this);
Howard Hinnant8ea98242013-08-23 17:37:05 +00002215#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002216 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002217 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002218}
2219
2220template <class _CharT, class _Traits, class _Allocator>
2221void
2222basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
2223 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002224 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 +00002225{
2226 size_type __ms = max_size();
2227 if (__delta_cap > __ms - __old_cap - 1)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002228 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002229 pointer __old_p = __get_pointer();
2230 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002231 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002232 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002233 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002234 __invalidate_all_iterators();
2235 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002236 traits_type::copy(_VSTD::__to_address(__p),
2237 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002238 if (__n_add != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002239 traits_type::copy(_VSTD::__to_address(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002240 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2241 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002242 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2243 _VSTD::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002244 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002245 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002246 __set_long_pointer(__p);
2247 __set_long_cap(__cap+1);
2248 __old_sz = __n_copy + __n_add + __sec_cp_sz;
2249 __set_long_size(__old_sz);
2250 traits_type::assign(__p[__old_sz], value_type());
2251}
2252
2253template <class _CharT, class _Traits, class _Allocator>
2254void
2255basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2256 size_type __n_copy, size_type __n_del, size_type __n_add)
2257{
2258 size_type __ms = max_size();
Marshall Clow2267c5d2013-11-06 14:24:38 +00002259 if (__delta_cap > __ms - __old_cap)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002260 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002261 pointer __old_p = __get_pointer();
2262 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002263 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002264 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002265 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002266 __invalidate_all_iterators();
2267 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002268 traits_type::copy(_VSTD::__to_address(__p),
2269 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002270 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2271 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002272 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2273 _VSTD::__to_address(__old_p) + __n_copy + __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002274 __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002275 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002276 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002277 __set_long_pointer(__p);
2278 __set_long_cap(__cap+1);
2279}
2280
2281// assign
2282
2283template <class _CharT, class _Traits, class _Allocator>
Martijn Vels596e3de2020-02-26 15:55:49 -05002284template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04002285basic_string<_CharT, _Traits, _Allocator>&
2286basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(
Martijn Vels596e3de2020-02-26 15:55:49 -05002287 const value_type* __s, size_type __n) {
Louis Dionne6209e9f2022-03-03 13:39:12 -05002288 size_type __cap = __is_short ? static_cast<size_type>(__min_cap) : __get_long_cap();
Martijn Vels596e3de2020-02-26 15:55:49 -05002289 if (__n < __cap) {
2290 pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
2291 __is_short ? __set_short_size(__n) : __set_long_size(__n);
2292 traits_type::copy(_VSTD::__to_address(__p), __s, __n);
2293 traits_type::assign(__p[__n], value_type());
2294 __invalidate_iterators_past(__n);
2295 } else {
2296 size_type __sz = __is_short ? __get_short_size() : __get_long_size();
2297 __grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
2298 }
Martijn Velsb6a08b62020-04-10 18:36:31 -04002299 return *this;
Martijn Vels596e3de2020-02-26 15:55:49 -05002300}
2301
2302template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002303basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002304basic_string<_CharT, _Traits, _Allocator>::__assign_external(
2305 const value_type* __s, size_type __n) {
2306 size_type __cap = capacity();
2307 if (__cap >= __n) {
2308 value_type* __p = _VSTD::__to_address(__get_pointer());
2309 traits_type::move(__p, __s, __n);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002310 return __null_terminate_at(__p, __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04002311 } else {
2312 size_type __sz = size();
2313 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002314 return *this;
Martijn Velsda7d94f2020-06-19 14:24:03 -04002315 }
Martijn Velsda7d94f2020-06-19 14:24:03 -04002316}
2317
2318template <class _CharT, class _Traits, class _Allocator>
2319basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002320basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002321{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002322 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Nikolas Klauser93826d12022-01-04 17:24:03 +01002323 return (__builtin_constant_p(__n) && __fits_in_sso(__n))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002324 ? __assign_short(__s, __n)
2325 : __assign_external(__s, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002326}
2327
2328template <class _CharT, class _Traits, class _Allocator>
2329basic_string<_CharT, _Traits, _Allocator>&
2330basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2331{
2332 size_type __cap = capacity();
2333 if (__cap < __n)
2334 {
2335 size_type __sz = size();
2336 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2337 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002338 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002339 traits_type::assign(__p, __n, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002340 return __null_terminate_at(__p, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002341}
2342
2343template <class _CharT, class _Traits, class _Allocator>
2344basic_string<_CharT, _Traits, _Allocator>&
2345basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2346{
2347 pointer __p;
2348 if (__is_long())
2349 {
2350 __p = __get_long_pointer();
2351 __set_long_size(1);
2352 }
2353 else
2354 {
2355 __p = __get_short_pointer();
2356 __set_short_size(1);
2357 }
2358 traits_type::assign(*__p, __c);
2359 traits_type::assign(*++__p, value_type());
2360 __invalidate_iterators_past(1);
2361 return *this;
2362}
2363
2364template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002365basic_string<_CharT, _Traits, _Allocator>&
2366basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2367{
Martijn Vels596e3de2020-02-26 15:55:49 -05002368 if (this != &__str) {
2369 __copy_assign_alloc(__str);
2370 if (!__is_long()) {
2371 if (!__str.__is_long()) {
Eric Fiselier03bbc922020-01-15 17:27:10 -05002372 __r_.first().__r = __str.__r_.first().__r;
Martijn Vels596e3de2020-02-26 15:55:49 -05002373 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002374 return __assign_no_alias<true>(__str.data(), __str.size());
Martijn Vels596e3de2020-02-26 15:55:49 -05002375 }
2376 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002377 return __assign_no_alias<false>(__str.data(), __str.size());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002378 }
Martijn Vels596e3de2020-02-26 15:55:49 -05002379 }
2380 return *this;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002381}
2382
Eric Fiselierfc92be82017-04-19 00:28:44 +00002383#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002384
2385template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002386inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002387void
2388basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002389 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002390{
2391 if (__alloc() != __str.__alloc())
2392 assign(__str);
2393 else
2394 __move_assign(__str, true_type());
2395}
2396
2397template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002398inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002399void
2400basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002401#if _LIBCPP_STD_VER > 14
2402 _NOEXCEPT
2403#else
Howard Hinnant3e276872011-06-03 18:40:47 +00002404 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002405#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002406{
marshall2ed41622019-11-27 07:13:00 -08002407 if (__is_long()) {
2408 __alloc_traits::deallocate(__alloc(), __get_long_pointer(),
2409 __get_long_cap());
2410#if _LIBCPP_STD_VER <= 14
2411 if (!is_nothrow_move_assignable<allocator_type>::value) {
2412 __set_short_size(0);
2413 traits_type::assign(__get_short_pointer()[0], value_type());
2414 }
2415#endif
2416 }
2417 __move_assign_alloc(__str);
2418 __r_.first() = __str.__r_.first();
2419 __str.__set_short_size(0);
2420 traits_type::assign(__str.__get_short_pointer()[0], value_type());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002421}
2422
2423template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002424inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002425basic_string<_CharT, _Traits, _Allocator>&
2426basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002427 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002428{
2429 __move_assign(__str, integral_constant<bool,
2430 __alloc_traits::propagate_on_container_move_assignment::value>());
2431 return *this;
2432}
2433
2434#endif
2435
2436template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002437template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002438__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002439<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002440 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002441 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002442>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002443basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2444{
Marshall Clow958362f2016-09-05 01:54:30 +00002445 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002446 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002447 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002448}
2449
2450template <class _CharT, class _Traits, class _Allocator>
2451template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002452__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002453<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002454 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002455 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002456>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002457basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2458{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002459 size_type __cap = capacity();
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002460 size_type __n = __string_is_trivial_iterator<_ForwardIterator>::value ?
2461 static_cast<size_type>(_VSTD::distance(__first, __last)) : 0;
2462
2463 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2464 (__cap >= __n || !__addr_in_range(*__first)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002465 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002466 if (__cap < __n)
2467 {
2468 size_type __sz = size();
2469 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2470 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002471 pointer __p = __get_pointer();
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002472 for (; __first != __last; ++__p, (void) ++__first)
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002473 traits_type::assign(*__p, *__first);
2474 traits_type::assign(*__p, value_type());
2475 __set_size(__n);
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04002476 __invalidate_iterators_past(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002477 }
2478 else
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002479 {
2480 const basic_string __temp(__first, __last, __alloc());
2481 assign(__temp.data(), __temp.size());
2482 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002483 return *this;
2484}
2485
2486template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002487basic_string<_CharT, _Traits, _Allocator>&
2488basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2489{
2490 size_type __sz = __str.size();
2491 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002492 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002493 return assign(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002494}
2495
2496template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002497template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002498__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002499<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002500 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
2501 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00002502 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002503>
Marshall Clow82513342016-09-24 22:45:42 +00002504basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002505{
Marshall Clow82513342016-09-24 22:45:42 +00002506 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002507 size_type __sz = __sv.size();
2508 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002509 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002510 return assign(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2511}
2512
2513
2514template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002515basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002516basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s) {
2517 return __assign_external(__s, traits_type::length(__s));
2518}
2519
2520template <class _CharT, class _Traits, class _Allocator>
2521basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002522basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002523{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002524 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Louis Dionne3e0c4332021-08-31 10:49:06 -04002525 return __builtin_constant_p(*__s)
Nikolas Klauser93826d12022-01-04 17:24:03 +01002526 ? (__fits_in_sso(traits_type::length(__s))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002527 ? __assign_short(__s, traits_type::length(__s))
2528 : __assign_external(__s, traits_type::length(__s)))
2529 : __assign_external(__s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002530}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002531// append
2532
2533template <class _CharT, class _Traits, class _Allocator>
2534basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002535basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002536{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002537 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002538 size_type __cap = capacity();
2539 size_type __sz = size();
2540 if (__cap - __sz >= __n)
2541 {
2542 if (__n)
2543 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002544 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002545 traits_type::copy(__p + __sz, __s, __n);
2546 __sz += __n;
2547 __set_size(__sz);
2548 traits_type::assign(__p[__sz], value_type());
2549 }
2550 }
2551 else
2552 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2553 return *this;
2554}
2555
2556template <class _CharT, class _Traits, class _Allocator>
2557basic_string<_CharT, _Traits, _Allocator>&
2558basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2559{
2560 if (__n)
2561 {
2562 size_type __cap = capacity();
2563 size_type __sz = size();
2564 if (__cap - __sz < __n)
2565 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2566 pointer __p = __get_pointer();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002567 traits_type::assign(_VSTD::__to_address(__p) + __sz, __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002568 __sz += __n;
2569 __set_size(__sz);
2570 traits_type::assign(__p[__sz], value_type());
2571 }
2572 return *this;
2573}
2574
2575template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00002576inline void
2577basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n)
2578{
2579 if (__n)
2580 {
2581 size_type __cap = capacity();
2582 size_type __sz = size();
2583 if (__cap - __sz < __n)
2584 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2585 pointer __p = __get_pointer();
2586 __sz += __n;
2587 __set_size(__sz);
2588 traits_type::assign(__p[__sz], value_type());
2589 }
2590}
2591
2592template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002593void
2594basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2595{
Howard Hinnant68bf1812013-04-30 21:44:48 +00002596 bool __is_short = !__is_long();
2597 size_type __cap;
2598 size_type __sz;
2599 if (__is_short)
2600 {
2601 __cap = __min_cap - 1;
2602 __sz = __get_short_size();
2603 }
2604 else
2605 {
2606 __cap = __get_long_cap() - 1;
2607 __sz = __get_long_size();
2608 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002609 if (__sz == __cap)
Howard Hinnant68bf1812013-04-30 21:44:48 +00002610 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002611 __grow_by(__cap, 1, __sz, __sz, 0);
Louis Dionne82d2b2c2021-11-16 11:26:56 -05002612 __is_short = false; // the string is always long after __grow_by
Howard Hinnant68bf1812013-04-30 21:44:48 +00002613 }
2614 pointer __p;
2615 if (__is_short)
2616 {
2617 __p = __get_short_pointer() + __sz;
2618 __set_short_size(__sz+1);
2619 }
2620 else
2621 {
2622 __p = __get_long_pointer() + __sz;
2623 __set_long_size(__sz+1);
2624 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002625 traits_type::assign(*__p, __c);
2626 traits_type::assign(*++__p, value_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002627}
2628
Howard Hinnantc51e1022010-05-11 19:42:16 +00002629template <class _CharT, class _Traits, class _Allocator>
2630template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002631__enable_if_t
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002632<
2633 __is_cpp17_forward_iterator<_ForwardIterator>::value,
2634 basic_string<_CharT, _Traits, _Allocator>&
2635>
2636basic_string<_CharT, _Traits, _Allocator>::append(
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002637 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002638{
2639 size_type __sz = size();
2640 size_type __cap = capacity();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002641 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002642 if (__n)
2643 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002644 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2645 !__addr_in_range(*__first))
Marshall Clow958362f2016-09-05 01:54:30 +00002646 {
2647 if (__cap - __sz < __n)
2648 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2649 pointer __p = __get_pointer() + __sz;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002650 for (; __first != __last; ++__p, (void) ++__first)
Marshall Clow958362f2016-09-05 01:54:30 +00002651 traits_type::assign(*__p, *__first);
2652 traits_type::assign(*__p, value_type());
2653 __set_size(__sz + __n);
2654 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002655 else
2656 {
2657 const basic_string __temp(__first, __last, __alloc());
2658 append(__temp.data(), __temp.size());
2659 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002660 }
2661 return *this;
2662}
2663
2664template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002665inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002666basic_string<_CharT, _Traits, _Allocator>&
2667basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2668{
2669 return append(__str.data(), __str.size());
2670}
2671
2672template <class _CharT, class _Traits, class _Allocator>
2673basic_string<_CharT, _Traits, _Allocator>&
2674basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2675{
2676 size_type __sz = __str.size();
2677 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002678 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002679 return append(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002680}
2681
2682template <class _CharT, class _Traits, class _Allocator>
Marshall Clow62953962016-10-03 23:40:48 +00002683template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002684 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002685 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002686 __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 +00002687 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002688 >
Marshall Clow82513342016-09-24 22:45:42 +00002689basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002690{
Marshall Clow82513342016-09-24 22:45:42 +00002691 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002692 size_type __sz = __sv.size();
2693 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002694 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002695 return append(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2696}
2697
2698template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002699basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002700basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002701{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002702 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002703 return append(__s, traits_type::length(__s));
2704}
2705
2706// insert
2707
2708template <class _CharT, class _Traits, class _Allocator>
2709basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002710basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002711{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002712 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002713 size_type __sz = size();
2714 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002715 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002716 size_type __cap = capacity();
2717 if (__cap - __sz >= __n)
2718 {
2719 if (__n)
2720 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002721 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002722 size_type __n_move = __sz - __pos;
2723 if (__n_move != 0)
2724 {
2725 if (__p + __pos <= __s && __s < __p + __sz)
2726 __s += __n;
2727 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2728 }
2729 traits_type::move(__p + __pos, __s, __n);
2730 __sz += __n;
2731 __set_size(__sz);
2732 traits_type::assign(__p[__sz], value_type());
2733 }
2734 }
2735 else
2736 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2737 return *this;
2738}
2739
2740template <class _CharT, class _Traits, class _Allocator>
2741basic_string<_CharT, _Traits, _Allocator>&
2742basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2743{
2744 size_type __sz = size();
2745 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002746 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002747 if (__n)
2748 {
2749 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002750 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002751 if (__cap - __sz >= __n)
2752 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002753 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002754 size_type __n_move = __sz - __pos;
2755 if (__n_move != 0)
2756 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2757 }
2758 else
2759 {
2760 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002761 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002762 }
2763 traits_type::assign(__p + __pos, __n, __c);
2764 __sz += __n;
2765 __set_size(__sz);
2766 traits_type::assign(__p[__sz], value_type());
2767 }
2768 return *this;
2769}
2770
2771template <class _CharT, class _Traits, class _Allocator>
2772template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002773__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002774<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002775 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Marshall Clow039b2f02016-01-13 21:54:34 +00002776 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002777>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002778basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2779{
Nikolas Klausereed25832021-12-15 01:32:30 +01002780 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2781 "string::insert(iterator, range) called with an iterator not"
2782 " referring to this string");
2783 const basic_string __temp(__first, __last, __alloc());
2784 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002785}
2786
2787template <class _CharT, class _Traits, class _Allocator>
2788template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002789__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002790<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002791 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002792 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002793>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002794basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2795{
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002796 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2797 "string::insert(iterator, range) called with an iterator not referring to this string");
Nikolas Klausereed25832021-12-15 01:32:30 +01002798
Howard Hinnantc51e1022010-05-11 19:42:16 +00002799 size_type __ip = static_cast<size_type>(__pos - begin());
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002800 size_type __n = static_cast<size_type>(std::distance(__first, __last));
2801 if (__n == 0)
2802 return begin() + __ip;
2803
2804 if (__string_is_trivial_iterator<_ForwardIterator>::value && !__addr_in_range(*__first))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002805 {
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002806 return __insert_from_safe_copy(__n, __ip, __first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002807 }
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002808 else
2809 {
2810 const basic_string __temp(__first, __last, __alloc());
2811 return __insert_from_safe_copy(__n, __ip, __temp.begin(), __temp.end());
2812 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002813}
2814
2815template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002816inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002817basic_string<_CharT, _Traits, _Allocator>&
2818basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2819{
2820 return insert(__pos1, __str.data(), __str.size());
2821}
2822
2823template <class _CharT, class _Traits, class _Allocator>
2824basic_string<_CharT, _Traits, _Allocator>&
2825basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2826 size_type __pos2, size_type __n)
2827{
2828 size_type __str_sz = __str.size();
2829 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002830 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002831 return insert(__pos1, __str.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002832}
2833
2834template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002835template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002836__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002837<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002838 __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 +00002839 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002840>
Marshall Clow82513342016-09-24 22:45:42 +00002841basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002842 size_type __pos2, size_type __n)
2843{
Marshall Clow82513342016-09-24 22:45:42 +00002844 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002845 size_type __str_sz = __sv.size();
2846 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002847 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002848 return insert(__pos1, __sv.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
2849}
2850
2851template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002852basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002853basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002854{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002855 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002856 return insert(__pos, __s, traits_type::length(__s));
2857}
2858
2859template <class _CharT, class _Traits, class _Allocator>
2860typename basic_string<_CharT, _Traits, _Allocator>::iterator
2861basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2862{
Louis Dionnef4cc6fb2022-02-15 15:47:45 -05002863 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2864 "string::insert(iterator, character) called with an iterator not"
2865 " referring to this string");
2866
Howard Hinnantc51e1022010-05-11 19:42:16 +00002867 size_type __ip = static_cast<size_type>(__pos - begin());
2868 size_type __sz = size();
2869 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002870 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002871 if (__cap == __sz)
2872 {
2873 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002874 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002875 }
2876 else
2877 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002878 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002879 size_type __n_move = __sz - __ip;
2880 if (__n_move != 0)
2881 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2882 }
2883 traits_type::assign(__p[__ip], __c);
2884 traits_type::assign(__p[++__sz], value_type());
2885 __set_size(__sz);
2886 return begin() + static_cast<difference_type>(__ip);
2887}
2888
2889template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002890inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002891typename basic_string<_CharT, _Traits, _Allocator>::iterator
2892basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2893{
Nikolas Klausereed25832021-12-15 01:32:30 +01002894 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2895 "string::insert(iterator, n, value) called with an iterator not"
2896 " referring to this string");
2897 difference_type __p = __pos - begin();
2898 insert(static_cast<size_type>(__p), __n, __c);
2899 return begin() + __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002900}
2901
2902// replace
2903
2904template <class _CharT, class _Traits, class _Allocator>
2905basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002906basic_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 +00002907 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00002908{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002909 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002910 size_type __sz = size();
2911 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002912 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002913 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002914 size_type __cap = capacity();
2915 if (__cap - __sz + __n1 >= __n2)
2916 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002917 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002918 if (__n1 != __n2)
2919 {
2920 size_type __n_move = __sz - __pos - __n1;
2921 if (__n_move != 0)
2922 {
2923 if (__n1 > __n2)
2924 {
2925 traits_type::move(__p + __pos, __s, __n2);
2926 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002927 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002928 }
2929 if (__p + __pos < __s && __s < __p + __sz)
2930 {
2931 if (__p + __pos + __n1 <= __s)
2932 __s += __n2 - __n1;
2933 else // __p + __pos < __s < __p + __pos + __n1
2934 {
2935 traits_type::move(__p + __pos, __s, __n1);
2936 __pos += __n1;
2937 __s += __n2;
2938 __n2 -= __n1;
2939 __n1 = 0;
2940 }
2941 }
2942 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2943 }
2944 }
2945 traits_type::move(__p + __pos, __s, __n2);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002946 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002947 }
2948 else
2949 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
2950 return *this;
2951}
2952
2953template <class _CharT, class _Traits, class _Allocator>
2954basic_string<_CharT, _Traits, _Allocator>&
2955basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
2956{
2957 size_type __sz = size();
2958 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002959 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002960 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002961 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002962 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002963 if (__cap - __sz + __n1 >= __n2)
2964 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002965 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002966 if (__n1 != __n2)
2967 {
2968 size_type __n_move = __sz - __pos - __n1;
2969 if (__n_move != 0)
2970 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2971 }
2972 }
2973 else
2974 {
2975 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002976 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002977 }
2978 traits_type::assign(__p + __pos, __n2, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002979 return __null_terminate_at(__p, __sz - (__n1 - __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002980}
2981
2982template <class _CharT, class _Traits, class _Allocator>
2983template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002984__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002985<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002986 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002987 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002988>
Howard Hinnant990d6e82010-11-17 21:11:40 +00002989basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002990 _InputIterator __j1, _InputIterator __j2)
2991{
Marshall Clow958362f2016-09-05 01:54:30 +00002992 const basic_string __temp(__j1, __j2, __alloc());
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002993 return replace(__i1, __i2, __temp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002994}
2995
2996template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002997inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002998basic_string<_CharT, _Traits, _Allocator>&
2999basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
3000{
3001 return replace(__pos1, __n1, __str.data(), __str.size());
3002}
3003
3004template <class _CharT, class _Traits, class _Allocator>
3005basic_string<_CharT, _Traits, _Allocator>&
3006basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
3007 size_type __pos2, size_type __n2)
3008{
3009 size_type __str_sz = __str.size();
3010 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003011 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003012 return replace(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003013}
3014
3015template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003016template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003017__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003018<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003019 __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 +00003020 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003021>
Marshall Clow82513342016-09-24 22:45:42 +00003022basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003023 size_type __pos2, size_type __n2)
3024{
Marshall Clow82513342016-09-24 22:45:42 +00003025 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003026 size_type __str_sz = __sv.size();
3027 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003028 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003029 return replace(__pos1, __n1, __sv.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
3030}
3031
3032template <class _CharT, class _Traits, class _Allocator>
3033basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003034basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003035{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003036 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003037 return replace(__pos, __n1, __s, traits_type::length(__s));
3038}
3039
3040template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003041inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003042basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003043basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003044{
3045 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
3046 __str.data(), __str.size());
3047}
3048
3049template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003050inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003051basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003052basic_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 +00003053{
3054 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
3055}
3056
3057template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003058inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003059basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003060basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003061{
3062 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __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, size_type __n, value_type __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003069{
3070 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
3071}
3072
3073// erase
3074
Martijn Velsa81fc792020-02-26 13:25:43 -05003075// 'externally instantiated' erase() implementation, called when __n != npos.
3076// Does not check __pos against size()
Howard Hinnantc51e1022010-05-11 19:42:16 +00003077template <class _CharT, class _Traits, class _Allocator>
Martijn Velsa81fc792020-02-26 13:25:43 -05003078void
3079basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(
3080 size_type __pos, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003081{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003082 if (__n)
3083 {
Martijn Velsa81fc792020-02-26 13:25:43 -05003084 size_type __sz = size();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003085 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003086 __n = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003087 size_type __n_move = __sz - __pos - __n;
3088 if (__n_move != 0)
3089 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003090 __null_terminate_at(__p, __sz - __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003091 }
Martijn Velsa81fc792020-02-26 13:25:43 -05003092}
3093
3094template <class _CharT, class _Traits, class _Allocator>
3095basic_string<_CharT, _Traits, _Allocator>&
3096basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos,
3097 size_type __n) {
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003098 if (__pos > size())
3099 __throw_out_of_range();
Martijn Velsa81fc792020-02-26 13:25:43 -05003100 if (__n == npos) {
3101 __erase_to_end(__pos);
3102 } else {
3103 __erase_external_with_move(__pos, __n);
3104 }
3105 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003106}
3107
3108template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003109inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003110typename basic_string<_CharT, _Traits, _Allocator>::iterator
3111basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
3112{
Nikolas Klausereed25832021-12-15 01:32:30 +01003113 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3114 "string::erase(iterator) called with an iterator not"
3115 " referring to this string");
3116
3117 _LIBCPP_ASSERT(__pos != end(), "string::erase(iterator) called with a non-dereferenceable iterator");
3118 iterator __b = begin();
3119 size_type __r = static_cast<size_type>(__pos - __b);
3120 erase(__r, 1);
3121 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003122}
3123
3124template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003125inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003126typename basic_string<_CharT, _Traits, _Allocator>::iterator
3127basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
3128{
Nikolas Klausereed25832021-12-15 01:32:30 +01003129 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
3130 "string::erase(iterator, iterator) called with an iterator not"
3131 " referring to this string");
3132
3133 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
3134 iterator __b = begin();
3135 size_type __r = static_cast<size_type>(__first - __b);
3136 erase(__r, static_cast<size_type>(__last - __first));
3137 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003138}
3139
3140template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003141inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003142void
3143basic_string<_CharT, _Traits, _Allocator>::pop_back()
3144{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003145 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003146 __erase_to_end(size() - 1);
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 +00003151void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003152basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003153{
3154 __invalidate_all_iterators();
3155 if (__is_long())
3156 {
3157 traits_type::assign(*__get_long_pointer(), value_type());
3158 __set_long_size(0);
3159 }
3160 else
3161 {
3162 traits_type::assign(*__get_short_pointer(), value_type());
3163 __set_short_size(0);
3164 }
3165}
3166
3167template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003168inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003169void
3170basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
3171{
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003172 __null_terminate_at(_VSTD::__to_address(__get_pointer()), __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003173}
3174
3175template <class _CharT, class _Traits, class _Allocator>
3176void
3177basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
3178{
3179 size_type __sz = size();
3180 if (__n > __sz)
3181 append(__n - __sz, __c);
3182 else
3183 __erase_to_end(__n);
3184}
3185
3186template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00003187inline void
3188basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n)
3189{
3190 size_type __sz = size();
3191 if (__n > __sz) {
3192 __append_default_init(__n - __sz);
3193 } else
3194 __erase_to_end(__n);
3195}
3196
3197template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003198inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003199typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003200basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003201{
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003202 size_type __m = __alloc_traits::max_size(__alloc());
Eric Fiseliere9cc5922017-10-17 13:16:01 +00003203#ifdef _LIBCPP_BIG_ENDIAN
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003204 return (__m <= ~__long_mask ? __m : __m/2) - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003205#else
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003206 return __m - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003207#endif
3208}
3209
3210template <class _CharT, class _Traits, class _Allocator>
3211void
Marek Kurdejc9848142020-11-26 10:07:16 +01003212basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __requested_capacity)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003213{
Marek Kurdejc9848142020-11-26 10:07:16 +01003214 if (__requested_capacity > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003215 __throw_length_error();
Marek Kurdejc9848142020-11-26 10:07:16 +01003216
Louis Dionne05b6d3e2022-01-14 12:30:22 -05003217 // Make sure reserve(n) never shrinks. This is technically only required in C++20
3218 // and later (since P0966R1), however we provide consistent behavior in all Standard
3219 // modes because this function is instantiated in the shared library.
3220 if (__requested_capacity <= capacity())
3221 return;
Marek Kurdejc9848142020-11-26 10:07:16 +01003222
3223 size_type __target_capacity = _VSTD::max(__requested_capacity, size());
3224 __target_capacity = __recommend(__target_capacity);
3225 if (__target_capacity == capacity()) return;
3226
3227 __shrink_or_extend(__target_capacity);
3228}
3229
3230template <class _CharT, class _Traits, class _Allocator>
Louis Dionnee38b8642021-12-13 14:15:21 -05003231inline
Marek Kurdejc9848142020-11-26 10:07:16 +01003232void
3233basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT
3234{
3235 size_type __target_capacity = __recommend(size());
3236 if (__target_capacity == capacity()) return;
3237
3238 __shrink_or_extend(__target_capacity);
3239}
3240
3241template <class _CharT, class _Traits, class _Allocator>
Louis Dionnee38b8642021-12-13 14:15:21 -05003242inline
Marek Kurdejc9848142020-11-26 10:07:16 +01003243void
3244basic_string<_CharT, _Traits, _Allocator>::__shrink_or_extend(size_type __target_capacity)
3245{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003246 size_type __cap = capacity();
3247 size_type __sz = size();
Marek Kurdejc9848142020-11-26 10:07:16 +01003248
3249 pointer __new_data, __p;
3250 bool __was_long, __now_long;
3251 if (__target_capacity == __min_cap - 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003252 {
Marek Kurdejc9848142020-11-26 10:07:16 +01003253 __was_long = true;
3254 __now_long = false;
3255 __new_data = __get_short_pointer();
3256 __p = __get_long_pointer();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003257 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003258 else
3259 {
3260 if (__target_capacity > __cap)
3261 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3262 else
3263 {
3264 #ifndef _LIBCPP_NO_EXCEPTIONS
3265 try
3266 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003267 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003268 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3269 #ifndef _LIBCPP_NO_EXCEPTIONS
3270 }
3271 catch (...)
3272 {
3273 return;
3274 }
3275 #else // _LIBCPP_NO_EXCEPTIONS
3276 if (__new_data == nullptr)
3277 return;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003278 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003279 }
3280 __now_long = true;
3281 __was_long = __is_long();
3282 __p = __get_pointer();
3283 }
3284 traits_type::copy(_VSTD::__to_address(__new_data),
3285 _VSTD::__to_address(__p), size()+1);
3286 if (__was_long)
3287 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
3288 if (__now_long)
3289 {
3290 __set_long_cap(__target_capacity+1);
3291 __set_long_size(__sz);
3292 __set_long_pointer(__new_data);
3293 }
3294 else
3295 __set_short_size(__sz);
3296 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003297}
3298
3299template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003300inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003301typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003302basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003303{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003304 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003305 return *(data() + __pos);
3306}
3307
3308template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003309inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003310typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003311basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003312{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003313 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003314 return *(__get_pointer() + __pos);
3315}
3316
3317template <class _CharT, class _Traits, class _Allocator>
3318typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3319basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3320{
3321 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003322 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003323 return (*this)[__n];
3324}
3325
3326template <class _CharT, class _Traits, class _Allocator>
3327typename basic_string<_CharT, _Traits, _Allocator>::reference
3328basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
3329{
3330 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003331 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003332 return (*this)[__n];
3333}
3334
3335template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003336inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003337typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003338basic_string<_CharT, _Traits, _Allocator>::front() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003339{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003340 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003341 return *__get_pointer();
3342}
3343
3344template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003345inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003346typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003347basic_string<_CharT, _Traits, _Allocator>::front() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003348{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003349 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003350 return *data();
3351}
3352
3353template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003354inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003355typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003356basic_string<_CharT, _Traits, _Allocator>::back() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003357{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003358 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003359 return *(__get_pointer() + size() - 1);
3360}
3361
3362template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003363inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003364typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003365basic_string<_CharT, _Traits, _Allocator>::back() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003366{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003367 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003368 return *(data() + size() - 1);
3369}
3370
3371template <class _CharT, class _Traits, class _Allocator>
3372typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003373basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003374{
3375 size_type __sz = size();
3376 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003377 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003378 size_type __rlen = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003379 traits_type::copy(__s, data() + __pos, __rlen);
3380 return __rlen;
3381}
3382
3383template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003384inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003385basic_string<_CharT, _Traits, _Allocator>
3386basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3387{
3388 return basic_string(*this, __pos, __n, __alloc());
3389}
3390
3391template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003392inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003393void
3394basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003395#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00003396 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00003397#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00003398 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003399 __is_nothrow_swappable<allocator_type>::value)
3400#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003401{
Louis Dionneba400782020-10-02 15:02:52 -04003402#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01003403 if (!__libcpp_is_constant_evaluated()) {
3404 if (!__is_long())
3405 __get_db()->__invalidate_all(this);
3406 if (!__str.__is_long())
3407 __get_db()->__invalidate_all(&__str);
3408 __get_db()->swap(this, &__str);
3409 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00003410#endif
Eric Fiselier9bf691f2016-12-28 05:53:01 +00003411 _LIBCPP_ASSERT(
3412 __alloc_traits::propagate_on_container_swap::value ||
3413 __alloc_traits::is_always_equal::value ||
3414 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003415 _VSTD::swap(__r_.first(), __str.__r_.first());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05003416 _VSTD::__swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003417}
3418
3419// find
3420
3421template <class _Traits>
3422struct _LIBCPP_HIDDEN __traits_eq
3423{
3424 typedef typename _Traits::char_type char_type;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003425 _LIBCPP_INLINE_VISIBILITY
3426 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3427 {return _Traits::eq(__x, __y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003428};
3429
3430template<class _CharT, class _Traits, class _Allocator>
3431typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003432basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003433 size_type __pos,
3434 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003435{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003436 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003437 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003438 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003439}
3440
3441template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003442inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003443typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003444basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3445 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003446{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003447 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003448 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003449}
3450
3451template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003452template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003453__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003454<
3455 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3456 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003457>
Marshall Clowe46031a2018-07-02 18:41:15 +00003458basic_string<_CharT, _Traits, _Allocator>::find(const _Tp &__t,
zoecarver1997e0a2021-02-05 11:54:47 -08003459 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003460{
Marshall Clowe46031a2018-07-02 18:41:15 +00003461 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003462 return __str_find<value_type, size_type, traits_type, npos>
3463 (data(), size(), __sv.data(), __pos, __sv.size());
3464}
3465
3466template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003467inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003468typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003469basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003470 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003471{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003472 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003473 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003474 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003475}
3476
3477template<class _CharT, class _Traits, class _Allocator>
3478typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003479basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3480 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003481{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003482 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003483 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003484}
3485
3486// rfind
3487
3488template<class _CharT, class _Traits, class _Allocator>
3489typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003490basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003491 size_type __pos,
3492 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003493{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003494 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003495 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003496 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003497}
3498
3499template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003500inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003501typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003502basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3503 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003504{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003505 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003506 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003507}
3508
3509template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003510template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003511__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003512<
3513 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3514 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003515>
Marshall Clowe46031a2018-07-02 18:41:15 +00003516basic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003517 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003518{
Marshall Clowe46031a2018-07-02 18:41:15 +00003519 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003520 return __str_rfind<value_type, size_type, traits_type, npos>
3521 (data(), size(), __sv.data(), __pos, __sv.size());
3522}
3523
3524template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003525inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003526typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003527basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003528 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003529{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003530 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003531 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003532 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003533}
3534
3535template<class _CharT, class _Traits, class _Allocator>
3536typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003537basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3538 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003539{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003540 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003541 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003542}
3543
3544// find_first_of
3545
3546template<class _CharT, class _Traits, class _Allocator>
3547typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003548basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003549 size_type __pos,
3550 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003551{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003552 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003553 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003554 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003555}
3556
3557template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003558inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003559typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003560basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3561 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003562{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003563 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003564 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003565}
3566
3567template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003568template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003569__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003570<
3571 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3572 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003573>
Marshall Clowe46031a2018-07-02 18:41:15 +00003574basic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003575 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003576{
Marshall Clowe46031a2018-07-02 18:41:15 +00003577 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003578 return __str_find_first_of<value_type, size_type, traits_type, npos>
3579 (data(), size(), __sv.data(), __pos, __sv.size());
3580}
3581
3582template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003583inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003584typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003585basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003586 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003587{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003588 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003589 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003590 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003591}
3592
3593template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003594inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003595typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003596basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3597 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003598{
3599 return find(__c, __pos);
3600}
3601
3602// find_last_of
3603
3604template<class _CharT, class _Traits, class _Allocator>
3605typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003606basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003607 size_type __pos,
3608 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003609{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003610 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003611 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003612 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003613}
3614
3615template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003616inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003617typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003618basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3619 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003620{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003621 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003622 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003623}
3624
3625template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003626template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003627__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003628<
3629 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3630 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003631>
Marshall Clowe46031a2018-07-02 18:41:15 +00003632basic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003633 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003634{
Marshall Clowe46031a2018-07-02 18:41:15 +00003635 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003636 return __str_find_last_of<value_type, size_type, traits_type, npos>
3637 (data(), size(), __sv.data(), __pos, __sv.size());
3638}
3639
3640template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003641inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003642typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003643basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003644 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003645{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003646 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003647 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003648 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003649}
3650
3651template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003652inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003653typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003654basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3655 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003656{
3657 return rfind(__c, __pos);
3658}
3659
3660// find_first_not_of
3661
3662template<class _CharT, class _Traits, class _Allocator>
3663typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003664basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003665 size_type __pos,
3666 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003667{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003668 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003669 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003670 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003671}
3672
3673template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003674inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003675typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003676basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3677 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003678{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003679 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003680 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003681}
3682
3683template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003684template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003685__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003686<
3687 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3688 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003689>
Marshall Clowe46031a2018-07-02 18:41:15 +00003690basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003691 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003692{
Marshall Clowe46031a2018-07-02 18:41:15 +00003693 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003694 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3695 (data(), size(), __sv.data(), __pos, __sv.size());
3696}
3697
3698template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003699inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003700typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003701basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003702 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003703{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003704 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003705 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003706 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003707}
3708
3709template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003710inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003711typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003712basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3713 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003714{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003715 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003716 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003717}
3718
3719// find_last_not_of
3720
3721template<class _CharT, class _Traits, class _Allocator>
3722typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003723basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003724 size_type __pos,
3725 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003726{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003727 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003728 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003729 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003730}
3731
3732template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003733inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003734typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003735basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3736 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003737{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003738 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003739 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003740}
3741
3742template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003743template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003744__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003745<
3746 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3747 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003748>
Marshall Clowe46031a2018-07-02 18:41:15 +00003749basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003750 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003751{
Marshall Clowe46031a2018-07-02 18:41:15 +00003752 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003753 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3754 (data(), size(), __sv.data(), __pos, __sv.size());
3755}
3756
3757template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003758inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003759typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003760basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003761 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003762{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003763 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003764 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003765 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003766}
3767
3768template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003769inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003770typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003771basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3772 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003773{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003774 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003775 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003776}
3777
3778// compare
3779
3780template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003781template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003782__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003783<
3784 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3785 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003786>
zoecarver1997e0a2021-02-05 11:54:47 -08003787basic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003788{
Marshall Clowe46031a2018-07-02 18:41:15 +00003789 __self_view __sv = __t;
Howard Hinnantb0485532011-07-24 21:45:06 +00003790 size_t __lhs_sz = size();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003791 size_t __rhs_sz = __sv.size();
3792 int __result = traits_type::compare(data(), __sv.data(),
Howard Hinnantb0485532011-07-24 21:45:06 +00003793 _VSTD::min(__lhs_sz, __rhs_sz));
3794 if (__result != 0)
3795 return __result;
3796 if (__lhs_sz < __rhs_sz)
3797 return -1;
3798 if (__lhs_sz > __rhs_sz)
3799 return 1;
3800 return 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003801}
3802
3803template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003804inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003805int
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003806basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003807{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003808 return compare(__self_view(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003809}
3810
3811template <class _CharT, class _Traits, class _Allocator>
3812int
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003813basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3814 size_type __n1,
Howard Hinnantd17880b2013-06-28 16:59:19 +00003815 const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003816 size_type __n2) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003817{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003818 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003819 size_type __sz = size();
3820 if (__pos1 > __sz || __n2 == npos)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003821 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003822 size_type __rlen = _VSTD::min(__n1, __sz - __pos1);
3823 int __r = traits_type::compare(data() + __pos1, __s, _VSTD::min(__rlen, __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003824 if (__r == 0)
3825 {
3826 if (__rlen < __n2)
3827 __r = -1;
3828 else if (__rlen > __n2)
3829 __r = 1;
3830 }
3831 return __r;
3832}
3833
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003834template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003835template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003836__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003837<
3838 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3839 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003840>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003841basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3842 size_type __n1,
Marshall Clowe46031a2018-07-02 18:41:15 +00003843 const _Tp& __t) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003844{
Marshall Clowe46031a2018-07-02 18:41:15 +00003845 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003846 return compare(__pos1, __n1, __sv.data(), __sv.size());
3847}
3848
3849template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003850inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003851int
3852basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3853 size_type __n1,
3854 const basic_string& __str) const
3855{
3856 return compare(__pos1, __n1, __str.data(), __str.size());
3857}
3858
3859template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003860template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003861__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003862<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003863 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
3864 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00003865 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003866>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003867basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3868 size_type __n1,
Marshall Clow82513342016-09-24 22:45:42 +00003869 const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003870 size_type __pos2,
3871 size_type __n2) const
3872{
Marshall Clow82513342016-09-24 22:45:42 +00003873 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003874 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3875}
3876
3877template <class _CharT, class _Traits, class _Allocator>
3878int
3879basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3880 size_type __n1,
3881 const basic_string& __str,
3882 size_type __pos2,
3883 size_type __n2) const
3884{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003885 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003886}
3887
3888template <class _CharT, class _Traits, class _Allocator>
3889int
3890basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
3891{
3892 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3893 return compare(0, npos, __s, traits_type::length(__s));
3894}
3895
3896template <class _CharT, class _Traits, class _Allocator>
3897int
3898basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3899 size_type __n1,
3900 const value_type* __s) const
3901{
3902 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3903 return compare(__pos1, __n1, __s, traits_type::length(__s));
3904}
3905
Howard Hinnantc51e1022010-05-11 19:42:16 +00003906// __invariants
3907
3908template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003909inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003910bool
3911basic_string<_CharT, _Traits, _Allocator>::__invariants() const
3912{
3913 if (size() > capacity())
3914 return false;
3915 if (capacity() < __min_cap - 1)
3916 return false;
Bruce Mitchener170d8972020-11-24 12:53:53 -05003917 if (data() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003918 return false;
Louis Dionne663415f2020-10-05 16:16:13 -04003919 if (data()[size()] != value_type())
Howard Hinnantc51e1022010-05-11 19:42:16 +00003920 return false;
3921 return true;
3922}
3923
Vedant Kumar55e007e2018-03-08 21:15:26 +00003924// __clear_and_shrink
3925
3926template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003927inline
Louis Dionne173f29e2019-05-29 16:01:36 +00003928void
Marshall Clowe60a7182018-05-29 17:04:37 +00003929basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
Vedant Kumar55e007e2018-03-08 21:15:26 +00003930{
3931 clear();
3932 if(__is_long())
3933 {
3934 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
3935 __set_long_cap(0);
3936 __set_short_size(0);
Louis Dionne663415f2020-10-05 16:16:13 -04003937 traits_type::assign(*__get_short_pointer(), value_type());
Vedant Kumar55e007e2018-03-08 21:15:26 +00003938 }
Louis Dionne173f29e2019-05-29 16:01:36 +00003939}
Vedant Kumar55e007e2018-03-08 21:15:26 +00003940
Howard Hinnantc51e1022010-05-11 19:42:16 +00003941// operator==
3942
3943template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003944inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003945bool
3946operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003947 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003948{
Howard Hinnantaaeb1132013-04-22 23:55:13 +00003949 size_t __lhs_sz = __lhs.size();
3950 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
3951 __rhs.data(),
3952 __lhs_sz) == 0;
3953}
3954
3955template<class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003956inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaaeb1132013-04-22 23:55:13 +00003957bool
3958operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
3959 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
3960{
3961 size_t __lhs_sz = __lhs.size();
3962 if (__lhs_sz != __rhs.size())
3963 return false;
3964 const char* __lp = __lhs.data();
3965 const char* __rp = __rhs.data();
3966 if (__lhs.__is_long())
3967 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
3968 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
3969 if (*__lp != *__rp)
3970 return false;
3971 return true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003972}
3973
3974template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003975inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003976bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003977operator==(const _CharT* __lhs,
3978 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003979{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00003980 typedef basic_string<_CharT, _Traits, _Allocator> _String;
3981 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
3982 size_t __lhs_len = _Traits::length(__lhs);
3983 if (__lhs_len != __rhs.size()) return false;
3984 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003985}
3986
Howard Hinnantc51e1022010-05-11 19:42:16 +00003987template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003988inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003989bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003990operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
3991 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003992{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00003993 typedef basic_string<_CharT, _Traits, _Allocator> _String;
3994 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
3995 size_t __rhs_len = _Traits::length(__rhs);
3996 if (__rhs_len != __lhs.size()) return false;
3997 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003998}
3999
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004000template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004001inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004002bool
4003operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004004 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004005{
4006 return !(__lhs == __rhs);
4007}
4008
4009template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004010inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004011bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004012operator!=(const _CharT* __lhs,
4013 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004014{
4015 return !(__lhs == __rhs);
4016}
4017
4018template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004019inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004020bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004021operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4022 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004023{
4024 return !(__lhs == __rhs);
4025}
4026
4027// operator<
4028
4029template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004030inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004031bool
4032operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004033 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004034{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004035 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004036}
4037
4038template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004039inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004040bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004041operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4042 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004043{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004044 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004045}
4046
4047template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004048inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004049bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004050operator< (const _CharT* __lhs,
4051 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004052{
4053 return __rhs.compare(__lhs) > 0;
4054}
4055
Howard Hinnantc51e1022010-05-11 19:42:16 +00004056// operator>
4057
4058template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004059inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004060bool
4061operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004062 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004063{
4064 return __rhs < __lhs;
4065}
4066
4067template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004068inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004069bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004070operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4071 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004072{
4073 return __rhs < __lhs;
4074}
4075
4076template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004077inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004078bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004079operator> (const _CharT* __lhs,
4080 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004081{
4082 return __rhs < __lhs;
4083}
4084
4085// operator<=
4086
4087template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004088inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004089bool
4090operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004091 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004092{
4093 return !(__rhs < __lhs);
4094}
4095
4096template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004097inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004098bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004099operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4100 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004101{
4102 return !(__rhs < __lhs);
4103}
4104
4105template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004106inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004107bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004108operator<=(const _CharT* __lhs,
4109 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004110{
4111 return !(__rhs < __lhs);
4112}
4113
4114// operator>=
4115
4116template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004117inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004118bool
4119operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004120 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004121{
4122 return !(__lhs < __rhs);
4123}
4124
4125template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004126inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004127bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004128operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4129 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004130{
4131 return !(__lhs < __rhs);
4132}
4133
4134template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004135inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004136bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004137operator>=(const _CharT* __lhs,
4138 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004139{
4140 return !(__lhs < __rhs);
4141}
4142
4143// operator +
4144
4145template<class _CharT, class _Traits, class _Allocator>
4146basic_string<_CharT, _Traits, _Allocator>
4147operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4148 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4149{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004150 using _String = basic_string<_CharT, _Traits, _Allocator>;
4151 _String __r(_String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4152 typename _String::size_type __lhs_sz = __lhs.size();
4153 typename _String::size_type __rhs_sz = __rhs.size();
Howard Hinnantc51e1022010-05-11 19:42:16 +00004154 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4155 __r.append(__rhs.data(), __rhs_sz);
4156 return __r;
4157}
4158
4159template<class _CharT, class _Traits, class _Allocator>
4160basic_string<_CharT, _Traits, _Allocator>
4161operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4162{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004163 using _String = basic_string<_CharT, _Traits, _Allocator>;
4164 _String __r(_String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator()));
4165 typename _String::size_type __lhs_sz = _Traits::length(__lhs);
4166 typename _String::size_type __rhs_sz = __rhs.size();
Howard Hinnantc51e1022010-05-11 19:42:16 +00004167 __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz);
4168 __r.append(__rhs.data(), __rhs_sz);
4169 return __r;
4170}
4171
4172template<class _CharT, class _Traits, class _Allocator>
4173basic_string<_CharT, _Traits, _Allocator>
4174operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4175{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004176 using _String = basic_string<_CharT, _Traits, _Allocator>;
4177 _String __r(_String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator()));
4178 typename _String::size_type __rhs_sz = __rhs.size();
Howard Hinnantc51e1022010-05-11 19:42:16 +00004179 __r.__init(&__lhs, 1, 1 + __rhs_sz);
4180 __r.append(__rhs.data(), __rhs_sz);
4181 return __r;
4182}
4183
4184template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004185inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004186basic_string<_CharT, _Traits, _Allocator>
4187operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
4188{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004189 using _String = basic_string<_CharT, _Traits, _Allocator>;
4190 _String __r(_String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4191 typename _String::size_type __lhs_sz = __lhs.size();
4192 typename _String::size_type __rhs_sz = _Traits::length(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004193 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4194 __r.append(__rhs, __rhs_sz);
4195 return __r;
4196}
4197
4198template<class _CharT, class _Traits, class _Allocator>
4199basic_string<_CharT, _Traits, _Allocator>
4200operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
4201{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004202 using _String = basic_string<_CharT, _Traits, _Allocator>;
4203 _String __r(_String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4204 typename _String::size_type __lhs_sz = __lhs.size();
Howard Hinnantc51e1022010-05-11 19:42:16 +00004205 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1);
4206 __r.push_back(__rhs);
4207 return __r;
4208}
4209
Eric Fiselierfc92be82017-04-19 00:28:44 +00004210#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004211
4212template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004213inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004214basic_string<_CharT, _Traits, _Allocator>
4215operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4216{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004217 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004218}
4219
4220template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004221inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004222basic_string<_CharT, _Traits, _Allocator>
4223operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4224{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004225 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004226}
4227
4228template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004229inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004230basic_string<_CharT, _Traits, _Allocator>
4231operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4232{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004233 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004234}
4235
4236template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004237inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004238basic_string<_CharT, _Traits, _Allocator>
4239operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4240{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004241 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004242}
4243
4244template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004245inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004246basic_string<_CharT, _Traits, _Allocator>
4247operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4248{
4249 __rhs.insert(__rhs.begin(), __lhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004250 return _VSTD::move(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004251}
4252
4253template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004254inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004255basic_string<_CharT, _Traits, _Allocator>
4256operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
4257{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004258 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004259}
4260
4261template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004262inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004263basic_string<_CharT, _Traits, _Allocator>
4264operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
4265{
4266 __lhs.push_back(__rhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004267 return _VSTD::move(__lhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004268}
4269
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004270#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004271
4272// swap
4273
4274template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004275inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004276void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004277swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +00004278 basic_string<_CharT, _Traits, _Allocator>& __rhs)
4279 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004280{
4281 __lhs.swap(__rhs);
4282}
4283
Bruce Mitchener170d8972020-11-24 12:53:53 -05004284_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = nullptr, int __base = 10);
4285_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = nullptr, int __base = 10);
4286_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = nullptr, int __base = 10);
4287_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = nullptr, int __base = 10);
4288_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004289
Bruce Mitchener170d8972020-11-24 12:53:53 -05004290_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = nullptr);
4291_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = nullptr);
4292_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004293
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004294_LIBCPP_FUNC_VIS string to_string(int __val);
4295_LIBCPP_FUNC_VIS string to_string(unsigned __val);
4296_LIBCPP_FUNC_VIS string to_string(long __val);
4297_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
4298_LIBCPP_FUNC_VIS string to_string(long long __val);
4299_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
4300_LIBCPP_FUNC_VIS string to_string(float __val);
4301_LIBCPP_FUNC_VIS string to_string(double __val);
4302_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004303
Louis Dionne89258142021-08-23 15:32:36 -04004304#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Bruce Mitchener170d8972020-11-24 12:53:53 -05004305_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4306_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4307_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4308_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4309_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004310
Bruce Mitchener170d8972020-11-24 12:53:53 -05004311_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = nullptr);
4312_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = nullptr);
4313_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004314
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004315_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
4316_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
4317_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
4318_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
4319_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
4320_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
4321_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
4322_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
4323_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Louis Dionne89258142021-08-23 15:32:36 -04004324#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004325
Howard Hinnantc51e1022010-05-11 19:42:16 +00004326template<class _CharT, class _Traits, class _Allocator>
Martin Storsjö88890c22021-02-22 01:13:13 +02004327_LIBCPP_TEMPLATE_DATA_VIS
Eric Fiselierc9fdaf12020-01-15 17:02:17 -05004328const typename basic_string<_CharT, _Traits, _Allocator>::size_type
4329 basic_string<_CharT, _Traits, _Allocator>::npos;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004330
Marshall Clow851b9ec2019-05-20 21:56:51 +00004331template <class _CharT, class _Allocator>
4332struct _LIBCPP_TEMPLATE_VIS
4333 hash<basic_string<_CharT, char_traits<_CharT>, _Allocator> >
4334 : public unary_function<
4335 basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004336{
4337 size_t
Marshall Clow851b9ec2019-05-20 21:56:51 +00004338 operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT
4339 { return __do_string_hash(__val.data(), __val.data() + __val.size()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004340};
4341
Howard Hinnantc51e1022010-05-11 19:42:16 +00004342
Howard Hinnantdc095972011-07-18 15:51:59 +00004343template<class _CharT, class _Traits, class _Allocator>
4344basic_ostream<_CharT, _Traits>&
4345operator<<(basic_ostream<_CharT, _Traits>& __os,
4346 const basic_string<_CharT, _Traits, _Allocator>& __str);
4347
4348template<class _CharT, class _Traits, class _Allocator>
4349basic_istream<_CharT, _Traits>&
4350operator>>(basic_istream<_CharT, _Traits>& __is,
4351 basic_string<_CharT, _Traits, _Allocator>& __str);
4352
4353template<class _CharT, class _Traits, class _Allocator>
4354basic_istream<_CharT, _Traits>&
4355getline(basic_istream<_CharT, _Traits>& __is,
4356 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4357
4358template<class _CharT, class _Traits, class _Allocator>
4359inline _LIBCPP_INLINE_VISIBILITY
4360basic_istream<_CharT, _Traits>&
4361getline(basic_istream<_CharT, _Traits>& __is,
4362 basic_string<_CharT, _Traits, _Allocator>& __str);
4363
Howard Hinnantdc095972011-07-18 15:51:59 +00004364template<class _CharT, class _Traits, class _Allocator>
4365inline _LIBCPP_INLINE_VISIBILITY
4366basic_istream<_CharT, _Traits>&
4367getline(basic_istream<_CharT, _Traits>&& __is,
4368 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4369
4370template<class _CharT, class _Traits, class _Allocator>
4371inline _LIBCPP_INLINE_VISIBILITY
4372basic_istream<_CharT, _Traits>&
4373getline(basic_istream<_CharT, _Traits>&& __is,
4374 basic_string<_CharT, _Traits, _Allocator>& __str);
4375
Marshall Clow29b53f22018-12-14 18:49:35 +00004376#if _LIBCPP_STD_VER > 17
Marek Kurdeja98b1412020-05-02 13:58:03 +02004377template <class _CharT, class _Traits, class _Allocator, class _Up>
Marshall Clow29b53f22018-12-14 18:49:35 +00004378inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004379 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4380 erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
4381 auto __old_size = __str.size();
4382 __str.erase(_VSTD::remove(__str.begin(), __str.end(), __v), __str.end());
4383 return __old_size - __str.size();
4384}
Marshall Clow29b53f22018-12-14 18:49:35 +00004385
Marek Kurdeja98b1412020-05-02 13:58:03 +02004386template <class _CharT, class _Traits, class _Allocator, class _Predicate>
Marshall Clow29b53f22018-12-14 18:49:35 +00004387inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004388 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4389 erase_if(basic_string<_CharT, _Traits, _Allocator>& __str,
4390 _Predicate __pred) {
4391 auto __old_size = __str.size();
4392 __str.erase(_VSTD::remove_if(__str.begin(), __str.end(), __pred),
4393 __str.end());
4394 return __old_size - __str.size();
4395}
Marshall Clow29b53f22018-12-14 18:49:35 +00004396#endif
4397
Louis Dionneba400782020-10-02 15:02:52 -04004398#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004399
4400template<class _CharT, class _Traits, class _Allocator>
4401bool
4402basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4403{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004404 return data() <= _VSTD::__to_address(__i->base()) &&
4405 _VSTD::__to_address(__i->base()) < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004406}
4407
4408template<class _CharT, class _Traits, class _Allocator>
4409bool
4410basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4411{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004412 return data() < _VSTD::__to_address(__i->base()) &&
4413 _VSTD::__to_address(__i->base()) <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004414}
4415
4416template<class _CharT, class _Traits, class _Allocator>
4417bool
4418basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4419{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004420 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004421 return data() <= __p && __p <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004422}
4423
4424template<class _CharT, class _Traits, class _Allocator>
4425bool
4426basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4427{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004428 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004429 return data() <= __p && __p < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004430}
4431
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004432#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004433
Louis Dionne173f29e2019-05-29 16:01:36 +00004434#if _LIBCPP_STD_VER > 11
Marshall Clowcba751f2013-07-23 17:05:24 +00004435// Literal suffixes for basic_string [basic.string.literals]
Marshall Clowac868372013-10-05 21:18:32 +00004436inline namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004437{
4438 inline namespace string_literals
4439 {
Howard Hinnant5c167562013-08-07 19:39:48 +00004440 inline _LIBCPP_INLINE_VISIBILITY
4441 basic_string<char> operator "" s( const char *__str, size_t __len )
4442 {
4443 return basic_string<char> (__str, __len);
4444 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004445
Louis Dionne89258142021-08-23 15:32:36 -04004446#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnant5c167562013-08-07 19:39:48 +00004447 inline _LIBCPP_INLINE_VISIBILITY
4448 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4449 {
4450 return basic_string<wchar_t> (__str, __len);
4451 }
Louis Dionne89258142021-08-23 15:32:36 -04004452#endif
Marshall Clowcba751f2013-07-23 17:05:24 +00004453
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -04004454#ifndef _LIBCPP_HAS_NO_CHAR8_T
Marshall Clow8732fed2018-12-11 04:35:44 +00004455 inline _LIBCPP_INLINE_VISIBILITY
4456 basic_string<char8_t> operator "" s(const char8_t *__str, size_t __len) _NOEXCEPT
4457 {
4458 return basic_string<char8_t> (__str, __len);
4459 }
4460#endif
4461
Howard Hinnant5c167562013-08-07 19:39:48 +00004462 inline _LIBCPP_INLINE_VISIBILITY
4463 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4464 {
4465 return basic_string<char16_t> (__str, __len);
4466 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004467
Howard Hinnant5c167562013-08-07 19:39:48 +00004468 inline _LIBCPP_INLINE_VISIBILITY
4469 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4470 {
4471 return basic_string<char32_t> (__str, __len);
4472 }
Nikolas Klauserd26407a2021-12-02 14:12:51 +01004473 } // namespace string_literals
4474} // namespace literals
Mark de Weverdf3e0d42021-09-26 15:47:42 +02004475
4476#if _LIBCPP_STD_VER > 17
4477template <>
4478inline constexpr bool __format::__enable_insertable<std::basic_string<char>> = true;
4479#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
4480template <>
4481inline constexpr bool __format::__enable_insertable<std::basic_string<wchar_t>> = true;
4482#endif
4483#endif
4484
Marshall Clowcba751f2013-07-23 17:05:24 +00004485#endif
4486
Howard Hinnantc51e1022010-05-11 19:42:16 +00004487_LIBCPP_END_NAMESPACE_STD
4488
Eric Fiselierf4433a32017-05-31 22:07:49 +00004489_LIBCPP_POP_MACROS
4490
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004491#endif // _LIBCPP_STRING