blob: c79c0ed72abd860c8307f02d9aa2f9a684c58ca1 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
Louis Dionne9bd93882021-11-17 16:25:01 -05002//===----------------------------------------------------------------------===//
Howard Hinnantc51e1022010-05-11 19:42:16 +00003//
Chandler Carruth7642bb12019-01-19 08:50:56 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_STRING
11#define _LIBCPP_STRING
12
13/*
14 string synopsis
15
16namespace std
17{
18
19template <class stateT>
20class fpos
21{
22private:
23 stateT st;
24public:
25 fpos(streamoff = streamoff());
26
27 operator streamoff() const;
28
29 stateT state() const;
30 void state(stateT);
31
32 fpos& operator+=(streamoff);
33 fpos operator+ (streamoff) const;
34 fpos& operator-=(streamoff);
35 fpos operator- (streamoff) const;
36};
37
38template <class stateT> streamoff operator-(const fpos<stateT>& x, const fpos<stateT>& y);
39
40template <class stateT> bool operator==(const fpos<stateT>& x, const fpos<stateT>& y);
41template <class stateT> bool operator!=(const fpos<stateT>& x, const fpos<stateT>& y);
42
43template <class charT>
44struct char_traits
45{
46 typedef charT char_type;
47 typedef ... int_type;
48 typedef streamoff off_type;
49 typedef streampos pos_type;
50 typedef mbstate_t state_type;
51
Howard Hinnantaeb17d82011-05-29 19:57:12 +000052 static void assign(char_type& c1, const char_type& c2) noexcept;
Howard Hinnant270c00e2012-07-20 19:09:12 +000053 static constexpr bool eq(char_type c1, char_type c2) noexcept;
54 static constexpr bool lt(char_type c1, char_type c2) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000055
56 static int compare(const char_type* s1, const char_type* s2, size_t n);
57 static size_t length(const char_type* s);
58 static const char_type* find(const char_type* s, size_t n, const char_type& a);
59 static char_type* move(char_type* s1, const char_type* s2, size_t n);
60 static char_type* copy(char_type* s1, const char_type* s2, size_t n);
61 static char_type* assign(char_type* s, size_t n, char_type a);
62
Howard Hinnant270c00e2012-07-20 19:09:12 +000063 static constexpr int_type not_eof(int_type c) noexcept;
64 static constexpr char_type to_char_type(int_type c) noexcept;
65 static constexpr int_type to_int_type(char_type c) noexcept;
66 static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept;
67 static constexpr int_type eof() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000068};
69
70template <> struct char_traits<char>;
71template <> struct char_traits<wchar_t>;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +010072template <> struct char_traits<char8_t>; // C++20
73template <> struct char_traits<char16_t>;
74template <> struct char_traits<char32_t>;
Howard Hinnantc51e1022010-05-11 19:42:16 +000075
Howard Hinnant3b6579a2010-08-22 00:02:43 +000076template<class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
Howard Hinnantc51e1022010-05-11 19:42:16 +000077class basic_string
78{
Howard Hinnant3b6579a2010-08-22 00:02:43 +000079public:
80// types:
Howard Hinnantc51e1022010-05-11 19:42:16 +000081 typedef traits traits_type;
82 typedef typename traits_type::char_type value_type;
83 typedef Allocator allocator_type;
84 typedef typename allocator_type::size_type size_type;
85 typedef typename allocator_type::difference_type difference_type;
86 typedef typename allocator_type::reference reference;
87 typedef typename allocator_type::const_reference const_reference;
88 typedef typename allocator_type::pointer pointer;
89 typedef typename allocator_type::const_pointer const_pointer;
90 typedef implementation-defined iterator;
91 typedef implementation-defined const_iterator;
92 typedef std::reverse_iterator<iterator> reverse_iterator;
93 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
94
95 static const size_type npos = -1;
96
Howard Hinnant3e276872011-06-03 18:40:47 +000097 basic_string()
98 noexcept(is_nothrow_default_constructible<allocator_type>::value);
99 explicit basic_string(const allocator_type& a);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000100 basic_string(const basic_string& str);
Howard Hinnant3e276872011-06-03 18:40:47 +0000101 basic_string(basic_string&& str)
102 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clow83445802016-04-07 18:13:41 +0000103 basic_string(const basic_string& str, size_type pos,
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000104 const allocator_type& a = allocator_type());
Marshall Clow83445802016-04-07 18:13:41 +0000105 basic_string(const basic_string& str, size_type pos, size_type n,
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000106 const Allocator& a = Allocator());
Marshall Clow78dbe462016-11-14 18:22:19 +0000107 template<class T>
108 basic_string(const T& t, size_type pos, size_type n, const Allocator& a = Allocator()); // C++17
Marshall Clowe46031a2018-07-02 18:41:15 +0000109 template <class T>
110 explicit basic_string(const T& t, const Allocator& a = Allocator()); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000111 basic_string(const value_type* s, const allocator_type& a = allocator_type());
112 basic_string(const value_type* s, size_type n, const allocator_type& a = allocator_type());
Marek Kurdej90d79712021-07-27 16:16:21 +0200113 basic_string(nullptr_t) = delete; // C++2b
Howard Hinnantc51e1022010-05-11 19:42:16 +0000114 basic_string(size_type n, value_type c, const allocator_type& a = allocator_type());
115 template<class InputIterator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000116 basic_string(InputIterator begin, InputIterator end,
117 const allocator_type& a = allocator_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000118 basic_string(initializer_list<value_type>, const Allocator& = Allocator());
119 basic_string(const basic_string&, const Allocator&);
120 basic_string(basic_string&&, const Allocator&);
121
122 ~basic_string();
123
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000124 operator basic_string_view<charT, traits>() const noexcept;
125
Howard Hinnantc51e1022010-05-11 19:42:16 +0000126 basic_string& operator=(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000127 template <class T>
128 basic_string& operator=(const T& t); // C++17
Howard Hinnant3e276872011-06-03 18:40:47 +0000129 basic_string& operator=(basic_string&& str)
130 noexcept(
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000131 allocator_type::propagate_on_container_move_assignment::value ||
132 allocator_type::is_always_equal::value ); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000133 basic_string& operator=(const value_type* s);
Marek Kurdej90d79712021-07-27 16:16:21 +0200134 basic_string& operator=(nullptr_t) = delete; // C++2b
Howard Hinnantc51e1022010-05-11 19:42:16 +0000135 basic_string& operator=(value_type c);
136 basic_string& operator=(initializer_list<value_type>);
137
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000138 iterator begin() noexcept;
139 const_iterator begin() const noexcept;
140 iterator end() noexcept;
141 const_iterator end() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000142
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000143 reverse_iterator rbegin() noexcept;
144 const_reverse_iterator rbegin() const noexcept;
145 reverse_iterator rend() noexcept;
146 const_reverse_iterator rend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000147
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000148 const_iterator cbegin() const noexcept;
149 const_iterator cend() const noexcept;
150 const_reverse_iterator crbegin() const noexcept;
151 const_reverse_iterator crend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000152
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000153 size_type size() const noexcept;
154 size_type length() const noexcept;
155 size_type max_size() const noexcept;
156 size_type capacity() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000157
158 void resize(size_type n, value_type c);
159 void resize(size_type n);
160
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100161 template<class Operation>
162 constexpr void resize_and_overwrite(size_type n, Operation op); // since C++23
163
Marek Kurdejc9848142020-11-26 10:07:16 +0100164 void reserve(size_type res_arg);
165 void reserve(); // deprecated in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000166 void shrink_to_fit();
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000167 void clear() noexcept;
168 bool empty() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000169
170 const_reference operator[](size_type pos) const;
171 reference operator[](size_type pos);
172
173 const_reference at(size_type n) const;
174 reference at(size_type n);
175
176 basic_string& operator+=(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000177 template <class T>
178 basic_string& operator+=(const T& t); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000179 basic_string& operator+=(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000180 basic_string& operator+=(value_type c);
181 basic_string& operator+=(initializer_list<value_type>);
182
183 basic_string& append(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000184 template <class T>
185 basic_string& append(const T& t); // C++17
Marshall Clow8db7fb02014-03-04 19:17:19 +0000186 basic_string& append(const basic_string& str, size_type pos, size_type n=npos); //C++14
Marshall Clow82513342016-09-24 22:45:42 +0000187 template <class T>
188 basic_string& append(const T& t, size_type pos, size_type n=npos); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000189 basic_string& append(const value_type* s, size_type n);
190 basic_string& append(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000191 basic_string& append(size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000192 template<class InputIterator>
193 basic_string& append(InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000194 basic_string& append(initializer_list<value_type>);
195
196 void push_back(value_type c);
197 void pop_back();
198 reference front();
199 const_reference front() const;
200 reference back();
201 const_reference back() const;
202
203 basic_string& assign(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000204 template <class T>
205 basic_string& assign(const T& t); // C++17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000206 basic_string& assign(basic_string&& str);
Marshall Clow8db7fb02014-03-04 19:17:19 +0000207 basic_string& assign(const basic_string& str, size_type pos, size_type n=npos); // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000208 template <class T>
209 basic_string& assign(const T& t, size_type pos, size_type n=npos); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000210 basic_string& assign(const value_type* s, size_type n);
211 basic_string& assign(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000212 basic_string& assign(size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000213 template<class InputIterator>
214 basic_string& assign(InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000215 basic_string& assign(initializer_list<value_type>);
216
217 basic_string& insert(size_type pos1, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000218 template <class T>
219 basic_string& insert(size_type pos1, const T& t);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000220 basic_string& insert(size_type pos1, const basic_string& str,
221 size_type pos2, size_type n);
Marshall Clow82513342016-09-24 22:45:42 +0000222 template <class T>
223 basic_string& insert(size_type pos1, const T& t, size_type pos2, size_type n); // C++17
Marshall Clow8db7fb02014-03-04 19:17:19 +0000224 basic_string& insert(size_type pos, const value_type* s, size_type n=npos); //C++14
Howard Hinnantd17880b2013-06-28 16:59:19 +0000225 basic_string& insert(size_type pos, const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000226 basic_string& insert(size_type pos, size_type n, value_type c);
227 iterator insert(const_iterator p, value_type c);
228 iterator insert(const_iterator p, size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000229 template<class InputIterator>
230 iterator insert(const_iterator p, InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000231 iterator insert(const_iterator p, initializer_list<value_type>);
232
233 basic_string& erase(size_type pos = 0, size_type n = npos);
234 iterator erase(const_iterator position);
235 iterator erase(const_iterator first, const_iterator last);
236
237 basic_string& replace(size_type pos1, size_type n1, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000238 template <class T>
239 basic_string& replace(size_type pos1, size_type n1, const T& t); // C++17
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000240 basic_string& replace(size_type pos1, size_type n1, const basic_string& str,
Marshall Clow8db7fb02014-03-04 19:17:19 +0000241 size_type pos2, size_type n2=npos); // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000242 template <class T>
243 basic_string& replace(size_type pos1, size_type n1, const T& t,
244 size_type pos2, size_type n); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000245 basic_string& replace(size_type pos, size_type n1, const value_type* s, size_type n2);
246 basic_string& replace(size_type pos, size_type n1, const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000247 basic_string& replace(size_type pos, size_type n1, size_type n2, value_type c);
Howard Hinnant990d6e82010-11-17 21:11:40 +0000248 basic_string& replace(const_iterator i1, const_iterator i2, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000249 template <class T>
250 basic_string& replace(const_iterator i1, const_iterator i2, const T& t); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000251 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s, size_type n);
252 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s);
Howard Hinnant990d6e82010-11-17 21:11:40 +0000253 basic_string& replace(const_iterator i1, const_iterator i2, size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000254 template<class InputIterator>
Howard Hinnant990d6e82010-11-17 21:11:40 +0000255 basic_string& replace(const_iterator i1, const_iterator i2, InputIterator j1, InputIterator j2);
256 basic_string& replace(const_iterator i1, const_iterator i2, initializer_list<value_type>);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000257
Howard Hinnantd17880b2013-06-28 16:59:19 +0000258 size_type copy(value_type* s, size_type n, size_type pos = 0) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000259 basic_string substr(size_type pos = 0, size_type n = npos) const;
260
Howard Hinnant3e276872011-06-03 18:40:47 +0000261 void swap(basic_string& str)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000262 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
263 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000264
Howard Hinnantd17880b2013-06-28 16:59:19 +0000265 const value_type* c_str() const noexcept;
266 const value_type* data() const noexcept;
Marshall Clowd3c22392016-03-08 15:44:30 +0000267 value_type* data() noexcept; // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000268
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000269 allocator_type get_allocator() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000270
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000271 size_type find(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000272 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800273 size_type find(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000274 size_type find(const value_type* s, size_type pos, size_type n) const noexcept;
275 size_type find(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000276 size_type find(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000277
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000278 size_type rfind(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000279 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800280 size_type rfind(const T& t, size_type pos = npos) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000281 size_type rfind(const value_type* s, size_type pos, size_type n) const noexcept;
282 size_type rfind(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000283 size_type rfind(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000284
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000285 size_type find_first_of(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000286 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800287 size_type find_first_of(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000288 size_type find_first_of(const value_type* s, size_type pos, size_type n) const noexcept;
289 size_type find_first_of(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000290 size_type find_first_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000291
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000292 size_type find_last_of(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000293 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800294 size_type find_last_of(const T& t, size_type pos = npos) const noexcept noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000295 size_type find_last_of(const value_type* s, size_type pos, size_type n) const noexcept;
296 size_type find_last_of(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000297 size_type find_last_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000298
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000299 size_type find_first_not_of(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000300 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800301 size_type find_first_not_of(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000302 size_type find_first_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
303 size_type find_first_not_of(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000304 size_type find_first_not_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000305
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000306 size_type find_last_not_of(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000307 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800308 size_type find_last_not_of(const T& t, size_type pos = npos) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000309 size_type find_last_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
310 size_type find_last_not_of(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000311 size_type find_last_not_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000312
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000313 int compare(const basic_string& str) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000314 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800315 int compare(const T& t) const noexcept; // C++17, noexcept as an extension
Howard Hinnantc51e1022010-05-11 19:42:16 +0000316 int compare(size_type pos1, size_type n1, const basic_string& str) const;
Marshall Clowe46031a2018-07-02 18:41:15 +0000317 template <class T>
318 int compare(size_type pos1, size_type n1, const T& t) const; // C++17
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000319 int compare(size_type pos1, size_type n1, const basic_string& str,
Marshall Clow8db7fb02014-03-04 19:17:19 +0000320 size_type pos2, size_type n2=npos) const; // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000321 template <class T>
322 int compare(size_type pos1, size_type n1, const T& t,
323 size_type pos2, size_type n2=npos) const; // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000324 int compare(const value_type* s) const noexcept;
325 int compare(size_type pos1, size_type n1, const value_type* s) const;
326 int compare(size_type pos1, size_type n1, const value_type* s, size_type n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000327
Marek Kurdej24b4c512021-01-07 12:29:04 +0100328 bool starts_with(basic_string_view<charT, traits> sv) const noexcept; // C++20
329 bool starts_with(charT c) const noexcept; // C++20
330 bool starts_with(const charT* s) const; // C++20
331 bool ends_with(basic_string_view<charT, traits> sv) const noexcept; // C++20
332 bool ends_with(charT c) const noexcept; // C++20
333 bool ends_with(const charT* s) const; // C++20
Marshall Clow18c293b2017-12-04 20:11:38 +0000334
Wim Leflere023c3542021-01-19 14:33:30 -0500335 constexpr bool contains(basic_string_view<charT, traits> sv) const noexcept; // C++2b
336 constexpr bool contains(charT c) const noexcept; // C++2b
337 constexpr bool contains(const charT* s) const; // C++2b
338
Howard Hinnantc51e1022010-05-11 19:42:16 +0000339 bool __invariants() const;
340};
341
Marshall Clowa0563332018-02-08 06:34:03 +0000342template<class InputIterator,
343 class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
344basic_string(InputIterator, InputIterator, Allocator = Allocator())
345 -> basic_string<typename iterator_traits<InputIterator>::value_type,
346 char_traits<typename iterator_traits<InputIterator>::value_type>,
347 Allocator>; // C++17
348
Howard Hinnantc51e1022010-05-11 19:42:16 +0000349template<class charT, class traits, class Allocator>
350basic_string<charT, traits, Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000351operator+(const basic_string<charT, traits, Allocator>& lhs,
352 const basic_string<charT, traits, Allocator>& rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000353
354template<class charT, class traits, class Allocator>
355basic_string<charT, traits, Allocator>
356operator+(const charT* lhs , const basic_string<charT,traits,Allocator>&rhs);
357
358template<class charT, class traits, class Allocator>
359basic_string<charT, traits, Allocator>
360operator+(charT lhs, const basic_string<charT,traits,Allocator>& rhs);
361
362template<class charT, class traits, class Allocator>
363basic_string<charT, traits, Allocator>
364operator+(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs);
365
366template<class charT, class traits, class Allocator>
367basic_string<charT, traits, Allocator>
368operator+(const basic_string<charT, traits, Allocator>& lhs, charT rhs);
369
370template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000371bool operator==(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000372 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000373
374template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000375bool operator==(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000376
377template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000378bool operator==(const basic_string<charT,traits,Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000379
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000380template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000381bool operator!=(const basic_string<charT,traits,Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000382 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000383
384template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000385bool operator!=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000386
387template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000388bool operator!=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000389
390template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000391bool operator< (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000392 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000393
394template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000395bool operator< (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000396
397template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000398bool operator< (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000399
400template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000401bool operator> (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000402 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000403
404template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000405bool operator> (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000406
407template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000408bool operator> (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000409
410template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000411bool operator<=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000412 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000413
414template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000415bool operator<=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000416
417template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000418bool operator<=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000419
420template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000421bool operator>=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000422 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000423
424template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000425bool operator>=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000426
427template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000428bool operator>=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000429
430template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000431void swap(basic_string<charT, traits, Allocator>& lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +0000432 basic_string<charT, traits, Allocator>& rhs)
433 noexcept(noexcept(lhs.swap(rhs)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000434
435template<class charT, class traits, class Allocator>
436basic_istream<charT, traits>&
437operator>>(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
438
439template<class charT, class traits, class Allocator>
440basic_ostream<charT, traits>&
441operator<<(basic_ostream<charT, traits>& os, const basic_string<charT, traits, Allocator>& str);
442
443template<class charT, class traits, class Allocator>
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000444basic_istream<charT, traits>&
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000445getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str,
446 charT delim);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000447
448template<class charT, class traits, class Allocator>
449basic_istream<charT, traits>&
450getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
451
Marshall Clow29b53f22018-12-14 18:49:35 +0000452template<class charT, class traits, class Allocator, class U>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200453typename basic_string<charT, traits, Allocator>::size_type
454erase(basic_string<charT, traits, Allocator>& c, const U& value); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000455template<class charT, class traits, class Allocator, class Predicate>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200456typename basic_string<charT, traits, Allocator>::size_type
457erase_if(basic_string<charT, traits, Allocator>& c, Predicate pred); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000458
Howard Hinnantc51e1022010-05-11 19:42:16 +0000459typedef basic_string<char> string;
460typedef basic_string<wchar_t> wstring;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100461typedef basic_string<char8_t> u8string; // C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000462typedef basic_string<char16_t> u16string;
463typedef basic_string<char32_t> u32string;
464
Bruce Mitchener170d8972020-11-24 12:53:53 -0500465int stoi (const string& str, size_t* idx = nullptr, int base = 10);
466long stol (const string& str, size_t* idx = nullptr, int base = 10);
467unsigned long stoul (const string& str, size_t* idx = nullptr, int base = 10);
468long long stoll (const string& str, size_t* idx = nullptr, int base = 10);
469unsigned long long stoull(const string& str, size_t* idx = nullptr, int base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000470
Bruce Mitchener170d8972020-11-24 12:53:53 -0500471float stof (const string& str, size_t* idx = nullptr);
472double stod (const string& str, size_t* idx = nullptr);
473long double stold(const string& str, size_t* idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000474
475string to_string(int val);
476string to_string(unsigned val);
477string to_string(long val);
478string to_string(unsigned long val);
479string to_string(long long val);
480string to_string(unsigned long long val);
481string to_string(float val);
482string to_string(double val);
483string to_string(long double val);
484
Bruce Mitchener170d8972020-11-24 12:53:53 -0500485int stoi (const wstring& str, size_t* idx = nullptr, int base = 10);
486long stol (const wstring& str, size_t* idx = nullptr, int base = 10);
487unsigned long stoul (const wstring& str, size_t* idx = nullptr, int base = 10);
488long long stoll (const wstring& str, size_t* idx = nullptr, int base = 10);
489unsigned long long stoull(const wstring& str, size_t* idx = nullptr, int base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000490
Bruce Mitchener170d8972020-11-24 12:53:53 -0500491float stof (const wstring& str, size_t* idx = nullptr);
492double stod (const wstring& str, size_t* idx = nullptr);
493long double stold(const wstring& str, size_t* idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000494
495wstring to_wstring(int val);
496wstring to_wstring(unsigned val);
497wstring to_wstring(long val);
498wstring to_wstring(unsigned long val);
499wstring to_wstring(long long val);
500wstring to_wstring(unsigned long long val);
501wstring to_wstring(float val);
502wstring to_wstring(double val);
503wstring to_wstring(long double val);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000504
505template <> struct hash<string>;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100506template <> struct hash<u8string>; // C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000507template <> struct hash<u16string>;
508template <> struct hash<u32string>;
509template <> struct hash<wstring>;
510
Marshall Clowcba751f2013-07-23 17:05:24 +0000511basic_string<char> operator "" s( const char *str, size_t len ); // C++14
512basic_string<wchar_t> operator "" s( const wchar_t *str, size_t len ); // C++14
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100513basic_string<char8_t> operator "" s( const char8_t *str, size_t len ); // C++20
Marshall Clowcba751f2013-07-23 17:05:24 +0000514basic_string<char16_t> operator "" s( const char16_t *str, size_t len ); // C++14
515basic_string<char32_t> operator "" s( const char32_t *str, size_t len ); // C++14
516
Howard Hinnantc51e1022010-05-11 19:42:16 +0000517} // std
518
519*/
520
Nikolas Klauserf210d8a2022-02-15 18:18:08 +0100521#include <__algorithm/max.h>
522#include <__algorithm/min.h>
523#include <__algorithm/remove.h>
524#include <__algorithm/remove_if.h>
Louis Dionne9bdbeb32022-02-14 13:41:09 -0500525#include <__assert>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000526#include <__config>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400527#include <__debug>
Nikolas Klauser80840272022-02-04 13:04:33 +0100528#include <__ios/fpos.h>
Louis Dionne77249522021-06-11 09:55:11 -0400529#include <__iterator/wrap_iter.h>
Nikolas Klauser8fccd622022-03-05 19:17:07 +0100530#include <__utility/auto_cast.h>
531#include <__utility/move.h>
532#include <__utility/swap.h>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400533#include <compare>
534#include <cstdio> // EOF
Louis Dionned24191c2021-08-19 12:39:16 -0400535#include <cstdlib>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400536#include <cstring>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400537#include <initializer_list>
538#include <iosfwd>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000539#include <iterator>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000540#include <memory>
541#include <stdexcept>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400542#include <string_view>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000543#include <type_traits>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000544#include <version>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000545
Nikolas Klauser4c1bf592022-02-11 19:15:18 +0100546// TODO: remove these headers
547#include <__functional/binary_function.h>
548#include <__functional/invoke.h>
549#include <__functional/operations.h>
550#include <__functional/reference_wrapper.h>
551#include <__functional/unary_function.h>
552#include <__functional/weak_result_type.h>
553#include <new>
554#include <typeinfo>
555
Louis Dionne89258142021-08-23 15:32:36 -0400556#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Nikolas Klauser80840272022-02-04 13:04:33 +0100557# include <cwchar>
Louis Dionne89258142021-08-23 15:32:36 -0400558#endif
559
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400560#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
Nikolas Klauser80840272022-02-04 13:04:33 +0100561# include <cstdint>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400562#endif
Eric Fiselier14b6de92014-08-10 23:53:08 +0000563
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000564#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Arthur O'Dwyer6eeaa002022-02-01 20:16:40 -0500565# pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000566#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000567
Eric Fiselierf4433a32017-05-31 22:07:49 +0000568_LIBCPP_PUSH_MACROS
569#include <__undef_macros>
570
571
Howard Hinnantc51e1022010-05-11 19:42:16 +0000572_LIBCPP_BEGIN_NAMESPACE_STD
573
Howard Hinnantc51e1022010-05-11 19:42:16 +0000574// basic_string
575
576template<class _CharT, class _Traits, class _Allocator>
577basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000578operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
579 const basic_string<_CharT, _Traits, _Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000580
581template<class _CharT, class _Traits, class _Allocator>
582basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000583operator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000584
585template<class _CharT, class _Traits, class _Allocator>
586basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000587operator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000588
589template<class _CharT, class _Traits, class _Allocator>
Louis Dionne8f8d95d2018-11-21 17:31:55 +0000590inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000591basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000592operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000593
594template<class _CharT, class _Traits, class _Allocator>
595basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000596operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000597
Shoaib Meenaiea363712017-07-29 02:54:41 +0000598_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&))
599
Marshall Clow039b2f02016-01-13 21:54:34 +0000600template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400601struct __string_is_trivial_iterator : public false_type {};
602
603template <class _Tp>
604struct __string_is_trivial_iterator<_Tp*>
605 : public is_arithmetic<_Tp> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000606
Louis Dionne173f29e2019-05-29 16:01:36 +0000607template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400608struct __string_is_trivial_iterator<__wrap_iter<_Iter> >
609 : public __string_is_trivial_iterator<_Iter> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000610
Marshall Clow82513342016-09-24 22:45:42 +0000611template <class _CharT, class _Traits, class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500612struct __can_be_converted_to_string_view : public _BoolConstant<
613 is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
614 !is_convertible<const _Tp&, const _CharT*>::value
615 > {};
Marshall Clow82513342016-09-24 22:45:42 +0000616
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000617#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000618
619template <class _CharT, size_t = sizeof(_CharT)>
620struct __padding
621{
622 unsigned char __xx[sizeof(_CharT)-1];
623};
624
625template <class _CharT>
626struct __padding<_CharT, 1>
627{
628};
629
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400630#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000631
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400632#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800633typedef basic_string<char8_t> u8string;
634#endif
635
636#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
637typedef basic_string<char16_t> u16string;
638typedef basic_string<char32_t> u32string;
Louis Dionne96fc5f52021-09-09 11:25:10 -0400639#endif
Richard Smith256954d2020-11-11 17:12:18 -0800640
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000641template<class _CharT, class _Traits, class _Allocator>
Richard Smith256954d2020-11-11 17:12:18 -0800642class
643 _LIBCPP_TEMPLATE_VIS
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400644#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800645 _LIBCPP_PREFERRED_NAME(u8string)
646#endif
647#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
648 _LIBCPP_PREFERRED_NAME(u16string)
649 _LIBCPP_PREFERRED_NAME(u32string)
650#endif
651 basic_string
Howard Hinnantc51e1022010-05-11 19:42:16 +0000652{
653public:
654 typedef basic_string __self;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000655 typedef basic_string_view<_CharT, _Traits> __self_view;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000656 typedef _Traits traits_type;
Marshall Clowa3a74e02017-03-15 18:41:11 +0000657 typedef _CharT value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000658 typedef _Allocator allocator_type;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000659 typedef allocator_traits<allocator_type> __alloc_traits;
660 typedef typename __alloc_traits::size_type size_type;
661 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant3e276872011-06-03 18:40:47 +0000662 typedef value_type& reference;
663 typedef const value_type& const_reference;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000664 typedef typename __alloc_traits::pointer pointer;
665 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000666
Marshall Clow79f33542018-03-21 00:36:05 +0000667 static_assert((!is_array<value_type>::value), "Character type of basic_string must not be an array");
668 static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string must be standard-layout");
669 static_assert(( is_trivial<value_type>::value), "Character type of basic_string must be trivial");
670 static_assert(( is_same<_CharT, typename traits_type::char_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000671 "traits_type::char_type must be the same type as CharT");
Marshall Clow79f33542018-03-21 00:36:05 +0000672 static_assert(( is_same<typename allocator_type::value_type, value_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000673 "Allocator::value_type must be same type as value_type");
Marshall Clowe46031a2018-07-02 18:41:15 +0000674
Howard Hinnantc51e1022010-05-11 19:42:16 +0000675 typedef __wrap_iter<pointer> iterator;
676 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000677 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
678 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000679
680private:
Howard Hinnant68bf1812013-04-30 21:44:48 +0000681
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000682#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000683
684 struct __long
685 {
686 pointer __data_;
687 size_type __size_;
688 size_type __cap_;
689 };
690
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000691#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000692 static const size_type __short_mask = 0x01;
693 static const size_type __long_mask = 0x1ul;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000694#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000695 static const size_type __short_mask = 0x80;
696 static const size_type __long_mask = ~(size_type(~0) >> 1);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400697#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +0000698
699 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
700 (sizeof(__long) - 1)/sizeof(value_type) : 2};
701
702 struct __short
703 {
704 value_type __data_[__min_cap];
705 struct
706 : __padding<value_type>
707 {
708 unsigned char __size_;
709 };
710 };
711
712#else
713
Howard Hinnantc51e1022010-05-11 19:42:16 +0000714 struct __long
715 {
716 size_type __cap_;
717 size_type __size_;
718 pointer __data_;
719 };
720
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000721#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000722 static const size_type __short_mask = 0x80;
723 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000724#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000725 static const size_type __short_mask = 0x01;
726 static const size_type __long_mask = 0x1ul;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400727#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +0000728
Howard Hinnantc51e1022010-05-11 19:42:16 +0000729 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
730 (sizeof(__long) - 1)/sizeof(value_type) : 2};
731
732 struct __short
733 {
734 union
735 {
736 unsigned char __size_;
Howard Hinnant49e145e2012-10-30 19:06:59 +0000737 value_type __lx;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000738 };
739 value_type __data_[__min_cap];
740 };
741
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400742#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000743
Howard Hinnant8ea98242013-08-23 17:37:05 +0000744 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000745
Howard Hinnant8ea98242013-08-23 17:37:05 +0000746 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000747
748 struct __raw
749 {
750 size_type __words[__n_words];
751 };
752
753 struct __rep
754 {
755 union
756 {
757 __long __l;
758 __short __s;
759 __raw __r;
760 };
761 };
762
763 __compressed_pair<__rep, allocator_type> __r_;
764
Howard Hinnantc51e1022010-05-11 19:42:16 +0000765public:
Martin Storsjö88890c22021-02-22 01:13:13 +0200766 _LIBCPP_TEMPLATE_DATA_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000767 static const size_type npos = -1;
768
Howard Hinnant3e276872011-06-03 18:40:47 +0000769 _LIBCPP_INLINE_VISIBILITY basic_string()
770 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000771
772 _LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a)
773#if _LIBCPP_STD_VER <= 14
774 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
775#else
776 _NOEXCEPT;
777#endif
778
Howard Hinnantc51e1022010-05-11 19:42:16 +0000779 basic_string(const basic_string& __str);
780 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clowa80abc72015-06-03 19:56:43 +0000781
Eric Fiselierfc92be82017-04-19 00:28:44 +0000782#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000783 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000784 basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +0000785#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +0000786 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000787#else
788 _NOEXCEPT;
789#endif
790
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000791 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000792 basic_string(basic_string&& __str, const allocator_type& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400793#endif // _LIBCPP_CXX03_LANG
Marshall Clowe46031a2018-07-02 18:41:15 +0000794
Louis Dionne9ce598d2021-09-08 09:14:43 -0400795 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Eric Fiseliera8567862018-07-17 05:48:48 +0000796 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5169d1c2019-12-16 19:03:23 -0500797 basic_string(const _CharT* __s) : __r_(__default_init_tag(), __default_init_tag()) {
Eric Fiseliera8567862018-07-17 05:48:48 +0000798 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
799 __init(__s, traits_type::length(__s));
Nikolas Klauserf2807732022-01-11 00:33:35 +0100800 _VSTD::__debug_db_insert_c(this);
Eric Fiseliera8567862018-07-17 05:48:48 +0000801 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000802
Louis Dionne9ce598d2021-09-08 09:14:43 -0400803 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000804 _LIBCPP_INLINE_VISIBILITY
805 basic_string(const _CharT* __s, const _Allocator& __a);
806
Marek Kurdej90d79712021-07-27 16:16:21 +0200807#if _LIBCPP_STD_VER > 20
808 basic_string(nullptr_t) = delete;
809#endif
810
Howard Hinnantcf823322010-12-17 14:46:43 +0000811 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000812 basic_string(const _CharT* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +0000813 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000814 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000815 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000816 basic_string(size_type __n, _CharT __c);
Marshall Clowe46031a2018-07-02 18:41:15 +0000817
Louis Dionne9ce598d2021-09-08 09:14:43 -0400818 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000819 _LIBCPP_INLINE_VISIBILITY
820 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
821
Marshall Clow83445802016-04-07 18:13:41 +0000822 basic_string(const basic_string& __str, size_type __pos, size_type __n,
Eric Fiselier812882b2017-02-17 01:17:10 +0000823 const _Allocator& __a = _Allocator());
Marshall Clow83445802016-04-07 18:13:41 +0000824 _LIBCPP_INLINE_VISIBILITY
825 basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +0000826 const _Allocator& __a = _Allocator());
Marshall Clowe46031a2018-07-02 18:41:15 +0000827
Louis Dionne9ce598d2021-09-08 09:14:43 -0400828 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 +0000829 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselier812882b2017-02-17 01:17:10 +0000830 basic_string(const _Tp& __t, size_type __pos, size_type __n,
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500831 const allocator_type& __a = allocator_type());
Marshall Clowe46031a2018-07-02 18:41:15 +0000832
Louis Dionne9ce598d2021-09-08 09:14:43 -0400833 template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500834 !__is_same_uncvref<_Tp, basic_string>::value> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000835 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
836 explicit basic_string(const _Tp& __t);
837
Louis Dionne9ce598d2021-09-08 09:14:43 -0400838 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 +0000839 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
840 explicit basic_string(const _Tp& __t, const allocator_type& __a);
841
Louis Dionne9ce598d2021-09-08 09:14:43 -0400842 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000843 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000844 basic_string(_InputIterator __first, _InputIterator __last);
Louis Dionne9ce598d2021-09-08 09:14:43 -0400845 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000846 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000847 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000848#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000849 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000850 basic_string(initializer_list<_CharT> __il);
Howard Hinnantcf823322010-12-17 14:46:43 +0000851 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000852 basic_string(initializer_list<_CharT> __il, const _Allocator& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400853#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000854
Eric Fiselierd9a702a2016-10-31 03:42:50 +0000855 inline ~basic_string();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000856
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000857 _LIBCPP_INLINE_VISIBILITY
858 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
859
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000860 basic_string& operator=(const basic_string& __str);
Eric Fiselier5ec13b12017-01-23 21:24:58 +0000861
Louis Dionne9ce598d2021-09-08 09:14:43 -0400862 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 +0000863 basic_string& operator=(const _Tp& __t)
864 {__self_view __sv = __t; return assign(__sv);}
865
Eric Fiselierfc92be82017-04-19 00:28:44 +0000866#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000867 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000868 basic_string& operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000869 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiselierfc92be82017-04-19 00:28:44 +0000870 _LIBCPP_INLINE_VISIBILITY
871 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000872#endif
Howard Hinnantd17880b2013-06-28 16:59:19 +0000873 _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const value_type* __s) {return assign(__s);}
Marek Kurdej90d79712021-07-27 16:16:21 +0200874#if _LIBCPP_STD_VER > 20
875 basic_string& operator=(nullptr_t) = delete;
876#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000877 basic_string& operator=(value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000878
Louis Dionneba400782020-10-02 15:02:52 -0400879#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +0000880 _LIBCPP_INLINE_VISIBILITY
881 iterator begin() _NOEXCEPT
882 {return iterator(this, __get_pointer());}
883 _LIBCPP_INLINE_VISIBILITY
884 const_iterator begin() const _NOEXCEPT
885 {return const_iterator(this, __get_pointer());}
886 _LIBCPP_INLINE_VISIBILITY
887 iterator end() _NOEXCEPT
888 {return iterator(this, __get_pointer() + size());}
889 _LIBCPP_INLINE_VISIBILITY
890 const_iterator end() const _NOEXCEPT
891 {return const_iterator(this, __get_pointer() + size());}
892#else
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000893 _LIBCPP_INLINE_VISIBILITY
894 iterator begin() _NOEXCEPT
895 {return iterator(__get_pointer());}
896 _LIBCPP_INLINE_VISIBILITY
897 const_iterator begin() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000898 {return const_iterator(__get_pointer());}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000899 _LIBCPP_INLINE_VISIBILITY
900 iterator end() _NOEXCEPT
901 {return iterator(__get_pointer() + size());}
902 _LIBCPP_INLINE_VISIBILITY
903 const_iterator end() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000904 {return const_iterator(__get_pointer() + size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400905#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000906 _LIBCPP_INLINE_VISIBILITY
907 reverse_iterator rbegin() _NOEXCEPT
908 {return reverse_iterator(end());}
909 _LIBCPP_INLINE_VISIBILITY
910 const_reverse_iterator rbegin() const _NOEXCEPT
911 {return const_reverse_iterator(end());}
912 _LIBCPP_INLINE_VISIBILITY
913 reverse_iterator rend() _NOEXCEPT
914 {return reverse_iterator(begin());}
915 _LIBCPP_INLINE_VISIBILITY
916 const_reverse_iterator rend() const _NOEXCEPT
917 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000918
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000919 _LIBCPP_INLINE_VISIBILITY
920 const_iterator cbegin() const _NOEXCEPT
921 {return begin();}
922 _LIBCPP_INLINE_VISIBILITY
923 const_iterator cend() const _NOEXCEPT
924 {return end();}
925 _LIBCPP_INLINE_VISIBILITY
926 const_reverse_iterator crbegin() const _NOEXCEPT
927 {return rbegin();}
928 _LIBCPP_INLINE_VISIBILITY
929 const_reverse_iterator crend() const _NOEXCEPT
930 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000931
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000932 _LIBCPP_INLINE_VISIBILITY size_type size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000933 {return __is_long() ? __get_long_size() : __get_short_size();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000934 _LIBCPP_INLINE_VISIBILITY size_type length() const _NOEXCEPT {return size();}
935 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT;
936 _LIBCPP_INLINE_VISIBILITY size_type capacity() const _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +0000937 {return (__is_long() ? __get_long_cap()
938 : static_cast<size_type>(__min_cap)) - 1;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000939
940 void resize(size_type __n, value_type __c);
941 _LIBCPP_INLINE_VISIBILITY void resize(size_type __n) {resize(__n, value_type());}
942
Marek Kurdejc9848142020-11-26 10:07:16 +0100943 void reserve(size_type __requested_capacity);
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100944
945#if _LIBCPP_STD_VER > 20
946 template <class _Op>
947 _LIBCPP_HIDE_FROM_ABI constexpr
948 void resize_and_overwrite(size_type __n, _Op __op) {
949 __resize_default_init(__n);
Nikolas Klausera4a76a12022-01-20 13:53:59 +0100950 __erase_to_end(_VSTD::move(__op)(data(), _LIBCPP_AUTO_CAST(__n)));
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100951 }
952#endif
953
Eric Fiselier451d5582018-11-26 20:15:38 +0000954 _LIBCPP_INLINE_VISIBILITY void __resize_default_init(size_type __n);
955
Marek Kurdejc9848142020-11-26 10:07:16 +0100956 _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_INLINE_VISIBILITY
957 void reserve() _NOEXCEPT {shrink_to_fit();}
Marshall Clow6ae12a82018-11-28 18:18:34 +0000958 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejc9848142020-11-26 10:07:16 +0100959 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000960 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000961 void clear() _NOEXCEPT;
Marshall Clowb7db4972017-11-15 20:02:27 +0000962 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
963 bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000964
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000965 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __pos) const _NOEXCEPT;
966 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000967
968 const_reference at(size_type __n) const;
969 reference at(size_type __n);
970
971 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const basic_string& __str) {return append(__str);}
Marshall Clowe46031a2018-07-02 18:41:15 +0000972
973 template <class _Tp>
974 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -0400975 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +0000976 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500977 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
978 && !__is_same_uncvref<_Tp, basic_string >::value,
Marshall Clowe46031a2018-07-02 18:41:15 +0000979 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500980 >
Marshall Clowe46031a2018-07-02 18:41:15 +0000981 operator+=(const _Tp& __t) {__self_view __sv = __t; return append(__sv);}
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000982 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const value_type* __s) {return append(__s);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000983 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(value_type __c) {push_back(__c); return *this;}
Eric Fiselierfc92be82017-04-19 00:28:44 +0000984#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000985 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(initializer_list<value_type> __il) {return append(__il);}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400986#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000987
Howard Hinnantcf823322010-12-17 14:46:43 +0000988 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000989 basic_string& append(const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000990
991 template <class _Tp>
992 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -0400993 __enable_if_t<
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500994 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
995 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clowe46031a2018-07-02 18:41:15 +0000996 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500997 >
Marshall Clowe46031a2018-07-02 18:41:15 +0000998 append(const _Tp& __t) { __self_view __sv = __t; return append(__sv.data(), __sv.size()); }
Marshall Clow8db7fb02014-03-04 19:17:19 +0000999 basic_string& append(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clowe46031a2018-07-02 18:41:15 +00001000
Marshall Clow62953962016-10-03 23:40:48 +00001001 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001002 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001003 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001004 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001005 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1006 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001007 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001008 >
Marshall Clow82513342016-09-24 22:45:42 +00001009 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001010 basic_string& append(const value_type* __s, size_type __n);
1011 basic_string& append(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001012 basic_string& append(size_type __n, value_type __c);
Eric Fiselier451d5582018-11-26 20:15:38 +00001013
1014 _LIBCPP_INLINE_VISIBILITY
1015 void __append_default_init(size_type __n);
1016
Howard Hinnantc51e1022010-05-11 19:42:16 +00001017 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001018 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001019 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001020 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001021 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001022 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001023 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001024 _LIBCPP_INLINE_VISIBILITY
1025 append(_InputIterator __first, _InputIterator __last) {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001026 const basic_string __temp(__first, __last, __alloc());
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001027 append(__temp.data(), __temp.size());
1028 return *this;
1029 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001030 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001031 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001032 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001033 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001034 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001035 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001036 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001037 _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001038 append(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001039
Eric Fiselierfc92be82017-04-19 00:28:44 +00001040#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001041 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001042 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001043#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001044
1045 void push_back(value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001046 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001047 void pop_back();
Marshall Clow05cf6692019-03-19 03:30:07 +00001048 _LIBCPP_INLINE_VISIBILITY reference front() _NOEXCEPT;
1049 _LIBCPP_INLINE_VISIBILITY const_reference front() const _NOEXCEPT;
1050 _LIBCPP_INLINE_VISIBILITY reference back() _NOEXCEPT;
1051 _LIBCPP_INLINE_VISIBILITY const_reference back() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001052
Marshall Clowe46031a2018-07-02 18:41:15 +00001053 template <class _Tp>
1054 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001055 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001056 <
1057 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1058 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001059 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001060 assign(const _Tp & __t) { __self_view __sv = __t; return assign(__sv.data(), __sv.size()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001061 _LIBCPP_INLINE_VISIBILITY
Marshall Clow95d5e9a2016-03-09 18:08:29 +00001062 basic_string& assign(const basic_string& __str) { return *this = __str; }
Eric Fiselierfc92be82017-04-19 00:28:44 +00001063#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001064 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001065 basic_string& assign(basic_string&& __str)
Marshall Clow5aa9e942015-10-05 16:17:34 +00001066 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001067 {*this = _VSTD::move(__str); return *this;}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001068#endif
Marshall Clow8db7fb02014-03-04 19:17:19 +00001069 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow62953962016-10-03 23:40:48 +00001070 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001071 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001072 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001073 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001074 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1075 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001076 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001077 >
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001078 assign(const _Tp & __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001079 basic_string& assign(const value_type* __s, size_type __n);
1080 basic_string& assign(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001081 basic_string& assign(size_type __n, value_type __c);
1082 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001083 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001084 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001085 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001086 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001087 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001088 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001089 assign(_InputIterator __first, _InputIterator __last);
1090 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001091 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001092 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001093 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001094 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001095 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001096 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001097 assign(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001098#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001099 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001100 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001101#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001102
Howard Hinnantcf823322010-12-17 14:46:43 +00001103 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001104 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001105
1106 template <class _Tp>
1107 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001108 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001109 <
1110 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1111 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001112 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001113 insert(size_type __pos1, const _Tp& __t)
1114 { __self_view __sv = __t; return insert(__pos1, __sv.data(), __sv.size()); }
1115
Marshall Clow82513342016-09-24 22:45:42 +00001116 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001117 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001118 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001119 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001120 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001121 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001122 >
Marshall Clow82513342016-09-24 22:45:42 +00001123 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Marshall Clow8db7fb02014-03-04 19:17:19 +00001124 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001125 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1126 basic_string& insert(size_type __pos, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001127 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1128 iterator insert(const_iterator __pos, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001129 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001130 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1131 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001132 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001133 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001134 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001135 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001136 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001137 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001138 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1139 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001140 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001141 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001142 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001143 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001144 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001145 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001146 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001147#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001148 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001149 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1150 {return insert(__pos, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001151#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001152
1153 basic_string& erase(size_type __pos = 0, size_type __n = npos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001154 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001155 iterator erase(const_iterator __pos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001156 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001157 iterator erase(const_iterator __first, const_iterator __last);
1158
Howard Hinnantcf823322010-12-17 14:46:43 +00001159 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001160 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001161
1162 template <class _Tp>
1163 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001164 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001165 <
1166 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1167 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001168 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001169 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 +00001170 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 +00001171 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001172 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001173 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001174 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001175 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001176 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001177 >
Marshall Clow82513342016-09-24 22:45:42 +00001178 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001179 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
1180 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001181 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001182 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001183 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001184
1185 template <class _Tp>
1186 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001187 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001188 <
1189 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1190 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001191 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001192 replace(const_iterator __i1, const_iterator __i2, const _Tp& __t) { __self_view __sv = __t; return replace(__i1 - begin(), __i2 - __i1, __sv); }
1193
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001194 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001195 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +00001196 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001197 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Howard Hinnantcf823322010-12-17 14:46:43 +00001198 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001199 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001200 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001201 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001202 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001203 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001204 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001205 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001206 >
Howard Hinnant990d6e82010-11-17 21:11:40 +00001207 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001208#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001209 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001210 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001211 {return replace(__i1, __i2, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001212#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001213
Howard Hinnantd17880b2013-06-28 16:59:19 +00001214 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
Howard Hinnantcf823322010-12-17 14:46:43 +00001215 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001216 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1217
Howard Hinnantcf823322010-12-17 14:46:43 +00001218 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001219 void swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001220#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00001221 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +00001222#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001223 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001224 __is_nothrow_swappable<allocator_type>::value);
1225#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001226
Howard Hinnantcf823322010-12-17 14:46:43 +00001227 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001228 const value_type* c_str() const _NOEXCEPT {return data();}
Howard Hinnantcf823322010-12-17 14:46:43 +00001229 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001230 const value_type* data() const _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Eric Fiselier4ee2a892017-11-20 20:23:27 +00001231#if _LIBCPP_STD_VER > 14 || defined(_LIBCPP_BUILDING_LIBRARY)
Marshall Clowd3c22392016-03-08 15:44:30 +00001232 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001233 value_type* data() _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Marshall Clowd3c22392016-03-08 15:44:30 +00001234#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001235
Howard Hinnantcf823322010-12-17 14:46:43 +00001236 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001237 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001238
Howard Hinnantcf823322010-12-17 14:46:43 +00001239 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001240 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001241
1242 template <class _Tp>
1243 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001244 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001245 <
1246 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1247 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001248 >
zoecarver1997e0a2021-02-05 11:54:47 -08001249 find(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001250 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001251 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001252 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001253 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001254
Howard Hinnantcf823322010-12-17 14:46:43 +00001255 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001256 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001257
1258 template <class _Tp>
1259 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001260 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001261 <
1262 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1263 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001264 >
zoecarver1997e0a2021-02-05 11:54:47 -08001265 rfind(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001266 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001267 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001268 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001269 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001270
Howard Hinnantcf823322010-12-17 14:46:43 +00001271 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001272 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001273
1274 template <class _Tp>
1275 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001276 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001277 <
1278 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1279 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001280 >
zoecarver1997e0a2021-02-05 11:54:47 -08001281 find_first_of(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001282 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001283 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001284 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001285 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001286 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001287
Howard Hinnantcf823322010-12-17 14:46:43 +00001288 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001289 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001290
1291 template <class _Tp>
1292 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001293 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001294 <
1295 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1296 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001297 >
zoecarver1997e0a2021-02-05 11:54:47 -08001298 find_last_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001299 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001300 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001301 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001302 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001303 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001304
Howard Hinnantcf823322010-12-17 14:46:43 +00001305 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001306 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001307
1308 template <class _Tp>
1309 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001310 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001311 <
1312 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1313 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001314 >
zoecarver1997e0a2021-02-05 11:54:47 -08001315 find_first_not_of(const _Tp &__t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001316 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 +00001317 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001318 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001319 _LIBCPP_INLINE_VISIBILITY
1320 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1321
1322 _LIBCPP_INLINE_VISIBILITY
1323 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001324
1325 template <class _Tp>
1326 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001327 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001328 <
1329 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1330 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001331 >
zoecarver1997e0a2021-02-05 11:54:47 -08001332 find_last_not_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001333 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 +00001334 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001335 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001336 _LIBCPP_INLINE_VISIBILITY
1337 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1338
1339 _LIBCPP_INLINE_VISIBILITY
1340 int compare(const basic_string& __str) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001341
1342 template <class _Tp>
1343 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001344 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001345 <
1346 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1347 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001348 >
zoecarver1997e0a2021-02-05 11:54:47 -08001349 compare(const _Tp &__t) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001350
1351 template <class _Tp>
1352 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001353 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001354 <
1355 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1356 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001357 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001358 compare(size_type __pos1, size_type __n1, const _Tp& __t) const;
1359
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001360 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001361 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Marshall Clow8db7fb02014-03-04 19:17:19 +00001362 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 +00001363
Marshall Clow82513342016-09-24 22:45:42 +00001364 template <class _Tp>
Eric Fiselier7d4aa5c2016-10-14 05:29:46 +00001365 inline _LIBCPP_INLINE_VISIBILITY
Louis Dionne9ce598d2021-09-08 09:14:43 -04001366 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001367 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001368 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001369 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001370 >
Marshall Clow82513342016-09-24 22:45:42 +00001371 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 +00001372 int compare(const value_type* __s) const _NOEXCEPT;
1373 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1374 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001375
Marshall Clow18c293b2017-12-04 20:11:38 +00001376#if _LIBCPP_STD_VER > 17
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001377 constexpr _LIBCPP_INLINE_VISIBILITY
1378 bool starts_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001379 { return __self_view(data(), size()).starts_with(__sv); }
1380
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001381 constexpr _LIBCPP_INLINE_VISIBILITY
1382 bool starts_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001383 { return !empty() && _Traits::eq(front(), __c); }
1384
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001385 constexpr _LIBCPP_INLINE_VISIBILITY
1386 bool starts_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001387 { return starts_with(__self_view(__s)); }
1388
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001389 constexpr _LIBCPP_INLINE_VISIBILITY
1390 bool ends_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001391 { return __self_view(data(), size()).ends_with( __sv); }
1392
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001393 constexpr _LIBCPP_INLINE_VISIBILITY
1394 bool ends_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001395 { return !empty() && _Traits::eq(back(), __c); }
1396
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001397 constexpr _LIBCPP_INLINE_VISIBILITY
1398 bool ends_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001399 { return ends_with(__self_view(__s)); }
1400#endif
1401
Wim Leflere023c3542021-01-19 14:33:30 -05001402#if _LIBCPP_STD_VER > 20
1403 constexpr _LIBCPP_INLINE_VISIBILITY
1404 bool contains(__self_view __sv) const noexcept
1405 { return __self_view(data(), size()).contains(__sv); }
1406
1407 constexpr _LIBCPP_INLINE_VISIBILITY
1408 bool contains(value_type __c) const noexcept
1409 { return __self_view(data(), size()).contains(__c); }
1410
1411 constexpr _LIBCPP_INLINE_VISIBILITY
1412 bool contains(const value_type* __s) const
1413 { return __self_view(data(), size()).contains(__s); }
1414#endif
1415
Howard Hinnantcf823322010-12-17 14:46:43 +00001416 _LIBCPP_INLINE_VISIBILITY bool __invariants() const;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001417
Marshall Clowe60a7182018-05-29 17:04:37 +00001418 _LIBCPP_INLINE_VISIBILITY void __clear_and_shrink() _NOEXCEPT;
Marshall Clow851b9ec2019-05-20 21:56:51 +00001419
Marek Kurdejc9848142020-11-26 10:07:16 +01001420 _LIBCPP_INLINE_VISIBILITY void __shrink_or_extend(size_type __target_capacity);
1421
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001422 _LIBCPP_INLINE_VISIBILITY
1423 bool __is_long() const _NOEXCEPT
1424 {return bool(__r_.first().__s.__size_ & __short_mask);}
1425
Louis Dionneba400782020-10-02 15:02:52 -04001426#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001427
1428 bool __dereferenceable(const const_iterator* __i) const;
1429 bool __decrementable(const const_iterator* __i) const;
1430 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1431 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1432
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001433#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001434
Howard Hinnantc51e1022010-05-11 19:42:16 +00001435private:
Nikolas Klauser93826d12022-01-04 17:24:03 +01001436 _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI static bool __fits_in_sso(size_type __sz) {
1437 // SSO is disabled during constant evaluation because `__is_long` isn't constexpr friendly
1438 return !__libcpp_is_constant_evaluated() && (__sz < __min_cap);
1439 }
1440
Nikolas Klauser48d680d2022-02-26 13:28:33 +01001441 template <class _ForwardIterator>
1442 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
1443 iterator __insert_from_safe_copy(size_type __n, size_type __ip, _ForwardIterator __first, _ForwardIterator __last) {
1444 size_type __sz = size();
1445 size_type __cap = capacity();
1446 value_type* __p;
1447 if (__cap - __sz >= __n)
1448 {
1449 __p = std::__to_address(__get_pointer());
1450 size_type __n_move = __sz - __ip;
1451 if (__n_move != 0)
1452 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
1453 }
1454 else
1455 {
1456 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
1457 __p = std::__to_address(__get_long_pointer());
1458 }
1459 __sz += __n;
1460 __set_size(__sz);
1461 traits_type::assign(__p[__sz], value_type());
1462 for (__p += __ip; __first != __last; ++__p, ++__first)
1463 traits_type::assign(*__p, *__first);
1464
1465 return begin() + __ip;
1466 }
1467
1468 _LIBCPP_HIDE_FROM_ABI allocator_type& __alloc() _NOEXCEPT { return __r_.second(); }
1469 _LIBCPP_HIDE_FROM_ABI const allocator_type& __alloc() const _NOEXCEPT { return __r_.second(); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001470
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001471#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001472
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001473 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001474 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001475# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001476 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001477# else
1478 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1479# endif
1480
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001481 _LIBCPP_INLINE_VISIBILITY
1482 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001483# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001484 {return __r_.first().__s.__size_ >> 1;}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001485# else
1486 {return __r_.first().__s.__size_;}
1487# endif
1488
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001489#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001490
1491 _LIBCPP_INLINE_VISIBILITY
1492 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001493# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001494 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1495# else
1496 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
1497# endif
1498
1499 _LIBCPP_INLINE_VISIBILITY
1500 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001501# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001502 {return __r_.first().__s.__size_;}
1503# else
1504 {return __r_.first().__s.__size_ >> 1;}
1505# endif
1506
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001507#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001508
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001509 _LIBCPP_INLINE_VISIBILITY
1510 void __set_long_size(size_type __s) _NOEXCEPT
1511 {__r_.first().__l.__size_ = __s;}
1512 _LIBCPP_INLINE_VISIBILITY
1513 size_type __get_long_size() const _NOEXCEPT
1514 {return __r_.first().__l.__size_;}
1515 _LIBCPP_INLINE_VISIBILITY
1516 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001517 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1518
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001519 _LIBCPP_INLINE_VISIBILITY
1520 void __set_long_cap(size_type __s) _NOEXCEPT
1521 {__r_.first().__l.__cap_ = __long_mask | __s;}
1522 _LIBCPP_INLINE_VISIBILITY
1523 size_type __get_long_cap() const _NOEXCEPT
Howard Hinnant28b24882011-12-01 20:21:04 +00001524 {return __r_.first().__l.__cap_ & size_type(~__long_mask);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001525
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001526 _LIBCPP_INLINE_VISIBILITY
1527 void __set_long_pointer(pointer __p) _NOEXCEPT
1528 {__r_.first().__l.__data_ = __p;}
1529 _LIBCPP_INLINE_VISIBILITY
1530 pointer __get_long_pointer() _NOEXCEPT
1531 {return __r_.first().__l.__data_;}
1532 _LIBCPP_INLINE_VISIBILITY
1533 const_pointer __get_long_pointer() const _NOEXCEPT
1534 {return __r_.first().__l.__data_;}
1535 _LIBCPP_INLINE_VISIBILITY
1536 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001537 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001538 _LIBCPP_INLINE_VISIBILITY
1539 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001540 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001541 _LIBCPP_INLINE_VISIBILITY
1542 pointer __get_pointer() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001543 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001544 _LIBCPP_INLINE_VISIBILITY
1545 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001546 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1547
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001548 _LIBCPP_INLINE_VISIBILITY
1549 void __zero() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001550 {
1551 size_type (&__a)[__n_words] = __r_.first().__r.__words;
1552 for (unsigned __i = 0; __i < __n_words; ++__i)
1553 __a[__i] = 0;
1554 }
1555
1556 template <size_type __a> static
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001557 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00001558 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +00001559 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001560 enum {__alignment = 16};
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001561 static _LIBCPP_INLINE_VISIBILITY
1562 size_type __recommend(size_type __s) _NOEXCEPT
Marshall Clow80584522018-02-07 21:30:17 +00001563 {
1564 if (__s < __min_cap) return static_cast<size_type>(__min_cap) - 1;
1565 size_type __guess = __align_it<sizeof(value_type) < __alignment ?
1566 __alignment/sizeof(value_type) : 1 > (__s+1) - 1;
1567 if (__guess == __min_cap) ++__guess;
1568 return __guess;
1569 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001570
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, size_type __reserve);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001573 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001574 void __init(const value_type* __s, size_type __sz);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001575 inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001576 void __init(size_type __n, value_type __c);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001577
Martijn Vels5e7c9752020-03-04 17:52:46 -05001578 // Slow path for the (inlined) copy constructor for 'long' strings.
1579 // Always externally instantiated and not inlined.
1580 // Requires that __s is zero terminated.
1581 // The main reason for this function to exist is because for unstable, we
1582 // want to allow inlining of the copy constructor. However, we don't want
1583 // to call the __init() functions as those are marked as inline which may
1584 // result in over-aggressive inlining by the compiler, where our aim is
1585 // to only inline the fast path code directly in the ctor.
1586 void __init_copy_ctor_external(const value_type* __s, size_type __sz);
1587
Howard Hinnantc51e1022010-05-11 19:42:16 +00001588 template <class _InputIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001589 inline
Louis Dionne9ce598d2021-09-08 09:14:43 -04001590 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001591 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001592 __is_exactly_cpp17_input_iterator<_InputIterator>::value
1593 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001594 __init(_InputIterator __first, _InputIterator __last);
1595
1596 template <class _ForwardIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001597 inline
Louis Dionne9ce598d2021-09-08 09:14:43 -04001598 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001599 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001600 __is_cpp17_forward_iterator<_ForwardIterator>::value
1601 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001602 __init(_ForwardIterator __first, _ForwardIterator __last);
1603
1604 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001605 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001606 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1607 size_type __n_copy, size_type __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00001608 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001609
Martijn Vels596e3de2020-02-26 15:55:49 -05001610 // __assign_no_alias is invoked for assignment operations where we
1611 // have proof that the input does not alias the current instance.
1612 // For example, operator=(basic_string) performs a 'self' check.
1613 template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04001614 basic_string& __assign_no_alias(const value_type* __s, size_type __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05001615
Howard Hinnantcf823322010-12-17 14:46:43 +00001616 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001617 void __erase_to_end(size_type __pos);
1618
Martijn Velsa81fc792020-02-26 13:25:43 -05001619 // __erase_external_with_move is invoked for erase() invocations where
1620 // `n ~= npos`, likely requiring memory moves on the string data.
1621 void __erase_external_with_move(size_type __pos, size_type __n);
1622
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001623 _LIBCPP_INLINE_VISIBILITY
1624 void __copy_assign_alloc(const basic_string& __str)
1625 {__copy_assign_alloc(__str, integral_constant<bool,
1626 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1627
1628 _LIBCPP_INLINE_VISIBILITY
1629 void __copy_assign_alloc(const basic_string& __str, true_type)
1630 {
Marshall Clowf258c202017-01-31 03:40:52 +00001631 if (__alloc() == __str.__alloc())
1632 __alloc() = __str.__alloc();
1633 else
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001634 {
Marshall Clowf258c202017-01-31 03:40:52 +00001635 if (!__str.__is_long())
1636 {
Vedant Kumar55e007e2018-03-08 21:15:26 +00001637 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001638 __alloc() = __str.__alloc();
1639 }
1640 else
1641 {
1642 allocator_type __a = __str.__alloc();
1643 pointer __p = __alloc_traits::allocate(__a, __str.__get_long_cap());
Vedant Kumar55e007e2018-03-08 21:15:26 +00001644 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001645 __alloc() = _VSTD::move(__a);
1646 __set_long_pointer(__p);
1647 __set_long_cap(__str.__get_long_cap());
1648 __set_long_size(__str.size());
1649 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001650 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001651 }
1652
1653 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001654 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001655 {}
1656
Eric Fiselierfc92be82017-04-19 00:28:44 +00001657#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +00001658 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001659 void __move_assign(basic_string& __str, false_type)
1660 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnantcf823322010-12-17 14:46:43 +00001661 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001662 void __move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001663#if _LIBCPP_STD_VER > 14
1664 _NOEXCEPT;
1665#else
Howard Hinnant3e276872011-06-03 18:40:47 +00001666 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001667#endif
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001668#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001669
1670 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001671 void
Howard Hinnantc2734962011-09-02 20:42:31 +00001672 __move_assign_alloc(basic_string& __str)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001673 _NOEXCEPT_(
1674 !__alloc_traits::propagate_on_container_move_assignment::value ||
1675 is_nothrow_move_assignable<allocator_type>::value)
1676 {__move_assign_alloc(__str, integral_constant<bool,
1677 __alloc_traits::propagate_on_container_move_assignment::value>());}
1678
1679 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00001680 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001681 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1682 {
1683 __alloc() = _VSTD::move(__c.__alloc());
1684 }
1685
1686 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001687 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001688 _NOEXCEPT
1689 {}
1690
Martijn Velsda7d94f2020-06-19 14:24:03 -04001691 basic_string& __assign_external(const value_type* __s);
1692 basic_string& __assign_external(const value_type* __s, size_type __n);
1693
1694 // Assigns the value in __s, guaranteed to be __n < __min_cap in length.
1695 inline basic_string& __assign_short(const value_type* __s, size_type __n) {
1696 pointer __p = __is_long()
1697 ? (__set_long_size(__n), __get_long_pointer())
1698 : (__set_short_size(__n), __get_short_pointer());
1699 traits_type::move(_VSTD::__to_address(__p), __s, __n);
1700 traits_type::assign(__p[__n], value_type());
1701 return *this;
1702 }
1703
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01001704 _LIBCPP_HIDE_FROM_ABI basic_string& __null_terminate_at(value_type* __p, size_type __newsz) {
1705 __set_size(__newsz);
1706 __invalidate_iterators_past(__newsz);
1707 traits_type::assign(__p[__newsz], value_type());
1708 return *this;
1709 }
1710
Howard Hinnantcf823322010-12-17 14:46:43 +00001711 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
1712 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(size_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001713
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001714 template<class _Tp>
1715 _LIBCPP_INLINE_VISIBILITY
1716 bool __addr_in_range(_Tp&& __t) const {
1717 const volatile void *__p = _VSTD::addressof(__t);
1718 return data() <= __p && __p <= data() + size();
1719 }
1720
Louis Dionned24191c2021-08-19 12:39:16 -04001721 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1722 void __throw_length_error() const {
Nikolas Klauserf89bb932022-01-24 19:44:34 +01001723 _VSTD::__throw_length_error("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001724 }
1725
1726 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1727 void __throw_out_of_range() const {
Nikolas Klauserf89bb932022-01-24 19:44:34 +01001728 _VSTD::__throw_out_of_range("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001729 }
1730
Howard Hinnantc51e1022010-05-11 19:42:16 +00001731 friend basic_string operator+<>(const basic_string&, const basic_string&);
1732 friend basic_string operator+<>(const value_type*, const basic_string&);
1733 friend basic_string operator+<>(value_type, const basic_string&);
1734 friend basic_string operator+<>(const basic_string&, const value_type*);
1735 friend basic_string operator+<>(const basic_string&, value_type);
1736};
1737
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001738// These declarations must appear before any functions are implicitly used
1739// so that they have the correct visibility specifier.
1740#ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
Louis Dionne89258142021-08-23 15:32:36 -04001741 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1742# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1743 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1744# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001745#else
Louis Dionne89258142021-08-23 15:32:36 -04001746 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1747# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1748 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1749# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001750#endif
1751
1752
Louis Dionned59f8a52021-08-17 11:59:07 -04001753#if _LIBCPP_STD_VER >= 17
Marshall Clowa0563332018-02-08 06:34:03 +00001754template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -05001755 class _CharT = __iter_value_type<_InputIterator>,
Marshall Clowa0563332018-02-08 06:34:03 +00001756 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001757 class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
1758 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowa0563332018-02-08 06:34:03 +00001759 >
1760basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
1761 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
Marshall Clowe46031a2018-07-02 18:41:15 +00001762
1763template<class _CharT,
1764 class _Traits,
1765 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001766 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowe46031a2018-07-02 18:41:15 +00001767 >
1768explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
1769 -> basic_string<_CharT, _Traits, _Allocator>;
1770
1771template<class _CharT,
1772 class _Traits,
1773 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001774 class = enable_if_t<__is_allocator<_Allocator>::value>,
Marshall Clowe46031a2018-07-02 18:41:15 +00001775 class _Sz = typename allocator_traits<_Allocator>::size_type
1776 >
1777basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
1778 -> basic_string<_CharT, _Traits, _Allocator>;
Marshall Clowa0563332018-02-08 06:34:03 +00001779#endif
1780
Howard Hinnantc51e1022010-05-11 19:42:16 +00001781template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001782inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001783void
1784basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
1785{
Louis Dionneba400782020-10-02 15:02:52 -04001786#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001787 if (!__libcpp_is_constant_evaluated())
1788 __get_db()->__invalidate_all(this);
Louis Dionneba400782020-10-02 15:02:52 -04001789#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001790}
1791
1792template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001793inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001794void
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001795basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type __pos)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001796{
Louis Dionneba400782020-10-02 15:02:52 -04001797#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001798 if (!__libcpp_is_constant_evaluated()) {
1799 __c_node* __c = __get_db()->__find_c_and_lock(this);
1800 if (__c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001801 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001802 const_pointer __new_last = __get_pointer() + __pos;
1803 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001804 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001805 --__p;
1806 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1807 if (__i->base() > __new_last)
1808 {
1809 (*__p)->__c_ = nullptr;
1810 if (--__c->end_ != __p)
1811 _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1812 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001813 }
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001814 __get_db()->unlock();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001815 }
1816 }
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001817#else
1818 (void)__pos;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001819#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001820}
1821
1822template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001823inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001824basic_string<_CharT, _Traits, _Allocator>::basic_string()
Marshall Clowe546cbb2015-06-04 02:05:41 +00001825 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001826 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001827{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001828 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001829 __zero();
1830}
1831
1832template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001833inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001834basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
Eric Fiseliere012bf22015-07-18 20:40:46 +00001835#if _LIBCPP_STD_VER <= 14
1836 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1837#else
1838 _NOEXCEPT
1839#endif
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001840: __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001841{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001842 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001843 __zero();
1844}
1845
1846template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier815ed732016-09-16 00:00:48 +00001847void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1848 size_type __sz,
1849 size_type __reserve)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001850{
1851 if (__reserve > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001852 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001853 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001854 if (__fits_in_sso(__reserve))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001855 {
1856 __set_short_size(__sz);
1857 __p = __get_short_pointer();
1858 }
1859 else
1860 {
1861 size_type __cap = __recommend(__reserve);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001862 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001863 __set_long_pointer(__p);
1864 __set_long_cap(__cap+1);
1865 __set_long_size(__sz);
1866 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001867 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001868 traits_type::assign(__p[__sz], value_type());
1869}
1870
1871template <class _CharT, class _Traits, class _Allocator>
1872void
Howard Hinnantd17880b2013-06-28 16:59:19 +00001873basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001874{
1875 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001876 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001877 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001878 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001879 {
1880 __set_short_size(__sz);
1881 __p = __get_short_pointer();
1882 }
1883 else
1884 {
1885 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001886 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001887 __set_long_pointer(__p);
1888 __set_long_cap(__cap+1);
1889 __set_long_size(__sz);
1890 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001891 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001892 traits_type::assign(__p[__sz], value_type());
1893}
1894
1895template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00001896template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00001897basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001898 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001899{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001900 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001901 __init(__s, traits_type::length(__s));
Nikolas Klauserf2807732022-01-11 00:33:35 +01001902 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001903}
1904
1905template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001906inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001907basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001908 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001909{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001910 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
1911 __init(__s, __n);
1912 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001913}
1914
1915template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001916inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001917basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001918 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001919{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001920 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001921 __init(__s, __n);
Nikolas Klauserf2807732022-01-11 00:33:35 +01001922 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001923}
1924
1925template <class _CharT, class _Traits, class _Allocator>
1926basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001927 : __r_(__default_init_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001928{
1929 if (!__str.__is_long())
1930 __r_.first().__r = __str.__r_.first().__r;
1931 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001932 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1933 __str.__get_long_size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01001934 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001935}
1936
1937template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00001938basic_string<_CharT, _Traits, _Allocator>::basic_string(
1939 const basic_string& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001940 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001941{
1942 if (!__str.__is_long())
1943 __r_.first().__r = __str.__r_.first().__r;
1944 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001945 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1946 __str.__get_long_size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01001947 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001948}
1949
Martijn Vels5e7c9752020-03-04 17:52:46 -05001950template <class _CharT, class _Traits, class _Allocator>
1951void basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(
1952 const value_type* __s, size_type __sz) {
1953 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001954 if (__fits_in_sso(__sz)) {
Martijn Vels5e7c9752020-03-04 17:52:46 -05001955 __p = __get_short_pointer();
1956 __set_short_size(__sz);
1957 } else {
1958 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001959 __throw_length_error();
Martijn Vels5e7c9752020-03-04 17:52:46 -05001960 size_t __cap = __recommend(__sz);
1961 __p = __alloc_traits::allocate(__alloc(), __cap + 1);
1962 __set_long_pointer(__p);
1963 __set_long_cap(__cap + 1);
1964 __set_long_size(__sz);
1965 }
1966 traits_type::copy(_VSTD::__to_address(__p), __s, __sz + 1);
1967}
1968
Eric Fiselierfc92be82017-04-19 00:28:44 +00001969#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001970
1971template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001972inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001973basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +00001974#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +00001975 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowa80abc72015-06-03 19:56:43 +00001976#else
1977 _NOEXCEPT
1978#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001979 : __r_(_VSTD::move(__str.__r_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001980{
1981 __str.__zero();
Nikolas Klauserf2807732022-01-11 00:33:35 +01001982 _VSTD::__debug_db_insert_c(this);
Louis Dionneba400782020-10-02 15:02:52 -04001983#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauserf2807732022-01-11 00:33:35 +01001984 if (!__libcpp_is_constant_evaluated() && __is_long())
1985 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001986#endif
1987}
1988
1989template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001990inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001991basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001992 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001993{
Marshall Clowd2d24692014-07-17 15:32:20 +00001994 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001995 __init(_VSTD::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd2d24692014-07-17 15:32:20 +00001996 else
1997 {
1998 __r_.first().__r = __str.__r_.first().__r;
1999 __str.__zero();
2000 }
Nikolas Klauserf2807732022-01-11 00:33:35 +01002001 _VSTD::__debug_db_insert_c(this);
Louis Dionneba400782020-10-02 15:02:52 -04002002#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauserf2807732022-01-11 00:33:35 +01002003 if (!__libcpp_is_constant_evaluated() && __is_long())
2004 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002005#endif
2006}
2007
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002008#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002009
2010template <class _CharT, class _Traits, class _Allocator>
2011void
2012basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
2013{
2014 if (__n > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002015 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002016 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002017 if (__fits_in_sso(__n))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002018 {
2019 __set_short_size(__n);
2020 __p = __get_short_pointer();
2021 }
2022 else
2023 {
2024 size_type __cap = __recommend(__n);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002025 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002026 __set_long_pointer(__p);
2027 __set_long_cap(__cap+1);
2028 __set_long_size(__n);
2029 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002030 traits_type::assign(_VSTD::__to_address(__p), __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002031 traits_type::assign(__p[__n], value_type());
2032}
2033
2034template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002035inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002036basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002037 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002038{
2039 __init(__n, __c);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002040 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002041}
2042
2043template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002044template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00002045basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002046 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002047{
2048 __init(__n, __c);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002049 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002050}
2051
Howard Hinnantc51e1022010-05-11 19:42:16 +00002052template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00002053basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
2054 size_type __pos, size_type __n,
2055 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002056 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002057{
2058 size_type __str_sz = __str.size();
2059 if (__pos > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002060 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002061 __init(__str.data() + __pos, _VSTD::min(__n, __str_sz - __pos));
Nikolas Klauserf2807732022-01-11 00:33:35 +01002062 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002063}
2064
2065template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002066inline
Marshall Clow83445802016-04-07 18:13:41 +00002067basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +00002068 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002069 : __r_(__default_init_tag(), __a)
Marshall Clow83445802016-04-07 18:13:41 +00002070{
2071 size_type __str_sz = __str.size();
2072 if (__pos > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002073 __throw_out_of_range();
Marshall Clow83445802016-04-07 18:13:41 +00002074 __init(__str.data() + __pos, __str_sz - __pos);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002075 _VSTD::__debug_db_insert_c(this);
Marshall Clow83445802016-04-07 18:13:41 +00002076}
2077
2078template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002079template <class _Tp, class>
Marshall Clow78dbe462016-11-14 18:22:19 +00002080basic_string<_CharT, _Traits, _Allocator>::basic_string(
Marshall Clowe46031a2018-07-02 18:41:15 +00002081 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002082 : __r_(__default_init_tag(), __a)
Marshall Clow78dbe462016-11-14 18:22:19 +00002083{
Marshall Clowe46031a2018-07-02 18:41:15 +00002084 __self_view __sv0 = __t;
2085 __self_view __sv = __sv0.substr(__pos, __n);
Marshall Clow78dbe462016-11-14 18:22:19 +00002086 __init(__sv.data(), __sv.size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002087 _VSTD::__debug_db_insert_c(this);
Marshall Clow78dbe462016-11-14 18:22:19 +00002088}
2089
2090template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002091template <class _Tp, class>
2092basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002093 : __r_(__default_init_tag(), __default_init_tag())
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002094{
Marshall Clowe46031a2018-07-02 18:41:15 +00002095 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002096 __init(__sv.data(), __sv.size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002097 _VSTD::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002098}
2099
2100template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002101template <class _Tp, class>
2102basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002103 : __r_(__default_init_tag(), __a)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002104{
Marshall Clowe46031a2018-07-02 18:41:15 +00002105 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002106 __init(__sv.data(), __sv.size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002107 _VSTD::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002108}
2109
2110template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002111template <class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002112__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002113<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002114 __is_exactly_cpp17_input_iterator<_InputIterator>::value
2115>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002116basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
2117{
2118 __zero();
2119#ifndef _LIBCPP_NO_EXCEPTIONS
2120 try
2121 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002122#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002123 for (; __first != __last; ++__first)
2124 push_back(*__first);
2125#ifndef _LIBCPP_NO_EXCEPTIONS
2126 }
2127 catch (...)
2128 {
2129 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002130 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002131 throw;
2132 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002133#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002134}
2135
2136template <class _CharT, class _Traits, class _Allocator>
2137template <class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002138__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002139<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002140 __is_cpp17_forward_iterator<_ForwardIterator>::value
2141>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002142basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
2143{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002144 size_type __sz = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002145 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002146 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002147 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002148 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002149 {
2150 __set_short_size(__sz);
2151 __p = __get_short_pointer();
2152 }
2153 else
2154 {
2155 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002156 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002157 __set_long_pointer(__p);
2158 __set_long_cap(__cap+1);
2159 __set_long_size(__sz);
2160 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002161
2162#ifndef _LIBCPP_NO_EXCEPTIONS
2163 try
2164 {
2165#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002166 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002167 traits_type::assign(*__p, *__first);
2168 traits_type::assign(*__p, value_type());
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002169#ifndef _LIBCPP_NO_EXCEPTIONS
2170 }
2171 catch (...)
2172 {
2173 if (__is_long())
2174 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
2175 throw;
2176 }
2177#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002178}
2179
2180template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002181template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002182inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002183basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002184 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002185{
2186 __init(__first, __last);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002187 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002188}
2189
2190template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002191template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002192inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002193basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
2194 const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002195 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002196{
2197 __init(__first, __last);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002198 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002199}
2200
Eric Fiselierfc92be82017-04-19 00:28:44 +00002201#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002202
Howard Hinnantc51e1022010-05-11 19:42:16 +00002203template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002204inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002205basic_string<_CharT, _Traits, _Allocator>::basic_string(
2206 initializer_list<_CharT> __il)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002207 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002208{
2209 __init(__il.begin(), __il.end());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002210 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002211}
2212
2213template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002214inline
Eric Fiselier9d355982017-04-12 23:45:53 +00002215
Eric Fiselier812882b2017-02-17 01:17:10 +00002216basic_string<_CharT, _Traits, _Allocator>::basic_string(
2217 initializer_list<_CharT> __il, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002218 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002219{
2220 __init(__il.begin(), __il.end());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002221 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002222}
2223
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002224#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002225
Howard Hinnantc51e1022010-05-11 19:42:16 +00002226template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002227basic_string<_CharT, _Traits, _Allocator>::~basic_string()
2228{
Louis Dionneba400782020-10-02 15:02:52 -04002229#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002230 if (!__libcpp_is_constant_evaluated())
2231 __get_db()->__erase_c(this);
Howard Hinnant8ea98242013-08-23 17:37:05 +00002232#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002233 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002234 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002235}
2236
2237template <class _CharT, class _Traits, class _Allocator>
2238void
2239basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
2240 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002241 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 +00002242{
2243 size_type __ms = max_size();
2244 if (__delta_cap > __ms - __old_cap - 1)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002245 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002246 pointer __old_p = __get_pointer();
2247 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002248 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002249 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002250 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002251 __invalidate_all_iterators();
2252 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002253 traits_type::copy(_VSTD::__to_address(__p),
2254 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002255 if (__n_add != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002256 traits_type::copy(_VSTD::__to_address(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002257 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2258 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002259 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2260 _VSTD::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002261 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002262 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002263 __set_long_pointer(__p);
2264 __set_long_cap(__cap+1);
2265 __old_sz = __n_copy + __n_add + __sec_cp_sz;
2266 __set_long_size(__old_sz);
2267 traits_type::assign(__p[__old_sz], value_type());
2268}
2269
2270template <class _CharT, class _Traits, class _Allocator>
2271void
2272basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2273 size_type __n_copy, size_type __n_del, size_type __n_add)
2274{
2275 size_type __ms = max_size();
Marshall Clow2267c5d2013-11-06 14:24:38 +00002276 if (__delta_cap > __ms - __old_cap)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002277 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002278 pointer __old_p = __get_pointer();
2279 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002280 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002281 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002282 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002283 __invalidate_all_iterators();
2284 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002285 traits_type::copy(_VSTD::__to_address(__p),
2286 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002287 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2288 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002289 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2290 _VSTD::__to_address(__old_p) + __n_copy + __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002291 __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002292 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002293 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002294 __set_long_pointer(__p);
2295 __set_long_cap(__cap+1);
2296}
2297
2298// assign
2299
2300template <class _CharT, class _Traits, class _Allocator>
Martijn Vels596e3de2020-02-26 15:55:49 -05002301template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04002302basic_string<_CharT, _Traits, _Allocator>&
2303basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(
Martijn Vels596e3de2020-02-26 15:55:49 -05002304 const value_type* __s, size_type __n) {
Louis Dionne6209e9f2022-03-03 13:39:12 -05002305 size_type __cap = __is_short ? static_cast<size_type>(__min_cap) : __get_long_cap();
Martijn Vels596e3de2020-02-26 15:55:49 -05002306 if (__n < __cap) {
2307 pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
2308 __is_short ? __set_short_size(__n) : __set_long_size(__n);
2309 traits_type::copy(_VSTD::__to_address(__p), __s, __n);
2310 traits_type::assign(__p[__n], value_type());
2311 __invalidate_iterators_past(__n);
2312 } else {
2313 size_type __sz = __is_short ? __get_short_size() : __get_long_size();
2314 __grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
2315 }
Martijn Velsb6a08b62020-04-10 18:36:31 -04002316 return *this;
Martijn Vels596e3de2020-02-26 15:55:49 -05002317}
2318
2319template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002320basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002321basic_string<_CharT, _Traits, _Allocator>::__assign_external(
2322 const value_type* __s, size_type __n) {
2323 size_type __cap = capacity();
2324 if (__cap >= __n) {
2325 value_type* __p = _VSTD::__to_address(__get_pointer());
2326 traits_type::move(__p, __s, __n);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002327 return __null_terminate_at(__p, __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04002328 } else {
2329 size_type __sz = size();
2330 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002331 return *this;
Martijn Velsda7d94f2020-06-19 14:24:03 -04002332 }
Martijn Velsda7d94f2020-06-19 14:24:03 -04002333}
2334
2335template <class _CharT, class _Traits, class _Allocator>
2336basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002337basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002338{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002339 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Nikolas Klauser93826d12022-01-04 17:24:03 +01002340 return (__builtin_constant_p(__n) && __fits_in_sso(__n))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002341 ? __assign_short(__s, __n)
2342 : __assign_external(__s, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002343}
2344
2345template <class _CharT, class _Traits, class _Allocator>
2346basic_string<_CharT, _Traits, _Allocator>&
2347basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2348{
2349 size_type __cap = capacity();
2350 if (__cap < __n)
2351 {
2352 size_type __sz = size();
2353 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2354 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002355 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002356 traits_type::assign(__p, __n, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002357 return __null_terminate_at(__p, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002358}
2359
2360template <class _CharT, class _Traits, class _Allocator>
2361basic_string<_CharT, _Traits, _Allocator>&
2362basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2363{
2364 pointer __p;
2365 if (__is_long())
2366 {
2367 __p = __get_long_pointer();
2368 __set_long_size(1);
2369 }
2370 else
2371 {
2372 __p = __get_short_pointer();
2373 __set_short_size(1);
2374 }
2375 traits_type::assign(*__p, __c);
2376 traits_type::assign(*++__p, value_type());
2377 __invalidate_iterators_past(1);
2378 return *this;
2379}
2380
2381template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002382basic_string<_CharT, _Traits, _Allocator>&
2383basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2384{
Martijn Vels596e3de2020-02-26 15:55:49 -05002385 if (this != &__str) {
2386 __copy_assign_alloc(__str);
2387 if (!__is_long()) {
2388 if (!__str.__is_long()) {
Eric Fiselier03bbc922020-01-15 17:27:10 -05002389 __r_.first().__r = __str.__r_.first().__r;
Martijn Vels596e3de2020-02-26 15:55:49 -05002390 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002391 return __assign_no_alias<true>(__str.data(), __str.size());
Martijn Vels596e3de2020-02-26 15:55:49 -05002392 }
2393 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002394 return __assign_no_alias<false>(__str.data(), __str.size());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002395 }
Martijn Vels596e3de2020-02-26 15:55:49 -05002396 }
2397 return *this;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002398}
2399
Eric Fiselierfc92be82017-04-19 00:28:44 +00002400#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002401
2402template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002403inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002404void
2405basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002406 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002407{
2408 if (__alloc() != __str.__alloc())
2409 assign(__str);
2410 else
2411 __move_assign(__str, true_type());
2412}
2413
2414template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002415inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002416void
2417basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002418#if _LIBCPP_STD_VER > 14
2419 _NOEXCEPT
2420#else
Howard Hinnant3e276872011-06-03 18:40:47 +00002421 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002422#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002423{
marshall2ed41622019-11-27 07:13:00 -08002424 if (__is_long()) {
2425 __alloc_traits::deallocate(__alloc(), __get_long_pointer(),
2426 __get_long_cap());
2427#if _LIBCPP_STD_VER <= 14
2428 if (!is_nothrow_move_assignable<allocator_type>::value) {
2429 __set_short_size(0);
2430 traits_type::assign(__get_short_pointer()[0], value_type());
2431 }
2432#endif
2433 }
2434 __move_assign_alloc(__str);
2435 __r_.first() = __str.__r_.first();
2436 __str.__set_short_size(0);
2437 traits_type::assign(__str.__get_short_pointer()[0], value_type());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002438}
2439
2440template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002441inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002442basic_string<_CharT, _Traits, _Allocator>&
2443basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002444 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002445{
2446 __move_assign(__str, integral_constant<bool,
2447 __alloc_traits::propagate_on_container_move_assignment::value>());
2448 return *this;
2449}
2450
2451#endif
2452
2453template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002454template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002455__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002456<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002457 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002458 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002459>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002460basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2461{
Marshall Clow958362f2016-09-05 01:54:30 +00002462 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002463 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002464 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002465}
2466
2467template <class _CharT, class _Traits, class _Allocator>
2468template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002469__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002470<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002471 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002472 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002473>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002474basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2475{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002476 size_type __cap = capacity();
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002477 size_type __n = __string_is_trivial_iterator<_ForwardIterator>::value ?
2478 static_cast<size_type>(_VSTD::distance(__first, __last)) : 0;
2479
2480 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2481 (__cap >= __n || !__addr_in_range(*__first)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002482 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002483 if (__cap < __n)
2484 {
2485 size_type __sz = size();
2486 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2487 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002488 pointer __p = __get_pointer();
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002489 for (; __first != __last; ++__p, (void) ++__first)
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002490 traits_type::assign(*__p, *__first);
2491 traits_type::assign(*__p, value_type());
2492 __set_size(__n);
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04002493 __invalidate_iterators_past(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002494 }
2495 else
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002496 {
2497 const basic_string __temp(__first, __last, __alloc());
2498 assign(__temp.data(), __temp.size());
2499 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002500 return *this;
2501}
2502
2503template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002504basic_string<_CharT, _Traits, _Allocator>&
2505basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2506{
2507 size_type __sz = __str.size();
2508 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002509 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002510 return assign(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002511}
2512
2513template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002514template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002515__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002516<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002517 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
2518 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00002519 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002520>
Marshall Clow82513342016-09-24 22:45:42 +00002521basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002522{
Marshall Clow82513342016-09-24 22:45:42 +00002523 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002524 size_type __sz = __sv.size();
2525 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002526 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002527 return assign(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2528}
2529
2530
2531template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002532basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002533basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s) {
2534 return __assign_external(__s, traits_type::length(__s));
2535}
2536
2537template <class _CharT, class _Traits, class _Allocator>
2538basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002539basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002540{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002541 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Louis Dionne3e0c4332021-08-31 10:49:06 -04002542 return __builtin_constant_p(*__s)
Nikolas Klauser93826d12022-01-04 17:24:03 +01002543 ? (__fits_in_sso(traits_type::length(__s))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002544 ? __assign_short(__s, traits_type::length(__s))
2545 : __assign_external(__s, traits_type::length(__s)))
2546 : __assign_external(__s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002547}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002548// append
2549
2550template <class _CharT, class _Traits, class _Allocator>
2551basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002552basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002553{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002554 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002555 size_type __cap = capacity();
2556 size_type __sz = size();
2557 if (__cap - __sz >= __n)
2558 {
2559 if (__n)
2560 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002561 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002562 traits_type::copy(__p + __sz, __s, __n);
2563 __sz += __n;
2564 __set_size(__sz);
2565 traits_type::assign(__p[__sz], value_type());
2566 }
2567 }
2568 else
2569 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2570 return *this;
2571}
2572
2573template <class _CharT, class _Traits, class _Allocator>
2574basic_string<_CharT, _Traits, _Allocator>&
2575basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2576{
2577 if (__n)
2578 {
2579 size_type __cap = capacity();
2580 size_type __sz = size();
2581 if (__cap - __sz < __n)
2582 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2583 pointer __p = __get_pointer();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002584 traits_type::assign(_VSTD::__to_address(__p) + __sz, __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002585 __sz += __n;
2586 __set_size(__sz);
2587 traits_type::assign(__p[__sz], value_type());
2588 }
2589 return *this;
2590}
2591
2592template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00002593inline void
2594basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n)
2595{
2596 if (__n)
2597 {
2598 size_type __cap = capacity();
2599 size_type __sz = size();
2600 if (__cap - __sz < __n)
2601 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2602 pointer __p = __get_pointer();
2603 __sz += __n;
2604 __set_size(__sz);
2605 traits_type::assign(__p[__sz], value_type());
2606 }
2607}
2608
2609template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002610void
2611basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2612{
Howard Hinnant68bf1812013-04-30 21:44:48 +00002613 bool __is_short = !__is_long();
2614 size_type __cap;
2615 size_type __sz;
2616 if (__is_short)
2617 {
2618 __cap = __min_cap - 1;
2619 __sz = __get_short_size();
2620 }
2621 else
2622 {
2623 __cap = __get_long_cap() - 1;
2624 __sz = __get_long_size();
2625 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002626 if (__sz == __cap)
Howard Hinnant68bf1812013-04-30 21:44:48 +00002627 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002628 __grow_by(__cap, 1, __sz, __sz, 0);
Louis Dionne82d2b2c2021-11-16 11:26:56 -05002629 __is_short = false; // the string is always long after __grow_by
Howard Hinnant68bf1812013-04-30 21:44:48 +00002630 }
2631 pointer __p;
2632 if (__is_short)
2633 {
2634 __p = __get_short_pointer() + __sz;
2635 __set_short_size(__sz+1);
2636 }
2637 else
2638 {
2639 __p = __get_long_pointer() + __sz;
2640 __set_long_size(__sz+1);
2641 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002642 traits_type::assign(*__p, __c);
2643 traits_type::assign(*++__p, value_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002644}
2645
Howard Hinnantc51e1022010-05-11 19:42:16 +00002646template <class _CharT, class _Traits, class _Allocator>
2647template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002648__enable_if_t
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002649<
2650 __is_cpp17_forward_iterator<_ForwardIterator>::value,
2651 basic_string<_CharT, _Traits, _Allocator>&
2652>
2653basic_string<_CharT, _Traits, _Allocator>::append(
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002654 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002655{
2656 size_type __sz = size();
2657 size_type __cap = capacity();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002658 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002659 if (__n)
2660 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002661 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2662 !__addr_in_range(*__first))
Marshall Clow958362f2016-09-05 01:54:30 +00002663 {
2664 if (__cap - __sz < __n)
2665 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2666 pointer __p = __get_pointer() + __sz;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002667 for (; __first != __last; ++__p, (void) ++__first)
Marshall Clow958362f2016-09-05 01:54:30 +00002668 traits_type::assign(*__p, *__first);
2669 traits_type::assign(*__p, value_type());
2670 __set_size(__sz + __n);
2671 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002672 else
2673 {
2674 const basic_string __temp(__first, __last, __alloc());
2675 append(__temp.data(), __temp.size());
2676 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002677 }
2678 return *this;
2679}
2680
2681template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002682inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002683basic_string<_CharT, _Traits, _Allocator>&
2684basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2685{
2686 return append(__str.data(), __str.size());
2687}
2688
2689template <class _CharT, class _Traits, class _Allocator>
2690basic_string<_CharT, _Traits, _Allocator>&
2691basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2692{
2693 size_type __sz = __str.size();
2694 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002695 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002696 return append(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002697}
2698
2699template <class _CharT, class _Traits, class _Allocator>
Marshall Clow62953962016-10-03 23:40:48 +00002700template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002701 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002702 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002703 __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 +00002704 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002705 >
Marshall Clow82513342016-09-24 22:45:42 +00002706basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002707{
Marshall Clow82513342016-09-24 22:45:42 +00002708 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002709 size_type __sz = __sv.size();
2710 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002711 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002712 return append(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2713}
2714
2715template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002716basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002717basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002718{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002719 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002720 return append(__s, traits_type::length(__s));
2721}
2722
2723// insert
2724
2725template <class _CharT, class _Traits, class _Allocator>
2726basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002727basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002728{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002729 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002730 size_type __sz = size();
2731 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002732 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002733 size_type __cap = capacity();
2734 if (__cap - __sz >= __n)
2735 {
2736 if (__n)
2737 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002738 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002739 size_type __n_move = __sz - __pos;
2740 if (__n_move != 0)
2741 {
2742 if (__p + __pos <= __s && __s < __p + __sz)
2743 __s += __n;
2744 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2745 }
2746 traits_type::move(__p + __pos, __s, __n);
2747 __sz += __n;
2748 __set_size(__sz);
2749 traits_type::assign(__p[__sz], value_type());
2750 }
2751 }
2752 else
2753 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2754 return *this;
2755}
2756
2757template <class _CharT, class _Traits, class _Allocator>
2758basic_string<_CharT, _Traits, _Allocator>&
2759basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2760{
2761 size_type __sz = size();
2762 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002763 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002764 if (__n)
2765 {
2766 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002767 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002768 if (__cap - __sz >= __n)
2769 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002770 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002771 size_type __n_move = __sz - __pos;
2772 if (__n_move != 0)
2773 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2774 }
2775 else
2776 {
2777 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002778 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002779 }
2780 traits_type::assign(__p + __pos, __n, __c);
2781 __sz += __n;
2782 __set_size(__sz);
2783 traits_type::assign(__p[__sz], value_type());
2784 }
2785 return *this;
2786}
2787
2788template <class _CharT, class _Traits, class _Allocator>
2789template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002790__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002791<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002792 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Marshall Clow039b2f02016-01-13 21:54:34 +00002793 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002794>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002795basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2796{
Nikolas Klausereed25832021-12-15 01:32:30 +01002797 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2798 "string::insert(iterator, range) called with an iterator not"
2799 " referring to this string");
2800 const basic_string __temp(__first, __last, __alloc());
2801 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002802}
2803
2804template <class _CharT, class _Traits, class _Allocator>
2805template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002806__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002807<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002808 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002809 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002810>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002811basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2812{
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002813 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2814 "string::insert(iterator, range) called with an iterator not referring to this string");
Nikolas Klausereed25832021-12-15 01:32:30 +01002815
Howard Hinnantc51e1022010-05-11 19:42:16 +00002816 size_type __ip = static_cast<size_type>(__pos - begin());
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002817 size_type __n = static_cast<size_type>(std::distance(__first, __last));
2818 if (__n == 0)
2819 return begin() + __ip;
2820
2821 if (__string_is_trivial_iterator<_ForwardIterator>::value && !__addr_in_range(*__first))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002822 {
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002823 return __insert_from_safe_copy(__n, __ip, __first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002824 }
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002825 else
2826 {
2827 const basic_string __temp(__first, __last, __alloc());
2828 return __insert_from_safe_copy(__n, __ip, __temp.begin(), __temp.end());
2829 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002830}
2831
2832template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002833inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002834basic_string<_CharT, _Traits, _Allocator>&
2835basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2836{
2837 return insert(__pos1, __str.data(), __str.size());
2838}
2839
2840template <class _CharT, class _Traits, class _Allocator>
2841basic_string<_CharT, _Traits, _Allocator>&
2842basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2843 size_type __pos2, size_type __n)
2844{
2845 size_type __str_sz = __str.size();
2846 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002847 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002848 return insert(__pos1, __str.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002849}
2850
2851template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002852template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002853__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002854<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002855 __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 +00002856 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002857>
Marshall Clow82513342016-09-24 22:45:42 +00002858basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002859 size_type __pos2, size_type __n)
2860{
Marshall Clow82513342016-09-24 22:45:42 +00002861 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002862 size_type __str_sz = __sv.size();
2863 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002864 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002865 return insert(__pos1, __sv.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
2866}
2867
2868template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002869basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002870basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002871{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002872 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002873 return insert(__pos, __s, traits_type::length(__s));
2874}
2875
2876template <class _CharT, class _Traits, class _Allocator>
2877typename basic_string<_CharT, _Traits, _Allocator>::iterator
2878basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2879{
Louis Dionnef4cc6fb2022-02-15 15:47:45 -05002880 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2881 "string::insert(iterator, character) called with an iterator not"
2882 " referring to this string");
2883
Howard Hinnantc51e1022010-05-11 19:42:16 +00002884 size_type __ip = static_cast<size_type>(__pos - begin());
2885 size_type __sz = size();
2886 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002887 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002888 if (__cap == __sz)
2889 {
2890 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002891 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002892 }
2893 else
2894 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002895 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002896 size_type __n_move = __sz - __ip;
2897 if (__n_move != 0)
2898 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2899 }
2900 traits_type::assign(__p[__ip], __c);
2901 traits_type::assign(__p[++__sz], value_type());
2902 __set_size(__sz);
2903 return begin() + static_cast<difference_type>(__ip);
2904}
2905
2906template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002907inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002908typename basic_string<_CharT, _Traits, _Allocator>::iterator
2909basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2910{
Nikolas Klausereed25832021-12-15 01:32:30 +01002911 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2912 "string::insert(iterator, n, value) called with an iterator not"
2913 " referring to this string");
2914 difference_type __p = __pos - begin();
2915 insert(static_cast<size_type>(__p), __n, __c);
2916 return begin() + __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002917}
2918
2919// replace
2920
2921template <class _CharT, class _Traits, class _Allocator>
2922basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002923basic_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 +00002924 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00002925{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002926 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002927 size_type __sz = size();
2928 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002929 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002930 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002931 size_type __cap = capacity();
2932 if (__cap - __sz + __n1 >= __n2)
2933 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002934 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002935 if (__n1 != __n2)
2936 {
2937 size_type __n_move = __sz - __pos - __n1;
2938 if (__n_move != 0)
2939 {
2940 if (__n1 > __n2)
2941 {
2942 traits_type::move(__p + __pos, __s, __n2);
2943 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002944 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002945 }
2946 if (__p + __pos < __s && __s < __p + __sz)
2947 {
2948 if (__p + __pos + __n1 <= __s)
2949 __s += __n2 - __n1;
2950 else // __p + __pos < __s < __p + __pos + __n1
2951 {
2952 traits_type::move(__p + __pos, __s, __n1);
2953 __pos += __n1;
2954 __s += __n2;
2955 __n2 -= __n1;
2956 __n1 = 0;
2957 }
2958 }
2959 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2960 }
2961 }
2962 traits_type::move(__p + __pos, __s, __n2);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002963 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002964 }
2965 else
2966 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
2967 return *this;
2968}
2969
2970template <class _CharT, class _Traits, class _Allocator>
2971basic_string<_CharT, _Traits, _Allocator>&
2972basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
2973{
2974 size_type __sz = size();
2975 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002976 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002977 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002978 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002979 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002980 if (__cap - __sz + __n1 >= __n2)
2981 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002982 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002983 if (__n1 != __n2)
2984 {
2985 size_type __n_move = __sz - __pos - __n1;
2986 if (__n_move != 0)
2987 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2988 }
2989 }
2990 else
2991 {
2992 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002993 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002994 }
2995 traits_type::assign(__p + __pos, __n2, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002996 return __null_terminate_at(__p, __sz - (__n1 - __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002997}
2998
2999template <class _CharT, class _Traits, class _Allocator>
3000template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003001__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00003002<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003003 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003004 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003005>
Howard Hinnant990d6e82010-11-17 21:11:40 +00003006basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003007 _InputIterator __j1, _InputIterator __j2)
3008{
Marshall Clow958362f2016-09-05 01:54:30 +00003009 const basic_string __temp(__j1, __j2, __alloc());
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003010 return replace(__i1, __i2, __temp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003011}
3012
3013template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003014inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003015basic_string<_CharT, _Traits, _Allocator>&
3016basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
3017{
3018 return replace(__pos1, __n1, __str.data(), __str.size());
3019}
3020
3021template <class _CharT, class _Traits, class _Allocator>
3022basic_string<_CharT, _Traits, _Allocator>&
3023basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
3024 size_type __pos2, size_type __n2)
3025{
3026 size_type __str_sz = __str.size();
3027 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003028 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003029 return replace(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003030}
3031
3032template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003033template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003034__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003035<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003036 __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 +00003037 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003038>
Marshall Clow82513342016-09-24 22:45:42 +00003039basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003040 size_type __pos2, size_type __n2)
3041{
Marshall Clow82513342016-09-24 22:45:42 +00003042 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003043 size_type __str_sz = __sv.size();
3044 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003045 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003046 return replace(__pos1, __n1, __sv.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
3047}
3048
3049template <class _CharT, class _Traits, class _Allocator>
3050basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003051basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003052{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003053 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003054 return replace(__pos, __n1, __s, traits_type::length(__s));
3055}
3056
3057template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003058inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003059basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003060basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003061{
3062 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
3063 __str.data(), __str.size());
3064}
3065
3066template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003067inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003068basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003069basic_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 +00003070{
3071 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
3072}
3073
3074template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003075inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003076basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003077basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003078{
3079 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
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 Hinnant990d6e82010-11-17 21:11:40 +00003085basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003086{
3087 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
3088}
3089
3090// erase
3091
Martijn Velsa81fc792020-02-26 13:25:43 -05003092// 'externally instantiated' erase() implementation, called when __n != npos.
3093// Does not check __pos against size()
Howard Hinnantc51e1022010-05-11 19:42:16 +00003094template <class _CharT, class _Traits, class _Allocator>
Martijn Velsa81fc792020-02-26 13:25:43 -05003095void
3096basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(
3097 size_type __pos, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003098{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003099 if (__n)
3100 {
Martijn Velsa81fc792020-02-26 13:25:43 -05003101 size_type __sz = size();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003102 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003103 __n = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003104 size_type __n_move = __sz - __pos - __n;
3105 if (__n_move != 0)
3106 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003107 __null_terminate_at(__p, __sz - __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003108 }
Martijn Velsa81fc792020-02-26 13:25:43 -05003109}
3110
3111template <class _CharT, class _Traits, class _Allocator>
3112basic_string<_CharT, _Traits, _Allocator>&
3113basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos,
3114 size_type __n) {
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003115 if (__pos > size())
3116 __throw_out_of_range();
Martijn Velsa81fc792020-02-26 13:25:43 -05003117 if (__n == npos) {
3118 __erase_to_end(__pos);
3119 } else {
3120 __erase_external_with_move(__pos, __n);
3121 }
3122 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003123}
3124
3125template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003126inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003127typename basic_string<_CharT, _Traits, _Allocator>::iterator
3128basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
3129{
Nikolas Klausereed25832021-12-15 01:32:30 +01003130 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3131 "string::erase(iterator) called with an iterator not"
3132 " referring to this string");
3133
3134 _LIBCPP_ASSERT(__pos != end(), "string::erase(iterator) called with a non-dereferenceable iterator");
3135 iterator __b = begin();
3136 size_type __r = static_cast<size_type>(__pos - __b);
3137 erase(__r, 1);
3138 return __b + static_cast<difference_type>(__r);
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 __first, const_iterator __last)
3145{
Nikolas Klausereed25832021-12-15 01:32:30 +01003146 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
3147 "string::erase(iterator, iterator) called with an iterator not"
3148 " referring to this string");
3149
3150 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
3151 iterator __b = begin();
3152 size_type __r = static_cast<size_type>(__first - __b);
3153 erase(__r, static_cast<size_type>(__last - __first));
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 +00003159void
3160basic_string<_CharT, _Traits, _Allocator>::pop_back()
3161{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003162 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003163 __erase_to_end(size() - 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003164}
3165
3166template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003167inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003168void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003169basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003170{
3171 __invalidate_all_iterators();
3172 if (__is_long())
3173 {
3174 traits_type::assign(*__get_long_pointer(), value_type());
3175 __set_long_size(0);
3176 }
3177 else
3178 {
3179 traits_type::assign(*__get_short_pointer(), value_type());
3180 __set_short_size(0);
3181 }
3182}
3183
3184template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003185inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003186void
3187basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
3188{
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003189 __null_terminate_at(_VSTD::__to_address(__get_pointer()), __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003190}
3191
3192template <class _CharT, class _Traits, class _Allocator>
3193void
3194basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
3195{
3196 size_type __sz = size();
3197 if (__n > __sz)
3198 append(__n - __sz, __c);
3199 else
3200 __erase_to_end(__n);
3201}
3202
3203template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00003204inline void
3205basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n)
3206{
3207 size_type __sz = size();
3208 if (__n > __sz) {
3209 __append_default_init(__n - __sz);
3210 } else
3211 __erase_to_end(__n);
3212}
3213
3214template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003215inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003216typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003217basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003218{
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003219 size_type __m = __alloc_traits::max_size(__alloc());
Eric Fiseliere9cc5922017-10-17 13:16:01 +00003220#ifdef _LIBCPP_BIG_ENDIAN
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003221 return (__m <= ~__long_mask ? __m : __m/2) - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003222#else
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003223 return __m - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003224#endif
3225}
3226
3227template <class _CharT, class _Traits, class _Allocator>
3228void
Marek Kurdejc9848142020-11-26 10:07:16 +01003229basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __requested_capacity)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003230{
Marek Kurdejc9848142020-11-26 10:07:16 +01003231 if (__requested_capacity > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003232 __throw_length_error();
Marek Kurdejc9848142020-11-26 10:07:16 +01003233
Louis Dionne05b6d3e2022-01-14 12:30:22 -05003234 // Make sure reserve(n) never shrinks. This is technically only required in C++20
3235 // and later (since P0966R1), however we provide consistent behavior in all Standard
3236 // modes because this function is instantiated in the shared library.
3237 if (__requested_capacity <= capacity())
3238 return;
Marek Kurdejc9848142020-11-26 10:07:16 +01003239
3240 size_type __target_capacity = _VSTD::max(__requested_capacity, size());
3241 __target_capacity = __recommend(__target_capacity);
3242 if (__target_capacity == capacity()) return;
3243
3244 __shrink_or_extend(__target_capacity);
3245}
3246
3247template <class _CharT, class _Traits, class _Allocator>
Louis Dionnee38b8642021-12-13 14:15:21 -05003248inline
Marek Kurdejc9848142020-11-26 10:07:16 +01003249void
3250basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT
3251{
3252 size_type __target_capacity = __recommend(size());
3253 if (__target_capacity == capacity()) return;
3254
3255 __shrink_or_extend(__target_capacity);
3256}
3257
3258template <class _CharT, class _Traits, class _Allocator>
Louis Dionnee38b8642021-12-13 14:15:21 -05003259inline
Marek Kurdejc9848142020-11-26 10:07:16 +01003260void
3261basic_string<_CharT, _Traits, _Allocator>::__shrink_or_extend(size_type __target_capacity)
3262{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003263 size_type __cap = capacity();
3264 size_type __sz = size();
Marek Kurdejc9848142020-11-26 10:07:16 +01003265
3266 pointer __new_data, __p;
3267 bool __was_long, __now_long;
3268 if (__target_capacity == __min_cap - 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003269 {
Marek Kurdejc9848142020-11-26 10:07:16 +01003270 __was_long = true;
3271 __now_long = false;
3272 __new_data = __get_short_pointer();
3273 __p = __get_long_pointer();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003274 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003275 else
3276 {
3277 if (__target_capacity > __cap)
3278 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3279 else
3280 {
3281 #ifndef _LIBCPP_NO_EXCEPTIONS
3282 try
3283 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003284 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003285 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3286 #ifndef _LIBCPP_NO_EXCEPTIONS
3287 }
3288 catch (...)
3289 {
3290 return;
3291 }
3292 #else // _LIBCPP_NO_EXCEPTIONS
3293 if (__new_data == nullptr)
3294 return;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003295 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003296 }
3297 __now_long = true;
3298 __was_long = __is_long();
3299 __p = __get_pointer();
3300 }
3301 traits_type::copy(_VSTD::__to_address(__new_data),
3302 _VSTD::__to_address(__p), size()+1);
3303 if (__was_long)
3304 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
3305 if (__now_long)
3306 {
3307 __set_long_cap(__target_capacity+1);
3308 __set_long_size(__sz);
3309 __set_long_pointer(__new_data);
3310 }
3311 else
3312 __set_short_size(__sz);
3313 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003314}
3315
3316template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003317inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003318typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003319basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003320{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003321 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003322 return *(data() + __pos);
3323}
3324
3325template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003326inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003327typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003328basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003329{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003330 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003331 return *(__get_pointer() + __pos);
3332}
3333
3334template <class _CharT, class _Traits, class _Allocator>
3335typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3336basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3337{
3338 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003339 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003340 return (*this)[__n];
3341}
3342
3343template <class _CharT, class _Traits, class _Allocator>
3344typename basic_string<_CharT, _Traits, _Allocator>::reference
3345basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
3346{
3347 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003348 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003349 return (*this)[__n];
3350}
3351
3352template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003353inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003354typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003355basic_string<_CharT, _Traits, _Allocator>::front() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003356{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003357 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003358 return *__get_pointer();
3359}
3360
3361template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003362inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003363typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003364basic_string<_CharT, _Traits, _Allocator>::front() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003365{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003366 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003367 return *data();
3368}
3369
3370template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003371inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003372typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003373basic_string<_CharT, _Traits, _Allocator>::back() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003374{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003375 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003376 return *(__get_pointer() + size() - 1);
3377}
3378
3379template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003380inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003381typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003382basic_string<_CharT, _Traits, _Allocator>::back() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003383{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003384 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003385 return *(data() + size() - 1);
3386}
3387
3388template <class _CharT, class _Traits, class _Allocator>
3389typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003390basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003391{
3392 size_type __sz = size();
3393 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003394 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003395 size_type __rlen = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003396 traits_type::copy(__s, data() + __pos, __rlen);
3397 return __rlen;
3398}
3399
3400template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003401inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003402basic_string<_CharT, _Traits, _Allocator>
3403basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3404{
3405 return basic_string(*this, __pos, __n, __alloc());
3406}
3407
3408template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003409inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003410void
3411basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003412#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00003413 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00003414#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00003415 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003416 __is_nothrow_swappable<allocator_type>::value)
3417#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003418{
Louis Dionneba400782020-10-02 15:02:52 -04003419#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01003420 if (!__libcpp_is_constant_evaluated()) {
3421 if (!__is_long())
3422 __get_db()->__invalidate_all(this);
3423 if (!__str.__is_long())
3424 __get_db()->__invalidate_all(&__str);
3425 __get_db()->swap(this, &__str);
3426 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00003427#endif
Eric Fiselier9bf691f2016-12-28 05:53:01 +00003428 _LIBCPP_ASSERT(
3429 __alloc_traits::propagate_on_container_swap::value ||
3430 __alloc_traits::is_always_equal::value ||
3431 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003432 _VSTD::swap(__r_.first(), __str.__r_.first());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05003433 _VSTD::__swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003434}
3435
3436// find
3437
3438template <class _Traits>
3439struct _LIBCPP_HIDDEN __traits_eq
3440{
3441 typedef typename _Traits::char_type char_type;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003442 _LIBCPP_INLINE_VISIBILITY
3443 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3444 {return _Traits::eq(__x, __y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003445};
3446
3447template<class _CharT, class _Traits, class _Allocator>
3448typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003449basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003450 size_type __pos,
3451 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003452{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003453 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003454 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003455 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003456}
3457
3458template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003459inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003460typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003461basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3462 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003463{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003464 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003465 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003466}
3467
3468template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003469template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003470__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003471<
3472 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3473 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003474>
Marshall Clowe46031a2018-07-02 18:41:15 +00003475basic_string<_CharT, _Traits, _Allocator>::find(const _Tp &__t,
zoecarver1997e0a2021-02-05 11:54:47 -08003476 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003477{
Marshall Clowe46031a2018-07-02 18:41:15 +00003478 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003479 return __str_find<value_type, size_type, traits_type, npos>
3480 (data(), size(), __sv.data(), __pos, __sv.size());
3481}
3482
3483template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003484inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003485typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003486basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003487 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003488{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003489 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003490 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003491 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003492}
3493
3494template<class _CharT, class _Traits, class _Allocator>
3495typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003496basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3497 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003498{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003499 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003500 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003501}
3502
3503// rfind
3504
3505template<class _CharT, class _Traits, class _Allocator>
3506typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003507basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003508 size_type __pos,
3509 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003510{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003511 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003512 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003513 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003514}
3515
3516template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003517inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003518typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003519basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3520 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003521{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003522 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003523 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003524}
3525
3526template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003527template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003528__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003529<
3530 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3531 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003532>
Marshall Clowe46031a2018-07-02 18:41:15 +00003533basic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003534 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003535{
Marshall Clowe46031a2018-07-02 18:41:15 +00003536 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003537 return __str_rfind<value_type, size_type, traits_type, npos>
3538 (data(), size(), __sv.data(), __pos, __sv.size());
3539}
3540
3541template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003542inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003543typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003544basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003545 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003546{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003547 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003548 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003549 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003550}
3551
3552template<class _CharT, class _Traits, class _Allocator>
3553typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003554basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3555 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003556{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003557 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003558 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003559}
3560
3561// find_first_of
3562
3563template<class _CharT, class _Traits, class _Allocator>
3564typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003565basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003566 size_type __pos,
3567 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003568{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003569 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003570 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003571 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003572}
3573
3574template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003575inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003576typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003577basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3578 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003579{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003580 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003581 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003582}
3583
3584template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003585template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003586__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003587<
3588 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3589 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003590>
Marshall Clowe46031a2018-07-02 18:41:15 +00003591basic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003592 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003593{
Marshall Clowe46031a2018-07-02 18:41:15 +00003594 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003595 return __str_find_first_of<value_type, size_type, traits_type, npos>
3596 (data(), size(), __sv.data(), __pos, __sv.size());
3597}
3598
3599template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003600inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003601typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003602basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003603 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003604{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003605 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003606 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003607 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003608}
3609
3610template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003611inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003612typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003613basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3614 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003615{
3616 return find(__c, __pos);
3617}
3618
3619// find_last_of
3620
3621template<class _CharT, class _Traits, class _Allocator>
3622typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003623basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003624 size_type __pos,
3625 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003626{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003627 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003628 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003629 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003630}
3631
3632template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003633inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003634typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003635basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3636 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003637{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003638 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003639 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003640}
3641
3642template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003643template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003644__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003645<
3646 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3647 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003648>
Marshall Clowe46031a2018-07-02 18:41:15 +00003649basic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003650 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003651{
Marshall Clowe46031a2018-07-02 18:41:15 +00003652 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003653 return __str_find_last_of<value_type, size_type, traits_type, npos>
3654 (data(), size(), __sv.data(), __pos, __sv.size());
3655}
3656
3657template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003658inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003659typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003660basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003661 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003662{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003663 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003664 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003665 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003666}
3667
3668template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003669inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003670typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003671basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3672 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003673{
3674 return rfind(__c, __pos);
3675}
3676
3677// find_first_not_of
3678
3679template<class _CharT, class _Traits, class _Allocator>
3680typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003681basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003682 size_type __pos,
3683 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003684{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003685 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003686 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003687 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003688}
3689
3690template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003691inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003692typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003693basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3694 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003695{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003696 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003697 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003698}
3699
3700template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003701template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003702__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003703<
3704 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3705 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003706>
Marshall Clowe46031a2018-07-02 18:41:15 +00003707basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003708 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003709{
Marshall Clowe46031a2018-07-02 18:41:15 +00003710 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003711 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3712 (data(), size(), __sv.data(), __pos, __sv.size());
3713}
3714
3715template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003716inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003717typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003718basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003719 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003720{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003721 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003722 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003723 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003724}
3725
3726template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003727inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003728typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003729basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3730 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003731{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003732 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003733 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003734}
3735
3736// find_last_not_of
3737
3738template<class _CharT, class _Traits, class _Allocator>
3739typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003740basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003741 size_type __pos,
3742 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003743{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003744 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003745 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003746 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003747}
3748
3749template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003750inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003751typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003752basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3753 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003754{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003755 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003756 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003757}
3758
3759template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003760template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003761__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003762<
3763 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3764 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003765>
Marshall Clowe46031a2018-07-02 18:41:15 +00003766basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003767 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003768{
Marshall Clowe46031a2018-07-02 18:41:15 +00003769 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003770 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3771 (data(), size(), __sv.data(), __pos, __sv.size());
3772}
3773
3774template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003775inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003776typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003777basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003778 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003779{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003780 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003781 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003782 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003783}
3784
3785template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003786inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003787typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003788basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3789 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003790{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003791 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003792 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003793}
3794
3795// compare
3796
3797template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003798template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003799__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003800<
3801 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3802 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003803>
zoecarver1997e0a2021-02-05 11:54:47 -08003804basic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003805{
Marshall Clowe46031a2018-07-02 18:41:15 +00003806 __self_view __sv = __t;
Howard Hinnantb0485532011-07-24 21:45:06 +00003807 size_t __lhs_sz = size();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003808 size_t __rhs_sz = __sv.size();
3809 int __result = traits_type::compare(data(), __sv.data(),
Howard Hinnantb0485532011-07-24 21:45:06 +00003810 _VSTD::min(__lhs_sz, __rhs_sz));
3811 if (__result != 0)
3812 return __result;
3813 if (__lhs_sz < __rhs_sz)
3814 return -1;
3815 if (__lhs_sz > __rhs_sz)
3816 return 1;
3817 return 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003818}
3819
3820template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003821inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003822int
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003823basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003824{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003825 return compare(__self_view(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003826}
3827
3828template <class _CharT, class _Traits, class _Allocator>
3829int
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003830basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3831 size_type __n1,
Howard Hinnantd17880b2013-06-28 16:59:19 +00003832 const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003833 size_type __n2) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003834{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003835 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003836 size_type __sz = size();
3837 if (__pos1 > __sz || __n2 == npos)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003838 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003839 size_type __rlen = _VSTD::min(__n1, __sz - __pos1);
3840 int __r = traits_type::compare(data() + __pos1, __s, _VSTD::min(__rlen, __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003841 if (__r == 0)
3842 {
3843 if (__rlen < __n2)
3844 __r = -1;
3845 else if (__rlen > __n2)
3846 __r = 1;
3847 }
3848 return __r;
3849}
3850
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003851template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003852template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003853__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003854<
3855 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3856 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003857>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003858basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3859 size_type __n1,
Marshall Clowe46031a2018-07-02 18:41:15 +00003860 const _Tp& __t) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003861{
Marshall Clowe46031a2018-07-02 18:41:15 +00003862 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003863 return compare(__pos1, __n1, __sv.data(), __sv.size());
3864}
3865
3866template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003867inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003868int
3869basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3870 size_type __n1,
3871 const basic_string& __str) const
3872{
3873 return compare(__pos1, __n1, __str.data(), __str.size());
3874}
3875
3876template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003877template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003878__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003879<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003880 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
3881 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00003882 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003883>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003884basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3885 size_type __n1,
Marshall Clow82513342016-09-24 22:45:42 +00003886 const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003887 size_type __pos2,
3888 size_type __n2) const
3889{
Marshall Clow82513342016-09-24 22:45:42 +00003890 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003891 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3892}
3893
3894template <class _CharT, class _Traits, class _Allocator>
3895int
3896basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3897 size_type __n1,
3898 const basic_string& __str,
3899 size_type __pos2,
3900 size_type __n2) const
3901{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003902 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003903}
3904
3905template <class _CharT, class _Traits, class _Allocator>
3906int
3907basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
3908{
3909 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3910 return compare(0, npos, __s, traits_type::length(__s));
3911}
3912
3913template <class _CharT, class _Traits, class _Allocator>
3914int
3915basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3916 size_type __n1,
3917 const value_type* __s) const
3918{
3919 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3920 return compare(__pos1, __n1, __s, traits_type::length(__s));
3921}
3922
Howard Hinnantc51e1022010-05-11 19:42:16 +00003923// __invariants
3924
3925template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003926inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003927bool
3928basic_string<_CharT, _Traits, _Allocator>::__invariants() const
3929{
3930 if (size() > capacity())
3931 return false;
3932 if (capacity() < __min_cap - 1)
3933 return false;
Bruce Mitchener170d8972020-11-24 12:53:53 -05003934 if (data() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003935 return false;
Louis Dionne663415f2020-10-05 16:16:13 -04003936 if (data()[size()] != value_type())
Howard Hinnantc51e1022010-05-11 19:42:16 +00003937 return false;
3938 return true;
3939}
3940
Vedant Kumar55e007e2018-03-08 21:15:26 +00003941// __clear_and_shrink
3942
3943template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003944inline
Louis Dionne173f29e2019-05-29 16:01:36 +00003945void
Marshall Clowe60a7182018-05-29 17:04:37 +00003946basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
Vedant Kumar55e007e2018-03-08 21:15:26 +00003947{
3948 clear();
3949 if(__is_long())
3950 {
3951 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
3952 __set_long_cap(0);
3953 __set_short_size(0);
Louis Dionne663415f2020-10-05 16:16:13 -04003954 traits_type::assign(*__get_short_pointer(), value_type());
Vedant Kumar55e007e2018-03-08 21:15:26 +00003955 }
Louis Dionne173f29e2019-05-29 16:01:36 +00003956}
Vedant Kumar55e007e2018-03-08 21:15:26 +00003957
Howard Hinnantc51e1022010-05-11 19:42:16 +00003958// operator==
3959
3960template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003961inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003962bool
3963operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003964 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003965{
Howard Hinnantaaeb1132013-04-22 23:55:13 +00003966 size_t __lhs_sz = __lhs.size();
3967 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
3968 __rhs.data(),
3969 __lhs_sz) == 0;
3970}
3971
3972template<class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003973inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaaeb1132013-04-22 23:55:13 +00003974bool
3975operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
3976 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
3977{
3978 size_t __lhs_sz = __lhs.size();
3979 if (__lhs_sz != __rhs.size())
3980 return false;
3981 const char* __lp = __lhs.data();
3982 const char* __rp = __rhs.data();
3983 if (__lhs.__is_long())
3984 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
3985 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
3986 if (*__lp != *__rp)
3987 return false;
3988 return true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003989}
3990
3991template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003992inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003993bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003994operator==(const _CharT* __lhs,
3995 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003996{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00003997 typedef basic_string<_CharT, _Traits, _Allocator> _String;
3998 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
3999 size_t __lhs_len = _Traits::length(__lhs);
4000 if (__lhs_len != __rhs.size()) return false;
4001 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004002}
4003
Howard Hinnantc51e1022010-05-11 19:42:16 +00004004template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004005inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004006bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004007operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
4008 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004009{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004010 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4011 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
4012 size_t __rhs_len = _Traits::length(__rhs);
4013 if (__rhs_len != __lhs.size()) return false;
4014 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004015}
4016
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004017template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004018inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004019bool
4020operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004021 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004022{
4023 return !(__lhs == __rhs);
4024}
4025
4026template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004027inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004028bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004029operator!=(const _CharT* __lhs,
4030 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004031{
4032 return !(__lhs == __rhs);
4033}
4034
4035template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004036inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004037bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004038operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4039 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004040{
4041 return !(__lhs == __rhs);
4042}
4043
4044// operator<
4045
4046template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004047inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004048bool
4049operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004050 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004051{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004052 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004053}
4054
4055template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004056inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004057bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004058operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4059 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004060{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004061 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004062}
4063
4064template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004065inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004066bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004067operator< (const _CharT* __lhs,
4068 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004069{
4070 return __rhs.compare(__lhs) > 0;
4071}
4072
Howard Hinnantc51e1022010-05-11 19:42:16 +00004073// operator>
4074
4075template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004076inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004077bool
4078operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004079 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004080{
4081 return __rhs < __lhs;
4082}
4083
4084template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004085inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004086bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004087operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4088 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004089{
4090 return __rhs < __lhs;
4091}
4092
4093template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004094inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004095bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004096operator> (const _CharT* __lhs,
4097 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004098{
4099 return __rhs < __lhs;
4100}
4101
4102// operator<=
4103
4104template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004105inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004106bool
4107operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004108 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004109{
4110 return !(__rhs < __lhs);
4111}
4112
4113template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004114inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004115bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004116operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4117 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004118{
4119 return !(__rhs < __lhs);
4120}
4121
4122template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004123inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004124bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004125operator<=(const _CharT* __lhs,
4126 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004127{
4128 return !(__rhs < __lhs);
4129}
4130
4131// operator>=
4132
4133template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004134inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004135bool
4136operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004137 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004138{
4139 return !(__lhs < __rhs);
4140}
4141
4142template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004143inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004144bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004145operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4146 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004147{
4148 return !(__lhs < __rhs);
4149}
4150
4151template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004152inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004153bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004154operator>=(const _CharT* __lhs,
4155 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004156{
4157 return !(__lhs < __rhs);
4158}
4159
4160// operator +
4161
4162template<class _CharT, class _Traits, class _Allocator>
4163basic_string<_CharT, _Traits, _Allocator>
4164operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4165 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4166{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004167 using _String = basic_string<_CharT, _Traits, _Allocator>;
4168 _String __r(_String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4169 typename _String::size_type __lhs_sz = __lhs.size();
4170 typename _String::size_type __rhs_sz = __rhs.size();
Howard Hinnantc51e1022010-05-11 19:42:16 +00004171 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4172 __r.append(__rhs.data(), __rhs_sz);
4173 return __r;
4174}
4175
4176template<class _CharT, class _Traits, class _Allocator>
4177basic_string<_CharT, _Traits, _Allocator>
4178operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4179{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004180 using _String = basic_string<_CharT, _Traits, _Allocator>;
4181 _String __r(_String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator()));
4182 typename _String::size_type __lhs_sz = _Traits::length(__lhs);
4183 typename _String::size_type __rhs_sz = __rhs.size();
Howard Hinnantc51e1022010-05-11 19:42:16 +00004184 __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz);
4185 __r.append(__rhs.data(), __rhs_sz);
4186 return __r;
4187}
4188
4189template<class _CharT, class _Traits, class _Allocator>
4190basic_string<_CharT, _Traits, _Allocator>
4191operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4192{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004193 using _String = basic_string<_CharT, _Traits, _Allocator>;
4194 _String __r(_String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator()));
4195 typename _String::size_type __rhs_sz = __rhs.size();
Howard Hinnantc51e1022010-05-11 19:42:16 +00004196 __r.__init(&__lhs, 1, 1 + __rhs_sz);
4197 __r.append(__rhs.data(), __rhs_sz);
4198 return __r;
4199}
4200
4201template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004202inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004203basic_string<_CharT, _Traits, _Allocator>
4204operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
4205{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004206 using _String = basic_string<_CharT, _Traits, _Allocator>;
4207 _String __r(_String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4208 typename _String::size_type __lhs_sz = __lhs.size();
4209 typename _String::size_type __rhs_sz = _Traits::length(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004210 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4211 __r.append(__rhs, __rhs_sz);
4212 return __r;
4213}
4214
4215template<class _CharT, class _Traits, class _Allocator>
4216basic_string<_CharT, _Traits, _Allocator>
4217operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
4218{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004219 using _String = basic_string<_CharT, _Traits, _Allocator>;
4220 _String __r(_String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4221 typename _String::size_type __lhs_sz = __lhs.size();
Howard Hinnantc51e1022010-05-11 19:42:16 +00004222 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1);
4223 __r.push_back(__rhs);
4224 return __r;
4225}
4226
Eric Fiselierfc92be82017-04-19 00:28:44 +00004227#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004228
4229template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004230inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004231basic_string<_CharT, _Traits, _Allocator>
4232operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4233{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004234 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004235}
4236
4237template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004238inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004239basic_string<_CharT, _Traits, _Allocator>
4240operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4241{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004242 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004243}
4244
4245template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004246inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004247basic_string<_CharT, _Traits, _Allocator>
4248operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4249{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004250 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004251}
4252
4253template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004254inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004255basic_string<_CharT, _Traits, _Allocator>
4256operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4257{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004258 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004259}
4260
4261template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004262inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004263basic_string<_CharT, _Traits, _Allocator>
4264operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4265{
4266 __rhs.insert(__rhs.begin(), __lhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004267 return _VSTD::move(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004268}
4269
4270template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004271inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004272basic_string<_CharT, _Traits, _Allocator>
4273operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
4274{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004275 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004276}
4277
4278template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004279inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004280basic_string<_CharT, _Traits, _Allocator>
4281operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
4282{
4283 __lhs.push_back(__rhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004284 return _VSTD::move(__lhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004285}
4286
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004287#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004288
4289// swap
4290
4291template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004292inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004293void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004294swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +00004295 basic_string<_CharT, _Traits, _Allocator>& __rhs)
4296 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004297{
4298 __lhs.swap(__rhs);
4299}
4300
Bruce Mitchener170d8972020-11-24 12:53:53 -05004301_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = nullptr, int __base = 10);
4302_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = nullptr, int __base = 10);
4303_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = nullptr, int __base = 10);
4304_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = nullptr, int __base = 10);
4305_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004306
Bruce Mitchener170d8972020-11-24 12:53:53 -05004307_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = nullptr);
4308_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = nullptr);
4309_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004310
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004311_LIBCPP_FUNC_VIS string to_string(int __val);
4312_LIBCPP_FUNC_VIS string to_string(unsigned __val);
4313_LIBCPP_FUNC_VIS string to_string(long __val);
4314_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
4315_LIBCPP_FUNC_VIS string to_string(long long __val);
4316_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
4317_LIBCPP_FUNC_VIS string to_string(float __val);
4318_LIBCPP_FUNC_VIS string to_string(double __val);
4319_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004320
Louis Dionne89258142021-08-23 15:32:36 -04004321#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Bruce Mitchener170d8972020-11-24 12:53:53 -05004322_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4323_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4324_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4325_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4326_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004327
Bruce Mitchener170d8972020-11-24 12:53:53 -05004328_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = nullptr);
4329_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = nullptr);
4330_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004331
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004332_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
4333_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
4334_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
4335_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
4336_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
4337_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
4338_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
4339_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
4340_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Louis Dionne89258142021-08-23 15:32:36 -04004341#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004342
Howard Hinnantc51e1022010-05-11 19:42:16 +00004343template<class _CharT, class _Traits, class _Allocator>
Martin Storsjö88890c22021-02-22 01:13:13 +02004344_LIBCPP_TEMPLATE_DATA_VIS
Eric Fiselierc9fdaf12020-01-15 17:02:17 -05004345const typename basic_string<_CharT, _Traits, _Allocator>::size_type
4346 basic_string<_CharT, _Traits, _Allocator>::npos;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004347
Marshall Clow851b9ec2019-05-20 21:56:51 +00004348template <class _CharT, class _Allocator>
4349struct _LIBCPP_TEMPLATE_VIS
4350 hash<basic_string<_CharT, char_traits<_CharT>, _Allocator> >
4351 : public unary_function<
4352 basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004353{
4354 size_t
Marshall Clow851b9ec2019-05-20 21:56:51 +00004355 operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT
4356 { return __do_string_hash(__val.data(), __val.data() + __val.size()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004357};
4358
Howard Hinnantc51e1022010-05-11 19:42:16 +00004359
Howard Hinnantdc095972011-07-18 15:51:59 +00004360template<class _CharT, class _Traits, class _Allocator>
4361basic_ostream<_CharT, _Traits>&
4362operator<<(basic_ostream<_CharT, _Traits>& __os,
4363 const basic_string<_CharT, _Traits, _Allocator>& __str);
4364
4365template<class _CharT, class _Traits, class _Allocator>
4366basic_istream<_CharT, _Traits>&
4367operator>>(basic_istream<_CharT, _Traits>& __is,
4368 basic_string<_CharT, _Traits, _Allocator>& __str);
4369
4370template<class _CharT, class _Traits, class _Allocator>
4371basic_istream<_CharT, _Traits>&
4372getline(basic_istream<_CharT, _Traits>& __is,
4373 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4374
4375template<class _CharT, class _Traits, class _Allocator>
4376inline _LIBCPP_INLINE_VISIBILITY
4377basic_istream<_CharT, _Traits>&
4378getline(basic_istream<_CharT, _Traits>& __is,
4379 basic_string<_CharT, _Traits, _Allocator>& __str);
4380
Howard Hinnantdc095972011-07-18 15:51:59 +00004381template<class _CharT, class _Traits, class _Allocator>
4382inline _LIBCPP_INLINE_VISIBILITY
4383basic_istream<_CharT, _Traits>&
4384getline(basic_istream<_CharT, _Traits>&& __is,
4385 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4386
4387template<class _CharT, class _Traits, class _Allocator>
4388inline _LIBCPP_INLINE_VISIBILITY
4389basic_istream<_CharT, _Traits>&
4390getline(basic_istream<_CharT, _Traits>&& __is,
4391 basic_string<_CharT, _Traits, _Allocator>& __str);
4392
Marshall Clow29b53f22018-12-14 18:49:35 +00004393#if _LIBCPP_STD_VER > 17
Marek Kurdeja98b1412020-05-02 13:58:03 +02004394template <class _CharT, class _Traits, class _Allocator, class _Up>
Marshall Clow29b53f22018-12-14 18:49:35 +00004395inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004396 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4397 erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
4398 auto __old_size = __str.size();
4399 __str.erase(_VSTD::remove(__str.begin(), __str.end(), __v), __str.end());
4400 return __old_size - __str.size();
4401}
Marshall Clow29b53f22018-12-14 18:49:35 +00004402
Marek Kurdeja98b1412020-05-02 13:58:03 +02004403template <class _CharT, class _Traits, class _Allocator, class _Predicate>
Marshall Clow29b53f22018-12-14 18:49:35 +00004404inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004405 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4406 erase_if(basic_string<_CharT, _Traits, _Allocator>& __str,
4407 _Predicate __pred) {
4408 auto __old_size = __str.size();
4409 __str.erase(_VSTD::remove_if(__str.begin(), __str.end(), __pred),
4410 __str.end());
4411 return __old_size - __str.size();
4412}
Marshall Clow29b53f22018-12-14 18:49:35 +00004413#endif
4414
Louis Dionneba400782020-10-02 15:02:52 -04004415#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004416
4417template<class _CharT, class _Traits, class _Allocator>
4418bool
4419basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4420{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004421 return data() <= _VSTD::__to_address(__i->base()) &&
4422 _VSTD::__to_address(__i->base()) < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004423}
4424
4425template<class _CharT, class _Traits, class _Allocator>
4426bool
4427basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4428{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004429 return data() < _VSTD::__to_address(__i->base()) &&
4430 _VSTD::__to_address(__i->base()) <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004431}
4432
4433template<class _CharT, class _Traits, class _Allocator>
4434bool
4435basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4436{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004437 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004438 return data() <= __p && __p <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004439}
4440
4441template<class _CharT, class _Traits, class _Allocator>
4442bool
4443basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4444{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004445 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004446 return data() <= __p && __p < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004447}
4448
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004449#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004450
Louis Dionne173f29e2019-05-29 16:01:36 +00004451#if _LIBCPP_STD_VER > 11
Marshall Clowcba751f2013-07-23 17:05:24 +00004452// Literal suffixes for basic_string [basic.string.literals]
Marshall Clowac868372013-10-05 21:18:32 +00004453inline namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004454{
4455 inline namespace string_literals
4456 {
Howard Hinnant5c167562013-08-07 19:39:48 +00004457 inline _LIBCPP_INLINE_VISIBILITY
4458 basic_string<char> operator "" s( const char *__str, size_t __len )
4459 {
4460 return basic_string<char> (__str, __len);
4461 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004462
Louis Dionne89258142021-08-23 15:32:36 -04004463#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnant5c167562013-08-07 19:39:48 +00004464 inline _LIBCPP_INLINE_VISIBILITY
4465 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4466 {
4467 return basic_string<wchar_t> (__str, __len);
4468 }
Louis Dionne89258142021-08-23 15:32:36 -04004469#endif
Marshall Clowcba751f2013-07-23 17:05:24 +00004470
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -04004471#ifndef _LIBCPP_HAS_NO_CHAR8_T
Marshall Clow8732fed2018-12-11 04:35:44 +00004472 inline _LIBCPP_INLINE_VISIBILITY
4473 basic_string<char8_t> operator "" s(const char8_t *__str, size_t __len) _NOEXCEPT
4474 {
4475 return basic_string<char8_t> (__str, __len);
4476 }
4477#endif
4478
Howard Hinnant5c167562013-08-07 19:39:48 +00004479 inline _LIBCPP_INLINE_VISIBILITY
4480 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4481 {
4482 return basic_string<char16_t> (__str, __len);
4483 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004484
Howard Hinnant5c167562013-08-07 19:39:48 +00004485 inline _LIBCPP_INLINE_VISIBILITY
4486 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4487 {
4488 return basic_string<char32_t> (__str, __len);
4489 }
Nikolas Klauserd26407a2021-12-02 14:12:51 +01004490 } // namespace string_literals
4491} // namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004492#endif
4493
Howard Hinnantc51e1022010-05-11 19:42:16 +00004494_LIBCPP_END_NAMESPACE_STD
4495
Eric Fiselierf4433a32017-05-31 22:07:49 +00004496_LIBCPP_POP_MACROS
4497
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004498#endif // _LIBCPP_STRING