blob: 1d0b7831cd6577bb720ad0dac40e28975867dd1f [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 Klauser8fccd622022-03-05 19:17:07 +0100546#include <utility> // TODO: Remove this
547
Nikolas Klauser4c1bf592022-02-11 19:15:18 +0100548// TODO: remove these headers
549#include <__functional/binary_function.h>
550#include <__functional/invoke.h>
551#include <__functional/operations.h>
552#include <__functional/reference_wrapper.h>
553#include <__functional/unary_function.h>
554#include <__functional/weak_result_type.h>
555#include <new>
556#include <typeinfo>
557
Louis Dionne89258142021-08-23 15:32:36 -0400558#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Nikolas Klauser80840272022-02-04 13:04:33 +0100559# include <cwchar>
Louis Dionne89258142021-08-23 15:32:36 -0400560#endif
561
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400562#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
Nikolas Klauser80840272022-02-04 13:04:33 +0100563# include <cstdint>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400564#endif
Eric Fiselier14b6de92014-08-10 23:53:08 +0000565
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000566#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Arthur O'Dwyer6eeaa002022-02-01 20:16:40 -0500567# pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000568#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000569
Eric Fiselierf4433a32017-05-31 22:07:49 +0000570_LIBCPP_PUSH_MACROS
571#include <__undef_macros>
572
573
Howard Hinnantc51e1022010-05-11 19:42:16 +0000574_LIBCPP_BEGIN_NAMESPACE_STD
575
Howard Hinnantc51e1022010-05-11 19:42:16 +0000576// basic_string
577
578template<class _CharT, class _Traits, class _Allocator>
579basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000580operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
581 const basic_string<_CharT, _Traits, _Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000582
583template<class _CharT, class _Traits, class _Allocator>
584basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000585operator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000586
587template<class _CharT, class _Traits, class _Allocator>
588basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000589operator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000590
591template<class _CharT, class _Traits, class _Allocator>
Louis Dionne8f8d95d2018-11-21 17:31:55 +0000592inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000593basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000594operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000595
596template<class _CharT, class _Traits, class _Allocator>
597basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000598operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000599
Shoaib Meenaiea363712017-07-29 02:54:41 +0000600_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&))
601
Marshall Clow039b2f02016-01-13 21:54:34 +0000602template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400603struct __string_is_trivial_iterator : public false_type {};
604
605template <class _Tp>
606struct __string_is_trivial_iterator<_Tp*>
607 : public is_arithmetic<_Tp> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000608
Louis Dionne173f29e2019-05-29 16:01:36 +0000609template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400610struct __string_is_trivial_iterator<__wrap_iter<_Iter> >
611 : public __string_is_trivial_iterator<_Iter> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000612
Marshall Clow82513342016-09-24 22:45:42 +0000613template <class _CharT, class _Traits, class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500614struct __can_be_converted_to_string_view : public _BoolConstant<
615 is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
616 !is_convertible<const _Tp&, const _CharT*>::value
617 > {};
Marshall Clow82513342016-09-24 22:45:42 +0000618
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000619#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000620
621template <class _CharT, size_t = sizeof(_CharT)>
622struct __padding
623{
624 unsigned char __xx[sizeof(_CharT)-1];
625};
626
627template <class _CharT>
628struct __padding<_CharT, 1>
629{
630};
631
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400632#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000633
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400634#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800635typedef basic_string<char8_t> u8string;
636#endif
637
638#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
639typedef basic_string<char16_t> u16string;
640typedef basic_string<char32_t> u32string;
Louis Dionne96fc5f52021-09-09 11:25:10 -0400641#endif
Richard Smith256954d2020-11-11 17:12:18 -0800642
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000643template<class _CharT, class _Traits, class _Allocator>
Richard Smith256954d2020-11-11 17:12:18 -0800644class
645 _LIBCPP_TEMPLATE_VIS
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400646#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800647 _LIBCPP_PREFERRED_NAME(u8string)
648#endif
649#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
650 _LIBCPP_PREFERRED_NAME(u16string)
651 _LIBCPP_PREFERRED_NAME(u32string)
652#endif
653 basic_string
Howard Hinnantc51e1022010-05-11 19:42:16 +0000654{
655public:
656 typedef basic_string __self;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000657 typedef basic_string_view<_CharT, _Traits> __self_view;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000658 typedef _Traits traits_type;
Marshall Clowa3a74e02017-03-15 18:41:11 +0000659 typedef _CharT value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000660 typedef _Allocator allocator_type;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000661 typedef allocator_traits<allocator_type> __alloc_traits;
662 typedef typename __alloc_traits::size_type size_type;
663 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant3e276872011-06-03 18:40:47 +0000664 typedef value_type& reference;
665 typedef const value_type& const_reference;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000666 typedef typename __alloc_traits::pointer pointer;
667 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000668
Marshall Clow79f33542018-03-21 00:36:05 +0000669 static_assert((!is_array<value_type>::value), "Character type of basic_string must not be an array");
670 static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string must be standard-layout");
671 static_assert(( is_trivial<value_type>::value), "Character type of basic_string must be trivial");
672 static_assert(( is_same<_CharT, typename traits_type::char_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000673 "traits_type::char_type must be the same type as CharT");
Marshall Clow79f33542018-03-21 00:36:05 +0000674 static_assert(( is_same<typename allocator_type::value_type, value_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000675 "Allocator::value_type must be same type as value_type");
Marshall Clowe46031a2018-07-02 18:41:15 +0000676
Howard Hinnantc51e1022010-05-11 19:42:16 +0000677 typedef __wrap_iter<pointer> iterator;
678 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000679 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
680 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000681
682private:
Howard Hinnant68bf1812013-04-30 21:44:48 +0000683
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000684#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000685
686 struct __long
687 {
688 pointer __data_;
689 size_type __size_;
690 size_type __cap_;
691 };
692
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000693#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000694 static const size_type __short_mask = 0x01;
695 static const size_type __long_mask = 0x1ul;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000696#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000697 static const size_type __short_mask = 0x80;
698 static const size_type __long_mask = ~(size_type(~0) >> 1);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400699#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +0000700
701 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
702 (sizeof(__long) - 1)/sizeof(value_type) : 2};
703
704 struct __short
705 {
706 value_type __data_[__min_cap];
707 struct
708 : __padding<value_type>
709 {
710 unsigned char __size_;
711 };
712 };
713
714#else
715
Howard Hinnantc51e1022010-05-11 19:42:16 +0000716 struct __long
717 {
718 size_type __cap_;
719 size_type __size_;
720 pointer __data_;
721 };
722
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000723#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000724 static const size_type __short_mask = 0x80;
725 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000726#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000727 static const size_type __short_mask = 0x01;
728 static const size_type __long_mask = 0x1ul;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400729#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +0000730
Howard Hinnantc51e1022010-05-11 19:42:16 +0000731 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
732 (sizeof(__long) - 1)/sizeof(value_type) : 2};
733
734 struct __short
735 {
736 union
737 {
738 unsigned char __size_;
Howard Hinnant49e145e2012-10-30 19:06:59 +0000739 value_type __lx;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000740 };
741 value_type __data_[__min_cap];
742 };
743
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400744#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000745
Howard Hinnant8ea98242013-08-23 17:37:05 +0000746 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000747
Howard Hinnant8ea98242013-08-23 17:37:05 +0000748 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000749
750 struct __raw
751 {
752 size_type __words[__n_words];
753 };
754
755 struct __rep
756 {
757 union
758 {
759 __long __l;
760 __short __s;
761 __raw __r;
762 };
763 };
764
765 __compressed_pair<__rep, allocator_type> __r_;
766
Howard Hinnantc51e1022010-05-11 19:42:16 +0000767public:
Martin Storsjö88890c22021-02-22 01:13:13 +0200768 _LIBCPP_TEMPLATE_DATA_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000769 static const size_type npos = -1;
770
Howard Hinnant3e276872011-06-03 18:40:47 +0000771 _LIBCPP_INLINE_VISIBILITY basic_string()
772 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000773
774 _LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a)
775#if _LIBCPP_STD_VER <= 14
776 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
777#else
778 _NOEXCEPT;
779#endif
780
Howard Hinnantc51e1022010-05-11 19:42:16 +0000781 basic_string(const basic_string& __str);
782 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clowa80abc72015-06-03 19:56:43 +0000783
Eric Fiselierfc92be82017-04-19 00:28:44 +0000784#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000785 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000786 basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +0000787#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +0000788 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000789#else
790 _NOEXCEPT;
791#endif
792
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000793 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000794 basic_string(basic_string&& __str, const allocator_type& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400795#endif // _LIBCPP_CXX03_LANG
Marshall Clowe46031a2018-07-02 18:41:15 +0000796
Louis Dionne9ce598d2021-09-08 09:14:43 -0400797 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Eric Fiseliera8567862018-07-17 05:48:48 +0000798 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5169d1c2019-12-16 19:03:23 -0500799 basic_string(const _CharT* __s) : __r_(__default_init_tag(), __default_init_tag()) {
Eric Fiseliera8567862018-07-17 05:48:48 +0000800 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
801 __init(__s, traits_type::length(__s));
Nikolas Klauserf2807732022-01-11 00:33:35 +0100802 _VSTD::__debug_db_insert_c(this);
Eric Fiseliera8567862018-07-17 05:48:48 +0000803 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000804
Louis Dionne9ce598d2021-09-08 09:14:43 -0400805 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000806 _LIBCPP_INLINE_VISIBILITY
807 basic_string(const _CharT* __s, const _Allocator& __a);
808
Marek Kurdej90d79712021-07-27 16:16:21 +0200809#if _LIBCPP_STD_VER > 20
810 basic_string(nullptr_t) = delete;
811#endif
812
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);
Howard Hinnantcf823322010-12-17 14:46:43 +0000815 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000816 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000817 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000818 basic_string(size_type __n, _CharT __c);
Marshall Clowe46031a2018-07-02 18:41:15 +0000819
Louis Dionne9ce598d2021-09-08 09:14:43 -0400820 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000821 _LIBCPP_INLINE_VISIBILITY
822 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
823
Marshall Clow83445802016-04-07 18:13:41 +0000824 basic_string(const basic_string& __str, size_type __pos, size_type __n,
Eric Fiselier812882b2017-02-17 01:17:10 +0000825 const _Allocator& __a = _Allocator());
Marshall Clow83445802016-04-07 18:13:41 +0000826 _LIBCPP_INLINE_VISIBILITY
827 basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +0000828 const _Allocator& __a = _Allocator());
Marshall Clowe46031a2018-07-02 18:41:15 +0000829
Louis Dionne9ce598d2021-09-08 09:14:43 -0400830 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 +0000831 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselier812882b2017-02-17 01:17:10 +0000832 basic_string(const _Tp& __t, size_type __pos, size_type __n,
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500833 const allocator_type& __a = allocator_type());
Marshall Clowe46031a2018-07-02 18:41:15 +0000834
Louis Dionne9ce598d2021-09-08 09:14:43 -0400835 template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500836 !__is_same_uncvref<_Tp, basic_string>::value> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000837 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
838 explicit basic_string(const _Tp& __t);
839
Louis Dionne9ce598d2021-09-08 09:14:43 -0400840 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 +0000841 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
842 explicit basic_string(const _Tp& __t, const allocator_type& __a);
843
Louis Dionne9ce598d2021-09-08 09:14:43 -0400844 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000845 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000846 basic_string(_InputIterator __first, _InputIterator __last);
Louis Dionne9ce598d2021-09-08 09:14:43 -0400847 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000848 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000849 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000850#ifndef _LIBCPP_CXX03_LANG
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);
Howard Hinnantcf823322010-12-17 14:46:43 +0000853 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000854 basic_string(initializer_list<_CharT> __il, const _Allocator& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400855#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000856
Eric Fiselierd9a702a2016-10-31 03:42:50 +0000857 inline ~basic_string();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000858
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000859 _LIBCPP_INLINE_VISIBILITY
860 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
861
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000862 basic_string& operator=(const basic_string& __str);
Eric Fiselier5ec13b12017-01-23 21:24:58 +0000863
Louis Dionne9ce598d2021-09-08 09:14:43 -0400864 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 +0000865 basic_string& operator=(const _Tp& __t)
866 {__self_view __sv = __t; return assign(__sv);}
867
Eric Fiselierfc92be82017-04-19 00:28:44 +0000868#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000869 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000870 basic_string& operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000871 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiselierfc92be82017-04-19 00:28:44 +0000872 _LIBCPP_INLINE_VISIBILITY
873 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000874#endif
Howard Hinnantd17880b2013-06-28 16:59:19 +0000875 _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const value_type* __s) {return assign(__s);}
Marek Kurdej90d79712021-07-27 16:16:21 +0200876#if _LIBCPP_STD_VER > 20
877 basic_string& operator=(nullptr_t) = delete;
878#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000879 basic_string& operator=(value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000880
Louis Dionneba400782020-10-02 15:02:52 -0400881#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +0000882 _LIBCPP_INLINE_VISIBILITY
883 iterator begin() _NOEXCEPT
884 {return iterator(this, __get_pointer());}
885 _LIBCPP_INLINE_VISIBILITY
886 const_iterator begin() const _NOEXCEPT
887 {return const_iterator(this, __get_pointer());}
888 _LIBCPP_INLINE_VISIBILITY
889 iterator end() _NOEXCEPT
890 {return iterator(this, __get_pointer() + size());}
891 _LIBCPP_INLINE_VISIBILITY
892 const_iterator end() const _NOEXCEPT
893 {return const_iterator(this, __get_pointer() + size());}
894#else
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000895 _LIBCPP_INLINE_VISIBILITY
896 iterator begin() _NOEXCEPT
897 {return iterator(__get_pointer());}
898 _LIBCPP_INLINE_VISIBILITY
899 const_iterator begin() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000900 {return const_iterator(__get_pointer());}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000901 _LIBCPP_INLINE_VISIBILITY
902 iterator end() _NOEXCEPT
903 {return iterator(__get_pointer() + size());}
904 _LIBCPP_INLINE_VISIBILITY
905 const_iterator end() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000906 {return const_iterator(__get_pointer() + size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400907#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000908 _LIBCPP_INLINE_VISIBILITY
909 reverse_iterator rbegin() _NOEXCEPT
910 {return reverse_iterator(end());}
911 _LIBCPP_INLINE_VISIBILITY
912 const_reverse_iterator rbegin() const _NOEXCEPT
913 {return const_reverse_iterator(end());}
914 _LIBCPP_INLINE_VISIBILITY
915 reverse_iterator rend() _NOEXCEPT
916 {return reverse_iterator(begin());}
917 _LIBCPP_INLINE_VISIBILITY
918 const_reverse_iterator rend() const _NOEXCEPT
919 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000920
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000921 _LIBCPP_INLINE_VISIBILITY
922 const_iterator cbegin() const _NOEXCEPT
923 {return begin();}
924 _LIBCPP_INLINE_VISIBILITY
925 const_iterator cend() const _NOEXCEPT
926 {return end();}
927 _LIBCPP_INLINE_VISIBILITY
928 const_reverse_iterator crbegin() const _NOEXCEPT
929 {return rbegin();}
930 _LIBCPP_INLINE_VISIBILITY
931 const_reverse_iterator crend() const _NOEXCEPT
932 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000933
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000934 _LIBCPP_INLINE_VISIBILITY size_type size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000935 {return __is_long() ? __get_long_size() : __get_short_size();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000936 _LIBCPP_INLINE_VISIBILITY size_type length() const _NOEXCEPT {return size();}
937 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT;
938 _LIBCPP_INLINE_VISIBILITY size_type capacity() const _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +0000939 {return (__is_long() ? __get_long_cap()
940 : static_cast<size_type>(__min_cap)) - 1;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000941
942 void resize(size_type __n, value_type __c);
943 _LIBCPP_INLINE_VISIBILITY void resize(size_type __n) {resize(__n, value_type());}
944
Marek Kurdejc9848142020-11-26 10:07:16 +0100945 void reserve(size_type __requested_capacity);
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100946
947#if _LIBCPP_STD_VER > 20
948 template <class _Op>
949 _LIBCPP_HIDE_FROM_ABI constexpr
950 void resize_and_overwrite(size_type __n, _Op __op) {
951 __resize_default_init(__n);
Nikolas Klausera4a76a12022-01-20 13:53:59 +0100952 __erase_to_end(_VSTD::move(__op)(data(), _LIBCPP_AUTO_CAST(__n)));
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100953 }
954#endif
955
Eric Fiselier451d5582018-11-26 20:15:38 +0000956 _LIBCPP_INLINE_VISIBILITY void __resize_default_init(size_type __n);
957
Marek Kurdejc9848142020-11-26 10:07:16 +0100958 _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_INLINE_VISIBILITY
959 void reserve() _NOEXCEPT {shrink_to_fit();}
Marshall Clow6ae12a82018-11-28 18:18:34 +0000960 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejc9848142020-11-26 10:07:16 +0100961 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000962 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000963 void clear() _NOEXCEPT;
Marshall Clowb7db4972017-11-15 20:02:27 +0000964 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
965 bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000966
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000967 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __pos) const _NOEXCEPT;
968 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000969
970 const_reference at(size_type __n) const;
971 reference at(size_type __n);
972
973 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const basic_string& __str) {return append(__str);}
Marshall Clowe46031a2018-07-02 18:41:15 +0000974
975 template <class _Tp>
976 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -0400977 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +0000978 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500979 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
980 && !__is_same_uncvref<_Tp, basic_string >::value,
Marshall Clowe46031a2018-07-02 18:41:15 +0000981 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500982 >
Marshall Clowe46031a2018-07-02 18:41:15 +0000983 operator+=(const _Tp& __t) {__self_view __sv = __t; return append(__sv);}
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000984 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const value_type* __s) {return append(__s);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000985 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(value_type __c) {push_back(__c); return *this;}
Eric Fiselierfc92be82017-04-19 00:28:44 +0000986#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000987 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(initializer_list<value_type> __il) {return append(__il);}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400988#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000989
Howard Hinnantcf823322010-12-17 14:46:43 +0000990 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000991 basic_string& append(const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000992
993 template <class _Tp>
994 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -0400995 __enable_if_t<
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500996 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
997 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clowe46031a2018-07-02 18:41:15 +0000998 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500999 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001000 append(const _Tp& __t) { __self_view __sv = __t; return append(__sv.data(), __sv.size()); }
Marshall Clow8db7fb02014-03-04 19:17:19 +00001001 basic_string& append(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clowe46031a2018-07-02 18:41:15 +00001002
Marshall Clow62953962016-10-03 23:40:48 +00001003 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001004 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001005 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001006 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001007 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1008 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001009 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001010 >
Marshall Clow82513342016-09-24 22:45:42 +00001011 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001012 basic_string& append(const value_type* __s, size_type __n);
1013 basic_string& append(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001014 basic_string& append(size_type __n, value_type __c);
Eric Fiselier451d5582018-11-26 20:15:38 +00001015
1016 _LIBCPP_INLINE_VISIBILITY
1017 void __append_default_init(size_type __n);
1018
Howard Hinnantc51e1022010-05-11 19:42:16 +00001019 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001020 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001021 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001022 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001023 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001024 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001025 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001026 _LIBCPP_INLINE_VISIBILITY
1027 append(_InputIterator __first, _InputIterator __last) {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001028 const basic_string __temp(__first, __last, __alloc());
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001029 append(__temp.data(), __temp.size());
1030 return *this;
1031 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001032 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001033 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001034 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001035 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001036 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001037 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001038 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001039 _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001040 append(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001041
Eric Fiselierfc92be82017-04-19 00:28:44 +00001042#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001043 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001044 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001045#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001046
1047 void push_back(value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001048 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001049 void pop_back();
Marshall Clow05cf6692019-03-19 03:30:07 +00001050 _LIBCPP_INLINE_VISIBILITY reference front() _NOEXCEPT;
1051 _LIBCPP_INLINE_VISIBILITY const_reference front() const _NOEXCEPT;
1052 _LIBCPP_INLINE_VISIBILITY reference back() _NOEXCEPT;
1053 _LIBCPP_INLINE_VISIBILITY const_reference back() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001054
Marshall Clowe46031a2018-07-02 18:41:15 +00001055 template <class _Tp>
1056 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001057 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001058 <
1059 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1060 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001061 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001062 assign(const _Tp & __t) { __self_view __sv = __t; return assign(__sv.data(), __sv.size()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001063 _LIBCPP_INLINE_VISIBILITY
Marshall Clow95d5e9a2016-03-09 18:08:29 +00001064 basic_string& assign(const basic_string& __str) { return *this = __str; }
Eric Fiselierfc92be82017-04-19 00:28:44 +00001065#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001066 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001067 basic_string& assign(basic_string&& __str)
Marshall Clow5aa9e942015-10-05 16:17:34 +00001068 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001069 {*this = _VSTD::move(__str); return *this;}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001070#endif
Marshall Clow8db7fb02014-03-04 19:17:19 +00001071 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow62953962016-10-03 23:40:48 +00001072 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001073 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001074 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001075 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001076 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1077 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001078 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001079 >
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001080 assign(const _Tp & __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001081 basic_string& assign(const value_type* __s, size_type __n);
1082 basic_string& assign(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001083 basic_string& assign(size_type __n, value_type __c);
1084 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001085 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001086 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001087 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001088 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001089 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001090 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001091 assign(_InputIterator __first, _InputIterator __last);
1092 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001093 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001094 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001095 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001096 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001097 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001098 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001099 assign(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001100#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001101 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001102 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001103#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001104
Howard Hinnantcf823322010-12-17 14:46:43 +00001105 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001106 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001107
1108 template <class _Tp>
1109 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001110 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001111 <
1112 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1113 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001114 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001115 insert(size_type __pos1, const _Tp& __t)
1116 { __self_view __sv = __t; return insert(__pos1, __sv.data(), __sv.size()); }
1117
Marshall Clow82513342016-09-24 22:45:42 +00001118 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001119 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001120 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001121 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001122 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001123 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001124 >
Marshall Clow82513342016-09-24 22:45:42 +00001125 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Marshall Clow8db7fb02014-03-04 19:17:19 +00001126 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001127 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1128 basic_string& insert(size_type __pos, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001129 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1130 iterator insert(const_iterator __pos, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001131 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001132 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1133 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001134 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001135 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001136 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001137 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001138 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001139 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001140 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1141 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001142 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001143 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001144 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001145 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001146 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001147 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001148 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001149#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001150 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001151 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1152 {return insert(__pos, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001153#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001154
1155 basic_string& erase(size_type __pos = 0, size_type __n = npos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001156 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001157 iterator erase(const_iterator __pos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001158 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001159 iterator erase(const_iterator __first, const_iterator __last);
1160
Howard Hinnantcf823322010-12-17 14:46:43 +00001161 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001162 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001163
1164 template <class _Tp>
1165 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001166 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001167 <
1168 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1169 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001170 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001171 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 +00001172 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 +00001173 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001174 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001175 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001176 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001177 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001178 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001179 >
Marshall Clow82513342016-09-24 22:45:42 +00001180 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001181 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
1182 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001183 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001184 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001185 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001186
1187 template <class _Tp>
1188 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001189 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001190 <
1191 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1192 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001193 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001194 replace(const_iterator __i1, const_iterator __i2, const _Tp& __t) { __self_view __sv = __t; return replace(__i1 - begin(), __i2 - __i1, __sv); }
1195
Marshall Clowdf63a6d2016-07-21 05:31:24 +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, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +00001198 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001199 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Howard Hinnantcf823322010-12-17 14:46:43 +00001200 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001201 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001202 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001203 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001204 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001205 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001206 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001207 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001208 >
Howard Hinnant990d6e82010-11-17 21:11:40 +00001209 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001210#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001211 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001212 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001213 {return replace(__i1, __i2, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001214#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001215
Howard Hinnantd17880b2013-06-28 16:59:19 +00001216 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
Howard Hinnantcf823322010-12-17 14:46:43 +00001217 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001218 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1219
Howard Hinnantcf823322010-12-17 14:46:43 +00001220 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001221 void swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001222#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00001223 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +00001224#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001225 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001226 __is_nothrow_swappable<allocator_type>::value);
1227#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001228
Howard Hinnantcf823322010-12-17 14:46:43 +00001229 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001230 const value_type* c_str() const _NOEXCEPT {return data();}
Howard Hinnantcf823322010-12-17 14:46:43 +00001231 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001232 const value_type* data() const _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Eric Fiselier4ee2a892017-11-20 20:23:27 +00001233#if _LIBCPP_STD_VER > 14 || defined(_LIBCPP_BUILDING_LIBRARY)
Marshall Clowd3c22392016-03-08 15:44:30 +00001234 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001235 value_type* data() _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Marshall Clowd3c22392016-03-08 15:44:30 +00001236#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001237
Howard Hinnantcf823322010-12-17 14:46:43 +00001238 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001239 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001240
Howard Hinnantcf823322010-12-17 14:46:43 +00001241 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001242 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001243
1244 template <class _Tp>
1245 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001246 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001247 <
1248 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1249 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001250 >
zoecarver1997e0a2021-02-05 11:54:47 -08001251 find(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001252 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001253 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001254 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001255 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001256
Howard Hinnantcf823322010-12-17 14:46:43 +00001257 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001258 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001259
1260 template <class _Tp>
1261 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001262 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001263 <
1264 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1265 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001266 >
zoecarver1997e0a2021-02-05 11:54:47 -08001267 rfind(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001268 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001269 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001270 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001271 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001272
Howard Hinnantcf823322010-12-17 14:46:43 +00001273 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001274 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001275
1276 template <class _Tp>
1277 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001278 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001279 <
1280 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1281 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001282 >
zoecarver1997e0a2021-02-05 11:54:47 -08001283 find_first_of(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001284 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001285 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001286 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001287 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001288 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001289
Howard Hinnantcf823322010-12-17 14:46:43 +00001290 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001291 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001292
1293 template <class _Tp>
1294 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001295 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001296 <
1297 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1298 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001299 >
zoecarver1997e0a2021-02-05 11:54:47 -08001300 find_last_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001301 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001302 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001303 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001304 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001305 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001306
Howard Hinnantcf823322010-12-17 14:46:43 +00001307 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001308 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001309
1310 template <class _Tp>
1311 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001312 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001313 <
1314 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1315 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001316 >
zoecarver1997e0a2021-02-05 11:54:47 -08001317 find_first_not_of(const _Tp &__t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001318 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 +00001319 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001320 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001321 _LIBCPP_INLINE_VISIBILITY
1322 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1323
1324 _LIBCPP_INLINE_VISIBILITY
1325 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001326
1327 template <class _Tp>
1328 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001329 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001330 <
1331 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1332 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001333 >
zoecarver1997e0a2021-02-05 11:54:47 -08001334 find_last_not_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001335 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 +00001336 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001337 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001338 _LIBCPP_INLINE_VISIBILITY
1339 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1340
1341 _LIBCPP_INLINE_VISIBILITY
1342 int compare(const basic_string& __str) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001343
1344 template <class _Tp>
1345 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001346 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001347 <
1348 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1349 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001350 >
zoecarver1997e0a2021-02-05 11:54:47 -08001351 compare(const _Tp &__t) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001352
1353 template <class _Tp>
1354 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001355 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001356 <
1357 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1358 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001359 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001360 compare(size_type __pos1, size_type __n1, const _Tp& __t) const;
1361
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001362 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001363 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Marshall Clow8db7fb02014-03-04 19:17:19 +00001364 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 +00001365
Marshall Clow82513342016-09-24 22:45:42 +00001366 template <class _Tp>
Eric Fiselier7d4aa5c2016-10-14 05:29:46 +00001367 inline _LIBCPP_INLINE_VISIBILITY
Louis Dionne9ce598d2021-09-08 09:14:43 -04001368 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001369 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001370 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001371 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001372 >
Marshall Clow82513342016-09-24 22:45:42 +00001373 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 +00001374 int compare(const value_type* __s) const _NOEXCEPT;
1375 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1376 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001377
Marshall Clow18c293b2017-12-04 20:11:38 +00001378#if _LIBCPP_STD_VER > 17
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001379 constexpr _LIBCPP_INLINE_VISIBILITY
1380 bool starts_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001381 { return __self_view(data(), size()).starts_with(__sv); }
1382
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001383 constexpr _LIBCPP_INLINE_VISIBILITY
1384 bool starts_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001385 { return !empty() && _Traits::eq(front(), __c); }
1386
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001387 constexpr _LIBCPP_INLINE_VISIBILITY
1388 bool starts_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001389 { return starts_with(__self_view(__s)); }
1390
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001391 constexpr _LIBCPP_INLINE_VISIBILITY
1392 bool ends_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001393 { return __self_view(data(), size()).ends_with( __sv); }
1394
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001395 constexpr _LIBCPP_INLINE_VISIBILITY
1396 bool ends_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001397 { return !empty() && _Traits::eq(back(), __c); }
1398
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001399 constexpr _LIBCPP_INLINE_VISIBILITY
1400 bool ends_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001401 { return ends_with(__self_view(__s)); }
1402#endif
1403
Wim Leflere023c3542021-01-19 14:33:30 -05001404#if _LIBCPP_STD_VER > 20
1405 constexpr _LIBCPP_INLINE_VISIBILITY
1406 bool contains(__self_view __sv) const noexcept
1407 { return __self_view(data(), size()).contains(__sv); }
1408
1409 constexpr _LIBCPP_INLINE_VISIBILITY
1410 bool contains(value_type __c) const noexcept
1411 { return __self_view(data(), size()).contains(__c); }
1412
1413 constexpr _LIBCPP_INLINE_VISIBILITY
1414 bool contains(const value_type* __s) const
1415 { return __self_view(data(), size()).contains(__s); }
1416#endif
1417
Howard Hinnantcf823322010-12-17 14:46:43 +00001418 _LIBCPP_INLINE_VISIBILITY bool __invariants() const;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001419
Marshall Clowe60a7182018-05-29 17:04:37 +00001420 _LIBCPP_INLINE_VISIBILITY void __clear_and_shrink() _NOEXCEPT;
Marshall Clow851b9ec2019-05-20 21:56:51 +00001421
Marek Kurdejc9848142020-11-26 10:07:16 +01001422 _LIBCPP_INLINE_VISIBILITY void __shrink_or_extend(size_type __target_capacity);
1423
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001424 _LIBCPP_INLINE_VISIBILITY
1425 bool __is_long() const _NOEXCEPT
1426 {return bool(__r_.first().__s.__size_ & __short_mask);}
1427
Louis Dionneba400782020-10-02 15:02:52 -04001428#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001429
1430 bool __dereferenceable(const const_iterator* __i) const;
1431 bool __decrementable(const const_iterator* __i) const;
1432 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1433 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1434
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001435#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001436
Howard Hinnantc51e1022010-05-11 19:42:16 +00001437private:
Nikolas Klauser93826d12022-01-04 17:24:03 +01001438 _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI static bool __fits_in_sso(size_type __sz) {
1439 // SSO is disabled during constant evaluation because `__is_long` isn't constexpr friendly
1440 return !__libcpp_is_constant_evaluated() && (__sz < __min_cap);
1441 }
1442
Nikolas Klauser48d680d2022-02-26 13:28:33 +01001443 template <class _ForwardIterator>
1444 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
1445 iterator __insert_from_safe_copy(size_type __n, size_type __ip, _ForwardIterator __first, _ForwardIterator __last) {
1446 size_type __sz = size();
1447 size_type __cap = capacity();
1448 value_type* __p;
1449 if (__cap - __sz >= __n)
1450 {
1451 __p = std::__to_address(__get_pointer());
1452 size_type __n_move = __sz - __ip;
1453 if (__n_move != 0)
1454 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
1455 }
1456 else
1457 {
1458 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
1459 __p = std::__to_address(__get_long_pointer());
1460 }
1461 __sz += __n;
1462 __set_size(__sz);
1463 traits_type::assign(__p[__sz], value_type());
1464 for (__p += __ip; __first != __last; ++__p, ++__first)
1465 traits_type::assign(*__p, *__first);
1466
1467 return begin() + __ip;
1468 }
1469
1470 _LIBCPP_HIDE_FROM_ABI allocator_type& __alloc() _NOEXCEPT { return __r_.second(); }
1471 _LIBCPP_HIDE_FROM_ABI const allocator_type& __alloc() const _NOEXCEPT { return __r_.second(); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001472
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001473#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001474
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001475 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001476 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001477# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001478 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001479# else
1480 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1481# endif
1482
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001483 _LIBCPP_INLINE_VISIBILITY
1484 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001485# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001486 {return __r_.first().__s.__size_ >> 1;}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001487# else
1488 {return __r_.first().__s.__size_;}
1489# endif
1490
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001491#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001492
1493 _LIBCPP_INLINE_VISIBILITY
1494 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001495# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001496 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1497# else
1498 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
1499# endif
1500
1501 _LIBCPP_INLINE_VISIBILITY
1502 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001503# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001504 {return __r_.first().__s.__size_;}
1505# else
1506 {return __r_.first().__s.__size_ >> 1;}
1507# endif
1508
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001509#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001510
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001511 _LIBCPP_INLINE_VISIBILITY
1512 void __set_long_size(size_type __s) _NOEXCEPT
1513 {__r_.first().__l.__size_ = __s;}
1514 _LIBCPP_INLINE_VISIBILITY
1515 size_type __get_long_size() const _NOEXCEPT
1516 {return __r_.first().__l.__size_;}
1517 _LIBCPP_INLINE_VISIBILITY
1518 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001519 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1520
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001521 _LIBCPP_INLINE_VISIBILITY
1522 void __set_long_cap(size_type __s) _NOEXCEPT
1523 {__r_.first().__l.__cap_ = __long_mask | __s;}
1524 _LIBCPP_INLINE_VISIBILITY
1525 size_type __get_long_cap() const _NOEXCEPT
Howard Hinnant28b24882011-12-01 20:21:04 +00001526 {return __r_.first().__l.__cap_ & size_type(~__long_mask);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001527
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001528 _LIBCPP_INLINE_VISIBILITY
1529 void __set_long_pointer(pointer __p) _NOEXCEPT
1530 {__r_.first().__l.__data_ = __p;}
1531 _LIBCPP_INLINE_VISIBILITY
1532 pointer __get_long_pointer() _NOEXCEPT
1533 {return __r_.first().__l.__data_;}
1534 _LIBCPP_INLINE_VISIBILITY
1535 const_pointer __get_long_pointer() const _NOEXCEPT
1536 {return __r_.first().__l.__data_;}
1537 _LIBCPP_INLINE_VISIBILITY
1538 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001539 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001540 _LIBCPP_INLINE_VISIBILITY
1541 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001542 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001543 _LIBCPP_INLINE_VISIBILITY
1544 pointer __get_pointer() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001545 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001546 _LIBCPP_INLINE_VISIBILITY
1547 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001548 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1549
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001550 _LIBCPP_INLINE_VISIBILITY
1551 void __zero() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001552 {
1553 size_type (&__a)[__n_words] = __r_.first().__r.__words;
1554 for (unsigned __i = 0; __i < __n_words; ++__i)
1555 __a[__i] = 0;
1556 }
1557
1558 template <size_type __a> static
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001559 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00001560 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +00001561 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001562 enum {__alignment = 16};
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001563 static _LIBCPP_INLINE_VISIBILITY
1564 size_type __recommend(size_type __s) _NOEXCEPT
Marshall Clow80584522018-02-07 21:30:17 +00001565 {
1566 if (__s < __min_cap) return static_cast<size_type>(__min_cap) - 1;
1567 size_type __guess = __align_it<sizeof(value_type) < __alignment ?
1568 __alignment/sizeof(value_type) : 1 > (__s+1) - 1;
1569 if (__guess == __min_cap) ++__guess;
1570 return __guess;
1571 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001572
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, size_type __reserve);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001575 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001576 void __init(const value_type* __s, size_type __sz);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001577 inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001578 void __init(size_type __n, value_type __c);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001579
Martijn Vels5e7c9752020-03-04 17:52:46 -05001580 // Slow path for the (inlined) copy constructor for 'long' strings.
1581 // Always externally instantiated and not inlined.
1582 // Requires that __s is zero terminated.
1583 // The main reason for this function to exist is because for unstable, we
1584 // want to allow inlining of the copy constructor. However, we don't want
1585 // to call the __init() functions as those are marked as inline which may
1586 // result in over-aggressive inlining by the compiler, where our aim is
1587 // to only inline the fast path code directly in the ctor.
1588 void __init_copy_ctor_external(const value_type* __s, size_type __sz);
1589
Howard Hinnantc51e1022010-05-11 19:42:16 +00001590 template <class _InputIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001591 inline
Louis Dionne9ce598d2021-09-08 09:14:43 -04001592 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001593 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001594 __is_exactly_cpp17_input_iterator<_InputIterator>::value
1595 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001596 __init(_InputIterator __first, _InputIterator __last);
1597
1598 template <class _ForwardIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001599 inline
Louis Dionne9ce598d2021-09-08 09:14:43 -04001600 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001601 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001602 __is_cpp17_forward_iterator<_ForwardIterator>::value
1603 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001604 __init(_ForwardIterator __first, _ForwardIterator __last);
1605
1606 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001607 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001608 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1609 size_type __n_copy, size_type __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00001610 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001611
Martijn Vels596e3de2020-02-26 15:55:49 -05001612 // __assign_no_alias is invoked for assignment operations where we
1613 // have proof that the input does not alias the current instance.
1614 // For example, operator=(basic_string) performs a 'self' check.
1615 template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04001616 basic_string& __assign_no_alias(const value_type* __s, size_type __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05001617
Howard Hinnantcf823322010-12-17 14:46:43 +00001618 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001619 void __erase_to_end(size_type __pos);
1620
Martijn Velsa81fc792020-02-26 13:25:43 -05001621 // __erase_external_with_move is invoked for erase() invocations where
1622 // `n ~= npos`, likely requiring memory moves on the string data.
1623 void __erase_external_with_move(size_type __pos, size_type __n);
1624
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001625 _LIBCPP_INLINE_VISIBILITY
1626 void __copy_assign_alloc(const basic_string& __str)
1627 {__copy_assign_alloc(__str, integral_constant<bool,
1628 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1629
1630 _LIBCPP_INLINE_VISIBILITY
1631 void __copy_assign_alloc(const basic_string& __str, true_type)
1632 {
Marshall Clowf258c202017-01-31 03:40:52 +00001633 if (__alloc() == __str.__alloc())
1634 __alloc() = __str.__alloc();
1635 else
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001636 {
Marshall Clowf258c202017-01-31 03:40:52 +00001637 if (!__str.__is_long())
1638 {
Vedant Kumar55e007e2018-03-08 21:15:26 +00001639 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001640 __alloc() = __str.__alloc();
1641 }
1642 else
1643 {
1644 allocator_type __a = __str.__alloc();
1645 pointer __p = __alloc_traits::allocate(__a, __str.__get_long_cap());
Vedant Kumar55e007e2018-03-08 21:15:26 +00001646 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001647 __alloc() = _VSTD::move(__a);
1648 __set_long_pointer(__p);
1649 __set_long_cap(__str.__get_long_cap());
1650 __set_long_size(__str.size());
1651 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001652 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001653 }
1654
1655 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001656 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001657 {}
1658
Eric Fiselierfc92be82017-04-19 00:28:44 +00001659#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +00001660 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001661 void __move_assign(basic_string& __str, false_type)
1662 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnantcf823322010-12-17 14:46:43 +00001663 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001664 void __move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001665#if _LIBCPP_STD_VER > 14
1666 _NOEXCEPT;
1667#else
Howard Hinnant3e276872011-06-03 18:40:47 +00001668 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001669#endif
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001670#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001671
1672 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001673 void
Howard Hinnantc2734962011-09-02 20:42:31 +00001674 __move_assign_alloc(basic_string& __str)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001675 _NOEXCEPT_(
1676 !__alloc_traits::propagate_on_container_move_assignment::value ||
1677 is_nothrow_move_assignable<allocator_type>::value)
1678 {__move_assign_alloc(__str, integral_constant<bool,
1679 __alloc_traits::propagate_on_container_move_assignment::value>());}
1680
1681 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00001682 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001683 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1684 {
1685 __alloc() = _VSTD::move(__c.__alloc());
1686 }
1687
1688 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001689 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001690 _NOEXCEPT
1691 {}
1692
Martijn Velsda7d94f2020-06-19 14:24:03 -04001693 basic_string& __assign_external(const value_type* __s);
1694 basic_string& __assign_external(const value_type* __s, size_type __n);
1695
1696 // Assigns the value in __s, guaranteed to be __n < __min_cap in length.
1697 inline basic_string& __assign_short(const value_type* __s, size_type __n) {
1698 pointer __p = __is_long()
1699 ? (__set_long_size(__n), __get_long_pointer())
1700 : (__set_short_size(__n), __get_short_pointer());
1701 traits_type::move(_VSTD::__to_address(__p), __s, __n);
1702 traits_type::assign(__p[__n], value_type());
1703 return *this;
1704 }
1705
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01001706 _LIBCPP_HIDE_FROM_ABI basic_string& __null_terminate_at(value_type* __p, size_type __newsz) {
1707 __set_size(__newsz);
1708 __invalidate_iterators_past(__newsz);
1709 traits_type::assign(__p[__newsz], value_type());
1710 return *this;
1711 }
1712
Howard Hinnantcf823322010-12-17 14:46:43 +00001713 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
1714 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(size_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001715
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001716 template<class _Tp>
1717 _LIBCPP_INLINE_VISIBILITY
1718 bool __addr_in_range(_Tp&& __t) const {
1719 const volatile void *__p = _VSTD::addressof(__t);
1720 return data() <= __p && __p <= data() + size();
1721 }
1722
Louis Dionned24191c2021-08-19 12:39:16 -04001723 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1724 void __throw_length_error() const {
Nikolas Klauserf89bb932022-01-24 19:44:34 +01001725 _VSTD::__throw_length_error("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001726 }
1727
1728 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1729 void __throw_out_of_range() const {
Nikolas Klauserf89bb932022-01-24 19:44:34 +01001730 _VSTD::__throw_out_of_range("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001731 }
1732
Howard Hinnantc51e1022010-05-11 19:42:16 +00001733 friend basic_string operator+<>(const basic_string&, const basic_string&);
1734 friend basic_string operator+<>(const value_type*, const basic_string&);
1735 friend basic_string operator+<>(value_type, const basic_string&);
1736 friend basic_string operator+<>(const basic_string&, const value_type*);
1737 friend basic_string operator+<>(const basic_string&, value_type);
1738};
1739
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001740// These declarations must appear before any functions are implicitly used
1741// so that they have the correct visibility specifier.
1742#ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
Louis Dionne89258142021-08-23 15:32:36 -04001743 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1744# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1745 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1746# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001747#else
Louis Dionne89258142021-08-23 15:32:36 -04001748 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1749# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1750 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1751# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001752#endif
1753
1754
Louis Dionned59f8a52021-08-17 11:59:07 -04001755#if _LIBCPP_STD_VER >= 17
Marshall Clowa0563332018-02-08 06:34:03 +00001756template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -05001757 class _CharT = __iter_value_type<_InputIterator>,
Marshall Clowa0563332018-02-08 06:34:03 +00001758 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001759 class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
1760 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowa0563332018-02-08 06:34:03 +00001761 >
1762basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
1763 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
Marshall Clowe46031a2018-07-02 18:41:15 +00001764
1765template<class _CharT,
1766 class _Traits,
1767 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001768 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowe46031a2018-07-02 18:41:15 +00001769 >
1770explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
1771 -> basic_string<_CharT, _Traits, _Allocator>;
1772
1773template<class _CharT,
1774 class _Traits,
1775 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001776 class = enable_if_t<__is_allocator<_Allocator>::value>,
Marshall Clowe46031a2018-07-02 18:41:15 +00001777 class _Sz = typename allocator_traits<_Allocator>::size_type
1778 >
1779basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
1780 -> basic_string<_CharT, _Traits, _Allocator>;
Marshall Clowa0563332018-02-08 06:34:03 +00001781#endif
1782
Howard Hinnantc51e1022010-05-11 19:42:16 +00001783template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001784inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001785void
1786basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
1787{
Louis Dionneba400782020-10-02 15:02:52 -04001788#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001789 if (!__libcpp_is_constant_evaluated())
1790 __get_db()->__invalidate_all(this);
Louis Dionneba400782020-10-02 15:02:52 -04001791#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001792}
1793
1794template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001795inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001796void
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001797basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type __pos)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001798{
Louis Dionneba400782020-10-02 15:02:52 -04001799#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001800 if (!__libcpp_is_constant_evaluated()) {
1801 __c_node* __c = __get_db()->__find_c_and_lock(this);
1802 if (__c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001803 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001804 const_pointer __new_last = __get_pointer() + __pos;
1805 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001806 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001807 --__p;
1808 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1809 if (__i->base() > __new_last)
1810 {
1811 (*__p)->__c_ = nullptr;
1812 if (--__c->end_ != __p)
1813 _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1814 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001815 }
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001816 __get_db()->unlock();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001817 }
1818 }
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001819#else
1820 (void)__pos;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001821#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001822}
1823
1824template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001825inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001826basic_string<_CharT, _Traits, _Allocator>::basic_string()
Marshall Clowe546cbb2015-06-04 02:05:41 +00001827 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001828 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001829{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001830 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001831 __zero();
1832}
1833
1834template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001835inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001836basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
Eric Fiseliere012bf22015-07-18 20:40:46 +00001837#if _LIBCPP_STD_VER <= 14
1838 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1839#else
1840 _NOEXCEPT
1841#endif
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001842: __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001843{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001844 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001845 __zero();
1846}
1847
1848template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier815ed732016-09-16 00:00:48 +00001849void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1850 size_type __sz,
1851 size_type __reserve)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001852{
1853 if (__reserve > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001854 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001855 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001856 if (__fits_in_sso(__reserve))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001857 {
1858 __set_short_size(__sz);
1859 __p = __get_short_pointer();
1860 }
1861 else
1862 {
1863 size_type __cap = __recommend(__reserve);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001864 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001865 __set_long_pointer(__p);
1866 __set_long_cap(__cap+1);
1867 __set_long_size(__sz);
1868 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001869 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001870 traits_type::assign(__p[__sz], value_type());
1871}
1872
1873template <class _CharT, class _Traits, class _Allocator>
1874void
Howard Hinnantd17880b2013-06-28 16:59:19 +00001875basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001876{
1877 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001878 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001879 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001880 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001881 {
1882 __set_short_size(__sz);
1883 __p = __get_short_pointer();
1884 }
1885 else
1886 {
1887 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001888 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001889 __set_long_pointer(__p);
1890 __set_long_cap(__cap+1);
1891 __set_long_size(__sz);
1892 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001893 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001894 traits_type::assign(__p[__sz], value_type());
1895}
1896
1897template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00001898template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00001899basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001900 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001901{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001902 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001903 __init(__s, traits_type::length(__s));
Nikolas Klauserf2807732022-01-11 00:33:35 +01001904 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001905}
1906
1907template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001908inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001909basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001910 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001911{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001912 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
1913 __init(__s, __n);
1914 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001915}
1916
1917template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001918inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001919basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001920 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001921{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001922 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001923 __init(__s, __n);
Nikolas Klauserf2807732022-01-11 00:33:35 +01001924 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001925}
1926
1927template <class _CharT, class _Traits, class _Allocator>
1928basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001929 : __r_(__default_init_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001930{
1931 if (!__str.__is_long())
1932 __r_.first().__r = __str.__r_.first().__r;
1933 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001934 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1935 __str.__get_long_size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01001936 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001937}
1938
1939template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00001940basic_string<_CharT, _Traits, _Allocator>::basic_string(
1941 const basic_string& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001942 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001943{
1944 if (!__str.__is_long())
1945 __r_.first().__r = __str.__r_.first().__r;
1946 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001947 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1948 __str.__get_long_size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01001949 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001950}
1951
Martijn Vels5e7c9752020-03-04 17:52:46 -05001952template <class _CharT, class _Traits, class _Allocator>
1953void basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(
1954 const value_type* __s, size_type __sz) {
1955 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001956 if (__fits_in_sso(__sz)) {
Martijn Vels5e7c9752020-03-04 17:52:46 -05001957 __p = __get_short_pointer();
1958 __set_short_size(__sz);
1959 } else {
1960 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001961 __throw_length_error();
Martijn Vels5e7c9752020-03-04 17:52:46 -05001962 size_t __cap = __recommend(__sz);
1963 __p = __alloc_traits::allocate(__alloc(), __cap + 1);
1964 __set_long_pointer(__p);
1965 __set_long_cap(__cap + 1);
1966 __set_long_size(__sz);
1967 }
1968 traits_type::copy(_VSTD::__to_address(__p), __s, __sz + 1);
1969}
1970
Eric Fiselierfc92be82017-04-19 00:28:44 +00001971#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001972
1973template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001974inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001975basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +00001976#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +00001977 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowa80abc72015-06-03 19:56:43 +00001978#else
1979 _NOEXCEPT
1980#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001981 : __r_(_VSTD::move(__str.__r_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001982{
1983 __str.__zero();
Nikolas Klauserf2807732022-01-11 00:33:35 +01001984 _VSTD::__debug_db_insert_c(this);
Louis Dionneba400782020-10-02 15:02:52 -04001985#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauserf2807732022-01-11 00:33:35 +01001986 if (!__libcpp_is_constant_evaluated() && __is_long())
1987 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001988#endif
1989}
1990
1991template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001992inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001993basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001994 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001995{
Marshall Clowd2d24692014-07-17 15:32:20 +00001996 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001997 __init(_VSTD::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd2d24692014-07-17 15:32:20 +00001998 else
1999 {
2000 __r_.first().__r = __str.__r_.first().__r;
2001 __str.__zero();
2002 }
Nikolas Klauserf2807732022-01-11 00:33:35 +01002003 _VSTD::__debug_db_insert_c(this);
Louis Dionneba400782020-10-02 15:02:52 -04002004#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauserf2807732022-01-11 00:33:35 +01002005 if (!__libcpp_is_constant_evaluated() && __is_long())
2006 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002007#endif
2008}
2009
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002010#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002011
2012template <class _CharT, class _Traits, class _Allocator>
2013void
2014basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
2015{
2016 if (__n > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002017 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002018 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002019 if (__fits_in_sso(__n))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002020 {
2021 __set_short_size(__n);
2022 __p = __get_short_pointer();
2023 }
2024 else
2025 {
2026 size_type __cap = __recommend(__n);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002027 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002028 __set_long_pointer(__p);
2029 __set_long_cap(__cap+1);
2030 __set_long_size(__n);
2031 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002032 traits_type::assign(_VSTD::__to_address(__p), __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002033 traits_type::assign(__p[__n], value_type());
2034}
2035
2036template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002037inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002038basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002039 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002040{
2041 __init(__n, __c);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002042 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002043}
2044
2045template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002046template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00002047basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002048 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002049{
2050 __init(__n, __c);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002051 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002052}
2053
Howard Hinnantc51e1022010-05-11 19:42:16 +00002054template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00002055basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
2056 size_type __pos, size_type __n,
2057 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002058 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002059{
2060 size_type __str_sz = __str.size();
2061 if (__pos > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002062 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002063 __init(__str.data() + __pos, _VSTD::min(__n, __str_sz - __pos));
Nikolas Klauserf2807732022-01-11 00:33:35 +01002064 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002065}
2066
2067template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002068inline
Marshall Clow83445802016-04-07 18:13:41 +00002069basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +00002070 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002071 : __r_(__default_init_tag(), __a)
Marshall Clow83445802016-04-07 18:13:41 +00002072{
2073 size_type __str_sz = __str.size();
2074 if (__pos > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002075 __throw_out_of_range();
Marshall Clow83445802016-04-07 18:13:41 +00002076 __init(__str.data() + __pos, __str_sz - __pos);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002077 _VSTD::__debug_db_insert_c(this);
Marshall Clow83445802016-04-07 18:13:41 +00002078}
2079
2080template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002081template <class _Tp, class>
Marshall Clow78dbe462016-11-14 18:22:19 +00002082basic_string<_CharT, _Traits, _Allocator>::basic_string(
Marshall Clowe46031a2018-07-02 18:41:15 +00002083 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002084 : __r_(__default_init_tag(), __a)
Marshall Clow78dbe462016-11-14 18:22:19 +00002085{
Marshall Clowe46031a2018-07-02 18:41:15 +00002086 __self_view __sv0 = __t;
2087 __self_view __sv = __sv0.substr(__pos, __n);
Marshall Clow78dbe462016-11-14 18:22:19 +00002088 __init(__sv.data(), __sv.size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002089 _VSTD::__debug_db_insert_c(this);
Marshall Clow78dbe462016-11-14 18:22:19 +00002090}
2091
2092template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002093template <class _Tp, class>
2094basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002095 : __r_(__default_init_tag(), __default_init_tag())
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002096{
Marshall Clowe46031a2018-07-02 18:41:15 +00002097 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002098 __init(__sv.data(), __sv.size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002099 _VSTD::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002100}
2101
2102template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002103template <class _Tp, class>
2104basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002105 : __r_(__default_init_tag(), __a)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002106{
Marshall Clowe46031a2018-07-02 18:41:15 +00002107 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002108 __init(__sv.data(), __sv.size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002109 _VSTD::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002110}
2111
2112template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002113template <class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002114__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002115<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002116 __is_exactly_cpp17_input_iterator<_InputIterator>::value
2117>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002118basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
2119{
2120 __zero();
2121#ifndef _LIBCPP_NO_EXCEPTIONS
2122 try
2123 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002124#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002125 for (; __first != __last; ++__first)
2126 push_back(*__first);
2127#ifndef _LIBCPP_NO_EXCEPTIONS
2128 }
2129 catch (...)
2130 {
2131 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002132 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002133 throw;
2134 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002135#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002136}
2137
2138template <class _CharT, class _Traits, class _Allocator>
2139template <class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002140__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002141<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002142 __is_cpp17_forward_iterator<_ForwardIterator>::value
2143>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002144basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
2145{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002146 size_type __sz = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002147 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002148 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002149 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002150 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002151 {
2152 __set_short_size(__sz);
2153 __p = __get_short_pointer();
2154 }
2155 else
2156 {
2157 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002158 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002159 __set_long_pointer(__p);
2160 __set_long_cap(__cap+1);
2161 __set_long_size(__sz);
2162 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002163
2164#ifndef _LIBCPP_NO_EXCEPTIONS
2165 try
2166 {
2167#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002168 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002169 traits_type::assign(*__p, *__first);
2170 traits_type::assign(*__p, value_type());
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002171#ifndef _LIBCPP_NO_EXCEPTIONS
2172 }
2173 catch (...)
2174 {
2175 if (__is_long())
2176 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
2177 throw;
2178 }
2179#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002180}
2181
2182template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002183template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002184inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002185basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002186 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002187{
2188 __init(__first, __last);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002189 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002190}
2191
2192template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002193template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002194inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002195basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
2196 const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002197 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002198{
2199 __init(__first, __last);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002200 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002201}
2202
Eric Fiselierfc92be82017-04-19 00:28:44 +00002203#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002204
Howard Hinnantc51e1022010-05-11 19:42:16 +00002205template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002206inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002207basic_string<_CharT, _Traits, _Allocator>::basic_string(
2208 initializer_list<_CharT> __il)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002209 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002210{
2211 __init(__il.begin(), __il.end());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002212 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002213}
2214
2215template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002216inline
Eric Fiselier9d355982017-04-12 23:45:53 +00002217
Eric Fiselier812882b2017-02-17 01:17:10 +00002218basic_string<_CharT, _Traits, _Allocator>::basic_string(
2219 initializer_list<_CharT> __il, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002220 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002221{
2222 __init(__il.begin(), __il.end());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002223 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002224}
2225
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002226#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002227
Howard Hinnantc51e1022010-05-11 19:42:16 +00002228template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002229basic_string<_CharT, _Traits, _Allocator>::~basic_string()
2230{
Louis Dionneba400782020-10-02 15:02:52 -04002231#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002232 if (!__libcpp_is_constant_evaluated())
2233 __get_db()->__erase_c(this);
Howard Hinnant8ea98242013-08-23 17:37:05 +00002234#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002235 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002236 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002237}
2238
2239template <class _CharT, class _Traits, class _Allocator>
2240void
2241basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
2242 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002243 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 +00002244{
2245 size_type __ms = max_size();
2246 if (__delta_cap > __ms - __old_cap - 1)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002247 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002248 pointer __old_p = __get_pointer();
2249 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002250 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002251 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002252 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002253 __invalidate_all_iterators();
2254 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002255 traits_type::copy(_VSTD::__to_address(__p),
2256 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002257 if (__n_add != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002258 traits_type::copy(_VSTD::__to_address(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002259 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2260 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002261 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2262 _VSTD::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002263 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002264 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002265 __set_long_pointer(__p);
2266 __set_long_cap(__cap+1);
2267 __old_sz = __n_copy + __n_add + __sec_cp_sz;
2268 __set_long_size(__old_sz);
2269 traits_type::assign(__p[__old_sz], value_type());
2270}
2271
2272template <class _CharT, class _Traits, class _Allocator>
2273void
2274basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2275 size_type __n_copy, size_type __n_del, size_type __n_add)
2276{
2277 size_type __ms = max_size();
Marshall Clow2267c5d2013-11-06 14:24:38 +00002278 if (__delta_cap > __ms - __old_cap)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002279 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002280 pointer __old_p = __get_pointer();
2281 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002282 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002283 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002284 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002285 __invalidate_all_iterators();
2286 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002287 traits_type::copy(_VSTD::__to_address(__p),
2288 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002289 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2290 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002291 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2292 _VSTD::__to_address(__old_p) + __n_copy + __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002293 __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002294 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002295 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002296 __set_long_pointer(__p);
2297 __set_long_cap(__cap+1);
2298}
2299
2300// assign
2301
2302template <class _CharT, class _Traits, class _Allocator>
Martijn Vels596e3de2020-02-26 15:55:49 -05002303template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04002304basic_string<_CharT, _Traits, _Allocator>&
2305basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(
Martijn Vels596e3de2020-02-26 15:55:49 -05002306 const value_type* __s, size_type __n) {
Louis Dionne6209e9f2022-03-03 13:39:12 -05002307 size_type __cap = __is_short ? static_cast<size_type>(__min_cap) : __get_long_cap();
Martijn Vels596e3de2020-02-26 15:55:49 -05002308 if (__n < __cap) {
2309 pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
2310 __is_short ? __set_short_size(__n) : __set_long_size(__n);
2311 traits_type::copy(_VSTD::__to_address(__p), __s, __n);
2312 traits_type::assign(__p[__n], value_type());
2313 __invalidate_iterators_past(__n);
2314 } else {
2315 size_type __sz = __is_short ? __get_short_size() : __get_long_size();
2316 __grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
2317 }
Martijn Velsb6a08b62020-04-10 18:36:31 -04002318 return *this;
Martijn Vels596e3de2020-02-26 15:55:49 -05002319}
2320
2321template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002322basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002323basic_string<_CharT, _Traits, _Allocator>::__assign_external(
2324 const value_type* __s, size_type __n) {
2325 size_type __cap = capacity();
2326 if (__cap >= __n) {
2327 value_type* __p = _VSTD::__to_address(__get_pointer());
2328 traits_type::move(__p, __s, __n);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002329 return __null_terminate_at(__p, __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04002330 } else {
2331 size_type __sz = size();
2332 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002333 return *this;
Martijn Velsda7d94f2020-06-19 14:24:03 -04002334 }
Martijn Velsda7d94f2020-06-19 14:24:03 -04002335}
2336
2337template <class _CharT, class _Traits, class _Allocator>
2338basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002339basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002340{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002341 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Nikolas Klauser93826d12022-01-04 17:24:03 +01002342 return (__builtin_constant_p(__n) && __fits_in_sso(__n))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002343 ? __assign_short(__s, __n)
2344 : __assign_external(__s, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002345}
2346
2347template <class _CharT, class _Traits, class _Allocator>
2348basic_string<_CharT, _Traits, _Allocator>&
2349basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2350{
2351 size_type __cap = capacity();
2352 if (__cap < __n)
2353 {
2354 size_type __sz = size();
2355 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2356 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002357 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002358 traits_type::assign(__p, __n, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002359 return __null_terminate_at(__p, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002360}
2361
2362template <class _CharT, class _Traits, class _Allocator>
2363basic_string<_CharT, _Traits, _Allocator>&
2364basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2365{
2366 pointer __p;
2367 if (__is_long())
2368 {
2369 __p = __get_long_pointer();
2370 __set_long_size(1);
2371 }
2372 else
2373 {
2374 __p = __get_short_pointer();
2375 __set_short_size(1);
2376 }
2377 traits_type::assign(*__p, __c);
2378 traits_type::assign(*++__p, value_type());
2379 __invalidate_iterators_past(1);
2380 return *this;
2381}
2382
2383template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002384basic_string<_CharT, _Traits, _Allocator>&
2385basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2386{
Martijn Vels596e3de2020-02-26 15:55:49 -05002387 if (this != &__str) {
2388 __copy_assign_alloc(__str);
2389 if (!__is_long()) {
2390 if (!__str.__is_long()) {
Eric Fiselier03bbc922020-01-15 17:27:10 -05002391 __r_.first().__r = __str.__r_.first().__r;
Martijn Vels596e3de2020-02-26 15:55:49 -05002392 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002393 return __assign_no_alias<true>(__str.data(), __str.size());
Martijn Vels596e3de2020-02-26 15:55:49 -05002394 }
2395 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002396 return __assign_no_alias<false>(__str.data(), __str.size());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002397 }
Martijn Vels596e3de2020-02-26 15:55:49 -05002398 }
2399 return *this;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002400}
2401
Eric Fiselierfc92be82017-04-19 00:28:44 +00002402#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002403
2404template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002405inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002406void
2407basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002408 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002409{
2410 if (__alloc() != __str.__alloc())
2411 assign(__str);
2412 else
2413 __move_assign(__str, true_type());
2414}
2415
2416template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002417inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002418void
2419basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002420#if _LIBCPP_STD_VER > 14
2421 _NOEXCEPT
2422#else
Howard Hinnant3e276872011-06-03 18:40:47 +00002423 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002424#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002425{
marshall2ed41622019-11-27 07:13:00 -08002426 if (__is_long()) {
2427 __alloc_traits::deallocate(__alloc(), __get_long_pointer(),
2428 __get_long_cap());
2429#if _LIBCPP_STD_VER <= 14
2430 if (!is_nothrow_move_assignable<allocator_type>::value) {
2431 __set_short_size(0);
2432 traits_type::assign(__get_short_pointer()[0], value_type());
2433 }
2434#endif
2435 }
2436 __move_assign_alloc(__str);
2437 __r_.first() = __str.__r_.first();
2438 __str.__set_short_size(0);
2439 traits_type::assign(__str.__get_short_pointer()[0], value_type());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002440}
2441
2442template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002443inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002444basic_string<_CharT, _Traits, _Allocator>&
2445basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002446 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002447{
2448 __move_assign(__str, integral_constant<bool,
2449 __alloc_traits::propagate_on_container_move_assignment::value>());
2450 return *this;
2451}
2452
2453#endif
2454
2455template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002456template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002457__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002458<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002459 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002460 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002461>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002462basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2463{
Marshall Clow958362f2016-09-05 01:54:30 +00002464 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002465 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002466 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002467}
2468
2469template <class _CharT, class _Traits, class _Allocator>
2470template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002471__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002472<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002473 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002474 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002475>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002476basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2477{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002478 size_type __cap = capacity();
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002479 size_type __n = __string_is_trivial_iterator<_ForwardIterator>::value ?
2480 static_cast<size_type>(_VSTD::distance(__first, __last)) : 0;
2481
2482 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2483 (__cap >= __n || !__addr_in_range(*__first)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002484 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002485 if (__cap < __n)
2486 {
2487 size_type __sz = size();
2488 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2489 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002490 pointer __p = __get_pointer();
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002491 for (; __first != __last; ++__p, (void) ++__first)
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002492 traits_type::assign(*__p, *__first);
2493 traits_type::assign(*__p, value_type());
2494 __set_size(__n);
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04002495 __invalidate_iterators_past(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002496 }
2497 else
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002498 {
2499 const basic_string __temp(__first, __last, __alloc());
2500 assign(__temp.data(), __temp.size());
2501 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002502 return *this;
2503}
2504
2505template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002506basic_string<_CharT, _Traits, _Allocator>&
2507basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2508{
2509 size_type __sz = __str.size();
2510 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002511 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002512 return assign(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002513}
2514
2515template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002516template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002517__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002518<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002519 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
2520 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00002521 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002522>
Marshall Clow82513342016-09-24 22:45:42 +00002523basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002524{
Marshall Clow82513342016-09-24 22:45:42 +00002525 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002526 size_type __sz = __sv.size();
2527 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002528 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002529 return assign(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2530}
2531
2532
2533template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002534basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002535basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s) {
2536 return __assign_external(__s, traits_type::length(__s));
2537}
2538
2539template <class _CharT, class _Traits, class _Allocator>
2540basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002541basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002542{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002543 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Louis Dionne3e0c4332021-08-31 10:49:06 -04002544 return __builtin_constant_p(*__s)
Nikolas Klauser93826d12022-01-04 17:24:03 +01002545 ? (__fits_in_sso(traits_type::length(__s))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002546 ? __assign_short(__s, traits_type::length(__s))
2547 : __assign_external(__s, traits_type::length(__s)))
2548 : __assign_external(__s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002549}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002550// append
2551
2552template <class _CharT, class _Traits, class _Allocator>
2553basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002554basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002555{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002556 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002557 size_type __cap = capacity();
2558 size_type __sz = size();
2559 if (__cap - __sz >= __n)
2560 {
2561 if (__n)
2562 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002563 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002564 traits_type::copy(__p + __sz, __s, __n);
2565 __sz += __n;
2566 __set_size(__sz);
2567 traits_type::assign(__p[__sz], value_type());
2568 }
2569 }
2570 else
2571 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2572 return *this;
2573}
2574
2575template <class _CharT, class _Traits, class _Allocator>
2576basic_string<_CharT, _Traits, _Allocator>&
2577basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2578{
2579 if (__n)
2580 {
2581 size_type __cap = capacity();
2582 size_type __sz = size();
2583 if (__cap - __sz < __n)
2584 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2585 pointer __p = __get_pointer();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002586 traits_type::assign(_VSTD::__to_address(__p) + __sz, __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002587 __sz += __n;
2588 __set_size(__sz);
2589 traits_type::assign(__p[__sz], value_type());
2590 }
2591 return *this;
2592}
2593
2594template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00002595inline void
2596basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n)
2597{
2598 if (__n)
2599 {
2600 size_type __cap = capacity();
2601 size_type __sz = size();
2602 if (__cap - __sz < __n)
2603 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2604 pointer __p = __get_pointer();
2605 __sz += __n;
2606 __set_size(__sz);
2607 traits_type::assign(__p[__sz], value_type());
2608 }
2609}
2610
2611template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002612void
2613basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2614{
Howard Hinnant68bf1812013-04-30 21:44:48 +00002615 bool __is_short = !__is_long();
2616 size_type __cap;
2617 size_type __sz;
2618 if (__is_short)
2619 {
2620 __cap = __min_cap - 1;
2621 __sz = __get_short_size();
2622 }
2623 else
2624 {
2625 __cap = __get_long_cap() - 1;
2626 __sz = __get_long_size();
2627 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002628 if (__sz == __cap)
Howard Hinnant68bf1812013-04-30 21:44:48 +00002629 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002630 __grow_by(__cap, 1, __sz, __sz, 0);
Louis Dionne82d2b2c2021-11-16 11:26:56 -05002631 __is_short = false; // the string is always long after __grow_by
Howard Hinnant68bf1812013-04-30 21:44:48 +00002632 }
2633 pointer __p;
2634 if (__is_short)
2635 {
2636 __p = __get_short_pointer() + __sz;
2637 __set_short_size(__sz+1);
2638 }
2639 else
2640 {
2641 __p = __get_long_pointer() + __sz;
2642 __set_long_size(__sz+1);
2643 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002644 traits_type::assign(*__p, __c);
2645 traits_type::assign(*++__p, value_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002646}
2647
Howard Hinnantc51e1022010-05-11 19:42:16 +00002648template <class _CharT, class _Traits, class _Allocator>
2649template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002650__enable_if_t
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002651<
2652 __is_cpp17_forward_iterator<_ForwardIterator>::value,
2653 basic_string<_CharT, _Traits, _Allocator>&
2654>
2655basic_string<_CharT, _Traits, _Allocator>::append(
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002656 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002657{
2658 size_type __sz = size();
2659 size_type __cap = capacity();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002660 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002661 if (__n)
2662 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002663 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2664 !__addr_in_range(*__first))
Marshall Clow958362f2016-09-05 01:54:30 +00002665 {
2666 if (__cap - __sz < __n)
2667 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2668 pointer __p = __get_pointer() + __sz;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002669 for (; __first != __last; ++__p, (void) ++__first)
Marshall Clow958362f2016-09-05 01:54:30 +00002670 traits_type::assign(*__p, *__first);
2671 traits_type::assign(*__p, value_type());
2672 __set_size(__sz + __n);
2673 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002674 else
2675 {
2676 const basic_string __temp(__first, __last, __alloc());
2677 append(__temp.data(), __temp.size());
2678 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002679 }
2680 return *this;
2681}
2682
2683template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002684inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002685basic_string<_CharT, _Traits, _Allocator>&
2686basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2687{
2688 return append(__str.data(), __str.size());
2689}
2690
2691template <class _CharT, class _Traits, class _Allocator>
2692basic_string<_CharT, _Traits, _Allocator>&
2693basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2694{
2695 size_type __sz = __str.size();
2696 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002697 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002698 return append(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002699}
2700
2701template <class _CharT, class _Traits, class _Allocator>
Marshall Clow62953962016-10-03 23:40:48 +00002702template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002703 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002704 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002705 __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 +00002706 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002707 >
Marshall Clow82513342016-09-24 22:45:42 +00002708basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002709{
Marshall Clow82513342016-09-24 22:45:42 +00002710 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002711 size_type __sz = __sv.size();
2712 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002713 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002714 return append(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2715}
2716
2717template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002718basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002719basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002720{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002721 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002722 return append(__s, traits_type::length(__s));
2723}
2724
2725// insert
2726
2727template <class _CharT, class _Traits, class _Allocator>
2728basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002729basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002730{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002731 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002732 size_type __sz = size();
2733 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002734 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002735 size_type __cap = capacity();
2736 if (__cap - __sz >= __n)
2737 {
2738 if (__n)
2739 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002740 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002741 size_type __n_move = __sz - __pos;
2742 if (__n_move != 0)
2743 {
2744 if (__p + __pos <= __s && __s < __p + __sz)
2745 __s += __n;
2746 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2747 }
2748 traits_type::move(__p + __pos, __s, __n);
2749 __sz += __n;
2750 __set_size(__sz);
2751 traits_type::assign(__p[__sz], value_type());
2752 }
2753 }
2754 else
2755 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2756 return *this;
2757}
2758
2759template <class _CharT, class _Traits, class _Allocator>
2760basic_string<_CharT, _Traits, _Allocator>&
2761basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2762{
2763 size_type __sz = size();
2764 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002765 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002766 if (__n)
2767 {
2768 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002769 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002770 if (__cap - __sz >= __n)
2771 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002772 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002773 size_type __n_move = __sz - __pos;
2774 if (__n_move != 0)
2775 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2776 }
2777 else
2778 {
2779 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002780 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002781 }
2782 traits_type::assign(__p + __pos, __n, __c);
2783 __sz += __n;
2784 __set_size(__sz);
2785 traits_type::assign(__p[__sz], value_type());
2786 }
2787 return *this;
2788}
2789
2790template <class _CharT, class _Traits, class _Allocator>
2791template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002792__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002793<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002794 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Marshall Clow039b2f02016-01-13 21:54:34 +00002795 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002796>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002797basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2798{
Nikolas Klausereed25832021-12-15 01:32:30 +01002799 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2800 "string::insert(iterator, range) called with an iterator not"
2801 " referring to this string");
2802 const basic_string __temp(__first, __last, __alloc());
2803 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002804}
2805
2806template <class _CharT, class _Traits, class _Allocator>
2807template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002808__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002809<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002810 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002811 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002812>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002813basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2814{
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002815 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2816 "string::insert(iterator, range) called with an iterator not referring to this string");
Nikolas Klausereed25832021-12-15 01:32:30 +01002817
Howard Hinnantc51e1022010-05-11 19:42:16 +00002818 size_type __ip = static_cast<size_type>(__pos - begin());
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002819 size_type __n = static_cast<size_type>(std::distance(__first, __last));
2820 if (__n == 0)
2821 return begin() + __ip;
2822
2823 if (__string_is_trivial_iterator<_ForwardIterator>::value && !__addr_in_range(*__first))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002824 {
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002825 return __insert_from_safe_copy(__n, __ip, __first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002826 }
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002827 else
2828 {
2829 const basic_string __temp(__first, __last, __alloc());
2830 return __insert_from_safe_copy(__n, __ip, __temp.begin(), __temp.end());
2831 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002832}
2833
2834template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002835inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002836basic_string<_CharT, _Traits, _Allocator>&
2837basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2838{
2839 return insert(__pos1, __str.data(), __str.size());
2840}
2841
2842template <class _CharT, class _Traits, class _Allocator>
2843basic_string<_CharT, _Traits, _Allocator>&
2844basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2845 size_type __pos2, size_type __n)
2846{
2847 size_type __str_sz = __str.size();
2848 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002849 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002850 return insert(__pos1, __str.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002851}
2852
2853template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002854template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002855__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002856<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002857 __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 +00002858 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002859>
Marshall Clow82513342016-09-24 22:45:42 +00002860basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002861 size_type __pos2, size_type __n)
2862{
Marshall Clow82513342016-09-24 22:45:42 +00002863 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002864 size_type __str_sz = __sv.size();
2865 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002866 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002867 return insert(__pos1, __sv.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
2868}
2869
2870template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002871basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002872basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002873{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002874 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002875 return insert(__pos, __s, traits_type::length(__s));
2876}
2877
2878template <class _CharT, class _Traits, class _Allocator>
2879typename basic_string<_CharT, _Traits, _Allocator>::iterator
2880basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2881{
Louis Dionnef4cc6fb2022-02-15 15:47:45 -05002882 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2883 "string::insert(iterator, character) called with an iterator not"
2884 " referring to this string");
2885
Howard Hinnantc51e1022010-05-11 19:42:16 +00002886 size_type __ip = static_cast<size_type>(__pos - begin());
2887 size_type __sz = size();
2888 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002889 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002890 if (__cap == __sz)
2891 {
2892 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002893 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002894 }
2895 else
2896 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002897 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002898 size_type __n_move = __sz - __ip;
2899 if (__n_move != 0)
2900 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2901 }
2902 traits_type::assign(__p[__ip], __c);
2903 traits_type::assign(__p[++__sz], value_type());
2904 __set_size(__sz);
2905 return begin() + static_cast<difference_type>(__ip);
2906}
2907
2908template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002909inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002910typename basic_string<_CharT, _Traits, _Allocator>::iterator
2911basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2912{
Nikolas Klausereed25832021-12-15 01:32:30 +01002913 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2914 "string::insert(iterator, n, value) called with an iterator not"
2915 " referring to this string");
2916 difference_type __p = __pos - begin();
2917 insert(static_cast<size_type>(__p), __n, __c);
2918 return begin() + __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002919}
2920
2921// replace
2922
2923template <class _CharT, class _Traits, class _Allocator>
2924basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002925basic_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 +00002926 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00002927{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002928 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002929 size_type __sz = size();
2930 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002931 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002932 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002933 size_type __cap = capacity();
2934 if (__cap - __sz + __n1 >= __n2)
2935 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002936 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002937 if (__n1 != __n2)
2938 {
2939 size_type __n_move = __sz - __pos - __n1;
2940 if (__n_move != 0)
2941 {
2942 if (__n1 > __n2)
2943 {
2944 traits_type::move(__p + __pos, __s, __n2);
2945 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002946 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002947 }
2948 if (__p + __pos < __s && __s < __p + __sz)
2949 {
2950 if (__p + __pos + __n1 <= __s)
2951 __s += __n2 - __n1;
2952 else // __p + __pos < __s < __p + __pos + __n1
2953 {
2954 traits_type::move(__p + __pos, __s, __n1);
2955 __pos += __n1;
2956 __s += __n2;
2957 __n2 -= __n1;
2958 __n1 = 0;
2959 }
2960 }
2961 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2962 }
2963 }
2964 traits_type::move(__p + __pos, __s, __n2);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002965 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002966 }
2967 else
2968 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
2969 return *this;
2970}
2971
2972template <class _CharT, class _Traits, class _Allocator>
2973basic_string<_CharT, _Traits, _Allocator>&
2974basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
2975{
2976 size_type __sz = size();
2977 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002978 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002979 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002980 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002981 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002982 if (__cap - __sz + __n1 >= __n2)
2983 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002984 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002985 if (__n1 != __n2)
2986 {
2987 size_type __n_move = __sz - __pos - __n1;
2988 if (__n_move != 0)
2989 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2990 }
2991 }
2992 else
2993 {
2994 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002995 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002996 }
2997 traits_type::assign(__p + __pos, __n2, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002998 return __null_terminate_at(__p, __sz - (__n1 - __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002999}
3000
3001template <class _CharT, class _Traits, class _Allocator>
3002template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003003__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00003004<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003005 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003006 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003007>
Howard Hinnant990d6e82010-11-17 21:11:40 +00003008basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003009 _InputIterator __j1, _InputIterator __j2)
3010{
Marshall Clow958362f2016-09-05 01:54:30 +00003011 const basic_string __temp(__j1, __j2, __alloc());
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003012 return replace(__i1, __i2, __temp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003013}
3014
3015template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003016inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003017basic_string<_CharT, _Traits, _Allocator>&
3018basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
3019{
3020 return replace(__pos1, __n1, __str.data(), __str.size());
3021}
3022
3023template <class _CharT, class _Traits, class _Allocator>
3024basic_string<_CharT, _Traits, _Allocator>&
3025basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
3026 size_type __pos2, size_type __n2)
3027{
3028 size_type __str_sz = __str.size();
3029 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003030 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003031 return replace(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003032}
3033
3034template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003035template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003036__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003037<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003038 __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 +00003039 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003040>
Marshall Clow82513342016-09-24 22:45:42 +00003041basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003042 size_type __pos2, size_type __n2)
3043{
Marshall Clow82513342016-09-24 22:45:42 +00003044 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003045 size_type __str_sz = __sv.size();
3046 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003047 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003048 return replace(__pos1, __n1, __sv.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
3049}
3050
3051template <class _CharT, class _Traits, class _Allocator>
3052basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003053basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003054{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003055 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003056 return replace(__pos, __n1, __s, traits_type::length(__s));
3057}
3058
3059template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003060inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003061basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003062basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003063{
3064 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
3065 __str.data(), __str.size());
3066}
3067
3068template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003069inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003070basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003071basic_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 +00003072{
3073 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
3074}
3075
3076template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003077inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003078basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003079basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003080{
3081 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
3082}
3083
3084template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003085inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003086basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003087basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003088{
3089 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
3090}
3091
3092// erase
3093
Martijn Velsa81fc792020-02-26 13:25:43 -05003094// 'externally instantiated' erase() implementation, called when __n != npos.
3095// Does not check __pos against size()
Howard Hinnantc51e1022010-05-11 19:42:16 +00003096template <class _CharT, class _Traits, class _Allocator>
Martijn Velsa81fc792020-02-26 13:25:43 -05003097void
3098basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(
3099 size_type __pos, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003100{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003101 if (__n)
3102 {
Martijn Velsa81fc792020-02-26 13:25:43 -05003103 size_type __sz = size();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003104 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003105 __n = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003106 size_type __n_move = __sz - __pos - __n;
3107 if (__n_move != 0)
3108 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003109 __null_terminate_at(__p, __sz - __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003110 }
Martijn Velsa81fc792020-02-26 13:25:43 -05003111}
3112
3113template <class _CharT, class _Traits, class _Allocator>
3114basic_string<_CharT, _Traits, _Allocator>&
3115basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos,
3116 size_type __n) {
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003117 if (__pos > size())
3118 __throw_out_of_range();
Martijn Velsa81fc792020-02-26 13:25:43 -05003119 if (__n == npos) {
3120 __erase_to_end(__pos);
3121 } else {
3122 __erase_external_with_move(__pos, __n);
3123 }
3124 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003125}
3126
3127template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003128inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003129typename basic_string<_CharT, _Traits, _Allocator>::iterator
3130basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
3131{
Nikolas Klausereed25832021-12-15 01:32:30 +01003132 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3133 "string::erase(iterator) called with an iterator not"
3134 " referring to this string");
3135
3136 _LIBCPP_ASSERT(__pos != end(), "string::erase(iterator) called with a non-dereferenceable iterator");
3137 iterator __b = begin();
3138 size_type __r = static_cast<size_type>(__pos - __b);
3139 erase(__r, 1);
3140 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003141}
3142
3143template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003144inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003145typename basic_string<_CharT, _Traits, _Allocator>::iterator
3146basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
3147{
Nikolas Klausereed25832021-12-15 01:32:30 +01003148 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
3149 "string::erase(iterator, iterator) called with an iterator not"
3150 " referring to this string");
3151
3152 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
3153 iterator __b = begin();
3154 size_type __r = static_cast<size_type>(__first - __b);
3155 erase(__r, static_cast<size_type>(__last - __first));
3156 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003157}
3158
3159template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003160inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003161void
3162basic_string<_CharT, _Traits, _Allocator>::pop_back()
3163{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003164 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003165 __erase_to_end(size() - 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003166}
3167
3168template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003169inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003170void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003171basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003172{
3173 __invalidate_all_iterators();
3174 if (__is_long())
3175 {
3176 traits_type::assign(*__get_long_pointer(), value_type());
3177 __set_long_size(0);
3178 }
3179 else
3180 {
3181 traits_type::assign(*__get_short_pointer(), value_type());
3182 __set_short_size(0);
3183 }
3184}
3185
3186template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003187inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003188void
3189basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
3190{
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003191 __null_terminate_at(_VSTD::__to_address(__get_pointer()), __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003192}
3193
3194template <class _CharT, class _Traits, class _Allocator>
3195void
3196basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
3197{
3198 size_type __sz = size();
3199 if (__n > __sz)
3200 append(__n - __sz, __c);
3201 else
3202 __erase_to_end(__n);
3203}
3204
3205template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00003206inline void
3207basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n)
3208{
3209 size_type __sz = size();
3210 if (__n > __sz) {
3211 __append_default_init(__n - __sz);
3212 } else
3213 __erase_to_end(__n);
3214}
3215
3216template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003217inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003218typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003219basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003220{
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003221 size_type __m = __alloc_traits::max_size(__alloc());
Eric Fiseliere9cc5922017-10-17 13:16:01 +00003222#ifdef _LIBCPP_BIG_ENDIAN
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003223 return (__m <= ~__long_mask ? __m : __m/2) - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003224#else
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003225 return __m - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003226#endif
3227}
3228
3229template <class _CharT, class _Traits, class _Allocator>
3230void
Marek Kurdejc9848142020-11-26 10:07:16 +01003231basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __requested_capacity)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003232{
Marek Kurdejc9848142020-11-26 10:07:16 +01003233 if (__requested_capacity > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003234 __throw_length_error();
Marek Kurdejc9848142020-11-26 10:07:16 +01003235
Louis Dionne05b6d3e2022-01-14 12:30:22 -05003236 // Make sure reserve(n) never shrinks. This is technically only required in C++20
3237 // and later (since P0966R1), however we provide consistent behavior in all Standard
3238 // modes because this function is instantiated in the shared library.
3239 if (__requested_capacity <= capacity())
3240 return;
Marek Kurdejc9848142020-11-26 10:07:16 +01003241
3242 size_type __target_capacity = _VSTD::max(__requested_capacity, size());
3243 __target_capacity = __recommend(__target_capacity);
3244 if (__target_capacity == capacity()) return;
3245
3246 __shrink_or_extend(__target_capacity);
3247}
3248
3249template <class _CharT, class _Traits, class _Allocator>
Louis Dionnee38b8642021-12-13 14:15:21 -05003250inline
Marek Kurdejc9848142020-11-26 10:07:16 +01003251void
3252basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT
3253{
3254 size_type __target_capacity = __recommend(size());
3255 if (__target_capacity == capacity()) return;
3256
3257 __shrink_or_extend(__target_capacity);
3258}
3259
3260template <class _CharT, class _Traits, class _Allocator>
Louis Dionnee38b8642021-12-13 14:15:21 -05003261inline
Marek Kurdejc9848142020-11-26 10:07:16 +01003262void
3263basic_string<_CharT, _Traits, _Allocator>::__shrink_or_extend(size_type __target_capacity)
3264{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003265 size_type __cap = capacity();
3266 size_type __sz = size();
Marek Kurdejc9848142020-11-26 10:07:16 +01003267
3268 pointer __new_data, __p;
3269 bool __was_long, __now_long;
3270 if (__target_capacity == __min_cap - 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003271 {
Marek Kurdejc9848142020-11-26 10:07:16 +01003272 __was_long = true;
3273 __now_long = false;
3274 __new_data = __get_short_pointer();
3275 __p = __get_long_pointer();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003276 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003277 else
3278 {
3279 if (__target_capacity > __cap)
3280 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3281 else
3282 {
3283 #ifndef _LIBCPP_NO_EXCEPTIONS
3284 try
3285 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003286 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003287 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3288 #ifndef _LIBCPP_NO_EXCEPTIONS
3289 }
3290 catch (...)
3291 {
3292 return;
3293 }
3294 #else // _LIBCPP_NO_EXCEPTIONS
3295 if (__new_data == nullptr)
3296 return;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003297 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003298 }
3299 __now_long = true;
3300 __was_long = __is_long();
3301 __p = __get_pointer();
3302 }
3303 traits_type::copy(_VSTD::__to_address(__new_data),
3304 _VSTD::__to_address(__p), size()+1);
3305 if (__was_long)
3306 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
3307 if (__now_long)
3308 {
3309 __set_long_cap(__target_capacity+1);
3310 __set_long_size(__sz);
3311 __set_long_pointer(__new_data);
3312 }
3313 else
3314 __set_short_size(__sz);
3315 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003316}
3317
3318template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003319inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003320typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003321basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003322{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003323 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003324 return *(data() + __pos);
3325}
3326
3327template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003328inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003329typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003330basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003331{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003332 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003333 return *(__get_pointer() + __pos);
3334}
3335
3336template <class _CharT, class _Traits, class _Allocator>
3337typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3338basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3339{
3340 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003341 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003342 return (*this)[__n];
3343}
3344
3345template <class _CharT, class _Traits, class _Allocator>
3346typename basic_string<_CharT, _Traits, _Allocator>::reference
3347basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
3348{
3349 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003350 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003351 return (*this)[__n];
3352}
3353
3354template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003355inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003356typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003357basic_string<_CharT, _Traits, _Allocator>::front() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003358{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003359 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003360 return *__get_pointer();
3361}
3362
3363template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003364inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003365typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003366basic_string<_CharT, _Traits, _Allocator>::front() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003367{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003368 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003369 return *data();
3370}
3371
3372template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003373inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003374typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003375basic_string<_CharT, _Traits, _Allocator>::back() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003376{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003377 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003378 return *(__get_pointer() + size() - 1);
3379}
3380
3381template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003382inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003383typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003384basic_string<_CharT, _Traits, _Allocator>::back() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003385{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003386 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003387 return *(data() + size() - 1);
3388}
3389
3390template <class _CharT, class _Traits, class _Allocator>
3391typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003392basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003393{
3394 size_type __sz = size();
3395 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003396 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003397 size_type __rlen = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003398 traits_type::copy(__s, data() + __pos, __rlen);
3399 return __rlen;
3400}
3401
3402template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003403inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003404basic_string<_CharT, _Traits, _Allocator>
3405basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3406{
3407 return basic_string(*this, __pos, __n, __alloc());
3408}
3409
3410template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003411inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003412void
3413basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003414#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00003415 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00003416#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00003417 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003418 __is_nothrow_swappable<allocator_type>::value)
3419#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003420{
Louis Dionneba400782020-10-02 15:02:52 -04003421#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01003422 if (!__libcpp_is_constant_evaluated()) {
3423 if (!__is_long())
3424 __get_db()->__invalidate_all(this);
3425 if (!__str.__is_long())
3426 __get_db()->__invalidate_all(&__str);
3427 __get_db()->swap(this, &__str);
3428 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00003429#endif
Eric Fiselier9bf691f2016-12-28 05:53:01 +00003430 _LIBCPP_ASSERT(
3431 __alloc_traits::propagate_on_container_swap::value ||
3432 __alloc_traits::is_always_equal::value ||
3433 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003434 _VSTD::swap(__r_.first(), __str.__r_.first());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05003435 _VSTD::__swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003436}
3437
3438// find
3439
3440template <class _Traits>
3441struct _LIBCPP_HIDDEN __traits_eq
3442{
3443 typedef typename _Traits::char_type char_type;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003444 _LIBCPP_INLINE_VISIBILITY
3445 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3446 {return _Traits::eq(__x, __y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003447};
3448
3449template<class _CharT, class _Traits, class _Allocator>
3450typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003451basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003452 size_type __pos,
3453 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003454{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003455 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003456 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003457 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003458}
3459
3460template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003461inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003462typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003463basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3464 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003465{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003466 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003467 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003468}
3469
3470template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003471template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003472__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003473<
3474 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3475 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003476>
Marshall Clowe46031a2018-07-02 18:41:15 +00003477basic_string<_CharT, _Traits, _Allocator>::find(const _Tp &__t,
zoecarver1997e0a2021-02-05 11:54:47 -08003478 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003479{
Marshall Clowe46031a2018-07-02 18:41:15 +00003480 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003481 return __str_find<value_type, size_type, traits_type, npos>
3482 (data(), size(), __sv.data(), __pos, __sv.size());
3483}
3484
3485template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003486inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003487typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003488basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003489 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003490{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003491 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003492 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003493 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003494}
3495
3496template<class _CharT, class _Traits, class _Allocator>
3497typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003498basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3499 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003500{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003501 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003502 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003503}
3504
3505// rfind
3506
3507template<class _CharT, class _Traits, class _Allocator>
3508typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003509basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003510 size_type __pos,
3511 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003512{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003513 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003514 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003515 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003516}
3517
3518template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003519inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003520typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003521basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3522 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003523{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003524 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003525 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003526}
3527
3528template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003529template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003530__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003531<
3532 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3533 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003534>
Marshall Clowe46031a2018-07-02 18:41:15 +00003535basic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003536 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003537{
Marshall Clowe46031a2018-07-02 18:41:15 +00003538 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003539 return __str_rfind<value_type, size_type, traits_type, npos>
3540 (data(), size(), __sv.data(), __pos, __sv.size());
3541}
3542
3543template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003544inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003545typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003546basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003547 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003548{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003549 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003550 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003551 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003552}
3553
3554template<class _CharT, class _Traits, class _Allocator>
3555typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003556basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3557 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003558{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003559 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003560 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003561}
3562
3563// find_first_of
3564
3565template<class _CharT, class _Traits, class _Allocator>
3566typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003567basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003568 size_type __pos,
3569 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003570{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003571 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003572 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003573 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003574}
3575
3576template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003577inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003578typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003579basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3580 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003581{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003582 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003583 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003584}
3585
3586template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003587template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003588__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003589<
3590 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3591 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003592>
Marshall Clowe46031a2018-07-02 18:41:15 +00003593basic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003594 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003595{
Marshall Clowe46031a2018-07-02 18:41:15 +00003596 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003597 return __str_find_first_of<value_type, size_type, traits_type, npos>
3598 (data(), size(), __sv.data(), __pos, __sv.size());
3599}
3600
3601template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003602inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003603typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003604basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003605 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003606{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003607 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003608 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003609 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003610}
3611
3612template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003613inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003614typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003615basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3616 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003617{
3618 return find(__c, __pos);
3619}
3620
3621// find_last_of
3622
3623template<class _CharT, class _Traits, class _Allocator>
3624typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003625basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003626 size_type __pos,
3627 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003628{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003629 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003630 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003631 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003632}
3633
3634template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003635inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003636typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003637basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3638 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003639{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003640 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003641 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003642}
3643
3644template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003645template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003646__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003647<
3648 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3649 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003650>
Marshall Clowe46031a2018-07-02 18:41:15 +00003651basic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003652 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003653{
Marshall Clowe46031a2018-07-02 18:41:15 +00003654 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003655 return __str_find_last_of<value_type, size_type, traits_type, npos>
3656 (data(), size(), __sv.data(), __pos, __sv.size());
3657}
3658
3659template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003660inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003661typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003662basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003663 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003664{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003665 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003666 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003667 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003668}
3669
3670template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003671inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003672typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003673basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3674 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003675{
3676 return rfind(__c, __pos);
3677}
3678
3679// find_first_not_of
3680
3681template<class _CharT, class _Traits, class _Allocator>
3682typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003683basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003684 size_type __pos,
3685 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003686{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003687 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003688 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003689 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003690}
3691
3692template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003693inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003694typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003695basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3696 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003697{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003698 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003699 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003700}
3701
3702template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003703template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003704__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003705<
3706 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3707 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003708>
Marshall Clowe46031a2018-07-02 18:41:15 +00003709basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003710 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003711{
Marshall Clowe46031a2018-07-02 18:41:15 +00003712 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003713 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3714 (data(), size(), __sv.data(), __pos, __sv.size());
3715}
3716
3717template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003718inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003719typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003720basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003721 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003722{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003723 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003724 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003725 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003726}
3727
3728template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003729inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003730typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003731basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3732 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003733{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003734 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003735 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003736}
3737
3738// find_last_not_of
3739
3740template<class _CharT, class _Traits, class _Allocator>
3741typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003742basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003743 size_type __pos,
3744 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003745{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003746 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003747 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003748 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003749}
3750
3751template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003752inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003753typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003754basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3755 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003756{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003757 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003758 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003759}
3760
3761template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003762template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003763__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003764<
3765 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3766 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003767>
Marshall Clowe46031a2018-07-02 18:41:15 +00003768basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003769 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003770{
Marshall Clowe46031a2018-07-02 18:41:15 +00003771 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003772 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3773 (data(), size(), __sv.data(), __pos, __sv.size());
3774}
3775
3776template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003777inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003778typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003779basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003780 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003781{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003782 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003783 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003784 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003785}
3786
3787template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003788inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003789typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003790basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3791 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003792{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003793 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003794 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003795}
3796
3797// compare
3798
3799template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003800template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003801__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003802<
3803 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3804 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003805>
zoecarver1997e0a2021-02-05 11:54:47 -08003806basic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003807{
Marshall Clowe46031a2018-07-02 18:41:15 +00003808 __self_view __sv = __t;
Howard Hinnantb0485532011-07-24 21:45:06 +00003809 size_t __lhs_sz = size();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003810 size_t __rhs_sz = __sv.size();
3811 int __result = traits_type::compare(data(), __sv.data(),
Howard Hinnantb0485532011-07-24 21:45:06 +00003812 _VSTD::min(__lhs_sz, __rhs_sz));
3813 if (__result != 0)
3814 return __result;
3815 if (__lhs_sz < __rhs_sz)
3816 return -1;
3817 if (__lhs_sz > __rhs_sz)
3818 return 1;
3819 return 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003820}
3821
3822template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003823inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003824int
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003825basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003826{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003827 return compare(__self_view(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003828}
3829
3830template <class _CharT, class _Traits, class _Allocator>
3831int
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003832basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3833 size_type __n1,
Howard Hinnantd17880b2013-06-28 16:59:19 +00003834 const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003835 size_type __n2) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003836{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003837 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003838 size_type __sz = size();
3839 if (__pos1 > __sz || __n2 == npos)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003840 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003841 size_type __rlen = _VSTD::min(__n1, __sz - __pos1);
3842 int __r = traits_type::compare(data() + __pos1, __s, _VSTD::min(__rlen, __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003843 if (__r == 0)
3844 {
3845 if (__rlen < __n2)
3846 __r = -1;
3847 else if (__rlen > __n2)
3848 __r = 1;
3849 }
3850 return __r;
3851}
3852
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003853template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003854template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003855__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003856<
3857 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3858 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003859>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003860basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3861 size_type __n1,
Marshall Clowe46031a2018-07-02 18:41:15 +00003862 const _Tp& __t) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003863{
Marshall Clowe46031a2018-07-02 18:41:15 +00003864 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003865 return compare(__pos1, __n1, __sv.data(), __sv.size());
3866}
3867
3868template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003869inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003870int
3871basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3872 size_type __n1,
3873 const basic_string& __str) const
3874{
3875 return compare(__pos1, __n1, __str.data(), __str.size());
3876}
3877
3878template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003879template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003880__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003881<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003882 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
3883 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00003884 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003885>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003886basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3887 size_type __n1,
Marshall Clow82513342016-09-24 22:45:42 +00003888 const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003889 size_type __pos2,
3890 size_type __n2) const
3891{
Marshall Clow82513342016-09-24 22:45:42 +00003892 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003893 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3894}
3895
3896template <class _CharT, class _Traits, class _Allocator>
3897int
3898basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3899 size_type __n1,
3900 const basic_string& __str,
3901 size_type __pos2,
3902 size_type __n2) const
3903{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003904 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003905}
3906
3907template <class _CharT, class _Traits, class _Allocator>
3908int
3909basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
3910{
3911 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3912 return compare(0, npos, __s, traits_type::length(__s));
3913}
3914
3915template <class _CharT, class _Traits, class _Allocator>
3916int
3917basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3918 size_type __n1,
3919 const value_type* __s) const
3920{
3921 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3922 return compare(__pos1, __n1, __s, traits_type::length(__s));
3923}
3924
Howard Hinnantc51e1022010-05-11 19:42:16 +00003925// __invariants
3926
3927template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003928inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003929bool
3930basic_string<_CharT, _Traits, _Allocator>::__invariants() const
3931{
3932 if (size() > capacity())
3933 return false;
3934 if (capacity() < __min_cap - 1)
3935 return false;
Bruce Mitchener170d8972020-11-24 12:53:53 -05003936 if (data() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003937 return false;
Louis Dionne663415f2020-10-05 16:16:13 -04003938 if (data()[size()] != value_type())
Howard Hinnantc51e1022010-05-11 19:42:16 +00003939 return false;
3940 return true;
3941}
3942
Vedant Kumar55e007e2018-03-08 21:15:26 +00003943// __clear_and_shrink
3944
3945template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003946inline
Louis Dionne173f29e2019-05-29 16:01:36 +00003947void
Marshall Clowe60a7182018-05-29 17:04:37 +00003948basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
Vedant Kumar55e007e2018-03-08 21:15:26 +00003949{
3950 clear();
3951 if(__is_long())
3952 {
3953 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
3954 __set_long_cap(0);
3955 __set_short_size(0);
Louis Dionne663415f2020-10-05 16:16:13 -04003956 traits_type::assign(*__get_short_pointer(), value_type());
Vedant Kumar55e007e2018-03-08 21:15:26 +00003957 }
Louis Dionne173f29e2019-05-29 16:01:36 +00003958}
Vedant Kumar55e007e2018-03-08 21:15:26 +00003959
Howard Hinnantc51e1022010-05-11 19:42:16 +00003960// operator==
3961
3962template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003963inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003964bool
3965operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003966 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003967{
Howard Hinnantaaeb1132013-04-22 23:55:13 +00003968 size_t __lhs_sz = __lhs.size();
3969 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
3970 __rhs.data(),
3971 __lhs_sz) == 0;
3972}
3973
3974template<class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003975inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaaeb1132013-04-22 23:55:13 +00003976bool
3977operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
3978 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
3979{
3980 size_t __lhs_sz = __lhs.size();
3981 if (__lhs_sz != __rhs.size())
3982 return false;
3983 const char* __lp = __lhs.data();
3984 const char* __rp = __rhs.data();
3985 if (__lhs.__is_long())
3986 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
3987 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
3988 if (*__lp != *__rp)
3989 return false;
3990 return true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003991}
3992
3993template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003994inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003995bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003996operator==(const _CharT* __lhs,
3997 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003998{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00003999 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4000 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
4001 size_t __lhs_len = _Traits::length(__lhs);
4002 if (__lhs_len != __rhs.size()) return false;
4003 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004004}
4005
Howard Hinnantc51e1022010-05-11 19:42:16 +00004006template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004007inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004008bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004009operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
4010 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004011{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004012 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4013 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
4014 size_t __rhs_len = _Traits::length(__rhs);
4015 if (__rhs_len != __lhs.size()) return false;
4016 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004017}
4018
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004019template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004020inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004021bool
4022operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004023 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004024{
4025 return !(__lhs == __rhs);
4026}
4027
4028template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004029inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004030bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004031operator!=(const _CharT* __lhs,
4032 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004033{
4034 return !(__lhs == __rhs);
4035}
4036
4037template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004038inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004039bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004040operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4041 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004042{
4043 return !(__lhs == __rhs);
4044}
4045
4046// operator<
4047
4048template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004049inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004050bool
4051operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004052 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004053{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004054 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004055}
4056
4057template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004058inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004059bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004060operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4061 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004062{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004063 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004064}
4065
4066template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004067inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004068bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004069operator< (const _CharT* __lhs,
4070 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004071{
4072 return __rhs.compare(__lhs) > 0;
4073}
4074
Howard Hinnantc51e1022010-05-11 19:42:16 +00004075// operator>
4076
4077template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004078inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004079bool
4080operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004081 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004082{
4083 return __rhs < __lhs;
4084}
4085
4086template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004087inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004088bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004089operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4090 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004091{
4092 return __rhs < __lhs;
4093}
4094
4095template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004096inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004097bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004098operator> (const _CharT* __lhs,
4099 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004100{
4101 return __rhs < __lhs;
4102}
4103
4104// operator<=
4105
4106template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004107inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004108bool
4109operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004110 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004111{
4112 return !(__rhs < __lhs);
4113}
4114
4115template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004116inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004117bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004118operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4119 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004120{
4121 return !(__rhs < __lhs);
4122}
4123
4124template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004125inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004126bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004127operator<=(const _CharT* __lhs,
4128 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004129{
4130 return !(__rhs < __lhs);
4131}
4132
4133// operator>=
4134
4135template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004136inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004137bool
4138operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004139 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004140{
4141 return !(__lhs < __rhs);
4142}
4143
4144template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004145inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004146bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004147operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4148 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004149{
4150 return !(__lhs < __rhs);
4151}
4152
4153template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004154inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004155bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004156operator>=(const _CharT* __lhs,
4157 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004158{
4159 return !(__lhs < __rhs);
4160}
4161
4162// operator +
4163
4164template<class _CharT, class _Traits, class _Allocator>
4165basic_string<_CharT, _Traits, _Allocator>
4166operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4167 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4168{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004169 using _String = basic_string<_CharT, _Traits, _Allocator>;
4170 _String __r(_String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4171 typename _String::size_type __lhs_sz = __lhs.size();
4172 typename _String::size_type __rhs_sz = __rhs.size();
Howard Hinnantc51e1022010-05-11 19:42:16 +00004173 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4174 __r.append(__rhs.data(), __rhs_sz);
4175 return __r;
4176}
4177
4178template<class _CharT, class _Traits, class _Allocator>
4179basic_string<_CharT, _Traits, _Allocator>
4180operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4181{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004182 using _String = basic_string<_CharT, _Traits, _Allocator>;
4183 _String __r(_String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator()));
4184 typename _String::size_type __lhs_sz = _Traits::length(__lhs);
4185 typename _String::size_type __rhs_sz = __rhs.size();
Howard Hinnantc51e1022010-05-11 19:42:16 +00004186 __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz);
4187 __r.append(__rhs.data(), __rhs_sz);
4188 return __r;
4189}
4190
4191template<class _CharT, class _Traits, class _Allocator>
4192basic_string<_CharT, _Traits, _Allocator>
4193operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4194{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004195 using _String = basic_string<_CharT, _Traits, _Allocator>;
4196 _String __r(_String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator()));
4197 typename _String::size_type __rhs_sz = __rhs.size();
Howard Hinnantc51e1022010-05-11 19:42:16 +00004198 __r.__init(&__lhs, 1, 1 + __rhs_sz);
4199 __r.append(__rhs.data(), __rhs_sz);
4200 return __r;
4201}
4202
4203template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004204inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004205basic_string<_CharT, _Traits, _Allocator>
4206operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
4207{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004208 using _String = basic_string<_CharT, _Traits, _Allocator>;
4209 _String __r(_String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4210 typename _String::size_type __lhs_sz = __lhs.size();
4211 typename _String::size_type __rhs_sz = _Traits::length(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004212 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4213 __r.append(__rhs, __rhs_sz);
4214 return __r;
4215}
4216
4217template<class _CharT, class _Traits, class _Allocator>
4218basic_string<_CharT, _Traits, _Allocator>
4219operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
4220{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004221 using _String = basic_string<_CharT, _Traits, _Allocator>;
4222 _String __r(_String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4223 typename _String::size_type __lhs_sz = __lhs.size();
Howard Hinnantc51e1022010-05-11 19:42:16 +00004224 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1);
4225 __r.push_back(__rhs);
4226 return __r;
4227}
4228
Eric Fiselierfc92be82017-04-19 00:28:44 +00004229#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004230
4231template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004232inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004233basic_string<_CharT, _Traits, _Allocator>
4234operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4235{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004236 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004237}
4238
4239template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004240inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004241basic_string<_CharT, _Traits, _Allocator>
4242operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4243{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004244 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004245}
4246
4247template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004248inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004249basic_string<_CharT, _Traits, _Allocator>
4250operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4251{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004252 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004253}
4254
4255template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004256inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004257basic_string<_CharT, _Traits, _Allocator>
4258operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4259{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004260 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004261}
4262
4263template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004264inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004265basic_string<_CharT, _Traits, _Allocator>
4266operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4267{
4268 __rhs.insert(__rhs.begin(), __lhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004269 return _VSTD::move(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004270}
4271
4272template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004273inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004274basic_string<_CharT, _Traits, _Allocator>
4275operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
4276{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004277 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004278}
4279
4280template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004281inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004282basic_string<_CharT, _Traits, _Allocator>
4283operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
4284{
4285 __lhs.push_back(__rhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004286 return _VSTD::move(__lhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004287}
4288
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004289#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004290
4291// swap
4292
4293template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004294inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004295void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004296swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +00004297 basic_string<_CharT, _Traits, _Allocator>& __rhs)
4298 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004299{
4300 __lhs.swap(__rhs);
4301}
4302
Bruce Mitchener170d8972020-11-24 12:53:53 -05004303_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = nullptr, int __base = 10);
4304_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = nullptr, int __base = 10);
4305_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = nullptr, int __base = 10);
4306_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = nullptr, int __base = 10);
4307_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004308
Bruce Mitchener170d8972020-11-24 12:53:53 -05004309_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = nullptr);
4310_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = nullptr);
4311_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004312
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004313_LIBCPP_FUNC_VIS string to_string(int __val);
4314_LIBCPP_FUNC_VIS string to_string(unsigned __val);
4315_LIBCPP_FUNC_VIS string to_string(long __val);
4316_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
4317_LIBCPP_FUNC_VIS string to_string(long long __val);
4318_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
4319_LIBCPP_FUNC_VIS string to_string(float __val);
4320_LIBCPP_FUNC_VIS string to_string(double __val);
4321_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004322
Louis Dionne89258142021-08-23 15:32:36 -04004323#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Bruce Mitchener170d8972020-11-24 12:53:53 -05004324_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4325_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4326_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4327_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4328_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004329
Bruce Mitchener170d8972020-11-24 12:53:53 -05004330_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = nullptr);
4331_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = nullptr);
4332_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004333
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004334_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
4335_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
4336_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
4337_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
4338_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
4339_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
4340_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
4341_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
4342_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Louis Dionne89258142021-08-23 15:32:36 -04004343#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004344
Howard Hinnantc51e1022010-05-11 19:42:16 +00004345template<class _CharT, class _Traits, class _Allocator>
Martin Storsjö88890c22021-02-22 01:13:13 +02004346_LIBCPP_TEMPLATE_DATA_VIS
Eric Fiselierc9fdaf12020-01-15 17:02:17 -05004347const typename basic_string<_CharT, _Traits, _Allocator>::size_type
4348 basic_string<_CharT, _Traits, _Allocator>::npos;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004349
Marshall Clow851b9ec2019-05-20 21:56:51 +00004350template <class _CharT, class _Allocator>
4351struct _LIBCPP_TEMPLATE_VIS
4352 hash<basic_string<_CharT, char_traits<_CharT>, _Allocator> >
4353 : public unary_function<
4354 basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004355{
4356 size_t
Marshall Clow851b9ec2019-05-20 21:56:51 +00004357 operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT
4358 { return __do_string_hash(__val.data(), __val.data() + __val.size()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004359};
4360
Howard Hinnantc51e1022010-05-11 19:42:16 +00004361
Howard Hinnantdc095972011-07-18 15:51:59 +00004362template<class _CharT, class _Traits, class _Allocator>
4363basic_ostream<_CharT, _Traits>&
4364operator<<(basic_ostream<_CharT, _Traits>& __os,
4365 const basic_string<_CharT, _Traits, _Allocator>& __str);
4366
4367template<class _CharT, class _Traits, class _Allocator>
4368basic_istream<_CharT, _Traits>&
4369operator>>(basic_istream<_CharT, _Traits>& __is,
4370 basic_string<_CharT, _Traits, _Allocator>& __str);
4371
4372template<class _CharT, class _Traits, class _Allocator>
4373basic_istream<_CharT, _Traits>&
4374getline(basic_istream<_CharT, _Traits>& __is,
4375 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4376
4377template<class _CharT, class _Traits, class _Allocator>
4378inline _LIBCPP_INLINE_VISIBILITY
4379basic_istream<_CharT, _Traits>&
4380getline(basic_istream<_CharT, _Traits>& __is,
4381 basic_string<_CharT, _Traits, _Allocator>& __str);
4382
Howard Hinnantdc095972011-07-18 15:51:59 +00004383template<class _CharT, class _Traits, class _Allocator>
4384inline _LIBCPP_INLINE_VISIBILITY
4385basic_istream<_CharT, _Traits>&
4386getline(basic_istream<_CharT, _Traits>&& __is,
4387 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4388
4389template<class _CharT, class _Traits, class _Allocator>
4390inline _LIBCPP_INLINE_VISIBILITY
4391basic_istream<_CharT, _Traits>&
4392getline(basic_istream<_CharT, _Traits>&& __is,
4393 basic_string<_CharT, _Traits, _Allocator>& __str);
4394
Marshall Clow29b53f22018-12-14 18:49:35 +00004395#if _LIBCPP_STD_VER > 17
Marek Kurdeja98b1412020-05-02 13:58:03 +02004396template <class _CharT, class _Traits, class _Allocator, class _Up>
Marshall Clow29b53f22018-12-14 18:49:35 +00004397inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004398 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4399 erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
4400 auto __old_size = __str.size();
4401 __str.erase(_VSTD::remove(__str.begin(), __str.end(), __v), __str.end());
4402 return __old_size - __str.size();
4403}
Marshall Clow29b53f22018-12-14 18:49:35 +00004404
Marek Kurdeja98b1412020-05-02 13:58:03 +02004405template <class _CharT, class _Traits, class _Allocator, class _Predicate>
Marshall Clow29b53f22018-12-14 18:49:35 +00004406inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004407 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4408 erase_if(basic_string<_CharT, _Traits, _Allocator>& __str,
4409 _Predicate __pred) {
4410 auto __old_size = __str.size();
4411 __str.erase(_VSTD::remove_if(__str.begin(), __str.end(), __pred),
4412 __str.end());
4413 return __old_size - __str.size();
4414}
Marshall Clow29b53f22018-12-14 18:49:35 +00004415#endif
4416
Louis Dionneba400782020-10-02 15:02:52 -04004417#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004418
4419template<class _CharT, class _Traits, class _Allocator>
4420bool
4421basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4422{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004423 return data() <= _VSTD::__to_address(__i->base()) &&
4424 _VSTD::__to_address(__i->base()) < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004425}
4426
4427template<class _CharT, class _Traits, class _Allocator>
4428bool
4429basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4430{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004431 return data() < _VSTD::__to_address(__i->base()) &&
4432 _VSTD::__to_address(__i->base()) <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004433}
4434
4435template<class _CharT, class _Traits, class _Allocator>
4436bool
4437basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4438{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004439 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004440 return data() <= __p && __p <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004441}
4442
4443template<class _CharT, class _Traits, class _Allocator>
4444bool
4445basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4446{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004447 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004448 return data() <= __p && __p < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004449}
4450
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004451#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004452
Louis Dionne173f29e2019-05-29 16:01:36 +00004453#if _LIBCPP_STD_VER > 11
Marshall Clowcba751f2013-07-23 17:05:24 +00004454// Literal suffixes for basic_string [basic.string.literals]
Marshall Clowac868372013-10-05 21:18:32 +00004455inline namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004456{
4457 inline namespace string_literals
4458 {
Howard Hinnant5c167562013-08-07 19:39:48 +00004459 inline _LIBCPP_INLINE_VISIBILITY
4460 basic_string<char> operator "" s( const char *__str, size_t __len )
4461 {
4462 return basic_string<char> (__str, __len);
4463 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004464
Louis Dionne89258142021-08-23 15:32:36 -04004465#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnant5c167562013-08-07 19:39:48 +00004466 inline _LIBCPP_INLINE_VISIBILITY
4467 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4468 {
4469 return basic_string<wchar_t> (__str, __len);
4470 }
Louis Dionne89258142021-08-23 15:32:36 -04004471#endif
Marshall Clowcba751f2013-07-23 17:05:24 +00004472
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -04004473#ifndef _LIBCPP_HAS_NO_CHAR8_T
Marshall Clow8732fed2018-12-11 04:35:44 +00004474 inline _LIBCPP_INLINE_VISIBILITY
4475 basic_string<char8_t> operator "" s(const char8_t *__str, size_t __len) _NOEXCEPT
4476 {
4477 return basic_string<char8_t> (__str, __len);
4478 }
4479#endif
4480
Howard Hinnant5c167562013-08-07 19:39:48 +00004481 inline _LIBCPP_INLINE_VISIBILITY
4482 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4483 {
4484 return basic_string<char16_t> (__str, __len);
4485 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004486
Howard Hinnant5c167562013-08-07 19:39:48 +00004487 inline _LIBCPP_INLINE_VISIBILITY
4488 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4489 {
4490 return basic_string<char32_t> (__str, __len);
4491 }
Nikolas Klauserd26407a2021-12-02 14:12:51 +01004492 } // namespace string_literals
4493} // namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004494#endif
4495
Howard Hinnantc51e1022010-05-11 19:42:16 +00004496_LIBCPP_END_NAMESPACE_STD
4497
Eric Fiselierf4433a32017-05-31 22:07:49 +00004498_LIBCPP_POP_MACROS
4499
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004500#endif // _LIBCPP_STRING