blob: 39647ff79db33b13a829d22b40e492851b63bbe0 [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 Dionne9bdbeb32022-02-14 13:41:09 -0500525#include <__assert>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000526#include <__config>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400527#include <__debug>
Nikolas Klauser80840272022-02-04 13:04:33 +0100528#include <__ios/fpos.h>
Louis Dionne77249522021-06-11 09:55:11 -0400529#include <__iterator/wrap_iter.h>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400530#include <compare>
531#include <cstdio> // EOF
Louis Dionned24191c2021-08-19 12:39:16 -0400532#include <cstdlib>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400533#include <cstring>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400534#include <initializer_list>
535#include <iosfwd>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000536#include <iterator>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000537#include <memory>
538#include <stdexcept>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400539#include <string_view>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000540#include <type_traits>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400541#include <utility>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000542#include <version>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000543
Nikolas Klauser4c1bf592022-02-11 19:15:18 +0100544// TODO: remove these headers
545#include <__functional/binary_function.h>
546#include <__functional/invoke.h>
547#include <__functional/operations.h>
548#include <__functional/reference_wrapper.h>
549#include <__functional/unary_function.h>
550#include <__functional/weak_result_type.h>
551#include <new>
552#include <typeinfo>
553
Louis Dionne89258142021-08-23 15:32:36 -0400554#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Nikolas Klauser80840272022-02-04 13:04:33 +0100555# include <cwchar>
Louis Dionne89258142021-08-23 15:32:36 -0400556#endif
557
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400558#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
Nikolas Klauser80840272022-02-04 13:04:33 +0100559# include <cstdint>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400560#endif
Eric Fiselier14b6de92014-08-10 23:53:08 +0000561
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000562#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Arthur O'Dwyer6eeaa002022-02-01 20:16:40 -0500563# pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000564#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000565
Eric Fiselierf4433a32017-05-31 22:07:49 +0000566_LIBCPP_PUSH_MACROS
567#include <__undef_macros>
568
569
Howard Hinnantc51e1022010-05-11 19:42:16 +0000570_LIBCPP_BEGIN_NAMESPACE_STD
571
Howard Hinnantc51e1022010-05-11 19:42:16 +0000572// basic_string
573
574template<class _CharT, class _Traits, class _Allocator>
575basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000576operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
577 const basic_string<_CharT, _Traits, _Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000578
579template<class _CharT, class _Traits, class _Allocator>
580basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000581operator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000582
583template<class _CharT, class _Traits, class _Allocator>
584basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000585operator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000586
587template<class _CharT, class _Traits, class _Allocator>
Louis Dionne8f8d95d2018-11-21 17:31:55 +0000588inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000589basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000590operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000591
592template<class _CharT, class _Traits, class _Allocator>
593basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000594operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000595
Shoaib Meenaiea363712017-07-29 02:54:41 +0000596_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&))
597
Marshall Clow039b2f02016-01-13 21:54:34 +0000598template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400599struct __string_is_trivial_iterator : public false_type {};
600
601template <class _Tp>
602struct __string_is_trivial_iterator<_Tp*>
603 : public is_arithmetic<_Tp> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000604
Louis Dionne173f29e2019-05-29 16:01:36 +0000605template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400606struct __string_is_trivial_iterator<__wrap_iter<_Iter> >
607 : public __string_is_trivial_iterator<_Iter> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000608
Marshall Clow82513342016-09-24 22:45:42 +0000609template <class _CharT, class _Traits, class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500610struct __can_be_converted_to_string_view : public _BoolConstant<
611 is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
612 !is_convertible<const _Tp&, const _CharT*>::value
613 > {};
Marshall Clow82513342016-09-24 22:45:42 +0000614
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000615#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000616
617template <class _CharT, size_t = sizeof(_CharT)>
618struct __padding
619{
620 unsigned char __xx[sizeof(_CharT)-1];
621};
622
623template <class _CharT>
624struct __padding<_CharT, 1>
625{
626};
627
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400628#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000629
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400630#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800631typedef basic_string<char8_t> u8string;
632#endif
633
634#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
635typedef basic_string<char16_t> u16string;
636typedef basic_string<char32_t> u32string;
Louis Dionne96fc5f52021-09-09 11:25:10 -0400637#endif
Richard Smith256954d2020-11-11 17:12:18 -0800638
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000639template<class _CharT, class _Traits, class _Allocator>
Richard Smith256954d2020-11-11 17:12:18 -0800640class
641 _LIBCPP_TEMPLATE_VIS
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400642#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800643 _LIBCPP_PREFERRED_NAME(u8string)
644#endif
645#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
646 _LIBCPP_PREFERRED_NAME(u16string)
647 _LIBCPP_PREFERRED_NAME(u32string)
648#endif
649 basic_string
Howard Hinnantc51e1022010-05-11 19:42:16 +0000650{
651public:
652 typedef basic_string __self;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000653 typedef basic_string_view<_CharT, _Traits> __self_view;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000654 typedef _Traits traits_type;
Marshall Clowa3a74e02017-03-15 18:41:11 +0000655 typedef _CharT value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000656 typedef _Allocator allocator_type;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000657 typedef allocator_traits<allocator_type> __alloc_traits;
658 typedef typename __alloc_traits::size_type size_type;
659 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant3e276872011-06-03 18:40:47 +0000660 typedef value_type& reference;
661 typedef const value_type& const_reference;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000662 typedef typename __alloc_traits::pointer pointer;
663 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000664
Marshall Clow79f33542018-03-21 00:36:05 +0000665 static_assert((!is_array<value_type>::value), "Character type of basic_string must not be an array");
666 static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string must be standard-layout");
667 static_assert(( is_trivial<value_type>::value), "Character type of basic_string must be trivial");
668 static_assert(( is_same<_CharT, typename traits_type::char_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000669 "traits_type::char_type must be the same type as CharT");
Marshall Clow79f33542018-03-21 00:36:05 +0000670 static_assert(( is_same<typename allocator_type::value_type, value_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000671 "Allocator::value_type must be same type as value_type");
Marshall Clowe46031a2018-07-02 18:41:15 +0000672
Howard Hinnantc51e1022010-05-11 19:42:16 +0000673 typedef __wrap_iter<pointer> iterator;
674 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000675 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
676 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000677
678private:
Howard Hinnant68bf1812013-04-30 21:44:48 +0000679
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000680#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000681
682 struct __long
683 {
684 pointer __data_;
685 size_type __size_;
686 size_type __cap_;
687 };
688
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000689#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000690 static const size_type __short_mask = 0x01;
691 static const size_type __long_mask = 0x1ul;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000692#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000693 static const size_type __short_mask = 0x80;
694 static const size_type __long_mask = ~(size_type(~0) >> 1);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400695#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +0000696
697 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
698 (sizeof(__long) - 1)/sizeof(value_type) : 2};
699
700 struct __short
701 {
702 value_type __data_[__min_cap];
703 struct
704 : __padding<value_type>
705 {
706 unsigned char __size_;
707 };
708 };
709
710#else
711
Howard Hinnantc51e1022010-05-11 19:42:16 +0000712 struct __long
713 {
714 size_type __cap_;
715 size_type __size_;
716 pointer __data_;
717 };
718
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000719#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000720 static const size_type __short_mask = 0x80;
721 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000722#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000723 static const size_type __short_mask = 0x01;
724 static const size_type __long_mask = 0x1ul;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400725#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +0000726
Howard Hinnantc51e1022010-05-11 19:42:16 +0000727 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
728 (sizeof(__long) - 1)/sizeof(value_type) : 2};
729
730 struct __short
731 {
732 union
733 {
734 unsigned char __size_;
Howard Hinnant49e145e2012-10-30 19:06:59 +0000735 value_type __lx;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000736 };
737 value_type __data_[__min_cap];
738 };
739
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400740#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000741
Howard Hinnant8ea98242013-08-23 17:37:05 +0000742 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000743
Howard Hinnant8ea98242013-08-23 17:37:05 +0000744 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000745
746 struct __raw
747 {
748 size_type __words[__n_words];
749 };
750
751 struct __rep
752 {
753 union
754 {
755 __long __l;
756 __short __s;
757 __raw __r;
758 };
759 };
760
761 __compressed_pair<__rep, allocator_type> __r_;
762
Howard Hinnantc51e1022010-05-11 19:42:16 +0000763public:
Martin Storsjö88890c22021-02-22 01:13:13 +0200764 _LIBCPP_TEMPLATE_DATA_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000765 static const size_type npos = -1;
766
Howard Hinnant3e276872011-06-03 18:40:47 +0000767 _LIBCPP_INLINE_VISIBILITY basic_string()
768 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000769
770 _LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a)
771#if _LIBCPP_STD_VER <= 14
772 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
773#else
774 _NOEXCEPT;
775#endif
776
Howard Hinnantc51e1022010-05-11 19:42:16 +0000777 basic_string(const basic_string& __str);
778 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clowa80abc72015-06-03 19:56:43 +0000779
Eric Fiselierfc92be82017-04-19 00:28:44 +0000780#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000781 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000782 basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +0000783#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +0000784 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000785#else
786 _NOEXCEPT;
787#endif
788
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000789 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000790 basic_string(basic_string&& __str, const allocator_type& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400791#endif // _LIBCPP_CXX03_LANG
Marshall Clowe46031a2018-07-02 18:41:15 +0000792
Louis Dionne9ce598d2021-09-08 09:14:43 -0400793 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Eric Fiseliera8567862018-07-17 05:48:48 +0000794 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5169d1c2019-12-16 19:03:23 -0500795 basic_string(const _CharT* __s) : __r_(__default_init_tag(), __default_init_tag()) {
Eric Fiseliera8567862018-07-17 05:48:48 +0000796 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
797 __init(__s, traits_type::length(__s));
Nikolas Klauserf2807732022-01-11 00:33:35 +0100798 _VSTD::__debug_db_insert_c(this);
Eric Fiseliera8567862018-07-17 05:48:48 +0000799 }
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(const _CharT* __s, const _Allocator& __a);
804
Marek Kurdej90d79712021-07-27 16:16:21 +0200805#if _LIBCPP_STD_VER > 20
806 basic_string(nullptr_t) = delete;
807#endif
808
Howard Hinnantcf823322010-12-17 14:46:43 +0000809 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000810 basic_string(const _CharT* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +0000811 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000812 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000813 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000814 basic_string(size_type __n, _CharT __c);
Marshall Clowe46031a2018-07-02 18:41:15 +0000815
Louis Dionne9ce598d2021-09-08 09:14:43 -0400816 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000817 _LIBCPP_INLINE_VISIBILITY
818 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
819
Marshall Clow83445802016-04-07 18:13:41 +0000820 basic_string(const basic_string& __str, size_type __pos, size_type __n,
Eric Fiselier812882b2017-02-17 01:17:10 +0000821 const _Allocator& __a = _Allocator());
Marshall Clow83445802016-04-07 18:13:41 +0000822 _LIBCPP_INLINE_VISIBILITY
823 basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +0000824 const _Allocator& __a = _Allocator());
Marshall Clowe46031a2018-07-02 18:41:15 +0000825
Louis Dionne9ce598d2021-09-08 09:14:43 -0400826 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 +0000827 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselier812882b2017-02-17 01:17:10 +0000828 basic_string(const _Tp& __t, size_type __pos, size_type __n,
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500829 const allocator_type& __a = allocator_type());
Marshall Clowe46031a2018-07-02 18:41:15 +0000830
Louis Dionne9ce598d2021-09-08 09:14:43 -0400831 template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500832 !__is_same_uncvref<_Tp, basic_string>::value> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000833 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
834 explicit basic_string(const _Tp& __t);
835
Louis Dionne9ce598d2021-09-08 09:14:43 -0400836 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 +0000837 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
838 explicit basic_string(const _Tp& __t, const allocator_type& __a);
839
Louis Dionne9ce598d2021-09-08 09:14:43 -0400840 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000841 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000842 basic_string(_InputIterator __first, _InputIterator __last);
Louis Dionne9ce598d2021-09-08 09:14:43 -0400843 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000844 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000845 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000846#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000847 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000848 basic_string(initializer_list<_CharT> __il);
Howard Hinnantcf823322010-12-17 14:46:43 +0000849 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000850 basic_string(initializer_list<_CharT> __il, const _Allocator& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400851#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000852
Eric Fiselierd9a702a2016-10-31 03:42:50 +0000853 inline ~basic_string();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000854
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000855 _LIBCPP_INLINE_VISIBILITY
856 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
857
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000858 basic_string& operator=(const basic_string& __str);
Eric Fiselier5ec13b12017-01-23 21:24:58 +0000859
Louis Dionne9ce598d2021-09-08 09:14:43 -0400860 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 +0000861 basic_string& operator=(const _Tp& __t)
862 {__self_view __sv = __t; return assign(__sv);}
863
Eric Fiselierfc92be82017-04-19 00:28:44 +0000864#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000865 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000866 basic_string& operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000867 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiselierfc92be82017-04-19 00:28:44 +0000868 _LIBCPP_INLINE_VISIBILITY
869 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000870#endif
Howard Hinnantd17880b2013-06-28 16:59:19 +0000871 _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const value_type* __s) {return assign(__s);}
Marek Kurdej90d79712021-07-27 16:16:21 +0200872#if _LIBCPP_STD_VER > 20
873 basic_string& operator=(nullptr_t) = delete;
874#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000875 basic_string& operator=(value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000876
Louis Dionneba400782020-10-02 15:02:52 -0400877#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +0000878 _LIBCPP_INLINE_VISIBILITY
879 iterator begin() _NOEXCEPT
880 {return iterator(this, __get_pointer());}
881 _LIBCPP_INLINE_VISIBILITY
882 const_iterator begin() const _NOEXCEPT
883 {return const_iterator(this, __get_pointer());}
884 _LIBCPP_INLINE_VISIBILITY
885 iterator end() _NOEXCEPT
886 {return iterator(this, __get_pointer() + size());}
887 _LIBCPP_INLINE_VISIBILITY
888 const_iterator end() const _NOEXCEPT
889 {return const_iterator(this, __get_pointer() + size());}
890#else
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000891 _LIBCPP_INLINE_VISIBILITY
892 iterator begin() _NOEXCEPT
893 {return iterator(__get_pointer());}
894 _LIBCPP_INLINE_VISIBILITY
895 const_iterator begin() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000896 {return const_iterator(__get_pointer());}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000897 _LIBCPP_INLINE_VISIBILITY
898 iterator end() _NOEXCEPT
899 {return iterator(__get_pointer() + size());}
900 _LIBCPP_INLINE_VISIBILITY
901 const_iterator end() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000902 {return const_iterator(__get_pointer() + size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400903#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000904 _LIBCPP_INLINE_VISIBILITY
905 reverse_iterator rbegin() _NOEXCEPT
906 {return reverse_iterator(end());}
907 _LIBCPP_INLINE_VISIBILITY
908 const_reverse_iterator rbegin() const _NOEXCEPT
909 {return const_reverse_iterator(end());}
910 _LIBCPP_INLINE_VISIBILITY
911 reverse_iterator rend() _NOEXCEPT
912 {return reverse_iterator(begin());}
913 _LIBCPP_INLINE_VISIBILITY
914 const_reverse_iterator rend() const _NOEXCEPT
915 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000916
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000917 _LIBCPP_INLINE_VISIBILITY
918 const_iterator cbegin() const _NOEXCEPT
919 {return begin();}
920 _LIBCPP_INLINE_VISIBILITY
921 const_iterator cend() const _NOEXCEPT
922 {return end();}
923 _LIBCPP_INLINE_VISIBILITY
924 const_reverse_iterator crbegin() const _NOEXCEPT
925 {return rbegin();}
926 _LIBCPP_INLINE_VISIBILITY
927 const_reverse_iterator crend() const _NOEXCEPT
928 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000929
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000930 _LIBCPP_INLINE_VISIBILITY size_type size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000931 {return __is_long() ? __get_long_size() : __get_short_size();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000932 _LIBCPP_INLINE_VISIBILITY size_type length() const _NOEXCEPT {return size();}
933 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT;
934 _LIBCPP_INLINE_VISIBILITY size_type capacity() const _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +0000935 {return (__is_long() ? __get_long_cap()
936 : static_cast<size_type>(__min_cap)) - 1;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000937
938 void resize(size_type __n, value_type __c);
939 _LIBCPP_INLINE_VISIBILITY void resize(size_type __n) {resize(__n, value_type());}
940
Marek Kurdejc9848142020-11-26 10:07:16 +0100941 void reserve(size_type __requested_capacity);
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100942
943#if _LIBCPP_STD_VER > 20
944 template <class _Op>
945 _LIBCPP_HIDE_FROM_ABI constexpr
946 void resize_and_overwrite(size_type __n, _Op __op) {
947 __resize_default_init(__n);
Nikolas Klausera4a76a12022-01-20 13:53:59 +0100948 __erase_to_end(_VSTD::move(__op)(data(), _LIBCPP_AUTO_CAST(__n)));
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100949 }
950#endif
951
Eric Fiselier451d5582018-11-26 20:15:38 +0000952 _LIBCPP_INLINE_VISIBILITY void __resize_default_init(size_type __n);
953
Marek Kurdejc9848142020-11-26 10:07:16 +0100954 _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_INLINE_VISIBILITY
955 void reserve() _NOEXCEPT {shrink_to_fit();}
Marshall Clow6ae12a82018-11-28 18:18:34 +0000956 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejc9848142020-11-26 10:07:16 +0100957 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000958 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000959 void clear() _NOEXCEPT;
Marshall Clowb7db4972017-11-15 20:02:27 +0000960 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
961 bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000962
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000963 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __pos) const _NOEXCEPT;
964 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000965
966 const_reference at(size_type __n) const;
967 reference at(size_type __n);
968
969 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const basic_string& __str) {return append(__str);}
Marshall Clowe46031a2018-07-02 18:41:15 +0000970
971 template <class _Tp>
972 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -0400973 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +0000974 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500975 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
976 && !__is_same_uncvref<_Tp, basic_string >::value,
Marshall Clowe46031a2018-07-02 18:41:15 +0000977 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500978 >
Marshall Clowe46031a2018-07-02 18:41:15 +0000979 operator+=(const _Tp& __t) {__self_view __sv = __t; return append(__sv);}
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000980 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const value_type* __s) {return append(__s);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000981 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(value_type __c) {push_back(__c); return *this;}
Eric Fiselierfc92be82017-04-19 00:28:44 +0000982#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000983 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(initializer_list<value_type> __il) {return append(__il);}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400984#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000985
Howard Hinnantcf823322010-12-17 14:46:43 +0000986 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000987 basic_string& append(const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000988
989 template <class _Tp>
990 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -0400991 __enable_if_t<
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500992 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
993 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clowe46031a2018-07-02 18:41:15 +0000994 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500995 >
Marshall Clowe46031a2018-07-02 18:41:15 +0000996 append(const _Tp& __t) { __self_view __sv = __t; return append(__sv.data(), __sv.size()); }
Marshall Clow8db7fb02014-03-04 19:17:19 +0000997 basic_string& append(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clowe46031a2018-07-02 18:41:15 +0000998
Marshall Clow62953962016-10-03 23:40:48 +0000999 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001000 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001001 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001002 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001003 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1004 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001005 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001006 >
Marshall Clow82513342016-09-24 22:45:42 +00001007 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001008 basic_string& append(const value_type* __s, size_type __n);
1009 basic_string& append(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001010 basic_string& append(size_type __n, value_type __c);
Eric Fiselier451d5582018-11-26 20:15:38 +00001011
1012 _LIBCPP_INLINE_VISIBILITY
1013 void __append_default_init(size_type __n);
1014
Howard Hinnantc51e1022010-05-11 19:42:16 +00001015 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001016 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001017 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001018 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001019 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001020 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001021 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001022 _LIBCPP_INLINE_VISIBILITY
1023 append(_InputIterator __first, _InputIterator __last) {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001024 const basic_string __temp(__first, __last, __alloc());
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001025 append(__temp.data(), __temp.size());
1026 return *this;
1027 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001028 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001029 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001030 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001031 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001032 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001033 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001034 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001035 _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001036 append(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001037
Eric Fiselierfc92be82017-04-19 00:28:44 +00001038#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001039 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001040 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001041#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001042
1043 void push_back(value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001044 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001045 void pop_back();
Marshall Clow05cf6692019-03-19 03:30:07 +00001046 _LIBCPP_INLINE_VISIBILITY reference front() _NOEXCEPT;
1047 _LIBCPP_INLINE_VISIBILITY const_reference front() const _NOEXCEPT;
1048 _LIBCPP_INLINE_VISIBILITY reference back() _NOEXCEPT;
1049 _LIBCPP_INLINE_VISIBILITY const_reference back() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001050
Marshall Clowe46031a2018-07-02 18:41:15 +00001051 template <class _Tp>
1052 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001053 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001054 <
1055 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1056 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001057 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001058 assign(const _Tp & __t) { __self_view __sv = __t; return assign(__sv.data(), __sv.size()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001059 _LIBCPP_INLINE_VISIBILITY
Marshall Clow95d5e9a2016-03-09 18:08:29 +00001060 basic_string& assign(const basic_string& __str) { return *this = __str; }
Eric Fiselierfc92be82017-04-19 00:28:44 +00001061#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001062 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001063 basic_string& assign(basic_string&& __str)
Marshall Clow5aa9e942015-10-05 16:17:34 +00001064 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001065 {*this = _VSTD::move(__str); return *this;}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001066#endif
Marshall Clow8db7fb02014-03-04 19:17:19 +00001067 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow62953962016-10-03 23:40:48 +00001068 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001069 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001070 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001071 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001072 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1073 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001074 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001075 >
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001076 assign(const _Tp & __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001077 basic_string& assign(const value_type* __s, size_type __n);
1078 basic_string& assign(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001079 basic_string& assign(size_type __n, value_type __c);
1080 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001081 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001082 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001083 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001084 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001085 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001086 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001087 assign(_InputIterator __first, _InputIterator __last);
1088 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001089 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001090 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001091 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001092 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001093 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001094 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001095 assign(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001096#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001097 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001098 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001099#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001100
Howard Hinnantcf823322010-12-17 14:46:43 +00001101 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001102 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001103
1104 template <class _Tp>
1105 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001106 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001107 <
1108 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1109 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001110 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001111 insert(size_type __pos1, const _Tp& __t)
1112 { __self_view __sv = __t; return insert(__pos1, __sv.data(), __sv.size()); }
1113
Marshall Clow82513342016-09-24 22:45:42 +00001114 template <class _Tp>
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
Marshall Clow82513342016-09-24 22:45:42 +00001117 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001118 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001119 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001120 >
Marshall Clow82513342016-09-24 22:45:42 +00001121 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Marshall Clow8db7fb02014-03-04 19:17:19 +00001122 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001123 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1124 basic_string& insert(size_type __pos, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001125 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1126 iterator insert(const_iterator __pos, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001127 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001128 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1129 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001130 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001131 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001132 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001133 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001134 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001135 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001136 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1137 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001138 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001139 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001140 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001141 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001142 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001143 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001144 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001145#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001146 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001147 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1148 {return insert(__pos, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001149#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001150
1151 basic_string& erase(size_type __pos = 0, size_type __n = npos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001152 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001153 iterator erase(const_iterator __pos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001154 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001155 iterator erase(const_iterator __first, const_iterator __last);
1156
Howard Hinnantcf823322010-12-17 14:46:43 +00001157 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001158 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001159
1160 template <class _Tp>
1161 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001162 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001163 <
1164 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1165 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001166 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001167 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 +00001168 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 +00001169 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001170 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001171 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001172 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001173 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001174 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001175 >
Marshall Clow82513342016-09-24 22:45:42 +00001176 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001177 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
1178 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001179 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001180 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001181 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001182
1183 template <class _Tp>
1184 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001185 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001186 <
1187 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1188 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001189 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001190 replace(const_iterator __i1, const_iterator __i2, const _Tp& __t) { __self_view __sv = __t; return replace(__i1 - begin(), __i2 - __i1, __sv); }
1191
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001192 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001193 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +00001194 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001195 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Howard Hinnantcf823322010-12-17 14:46:43 +00001196 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001197 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001198 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001199 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001200 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001201 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001202 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001203 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001204 >
Howard Hinnant990d6e82010-11-17 21:11:40 +00001205 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001206#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001207 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001208 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001209 {return replace(__i1, __i2, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001210#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001211
Howard Hinnantd17880b2013-06-28 16:59:19 +00001212 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
Howard Hinnantcf823322010-12-17 14:46:43 +00001213 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001214 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1215
Howard Hinnantcf823322010-12-17 14:46:43 +00001216 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001217 void swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001218#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00001219 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +00001220#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001221 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001222 __is_nothrow_swappable<allocator_type>::value);
1223#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001224
Howard Hinnantcf823322010-12-17 14:46:43 +00001225 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001226 const value_type* c_str() const _NOEXCEPT {return data();}
Howard Hinnantcf823322010-12-17 14:46:43 +00001227 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001228 const value_type* data() const _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Eric Fiselier4ee2a892017-11-20 20:23:27 +00001229#if _LIBCPP_STD_VER > 14 || defined(_LIBCPP_BUILDING_LIBRARY)
Marshall Clowd3c22392016-03-08 15:44:30 +00001230 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001231 value_type* data() _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Marshall Clowd3c22392016-03-08 15:44:30 +00001232#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001233
Howard Hinnantcf823322010-12-17 14:46:43 +00001234 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001235 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001236
Howard Hinnantcf823322010-12-17 14:46:43 +00001237 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001238 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001239
1240 template <class _Tp>
1241 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001242 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001243 <
1244 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1245 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001246 >
zoecarver1997e0a2021-02-05 11:54:47 -08001247 find(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001248 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001249 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001250 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001251 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001252
Howard Hinnantcf823322010-12-17 14:46:43 +00001253 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001254 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001255
1256 template <class _Tp>
1257 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001258 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001259 <
1260 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1261 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001262 >
zoecarver1997e0a2021-02-05 11:54:47 -08001263 rfind(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001264 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001265 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001266 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001267 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001268
Howard Hinnantcf823322010-12-17 14:46:43 +00001269 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001270 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001271
1272 template <class _Tp>
1273 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001274 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001275 <
1276 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1277 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001278 >
zoecarver1997e0a2021-02-05 11:54:47 -08001279 find_first_of(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001280 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001281 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001282 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001283 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001284 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001285
Howard Hinnantcf823322010-12-17 14:46:43 +00001286 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001287 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001288
1289 template <class _Tp>
1290 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001291 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001292 <
1293 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1294 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001295 >
zoecarver1997e0a2021-02-05 11:54:47 -08001296 find_last_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001297 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001298 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001299 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001300 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001301 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001302
Howard Hinnantcf823322010-12-17 14:46:43 +00001303 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001304 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001305
1306 template <class _Tp>
1307 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001308 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001309 <
1310 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1311 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001312 >
zoecarver1997e0a2021-02-05 11:54:47 -08001313 find_first_not_of(const _Tp &__t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001314 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 +00001315 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001316 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001317 _LIBCPP_INLINE_VISIBILITY
1318 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1319
1320 _LIBCPP_INLINE_VISIBILITY
1321 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001322
1323 template <class _Tp>
1324 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001325 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001326 <
1327 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1328 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001329 >
zoecarver1997e0a2021-02-05 11:54:47 -08001330 find_last_not_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001331 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 +00001332 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001333 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001334 _LIBCPP_INLINE_VISIBILITY
1335 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1336
1337 _LIBCPP_INLINE_VISIBILITY
1338 int compare(const basic_string& __str) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001339
1340 template <class _Tp>
1341 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001342 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001343 <
1344 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1345 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001346 >
zoecarver1997e0a2021-02-05 11:54:47 -08001347 compare(const _Tp &__t) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001348
1349 template <class _Tp>
1350 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001351 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001352 <
1353 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1354 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001355 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001356 compare(size_type __pos1, size_type __n1, const _Tp& __t) const;
1357
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001358 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001359 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Marshall Clow8db7fb02014-03-04 19:17:19 +00001360 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 +00001361
Marshall Clow82513342016-09-24 22:45:42 +00001362 template <class _Tp>
Eric Fiselier7d4aa5c2016-10-14 05:29:46 +00001363 inline _LIBCPP_INLINE_VISIBILITY
Louis Dionne9ce598d2021-09-08 09:14:43 -04001364 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001365 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001366 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001367 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001368 >
Marshall Clow82513342016-09-24 22:45:42 +00001369 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 +00001370 int compare(const value_type* __s) const _NOEXCEPT;
1371 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1372 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001373
Marshall Clow18c293b2017-12-04 20:11:38 +00001374#if _LIBCPP_STD_VER > 17
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001375 constexpr _LIBCPP_INLINE_VISIBILITY
1376 bool starts_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001377 { return __self_view(data(), size()).starts_with(__sv); }
1378
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001379 constexpr _LIBCPP_INLINE_VISIBILITY
1380 bool starts_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001381 { return !empty() && _Traits::eq(front(), __c); }
1382
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001383 constexpr _LIBCPP_INLINE_VISIBILITY
1384 bool starts_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001385 { return starts_with(__self_view(__s)); }
1386
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001387 constexpr _LIBCPP_INLINE_VISIBILITY
1388 bool ends_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001389 { return __self_view(data(), size()).ends_with( __sv); }
1390
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001391 constexpr _LIBCPP_INLINE_VISIBILITY
1392 bool ends_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001393 { return !empty() && _Traits::eq(back(), __c); }
1394
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001395 constexpr _LIBCPP_INLINE_VISIBILITY
1396 bool ends_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001397 { return ends_with(__self_view(__s)); }
1398#endif
1399
Wim Leflere023c3542021-01-19 14:33:30 -05001400#if _LIBCPP_STD_VER > 20
1401 constexpr _LIBCPP_INLINE_VISIBILITY
1402 bool contains(__self_view __sv) const noexcept
1403 { return __self_view(data(), size()).contains(__sv); }
1404
1405 constexpr _LIBCPP_INLINE_VISIBILITY
1406 bool contains(value_type __c) const noexcept
1407 { return __self_view(data(), size()).contains(__c); }
1408
1409 constexpr _LIBCPP_INLINE_VISIBILITY
1410 bool contains(const value_type* __s) const
1411 { return __self_view(data(), size()).contains(__s); }
1412#endif
1413
Howard Hinnantcf823322010-12-17 14:46:43 +00001414 _LIBCPP_INLINE_VISIBILITY bool __invariants() const;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001415
Marshall Clowe60a7182018-05-29 17:04:37 +00001416 _LIBCPP_INLINE_VISIBILITY void __clear_and_shrink() _NOEXCEPT;
Marshall Clow851b9ec2019-05-20 21:56:51 +00001417
Marek Kurdejc9848142020-11-26 10:07:16 +01001418 _LIBCPP_INLINE_VISIBILITY void __shrink_or_extend(size_type __target_capacity);
1419
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001420 _LIBCPP_INLINE_VISIBILITY
1421 bool __is_long() const _NOEXCEPT
1422 {return bool(__r_.first().__s.__size_ & __short_mask);}
1423
Louis Dionneba400782020-10-02 15:02:52 -04001424#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001425
1426 bool __dereferenceable(const const_iterator* __i) const;
1427 bool __decrementable(const const_iterator* __i) const;
1428 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1429 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1430
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001431#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001432
Howard Hinnantc51e1022010-05-11 19:42:16 +00001433private:
Nikolas Klauser93826d12022-01-04 17:24:03 +01001434 _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI static bool __fits_in_sso(size_type __sz) {
1435 // SSO is disabled during constant evaluation because `__is_long` isn't constexpr friendly
1436 return !__libcpp_is_constant_evaluated() && (__sz < __min_cap);
1437 }
1438
Nikolas Klauser48d680d2022-02-26 13:28:33 +01001439 template <class _ForwardIterator>
1440 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
1441 iterator __insert_from_safe_copy(size_type __n, size_type __ip, _ForwardIterator __first, _ForwardIterator __last) {
1442 size_type __sz = size();
1443 size_type __cap = capacity();
1444 value_type* __p;
1445 if (__cap - __sz >= __n)
1446 {
1447 __p = std::__to_address(__get_pointer());
1448 size_type __n_move = __sz - __ip;
1449 if (__n_move != 0)
1450 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
1451 }
1452 else
1453 {
1454 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
1455 __p = std::__to_address(__get_long_pointer());
1456 }
1457 __sz += __n;
1458 __set_size(__sz);
1459 traits_type::assign(__p[__sz], value_type());
1460 for (__p += __ip; __first != __last; ++__p, ++__first)
1461 traits_type::assign(*__p, *__first);
1462
1463 return begin() + __ip;
1464 }
1465
1466 _LIBCPP_HIDE_FROM_ABI allocator_type& __alloc() _NOEXCEPT { return __r_.second(); }
1467 _LIBCPP_HIDE_FROM_ABI const allocator_type& __alloc() const _NOEXCEPT { return __r_.second(); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001468
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001469#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001470
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001471 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001472 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001473# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001474 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001475# else
1476 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1477# endif
1478
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001479 _LIBCPP_INLINE_VISIBILITY
1480 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001481# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001482 {return __r_.first().__s.__size_ >> 1;}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001483# else
1484 {return __r_.first().__s.__size_;}
1485# endif
1486
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001487#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001488
1489 _LIBCPP_INLINE_VISIBILITY
1490 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001491# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001492 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1493# else
1494 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
1495# endif
1496
1497 _LIBCPP_INLINE_VISIBILITY
1498 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001499# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001500 {return __r_.first().__s.__size_;}
1501# else
1502 {return __r_.first().__s.__size_ >> 1;}
1503# endif
1504
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001505#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001506
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001507 _LIBCPP_INLINE_VISIBILITY
1508 void __set_long_size(size_type __s) _NOEXCEPT
1509 {__r_.first().__l.__size_ = __s;}
1510 _LIBCPP_INLINE_VISIBILITY
1511 size_type __get_long_size() const _NOEXCEPT
1512 {return __r_.first().__l.__size_;}
1513 _LIBCPP_INLINE_VISIBILITY
1514 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001515 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1516
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001517 _LIBCPP_INLINE_VISIBILITY
1518 void __set_long_cap(size_type __s) _NOEXCEPT
1519 {__r_.first().__l.__cap_ = __long_mask | __s;}
1520 _LIBCPP_INLINE_VISIBILITY
1521 size_type __get_long_cap() const _NOEXCEPT
Howard Hinnant28b24882011-12-01 20:21:04 +00001522 {return __r_.first().__l.__cap_ & size_type(~__long_mask);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001523
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001524 _LIBCPP_INLINE_VISIBILITY
1525 void __set_long_pointer(pointer __p) _NOEXCEPT
1526 {__r_.first().__l.__data_ = __p;}
1527 _LIBCPP_INLINE_VISIBILITY
1528 pointer __get_long_pointer() _NOEXCEPT
1529 {return __r_.first().__l.__data_;}
1530 _LIBCPP_INLINE_VISIBILITY
1531 const_pointer __get_long_pointer() const _NOEXCEPT
1532 {return __r_.first().__l.__data_;}
1533 _LIBCPP_INLINE_VISIBILITY
1534 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001535 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001536 _LIBCPP_INLINE_VISIBILITY
1537 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001538 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001539 _LIBCPP_INLINE_VISIBILITY
1540 pointer __get_pointer() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001541 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001542 _LIBCPP_INLINE_VISIBILITY
1543 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001544 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1545
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001546 _LIBCPP_INLINE_VISIBILITY
1547 void __zero() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001548 {
1549 size_type (&__a)[__n_words] = __r_.first().__r.__words;
1550 for (unsigned __i = 0; __i < __n_words; ++__i)
1551 __a[__i] = 0;
1552 }
1553
1554 template <size_type __a> static
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001555 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00001556 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +00001557 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001558 enum {__alignment = 16};
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001559 static _LIBCPP_INLINE_VISIBILITY
1560 size_type __recommend(size_type __s) _NOEXCEPT
Marshall Clow80584522018-02-07 21:30:17 +00001561 {
1562 if (__s < __min_cap) return static_cast<size_type>(__min_cap) - 1;
1563 size_type __guess = __align_it<sizeof(value_type) < __alignment ?
1564 __alignment/sizeof(value_type) : 1 > (__s+1) - 1;
1565 if (__guess == __min_cap) ++__guess;
1566 return __guess;
1567 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001568
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001569 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001570 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001571 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001572 void __init(const value_type* __s, size_type __sz);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001573 inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001574 void __init(size_type __n, value_type __c);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001575
Martijn Vels5e7c9752020-03-04 17:52:46 -05001576 // Slow path for the (inlined) copy constructor for 'long' strings.
1577 // Always externally instantiated and not inlined.
1578 // Requires that __s is zero terminated.
1579 // The main reason for this function to exist is because for unstable, we
1580 // want to allow inlining of the copy constructor. However, we don't want
1581 // to call the __init() functions as those are marked as inline which may
1582 // result in over-aggressive inlining by the compiler, where our aim is
1583 // to only inline the fast path code directly in the ctor.
1584 void __init_copy_ctor_external(const value_type* __s, size_type __sz);
1585
Howard Hinnantc51e1022010-05-11 19:42:16 +00001586 template <class _InputIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001587 inline
Louis Dionne9ce598d2021-09-08 09:14:43 -04001588 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001589 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001590 __is_exactly_cpp17_input_iterator<_InputIterator>::value
1591 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001592 __init(_InputIterator __first, _InputIterator __last);
1593
1594 template <class _ForwardIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001595 inline
Louis Dionne9ce598d2021-09-08 09:14:43 -04001596 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001597 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001598 __is_cpp17_forward_iterator<_ForwardIterator>::value
1599 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001600 __init(_ForwardIterator __first, _ForwardIterator __last);
1601
1602 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001603 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001604 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1605 size_type __n_copy, size_type __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00001606 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001607
Martijn Vels596e3de2020-02-26 15:55:49 -05001608 // __assign_no_alias is invoked for assignment operations where we
1609 // have proof that the input does not alias the current instance.
1610 // For example, operator=(basic_string) performs a 'self' check.
1611 template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04001612 basic_string& __assign_no_alias(const value_type* __s, size_type __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05001613
Howard Hinnantcf823322010-12-17 14:46:43 +00001614 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001615 void __erase_to_end(size_type __pos);
1616
Martijn Velsa81fc792020-02-26 13:25:43 -05001617 // __erase_external_with_move is invoked for erase() invocations where
1618 // `n ~= npos`, likely requiring memory moves on the string data.
1619 void __erase_external_with_move(size_type __pos, size_type __n);
1620
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001621 _LIBCPP_INLINE_VISIBILITY
1622 void __copy_assign_alloc(const basic_string& __str)
1623 {__copy_assign_alloc(__str, integral_constant<bool,
1624 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1625
1626 _LIBCPP_INLINE_VISIBILITY
1627 void __copy_assign_alloc(const basic_string& __str, true_type)
1628 {
Marshall Clowf258c202017-01-31 03:40:52 +00001629 if (__alloc() == __str.__alloc())
1630 __alloc() = __str.__alloc();
1631 else
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001632 {
Marshall Clowf258c202017-01-31 03:40:52 +00001633 if (!__str.__is_long())
1634 {
Vedant Kumar55e007e2018-03-08 21:15:26 +00001635 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001636 __alloc() = __str.__alloc();
1637 }
1638 else
1639 {
1640 allocator_type __a = __str.__alloc();
1641 pointer __p = __alloc_traits::allocate(__a, __str.__get_long_cap());
Vedant Kumar55e007e2018-03-08 21:15:26 +00001642 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001643 __alloc() = _VSTD::move(__a);
1644 __set_long_pointer(__p);
1645 __set_long_cap(__str.__get_long_cap());
1646 __set_long_size(__str.size());
1647 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001648 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001649 }
1650
1651 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001652 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001653 {}
1654
Eric Fiselierfc92be82017-04-19 00:28:44 +00001655#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +00001656 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001657 void __move_assign(basic_string& __str, false_type)
1658 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnantcf823322010-12-17 14:46:43 +00001659 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001660 void __move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001661#if _LIBCPP_STD_VER > 14
1662 _NOEXCEPT;
1663#else
Howard Hinnant3e276872011-06-03 18:40:47 +00001664 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001665#endif
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001666#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001667
1668 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001669 void
Howard Hinnantc2734962011-09-02 20:42:31 +00001670 __move_assign_alloc(basic_string& __str)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001671 _NOEXCEPT_(
1672 !__alloc_traits::propagate_on_container_move_assignment::value ||
1673 is_nothrow_move_assignable<allocator_type>::value)
1674 {__move_assign_alloc(__str, integral_constant<bool,
1675 __alloc_traits::propagate_on_container_move_assignment::value>());}
1676
1677 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00001678 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001679 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1680 {
1681 __alloc() = _VSTD::move(__c.__alloc());
1682 }
1683
1684 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001685 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001686 _NOEXCEPT
1687 {}
1688
Martijn Velsda7d94f2020-06-19 14:24:03 -04001689 basic_string& __assign_external(const value_type* __s);
1690 basic_string& __assign_external(const value_type* __s, size_type __n);
1691
1692 // Assigns the value in __s, guaranteed to be __n < __min_cap in length.
1693 inline basic_string& __assign_short(const value_type* __s, size_type __n) {
1694 pointer __p = __is_long()
1695 ? (__set_long_size(__n), __get_long_pointer())
1696 : (__set_short_size(__n), __get_short_pointer());
1697 traits_type::move(_VSTD::__to_address(__p), __s, __n);
1698 traits_type::assign(__p[__n], value_type());
1699 return *this;
1700 }
1701
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01001702 _LIBCPP_HIDE_FROM_ABI basic_string& __null_terminate_at(value_type* __p, size_type __newsz) {
1703 __set_size(__newsz);
1704 __invalidate_iterators_past(__newsz);
1705 traits_type::assign(__p[__newsz], value_type());
1706 return *this;
1707 }
1708
Howard Hinnantcf823322010-12-17 14:46:43 +00001709 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
1710 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(size_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001711
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001712 template<class _Tp>
1713 _LIBCPP_INLINE_VISIBILITY
1714 bool __addr_in_range(_Tp&& __t) const {
1715 const volatile void *__p = _VSTD::addressof(__t);
1716 return data() <= __p && __p <= data() + size();
1717 }
1718
Louis Dionned24191c2021-08-19 12:39:16 -04001719 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1720 void __throw_length_error() const {
Nikolas Klauserf89bb932022-01-24 19:44:34 +01001721 _VSTD::__throw_length_error("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001722 }
1723
1724 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1725 void __throw_out_of_range() const {
Nikolas Klauserf89bb932022-01-24 19:44:34 +01001726 _VSTD::__throw_out_of_range("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001727 }
1728
Howard Hinnantc51e1022010-05-11 19:42:16 +00001729 friend basic_string operator+<>(const basic_string&, const basic_string&);
1730 friend basic_string operator+<>(const value_type*, const basic_string&);
1731 friend basic_string operator+<>(value_type, const basic_string&);
1732 friend basic_string operator+<>(const basic_string&, const value_type*);
1733 friend basic_string operator+<>(const basic_string&, value_type);
1734};
1735
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001736// These declarations must appear before any functions are implicitly used
1737// so that they have the correct visibility specifier.
1738#ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
Louis Dionne89258142021-08-23 15:32:36 -04001739 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1740# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1741 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1742# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001743#else
Louis Dionne89258142021-08-23 15:32:36 -04001744 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1745# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1746 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1747# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001748#endif
1749
1750
Louis Dionned59f8a52021-08-17 11:59:07 -04001751#if _LIBCPP_STD_VER >= 17
Marshall Clowa0563332018-02-08 06:34:03 +00001752template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -05001753 class _CharT = __iter_value_type<_InputIterator>,
Marshall Clowa0563332018-02-08 06:34:03 +00001754 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001755 class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
1756 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowa0563332018-02-08 06:34:03 +00001757 >
1758basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
1759 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
Marshall Clowe46031a2018-07-02 18:41:15 +00001760
1761template<class _CharT,
1762 class _Traits,
1763 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001764 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowe46031a2018-07-02 18:41:15 +00001765 >
1766explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
1767 -> basic_string<_CharT, _Traits, _Allocator>;
1768
1769template<class _CharT,
1770 class _Traits,
1771 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001772 class = enable_if_t<__is_allocator<_Allocator>::value>,
Marshall Clowe46031a2018-07-02 18:41:15 +00001773 class _Sz = typename allocator_traits<_Allocator>::size_type
1774 >
1775basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
1776 -> basic_string<_CharT, _Traits, _Allocator>;
Marshall Clowa0563332018-02-08 06:34:03 +00001777#endif
1778
Howard Hinnantc51e1022010-05-11 19:42:16 +00001779template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001780inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001781void
1782basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
1783{
Louis Dionneba400782020-10-02 15:02:52 -04001784#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001785 if (!__libcpp_is_constant_evaluated())
1786 __get_db()->__invalidate_all(this);
Louis Dionneba400782020-10-02 15:02:52 -04001787#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001788}
1789
1790template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001791inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001792void
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001793basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type __pos)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001794{
Louis Dionneba400782020-10-02 15:02:52 -04001795#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001796 if (!__libcpp_is_constant_evaluated()) {
1797 __c_node* __c = __get_db()->__find_c_and_lock(this);
1798 if (__c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001799 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001800 const_pointer __new_last = __get_pointer() + __pos;
1801 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001802 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001803 --__p;
1804 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1805 if (__i->base() > __new_last)
1806 {
1807 (*__p)->__c_ = nullptr;
1808 if (--__c->end_ != __p)
1809 _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1810 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001811 }
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001812 __get_db()->unlock();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001813 }
1814 }
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001815#else
1816 (void)__pos;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001817#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001818}
1819
1820template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001821inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001822basic_string<_CharT, _Traits, _Allocator>::basic_string()
Marshall Clowe546cbb2015-06-04 02:05:41 +00001823 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001824 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001825{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001826 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001827 __zero();
1828}
1829
1830template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001831inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001832basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
Eric Fiseliere012bf22015-07-18 20:40:46 +00001833#if _LIBCPP_STD_VER <= 14
1834 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1835#else
1836 _NOEXCEPT
1837#endif
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001838: __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001839{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001840 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001841 __zero();
1842}
1843
1844template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier815ed732016-09-16 00:00:48 +00001845void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1846 size_type __sz,
1847 size_type __reserve)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001848{
1849 if (__reserve > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001850 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001851 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001852 if (__fits_in_sso(__reserve))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001853 {
1854 __set_short_size(__sz);
1855 __p = __get_short_pointer();
1856 }
1857 else
1858 {
1859 size_type __cap = __recommend(__reserve);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001860 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001861 __set_long_pointer(__p);
1862 __set_long_cap(__cap+1);
1863 __set_long_size(__sz);
1864 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001865 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001866 traits_type::assign(__p[__sz], value_type());
1867}
1868
1869template <class _CharT, class _Traits, class _Allocator>
1870void
Howard Hinnantd17880b2013-06-28 16:59:19 +00001871basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001872{
1873 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001874 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001875 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001876 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001877 {
1878 __set_short_size(__sz);
1879 __p = __get_short_pointer();
1880 }
1881 else
1882 {
1883 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001884 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001885 __set_long_pointer(__p);
1886 __set_long_cap(__cap+1);
1887 __set_long_size(__sz);
1888 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001889 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001890 traits_type::assign(__p[__sz], value_type());
1891}
1892
1893template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00001894template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00001895basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001896 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001897{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001898 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001899 __init(__s, traits_type::length(__s));
Nikolas Klauserf2807732022-01-11 00:33:35 +01001900 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001901}
1902
1903template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001904inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001905basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001906 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001907{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001908 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
1909 __init(__s, __n);
1910 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001911}
1912
1913template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001914inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001915basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001916 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001917{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001918 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001919 __init(__s, __n);
Nikolas Klauserf2807732022-01-11 00:33:35 +01001920 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001921}
1922
1923template <class _CharT, class _Traits, class _Allocator>
1924basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001925 : __r_(__default_init_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001926{
1927 if (!__str.__is_long())
1928 __r_.first().__r = __str.__r_.first().__r;
1929 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001930 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1931 __str.__get_long_size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01001932 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001933}
1934
1935template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00001936basic_string<_CharT, _Traits, _Allocator>::basic_string(
1937 const basic_string& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001938 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001939{
1940 if (!__str.__is_long())
1941 __r_.first().__r = __str.__r_.first().__r;
1942 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001943 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1944 __str.__get_long_size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01001945 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001946}
1947
Martijn Vels5e7c9752020-03-04 17:52:46 -05001948template <class _CharT, class _Traits, class _Allocator>
1949void basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(
1950 const value_type* __s, size_type __sz) {
1951 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001952 if (__fits_in_sso(__sz)) {
Martijn Vels5e7c9752020-03-04 17:52:46 -05001953 __p = __get_short_pointer();
1954 __set_short_size(__sz);
1955 } else {
1956 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001957 __throw_length_error();
Martijn Vels5e7c9752020-03-04 17:52:46 -05001958 size_t __cap = __recommend(__sz);
1959 __p = __alloc_traits::allocate(__alloc(), __cap + 1);
1960 __set_long_pointer(__p);
1961 __set_long_cap(__cap + 1);
1962 __set_long_size(__sz);
1963 }
1964 traits_type::copy(_VSTD::__to_address(__p), __s, __sz + 1);
1965}
1966
Eric Fiselierfc92be82017-04-19 00:28:44 +00001967#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001968
1969template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001970inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001971basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +00001972#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +00001973 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowa80abc72015-06-03 19:56:43 +00001974#else
1975 _NOEXCEPT
1976#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001977 : __r_(_VSTD::move(__str.__r_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001978{
1979 __str.__zero();
Nikolas Klauserf2807732022-01-11 00:33:35 +01001980 _VSTD::__debug_db_insert_c(this);
Louis Dionneba400782020-10-02 15:02:52 -04001981#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauserf2807732022-01-11 00:33:35 +01001982 if (!__libcpp_is_constant_evaluated() && __is_long())
1983 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001984#endif
1985}
1986
1987template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001988inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001989basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001990 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001991{
Marshall Clowd2d24692014-07-17 15:32:20 +00001992 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001993 __init(_VSTD::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd2d24692014-07-17 15:32:20 +00001994 else
1995 {
1996 __r_.first().__r = __str.__r_.first().__r;
1997 __str.__zero();
1998 }
Nikolas Klauserf2807732022-01-11 00:33:35 +01001999 _VSTD::__debug_db_insert_c(this);
Louis Dionneba400782020-10-02 15:02:52 -04002000#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauserf2807732022-01-11 00:33:35 +01002001 if (!__libcpp_is_constant_evaluated() && __is_long())
2002 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002003#endif
2004}
2005
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002006#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002007
2008template <class _CharT, class _Traits, class _Allocator>
2009void
2010basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
2011{
2012 if (__n > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002013 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002014 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002015 if (__fits_in_sso(__n))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002016 {
2017 __set_short_size(__n);
2018 __p = __get_short_pointer();
2019 }
2020 else
2021 {
2022 size_type __cap = __recommend(__n);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002023 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002024 __set_long_pointer(__p);
2025 __set_long_cap(__cap+1);
2026 __set_long_size(__n);
2027 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002028 traits_type::assign(_VSTD::__to_address(__p), __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002029 traits_type::assign(__p[__n], value_type());
2030}
2031
2032template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002033inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002034basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002035 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002036{
2037 __init(__n, __c);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002038 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002039}
2040
2041template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002042template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00002043basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002044 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002045{
2046 __init(__n, __c);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002047 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002048}
2049
Howard Hinnantc51e1022010-05-11 19:42:16 +00002050template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00002051basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
2052 size_type __pos, size_type __n,
2053 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002054 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002055{
2056 size_type __str_sz = __str.size();
2057 if (__pos > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002058 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002059 __init(__str.data() + __pos, _VSTD::min(__n, __str_sz - __pos));
Nikolas Klauserf2807732022-01-11 00:33:35 +01002060 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002061}
2062
2063template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002064inline
Marshall Clow83445802016-04-07 18:13:41 +00002065basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +00002066 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002067 : __r_(__default_init_tag(), __a)
Marshall Clow83445802016-04-07 18:13:41 +00002068{
2069 size_type __str_sz = __str.size();
2070 if (__pos > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002071 __throw_out_of_range();
Marshall Clow83445802016-04-07 18:13:41 +00002072 __init(__str.data() + __pos, __str_sz - __pos);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002073 _VSTD::__debug_db_insert_c(this);
Marshall Clow83445802016-04-07 18:13:41 +00002074}
2075
2076template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002077template <class _Tp, class>
Marshall Clow78dbe462016-11-14 18:22:19 +00002078basic_string<_CharT, _Traits, _Allocator>::basic_string(
Marshall Clowe46031a2018-07-02 18:41:15 +00002079 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002080 : __r_(__default_init_tag(), __a)
Marshall Clow78dbe462016-11-14 18:22:19 +00002081{
Marshall Clowe46031a2018-07-02 18:41:15 +00002082 __self_view __sv0 = __t;
2083 __self_view __sv = __sv0.substr(__pos, __n);
Marshall Clow78dbe462016-11-14 18:22:19 +00002084 __init(__sv.data(), __sv.size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002085 _VSTD::__debug_db_insert_c(this);
Marshall Clow78dbe462016-11-14 18:22:19 +00002086}
2087
2088template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002089template <class _Tp, class>
2090basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002091 : __r_(__default_init_tag(), __default_init_tag())
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002092{
Marshall Clowe46031a2018-07-02 18:41:15 +00002093 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002094 __init(__sv.data(), __sv.size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002095 _VSTD::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002096}
2097
2098template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002099template <class _Tp, class>
2100basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002101 : __r_(__default_init_tag(), __a)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002102{
Marshall Clowe46031a2018-07-02 18:41:15 +00002103 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002104 __init(__sv.data(), __sv.size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002105 _VSTD::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002106}
2107
2108template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002109template <class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002110__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002111<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002112 __is_exactly_cpp17_input_iterator<_InputIterator>::value
2113>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002114basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
2115{
2116 __zero();
2117#ifndef _LIBCPP_NO_EXCEPTIONS
2118 try
2119 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002120#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002121 for (; __first != __last; ++__first)
2122 push_back(*__first);
2123#ifndef _LIBCPP_NO_EXCEPTIONS
2124 }
2125 catch (...)
2126 {
2127 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002128 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002129 throw;
2130 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002131#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002132}
2133
2134template <class _CharT, class _Traits, class _Allocator>
2135template <class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002136__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002137<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002138 __is_cpp17_forward_iterator<_ForwardIterator>::value
2139>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002140basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
2141{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002142 size_type __sz = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002143 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002144 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002145 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002146 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002147 {
2148 __set_short_size(__sz);
2149 __p = __get_short_pointer();
2150 }
2151 else
2152 {
2153 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002154 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002155 __set_long_pointer(__p);
2156 __set_long_cap(__cap+1);
2157 __set_long_size(__sz);
2158 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002159
2160#ifndef _LIBCPP_NO_EXCEPTIONS
2161 try
2162 {
2163#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002164 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002165 traits_type::assign(*__p, *__first);
2166 traits_type::assign(*__p, value_type());
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002167#ifndef _LIBCPP_NO_EXCEPTIONS
2168 }
2169 catch (...)
2170 {
2171 if (__is_long())
2172 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
2173 throw;
2174 }
2175#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002176}
2177
2178template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002179template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002180inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002181basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002182 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002183{
2184 __init(__first, __last);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002185 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002186}
2187
2188template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002189template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002190inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002191basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
2192 const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002193 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002194{
2195 __init(__first, __last);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002196 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002197}
2198
Eric Fiselierfc92be82017-04-19 00:28:44 +00002199#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002200
Howard Hinnantc51e1022010-05-11 19:42:16 +00002201template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002202inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002203basic_string<_CharT, _Traits, _Allocator>::basic_string(
2204 initializer_list<_CharT> __il)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002205 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002206{
2207 __init(__il.begin(), __il.end());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002208 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002209}
2210
2211template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002212inline
Eric Fiselier9d355982017-04-12 23:45:53 +00002213
Eric Fiselier812882b2017-02-17 01:17:10 +00002214basic_string<_CharT, _Traits, _Allocator>::basic_string(
2215 initializer_list<_CharT> __il, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002216 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002217{
2218 __init(__il.begin(), __il.end());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002219 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002220}
2221
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002222#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002223
Howard Hinnantc51e1022010-05-11 19:42:16 +00002224template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002225basic_string<_CharT, _Traits, _Allocator>::~basic_string()
2226{
Louis Dionneba400782020-10-02 15:02:52 -04002227#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002228 if (!__libcpp_is_constant_evaluated())
2229 __get_db()->__erase_c(this);
Howard Hinnant8ea98242013-08-23 17:37:05 +00002230#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002231 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002232 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002233}
2234
2235template <class _CharT, class _Traits, class _Allocator>
2236void
2237basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
2238 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002239 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 +00002240{
2241 size_type __ms = max_size();
2242 if (__delta_cap > __ms - __old_cap - 1)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002243 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002244 pointer __old_p = __get_pointer();
2245 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002246 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002247 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002248 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002249 __invalidate_all_iterators();
2250 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002251 traits_type::copy(_VSTD::__to_address(__p),
2252 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002253 if (__n_add != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002254 traits_type::copy(_VSTD::__to_address(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002255 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2256 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002257 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2258 _VSTD::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002259 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002260 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002261 __set_long_pointer(__p);
2262 __set_long_cap(__cap+1);
2263 __old_sz = __n_copy + __n_add + __sec_cp_sz;
2264 __set_long_size(__old_sz);
2265 traits_type::assign(__p[__old_sz], value_type());
2266}
2267
2268template <class _CharT, class _Traits, class _Allocator>
2269void
2270basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2271 size_type __n_copy, size_type __n_del, size_type __n_add)
2272{
2273 size_type __ms = max_size();
Marshall Clow2267c5d2013-11-06 14:24:38 +00002274 if (__delta_cap > __ms - __old_cap)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002275 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002276 pointer __old_p = __get_pointer();
2277 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002278 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002279 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002280 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002281 __invalidate_all_iterators();
2282 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002283 traits_type::copy(_VSTD::__to_address(__p),
2284 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002285 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2286 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002287 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2288 _VSTD::__to_address(__old_p) + __n_copy + __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002289 __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002290 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002291 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002292 __set_long_pointer(__p);
2293 __set_long_cap(__cap+1);
2294}
2295
2296// assign
2297
2298template <class _CharT, class _Traits, class _Allocator>
Martijn Vels596e3de2020-02-26 15:55:49 -05002299template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04002300basic_string<_CharT, _Traits, _Allocator>&
2301basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(
Martijn Vels596e3de2020-02-26 15:55:49 -05002302 const value_type* __s, size_type __n) {
2303 size_type __cap = __is_short ? __min_cap : __get_long_cap();
2304 if (__n < __cap) {
2305 pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
2306 __is_short ? __set_short_size(__n) : __set_long_size(__n);
2307 traits_type::copy(_VSTD::__to_address(__p), __s, __n);
2308 traits_type::assign(__p[__n], value_type());
2309 __invalidate_iterators_past(__n);
2310 } else {
2311 size_type __sz = __is_short ? __get_short_size() : __get_long_size();
2312 __grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
2313 }
Martijn Velsb6a08b62020-04-10 18:36:31 -04002314 return *this;
Martijn Vels596e3de2020-02-26 15:55:49 -05002315}
2316
2317template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002318basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002319basic_string<_CharT, _Traits, _Allocator>::__assign_external(
2320 const value_type* __s, size_type __n) {
2321 size_type __cap = capacity();
2322 if (__cap >= __n) {
2323 value_type* __p = _VSTD::__to_address(__get_pointer());
2324 traits_type::move(__p, __s, __n);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002325 return __null_terminate_at(__p, __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04002326 } else {
2327 size_type __sz = size();
2328 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002329 return *this;
Martijn Velsda7d94f2020-06-19 14:24:03 -04002330 }
Martijn Velsda7d94f2020-06-19 14:24:03 -04002331}
2332
2333template <class _CharT, class _Traits, class _Allocator>
2334basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002335basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002336{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002337 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Nikolas Klauser93826d12022-01-04 17:24:03 +01002338 return (__builtin_constant_p(__n) && __fits_in_sso(__n))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002339 ? __assign_short(__s, __n)
2340 : __assign_external(__s, __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>::assign(size_type __n, value_type __c)
2346{
2347 size_type __cap = capacity();
2348 if (__cap < __n)
2349 {
2350 size_type __sz = size();
2351 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2352 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002353 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002354 traits_type::assign(__p, __n, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002355 return __null_terminate_at(__p, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002356}
2357
2358template <class _CharT, class _Traits, class _Allocator>
2359basic_string<_CharT, _Traits, _Allocator>&
2360basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2361{
2362 pointer __p;
2363 if (__is_long())
2364 {
2365 __p = __get_long_pointer();
2366 __set_long_size(1);
2367 }
2368 else
2369 {
2370 __p = __get_short_pointer();
2371 __set_short_size(1);
2372 }
2373 traits_type::assign(*__p, __c);
2374 traits_type::assign(*++__p, value_type());
2375 __invalidate_iterators_past(1);
2376 return *this;
2377}
2378
2379template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002380basic_string<_CharT, _Traits, _Allocator>&
2381basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2382{
Martijn Vels596e3de2020-02-26 15:55:49 -05002383 if (this != &__str) {
2384 __copy_assign_alloc(__str);
2385 if (!__is_long()) {
2386 if (!__str.__is_long()) {
Eric Fiselier03bbc922020-01-15 17:27:10 -05002387 __r_.first().__r = __str.__r_.first().__r;
Martijn Vels596e3de2020-02-26 15:55:49 -05002388 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002389 return __assign_no_alias<true>(__str.data(), __str.size());
Martijn Vels596e3de2020-02-26 15:55:49 -05002390 }
2391 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002392 return __assign_no_alias<false>(__str.data(), __str.size());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002393 }
Martijn Vels596e3de2020-02-26 15:55:49 -05002394 }
2395 return *this;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002396}
2397
Eric Fiselierfc92be82017-04-19 00:28:44 +00002398#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002399
2400template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002401inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002402void
2403basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002404 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002405{
2406 if (__alloc() != __str.__alloc())
2407 assign(__str);
2408 else
2409 __move_assign(__str, true_type());
2410}
2411
2412template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002413inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002414void
2415basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002416#if _LIBCPP_STD_VER > 14
2417 _NOEXCEPT
2418#else
Howard Hinnant3e276872011-06-03 18:40:47 +00002419 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002420#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002421{
marshall2ed41622019-11-27 07:13:00 -08002422 if (__is_long()) {
2423 __alloc_traits::deallocate(__alloc(), __get_long_pointer(),
2424 __get_long_cap());
2425#if _LIBCPP_STD_VER <= 14
2426 if (!is_nothrow_move_assignable<allocator_type>::value) {
2427 __set_short_size(0);
2428 traits_type::assign(__get_short_pointer()[0], value_type());
2429 }
2430#endif
2431 }
2432 __move_assign_alloc(__str);
2433 __r_.first() = __str.__r_.first();
2434 __str.__set_short_size(0);
2435 traits_type::assign(__str.__get_short_pointer()[0], value_type());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002436}
2437
2438template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002439inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002440basic_string<_CharT, _Traits, _Allocator>&
2441basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002442 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002443{
2444 __move_assign(__str, integral_constant<bool,
2445 __alloc_traits::propagate_on_container_move_assignment::value>());
2446 return *this;
2447}
2448
2449#endif
2450
2451template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002452template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002453__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002454<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002455 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002456 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002457>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002458basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2459{
Marshall Clow958362f2016-09-05 01:54:30 +00002460 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002461 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002462 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002463}
2464
2465template <class _CharT, class _Traits, class _Allocator>
2466template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002467__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002468<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002469 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002470 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002471>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002472basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2473{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002474 size_type __cap = capacity();
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002475 size_type __n = __string_is_trivial_iterator<_ForwardIterator>::value ?
2476 static_cast<size_type>(_VSTD::distance(__first, __last)) : 0;
2477
2478 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2479 (__cap >= __n || !__addr_in_range(*__first)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002480 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002481 if (__cap < __n)
2482 {
2483 size_type __sz = size();
2484 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2485 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002486 pointer __p = __get_pointer();
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002487 for (; __first != __last; ++__p, (void) ++__first)
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002488 traits_type::assign(*__p, *__first);
2489 traits_type::assign(*__p, value_type());
2490 __set_size(__n);
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04002491 __invalidate_iterators_past(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002492 }
2493 else
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002494 {
2495 const basic_string __temp(__first, __last, __alloc());
2496 assign(__temp.data(), __temp.size());
2497 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002498 return *this;
2499}
2500
2501template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002502basic_string<_CharT, _Traits, _Allocator>&
2503basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2504{
2505 size_type __sz = __str.size();
2506 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002507 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002508 return assign(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002509}
2510
2511template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002512template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002513__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002514<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002515 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
2516 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00002517 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002518>
Marshall Clow82513342016-09-24 22:45:42 +00002519basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002520{
Marshall Clow82513342016-09-24 22:45:42 +00002521 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002522 size_type __sz = __sv.size();
2523 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002524 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002525 return assign(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2526}
2527
2528
2529template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002530basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002531basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s) {
2532 return __assign_external(__s, traits_type::length(__s));
2533}
2534
2535template <class _CharT, class _Traits, class _Allocator>
2536basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002537basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002538{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002539 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Louis Dionne3e0c4332021-08-31 10:49:06 -04002540 return __builtin_constant_p(*__s)
Nikolas Klauser93826d12022-01-04 17:24:03 +01002541 ? (__fits_in_sso(traits_type::length(__s))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002542 ? __assign_short(__s, traits_type::length(__s))
2543 : __assign_external(__s, traits_type::length(__s)))
2544 : __assign_external(__s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002545}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002546// append
2547
2548template <class _CharT, class _Traits, class _Allocator>
2549basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002550basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002551{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002552 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002553 size_type __cap = capacity();
2554 size_type __sz = size();
2555 if (__cap - __sz >= __n)
2556 {
2557 if (__n)
2558 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002559 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002560 traits_type::copy(__p + __sz, __s, __n);
2561 __sz += __n;
2562 __set_size(__sz);
2563 traits_type::assign(__p[__sz], value_type());
2564 }
2565 }
2566 else
2567 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2568 return *this;
2569}
2570
2571template <class _CharT, class _Traits, class _Allocator>
2572basic_string<_CharT, _Traits, _Allocator>&
2573basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2574{
2575 if (__n)
2576 {
2577 size_type __cap = capacity();
2578 size_type __sz = size();
2579 if (__cap - __sz < __n)
2580 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2581 pointer __p = __get_pointer();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002582 traits_type::assign(_VSTD::__to_address(__p) + __sz, __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002583 __sz += __n;
2584 __set_size(__sz);
2585 traits_type::assign(__p[__sz], value_type());
2586 }
2587 return *this;
2588}
2589
2590template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00002591inline void
2592basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n)
2593{
2594 if (__n)
2595 {
2596 size_type __cap = capacity();
2597 size_type __sz = size();
2598 if (__cap - __sz < __n)
2599 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2600 pointer __p = __get_pointer();
2601 __sz += __n;
2602 __set_size(__sz);
2603 traits_type::assign(__p[__sz], value_type());
2604 }
2605}
2606
2607template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002608void
2609basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2610{
Howard Hinnant68bf1812013-04-30 21:44:48 +00002611 bool __is_short = !__is_long();
2612 size_type __cap;
2613 size_type __sz;
2614 if (__is_short)
2615 {
2616 __cap = __min_cap - 1;
2617 __sz = __get_short_size();
2618 }
2619 else
2620 {
2621 __cap = __get_long_cap() - 1;
2622 __sz = __get_long_size();
2623 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002624 if (__sz == __cap)
Howard Hinnant68bf1812013-04-30 21:44:48 +00002625 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002626 __grow_by(__cap, 1, __sz, __sz, 0);
Louis Dionne82d2b2c2021-11-16 11:26:56 -05002627 __is_short = false; // the string is always long after __grow_by
Howard Hinnant68bf1812013-04-30 21:44:48 +00002628 }
2629 pointer __p;
2630 if (__is_short)
2631 {
2632 __p = __get_short_pointer() + __sz;
2633 __set_short_size(__sz+1);
2634 }
2635 else
2636 {
2637 __p = __get_long_pointer() + __sz;
2638 __set_long_size(__sz+1);
2639 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002640 traits_type::assign(*__p, __c);
2641 traits_type::assign(*++__p, value_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002642}
2643
Howard Hinnantc51e1022010-05-11 19:42:16 +00002644template <class _CharT, class _Traits, class _Allocator>
2645template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002646__enable_if_t
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002647<
2648 __is_cpp17_forward_iterator<_ForwardIterator>::value,
2649 basic_string<_CharT, _Traits, _Allocator>&
2650>
2651basic_string<_CharT, _Traits, _Allocator>::append(
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002652 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002653{
2654 size_type __sz = size();
2655 size_type __cap = capacity();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002656 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002657 if (__n)
2658 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002659 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2660 !__addr_in_range(*__first))
Marshall Clow958362f2016-09-05 01:54:30 +00002661 {
2662 if (__cap - __sz < __n)
2663 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2664 pointer __p = __get_pointer() + __sz;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002665 for (; __first != __last; ++__p, (void) ++__first)
Marshall Clow958362f2016-09-05 01:54:30 +00002666 traits_type::assign(*__p, *__first);
2667 traits_type::assign(*__p, value_type());
2668 __set_size(__sz + __n);
2669 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002670 else
2671 {
2672 const basic_string __temp(__first, __last, __alloc());
2673 append(__temp.data(), __temp.size());
2674 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002675 }
2676 return *this;
2677}
2678
2679template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002680inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002681basic_string<_CharT, _Traits, _Allocator>&
2682basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2683{
2684 return append(__str.data(), __str.size());
2685}
2686
2687template <class _CharT, class _Traits, class _Allocator>
2688basic_string<_CharT, _Traits, _Allocator>&
2689basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2690{
2691 size_type __sz = __str.size();
2692 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002693 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002694 return append(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002695}
2696
2697template <class _CharT, class _Traits, class _Allocator>
Marshall Clow62953962016-10-03 23:40:48 +00002698template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002699 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002700 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002701 __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 +00002702 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002703 >
Marshall Clow82513342016-09-24 22:45:42 +00002704basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002705{
Marshall Clow82513342016-09-24 22:45:42 +00002706 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002707 size_type __sz = __sv.size();
2708 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002709 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002710 return append(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2711}
2712
2713template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002714basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002715basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002716{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002717 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002718 return append(__s, traits_type::length(__s));
2719}
2720
2721// insert
2722
2723template <class _CharT, class _Traits, class _Allocator>
2724basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002725basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002726{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002727 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002728 size_type __sz = size();
2729 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002730 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002731 size_type __cap = capacity();
2732 if (__cap - __sz >= __n)
2733 {
2734 if (__n)
2735 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002736 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002737 size_type __n_move = __sz - __pos;
2738 if (__n_move != 0)
2739 {
2740 if (__p + __pos <= __s && __s < __p + __sz)
2741 __s += __n;
2742 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2743 }
2744 traits_type::move(__p + __pos, __s, __n);
2745 __sz += __n;
2746 __set_size(__sz);
2747 traits_type::assign(__p[__sz], value_type());
2748 }
2749 }
2750 else
2751 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2752 return *this;
2753}
2754
2755template <class _CharT, class _Traits, class _Allocator>
2756basic_string<_CharT, _Traits, _Allocator>&
2757basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2758{
2759 size_type __sz = size();
2760 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002761 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002762 if (__n)
2763 {
2764 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002765 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002766 if (__cap - __sz >= __n)
2767 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002768 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002769 size_type __n_move = __sz - __pos;
2770 if (__n_move != 0)
2771 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2772 }
2773 else
2774 {
2775 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002776 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002777 }
2778 traits_type::assign(__p + __pos, __n, __c);
2779 __sz += __n;
2780 __set_size(__sz);
2781 traits_type::assign(__p[__sz], value_type());
2782 }
2783 return *this;
2784}
2785
2786template <class _CharT, class _Traits, class _Allocator>
2787template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002788__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002789<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002790 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Marshall Clow039b2f02016-01-13 21:54:34 +00002791 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002792>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002793basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2794{
Nikolas Klausereed25832021-12-15 01:32:30 +01002795 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2796 "string::insert(iterator, range) called with an iterator not"
2797 " referring to this string");
2798 const basic_string __temp(__first, __last, __alloc());
2799 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002800}
2801
2802template <class _CharT, class _Traits, class _Allocator>
2803template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002804__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002805<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002806 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002807 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002808>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002809basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2810{
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002811 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2812 "string::insert(iterator, range) called with an iterator not referring to this string");
Nikolas Klausereed25832021-12-15 01:32:30 +01002813
Howard Hinnantc51e1022010-05-11 19:42:16 +00002814 size_type __ip = static_cast<size_type>(__pos - begin());
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002815 size_type __n = static_cast<size_type>(std::distance(__first, __last));
2816 if (__n == 0)
2817 return begin() + __ip;
2818
2819 if (__string_is_trivial_iterator<_ForwardIterator>::value && !__addr_in_range(*__first))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002820 {
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002821 return __insert_from_safe_copy(__n, __ip, __first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002822 }
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002823 else
2824 {
2825 const basic_string __temp(__first, __last, __alloc());
2826 return __insert_from_safe_copy(__n, __ip, __temp.begin(), __temp.end());
2827 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002828}
2829
2830template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002831inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002832basic_string<_CharT, _Traits, _Allocator>&
2833basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2834{
2835 return insert(__pos1, __str.data(), __str.size());
2836}
2837
2838template <class _CharT, class _Traits, class _Allocator>
2839basic_string<_CharT, _Traits, _Allocator>&
2840basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2841 size_type __pos2, size_type __n)
2842{
2843 size_type __str_sz = __str.size();
2844 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002845 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002846 return insert(__pos1, __str.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002847}
2848
2849template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002850template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002851__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002852<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002853 __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 +00002854 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002855>
Marshall Clow82513342016-09-24 22:45:42 +00002856basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002857 size_type __pos2, size_type __n)
2858{
Marshall Clow82513342016-09-24 22:45:42 +00002859 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002860 size_type __str_sz = __sv.size();
2861 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002862 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002863 return insert(__pos1, __sv.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
2864}
2865
2866template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002867basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002868basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002869{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002870 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002871 return insert(__pos, __s, traits_type::length(__s));
2872}
2873
2874template <class _CharT, class _Traits, class _Allocator>
2875typename basic_string<_CharT, _Traits, _Allocator>::iterator
2876basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2877{
Louis Dionnef4cc6fb2022-02-15 15:47:45 -05002878 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2879 "string::insert(iterator, character) called with an iterator not"
2880 " referring to this string");
2881
Howard Hinnantc51e1022010-05-11 19:42:16 +00002882 size_type __ip = static_cast<size_type>(__pos - begin());
2883 size_type __sz = size();
2884 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002885 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002886 if (__cap == __sz)
2887 {
2888 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002889 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002890 }
2891 else
2892 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002893 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002894 size_type __n_move = __sz - __ip;
2895 if (__n_move != 0)
2896 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2897 }
2898 traits_type::assign(__p[__ip], __c);
2899 traits_type::assign(__p[++__sz], value_type());
2900 __set_size(__sz);
2901 return begin() + static_cast<difference_type>(__ip);
2902}
2903
2904template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002905inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002906typename basic_string<_CharT, _Traits, _Allocator>::iterator
2907basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2908{
Nikolas Klausereed25832021-12-15 01:32:30 +01002909 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2910 "string::insert(iterator, n, value) called with an iterator not"
2911 " referring to this string");
2912 difference_type __p = __pos - begin();
2913 insert(static_cast<size_type>(__p), __n, __c);
2914 return begin() + __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002915}
2916
2917// replace
2918
2919template <class _CharT, class _Traits, class _Allocator>
2920basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002921basic_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 +00002922 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00002923{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002924 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002925 size_type __sz = size();
2926 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002927 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002928 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002929 size_type __cap = capacity();
2930 if (__cap - __sz + __n1 >= __n2)
2931 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002932 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002933 if (__n1 != __n2)
2934 {
2935 size_type __n_move = __sz - __pos - __n1;
2936 if (__n_move != 0)
2937 {
2938 if (__n1 > __n2)
2939 {
2940 traits_type::move(__p + __pos, __s, __n2);
2941 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002942 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002943 }
2944 if (__p + __pos < __s && __s < __p + __sz)
2945 {
2946 if (__p + __pos + __n1 <= __s)
2947 __s += __n2 - __n1;
2948 else // __p + __pos < __s < __p + __pos + __n1
2949 {
2950 traits_type::move(__p + __pos, __s, __n1);
2951 __pos += __n1;
2952 __s += __n2;
2953 __n2 -= __n1;
2954 __n1 = 0;
2955 }
2956 }
2957 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2958 }
2959 }
2960 traits_type::move(__p + __pos, __s, __n2);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002961 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002962 }
2963 else
2964 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
2965 return *this;
2966}
2967
2968template <class _CharT, class _Traits, class _Allocator>
2969basic_string<_CharT, _Traits, _Allocator>&
2970basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
2971{
2972 size_type __sz = size();
2973 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002974 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002975 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002976 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002977 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002978 if (__cap - __sz + __n1 >= __n2)
2979 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002980 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002981 if (__n1 != __n2)
2982 {
2983 size_type __n_move = __sz - __pos - __n1;
2984 if (__n_move != 0)
2985 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2986 }
2987 }
2988 else
2989 {
2990 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002991 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002992 }
2993 traits_type::assign(__p + __pos, __n2, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002994 return __null_terminate_at(__p, __sz - (__n1 - __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002995}
2996
2997template <class _CharT, class _Traits, class _Allocator>
2998template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002999__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00003000<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003001 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003002 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003003>
Howard Hinnant990d6e82010-11-17 21:11:40 +00003004basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003005 _InputIterator __j1, _InputIterator __j2)
3006{
Marshall Clow958362f2016-09-05 01:54:30 +00003007 const basic_string __temp(__j1, __j2, __alloc());
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003008 return replace(__i1, __i2, __temp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003009}
3010
3011template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003012inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003013basic_string<_CharT, _Traits, _Allocator>&
3014basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
3015{
3016 return replace(__pos1, __n1, __str.data(), __str.size());
3017}
3018
3019template <class _CharT, class _Traits, class _Allocator>
3020basic_string<_CharT, _Traits, _Allocator>&
3021basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
3022 size_type __pos2, size_type __n2)
3023{
3024 size_type __str_sz = __str.size();
3025 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003026 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003027 return replace(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003028}
3029
3030template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003031template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003032__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003033<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003034 __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 +00003035 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003036>
Marshall Clow82513342016-09-24 22:45:42 +00003037basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003038 size_type __pos2, size_type __n2)
3039{
Marshall Clow82513342016-09-24 22:45:42 +00003040 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003041 size_type __str_sz = __sv.size();
3042 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003043 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003044 return replace(__pos1, __n1, __sv.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
3045}
3046
3047template <class _CharT, class _Traits, class _Allocator>
3048basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003049basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003050{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003051 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003052 return replace(__pos, __n1, __s, traits_type::length(__s));
3053}
3054
3055template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003056inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003057basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003058basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003059{
3060 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
3061 __str.data(), __str.size());
3062}
3063
3064template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003065inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003066basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003067basic_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 +00003068{
3069 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
3070}
3071
3072template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003073inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003074basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003075basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003076{
3077 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
3078}
3079
3080template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003081inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003082basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003083basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003084{
3085 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
3086}
3087
3088// erase
3089
Martijn Velsa81fc792020-02-26 13:25:43 -05003090// 'externally instantiated' erase() implementation, called when __n != npos.
3091// Does not check __pos against size()
Howard Hinnantc51e1022010-05-11 19:42:16 +00003092template <class _CharT, class _Traits, class _Allocator>
Martijn Velsa81fc792020-02-26 13:25:43 -05003093void
3094basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(
3095 size_type __pos, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003096{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003097 if (__n)
3098 {
Martijn Velsa81fc792020-02-26 13:25:43 -05003099 size_type __sz = size();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003100 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003101 __n = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003102 size_type __n_move = __sz - __pos - __n;
3103 if (__n_move != 0)
3104 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003105 __null_terminate_at(__p, __sz - __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003106 }
Martijn Velsa81fc792020-02-26 13:25:43 -05003107}
3108
3109template <class _CharT, class _Traits, class _Allocator>
3110basic_string<_CharT, _Traits, _Allocator>&
3111basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos,
3112 size_type __n) {
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003113 if (__pos > size())
3114 __throw_out_of_range();
Martijn Velsa81fc792020-02-26 13:25:43 -05003115 if (__n == npos) {
3116 __erase_to_end(__pos);
3117 } else {
3118 __erase_external_with_move(__pos, __n);
3119 }
3120 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003121}
3122
3123template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003124inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003125typename basic_string<_CharT, _Traits, _Allocator>::iterator
3126basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
3127{
Nikolas Klausereed25832021-12-15 01:32:30 +01003128 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3129 "string::erase(iterator) called with an iterator not"
3130 " referring to this string");
3131
3132 _LIBCPP_ASSERT(__pos != end(), "string::erase(iterator) called with a non-dereferenceable iterator");
3133 iterator __b = begin();
3134 size_type __r = static_cast<size_type>(__pos - __b);
3135 erase(__r, 1);
3136 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003137}
3138
3139template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003140inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003141typename basic_string<_CharT, _Traits, _Allocator>::iterator
3142basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
3143{
Nikolas Klausereed25832021-12-15 01:32:30 +01003144 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
3145 "string::erase(iterator, iterator) called with an iterator not"
3146 " referring to this string");
3147
3148 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
3149 iterator __b = begin();
3150 size_type __r = static_cast<size_type>(__first - __b);
3151 erase(__r, static_cast<size_type>(__last - __first));
3152 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003153}
3154
3155template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003156inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003157void
3158basic_string<_CharT, _Traits, _Allocator>::pop_back()
3159{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003160 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003161 __erase_to_end(size() - 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003162}
3163
3164template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003165inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003166void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003167basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003168{
3169 __invalidate_all_iterators();
3170 if (__is_long())
3171 {
3172 traits_type::assign(*__get_long_pointer(), value_type());
3173 __set_long_size(0);
3174 }
3175 else
3176 {
3177 traits_type::assign(*__get_short_pointer(), value_type());
3178 __set_short_size(0);
3179 }
3180}
3181
3182template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003183inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003184void
3185basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
3186{
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003187 __null_terminate_at(_VSTD::__to_address(__get_pointer()), __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003188}
3189
3190template <class _CharT, class _Traits, class _Allocator>
3191void
3192basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
3193{
3194 size_type __sz = size();
3195 if (__n > __sz)
3196 append(__n - __sz, __c);
3197 else
3198 __erase_to_end(__n);
3199}
3200
3201template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00003202inline void
3203basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n)
3204{
3205 size_type __sz = size();
3206 if (__n > __sz) {
3207 __append_default_init(__n - __sz);
3208 } else
3209 __erase_to_end(__n);
3210}
3211
3212template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003213inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003214typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003215basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003216{
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003217 size_type __m = __alloc_traits::max_size(__alloc());
Eric Fiseliere9cc5922017-10-17 13:16:01 +00003218#ifdef _LIBCPP_BIG_ENDIAN
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003219 return (__m <= ~__long_mask ? __m : __m/2) - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003220#else
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003221 return __m - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003222#endif
3223}
3224
3225template <class _CharT, class _Traits, class _Allocator>
3226void
Marek Kurdejc9848142020-11-26 10:07:16 +01003227basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __requested_capacity)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003228{
Marek Kurdejc9848142020-11-26 10:07:16 +01003229 if (__requested_capacity > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003230 __throw_length_error();
Marek Kurdejc9848142020-11-26 10:07:16 +01003231
Louis Dionne05b6d3e2022-01-14 12:30:22 -05003232 // Make sure reserve(n) never shrinks. This is technically only required in C++20
3233 // and later (since P0966R1), however we provide consistent behavior in all Standard
3234 // modes because this function is instantiated in the shared library.
3235 if (__requested_capacity <= capacity())
3236 return;
Marek Kurdejc9848142020-11-26 10:07:16 +01003237
3238 size_type __target_capacity = _VSTD::max(__requested_capacity, size());
3239 __target_capacity = __recommend(__target_capacity);
3240 if (__target_capacity == capacity()) return;
3241
3242 __shrink_or_extend(__target_capacity);
3243}
3244
3245template <class _CharT, class _Traits, class _Allocator>
Louis Dionnee38b8642021-12-13 14:15:21 -05003246inline
Marek Kurdejc9848142020-11-26 10:07:16 +01003247void
3248basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT
3249{
3250 size_type __target_capacity = __recommend(size());
3251 if (__target_capacity == capacity()) return;
3252
3253 __shrink_or_extend(__target_capacity);
3254}
3255
3256template <class _CharT, class _Traits, class _Allocator>
Louis Dionnee38b8642021-12-13 14:15:21 -05003257inline
Marek Kurdejc9848142020-11-26 10:07:16 +01003258void
3259basic_string<_CharT, _Traits, _Allocator>::__shrink_or_extend(size_type __target_capacity)
3260{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003261 size_type __cap = capacity();
3262 size_type __sz = size();
Marek Kurdejc9848142020-11-26 10:07:16 +01003263
3264 pointer __new_data, __p;
3265 bool __was_long, __now_long;
3266 if (__target_capacity == __min_cap - 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003267 {
Marek Kurdejc9848142020-11-26 10:07:16 +01003268 __was_long = true;
3269 __now_long = false;
3270 __new_data = __get_short_pointer();
3271 __p = __get_long_pointer();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003272 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003273 else
3274 {
3275 if (__target_capacity > __cap)
3276 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3277 else
3278 {
3279 #ifndef _LIBCPP_NO_EXCEPTIONS
3280 try
3281 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003282 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003283 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3284 #ifndef _LIBCPP_NO_EXCEPTIONS
3285 }
3286 catch (...)
3287 {
3288 return;
3289 }
3290 #else // _LIBCPP_NO_EXCEPTIONS
3291 if (__new_data == nullptr)
3292 return;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003293 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003294 }
3295 __now_long = true;
3296 __was_long = __is_long();
3297 __p = __get_pointer();
3298 }
3299 traits_type::copy(_VSTD::__to_address(__new_data),
3300 _VSTD::__to_address(__p), size()+1);
3301 if (__was_long)
3302 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
3303 if (__now_long)
3304 {
3305 __set_long_cap(__target_capacity+1);
3306 __set_long_size(__sz);
3307 __set_long_pointer(__new_data);
3308 }
3309 else
3310 __set_short_size(__sz);
3311 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003312}
3313
3314template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003315inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003316typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003317basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003318{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003319 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003320 return *(data() + __pos);
3321}
3322
3323template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003324inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003325typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003326basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003327{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003328 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003329 return *(__get_pointer() + __pos);
3330}
3331
3332template <class _CharT, class _Traits, class _Allocator>
3333typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3334basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3335{
3336 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003337 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003338 return (*this)[__n];
3339}
3340
3341template <class _CharT, class _Traits, class _Allocator>
3342typename basic_string<_CharT, _Traits, _Allocator>::reference
3343basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
3344{
3345 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003346 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003347 return (*this)[__n];
3348}
3349
3350template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003351inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003352typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003353basic_string<_CharT, _Traits, _Allocator>::front() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003354{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003355 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003356 return *__get_pointer();
3357}
3358
3359template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003360inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003361typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003362basic_string<_CharT, _Traits, _Allocator>::front() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003363{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003364 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003365 return *data();
3366}
3367
3368template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003369inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003370typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003371basic_string<_CharT, _Traits, _Allocator>::back() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003372{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003373 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003374 return *(__get_pointer() + size() - 1);
3375}
3376
3377template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003378inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003379typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003380basic_string<_CharT, _Traits, _Allocator>::back() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003381{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003382 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003383 return *(data() + size() - 1);
3384}
3385
3386template <class _CharT, class _Traits, class _Allocator>
3387typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003388basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003389{
3390 size_type __sz = size();
3391 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003392 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003393 size_type __rlen = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003394 traits_type::copy(__s, data() + __pos, __rlen);
3395 return __rlen;
3396}
3397
3398template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003399inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003400basic_string<_CharT, _Traits, _Allocator>
3401basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3402{
3403 return basic_string(*this, __pos, __n, __alloc());
3404}
3405
3406template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003407inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003408void
3409basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003410#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00003411 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00003412#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00003413 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003414 __is_nothrow_swappable<allocator_type>::value)
3415#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003416{
Louis Dionneba400782020-10-02 15:02:52 -04003417#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01003418 if (!__libcpp_is_constant_evaluated()) {
3419 if (!__is_long())
3420 __get_db()->__invalidate_all(this);
3421 if (!__str.__is_long())
3422 __get_db()->__invalidate_all(&__str);
3423 __get_db()->swap(this, &__str);
3424 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00003425#endif
Eric Fiselier9bf691f2016-12-28 05:53:01 +00003426 _LIBCPP_ASSERT(
3427 __alloc_traits::propagate_on_container_swap::value ||
3428 __alloc_traits::is_always_equal::value ||
3429 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003430 _VSTD::swap(__r_.first(), __str.__r_.first());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05003431 _VSTD::__swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003432}
3433
3434// find
3435
3436template <class _Traits>
3437struct _LIBCPP_HIDDEN __traits_eq
3438{
3439 typedef typename _Traits::char_type char_type;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003440 _LIBCPP_INLINE_VISIBILITY
3441 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3442 {return _Traits::eq(__x, __y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003443};
3444
3445template<class _CharT, class _Traits, class _Allocator>
3446typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003447basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003448 size_type __pos,
3449 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003450{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003451 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003452 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003453 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003454}
3455
3456template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003457inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003458typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003459basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3460 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003461{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003462 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003463 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003464}
3465
3466template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003467template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003468__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003469<
3470 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3471 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003472>
Marshall Clowe46031a2018-07-02 18:41:15 +00003473basic_string<_CharT, _Traits, _Allocator>::find(const _Tp &__t,
zoecarver1997e0a2021-02-05 11:54:47 -08003474 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003475{
Marshall Clowe46031a2018-07-02 18:41:15 +00003476 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003477 return __str_find<value_type, size_type, traits_type, npos>
3478 (data(), size(), __sv.data(), __pos, __sv.size());
3479}
3480
3481template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003482inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003483typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003484basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003485 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003486{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003487 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003488 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003489 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003490}
3491
3492template<class _CharT, class _Traits, class _Allocator>
3493typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003494basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3495 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003496{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003497 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003498 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003499}
3500
3501// rfind
3502
3503template<class _CharT, class _Traits, class _Allocator>
3504typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003505basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003506 size_type __pos,
3507 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003508{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003509 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003510 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003511 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003512}
3513
3514template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003515inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003516typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003517basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3518 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003519{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003520 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003521 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003522}
3523
3524template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003525template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003526__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003527<
3528 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3529 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003530>
Marshall Clowe46031a2018-07-02 18:41:15 +00003531basic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003532 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003533{
Marshall Clowe46031a2018-07-02 18:41:15 +00003534 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003535 return __str_rfind<value_type, size_type, traits_type, npos>
3536 (data(), size(), __sv.data(), __pos, __sv.size());
3537}
3538
3539template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003540inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003541typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003542basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003543 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003544{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003545 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003546 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003547 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003548}
3549
3550template<class _CharT, class _Traits, class _Allocator>
3551typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003552basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3553 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003554{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003555 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003556 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003557}
3558
3559// find_first_of
3560
3561template<class _CharT, class _Traits, class _Allocator>
3562typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003563basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003564 size_type __pos,
3565 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003566{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003567 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003568 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003569 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003570}
3571
3572template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003573inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003574typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003575basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3576 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003577{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003578 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003579 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003580}
3581
3582template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003583template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003584__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003585<
3586 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3587 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003588>
Marshall Clowe46031a2018-07-02 18:41:15 +00003589basic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003590 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003591{
Marshall Clowe46031a2018-07-02 18:41:15 +00003592 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003593 return __str_find_first_of<value_type, size_type, traits_type, npos>
3594 (data(), size(), __sv.data(), __pos, __sv.size());
3595}
3596
3597template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003598inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003599typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003600basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003601 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003602{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003603 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003604 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003605 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003606}
3607
3608template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003609inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003610typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003611basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3612 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003613{
3614 return find(__c, __pos);
3615}
3616
3617// find_last_of
3618
3619template<class _CharT, class _Traits, class _Allocator>
3620typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003621basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003622 size_type __pos,
3623 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003624{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003625 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003626 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003627 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003628}
3629
3630template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003631inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003632typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003633basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3634 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003635{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003636 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003637 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003638}
3639
3640template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003641template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003642__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003643<
3644 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3645 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003646>
Marshall Clowe46031a2018-07-02 18:41:15 +00003647basic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003648 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003649{
Marshall Clowe46031a2018-07-02 18:41:15 +00003650 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003651 return __str_find_last_of<value_type, size_type, traits_type, npos>
3652 (data(), size(), __sv.data(), __pos, __sv.size());
3653}
3654
3655template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003656inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003657typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003658basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003659 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003660{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003661 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003662 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003663 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003664}
3665
3666template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003667inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003668typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003669basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3670 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003671{
3672 return rfind(__c, __pos);
3673}
3674
3675// find_first_not_of
3676
3677template<class _CharT, class _Traits, class _Allocator>
3678typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003679basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003680 size_type __pos,
3681 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003682{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003683 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003684 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003685 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003686}
3687
3688template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003689inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003690typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003691basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3692 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003693{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003694 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003695 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003696}
3697
3698template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003699template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003700__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003701<
3702 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3703 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003704>
Marshall Clowe46031a2018-07-02 18:41:15 +00003705basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003706 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003707{
Marshall Clowe46031a2018-07-02 18:41:15 +00003708 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003709 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3710 (data(), size(), __sv.data(), __pos, __sv.size());
3711}
3712
3713template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003714inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003715typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003716basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003717 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003718{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003719 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003720 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003721 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003722}
3723
3724template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003725inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003726typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003727basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3728 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003729{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003730 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003731 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003732}
3733
3734// find_last_not_of
3735
3736template<class _CharT, class _Traits, class _Allocator>
3737typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003738basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003739 size_type __pos,
3740 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003741{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003742 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003743 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003744 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003745}
3746
3747template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003748inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003749typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003750basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3751 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003752{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003753 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003754 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003755}
3756
3757template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003758template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003759__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003760<
3761 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3762 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003763>
Marshall Clowe46031a2018-07-02 18:41:15 +00003764basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003765 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003766{
Marshall Clowe46031a2018-07-02 18:41:15 +00003767 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003768 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3769 (data(), size(), __sv.data(), __pos, __sv.size());
3770}
3771
3772template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003773inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003774typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003775basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003776 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003777{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003778 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003779 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003780 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003781}
3782
3783template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003784inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003785typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003786basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3787 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003788{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003789 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003790 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003791}
3792
3793// compare
3794
3795template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003796template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003797__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003798<
3799 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3800 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003801>
zoecarver1997e0a2021-02-05 11:54:47 -08003802basic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003803{
Marshall Clowe46031a2018-07-02 18:41:15 +00003804 __self_view __sv = __t;
Howard Hinnantb0485532011-07-24 21:45:06 +00003805 size_t __lhs_sz = size();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003806 size_t __rhs_sz = __sv.size();
3807 int __result = traits_type::compare(data(), __sv.data(),
Howard Hinnantb0485532011-07-24 21:45:06 +00003808 _VSTD::min(__lhs_sz, __rhs_sz));
3809 if (__result != 0)
3810 return __result;
3811 if (__lhs_sz < __rhs_sz)
3812 return -1;
3813 if (__lhs_sz > __rhs_sz)
3814 return 1;
3815 return 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003816}
3817
3818template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003819inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003820int
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003821basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003822{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003823 return compare(__self_view(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003824}
3825
3826template <class _CharT, class _Traits, class _Allocator>
3827int
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003828basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3829 size_type __n1,
Howard Hinnantd17880b2013-06-28 16:59:19 +00003830 const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003831 size_type __n2) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003832{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003833 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003834 size_type __sz = size();
3835 if (__pos1 > __sz || __n2 == npos)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003836 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003837 size_type __rlen = _VSTD::min(__n1, __sz - __pos1);
3838 int __r = traits_type::compare(data() + __pos1, __s, _VSTD::min(__rlen, __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003839 if (__r == 0)
3840 {
3841 if (__rlen < __n2)
3842 __r = -1;
3843 else if (__rlen > __n2)
3844 __r = 1;
3845 }
3846 return __r;
3847}
3848
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003849template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003850template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003851__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003852<
3853 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3854 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003855>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003856basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3857 size_type __n1,
Marshall Clowe46031a2018-07-02 18:41:15 +00003858 const _Tp& __t) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003859{
Marshall Clowe46031a2018-07-02 18:41:15 +00003860 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003861 return compare(__pos1, __n1, __sv.data(), __sv.size());
3862}
3863
3864template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003865inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003866int
3867basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3868 size_type __n1,
3869 const basic_string& __str) const
3870{
3871 return compare(__pos1, __n1, __str.data(), __str.size());
3872}
3873
3874template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003875template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003876__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003877<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003878 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
3879 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00003880 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003881>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003882basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3883 size_type __n1,
Marshall Clow82513342016-09-24 22:45:42 +00003884 const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003885 size_type __pos2,
3886 size_type __n2) const
3887{
Marshall Clow82513342016-09-24 22:45:42 +00003888 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003889 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3890}
3891
3892template <class _CharT, class _Traits, class _Allocator>
3893int
3894basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3895 size_type __n1,
3896 const basic_string& __str,
3897 size_type __pos2,
3898 size_type __n2) const
3899{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003900 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003901}
3902
3903template <class _CharT, class _Traits, class _Allocator>
3904int
3905basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
3906{
3907 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3908 return compare(0, npos, __s, traits_type::length(__s));
3909}
3910
3911template <class _CharT, class _Traits, class _Allocator>
3912int
3913basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3914 size_type __n1,
3915 const value_type* __s) const
3916{
3917 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3918 return compare(__pos1, __n1, __s, traits_type::length(__s));
3919}
3920
Howard Hinnantc51e1022010-05-11 19:42:16 +00003921// __invariants
3922
3923template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003924inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003925bool
3926basic_string<_CharT, _Traits, _Allocator>::__invariants() const
3927{
3928 if (size() > capacity())
3929 return false;
3930 if (capacity() < __min_cap - 1)
3931 return false;
Bruce Mitchener170d8972020-11-24 12:53:53 -05003932 if (data() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003933 return false;
Louis Dionne663415f2020-10-05 16:16:13 -04003934 if (data()[size()] != value_type())
Howard Hinnantc51e1022010-05-11 19:42:16 +00003935 return false;
3936 return true;
3937}
3938
Vedant Kumar55e007e2018-03-08 21:15:26 +00003939// __clear_and_shrink
3940
3941template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003942inline
Louis Dionne173f29e2019-05-29 16:01:36 +00003943void
Marshall Clowe60a7182018-05-29 17:04:37 +00003944basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
Vedant Kumar55e007e2018-03-08 21:15:26 +00003945{
3946 clear();
3947 if(__is_long())
3948 {
3949 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
3950 __set_long_cap(0);
3951 __set_short_size(0);
Louis Dionne663415f2020-10-05 16:16:13 -04003952 traits_type::assign(*__get_short_pointer(), value_type());
Vedant Kumar55e007e2018-03-08 21:15:26 +00003953 }
Louis Dionne173f29e2019-05-29 16:01:36 +00003954}
Vedant Kumar55e007e2018-03-08 21:15:26 +00003955
Howard Hinnantc51e1022010-05-11 19:42:16 +00003956// operator==
3957
3958template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003959inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003960bool
3961operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003962 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003963{
Howard Hinnantaaeb1132013-04-22 23:55:13 +00003964 size_t __lhs_sz = __lhs.size();
3965 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
3966 __rhs.data(),
3967 __lhs_sz) == 0;
3968}
3969
3970template<class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003971inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaaeb1132013-04-22 23:55:13 +00003972bool
3973operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
3974 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
3975{
3976 size_t __lhs_sz = __lhs.size();
3977 if (__lhs_sz != __rhs.size())
3978 return false;
3979 const char* __lp = __lhs.data();
3980 const char* __rp = __rhs.data();
3981 if (__lhs.__is_long())
3982 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
3983 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
3984 if (*__lp != *__rp)
3985 return false;
3986 return true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003987}
3988
3989template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003990inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003991bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003992operator==(const _CharT* __lhs,
3993 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003994{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00003995 typedef basic_string<_CharT, _Traits, _Allocator> _String;
3996 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
3997 size_t __lhs_len = _Traits::length(__lhs);
3998 if (__lhs_len != __rhs.size()) return false;
3999 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004000}
4001
Howard Hinnantc51e1022010-05-11 19:42:16 +00004002template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004003inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004004bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004005operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
4006 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004007{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004008 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4009 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
4010 size_t __rhs_len = _Traits::length(__rhs);
4011 if (__rhs_len != __lhs.size()) return false;
4012 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004013}
4014
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004015template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004016inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004017bool
4018operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004019 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004020{
4021 return !(__lhs == __rhs);
4022}
4023
4024template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004025inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004026bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004027operator!=(const _CharT* __lhs,
4028 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004029{
4030 return !(__lhs == __rhs);
4031}
4032
4033template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004034inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004035bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004036operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4037 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004038{
4039 return !(__lhs == __rhs);
4040}
4041
4042// operator<
4043
4044template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004045inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004046bool
4047operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004048 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004049{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004050 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004051}
4052
4053template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004054inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004055bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004056operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4057 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004058{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004059 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004060}
4061
4062template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004063inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004064bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004065operator< (const _CharT* __lhs,
4066 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004067{
4068 return __rhs.compare(__lhs) > 0;
4069}
4070
Howard Hinnantc51e1022010-05-11 19:42:16 +00004071// operator>
4072
4073template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004074inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004075bool
4076operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004077 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004078{
4079 return __rhs < __lhs;
4080}
4081
4082template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004083inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004084bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004085operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4086 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004087{
4088 return __rhs < __lhs;
4089}
4090
4091template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004092inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004093bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004094operator> (const _CharT* __lhs,
4095 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004096{
4097 return __rhs < __lhs;
4098}
4099
4100// operator<=
4101
4102template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004103inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004104bool
4105operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004106 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004107{
4108 return !(__rhs < __lhs);
4109}
4110
4111template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004112inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004113bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004114operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4115 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004116{
4117 return !(__rhs < __lhs);
4118}
4119
4120template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004121inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004122bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004123operator<=(const _CharT* __lhs,
4124 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004125{
4126 return !(__rhs < __lhs);
4127}
4128
4129// operator>=
4130
4131template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004132inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004133bool
4134operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004135 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004136{
4137 return !(__lhs < __rhs);
4138}
4139
4140template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004141inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004142bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004143operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4144 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004145{
4146 return !(__lhs < __rhs);
4147}
4148
4149template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004150inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004151bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004152operator>=(const _CharT* __lhs,
4153 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004154{
4155 return !(__lhs < __rhs);
4156}
4157
4158// operator +
4159
4160template<class _CharT, class _Traits, class _Allocator>
4161basic_string<_CharT, _Traits, _Allocator>
4162operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4163 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4164{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004165 using _String = basic_string<_CharT, _Traits, _Allocator>;
4166 _String __r(_String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4167 typename _String::size_type __lhs_sz = __lhs.size();
4168 typename _String::size_type __rhs_sz = __rhs.size();
Howard Hinnantc51e1022010-05-11 19:42:16 +00004169 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4170 __r.append(__rhs.data(), __rhs_sz);
4171 return __r;
4172}
4173
4174template<class _CharT, class _Traits, class _Allocator>
4175basic_string<_CharT, _Traits, _Allocator>
4176operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4177{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004178 using _String = basic_string<_CharT, _Traits, _Allocator>;
4179 _String __r(_String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator()));
4180 typename _String::size_type __lhs_sz = _Traits::length(__lhs);
4181 typename _String::size_type __rhs_sz = __rhs.size();
Howard Hinnantc51e1022010-05-11 19:42:16 +00004182 __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz);
4183 __r.append(__rhs.data(), __rhs_sz);
4184 return __r;
4185}
4186
4187template<class _CharT, class _Traits, class _Allocator>
4188basic_string<_CharT, _Traits, _Allocator>
4189operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4190{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004191 using _String = basic_string<_CharT, _Traits, _Allocator>;
4192 _String __r(_String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator()));
4193 typename _String::size_type __rhs_sz = __rhs.size();
Howard Hinnantc51e1022010-05-11 19:42:16 +00004194 __r.__init(&__lhs, 1, 1 + __rhs_sz);
4195 __r.append(__rhs.data(), __rhs_sz);
4196 return __r;
4197}
4198
4199template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004200inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004201basic_string<_CharT, _Traits, _Allocator>
4202operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
4203{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004204 using _String = basic_string<_CharT, _Traits, _Allocator>;
4205 _String __r(_String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4206 typename _String::size_type __lhs_sz = __lhs.size();
4207 typename _String::size_type __rhs_sz = _Traits::length(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004208 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4209 __r.append(__rhs, __rhs_sz);
4210 return __r;
4211}
4212
4213template<class _CharT, class _Traits, class _Allocator>
4214basic_string<_CharT, _Traits, _Allocator>
4215operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
4216{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004217 using _String = basic_string<_CharT, _Traits, _Allocator>;
4218 _String __r(_String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4219 typename _String::size_type __lhs_sz = __lhs.size();
Howard Hinnantc51e1022010-05-11 19:42:16 +00004220 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1);
4221 __r.push_back(__rhs);
4222 return __r;
4223}
4224
Eric Fiselierfc92be82017-04-19 00:28:44 +00004225#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004226
4227template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004228inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004229basic_string<_CharT, _Traits, _Allocator>
4230operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4231{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004232 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004233}
4234
4235template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004236inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004237basic_string<_CharT, _Traits, _Allocator>
4238operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4239{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004240 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004241}
4242
4243template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004244inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004245basic_string<_CharT, _Traits, _Allocator>
4246operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4247{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004248 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004249}
4250
4251template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004252inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004253basic_string<_CharT, _Traits, _Allocator>
4254operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4255{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004256 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004257}
4258
4259template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004260inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004261basic_string<_CharT, _Traits, _Allocator>
4262operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4263{
4264 __rhs.insert(__rhs.begin(), __lhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004265 return _VSTD::move(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004266}
4267
4268template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004269inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004270basic_string<_CharT, _Traits, _Allocator>
4271operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
4272{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004273 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004274}
4275
4276template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004277inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004278basic_string<_CharT, _Traits, _Allocator>
4279operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
4280{
4281 __lhs.push_back(__rhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004282 return _VSTD::move(__lhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004283}
4284
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004285#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004286
4287// swap
4288
4289template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004290inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004291void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004292swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +00004293 basic_string<_CharT, _Traits, _Allocator>& __rhs)
4294 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004295{
4296 __lhs.swap(__rhs);
4297}
4298
Bruce Mitchener170d8972020-11-24 12:53:53 -05004299_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = nullptr, int __base = 10);
4300_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = nullptr, int __base = 10);
4301_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = nullptr, int __base = 10);
4302_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = nullptr, int __base = 10);
4303_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004304
Bruce Mitchener170d8972020-11-24 12:53:53 -05004305_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = nullptr);
4306_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = nullptr);
4307_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004308
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004309_LIBCPP_FUNC_VIS string to_string(int __val);
4310_LIBCPP_FUNC_VIS string to_string(unsigned __val);
4311_LIBCPP_FUNC_VIS string to_string(long __val);
4312_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
4313_LIBCPP_FUNC_VIS string to_string(long long __val);
4314_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
4315_LIBCPP_FUNC_VIS string to_string(float __val);
4316_LIBCPP_FUNC_VIS string to_string(double __val);
4317_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004318
Louis Dionne89258142021-08-23 15:32:36 -04004319#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Bruce Mitchener170d8972020-11-24 12:53:53 -05004320_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4321_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4322_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4323_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4324_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004325
Bruce Mitchener170d8972020-11-24 12:53:53 -05004326_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = nullptr);
4327_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = nullptr);
4328_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004329
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004330_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
4331_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
4332_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
4333_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
4334_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
4335_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
4336_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
4337_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
4338_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Louis Dionne89258142021-08-23 15:32:36 -04004339#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004340
Howard Hinnantc51e1022010-05-11 19:42:16 +00004341template<class _CharT, class _Traits, class _Allocator>
Martin Storsjö88890c22021-02-22 01:13:13 +02004342_LIBCPP_TEMPLATE_DATA_VIS
Eric Fiselierc9fdaf12020-01-15 17:02:17 -05004343const typename basic_string<_CharT, _Traits, _Allocator>::size_type
4344 basic_string<_CharT, _Traits, _Allocator>::npos;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004345
Marshall Clow851b9ec2019-05-20 21:56:51 +00004346template <class _CharT, class _Allocator>
4347struct _LIBCPP_TEMPLATE_VIS
4348 hash<basic_string<_CharT, char_traits<_CharT>, _Allocator> >
4349 : public unary_function<
4350 basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004351{
4352 size_t
Marshall Clow851b9ec2019-05-20 21:56:51 +00004353 operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT
4354 { return __do_string_hash(__val.data(), __val.data() + __val.size()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004355};
4356
Howard Hinnantc51e1022010-05-11 19:42:16 +00004357
Howard Hinnantdc095972011-07-18 15:51:59 +00004358template<class _CharT, class _Traits, class _Allocator>
4359basic_ostream<_CharT, _Traits>&
4360operator<<(basic_ostream<_CharT, _Traits>& __os,
4361 const basic_string<_CharT, _Traits, _Allocator>& __str);
4362
4363template<class _CharT, class _Traits, class _Allocator>
4364basic_istream<_CharT, _Traits>&
4365operator>>(basic_istream<_CharT, _Traits>& __is,
4366 basic_string<_CharT, _Traits, _Allocator>& __str);
4367
4368template<class _CharT, class _Traits, class _Allocator>
4369basic_istream<_CharT, _Traits>&
4370getline(basic_istream<_CharT, _Traits>& __is,
4371 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4372
4373template<class _CharT, class _Traits, class _Allocator>
4374inline _LIBCPP_INLINE_VISIBILITY
4375basic_istream<_CharT, _Traits>&
4376getline(basic_istream<_CharT, _Traits>& __is,
4377 basic_string<_CharT, _Traits, _Allocator>& __str);
4378
Howard Hinnantdc095972011-07-18 15:51:59 +00004379template<class _CharT, class _Traits, class _Allocator>
4380inline _LIBCPP_INLINE_VISIBILITY
4381basic_istream<_CharT, _Traits>&
4382getline(basic_istream<_CharT, _Traits>&& __is,
4383 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4384
4385template<class _CharT, class _Traits, class _Allocator>
4386inline _LIBCPP_INLINE_VISIBILITY
4387basic_istream<_CharT, _Traits>&
4388getline(basic_istream<_CharT, _Traits>&& __is,
4389 basic_string<_CharT, _Traits, _Allocator>& __str);
4390
Marshall Clow29b53f22018-12-14 18:49:35 +00004391#if _LIBCPP_STD_VER > 17
Marek Kurdeja98b1412020-05-02 13:58:03 +02004392template <class _CharT, class _Traits, class _Allocator, class _Up>
Marshall Clow29b53f22018-12-14 18:49:35 +00004393inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004394 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4395 erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
4396 auto __old_size = __str.size();
4397 __str.erase(_VSTD::remove(__str.begin(), __str.end(), __v), __str.end());
4398 return __old_size - __str.size();
4399}
Marshall Clow29b53f22018-12-14 18:49:35 +00004400
Marek Kurdeja98b1412020-05-02 13:58:03 +02004401template <class _CharT, class _Traits, class _Allocator, class _Predicate>
Marshall Clow29b53f22018-12-14 18:49:35 +00004402inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004403 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4404 erase_if(basic_string<_CharT, _Traits, _Allocator>& __str,
4405 _Predicate __pred) {
4406 auto __old_size = __str.size();
4407 __str.erase(_VSTD::remove_if(__str.begin(), __str.end(), __pred),
4408 __str.end());
4409 return __old_size - __str.size();
4410}
Marshall Clow29b53f22018-12-14 18:49:35 +00004411#endif
4412
Louis Dionneba400782020-10-02 15:02:52 -04004413#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004414
4415template<class _CharT, class _Traits, class _Allocator>
4416bool
4417basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4418{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004419 return data() <= _VSTD::__to_address(__i->base()) &&
4420 _VSTD::__to_address(__i->base()) < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004421}
4422
4423template<class _CharT, class _Traits, class _Allocator>
4424bool
4425basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4426{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004427 return data() < _VSTD::__to_address(__i->base()) &&
4428 _VSTD::__to_address(__i->base()) <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004429}
4430
4431template<class _CharT, class _Traits, class _Allocator>
4432bool
4433basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4434{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004435 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004436 return data() <= __p && __p <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004437}
4438
4439template<class _CharT, class _Traits, class _Allocator>
4440bool
4441basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4442{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004443 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004444 return data() <= __p && __p < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004445}
4446
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004447#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004448
Louis Dionne173f29e2019-05-29 16:01:36 +00004449#if _LIBCPP_STD_VER > 11
Marshall Clowcba751f2013-07-23 17:05:24 +00004450// Literal suffixes for basic_string [basic.string.literals]
Marshall Clowac868372013-10-05 21:18:32 +00004451inline namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004452{
4453 inline namespace string_literals
4454 {
Howard Hinnant5c167562013-08-07 19:39:48 +00004455 inline _LIBCPP_INLINE_VISIBILITY
4456 basic_string<char> operator "" s( const char *__str, size_t __len )
4457 {
4458 return basic_string<char> (__str, __len);
4459 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004460
Louis Dionne89258142021-08-23 15:32:36 -04004461#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnant5c167562013-08-07 19:39:48 +00004462 inline _LIBCPP_INLINE_VISIBILITY
4463 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4464 {
4465 return basic_string<wchar_t> (__str, __len);
4466 }
Louis Dionne89258142021-08-23 15:32:36 -04004467#endif
Marshall Clowcba751f2013-07-23 17:05:24 +00004468
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -04004469#ifndef _LIBCPP_HAS_NO_CHAR8_T
Marshall Clow8732fed2018-12-11 04:35:44 +00004470 inline _LIBCPP_INLINE_VISIBILITY
4471 basic_string<char8_t> operator "" s(const char8_t *__str, size_t __len) _NOEXCEPT
4472 {
4473 return basic_string<char8_t> (__str, __len);
4474 }
4475#endif
4476
Howard Hinnant5c167562013-08-07 19:39:48 +00004477 inline _LIBCPP_INLINE_VISIBILITY
4478 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4479 {
4480 return basic_string<char16_t> (__str, __len);
4481 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004482
Howard Hinnant5c167562013-08-07 19:39:48 +00004483 inline _LIBCPP_INLINE_VISIBILITY
4484 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4485 {
4486 return basic_string<char32_t> (__str, __len);
4487 }
Nikolas Klauserd26407a2021-12-02 14:12:51 +01004488 } // namespace string_literals
4489} // namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004490#endif
4491
Howard Hinnantc51e1022010-05-11 19:42:16 +00004492_LIBCPP_END_NAMESPACE_STD
4493
Eric Fiselierf4433a32017-05-31 22:07:49 +00004494_LIBCPP_POP_MACROS
4495
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004496#endif // _LIBCPP_STRING