blob: d196137bbe25678770ab5b95191e0e593cf6fee9 [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
521#include <__config>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400522#include <__debug>
523#include <__functional_base>
Louis Dionne77249522021-06-11 09:55:11 -0400524#include <__iterator/wrap_iter.h>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000525#include <algorithm>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400526#include <compare>
527#include <cstdio> // EOF
Louis Dionned24191c2021-08-19 12:39:16 -0400528#include <cstdlib>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400529#include <cstring>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400530#include <initializer_list>
531#include <iosfwd>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000532#include <iterator>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000533#include <memory>
534#include <stdexcept>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400535#include <string_view>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000536#include <type_traits>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400537#include <utility>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000538#include <version>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000539
Louis Dionne89258142021-08-23 15:32:36 -0400540#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
541# include <cwchar>
542#endif
543
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400544#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
545# include <cstdint>
546#endif
Eric Fiselier14b6de92014-08-10 23:53:08 +0000547
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000548#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Arthur O'Dwyer6eeaa002022-02-01 20:16:40 -0500549# pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000550#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000551
Eric Fiselierf4433a32017-05-31 22:07:49 +0000552_LIBCPP_PUSH_MACROS
553#include <__undef_macros>
554
555
Howard Hinnantc51e1022010-05-11 19:42:16 +0000556_LIBCPP_BEGIN_NAMESPACE_STD
557
558// fpos
559
560template <class _StateT>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000561class _LIBCPP_TEMPLATE_VIS fpos
Howard Hinnantc51e1022010-05-11 19:42:16 +0000562{
563private:
564 _StateT __st_;
565 streamoff __off_;
566public:
567 _LIBCPP_INLINE_VISIBILITY fpos(streamoff __off = streamoff()) : __st_(), __off_(__off) {}
568
569 _LIBCPP_INLINE_VISIBILITY operator streamoff() const {return __off_;}
570
571 _LIBCPP_INLINE_VISIBILITY _StateT state() const {return __st_;}
572 _LIBCPP_INLINE_VISIBILITY void state(_StateT __st) {__st_ = __st;}
573
574 _LIBCPP_INLINE_VISIBILITY fpos& operator+=(streamoff __off) {__off_ += __off; return *this;}
575 _LIBCPP_INLINE_VISIBILITY fpos operator+ (streamoff __off) const {fpos __t(*this); __t += __off; return __t;}
576 _LIBCPP_INLINE_VISIBILITY fpos& operator-=(streamoff __off) {__off_ -= __off; return *this;}
577 _LIBCPP_INLINE_VISIBILITY fpos operator- (streamoff __off) const {fpos __t(*this); __t -= __off; return __t;}
578};
579
580template <class _StateT>
581inline _LIBCPP_INLINE_VISIBILITY
582streamoff operator-(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
583 {return streamoff(__x) - streamoff(__y);}
584
585template <class _StateT>
586inline _LIBCPP_INLINE_VISIBILITY
587bool operator==(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
588 {return streamoff(__x) == streamoff(__y);}
589
590template <class _StateT>
591inline _LIBCPP_INLINE_VISIBILITY
592bool operator!=(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
593 {return streamoff(__x) != streamoff(__y);}
594
Howard Hinnantc51e1022010-05-11 19:42:16 +0000595// basic_string
596
597template<class _CharT, class _Traits, class _Allocator>
598basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000599operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
600 const basic_string<_CharT, _Traits, _Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000601
602template<class _CharT, class _Traits, class _Allocator>
603basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000604operator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000605
606template<class _CharT, class _Traits, class _Allocator>
607basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000608operator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000609
610template<class _CharT, class _Traits, class _Allocator>
Louis Dionne8f8d95d2018-11-21 17:31:55 +0000611inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000612basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000613operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000614
615template<class _CharT, class _Traits, class _Allocator>
616basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000617operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000618
Shoaib Meenaiea363712017-07-29 02:54:41 +0000619_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&))
620
Marshall Clow039b2f02016-01-13 21:54:34 +0000621template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400622struct __string_is_trivial_iterator : public false_type {};
623
624template <class _Tp>
625struct __string_is_trivial_iterator<_Tp*>
626 : public is_arithmetic<_Tp> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000627
Louis Dionne173f29e2019-05-29 16:01:36 +0000628template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400629struct __string_is_trivial_iterator<__wrap_iter<_Iter> >
630 : public __string_is_trivial_iterator<_Iter> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000631
Marshall Clow82513342016-09-24 22:45:42 +0000632template <class _CharT, class _Traits, class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500633struct __can_be_converted_to_string_view : public _BoolConstant<
634 is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
635 !is_convertible<const _Tp&, const _CharT*>::value
636 > {};
Marshall Clow82513342016-09-24 22:45:42 +0000637
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000638#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000639
640template <class _CharT, size_t = sizeof(_CharT)>
641struct __padding
642{
643 unsigned char __xx[sizeof(_CharT)-1];
644};
645
646template <class _CharT>
647struct __padding<_CharT, 1>
648{
649};
650
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400651#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000652
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400653#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800654typedef basic_string<char8_t> u8string;
655#endif
656
657#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
658typedef basic_string<char16_t> u16string;
659typedef basic_string<char32_t> u32string;
Louis Dionne96fc5f52021-09-09 11:25:10 -0400660#endif
Richard Smith256954d2020-11-11 17:12:18 -0800661
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000662template<class _CharT, class _Traits, class _Allocator>
Richard Smith256954d2020-11-11 17:12:18 -0800663class
664 _LIBCPP_TEMPLATE_VIS
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400665#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800666 _LIBCPP_PREFERRED_NAME(u8string)
667#endif
668#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
669 _LIBCPP_PREFERRED_NAME(u16string)
670 _LIBCPP_PREFERRED_NAME(u32string)
671#endif
672 basic_string
Howard Hinnantc51e1022010-05-11 19:42:16 +0000673{
674public:
675 typedef basic_string __self;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000676 typedef basic_string_view<_CharT, _Traits> __self_view;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000677 typedef _Traits traits_type;
Marshall Clowa3a74e02017-03-15 18:41:11 +0000678 typedef _CharT value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000679 typedef _Allocator allocator_type;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000680 typedef allocator_traits<allocator_type> __alloc_traits;
681 typedef typename __alloc_traits::size_type size_type;
682 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant3e276872011-06-03 18:40:47 +0000683 typedef value_type& reference;
684 typedef const value_type& const_reference;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000685 typedef typename __alloc_traits::pointer pointer;
686 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000687
Marshall Clow79f33542018-03-21 00:36:05 +0000688 static_assert((!is_array<value_type>::value), "Character type of basic_string must not be an array");
689 static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string must be standard-layout");
690 static_assert(( is_trivial<value_type>::value), "Character type of basic_string must be trivial");
691 static_assert(( is_same<_CharT, typename traits_type::char_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000692 "traits_type::char_type must be the same type as CharT");
Marshall Clow79f33542018-03-21 00:36:05 +0000693 static_assert(( is_same<typename allocator_type::value_type, value_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000694 "Allocator::value_type must be same type as value_type");
Marshall Clowe46031a2018-07-02 18:41:15 +0000695
Howard Hinnantc51e1022010-05-11 19:42:16 +0000696 typedef __wrap_iter<pointer> iterator;
697 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000698 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
699 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000700
701private:
Howard Hinnant68bf1812013-04-30 21:44:48 +0000702
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000703#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000704
705 struct __long
706 {
707 pointer __data_;
708 size_type __size_;
709 size_type __cap_;
710 };
711
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000712#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000713 static const size_type __short_mask = 0x01;
714 static const size_type __long_mask = 0x1ul;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000715#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000716 static const size_type __short_mask = 0x80;
717 static const size_type __long_mask = ~(size_type(~0) >> 1);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400718#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +0000719
720 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
721 (sizeof(__long) - 1)/sizeof(value_type) : 2};
722
723 struct __short
724 {
725 value_type __data_[__min_cap];
726 struct
727 : __padding<value_type>
728 {
729 unsigned char __size_;
730 };
731 };
732
733#else
734
Howard Hinnantc51e1022010-05-11 19:42:16 +0000735 struct __long
736 {
737 size_type __cap_;
738 size_type __size_;
739 pointer __data_;
740 };
741
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000742#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000743 static const size_type __short_mask = 0x80;
744 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000745#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000746 static const size_type __short_mask = 0x01;
747 static const size_type __long_mask = 0x1ul;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400748#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +0000749
Howard Hinnantc51e1022010-05-11 19:42:16 +0000750 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
751 (sizeof(__long) - 1)/sizeof(value_type) : 2};
752
753 struct __short
754 {
755 union
756 {
757 unsigned char __size_;
Howard Hinnant49e145e2012-10-30 19:06:59 +0000758 value_type __lx;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000759 };
760 value_type __data_[__min_cap];
761 };
762
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400763#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000764
Howard Hinnant8ea98242013-08-23 17:37:05 +0000765 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000766
Howard Hinnant8ea98242013-08-23 17:37:05 +0000767 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000768
769 struct __raw
770 {
771 size_type __words[__n_words];
772 };
773
774 struct __rep
775 {
776 union
777 {
778 __long __l;
779 __short __s;
780 __raw __r;
781 };
782 };
783
784 __compressed_pair<__rep, allocator_type> __r_;
785
Howard Hinnantc51e1022010-05-11 19:42:16 +0000786public:
Martin Storsjö88890c22021-02-22 01:13:13 +0200787 _LIBCPP_TEMPLATE_DATA_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000788 static const size_type npos = -1;
789
Howard Hinnant3e276872011-06-03 18:40:47 +0000790 _LIBCPP_INLINE_VISIBILITY basic_string()
791 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000792
793 _LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a)
794#if _LIBCPP_STD_VER <= 14
795 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
796#else
797 _NOEXCEPT;
798#endif
799
Howard Hinnantc51e1022010-05-11 19:42:16 +0000800 basic_string(const basic_string& __str);
801 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clowa80abc72015-06-03 19:56:43 +0000802
Eric Fiselierfc92be82017-04-19 00:28:44 +0000803#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000804 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000805 basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +0000806#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +0000807 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000808#else
809 _NOEXCEPT;
810#endif
811
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000812 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000813 basic_string(basic_string&& __str, const allocator_type& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400814#endif // _LIBCPP_CXX03_LANG
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> >
Eric Fiseliera8567862018-07-17 05:48:48 +0000817 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5169d1c2019-12-16 19:03:23 -0500818 basic_string(const _CharT* __s) : __r_(__default_init_tag(), __default_init_tag()) {
Eric Fiseliera8567862018-07-17 05:48:48 +0000819 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
820 __init(__s, traits_type::length(__s));
Nikolas Klauserf2807732022-01-11 00:33:35 +0100821 _VSTD::__debug_db_insert_c(this);
Eric Fiseliera8567862018-07-17 05:48:48 +0000822 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000823
Louis Dionne9ce598d2021-09-08 09:14:43 -0400824 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000825 _LIBCPP_INLINE_VISIBILITY
826 basic_string(const _CharT* __s, const _Allocator& __a);
827
Marek Kurdej90d79712021-07-27 16:16:21 +0200828#if _LIBCPP_STD_VER > 20
829 basic_string(nullptr_t) = delete;
830#endif
831
Howard Hinnantcf823322010-12-17 14:46:43 +0000832 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000833 basic_string(const _CharT* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +0000834 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000835 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000836 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000837 basic_string(size_type __n, _CharT __c);
Marshall Clowe46031a2018-07-02 18:41:15 +0000838
Louis Dionne9ce598d2021-09-08 09:14:43 -0400839 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000840 _LIBCPP_INLINE_VISIBILITY
841 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
842
Marshall Clow83445802016-04-07 18:13:41 +0000843 basic_string(const basic_string& __str, size_type __pos, size_type __n,
Eric Fiselier812882b2017-02-17 01:17:10 +0000844 const _Allocator& __a = _Allocator());
Marshall Clow83445802016-04-07 18:13:41 +0000845 _LIBCPP_INLINE_VISIBILITY
846 basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +0000847 const _Allocator& __a = _Allocator());
Marshall Clowe46031a2018-07-02 18:41:15 +0000848
Louis Dionne9ce598d2021-09-08 09:14:43 -0400849 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 +0000850 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselier812882b2017-02-17 01:17:10 +0000851 basic_string(const _Tp& __t, size_type __pos, size_type __n,
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500852 const allocator_type& __a = allocator_type());
Marshall Clowe46031a2018-07-02 18:41:15 +0000853
Louis Dionne9ce598d2021-09-08 09:14:43 -0400854 template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500855 !__is_same_uncvref<_Tp, basic_string>::value> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000856 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
857 explicit basic_string(const _Tp& __t);
858
Louis Dionne9ce598d2021-09-08 09:14:43 -0400859 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 +0000860 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
861 explicit basic_string(const _Tp& __t, const allocator_type& __a);
862
Louis Dionne9ce598d2021-09-08 09:14:43 -0400863 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000864 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000865 basic_string(_InputIterator __first, _InputIterator __last);
Louis Dionne9ce598d2021-09-08 09:14:43 -0400866 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000867 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000868 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000869#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000870 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000871 basic_string(initializer_list<_CharT> __il);
Howard Hinnantcf823322010-12-17 14:46:43 +0000872 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000873 basic_string(initializer_list<_CharT> __il, const _Allocator& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400874#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000875
Eric Fiselierd9a702a2016-10-31 03:42:50 +0000876 inline ~basic_string();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000877
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000878 _LIBCPP_INLINE_VISIBILITY
879 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
880
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000881 basic_string& operator=(const basic_string& __str);
Eric Fiselier5ec13b12017-01-23 21:24:58 +0000882
Louis Dionne9ce598d2021-09-08 09:14:43 -0400883 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 +0000884 basic_string& operator=(const _Tp& __t)
885 {__self_view __sv = __t; return assign(__sv);}
886
Eric Fiselierfc92be82017-04-19 00:28:44 +0000887#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000888 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000889 basic_string& operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000890 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiselierfc92be82017-04-19 00:28:44 +0000891 _LIBCPP_INLINE_VISIBILITY
892 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000893#endif
Howard Hinnantd17880b2013-06-28 16:59:19 +0000894 _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const value_type* __s) {return assign(__s);}
Marek Kurdej90d79712021-07-27 16:16:21 +0200895#if _LIBCPP_STD_VER > 20
896 basic_string& operator=(nullptr_t) = delete;
897#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000898 basic_string& operator=(value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000899
Louis Dionneba400782020-10-02 15:02:52 -0400900#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +0000901 _LIBCPP_INLINE_VISIBILITY
902 iterator begin() _NOEXCEPT
903 {return iterator(this, __get_pointer());}
904 _LIBCPP_INLINE_VISIBILITY
905 const_iterator begin() const _NOEXCEPT
906 {return const_iterator(this, __get_pointer());}
907 _LIBCPP_INLINE_VISIBILITY
908 iterator end() _NOEXCEPT
909 {return iterator(this, __get_pointer() + size());}
910 _LIBCPP_INLINE_VISIBILITY
911 const_iterator end() const _NOEXCEPT
912 {return const_iterator(this, __get_pointer() + size());}
913#else
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000914 _LIBCPP_INLINE_VISIBILITY
915 iterator begin() _NOEXCEPT
916 {return iterator(__get_pointer());}
917 _LIBCPP_INLINE_VISIBILITY
918 const_iterator begin() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000919 {return const_iterator(__get_pointer());}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000920 _LIBCPP_INLINE_VISIBILITY
921 iterator end() _NOEXCEPT
922 {return iterator(__get_pointer() + size());}
923 _LIBCPP_INLINE_VISIBILITY
924 const_iterator end() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000925 {return const_iterator(__get_pointer() + size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400926#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000927 _LIBCPP_INLINE_VISIBILITY
928 reverse_iterator rbegin() _NOEXCEPT
929 {return reverse_iterator(end());}
930 _LIBCPP_INLINE_VISIBILITY
931 const_reverse_iterator rbegin() const _NOEXCEPT
932 {return const_reverse_iterator(end());}
933 _LIBCPP_INLINE_VISIBILITY
934 reverse_iterator rend() _NOEXCEPT
935 {return reverse_iterator(begin());}
936 _LIBCPP_INLINE_VISIBILITY
937 const_reverse_iterator rend() const _NOEXCEPT
938 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000939
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000940 _LIBCPP_INLINE_VISIBILITY
941 const_iterator cbegin() const _NOEXCEPT
942 {return begin();}
943 _LIBCPP_INLINE_VISIBILITY
944 const_iterator cend() const _NOEXCEPT
945 {return end();}
946 _LIBCPP_INLINE_VISIBILITY
947 const_reverse_iterator crbegin() const _NOEXCEPT
948 {return rbegin();}
949 _LIBCPP_INLINE_VISIBILITY
950 const_reverse_iterator crend() const _NOEXCEPT
951 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000952
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000953 _LIBCPP_INLINE_VISIBILITY size_type size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000954 {return __is_long() ? __get_long_size() : __get_short_size();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000955 _LIBCPP_INLINE_VISIBILITY size_type length() const _NOEXCEPT {return size();}
956 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT;
957 _LIBCPP_INLINE_VISIBILITY size_type capacity() const _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +0000958 {return (__is_long() ? __get_long_cap()
959 : static_cast<size_type>(__min_cap)) - 1;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000960
961 void resize(size_type __n, value_type __c);
962 _LIBCPP_INLINE_VISIBILITY void resize(size_type __n) {resize(__n, value_type());}
963
Marek Kurdejc9848142020-11-26 10:07:16 +0100964 void reserve(size_type __requested_capacity);
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100965
966#if _LIBCPP_STD_VER > 20
967 template <class _Op>
968 _LIBCPP_HIDE_FROM_ABI constexpr
969 void resize_and_overwrite(size_type __n, _Op __op) {
970 __resize_default_init(__n);
Nikolas Klausera4a76a12022-01-20 13:53:59 +0100971 __erase_to_end(_VSTD::move(__op)(data(), _LIBCPP_AUTO_CAST(__n)));
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100972 }
973#endif
974
Eric Fiselier451d5582018-11-26 20:15:38 +0000975 _LIBCPP_INLINE_VISIBILITY void __resize_default_init(size_type __n);
976
Marek Kurdejc9848142020-11-26 10:07:16 +0100977 _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_INLINE_VISIBILITY
978 void reserve() _NOEXCEPT {shrink_to_fit();}
Marshall Clow6ae12a82018-11-28 18:18:34 +0000979 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejc9848142020-11-26 10:07:16 +0100980 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000981 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000982 void clear() _NOEXCEPT;
Marshall Clowb7db4972017-11-15 20:02:27 +0000983 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
984 bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000985
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000986 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __pos) const _NOEXCEPT;
987 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000988
989 const_reference at(size_type __n) const;
990 reference at(size_type __n);
991
992 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const basic_string& __str) {return append(__str);}
Marshall Clowe46031a2018-07-02 18:41:15 +0000993
994 template <class _Tp>
995 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -0400996 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +0000997 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500998 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
999 && !__is_same_uncvref<_Tp, basic_string >::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001000 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001001 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001002 operator+=(const _Tp& __t) {__self_view __sv = __t; return append(__sv);}
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001003 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const value_type* __s) {return append(__s);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001004 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(value_type __c) {push_back(__c); return *this;}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001005#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001006 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(initializer_list<value_type> __il) {return append(__il);}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001007#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001008
Howard Hinnantcf823322010-12-17 14:46:43 +00001009 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001010 basic_string& append(const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001011
1012 template <class _Tp>
1013 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001014 __enable_if_t<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001015 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1016 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001017 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001018 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001019 append(const _Tp& __t) { __self_view __sv = __t; return append(__sv.data(), __sv.size()); }
Marshall Clow8db7fb02014-03-04 19:17:19 +00001020 basic_string& append(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clowe46031a2018-07-02 18:41:15 +00001021
Marshall Clow62953962016-10-03 23:40:48 +00001022 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001023 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001024 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001025 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001026 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1027 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001028 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001029 >
Marshall Clow82513342016-09-24 22:45:42 +00001030 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001031 basic_string& append(const value_type* __s, size_type __n);
1032 basic_string& append(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001033 basic_string& append(size_type __n, value_type __c);
Eric Fiselier451d5582018-11-26 20:15:38 +00001034
1035 _LIBCPP_INLINE_VISIBILITY
1036 void __append_default_init(size_type __n);
1037
Howard Hinnantc51e1022010-05-11 19:42:16 +00001038 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001039 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001040 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001041 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001042 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001043 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001044 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001045 _LIBCPP_INLINE_VISIBILITY
1046 append(_InputIterator __first, _InputIterator __last) {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001047 const basic_string __temp(__first, __last, __alloc());
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001048 append(__temp.data(), __temp.size());
1049 return *this;
1050 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001051 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001052 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001053 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001054 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001055 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001056 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001057 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001058 _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001059 append(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001060
Eric Fiselierfc92be82017-04-19 00:28:44 +00001061#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001062 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001063 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001064#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001065
1066 void push_back(value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001067 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001068 void pop_back();
Marshall Clow05cf6692019-03-19 03:30:07 +00001069 _LIBCPP_INLINE_VISIBILITY reference front() _NOEXCEPT;
1070 _LIBCPP_INLINE_VISIBILITY const_reference front() const _NOEXCEPT;
1071 _LIBCPP_INLINE_VISIBILITY reference back() _NOEXCEPT;
1072 _LIBCPP_INLINE_VISIBILITY const_reference back() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001073
Marshall Clowe46031a2018-07-02 18:41:15 +00001074 template <class _Tp>
1075 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001076 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001077 <
1078 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1079 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001080 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001081 assign(const _Tp & __t) { __self_view __sv = __t; return assign(__sv.data(), __sv.size()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001082 _LIBCPP_INLINE_VISIBILITY
Marshall Clow95d5e9a2016-03-09 18:08:29 +00001083 basic_string& assign(const basic_string& __str) { return *this = __str; }
Eric Fiselierfc92be82017-04-19 00:28:44 +00001084#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001085 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001086 basic_string& assign(basic_string&& __str)
Marshall Clow5aa9e942015-10-05 16:17:34 +00001087 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001088 {*this = _VSTD::move(__str); return *this;}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001089#endif
Marshall Clow8db7fb02014-03-04 19:17:19 +00001090 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow62953962016-10-03 23:40:48 +00001091 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001092 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001093 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001094 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001095 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1096 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001097 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001098 >
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001099 assign(const _Tp & __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001100 basic_string& assign(const value_type* __s, size_type __n);
1101 basic_string& assign(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001102 basic_string& assign(size_type __n, value_type __c);
1103 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001104 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001105 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001106 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001107 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001108 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001109 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001110 assign(_InputIterator __first, _InputIterator __last);
1111 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001112 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001113 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001114 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001115 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001116 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001117 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001118 assign(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001119#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001120 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001121 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001122#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001123
Howard Hinnantcf823322010-12-17 14:46:43 +00001124 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001125 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001126
1127 template <class _Tp>
1128 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001129 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001130 <
1131 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1132 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001133 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001134 insert(size_type __pos1, const _Tp& __t)
1135 { __self_view __sv = __t; return insert(__pos1, __sv.data(), __sv.size()); }
1136
Marshall Clow82513342016-09-24 22:45:42 +00001137 template <class _Tp>
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
Marshall Clow82513342016-09-24 22:45:42 +00001140 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001141 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001142 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001143 >
Marshall Clow82513342016-09-24 22:45:42 +00001144 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Marshall Clow8db7fb02014-03-04 19:17:19 +00001145 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001146 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1147 basic_string& insert(size_type __pos, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001148 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1149 iterator insert(const_iterator __pos, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001150 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001151 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1152 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001153 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001154 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001155 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001156 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001157 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001158 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001159 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1160 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001161 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001162 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001163 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001164 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001165 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001166 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001167 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001168#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001169 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001170 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1171 {return insert(__pos, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001172#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001173
1174 basic_string& erase(size_type __pos = 0, size_type __n = npos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001175 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001176 iterator erase(const_iterator __pos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001177 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001178 iterator erase(const_iterator __first, const_iterator __last);
1179
Howard Hinnantcf823322010-12-17 14:46:43 +00001180 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001181 basic_string& replace(size_type __pos1, size_type __n1, 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(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 +00001191 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 +00001192 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001193 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001194 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001195 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001196 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001197 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001198 >
Marshall Clow82513342016-09-24 22:45:42 +00001199 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001200 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
1201 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001202 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001203 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001204 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001205
1206 template <class _Tp>
1207 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001208 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001209 <
1210 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1211 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001212 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001213 replace(const_iterator __i1, const_iterator __i2, const _Tp& __t) { __self_view __sv = __t; return replace(__i1 - begin(), __i2 - __i1, __sv); }
1214
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001215 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001216 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +00001217 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001218 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Howard Hinnantcf823322010-12-17 14:46:43 +00001219 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001220 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001221 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001222 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001223 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001224 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001225 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001226 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001227 >
Howard Hinnant990d6e82010-11-17 21:11:40 +00001228 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001229#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001230 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001231 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001232 {return replace(__i1, __i2, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001233#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001234
Howard Hinnantd17880b2013-06-28 16:59:19 +00001235 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
Howard Hinnantcf823322010-12-17 14:46:43 +00001236 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001237 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1238
Howard Hinnantcf823322010-12-17 14:46:43 +00001239 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001240 void swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001241#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00001242 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +00001243#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001244 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001245 __is_nothrow_swappable<allocator_type>::value);
1246#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001247
Howard Hinnantcf823322010-12-17 14:46:43 +00001248 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001249 const value_type* c_str() const _NOEXCEPT {return data();}
Howard Hinnantcf823322010-12-17 14:46:43 +00001250 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001251 const value_type* data() const _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Eric Fiselier4ee2a892017-11-20 20:23:27 +00001252#if _LIBCPP_STD_VER > 14 || defined(_LIBCPP_BUILDING_LIBRARY)
Marshall Clowd3c22392016-03-08 15:44:30 +00001253 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001254 value_type* data() _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Marshall Clowd3c22392016-03-08 15:44:30 +00001255#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001256
Howard Hinnantcf823322010-12-17 14:46:43 +00001257 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001258 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001259
Howard Hinnantcf823322010-12-17 14:46:43 +00001260 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001261 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001262
1263 template <class _Tp>
1264 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001265 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001266 <
1267 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1268 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001269 >
zoecarver1997e0a2021-02-05 11:54:47 -08001270 find(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001271 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001272 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001273 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001274 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001275
Howard Hinnantcf823322010-12-17 14:46:43 +00001276 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001277 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001278
1279 template <class _Tp>
1280 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001281 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001282 <
1283 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1284 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001285 >
zoecarver1997e0a2021-02-05 11:54:47 -08001286 rfind(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001287 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001288 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001289 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001290 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001291
Howard Hinnantcf823322010-12-17 14:46:43 +00001292 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001293 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001294
1295 template <class _Tp>
1296 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001297 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001298 <
1299 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1300 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001301 >
zoecarver1997e0a2021-02-05 11:54:47 -08001302 find_first_of(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001303 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001304 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001305 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001306 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001307 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001308
Howard Hinnantcf823322010-12-17 14:46:43 +00001309 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001310 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001311
1312 template <class _Tp>
1313 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001314 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001315 <
1316 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1317 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001318 >
zoecarver1997e0a2021-02-05 11:54:47 -08001319 find_last_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001320 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001321 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001322 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001323 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001324 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001325
Howard Hinnantcf823322010-12-17 14:46:43 +00001326 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001327 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001328
1329 template <class _Tp>
1330 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001331 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001332 <
1333 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1334 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001335 >
zoecarver1997e0a2021-02-05 11:54:47 -08001336 find_first_not_of(const _Tp &__t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001337 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 +00001338 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001339 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001340 _LIBCPP_INLINE_VISIBILITY
1341 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1342
1343 _LIBCPP_INLINE_VISIBILITY
1344 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001345
1346 template <class _Tp>
1347 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001348 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001349 <
1350 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1351 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001352 >
zoecarver1997e0a2021-02-05 11:54:47 -08001353 find_last_not_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001354 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 +00001355 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001356 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001357 _LIBCPP_INLINE_VISIBILITY
1358 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1359
1360 _LIBCPP_INLINE_VISIBILITY
1361 int compare(const basic_string& __str) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001362
1363 template <class _Tp>
1364 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001365 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001366 <
1367 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1368 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001369 >
zoecarver1997e0a2021-02-05 11:54:47 -08001370 compare(const _Tp &__t) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001371
1372 template <class _Tp>
1373 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001374 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001375 <
1376 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1377 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001378 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001379 compare(size_type __pos1, size_type __n1, const _Tp& __t) const;
1380
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001381 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001382 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Marshall Clow8db7fb02014-03-04 19:17:19 +00001383 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 +00001384
Marshall Clow82513342016-09-24 22:45:42 +00001385 template <class _Tp>
Eric Fiselier7d4aa5c2016-10-14 05:29:46 +00001386 inline _LIBCPP_INLINE_VISIBILITY
Louis Dionne9ce598d2021-09-08 09:14:43 -04001387 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001388 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001389 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001390 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001391 >
Marshall Clow82513342016-09-24 22:45:42 +00001392 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 +00001393 int compare(const value_type* __s) const _NOEXCEPT;
1394 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1395 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001396
Marshall Clow18c293b2017-12-04 20:11:38 +00001397#if _LIBCPP_STD_VER > 17
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001398 constexpr _LIBCPP_INLINE_VISIBILITY
1399 bool starts_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001400 { return __self_view(data(), size()).starts_with(__sv); }
1401
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001402 constexpr _LIBCPP_INLINE_VISIBILITY
1403 bool starts_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001404 { return !empty() && _Traits::eq(front(), __c); }
1405
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001406 constexpr _LIBCPP_INLINE_VISIBILITY
1407 bool starts_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001408 { return starts_with(__self_view(__s)); }
1409
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001410 constexpr _LIBCPP_INLINE_VISIBILITY
1411 bool ends_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001412 { return __self_view(data(), size()).ends_with( __sv); }
1413
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001414 constexpr _LIBCPP_INLINE_VISIBILITY
1415 bool ends_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001416 { return !empty() && _Traits::eq(back(), __c); }
1417
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001418 constexpr _LIBCPP_INLINE_VISIBILITY
1419 bool ends_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001420 { return ends_with(__self_view(__s)); }
1421#endif
1422
Wim Leflere023c3542021-01-19 14:33:30 -05001423#if _LIBCPP_STD_VER > 20
1424 constexpr _LIBCPP_INLINE_VISIBILITY
1425 bool contains(__self_view __sv) const noexcept
1426 { return __self_view(data(), size()).contains(__sv); }
1427
1428 constexpr _LIBCPP_INLINE_VISIBILITY
1429 bool contains(value_type __c) const noexcept
1430 { return __self_view(data(), size()).contains(__c); }
1431
1432 constexpr _LIBCPP_INLINE_VISIBILITY
1433 bool contains(const value_type* __s) const
1434 { return __self_view(data(), size()).contains(__s); }
1435#endif
1436
Howard Hinnantcf823322010-12-17 14:46:43 +00001437 _LIBCPP_INLINE_VISIBILITY bool __invariants() const;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001438
Marshall Clowe60a7182018-05-29 17:04:37 +00001439 _LIBCPP_INLINE_VISIBILITY void __clear_and_shrink() _NOEXCEPT;
Marshall Clow851b9ec2019-05-20 21:56:51 +00001440
Marek Kurdejc9848142020-11-26 10:07:16 +01001441 _LIBCPP_INLINE_VISIBILITY void __shrink_or_extend(size_type __target_capacity);
1442
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001443 _LIBCPP_INLINE_VISIBILITY
1444 bool __is_long() const _NOEXCEPT
1445 {return bool(__r_.first().__s.__size_ & __short_mask);}
1446
Louis Dionneba400782020-10-02 15:02:52 -04001447#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001448
1449 bool __dereferenceable(const const_iterator* __i) const;
1450 bool __decrementable(const const_iterator* __i) const;
1451 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1452 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1453
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001454#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001455
Howard Hinnantc51e1022010-05-11 19:42:16 +00001456private:
Nikolas Klauser93826d12022-01-04 17:24:03 +01001457 _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI static bool __fits_in_sso(size_type __sz) {
1458 // SSO is disabled during constant evaluation because `__is_long` isn't constexpr friendly
1459 return !__libcpp_is_constant_evaluated() && (__sz < __min_cap);
1460 }
1461
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001462 _LIBCPP_INLINE_VISIBILITY
1463 allocator_type& __alloc() _NOEXCEPT
1464 {return __r_.second();}
1465 _LIBCPP_INLINE_VISIBILITY
1466 const allocator_type& __alloc() const _NOEXCEPT
1467 {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 Klausereed25832021-12-15 01:32:30 +01002811 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2812 "string::insert(iterator, range) called with an iterator not"
2813 " referring to this string");
2814
Howard Hinnantc51e1022010-05-11 19:42:16 +00002815 size_type __ip = static_cast<size_type>(__pos - begin());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002816 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002817 if (__n)
2818 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002819 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2820 !__addr_in_range(*__first))
2821 {
2822 size_type __sz = size();
2823 size_type __cap = capacity();
2824 value_type* __p;
2825 if (__cap - __sz >= __n)
2826 {
2827 __p = _VSTD::__to_address(__get_pointer());
2828 size_type __n_move = __sz - __ip;
2829 if (__n_move != 0)
2830 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
2831 }
2832 else
2833 {
2834 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
2835 __p = _VSTD::__to_address(__get_long_pointer());
2836 }
2837 __sz += __n;
2838 __set_size(__sz);
2839 traits_type::assign(__p[__sz], value_type());
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002840 for (__p += __ip; __first != __last; ++__p, (void) ++__first)
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002841 traits_type::assign(*__p, *__first);
2842 }
2843 else
Marshall Clow958362f2016-09-05 01:54:30 +00002844 {
2845 const basic_string __temp(__first, __last, __alloc());
2846 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
2847 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002848 }
2849 return begin() + __ip;
2850}
2851
2852template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002853inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002854basic_string<_CharT, _Traits, _Allocator>&
2855basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2856{
2857 return insert(__pos1, __str.data(), __str.size());
2858}
2859
2860template <class _CharT, class _Traits, class _Allocator>
2861basic_string<_CharT, _Traits, _Allocator>&
2862basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2863 size_type __pos2, size_type __n)
2864{
2865 size_type __str_sz = __str.size();
2866 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002867 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002868 return insert(__pos1, __str.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002869}
2870
2871template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002872template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002873__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002874<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002875 __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 +00002876 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002877>
Marshall Clow82513342016-09-24 22:45:42 +00002878basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002879 size_type __pos2, size_type __n)
2880{
Marshall Clow82513342016-09-24 22:45:42 +00002881 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002882 size_type __str_sz = __sv.size();
2883 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002884 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002885 return insert(__pos1, __sv.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
2886}
2887
2888template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002889basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002890basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002891{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002892 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002893 return insert(__pos, __s, traits_type::length(__s));
2894}
2895
2896template <class _CharT, class _Traits, class _Allocator>
2897typename basic_string<_CharT, _Traits, _Allocator>::iterator
2898basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2899{
2900 size_type __ip = static_cast<size_type>(__pos - begin());
2901 size_type __sz = size();
2902 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002903 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002904 if (__cap == __sz)
2905 {
2906 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002907 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002908 }
2909 else
2910 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002911 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002912 size_type __n_move = __sz - __ip;
2913 if (__n_move != 0)
2914 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2915 }
2916 traits_type::assign(__p[__ip], __c);
2917 traits_type::assign(__p[++__sz], value_type());
2918 __set_size(__sz);
2919 return begin() + static_cast<difference_type>(__ip);
2920}
2921
2922template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002923inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002924typename basic_string<_CharT, _Traits, _Allocator>::iterator
2925basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2926{
Nikolas Klausereed25832021-12-15 01:32:30 +01002927 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2928 "string::insert(iterator, n, value) called with an iterator not"
2929 " referring to this string");
2930 difference_type __p = __pos - begin();
2931 insert(static_cast<size_type>(__p), __n, __c);
2932 return begin() + __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002933}
2934
2935// replace
2936
2937template <class _CharT, class _Traits, class _Allocator>
2938basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002939basic_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 +00002940 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00002941{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002942 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002943 size_type __sz = size();
2944 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002945 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002946 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002947 size_type __cap = capacity();
2948 if (__cap - __sz + __n1 >= __n2)
2949 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002950 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002951 if (__n1 != __n2)
2952 {
2953 size_type __n_move = __sz - __pos - __n1;
2954 if (__n_move != 0)
2955 {
2956 if (__n1 > __n2)
2957 {
2958 traits_type::move(__p + __pos, __s, __n2);
2959 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002960 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002961 }
2962 if (__p + __pos < __s && __s < __p + __sz)
2963 {
2964 if (__p + __pos + __n1 <= __s)
2965 __s += __n2 - __n1;
2966 else // __p + __pos < __s < __p + __pos + __n1
2967 {
2968 traits_type::move(__p + __pos, __s, __n1);
2969 __pos += __n1;
2970 __s += __n2;
2971 __n2 -= __n1;
2972 __n1 = 0;
2973 }
2974 }
2975 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2976 }
2977 }
2978 traits_type::move(__p + __pos, __s, __n2);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002979 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002980 }
2981 else
2982 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
2983 return *this;
2984}
2985
2986template <class _CharT, class _Traits, class _Allocator>
2987basic_string<_CharT, _Traits, _Allocator>&
2988basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
2989{
2990 size_type __sz = size();
2991 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002992 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002993 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002994 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002995 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002996 if (__cap - __sz + __n1 >= __n2)
2997 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002998 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002999 if (__n1 != __n2)
3000 {
3001 size_type __n_move = __sz - __pos - __n1;
3002 if (__n_move != 0)
3003 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3004 }
3005 }
3006 else
3007 {
3008 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003009 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003010 }
3011 traits_type::assign(__p + __pos, __n2, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003012 return __null_terminate_at(__p, __sz - (__n1 - __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003013}
3014
3015template <class _CharT, class _Traits, class _Allocator>
3016template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003017__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00003018<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003019 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003020 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003021>
Howard Hinnant990d6e82010-11-17 21:11:40 +00003022basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003023 _InputIterator __j1, _InputIterator __j2)
3024{
Marshall Clow958362f2016-09-05 01:54:30 +00003025 const basic_string __temp(__j1, __j2, __alloc());
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003026 return replace(__i1, __i2, __temp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003027}
3028
3029template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003030inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003031basic_string<_CharT, _Traits, _Allocator>&
3032basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
3033{
3034 return replace(__pos1, __n1, __str.data(), __str.size());
3035}
3036
3037template <class _CharT, class _Traits, class _Allocator>
3038basic_string<_CharT, _Traits, _Allocator>&
3039basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
3040 size_type __pos2, size_type __n2)
3041{
3042 size_type __str_sz = __str.size();
3043 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003044 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003045 return replace(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003046}
3047
3048template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003049template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003050__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003051<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003052 __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 +00003053 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003054>
Marshall Clow82513342016-09-24 22:45:42 +00003055basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003056 size_type __pos2, size_type __n2)
3057{
Marshall Clow82513342016-09-24 22:45:42 +00003058 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003059 size_type __str_sz = __sv.size();
3060 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003061 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003062 return replace(__pos1, __n1, __sv.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
3063}
3064
3065template <class _CharT, class _Traits, class _Allocator>
3066basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003067basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003068{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003069 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003070 return replace(__pos, __n1, __s, traits_type::length(__s));
3071}
3072
3073template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003074inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003075basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003076basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003077{
3078 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
3079 __str.data(), __str.size());
3080}
3081
3082template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003083inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003084basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003085basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003086{
3087 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
3088}
3089
3090template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003091inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003092basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003093basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003094{
3095 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
3096}
3097
3098template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003099inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003100basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003101basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003102{
3103 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
3104}
3105
3106// erase
3107
Martijn Velsa81fc792020-02-26 13:25:43 -05003108// 'externally instantiated' erase() implementation, called when __n != npos.
3109// Does not check __pos against size()
Howard Hinnantc51e1022010-05-11 19:42:16 +00003110template <class _CharT, class _Traits, class _Allocator>
Martijn Velsa81fc792020-02-26 13:25:43 -05003111void
3112basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(
3113 size_type __pos, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003114{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003115 if (__n)
3116 {
Martijn Velsa81fc792020-02-26 13:25:43 -05003117 size_type __sz = size();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003118 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003119 __n = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003120 size_type __n_move = __sz - __pos - __n;
3121 if (__n_move != 0)
3122 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003123 __null_terminate_at(__p, __sz - __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003124 }
Martijn Velsa81fc792020-02-26 13:25:43 -05003125}
3126
3127template <class _CharT, class _Traits, class _Allocator>
3128basic_string<_CharT, _Traits, _Allocator>&
3129basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos,
3130 size_type __n) {
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003131 if (__pos > size())
3132 __throw_out_of_range();
Martijn Velsa81fc792020-02-26 13:25:43 -05003133 if (__n == npos) {
3134 __erase_to_end(__pos);
3135 } else {
3136 __erase_external_with_move(__pos, __n);
3137 }
3138 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003139}
3140
3141template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003142inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003143typename basic_string<_CharT, _Traits, _Allocator>::iterator
3144basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
3145{
Nikolas Klausereed25832021-12-15 01:32:30 +01003146 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3147 "string::erase(iterator) called with an iterator not"
3148 " referring to this string");
3149
3150 _LIBCPP_ASSERT(__pos != end(), "string::erase(iterator) called with a non-dereferenceable iterator");
3151 iterator __b = begin();
3152 size_type __r = static_cast<size_type>(__pos - __b);
3153 erase(__r, 1);
3154 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003155}
3156
3157template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003158inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003159typename basic_string<_CharT, _Traits, _Allocator>::iterator
3160basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
3161{
Nikolas Klausereed25832021-12-15 01:32:30 +01003162 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
3163 "string::erase(iterator, iterator) called with an iterator not"
3164 " referring to this string");
3165
3166 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
3167 iterator __b = begin();
3168 size_type __r = static_cast<size_type>(__first - __b);
3169 erase(__r, static_cast<size_type>(__last - __first));
3170 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003171}
3172
3173template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003174inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003175void
3176basic_string<_CharT, _Traits, _Allocator>::pop_back()
3177{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003178 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003179 __erase_to_end(size() - 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003180}
3181
3182template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003183inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003184void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003185basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003186{
3187 __invalidate_all_iterators();
3188 if (__is_long())
3189 {
3190 traits_type::assign(*__get_long_pointer(), value_type());
3191 __set_long_size(0);
3192 }
3193 else
3194 {
3195 traits_type::assign(*__get_short_pointer(), value_type());
3196 __set_short_size(0);
3197 }
3198}
3199
3200template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003201inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003202void
3203basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
3204{
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003205 __null_terminate_at(_VSTD::__to_address(__get_pointer()), __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003206}
3207
3208template <class _CharT, class _Traits, class _Allocator>
3209void
3210basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
3211{
3212 size_type __sz = size();
3213 if (__n > __sz)
3214 append(__n - __sz, __c);
3215 else
3216 __erase_to_end(__n);
3217}
3218
3219template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00003220inline void
3221basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n)
3222{
3223 size_type __sz = size();
3224 if (__n > __sz) {
3225 __append_default_init(__n - __sz);
3226 } else
3227 __erase_to_end(__n);
3228}
3229
3230template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003231inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003232typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003233basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003234{
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003235 size_type __m = __alloc_traits::max_size(__alloc());
Eric Fiseliere9cc5922017-10-17 13:16:01 +00003236#ifdef _LIBCPP_BIG_ENDIAN
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003237 return (__m <= ~__long_mask ? __m : __m/2) - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003238#else
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003239 return __m - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003240#endif
3241}
3242
3243template <class _CharT, class _Traits, class _Allocator>
3244void
Marek Kurdejc9848142020-11-26 10:07:16 +01003245basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __requested_capacity)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003246{
Marek Kurdejc9848142020-11-26 10:07:16 +01003247 if (__requested_capacity > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003248 __throw_length_error();
Marek Kurdejc9848142020-11-26 10:07:16 +01003249
Louis Dionne05b6d3e2022-01-14 12:30:22 -05003250 // Make sure reserve(n) never shrinks. This is technically only required in C++20
3251 // and later (since P0966R1), however we provide consistent behavior in all Standard
3252 // modes because this function is instantiated in the shared library.
3253 if (__requested_capacity <= capacity())
3254 return;
Marek Kurdejc9848142020-11-26 10:07:16 +01003255
3256 size_type __target_capacity = _VSTD::max(__requested_capacity, size());
3257 __target_capacity = __recommend(__target_capacity);
3258 if (__target_capacity == capacity()) return;
3259
3260 __shrink_or_extend(__target_capacity);
3261}
3262
3263template <class _CharT, class _Traits, class _Allocator>
Louis Dionnee38b8642021-12-13 14:15:21 -05003264inline
Marek Kurdejc9848142020-11-26 10:07:16 +01003265void
3266basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT
3267{
3268 size_type __target_capacity = __recommend(size());
3269 if (__target_capacity == capacity()) return;
3270
3271 __shrink_or_extend(__target_capacity);
3272}
3273
3274template <class _CharT, class _Traits, class _Allocator>
Louis Dionnee38b8642021-12-13 14:15:21 -05003275inline
Marek Kurdejc9848142020-11-26 10:07:16 +01003276void
3277basic_string<_CharT, _Traits, _Allocator>::__shrink_or_extend(size_type __target_capacity)
3278{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003279 size_type __cap = capacity();
3280 size_type __sz = size();
Marek Kurdejc9848142020-11-26 10:07:16 +01003281
3282 pointer __new_data, __p;
3283 bool __was_long, __now_long;
3284 if (__target_capacity == __min_cap - 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003285 {
Marek Kurdejc9848142020-11-26 10:07:16 +01003286 __was_long = true;
3287 __now_long = false;
3288 __new_data = __get_short_pointer();
3289 __p = __get_long_pointer();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003290 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003291 else
3292 {
3293 if (__target_capacity > __cap)
3294 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3295 else
3296 {
3297 #ifndef _LIBCPP_NO_EXCEPTIONS
3298 try
3299 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003300 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003301 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3302 #ifndef _LIBCPP_NO_EXCEPTIONS
3303 }
3304 catch (...)
3305 {
3306 return;
3307 }
3308 #else // _LIBCPP_NO_EXCEPTIONS
3309 if (__new_data == nullptr)
3310 return;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003311 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003312 }
3313 __now_long = true;
3314 __was_long = __is_long();
3315 __p = __get_pointer();
3316 }
3317 traits_type::copy(_VSTD::__to_address(__new_data),
3318 _VSTD::__to_address(__p), size()+1);
3319 if (__was_long)
3320 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
3321 if (__now_long)
3322 {
3323 __set_long_cap(__target_capacity+1);
3324 __set_long_size(__sz);
3325 __set_long_pointer(__new_data);
3326 }
3327 else
3328 __set_short_size(__sz);
3329 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003330}
3331
3332template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003333inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003334typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003335basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003336{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003337 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003338 return *(data() + __pos);
3339}
3340
3341template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003342inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003343typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003344basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003345{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003346 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003347 return *(__get_pointer() + __pos);
3348}
3349
3350template <class _CharT, class _Traits, class _Allocator>
3351typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3352basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3353{
3354 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003355 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003356 return (*this)[__n];
3357}
3358
3359template <class _CharT, class _Traits, class _Allocator>
3360typename basic_string<_CharT, _Traits, _Allocator>::reference
3361basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
3362{
3363 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003364 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003365 return (*this)[__n];
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>::front() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003372{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003373 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003374 return *__get_pointer();
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>::front() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003381{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003382 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003383 return *data();
3384}
3385
3386template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003387inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003388typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003389basic_string<_CharT, _Traits, _Allocator>::back() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003390{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003391 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003392 return *(__get_pointer() + size() - 1);
3393}
3394
3395template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003396inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003397typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003398basic_string<_CharT, _Traits, _Allocator>::back() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003399{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003400 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003401 return *(data() + size() - 1);
3402}
3403
3404template <class _CharT, class _Traits, class _Allocator>
3405typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003406basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003407{
3408 size_type __sz = size();
3409 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003410 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003411 size_type __rlen = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003412 traits_type::copy(__s, data() + __pos, __rlen);
3413 return __rlen;
3414}
3415
3416template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003417inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003418basic_string<_CharT, _Traits, _Allocator>
3419basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3420{
3421 return basic_string(*this, __pos, __n, __alloc());
3422}
3423
3424template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003425inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003426void
3427basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003428#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00003429 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00003430#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00003431 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003432 __is_nothrow_swappable<allocator_type>::value)
3433#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003434{
Louis Dionneba400782020-10-02 15:02:52 -04003435#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01003436 if (!__libcpp_is_constant_evaluated()) {
3437 if (!__is_long())
3438 __get_db()->__invalidate_all(this);
3439 if (!__str.__is_long())
3440 __get_db()->__invalidate_all(&__str);
3441 __get_db()->swap(this, &__str);
3442 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00003443#endif
Eric Fiselier9bf691f2016-12-28 05:53:01 +00003444 _LIBCPP_ASSERT(
3445 __alloc_traits::propagate_on_container_swap::value ||
3446 __alloc_traits::is_always_equal::value ||
3447 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003448 _VSTD::swap(__r_.first(), __str.__r_.first());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05003449 _VSTD::__swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003450}
3451
3452// find
3453
3454template <class _Traits>
3455struct _LIBCPP_HIDDEN __traits_eq
3456{
3457 typedef typename _Traits::char_type char_type;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003458 _LIBCPP_INLINE_VISIBILITY
3459 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3460 {return _Traits::eq(__x, __y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003461};
3462
3463template<class _CharT, class _Traits, class _Allocator>
3464typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003465basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003466 size_type __pos,
3467 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003468{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003469 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003470 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003471 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003472}
3473
3474template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003475inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003476typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003477basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3478 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003479{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003480 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003481 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003482}
3483
3484template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003485template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003486__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003487<
3488 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3489 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003490>
Marshall Clowe46031a2018-07-02 18:41:15 +00003491basic_string<_CharT, _Traits, _Allocator>::find(const _Tp &__t,
zoecarver1997e0a2021-02-05 11:54:47 -08003492 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003493{
Marshall Clowe46031a2018-07-02 18:41:15 +00003494 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003495 return __str_find<value_type, size_type, traits_type, npos>
3496 (data(), size(), __sv.data(), __pos, __sv.size());
3497}
3498
3499template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003500inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003501typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003502basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003503 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003504{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003505 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003506 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003507 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003508}
3509
3510template<class _CharT, class _Traits, class _Allocator>
3511typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003512basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3513 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003514{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003515 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003516 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003517}
3518
3519// rfind
3520
3521template<class _CharT, class _Traits, class _Allocator>
3522typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003523basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003524 size_type __pos,
3525 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003526{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003527 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003528 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003529 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003530}
3531
3532template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003533inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003534typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003535basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3536 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003537{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003538 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003539 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003540}
3541
3542template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003543template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003544__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003545<
3546 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3547 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003548>
Marshall Clowe46031a2018-07-02 18:41:15 +00003549basic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003550 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003551{
Marshall Clowe46031a2018-07-02 18:41:15 +00003552 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003553 return __str_rfind<value_type, size_type, traits_type, npos>
3554 (data(), size(), __sv.data(), __pos, __sv.size());
3555}
3556
3557template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003558inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003559typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003560basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003561 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003562{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003563 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003564 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003565 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003566}
3567
3568template<class _CharT, class _Traits, class _Allocator>
3569typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003570basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3571 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003572{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003573 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003574 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003575}
3576
3577// find_first_of
3578
3579template<class _CharT, class _Traits, class _Allocator>
3580typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003581basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003582 size_type __pos,
3583 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003584{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003585 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003586 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003587 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003588}
3589
3590template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003591inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003592typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003593basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3594 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003595{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003596 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003597 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003598}
3599
3600template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003601template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003602__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003603<
3604 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3605 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003606>
Marshall Clowe46031a2018-07-02 18:41:15 +00003607basic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003608 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003609{
Marshall Clowe46031a2018-07-02 18:41:15 +00003610 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003611 return __str_find_first_of<value_type, size_type, traits_type, npos>
3612 (data(), size(), __sv.data(), __pos, __sv.size());
3613}
3614
3615template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003616inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003617typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003618basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003619 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003620{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003621 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003622 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003623 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003624}
3625
3626template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003627inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003628typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003629basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3630 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003631{
3632 return find(__c, __pos);
3633}
3634
3635// find_last_of
3636
3637template<class _CharT, class _Traits, class _Allocator>
3638typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003639basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003640 size_type __pos,
3641 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003642{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003643 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003644 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003645 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003646}
3647
3648template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003649inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003650typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003651basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3652 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003653{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003654 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003655 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003656}
3657
3658template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003659template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003660__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003661<
3662 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3663 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003664>
Marshall Clowe46031a2018-07-02 18:41:15 +00003665basic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003666 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003667{
Marshall Clowe46031a2018-07-02 18:41:15 +00003668 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003669 return __str_find_last_of<value_type, size_type, traits_type, npos>
3670 (data(), size(), __sv.data(), __pos, __sv.size());
3671}
3672
3673template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003674inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003675typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003676basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003677 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003678{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003679 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003680 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003681 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003682}
3683
3684template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003685inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003686typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003687basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3688 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003689{
3690 return rfind(__c, __pos);
3691}
3692
3693// find_first_not_of
3694
3695template<class _CharT, class _Traits, class _Allocator>
3696typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003697basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003698 size_type __pos,
3699 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003700{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003701 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003702 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003703 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003704}
3705
3706template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003707inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003708typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003709basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3710 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003711{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003712 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003713 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003714}
3715
3716template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003717template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003718__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003719<
3720 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3721 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003722>
Marshall Clowe46031a2018-07-02 18:41:15 +00003723basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003724 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003725{
Marshall Clowe46031a2018-07-02 18:41:15 +00003726 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003727 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3728 (data(), size(), __sv.data(), __pos, __sv.size());
3729}
3730
3731template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003732inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003733typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003734basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003735 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003736{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003737 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003738 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003739 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003740}
3741
3742template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003743inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003744typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003745basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3746 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003747{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003748 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003749 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003750}
3751
3752// find_last_not_of
3753
3754template<class _CharT, class _Traits, class _Allocator>
3755typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003756basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003757 size_type __pos,
3758 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003759{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003760 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003761 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003762 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003763}
3764
3765template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003766inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003767typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003768basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3769 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003770{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003771 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003772 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003773}
3774
3775template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003776template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003777__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003778<
3779 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3780 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003781>
Marshall Clowe46031a2018-07-02 18:41:15 +00003782basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003783 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003784{
Marshall Clowe46031a2018-07-02 18:41:15 +00003785 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003786 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3787 (data(), size(), __sv.data(), __pos, __sv.size());
3788}
3789
3790template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003791inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003792typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003793basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003794 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003795{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003796 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003797 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003798 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003799}
3800
3801template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003802inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003803typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003804basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3805 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003806{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003807 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003808 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003809}
3810
3811// compare
3812
3813template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003814template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003815__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003816<
3817 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3818 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003819>
zoecarver1997e0a2021-02-05 11:54:47 -08003820basic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003821{
Marshall Clowe46031a2018-07-02 18:41:15 +00003822 __self_view __sv = __t;
Howard Hinnantb0485532011-07-24 21:45:06 +00003823 size_t __lhs_sz = size();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003824 size_t __rhs_sz = __sv.size();
3825 int __result = traits_type::compare(data(), __sv.data(),
Howard Hinnantb0485532011-07-24 21:45:06 +00003826 _VSTD::min(__lhs_sz, __rhs_sz));
3827 if (__result != 0)
3828 return __result;
3829 if (__lhs_sz < __rhs_sz)
3830 return -1;
3831 if (__lhs_sz > __rhs_sz)
3832 return 1;
3833 return 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003834}
3835
3836template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003837inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003838int
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003839basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003840{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003841 return compare(__self_view(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003842}
3843
3844template <class _CharT, class _Traits, class _Allocator>
3845int
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003846basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3847 size_type __n1,
Howard Hinnantd17880b2013-06-28 16:59:19 +00003848 const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003849 size_type __n2) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003850{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003851 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003852 size_type __sz = size();
3853 if (__pos1 > __sz || __n2 == npos)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003854 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003855 size_type __rlen = _VSTD::min(__n1, __sz - __pos1);
3856 int __r = traits_type::compare(data() + __pos1, __s, _VSTD::min(__rlen, __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003857 if (__r == 0)
3858 {
3859 if (__rlen < __n2)
3860 __r = -1;
3861 else if (__rlen > __n2)
3862 __r = 1;
3863 }
3864 return __r;
3865}
3866
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003867template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003868template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003869__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003870<
3871 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3872 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003873>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003874basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3875 size_type __n1,
Marshall Clowe46031a2018-07-02 18:41:15 +00003876 const _Tp& __t) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003877{
Marshall Clowe46031a2018-07-02 18:41:15 +00003878 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003879 return compare(__pos1, __n1, __sv.data(), __sv.size());
3880}
3881
3882template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003883inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003884int
3885basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3886 size_type __n1,
3887 const basic_string& __str) const
3888{
3889 return compare(__pos1, __n1, __str.data(), __str.size());
3890}
3891
3892template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003893template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003894__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003895<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003896 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
3897 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00003898 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003899>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003900basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3901 size_type __n1,
Marshall Clow82513342016-09-24 22:45:42 +00003902 const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003903 size_type __pos2,
3904 size_type __n2) const
3905{
Marshall Clow82513342016-09-24 22:45:42 +00003906 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003907 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3908}
3909
3910template <class _CharT, class _Traits, class _Allocator>
3911int
3912basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3913 size_type __n1,
3914 const basic_string& __str,
3915 size_type __pos2,
3916 size_type __n2) const
3917{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003918 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003919}
3920
3921template <class _CharT, class _Traits, class _Allocator>
3922int
3923basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
3924{
3925 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3926 return compare(0, npos, __s, traits_type::length(__s));
3927}
3928
3929template <class _CharT, class _Traits, class _Allocator>
3930int
3931basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3932 size_type __n1,
3933 const value_type* __s) const
3934{
3935 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3936 return compare(__pos1, __n1, __s, traits_type::length(__s));
3937}
3938
Howard Hinnantc51e1022010-05-11 19:42:16 +00003939// __invariants
3940
3941template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003942inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003943bool
3944basic_string<_CharT, _Traits, _Allocator>::__invariants() const
3945{
3946 if (size() > capacity())
3947 return false;
3948 if (capacity() < __min_cap - 1)
3949 return false;
Bruce Mitchener170d8972020-11-24 12:53:53 -05003950 if (data() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003951 return false;
Louis Dionne663415f2020-10-05 16:16:13 -04003952 if (data()[size()] != value_type())
Howard Hinnantc51e1022010-05-11 19:42:16 +00003953 return false;
3954 return true;
3955}
3956
Vedant Kumar55e007e2018-03-08 21:15:26 +00003957// __clear_and_shrink
3958
3959template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003960inline
Louis Dionne173f29e2019-05-29 16:01:36 +00003961void
Marshall Clowe60a7182018-05-29 17:04:37 +00003962basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
Vedant Kumar55e007e2018-03-08 21:15:26 +00003963{
3964 clear();
3965 if(__is_long())
3966 {
3967 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
3968 __set_long_cap(0);
3969 __set_short_size(0);
Louis Dionne663415f2020-10-05 16:16:13 -04003970 traits_type::assign(*__get_short_pointer(), value_type());
Vedant Kumar55e007e2018-03-08 21:15:26 +00003971 }
Louis Dionne173f29e2019-05-29 16:01:36 +00003972}
Vedant Kumar55e007e2018-03-08 21:15:26 +00003973
Howard Hinnantc51e1022010-05-11 19:42:16 +00003974// operator==
3975
3976template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003977inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003978bool
3979operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003980 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003981{
Howard Hinnantaaeb1132013-04-22 23:55:13 +00003982 size_t __lhs_sz = __lhs.size();
3983 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
3984 __rhs.data(),
3985 __lhs_sz) == 0;
3986}
3987
3988template<class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003989inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaaeb1132013-04-22 23:55:13 +00003990bool
3991operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
3992 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
3993{
3994 size_t __lhs_sz = __lhs.size();
3995 if (__lhs_sz != __rhs.size())
3996 return false;
3997 const char* __lp = __lhs.data();
3998 const char* __rp = __rhs.data();
3999 if (__lhs.__is_long())
4000 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
4001 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
4002 if (*__lp != *__rp)
4003 return false;
4004 return true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004005}
4006
4007template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004008inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004009bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004010operator==(const _CharT* __lhs,
4011 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004012{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004013 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4014 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
4015 size_t __lhs_len = _Traits::length(__lhs);
4016 if (__lhs_len != __rhs.size()) return false;
4017 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004018}
4019
Howard Hinnantc51e1022010-05-11 19:42:16 +00004020template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004021inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004022bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004023operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
4024 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004025{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004026 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4027 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
4028 size_t __rhs_len = _Traits::length(__rhs);
4029 if (__rhs_len != __lhs.size()) return false;
4030 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004031}
4032
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004033template<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
4036operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004037 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004038{
4039 return !(__lhs == __rhs);
4040}
4041
4042template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004043inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004044bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004045operator!=(const _CharT* __lhs,
4046 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004047{
4048 return !(__lhs == __rhs);
4049}
4050
4051template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004052inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004053bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004054operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4055 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004056{
4057 return !(__lhs == __rhs);
4058}
4059
4060// operator<
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
4065operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004066 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004067{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004068 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004069}
4070
4071template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004072inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004073bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004074operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4075 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004076{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004077 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004078}
4079
4080template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004081inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004082bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004083operator< (const _CharT* __lhs,
4084 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004085{
4086 return __rhs.compare(__lhs) > 0;
4087}
4088
Howard Hinnantc51e1022010-05-11 19:42:16 +00004089// operator>
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
4094operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004095 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004096{
4097 return __rhs < __lhs;
4098}
4099
4100template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004101inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004102bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004103operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4104 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004105{
4106 return __rhs < __lhs;
4107}
4108
4109template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004110inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004111bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004112operator> (const _CharT* __lhs,
4113 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004114{
4115 return __rhs < __lhs;
4116}
4117
4118// operator<=
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
4123operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004124 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004125{
4126 return !(__rhs < __lhs);
4127}
4128
4129template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004130inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004131bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004132operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4133 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004134{
4135 return !(__rhs < __lhs);
4136}
4137
4138template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004139inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004140bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004141operator<=(const _CharT* __lhs,
4142 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004143{
4144 return !(__rhs < __lhs);
4145}
4146
4147// operator>=
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
4152operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004153 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004154{
4155 return !(__lhs < __rhs);
4156}
4157
4158template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004159inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004160bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004161operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4162 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004163{
4164 return !(__lhs < __rhs);
4165}
4166
4167template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004168inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004169bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004170operator>=(const _CharT* __lhs,
4171 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004172{
4173 return !(__lhs < __rhs);
4174}
4175
4176// operator +
4177
4178template<class _CharT, class _Traits, class _Allocator>
4179basic_string<_CharT, _Traits, _Allocator>
4180operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4181 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4182{
4183 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4184 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4185 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4186 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4187 __r.append(__rhs.data(), __rhs_sz);
4188 return __r;
4189}
4190
4191template<class _CharT, class _Traits, class _Allocator>
4192basic_string<_CharT, _Traits, _Allocator>
4193operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4194{
4195 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4196 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = _Traits::length(__lhs);
4197 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4198 __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz);
4199 __r.append(__rhs.data(), __rhs_sz);
4200 return __r;
4201}
4202
4203template<class _CharT, class _Traits, class _Allocator>
4204basic_string<_CharT, _Traits, _Allocator>
4205operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4206{
4207 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4208 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4209 __r.__init(&__lhs, 1, 1 + __rhs_sz);
4210 __r.append(__rhs.data(), __rhs_sz);
4211 return __r;
4212}
4213
4214template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004215inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004216basic_string<_CharT, _Traits, _Allocator>
4217operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
4218{
4219 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4220 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4221 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = _Traits::length(__rhs);
4222 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4223 __r.append(__rhs, __rhs_sz);
4224 return __r;
4225}
4226
4227template<class _CharT, class _Traits, class _Allocator>
4228basic_string<_CharT, _Traits, _Allocator>
4229operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
4230{
4231 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4232 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4233 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1);
4234 __r.push_back(__rhs);
4235 return __r;
4236}
4237
Eric Fiselierfc92be82017-04-19 00:28:44 +00004238#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004239
4240template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004241inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004242basic_string<_CharT, _Traits, _Allocator>
4243operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4244{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004245 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004246}
4247
4248template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004249inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004250basic_string<_CharT, _Traits, _Allocator>
4251operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4252{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004253 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004254}
4255
4256template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004257inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004258basic_string<_CharT, _Traits, _Allocator>
4259operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4260{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004261 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004262}
4263
4264template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004265inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004266basic_string<_CharT, _Traits, _Allocator>
4267operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4268{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004269 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004270}
4271
4272template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004273inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004274basic_string<_CharT, _Traits, _Allocator>
4275operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4276{
4277 __rhs.insert(__rhs.begin(), __lhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004278 return _VSTD::move(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004279}
4280
4281template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004282inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004283basic_string<_CharT, _Traits, _Allocator>
4284operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
4285{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004286 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004287}
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 +00004291basic_string<_CharT, _Traits, _Allocator>
4292operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
4293{
4294 __lhs.push_back(__rhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004295 return _VSTD::move(__lhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004296}
4297
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004298#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004299
4300// swap
4301
4302template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004303inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004304void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004305swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +00004306 basic_string<_CharT, _Traits, _Allocator>& __rhs)
4307 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004308{
4309 __lhs.swap(__rhs);
4310}
4311
Bruce Mitchener170d8972020-11-24 12:53:53 -05004312_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = nullptr, int __base = 10);
4313_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = nullptr, int __base = 10);
4314_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = nullptr, int __base = 10);
4315_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = nullptr, int __base = 10);
4316_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004317
Bruce Mitchener170d8972020-11-24 12:53:53 -05004318_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = nullptr);
4319_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = nullptr);
4320_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004321
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004322_LIBCPP_FUNC_VIS string to_string(int __val);
4323_LIBCPP_FUNC_VIS string to_string(unsigned __val);
4324_LIBCPP_FUNC_VIS string to_string(long __val);
4325_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
4326_LIBCPP_FUNC_VIS string to_string(long long __val);
4327_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
4328_LIBCPP_FUNC_VIS string to_string(float __val);
4329_LIBCPP_FUNC_VIS string to_string(double __val);
4330_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004331
Louis Dionne89258142021-08-23 15:32:36 -04004332#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Bruce Mitchener170d8972020-11-24 12:53:53 -05004333_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4334_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4335_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4336_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4337_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004338
Bruce Mitchener170d8972020-11-24 12:53:53 -05004339_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = nullptr);
4340_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = nullptr);
4341_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004342
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004343_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
4344_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
4345_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
4346_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
4347_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
4348_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
4349_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
4350_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
4351_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Louis Dionne89258142021-08-23 15:32:36 -04004352#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004353
Howard Hinnantc51e1022010-05-11 19:42:16 +00004354template<class _CharT, class _Traits, class _Allocator>
Martin Storsjö88890c22021-02-22 01:13:13 +02004355_LIBCPP_TEMPLATE_DATA_VIS
Eric Fiselierc9fdaf12020-01-15 17:02:17 -05004356const typename basic_string<_CharT, _Traits, _Allocator>::size_type
4357 basic_string<_CharT, _Traits, _Allocator>::npos;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004358
Marshall Clow851b9ec2019-05-20 21:56:51 +00004359template <class _CharT, class _Allocator>
4360struct _LIBCPP_TEMPLATE_VIS
4361 hash<basic_string<_CharT, char_traits<_CharT>, _Allocator> >
4362 : public unary_function<
4363 basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004364{
4365 size_t
Marshall Clow851b9ec2019-05-20 21:56:51 +00004366 operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT
4367 { return __do_string_hash(__val.data(), __val.data() + __val.size()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004368};
4369
Howard Hinnantc51e1022010-05-11 19:42:16 +00004370
Howard Hinnantdc095972011-07-18 15:51:59 +00004371template<class _CharT, class _Traits, class _Allocator>
4372basic_ostream<_CharT, _Traits>&
4373operator<<(basic_ostream<_CharT, _Traits>& __os,
4374 const basic_string<_CharT, _Traits, _Allocator>& __str);
4375
4376template<class _CharT, class _Traits, class _Allocator>
4377basic_istream<_CharT, _Traits>&
4378operator>>(basic_istream<_CharT, _Traits>& __is,
4379 basic_string<_CharT, _Traits, _Allocator>& __str);
4380
4381template<class _CharT, class _Traits, class _Allocator>
4382basic_istream<_CharT, _Traits>&
4383getline(basic_istream<_CharT, _Traits>& __is,
4384 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4385
4386template<class _CharT, class _Traits, class _Allocator>
4387inline _LIBCPP_INLINE_VISIBILITY
4388basic_istream<_CharT, _Traits>&
4389getline(basic_istream<_CharT, _Traits>& __is,
4390 basic_string<_CharT, _Traits, _Allocator>& __str);
4391
Howard Hinnantdc095972011-07-18 15:51:59 +00004392template<class _CharT, class _Traits, class _Allocator>
4393inline _LIBCPP_INLINE_VISIBILITY
4394basic_istream<_CharT, _Traits>&
4395getline(basic_istream<_CharT, _Traits>&& __is,
4396 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4397
4398template<class _CharT, class _Traits, class _Allocator>
4399inline _LIBCPP_INLINE_VISIBILITY
4400basic_istream<_CharT, _Traits>&
4401getline(basic_istream<_CharT, _Traits>&& __is,
4402 basic_string<_CharT, _Traits, _Allocator>& __str);
4403
Marshall Clow29b53f22018-12-14 18:49:35 +00004404#if _LIBCPP_STD_VER > 17
Marek Kurdeja98b1412020-05-02 13:58:03 +02004405template <class _CharT, class _Traits, class _Allocator, class _Up>
Marshall Clow29b53f22018-12-14 18:49:35 +00004406inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004407 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4408 erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
4409 auto __old_size = __str.size();
4410 __str.erase(_VSTD::remove(__str.begin(), __str.end(), __v), __str.end());
4411 return __old_size - __str.size();
4412}
Marshall Clow29b53f22018-12-14 18:49:35 +00004413
Marek Kurdeja98b1412020-05-02 13:58:03 +02004414template <class _CharT, class _Traits, class _Allocator, class _Predicate>
Marshall Clow29b53f22018-12-14 18:49:35 +00004415inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004416 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4417 erase_if(basic_string<_CharT, _Traits, _Allocator>& __str,
4418 _Predicate __pred) {
4419 auto __old_size = __str.size();
4420 __str.erase(_VSTD::remove_if(__str.begin(), __str.end(), __pred),
4421 __str.end());
4422 return __old_size - __str.size();
4423}
Marshall Clow29b53f22018-12-14 18:49:35 +00004424#endif
4425
Louis Dionneba400782020-10-02 15:02:52 -04004426#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004427
4428template<class _CharT, class _Traits, class _Allocator>
4429bool
4430basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4431{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004432 return data() <= _VSTD::__to_address(__i->base()) &&
4433 _VSTD::__to_address(__i->base()) < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004434}
4435
4436template<class _CharT, class _Traits, class _Allocator>
4437bool
4438basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4439{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004440 return data() < _VSTD::__to_address(__i->base()) &&
4441 _VSTD::__to_address(__i->base()) <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004442}
4443
4444template<class _CharT, class _Traits, class _Allocator>
4445bool
4446basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4447{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004448 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004449 return data() <= __p && __p <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004450}
4451
4452template<class _CharT, class _Traits, class _Allocator>
4453bool
4454basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4455{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004456 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004457 return data() <= __p && __p < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004458}
4459
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004460#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004461
Louis Dionne173f29e2019-05-29 16:01:36 +00004462#if _LIBCPP_STD_VER > 11
Marshall Clowcba751f2013-07-23 17:05:24 +00004463// Literal suffixes for basic_string [basic.string.literals]
Marshall Clowac868372013-10-05 21:18:32 +00004464inline namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004465{
4466 inline namespace string_literals
4467 {
Howard Hinnant5c167562013-08-07 19:39:48 +00004468 inline _LIBCPP_INLINE_VISIBILITY
4469 basic_string<char> operator "" s( const char *__str, size_t __len )
4470 {
4471 return basic_string<char> (__str, __len);
4472 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004473
Louis Dionne89258142021-08-23 15:32:36 -04004474#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnant5c167562013-08-07 19:39:48 +00004475 inline _LIBCPP_INLINE_VISIBILITY
4476 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4477 {
4478 return basic_string<wchar_t> (__str, __len);
4479 }
Louis Dionne89258142021-08-23 15:32:36 -04004480#endif
Marshall Clowcba751f2013-07-23 17:05:24 +00004481
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -04004482#ifndef _LIBCPP_HAS_NO_CHAR8_T
Marshall Clow8732fed2018-12-11 04:35:44 +00004483 inline _LIBCPP_INLINE_VISIBILITY
4484 basic_string<char8_t> operator "" s(const char8_t *__str, size_t __len) _NOEXCEPT
4485 {
4486 return basic_string<char8_t> (__str, __len);
4487 }
4488#endif
4489
Howard Hinnant5c167562013-08-07 19:39:48 +00004490 inline _LIBCPP_INLINE_VISIBILITY
4491 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4492 {
4493 return basic_string<char16_t> (__str, __len);
4494 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004495
Howard Hinnant5c167562013-08-07 19:39:48 +00004496 inline _LIBCPP_INLINE_VISIBILITY
4497 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4498 {
4499 return basic_string<char32_t> (__str, __len);
4500 }
Nikolas Klauserd26407a2021-12-02 14:12:51 +01004501 } // namespace string_literals
4502} // namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004503#endif
4504
Howard Hinnantc51e1022010-05-11 19:42:16 +00004505_LIBCPP_END_NAMESPACE_STD
4506
Eric Fiselierf4433a32017-05-31 22:07:49 +00004507_LIBCPP_POP_MACROS
4508
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004509#endif // _LIBCPP_STRING