blob: 809ae41a5bd74d9b97cdc1abd9ec23c849060104 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
Louis Dionne9bd93882021-11-17 16:25:01 -05002//===----------------------------------------------------------------------===//
Howard Hinnantc51e1022010-05-11 19:42:16 +00003//
Chandler Carruth7642bb12019-01-19 08:50:56 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_STRING
11#define _LIBCPP_STRING
12
13/*
14 string synopsis
15
16namespace std
17{
18
19template <class stateT>
20class fpos
21{
22private:
23 stateT st;
24public:
25 fpos(streamoff = streamoff());
26
27 operator streamoff() const;
28
29 stateT state() const;
30 void state(stateT);
31
32 fpos& operator+=(streamoff);
33 fpos operator+ (streamoff) const;
34 fpos& operator-=(streamoff);
35 fpos operator- (streamoff) const;
36};
37
38template <class stateT> streamoff operator-(const fpos<stateT>& x, const fpos<stateT>& y);
39
40template <class stateT> bool operator==(const fpos<stateT>& x, const fpos<stateT>& y);
41template <class stateT> bool operator!=(const fpos<stateT>& x, const fpos<stateT>& y);
42
43template <class charT>
44struct char_traits
45{
46 typedef charT char_type;
47 typedef ... int_type;
48 typedef streamoff off_type;
49 typedef streampos pos_type;
50 typedef mbstate_t state_type;
51
Howard Hinnantaeb17d82011-05-29 19:57:12 +000052 static void assign(char_type& c1, const char_type& c2) noexcept;
Howard Hinnant270c00e2012-07-20 19:09:12 +000053 static constexpr bool eq(char_type c1, char_type c2) noexcept;
54 static constexpr bool lt(char_type c1, char_type c2) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000055
56 static int compare(const char_type* s1, const char_type* s2, size_t n);
57 static size_t length(const char_type* s);
58 static const char_type* find(const char_type* s, size_t n, const char_type& a);
59 static char_type* move(char_type* s1, const char_type* s2, size_t n);
60 static char_type* copy(char_type* s1, const char_type* s2, size_t n);
61 static char_type* assign(char_type* s, size_t n, char_type a);
62
Howard Hinnant270c00e2012-07-20 19:09:12 +000063 static constexpr int_type not_eof(int_type c) noexcept;
64 static constexpr char_type to_char_type(int_type c) noexcept;
65 static constexpr int_type to_int_type(char_type c) noexcept;
66 static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept;
67 static constexpr int_type eof() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000068};
69
70template <> struct char_traits<char>;
71template <> struct char_traits<wchar_t>;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +010072template <> struct char_traits<char8_t>; // C++20
73template <> struct char_traits<char16_t>;
74template <> struct char_traits<char32_t>;
Howard Hinnantc51e1022010-05-11 19:42:16 +000075
Howard Hinnant3b6579a2010-08-22 00:02:43 +000076template<class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
Howard Hinnantc51e1022010-05-11 19:42:16 +000077class basic_string
78{
Howard Hinnant3b6579a2010-08-22 00:02:43 +000079public:
80// types:
Howard Hinnantc51e1022010-05-11 19:42:16 +000081 typedef traits traits_type;
82 typedef typename traits_type::char_type value_type;
83 typedef Allocator allocator_type;
84 typedef typename allocator_type::size_type size_type;
85 typedef typename allocator_type::difference_type difference_type;
86 typedef typename allocator_type::reference reference;
87 typedef typename allocator_type::const_reference const_reference;
88 typedef typename allocator_type::pointer pointer;
89 typedef typename allocator_type::const_pointer const_pointer;
90 typedef implementation-defined iterator;
91 typedef implementation-defined const_iterator;
92 typedef std::reverse_iterator<iterator> reverse_iterator;
93 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
94
95 static const size_type npos = -1;
96
Howard Hinnant3e276872011-06-03 18:40:47 +000097 basic_string()
98 noexcept(is_nothrow_default_constructible<allocator_type>::value);
99 explicit basic_string(const allocator_type& a);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000100 basic_string(const basic_string& str);
Howard Hinnant3e276872011-06-03 18:40:47 +0000101 basic_string(basic_string&& str)
102 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clow83445802016-04-07 18:13:41 +0000103 basic_string(const basic_string& str, size_type pos,
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000104 const allocator_type& a = allocator_type());
Marshall Clow83445802016-04-07 18:13:41 +0000105 basic_string(const basic_string& str, size_type pos, size_type n,
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000106 const Allocator& a = Allocator());
Marshall Clow78dbe462016-11-14 18:22:19 +0000107 template<class T>
108 basic_string(const T& t, size_type pos, size_type n, const Allocator& a = Allocator()); // C++17
Marshall Clowe46031a2018-07-02 18:41:15 +0000109 template <class T>
110 explicit basic_string(const T& t, const Allocator& a = Allocator()); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000111 basic_string(const value_type* s, const allocator_type& a = allocator_type());
112 basic_string(const value_type* s, size_type n, const allocator_type& a = allocator_type());
Marek Kurdej90d79712021-07-27 16:16:21 +0200113 basic_string(nullptr_t) = delete; // C++2b
Howard Hinnantc51e1022010-05-11 19:42:16 +0000114 basic_string(size_type n, value_type c, const allocator_type& a = allocator_type());
115 template<class InputIterator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000116 basic_string(InputIterator begin, InputIterator end,
117 const allocator_type& a = allocator_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000118 basic_string(initializer_list<value_type>, const Allocator& = Allocator());
119 basic_string(const basic_string&, const Allocator&);
120 basic_string(basic_string&&, const Allocator&);
121
122 ~basic_string();
123
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000124 operator basic_string_view<charT, traits>() const noexcept;
125
Howard Hinnantc51e1022010-05-11 19:42:16 +0000126 basic_string& operator=(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000127 template <class T>
128 basic_string& operator=(const T& t); // C++17
Howard Hinnant3e276872011-06-03 18:40:47 +0000129 basic_string& operator=(basic_string&& str)
130 noexcept(
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000131 allocator_type::propagate_on_container_move_assignment::value ||
132 allocator_type::is_always_equal::value ); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000133 basic_string& operator=(const value_type* s);
Marek Kurdej90d79712021-07-27 16:16:21 +0200134 basic_string& operator=(nullptr_t) = delete; // C++2b
Howard Hinnantc51e1022010-05-11 19:42:16 +0000135 basic_string& operator=(value_type c);
136 basic_string& operator=(initializer_list<value_type>);
137
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000138 iterator begin() noexcept;
139 const_iterator begin() const noexcept;
140 iterator end() noexcept;
141 const_iterator end() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000142
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000143 reverse_iterator rbegin() noexcept;
144 const_reverse_iterator rbegin() const noexcept;
145 reverse_iterator rend() noexcept;
146 const_reverse_iterator rend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000147
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000148 const_iterator cbegin() const noexcept;
149 const_iterator cend() const noexcept;
150 const_reverse_iterator crbegin() const noexcept;
151 const_reverse_iterator crend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000152
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000153 size_type size() const noexcept;
154 size_type length() const noexcept;
155 size_type max_size() const noexcept;
156 size_type capacity() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000157
158 void resize(size_type n, value_type c);
159 void resize(size_type n);
160
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100161 template<class Operation>
162 constexpr void resize_and_overwrite(size_type n, Operation op); // since C++23
163
Marek Kurdejc9848142020-11-26 10:07:16 +0100164 void reserve(size_type res_arg);
165 void reserve(); // deprecated in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000166 void shrink_to_fit();
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000167 void clear() noexcept;
168 bool empty() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000169
170 const_reference operator[](size_type pos) const;
171 reference operator[](size_type pos);
172
173 const_reference at(size_type n) const;
174 reference at(size_type n);
175
176 basic_string& operator+=(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000177 template <class T>
178 basic_string& operator+=(const T& t); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000179 basic_string& operator+=(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000180 basic_string& operator+=(value_type c);
181 basic_string& operator+=(initializer_list<value_type>);
182
183 basic_string& append(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000184 template <class T>
185 basic_string& append(const T& t); // C++17
Marshall Clow8db7fb02014-03-04 19:17:19 +0000186 basic_string& append(const basic_string& str, size_type pos, size_type n=npos); //C++14
Marshall Clow82513342016-09-24 22:45:42 +0000187 template <class T>
188 basic_string& append(const T& t, size_type pos, size_type n=npos); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000189 basic_string& append(const value_type* s, size_type n);
190 basic_string& append(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000191 basic_string& append(size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000192 template<class InputIterator>
193 basic_string& append(InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000194 basic_string& append(initializer_list<value_type>);
195
196 void push_back(value_type c);
197 void pop_back();
198 reference front();
199 const_reference front() const;
200 reference back();
201 const_reference back() const;
202
203 basic_string& assign(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000204 template <class T>
205 basic_string& assign(const T& t); // C++17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000206 basic_string& assign(basic_string&& str);
Marshall Clow8db7fb02014-03-04 19:17:19 +0000207 basic_string& assign(const basic_string& str, size_type pos, size_type n=npos); // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000208 template <class T>
209 basic_string& assign(const T& t, size_type pos, size_type n=npos); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000210 basic_string& assign(const value_type* s, size_type n);
211 basic_string& assign(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000212 basic_string& assign(size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000213 template<class InputIterator>
214 basic_string& assign(InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000215 basic_string& assign(initializer_list<value_type>);
216
217 basic_string& insert(size_type pos1, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000218 template <class T>
219 basic_string& insert(size_type pos1, const T& t);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000220 basic_string& insert(size_type pos1, const basic_string& str,
221 size_type pos2, size_type n);
Marshall Clow82513342016-09-24 22:45:42 +0000222 template <class T>
223 basic_string& insert(size_type pos1, const T& t, size_type pos2, size_type n); // C++17
Marshall Clow8db7fb02014-03-04 19:17:19 +0000224 basic_string& insert(size_type pos, const value_type* s, size_type n=npos); //C++14
Howard Hinnantd17880b2013-06-28 16:59:19 +0000225 basic_string& insert(size_type pos, const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000226 basic_string& insert(size_type pos, size_type n, value_type c);
227 iterator insert(const_iterator p, value_type c);
228 iterator insert(const_iterator p, size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000229 template<class InputIterator>
230 iterator insert(const_iterator p, InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000231 iterator insert(const_iterator p, initializer_list<value_type>);
232
233 basic_string& erase(size_type pos = 0, size_type n = npos);
234 iterator erase(const_iterator position);
235 iterator erase(const_iterator first, const_iterator last);
236
237 basic_string& replace(size_type pos1, size_type n1, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000238 template <class T>
239 basic_string& replace(size_type pos1, size_type n1, const T& t); // C++17
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000240 basic_string& replace(size_type pos1, size_type n1, const basic_string& str,
Marshall Clow8db7fb02014-03-04 19:17:19 +0000241 size_type pos2, size_type n2=npos); // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000242 template <class T>
243 basic_string& replace(size_type pos1, size_type n1, const T& t,
244 size_type pos2, size_type n); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000245 basic_string& replace(size_type pos, size_type n1, const value_type* s, size_type n2);
246 basic_string& replace(size_type pos, size_type n1, const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000247 basic_string& replace(size_type pos, size_type n1, size_type n2, value_type c);
Howard Hinnant990d6e82010-11-17 21:11:40 +0000248 basic_string& replace(const_iterator i1, const_iterator i2, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000249 template <class T>
250 basic_string& replace(const_iterator i1, const_iterator i2, const T& t); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000251 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s, size_type n);
252 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s);
Howard Hinnant990d6e82010-11-17 21:11:40 +0000253 basic_string& replace(const_iterator i1, const_iterator i2, size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000254 template<class InputIterator>
Howard Hinnant990d6e82010-11-17 21:11:40 +0000255 basic_string& replace(const_iterator i1, const_iterator i2, InputIterator j1, InputIterator j2);
256 basic_string& replace(const_iterator i1, const_iterator i2, initializer_list<value_type>);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000257
Howard Hinnantd17880b2013-06-28 16:59:19 +0000258 size_type copy(value_type* s, size_type n, size_type pos = 0) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000259 basic_string substr(size_type pos = 0, size_type n = npos) const;
260
Howard Hinnant3e276872011-06-03 18:40:47 +0000261 void swap(basic_string& str)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000262 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
263 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000264
Howard Hinnantd17880b2013-06-28 16:59:19 +0000265 const value_type* c_str() const noexcept;
266 const value_type* data() const noexcept;
Marshall Clowd3c22392016-03-08 15:44:30 +0000267 value_type* data() noexcept; // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000268
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000269 allocator_type get_allocator() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000270
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000271 size_type find(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000272 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800273 size_type find(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000274 size_type find(const value_type* s, size_type pos, size_type n) const noexcept;
275 size_type find(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000276 size_type find(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000277
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000278 size_type rfind(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000279 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800280 size_type rfind(const T& t, size_type pos = npos) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000281 size_type rfind(const value_type* s, size_type pos, size_type n) const noexcept;
282 size_type rfind(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000283 size_type rfind(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000284
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000285 size_type find_first_of(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000286 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800287 size_type find_first_of(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000288 size_type find_first_of(const value_type* s, size_type pos, size_type n) const noexcept;
289 size_type find_first_of(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000290 size_type find_first_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000291
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000292 size_type find_last_of(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000293 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800294 size_type find_last_of(const T& t, size_type pos = npos) const noexcept noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000295 size_type find_last_of(const value_type* s, size_type pos, size_type n) const noexcept;
296 size_type find_last_of(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000297 size_type find_last_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000298
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000299 size_type find_first_not_of(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000300 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800301 size_type find_first_not_of(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000302 size_type find_first_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
303 size_type find_first_not_of(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000304 size_type find_first_not_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000305
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000306 size_type find_last_not_of(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000307 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800308 size_type find_last_not_of(const T& t, size_type pos = npos) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000309 size_type find_last_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
310 size_type find_last_not_of(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000311 size_type find_last_not_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000312
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000313 int compare(const basic_string& str) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000314 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800315 int compare(const T& t) const noexcept; // C++17, noexcept as an extension
Howard Hinnantc51e1022010-05-11 19:42:16 +0000316 int compare(size_type pos1, size_type n1, const basic_string& str) const;
Marshall Clowe46031a2018-07-02 18:41:15 +0000317 template <class T>
318 int compare(size_type pos1, size_type n1, const T& t) const; // C++17
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000319 int compare(size_type pos1, size_type n1, const basic_string& str,
Marshall Clow8db7fb02014-03-04 19:17:19 +0000320 size_type pos2, size_type n2=npos) const; // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000321 template <class T>
322 int compare(size_type pos1, size_type n1, const T& t,
323 size_type pos2, size_type n2=npos) const; // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000324 int compare(const value_type* s) const noexcept;
325 int compare(size_type pos1, size_type n1, const value_type* s) const;
326 int compare(size_type pos1, size_type n1, const value_type* s, size_type n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000327
Marek Kurdej24b4c512021-01-07 12:29:04 +0100328 bool starts_with(basic_string_view<charT, traits> sv) const noexcept; // C++20
329 bool starts_with(charT c) const noexcept; // C++20
330 bool starts_with(const charT* s) const; // C++20
331 bool ends_with(basic_string_view<charT, traits> sv) const noexcept; // C++20
332 bool ends_with(charT c) const noexcept; // C++20
333 bool ends_with(const charT* s) const; // C++20
Marshall Clow18c293b2017-12-04 20:11:38 +0000334
Wim Leflere023c3542021-01-19 14:33:30 -0500335 constexpr bool contains(basic_string_view<charT, traits> sv) const noexcept; // C++2b
336 constexpr bool contains(charT c) const noexcept; // C++2b
337 constexpr bool contains(const charT* s) const; // C++2b
338
Howard Hinnantc51e1022010-05-11 19:42:16 +0000339 bool __invariants() const;
340};
341
Marshall Clowa0563332018-02-08 06:34:03 +0000342template<class InputIterator,
343 class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
344basic_string(InputIterator, InputIterator, Allocator = Allocator())
345 -> basic_string<typename iterator_traits<InputIterator>::value_type,
346 char_traits<typename iterator_traits<InputIterator>::value_type>,
347 Allocator>; // C++17
348
Howard Hinnantc51e1022010-05-11 19:42:16 +0000349template<class charT, class traits, class Allocator>
350basic_string<charT, traits, Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000351operator+(const basic_string<charT, traits, Allocator>& lhs,
352 const basic_string<charT, traits, Allocator>& rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000353
354template<class charT, class traits, class Allocator>
355basic_string<charT, traits, Allocator>
356operator+(const charT* lhs , const basic_string<charT,traits,Allocator>&rhs);
357
358template<class charT, class traits, class Allocator>
359basic_string<charT, traits, Allocator>
360operator+(charT lhs, const basic_string<charT,traits,Allocator>& rhs);
361
362template<class charT, class traits, class Allocator>
363basic_string<charT, traits, Allocator>
364operator+(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs);
365
366template<class charT, class traits, class Allocator>
367basic_string<charT, traits, Allocator>
368operator+(const basic_string<charT, traits, Allocator>& lhs, charT rhs);
369
370template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000371bool operator==(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000372 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000373
374template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000375bool operator==(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000376
377template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000378bool operator==(const basic_string<charT,traits,Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000379
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000380template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000381bool operator!=(const basic_string<charT,traits,Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000382 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000383
384template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000385bool operator!=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000386
387template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000388bool operator!=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000389
390template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000391bool operator< (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000392 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000393
394template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000395bool operator< (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000396
397template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000398bool operator< (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000399
400template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000401bool operator> (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000402 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000403
404template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000405bool operator> (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000406
407template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000408bool operator> (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000409
410template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000411bool operator<=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000412 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000413
414template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000415bool operator<=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000416
417template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000418bool operator<=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000419
420template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000421bool operator>=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000422 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000423
424template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000425bool operator>=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000426
427template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000428bool operator>=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000429
430template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000431void swap(basic_string<charT, traits, Allocator>& lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +0000432 basic_string<charT, traits, Allocator>& rhs)
433 noexcept(noexcept(lhs.swap(rhs)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000434
435template<class charT, class traits, class Allocator>
436basic_istream<charT, traits>&
437operator>>(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
438
439template<class charT, class traits, class Allocator>
440basic_ostream<charT, traits>&
441operator<<(basic_ostream<charT, traits>& os, const basic_string<charT, traits, Allocator>& str);
442
443template<class charT, class traits, class Allocator>
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000444basic_istream<charT, traits>&
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000445getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str,
446 charT delim);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000447
448template<class charT, class traits, class Allocator>
449basic_istream<charT, traits>&
450getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
451
Marshall Clow29b53f22018-12-14 18:49:35 +0000452template<class charT, class traits, class Allocator, class U>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200453typename basic_string<charT, traits, Allocator>::size_type
454erase(basic_string<charT, traits, Allocator>& c, const U& value); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000455template<class charT, class traits, class Allocator, class Predicate>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200456typename basic_string<charT, traits, Allocator>::size_type
457erase_if(basic_string<charT, traits, Allocator>& c, Predicate pred); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000458
Howard Hinnantc51e1022010-05-11 19:42:16 +0000459typedef basic_string<char> string;
460typedef basic_string<wchar_t> wstring;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100461typedef basic_string<char8_t> u8string; // C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000462typedef basic_string<char16_t> u16string;
463typedef basic_string<char32_t> u32string;
464
Bruce Mitchener170d8972020-11-24 12:53:53 -0500465int stoi (const string& str, size_t* idx = nullptr, int base = 10);
466long stol (const string& str, size_t* idx = nullptr, int base = 10);
467unsigned long stoul (const string& str, size_t* idx = nullptr, int base = 10);
468long long stoll (const string& str, size_t* idx = nullptr, int base = 10);
469unsigned long long stoull(const string& str, size_t* idx = nullptr, int base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000470
Bruce Mitchener170d8972020-11-24 12:53:53 -0500471float stof (const string& str, size_t* idx = nullptr);
472double stod (const string& str, size_t* idx = nullptr);
473long double stold(const string& str, size_t* idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000474
475string to_string(int val);
476string to_string(unsigned val);
477string to_string(long val);
478string to_string(unsigned long val);
479string to_string(long long val);
480string to_string(unsigned long long val);
481string to_string(float val);
482string to_string(double val);
483string to_string(long double val);
484
Bruce Mitchener170d8972020-11-24 12:53:53 -0500485int stoi (const wstring& str, size_t* idx = nullptr, int base = 10);
486long stol (const wstring& str, size_t* idx = nullptr, int base = 10);
487unsigned long stoul (const wstring& str, size_t* idx = nullptr, int base = 10);
488long long stoll (const wstring& str, size_t* idx = nullptr, int base = 10);
489unsigned long long stoull(const wstring& str, size_t* idx = nullptr, int base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000490
Bruce Mitchener170d8972020-11-24 12:53:53 -0500491float stof (const wstring& str, size_t* idx = nullptr);
492double stod (const wstring& str, size_t* idx = nullptr);
493long double stold(const wstring& str, size_t* idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000494
495wstring to_wstring(int val);
496wstring to_wstring(unsigned val);
497wstring to_wstring(long val);
498wstring to_wstring(unsigned long val);
499wstring to_wstring(long long val);
500wstring to_wstring(unsigned long long val);
501wstring to_wstring(float val);
502wstring to_wstring(double val);
503wstring to_wstring(long double val);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000504
505template <> struct hash<string>;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100506template <> struct hash<u8string>; // C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000507template <> struct hash<u16string>;
508template <> struct hash<u32string>;
509template <> struct hash<wstring>;
510
Marshall Clowcba751f2013-07-23 17:05:24 +0000511basic_string<char> operator "" s( const char *str, size_t len ); // C++14
512basic_string<wchar_t> operator "" s( const wchar_t *str, size_t len ); // C++14
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100513basic_string<char8_t> operator "" s( const char8_t *str, size_t len ); // C++20
Marshall Clowcba751f2013-07-23 17:05:24 +0000514basic_string<char16_t> operator "" s( const char16_t *str, size_t len ); // C++14
515basic_string<char32_t> operator "" s( const char32_t *str, size_t len ); // C++14
516
Howard Hinnantc51e1022010-05-11 19:42:16 +0000517} // std
518
519*/
520
521#include <__config>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400522#include <__debug>
Nikolas Klauser80840272022-02-04 13:04:33 +0100523#include <__ios/fpos.h>
Louis Dionne77249522021-06-11 09:55:11 -0400524#include <__iterator/wrap_iter.h>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000525#include <algorithm>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400526#include <compare>
527#include <cstdio> // EOF
Louis Dionned24191c2021-08-19 12:39:16 -0400528#include <cstdlib>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400529#include <cstring>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400530#include <initializer_list>
531#include <iosfwd>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000532#include <iterator>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000533#include <memory>
534#include <stdexcept>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400535#include <string_view>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000536#include <type_traits>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400537#include <utility>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000538#include <version>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000539
Nikolas Klauser4c1bf592022-02-11 19:15:18 +0100540// TODO: remove these headers
541#include <__functional/binary_function.h>
542#include <__functional/invoke.h>
543#include <__functional/operations.h>
544#include <__functional/reference_wrapper.h>
545#include <__functional/unary_function.h>
546#include <__functional/weak_result_type.h>
547#include <new>
548#include <typeinfo>
549
Louis Dionne89258142021-08-23 15:32:36 -0400550#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Nikolas Klauser80840272022-02-04 13:04:33 +0100551# include <cwchar>
Louis Dionne89258142021-08-23 15:32:36 -0400552#endif
553
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400554#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
Nikolas Klauser80840272022-02-04 13:04:33 +0100555# include <cstdint>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400556#endif
Eric Fiselier14b6de92014-08-10 23:53:08 +0000557
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000558#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Arthur O'Dwyer6eeaa002022-02-01 20:16:40 -0500559# pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000560#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000561
Eric Fiselierf4433a32017-05-31 22:07:49 +0000562_LIBCPP_PUSH_MACROS
563#include <__undef_macros>
564
565
Howard Hinnantc51e1022010-05-11 19:42:16 +0000566_LIBCPP_BEGIN_NAMESPACE_STD
567
Howard Hinnantc51e1022010-05-11 19:42:16 +0000568// basic_string
569
570template<class _CharT, class _Traits, class _Allocator>
571basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000572operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
573 const basic_string<_CharT, _Traits, _Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000574
575template<class _CharT, class _Traits, class _Allocator>
576basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000577operator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000578
579template<class _CharT, class _Traits, class _Allocator>
580basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000581operator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000582
583template<class _CharT, class _Traits, class _Allocator>
Louis Dionne8f8d95d2018-11-21 17:31:55 +0000584inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000585basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000586operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000587
588template<class _CharT, class _Traits, class _Allocator>
589basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000590operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000591
Shoaib Meenaiea363712017-07-29 02:54:41 +0000592_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&))
593
Marshall Clow039b2f02016-01-13 21:54:34 +0000594template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400595struct __string_is_trivial_iterator : public false_type {};
596
597template <class _Tp>
598struct __string_is_trivial_iterator<_Tp*>
599 : public is_arithmetic<_Tp> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000600
Louis Dionne173f29e2019-05-29 16:01:36 +0000601template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400602struct __string_is_trivial_iterator<__wrap_iter<_Iter> >
603 : public __string_is_trivial_iterator<_Iter> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000604
Marshall Clow82513342016-09-24 22:45:42 +0000605template <class _CharT, class _Traits, class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500606struct __can_be_converted_to_string_view : public _BoolConstant<
607 is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
608 !is_convertible<const _Tp&, const _CharT*>::value
609 > {};
Marshall Clow82513342016-09-24 22:45:42 +0000610
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000611#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000612
613template <class _CharT, size_t = sizeof(_CharT)>
614struct __padding
615{
616 unsigned char __xx[sizeof(_CharT)-1];
617};
618
619template <class _CharT>
620struct __padding<_CharT, 1>
621{
622};
623
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400624#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000625
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400626#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800627typedef basic_string<char8_t> u8string;
628#endif
629
630#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
631typedef basic_string<char16_t> u16string;
632typedef basic_string<char32_t> u32string;
Louis Dionne96fc5f52021-09-09 11:25:10 -0400633#endif
Richard Smith256954d2020-11-11 17:12:18 -0800634
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000635template<class _CharT, class _Traits, class _Allocator>
Richard Smith256954d2020-11-11 17:12:18 -0800636class
637 _LIBCPP_TEMPLATE_VIS
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400638#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800639 _LIBCPP_PREFERRED_NAME(u8string)
640#endif
641#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
642 _LIBCPP_PREFERRED_NAME(u16string)
643 _LIBCPP_PREFERRED_NAME(u32string)
644#endif
645 basic_string
Howard Hinnantc51e1022010-05-11 19:42:16 +0000646{
647public:
648 typedef basic_string __self;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000649 typedef basic_string_view<_CharT, _Traits> __self_view;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000650 typedef _Traits traits_type;
Marshall Clowa3a74e02017-03-15 18:41:11 +0000651 typedef _CharT value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000652 typedef _Allocator allocator_type;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000653 typedef allocator_traits<allocator_type> __alloc_traits;
654 typedef typename __alloc_traits::size_type size_type;
655 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant3e276872011-06-03 18:40:47 +0000656 typedef value_type& reference;
657 typedef const value_type& const_reference;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000658 typedef typename __alloc_traits::pointer pointer;
659 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000660
Marshall Clow79f33542018-03-21 00:36:05 +0000661 static_assert((!is_array<value_type>::value), "Character type of basic_string must not be an array");
662 static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string must be standard-layout");
663 static_assert(( is_trivial<value_type>::value), "Character type of basic_string must be trivial");
664 static_assert(( is_same<_CharT, typename traits_type::char_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000665 "traits_type::char_type must be the same type as CharT");
Marshall Clow79f33542018-03-21 00:36:05 +0000666 static_assert(( is_same<typename allocator_type::value_type, value_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000667 "Allocator::value_type must be same type as value_type");
Marshall Clowe46031a2018-07-02 18:41:15 +0000668
Howard Hinnantc51e1022010-05-11 19:42:16 +0000669 typedef __wrap_iter<pointer> iterator;
670 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000671 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
672 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000673
674private:
Howard Hinnant68bf1812013-04-30 21:44:48 +0000675
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000676#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000677
678 struct __long
679 {
680 pointer __data_;
681 size_type __size_;
682 size_type __cap_;
683 };
684
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000685#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000686 static const size_type __short_mask = 0x01;
687 static const size_type __long_mask = 0x1ul;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000688#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000689 static const size_type __short_mask = 0x80;
690 static const size_type __long_mask = ~(size_type(~0) >> 1);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400691#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +0000692
693 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
694 (sizeof(__long) - 1)/sizeof(value_type) : 2};
695
696 struct __short
697 {
698 value_type __data_[__min_cap];
699 struct
700 : __padding<value_type>
701 {
702 unsigned char __size_;
703 };
704 };
705
706#else
707
Howard Hinnantc51e1022010-05-11 19:42:16 +0000708 struct __long
709 {
710 size_type __cap_;
711 size_type __size_;
712 pointer __data_;
713 };
714
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000715#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000716 static const size_type __short_mask = 0x80;
717 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000718#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000719 static const size_type __short_mask = 0x01;
720 static const size_type __long_mask = 0x1ul;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400721#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +0000722
Howard Hinnantc51e1022010-05-11 19:42:16 +0000723 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
724 (sizeof(__long) - 1)/sizeof(value_type) : 2};
725
726 struct __short
727 {
728 union
729 {
730 unsigned char __size_;
Howard Hinnant49e145e2012-10-30 19:06:59 +0000731 value_type __lx;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000732 };
733 value_type __data_[__min_cap];
734 };
735
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400736#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000737
Howard Hinnant8ea98242013-08-23 17:37:05 +0000738 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000739
Howard Hinnant8ea98242013-08-23 17:37:05 +0000740 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000741
742 struct __raw
743 {
744 size_type __words[__n_words];
745 };
746
747 struct __rep
748 {
749 union
750 {
751 __long __l;
752 __short __s;
753 __raw __r;
754 };
755 };
756
757 __compressed_pair<__rep, allocator_type> __r_;
758
Howard Hinnantc51e1022010-05-11 19:42:16 +0000759public:
Martin Storsjö88890c22021-02-22 01:13:13 +0200760 _LIBCPP_TEMPLATE_DATA_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000761 static const size_type npos = -1;
762
Howard Hinnant3e276872011-06-03 18:40:47 +0000763 _LIBCPP_INLINE_VISIBILITY basic_string()
764 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000765
766 _LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a)
767#if _LIBCPP_STD_VER <= 14
768 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
769#else
770 _NOEXCEPT;
771#endif
772
Howard Hinnantc51e1022010-05-11 19:42:16 +0000773 basic_string(const basic_string& __str);
774 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clowa80abc72015-06-03 19:56:43 +0000775
Eric Fiselierfc92be82017-04-19 00:28:44 +0000776#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000777 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000778 basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +0000779#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +0000780 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000781#else
782 _NOEXCEPT;
783#endif
784
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000785 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000786 basic_string(basic_string&& __str, const allocator_type& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400787#endif // _LIBCPP_CXX03_LANG
Marshall Clowe46031a2018-07-02 18:41:15 +0000788
Louis Dionne9ce598d2021-09-08 09:14:43 -0400789 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Eric Fiseliera8567862018-07-17 05:48:48 +0000790 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5169d1c2019-12-16 19:03:23 -0500791 basic_string(const _CharT* __s) : __r_(__default_init_tag(), __default_init_tag()) {
Eric Fiseliera8567862018-07-17 05:48:48 +0000792 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
793 __init(__s, traits_type::length(__s));
Nikolas Klauserf2807732022-01-11 00:33:35 +0100794 _VSTD::__debug_db_insert_c(this);
Eric Fiseliera8567862018-07-17 05:48:48 +0000795 }
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> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000798 _LIBCPP_INLINE_VISIBILITY
799 basic_string(const _CharT* __s, const _Allocator& __a);
800
Marek Kurdej90d79712021-07-27 16:16:21 +0200801#if _LIBCPP_STD_VER > 20
802 basic_string(nullptr_t) = delete;
803#endif
804
Howard Hinnantcf823322010-12-17 14:46:43 +0000805 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000806 basic_string(const _CharT* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +0000807 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000808 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000809 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000810 basic_string(size_type __n, _CharT __c);
Marshall Clowe46031a2018-07-02 18:41:15 +0000811
Louis Dionne9ce598d2021-09-08 09:14:43 -0400812 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000813 _LIBCPP_INLINE_VISIBILITY
814 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
815
Marshall Clow83445802016-04-07 18:13:41 +0000816 basic_string(const basic_string& __str, size_type __pos, size_type __n,
Eric Fiselier812882b2017-02-17 01:17:10 +0000817 const _Allocator& __a = _Allocator());
Marshall Clow83445802016-04-07 18:13:41 +0000818 _LIBCPP_INLINE_VISIBILITY
819 basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +0000820 const _Allocator& __a = _Allocator());
Marshall Clowe46031a2018-07-02 18:41:15 +0000821
Louis Dionne9ce598d2021-09-08 09:14:43 -0400822 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 +0000823 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselier812882b2017-02-17 01:17:10 +0000824 basic_string(const _Tp& __t, size_type __pos, size_type __n,
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500825 const allocator_type& __a = allocator_type());
Marshall Clowe46031a2018-07-02 18:41:15 +0000826
Louis Dionne9ce598d2021-09-08 09:14:43 -0400827 template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500828 !__is_same_uncvref<_Tp, basic_string>::value> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000829 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
830 explicit basic_string(const _Tp& __t);
831
Louis Dionne9ce598d2021-09-08 09:14:43 -0400832 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 +0000833 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
834 explicit basic_string(const _Tp& __t, const allocator_type& __a);
835
Louis Dionne9ce598d2021-09-08 09:14:43 -0400836 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000837 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000838 basic_string(_InputIterator __first, _InputIterator __last);
Louis Dionne9ce598d2021-09-08 09:14:43 -0400839 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000840 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000841 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000842#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000843 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000844 basic_string(initializer_list<_CharT> __il);
Howard Hinnantcf823322010-12-17 14:46:43 +0000845 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000846 basic_string(initializer_list<_CharT> __il, const _Allocator& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400847#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000848
Eric Fiselierd9a702a2016-10-31 03:42:50 +0000849 inline ~basic_string();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000850
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000851 _LIBCPP_INLINE_VISIBILITY
852 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
853
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000854 basic_string& operator=(const basic_string& __str);
Eric Fiselier5ec13b12017-01-23 21:24:58 +0000855
Louis Dionne9ce598d2021-09-08 09:14:43 -0400856 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 +0000857 basic_string& operator=(const _Tp& __t)
858 {__self_view __sv = __t; return assign(__sv);}
859
Eric Fiselierfc92be82017-04-19 00:28:44 +0000860#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000861 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000862 basic_string& operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000863 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiselierfc92be82017-04-19 00:28:44 +0000864 _LIBCPP_INLINE_VISIBILITY
865 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000866#endif
Howard Hinnantd17880b2013-06-28 16:59:19 +0000867 _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const value_type* __s) {return assign(__s);}
Marek Kurdej90d79712021-07-27 16:16:21 +0200868#if _LIBCPP_STD_VER > 20
869 basic_string& operator=(nullptr_t) = delete;
870#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000871 basic_string& operator=(value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000872
Louis Dionneba400782020-10-02 15:02:52 -0400873#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +0000874 _LIBCPP_INLINE_VISIBILITY
875 iterator begin() _NOEXCEPT
876 {return iterator(this, __get_pointer());}
877 _LIBCPP_INLINE_VISIBILITY
878 const_iterator begin() const _NOEXCEPT
879 {return const_iterator(this, __get_pointer());}
880 _LIBCPP_INLINE_VISIBILITY
881 iterator end() _NOEXCEPT
882 {return iterator(this, __get_pointer() + size());}
883 _LIBCPP_INLINE_VISIBILITY
884 const_iterator end() const _NOEXCEPT
885 {return const_iterator(this, __get_pointer() + size());}
886#else
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000887 _LIBCPP_INLINE_VISIBILITY
888 iterator begin() _NOEXCEPT
889 {return iterator(__get_pointer());}
890 _LIBCPP_INLINE_VISIBILITY
891 const_iterator begin() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000892 {return const_iterator(__get_pointer());}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000893 _LIBCPP_INLINE_VISIBILITY
894 iterator end() _NOEXCEPT
895 {return iterator(__get_pointer() + size());}
896 _LIBCPP_INLINE_VISIBILITY
897 const_iterator end() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000898 {return const_iterator(__get_pointer() + size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400899#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000900 _LIBCPP_INLINE_VISIBILITY
901 reverse_iterator rbegin() _NOEXCEPT
902 {return reverse_iterator(end());}
903 _LIBCPP_INLINE_VISIBILITY
904 const_reverse_iterator rbegin() const _NOEXCEPT
905 {return const_reverse_iterator(end());}
906 _LIBCPP_INLINE_VISIBILITY
907 reverse_iterator rend() _NOEXCEPT
908 {return reverse_iterator(begin());}
909 _LIBCPP_INLINE_VISIBILITY
910 const_reverse_iterator rend() const _NOEXCEPT
911 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000912
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000913 _LIBCPP_INLINE_VISIBILITY
914 const_iterator cbegin() const _NOEXCEPT
915 {return begin();}
916 _LIBCPP_INLINE_VISIBILITY
917 const_iterator cend() const _NOEXCEPT
918 {return end();}
919 _LIBCPP_INLINE_VISIBILITY
920 const_reverse_iterator crbegin() const _NOEXCEPT
921 {return rbegin();}
922 _LIBCPP_INLINE_VISIBILITY
923 const_reverse_iterator crend() const _NOEXCEPT
924 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000925
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000926 _LIBCPP_INLINE_VISIBILITY size_type size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000927 {return __is_long() ? __get_long_size() : __get_short_size();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000928 _LIBCPP_INLINE_VISIBILITY size_type length() const _NOEXCEPT {return size();}
929 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT;
930 _LIBCPP_INLINE_VISIBILITY size_type capacity() const _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +0000931 {return (__is_long() ? __get_long_cap()
932 : static_cast<size_type>(__min_cap)) - 1;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000933
934 void resize(size_type __n, value_type __c);
935 _LIBCPP_INLINE_VISIBILITY void resize(size_type __n) {resize(__n, value_type());}
936
Marek Kurdejc9848142020-11-26 10:07:16 +0100937 void reserve(size_type __requested_capacity);
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100938
939#if _LIBCPP_STD_VER > 20
940 template <class _Op>
941 _LIBCPP_HIDE_FROM_ABI constexpr
942 void resize_and_overwrite(size_type __n, _Op __op) {
943 __resize_default_init(__n);
Nikolas Klausera4a76a12022-01-20 13:53:59 +0100944 __erase_to_end(_VSTD::move(__op)(data(), _LIBCPP_AUTO_CAST(__n)));
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100945 }
946#endif
947
Eric Fiselier451d5582018-11-26 20:15:38 +0000948 _LIBCPP_INLINE_VISIBILITY void __resize_default_init(size_type __n);
949
Marek Kurdejc9848142020-11-26 10:07:16 +0100950 _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_INLINE_VISIBILITY
951 void reserve() _NOEXCEPT {shrink_to_fit();}
Marshall Clow6ae12a82018-11-28 18:18:34 +0000952 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejc9848142020-11-26 10:07:16 +0100953 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000954 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000955 void clear() _NOEXCEPT;
Marshall Clowb7db4972017-11-15 20:02:27 +0000956 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
957 bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000958
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000959 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __pos) const _NOEXCEPT;
960 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000961
962 const_reference at(size_type __n) const;
963 reference at(size_type __n);
964
965 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const basic_string& __str) {return append(__str);}
Marshall Clowe46031a2018-07-02 18:41:15 +0000966
967 template <class _Tp>
968 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -0400969 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +0000970 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500971 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
972 && !__is_same_uncvref<_Tp, basic_string >::value,
Marshall Clowe46031a2018-07-02 18:41:15 +0000973 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500974 >
Marshall Clowe46031a2018-07-02 18:41:15 +0000975 operator+=(const _Tp& __t) {__self_view __sv = __t; return append(__sv);}
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000976 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const value_type* __s) {return append(__s);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000977 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(value_type __c) {push_back(__c); return *this;}
Eric Fiselierfc92be82017-04-19 00:28:44 +0000978#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000979 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(initializer_list<value_type> __il) {return append(__il);}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400980#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000981
Howard Hinnantcf823322010-12-17 14:46:43 +0000982 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000983 basic_string& append(const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000984
985 template <class _Tp>
986 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -0400987 __enable_if_t<
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500988 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
989 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clowe46031a2018-07-02 18:41:15 +0000990 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500991 >
Marshall Clowe46031a2018-07-02 18:41:15 +0000992 append(const _Tp& __t) { __self_view __sv = __t; return append(__sv.data(), __sv.size()); }
Marshall Clow8db7fb02014-03-04 19:17:19 +0000993 basic_string& append(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clowe46031a2018-07-02 18:41:15 +0000994
Marshall Clow62953962016-10-03 23:40:48 +0000995 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +0000996 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -0400997 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +0000998 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500999 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1000 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001001 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001002 >
Marshall Clow82513342016-09-24 22:45:42 +00001003 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001004 basic_string& append(const value_type* __s, size_type __n);
1005 basic_string& append(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001006 basic_string& append(size_type __n, value_type __c);
Eric Fiselier451d5582018-11-26 20:15:38 +00001007
1008 _LIBCPP_INLINE_VISIBILITY
1009 void __append_default_init(size_type __n);
1010
Howard Hinnantc51e1022010-05-11 19:42:16 +00001011 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001012 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001013 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001014 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001015 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001016 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001017 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001018 _LIBCPP_INLINE_VISIBILITY
1019 append(_InputIterator __first, _InputIterator __last) {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001020 const basic_string __temp(__first, __last, __alloc());
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001021 append(__temp.data(), __temp.size());
1022 return *this;
1023 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001024 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001025 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001026 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001027 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001028 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001029 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001030 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001031 _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001032 append(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001033
Eric Fiselierfc92be82017-04-19 00:28:44 +00001034#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001035 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001036 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001037#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001038
1039 void push_back(value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001040 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001041 void pop_back();
Marshall Clow05cf6692019-03-19 03:30:07 +00001042 _LIBCPP_INLINE_VISIBILITY reference front() _NOEXCEPT;
1043 _LIBCPP_INLINE_VISIBILITY const_reference front() const _NOEXCEPT;
1044 _LIBCPP_INLINE_VISIBILITY reference back() _NOEXCEPT;
1045 _LIBCPP_INLINE_VISIBILITY const_reference back() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001046
Marshall Clowe46031a2018-07-02 18:41:15 +00001047 template <class _Tp>
1048 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001049 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001050 <
1051 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1052 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001053 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001054 assign(const _Tp & __t) { __self_view __sv = __t; return assign(__sv.data(), __sv.size()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001055 _LIBCPP_INLINE_VISIBILITY
Marshall Clow95d5e9a2016-03-09 18:08:29 +00001056 basic_string& assign(const basic_string& __str) { return *this = __str; }
Eric Fiselierfc92be82017-04-19 00:28:44 +00001057#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001058 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001059 basic_string& assign(basic_string&& __str)
Marshall Clow5aa9e942015-10-05 16:17:34 +00001060 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001061 {*this = _VSTD::move(__str); return *this;}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001062#endif
Marshall Clow8db7fb02014-03-04 19:17:19 +00001063 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow62953962016-10-03 23:40:48 +00001064 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001065 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001066 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001067 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001068 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1069 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001070 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001071 >
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001072 assign(const _Tp & __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001073 basic_string& assign(const value_type* __s, size_type __n);
1074 basic_string& assign(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001075 basic_string& assign(size_type __n, value_type __c);
1076 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001077 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001078 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001079 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001080 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001081 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001082 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001083 assign(_InputIterator __first, _InputIterator __last);
1084 template<class _ForwardIterator>
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_cpp17_forward_iterator<_ForwardIterator>::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(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001092#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001093 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001094 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001095#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001096
Howard Hinnantcf823322010-12-17 14:46:43 +00001097 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001098 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001099
1100 template <class _Tp>
1101 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001102 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001103 <
1104 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1105 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001106 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001107 insert(size_type __pos1, const _Tp& __t)
1108 { __self_view __sv = __t; return insert(__pos1, __sv.data(), __sv.size()); }
1109
Marshall Clow82513342016-09-24 22:45:42 +00001110 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001111 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001112 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001113 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001114 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001115 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001116 >
Marshall Clow82513342016-09-24 22:45:42 +00001117 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Marshall Clow8db7fb02014-03-04 19:17:19 +00001118 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001119 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1120 basic_string& insert(size_type __pos, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001121 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1122 iterator insert(const_iterator __pos, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001123 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001124 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1125 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001126 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001127 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001128 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001129 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001130 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001131 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001132 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1133 template<class _ForwardIterator>
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_cpp17_forward_iterator<_ForwardIterator>::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, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001141#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001142 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001143 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1144 {return insert(__pos, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001145#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001146
1147 basic_string& erase(size_type __pos = 0, size_type __n = npos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001148 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001149 iterator erase(const_iterator __pos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001150 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001151 iterator erase(const_iterator __first, const_iterator __last);
1152
Howard Hinnantcf823322010-12-17 14:46:43 +00001153 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001154 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001155
1156 template <class _Tp>
1157 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001158 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001159 <
1160 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1161 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001162 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001163 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 +00001164 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 +00001165 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001166 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001167 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001168 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001169 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001170 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001171 >
Marshall Clow82513342016-09-24 22:45:42 +00001172 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001173 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
1174 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001175 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001176 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001177 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001178
1179 template <class _Tp>
1180 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001181 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001182 <
1183 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1184 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001185 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001186 replace(const_iterator __i1, const_iterator __i2, const _Tp& __t) { __self_view __sv = __t; return replace(__i1 - begin(), __i2 - __i1, __sv); }
1187
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001188 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001189 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +00001190 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001191 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Howard Hinnantcf823322010-12-17 14:46:43 +00001192 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001193 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001194 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001195 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001196 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001197 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001198 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001199 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001200 >
Howard Hinnant990d6e82010-11-17 21:11:40 +00001201 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001202#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001203 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001204 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001205 {return replace(__i1, __i2, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001206#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001207
Howard Hinnantd17880b2013-06-28 16:59:19 +00001208 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
Howard Hinnantcf823322010-12-17 14:46:43 +00001209 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001210 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1211
Howard Hinnantcf823322010-12-17 14:46:43 +00001212 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001213 void swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001214#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00001215 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +00001216#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001217 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001218 __is_nothrow_swappable<allocator_type>::value);
1219#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001220
Howard Hinnantcf823322010-12-17 14:46:43 +00001221 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001222 const value_type* c_str() const _NOEXCEPT {return data();}
Howard Hinnantcf823322010-12-17 14:46:43 +00001223 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001224 const value_type* data() const _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Eric Fiselier4ee2a892017-11-20 20:23:27 +00001225#if _LIBCPP_STD_VER > 14 || defined(_LIBCPP_BUILDING_LIBRARY)
Marshall Clowd3c22392016-03-08 15:44:30 +00001226 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001227 value_type* data() _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Marshall Clowd3c22392016-03-08 15:44:30 +00001228#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001229
Howard Hinnantcf823322010-12-17 14:46:43 +00001230 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001231 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001232
Howard Hinnantcf823322010-12-17 14:46:43 +00001233 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001234 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001235
1236 template <class _Tp>
1237 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001238 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001239 <
1240 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1241 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001242 >
zoecarver1997e0a2021-02-05 11:54:47 -08001243 find(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001244 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001245 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001246 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001247 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001248
Howard Hinnantcf823322010-12-17 14:46:43 +00001249 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001250 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001251
1252 template <class _Tp>
1253 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001254 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001255 <
1256 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1257 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001258 >
zoecarver1997e0a2021-02-05 11:54:47 -08001259 rfind(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001260 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001261 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001262 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001263 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001264
Howard Hinnantcf823322010-12-17 14:46:43 +00001265 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001266 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001267
1268 template <class _Tp>
1269 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001270 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001271 <
1272 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1273 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001274 >
zoecarver1997e0a2021-02-05 11:54:47 -08001275 find_first_of(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001276 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001277 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001278 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001279 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001280 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001281
Howard Hinnantcf823322010-12-17 14:46:43 +00001282 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001283 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001284
1285 template <class _Tp>
1286 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001287 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001288 <
1289 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1290 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001291 >
zoecarver1997e0a2021-02-05 11:54:47 -08001292 find_last_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001293 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001294 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001295 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001296 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001297 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001298
Howard Hinnantcf823322010-12-17 14:46:43 +00001299 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001300 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001301
1302 template <class _Tp>
1303 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001304 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001305 <
1306 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1307 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001308 >
zoecarver1997e0a2021-02-05 11:54:47 -08001309 find_first_not_of(const _Tp &__t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001310 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 +00001311 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001312 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001313 _LIBCPP_INLINE_VISIBILITY
1314 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1315
1316 _LIBCPP_INLINE_VISIBILITY
1317 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001318
1319 template <class _Tp>
1320 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001321 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001322 <
1323 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1324 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001325 >
zoecarver1997e0a2021-02-05 11:54:47 -08001326 find_last_not_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001327 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 +00001328 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001329 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001330 _LIBCPP_INLINE_VISIBILITY
1331 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1332
1333 _LIBCPP_INLINE_VISIBILITY
1334 int compare(const basic_string& __str) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001335
1336 template <class _Tp>
1337 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001338 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001339 <
1340 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1341 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001342 >
zoecarver1997e0a2021-02-05 11:54:47 -08001343 compare(const _Tp &__t) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001344
1345 template <class _Tp>
1346 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001347 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001348 <
1349 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1350 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001351 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001352 compare(size_type __pos1, size_type __n1, const _Tp& __t) const;
1353
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001354 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001355 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Marshall Clow8db7fb02014-03-04 19:17:19 +00001356 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 +00001357
Marshall Clow82513342016-09-24 22:45:42 +00001358 template <class _Tp>
Eric Fiselier7d4aa5c2016-10-14 05:29:46 +00001359 inline _LIBCPP_INLINE_VISIBILITY
Louis Dionne9ce598d2021-09-08 09:14:43 -04001360 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001361 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001362 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001363 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001364 >
Marshall Clow82513342016-09-24 22:45:42 +00001365 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 +00001366 int compare(const value_type* __s) const _NOEXCEPT;
1367 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1368 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001369
Marshall Clow18c293b2017-12-04 20:11:38 +00001370#if _LIBCPP_STD_VER > 17
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001371 constexpr _LIBCPP_INLINE_VISIBILITY
1372 bool starts_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001373 { return __self_view(data(), size()).starts_with(__sv); }
1374
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001375 constexpr _LIBCPP_INLINE_VISIBILITY
1376 bool starts_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001377 { return !empty() && _Traits::eq(front(), __c); }
1378
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001379 constexpr _LIBCPP_INLINE_VISIBILITY
1380 bool starts_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001381 { return starts_with(__self_view(__s)); }
1382
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001383 constexpr _LIBCPP_INLINE_VISIBILITY
1384 bool ends_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001385 { return __self_view(data(), size()).ends_with( __sv); }
1386
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001387 constexpr _LIBCPP_INLINE_VISIBILITY
1388 bool ends_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001389 { return !empty() && _Traits::eq(back(), __c); }
1390
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001391 constexpr _LIBCPP_INLINE_VISIBILITY
1392 bool ends_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001393 { return ends_with(__self_view(__s)); }
1394#endif
1395
Wim Leflere023c3542021-01-19 14:33:30 -05001396#if _LIBCPP_STD_VER > 20
1397 constexpr _LIBCPP_INLINE_VISIBILITY
1398 bool contains(__self_view __sv) const noexcept
1399 { return __self_view(data(), size()).contains(__sv); }
1400
1401 constexpr _LIBCPP_INLINE_VISIBILITY
1402 bool contains(value_type __c) const noexcept
1403 { return __self_view(data(), size()).contains(__c); }
1404
1405 constexpr _LIBCPP_INLINE_VISIBILITY
1406 bool contains(const value_type* __s) const
1407 { return __self_view(data(), size()).contains(__s); }
1408#endif
1409
Howard Hinnantcf823322010-12-17 14:46:43 +00001410 _LIBCPP_INLINE_VISIBILITY bool __invariants() const;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001411
Marshall Clowe60a7182018-05-29 17:04:37 +00001412 _LIBCPP_INLINE_VISIBILITY void __clear_and_shrink() _NOEXCEPT;
Marshall Clow851b9ec2019-05-20 21:56:51 +00001413
Marek Kurdejc9848142020-11-26 10:07:16 +01001414 _LIBCPP_INLINE_VISIBILITY void __shrink_or_extend(size_type __target_capacity);
1415
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001416 _LIBCPP_INLINE_VISIBILITY
1417 bool __is_long() const _NOEXCEPT
1418 {return bool(__r_.first().__s.__size_ & __short_mask);}
1419
Louis Dionneba400782020-10-02 15:02:52 -04001420#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001421
1422 bool __dereferenceable(const const_iterator* __i) const;
1423 bool __decrementable(const const_iterator* __i) const;
1424 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1425 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1426
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001427#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001428
Howard Hinnantc51e1022010-05-11 19:42:16 +00001429private:
Nikolas Klauser93826d12022-01-04 17:24:03 +01001430 _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI static bool __fits_in_sso(size_type __sz) {
1431 // SSO is disabled during constant evaluation because `__is_long` isn't constexpr friendly
1432 return !__libcpp_is_constant_evaluated() && (__sz < __min_cap);
1433 }
1434
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001435 _LIBCPP_INLINE_VISIBILITY
1436 allocator_type& __alloc() _NOEXCEPT
1437 {return __r_.second();}
1438 _LIBCPP_INLINE_VISIBILITY
1439 const allocator_type& __alloc() const _NOEXCEPT
1440 {return __r_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001441
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001442#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001443
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001444 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001445 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001446# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001447 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001448# else
1449 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1450# endif
1451
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001452 _LIBCPP_INLINE_VISIBILITY
1453 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001454# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001455 {return __r_.first().__s.__size_ >> 1;}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001456# else
1457 {return __r_.first().__s.__size_;}
1458# endif
1459
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001460#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001461
1462 _LIBCPP_INLINE_VISIBILITY
1463 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001464# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001465 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1466# else
1467 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
1468# endif
1469
1470 _LIBCPP_INLINE_VISIBILITY
1471 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001472# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001473 {return __r_.first().__s.__size_;}
1474# else
1475 {return __r_.first().__s.__size_ >> 1;}
1476# endif
1477
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001478#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001479
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001480 _LIBCPP_INLINE_VISIBILITY
1481 void __set_long_size(size_type __s) _NOEXCEPT
1482 {__r_.first().__l.__size_ = __s;}
1483 _LIBCPP_INLINE_VISIBILITY
1484 size_type __get_long_size() const _NOEXCEPT
1485 {return __r_.first().__l.__size_;}
1486 _LIBCPP_INLINE_VISIBILITY
1487 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001488 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1489
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001490 _LIBCPP_INLINE_VISIBILITY
1491 void __set_long_cap(size_type __s) _NOEXCEPT
1492 {__r_.first().__l.__cap_ = __long_mask | __s;}
1493 _LIBCPP_INLINE_VISIBILITY
1494 size_type __get_long_cap() const _NOEXCEPT
Howard Hinnant28b24882011-12-01 20:21:04 +00001495 {return __r_.first().__l.__cap_ & size_type(~__long_mask);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001496
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001497 _LIBCPP_INLINE_VISIBILITY
1498 void __set_long_pointer(pointer __p) _NOEXCEPT
1499 {__r_.first().__l.__data_ = __p;}
1500 _LIBCPP_INLINE_VISIBILITY
1501 pointer __get_long_pointer() _NOEXCEPT
1502 {return __r_.first().__l.__data_;}
1503 _LIBCPP_INLINE_VISIBILITY
1504 const_pointer __get_long_pointer() const _NOEXCEPT
1505 {return __r_.first().__l.__data_;}
1506 _LIBCPP_INLINE_VISIBILITY
1507 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001508 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001509 _LIBCPP_INLINE_VISIBILITY
1510 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001511 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001512 _LIBCPP_INLINE_VISIBILITY
1513 pointer __get_pointer() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001514 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001515 _LIBCPP_INLINE_VISIBILITY
1516 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001517 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1518
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001519 _LIBCPP_INLINE_VISIBILITY
1520 void __zero() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001521 {
1522 size_type (&__a)[__n_words] = __r_.first().__r.__words;
1523 for (unsigned __i = 0; __i < __n_words; ++__i)
1524 __a[__i] = 0;
1525 }
1526
1527 template <size_type __a> static
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001528 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00001529 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +00001530 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001531 enum {__alignment = 16};
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001532 static _LIBCPP_INLINE_VISIBILITY
1533 size_type __recommend(size_type __s) _NOEXCEPT
Marshall Clow80584522018-02-07 21:30:17 +00001534 {
1535 if (__s < __min_cap) return static_cast<size_type>(__min_cap) - 1;
1536 size_type __guess = __align_it<sizeof(value_type) < __alignment ?
1537 __alignment/sizeof(value_type) : 1 > (__s+1) - 1;
1538 if (__guess == __min_cap) ++__guess;
1539 return __guess;
1540 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001541
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001542 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001543 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001544 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001545 void __init(const value_type* __s, size_type __sz);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001546 inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001547 void __init(size_type __n, value_type __c);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001548
Martijn Vels5e7c9752020-03-04 17:52:46 -05001549 // Slow path for the (inlined) copy constructor for 'long' strings.
1550 // Always externally instantiated and not inlined.
1551 // Requires that __s is zero terminated.
1552 // The main reason for this function to exist is because for unstable, we
1553 // want to allow inlining of the copy constructor. However, we don't want
1554 // to call the __init() functions as those are marked as inline which may
1555 // result in over-aggressive inlining by the compiler, where our aim is
1556 // to only inline the fast path code directly in the ctor.
1557 void __init_copy_ctor_external(const value_type* __s, size_type __sz);
1558
Howard Hinnantc51e1022010-05-11 19:42:16 +00001559 template <class _InputIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001560 inline
Louis Dionne9ce598d2021-09-08 09:14:43 -04001561 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001562 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001563 __is_exactly_cpp17_input_iterator<_InputIterator>::value
1564 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001565 __init(_InputIterator __first, _InputIterator __last);
1566
1567 template <class _ForwardIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001568 inline
Louis Dionne9ce598d2021-09-08 09:14:43 -04001569 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001570 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001571 __is_cpp17_forward_iterator<_ForwardIterator>::value
1572 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001573 __init(_ForwardIterator __first, _ForwardIterator __last);
1574
1575 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001576 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001577 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1578 size_type __n_copy, size_type __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00001579 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001580
Martijn Vels596e3de2020-02-26 15:55:49 -05001581 // __assign_no_alias is invoked for assignment operations where we
1582 // have proof that the input does not alias the current instance.
1583 // For example, operator=(basic_string) performs a 'self' check.
1584 template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04001585 basic_string& __assign_no_alias(const value_type* __s, size_type __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05001586
Howard Hinnantcf823322010-12-17 14:46:43 +00001587 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001588 void __erase_to_end(size_type __pos);
1589
Martijn Velsa81fc792020-02-26 13:25:43 -05001590 // __erase_external_with_move is invoked for erase() invocations where
1591 // `n ~= npos`, likely requiring memory moves on the string data.
1592 void __erase_external_with_move(size_type __pos, size_type __n);
1593
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001594 _LIBCPP_INLINE_VISIBILITY
1595 void __copy_assign_alloc(const basic_string& __str)
1596 {__copy_assign_alloc(__str, integral_constant<bool,
1597 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1598
1599 _LIBCPP_INLINE_VISIBILITY
1600 void __copy_assign_alloc(const basic_string& __str, true_type)
1601 {
Marshall Clowf258c202017-01-31 03:40:52 +00001602 if (__alloc() == __str.__alloc())
1603 __alloc() = __str.__alloc();
1604 else
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001605 {
Marshall Clowf258c202017-01-31 03:40:52 +00001606 if (!__str.__is_long())
1607 {
Vedant Kumar55e007e2018-03-08 21:15:26 +00001608 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001609 __alloc() = __str.__alloc();
1610 }
1611 else
1612 {
1613 allocator_type __a = __str.__alloc();
1614 pointer __p = __alloc_traits::allocate(__a, __str.__get_long_cap());
Vedant Kumar55e007e2018-03-08 21:15:26 +00001615 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001616 __alloc() = _VSTD::move(__a);
1617 __set_long_pointer(__p);
1618 __set_long_cap(__str.__get_long_cap());
1619 __set_long_size(__str.size());
1620 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001621 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001622 }
1623
1624 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001625 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001626 {}
1627
Eric Fiselierfc92be82017-04-19 00:28:44 +00001628#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +00001629 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001630 void __move_assign(basic_string& __str, false_type)
1631 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnantcf823322010-12-17 14:46:43 +00001632 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001633 void __move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001634#if _LIBCPP_STD_VER > 14
1635 _NOEXCEPT;
1636#else
Howard Hinnant3e276872011-06-03 18:40:47 +00001637 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001638#endif
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001639#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001640
1641 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001642 void
Howard Hinnantc2734962011-09-02 20:42:31 +00001643 __move_assign_alloc(basic_string& __str)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001644 _NOEXCEPT_(
1645 !__alloc_traits::propagate_on_container_move_assignment::value ||
1646 is_nothrow_move_assignable<allocator_type>::value)
1647 {__move_assign_alloc(__str, integral_constant<bool,
1648 __alloc_traits::propagate_on_container_move_assignment::value>());}
1649
1650 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00001651 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001652 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1653 {
1654 __alloc() = _VSTD::move(__c.__alloc());
1655 }
1656
1657 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001658 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001659 _NOEXCEPT
1660 {}
1661
Martijn Velsda7d94f2020-06-19 14:24:03 -04001662 basic_string& __assign_external(const value_type* __s);
1663 basic_string& __assign_external(const value_type* __s, size_type __n);
1664
1665 // Assigns the value in __s, guaranteed to be __n < __min_cap in length.
1666 inline basic_string& __assign_short(const value_type* __s, size_type __n) {
1667 pointer __p = __is_long()
1668 ? (__set_long_size(__n), __get_long_pointer())
1669 : (__set_short_size(__n), __get_short_pointer());
1670 traits_type::move(_VSTD::__to_address(__p), __s, __n);
1671 traits_type::assign(__p[__n], value_type());
1672 return *this;
1673 }
1674
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01001675 _LIBCPP_HIDE_FROM_ABI basic_string& __null_terminate_at(value_type* __p, size_type __newsz) {
1676 __set_size(__newsz);
1677 __invalidate_iterators_past(__newsz);
1678 traits_type::assign(__p[__newsz], value_type());
1679 return *this;
1680 }
1681
Howard Hinnantcf823322010-12-17 14:46:43 +00001682 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
1683 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(size_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001684
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001685 template<class _Tp>
1686 _LIBCPP_INLINE_VISIBILITY
1687 bool __addr_in_range(_Tp&& __t) const {
1688 const volatile void *__p = _VSTD::addressof(__t);
1689 return data() <= __p && __p <= data() + size();
1690 }
1691
Louis Dionned24191c2021-08-19 12:39:16 -04001692 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1693 void __throw_length_error() const {
Nikolas Klauserf89bb932022-01-24 19:44:34 +01001694 _VSTD::__throw_length_error("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001695 }
1696
1697 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1698 void __throw_out_of_range() const {
Nikolas Klauserf89bb932022-01-24 19:44:34 +01001699 _VSTD::__throw_out_of_range("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001700 }
1701
Howard Hinnantc51e1022010-05-11 19:42:16 +00001702 friend basic_string operator+<>(const basic_string&, const basic_string&);
1703 friend basic_string operator+<>(const value_type*, const basic_string&);
1704 friend basic_string operator+<>(value_type, const basic_string&);
1705 friend basic_string operator+<>(const basic_string&, const value_type*);
1706 friend basic_string operator+<>(const basic_string&, value_type);
1707};
1708
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001709// These declarations must appear before any functions are implicitly used
1710// so that they have the correct visibility specifier.
1711#ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
Louis Dionne89258142021-08-23 15:32:36 -04001712 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1713# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1714 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1715# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001716#else
Louis Dionne89258142021-08-23 15:32:36 -04001717 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1718# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1719 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1720# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001721#endif
1722
1723
Louis Dionned59f8a52021-08-17 11:59:07 -04001724#if _LIBCPP_STD_VER >= 17
Marshall Clowa0563332018-02-08 06:34:03 +00001725template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -05001726 class _CharT = __iter_value_type<_InputIterator>,
Marshall Clowa0563332018-02-08 06:34:03 +00001727 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001728 class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
1729 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowa0563332018-02-08 06:34:03 +00001730 >
1731basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
1732 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
Marshall Clowe46031a2018-07-02 18:41:15 +00001733
1734template<class _CharT,
1735 class _Traits,
1736 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001737 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowe46031a2018-07-02 18:41:15 +00001738 >
1739explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
1740 -> basic_string<_CharT, _Traits, _Allocator>;
1741
1742template<class _CharT,
1743 class _Traits,
1744 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001745 class = enable_if_t<__is_allocator<_Allocator>::value>,
Marshall Clowe46031a2018-07-02 18:41:15 +00001746 class _Sz = typename allocator_traits<_Allocator>::size_type
1747 >
1748basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
1749 -> basic_string<_CharT, _Traits, _Allocator>;
Marshall Clowa0563332018-02-08 06:34:03 +00001750#endif
1751
Howard Hinnantc51e1022010-05-11 19:42:16 +00001752template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001753inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001754void
1755basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
1756{
Louis Dionneba400782020-10-02 15:02:52 -04001757#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001758 if (!__libcpp_is_constant_evaluated())
1759 __get_db()->__invalidate_all(this);
Louis Dionneba400782020-10-02 15:02:52 -04001760#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001761}
1762
1763template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001764inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001765void
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001766basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type __pos)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001767{
Louis Dionneba400782020-10-02 15:02:52 -04001768#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001769 if (!__libcpp_is_constant_evaluated()) {
1770 __c_node* __c = __get_db()->__find_c_and_lock(this);
1771 if (__c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001772 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001773 const_pointer __new_last = __get_pointer() + __pos;
1774 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001775 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001776 --__p;
1777 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1778 if (__i->base() > __new_last)
1779 {
1780 (*__p)->__c_ = nullptr;
1781 if (--__c->end_ != __p)
1782 _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1783 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001784 }
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001785 __get_db()->unlock();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001786 }
1787 }
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001788#else
1789 (void)__pos;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001790#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001791}
1792
1793template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001794inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001795basic_string<_CharT, _Traits, _Allocator>::basic_string()
Marshall Clowe546cbb2015-06-04 02:05:41 +00001796 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001797 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001798{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001799 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001800 __zero();
1801}
1802
1803template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001804inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001805basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
Eric Fiseliere012bf22015-07-18 20:40:46 +00001806#if _LIBCPP_STD_VER <= 14
1807 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1808#else
1809 _NOEXCEPT
1810#endif
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001811: __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001812{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001813 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001814 __zero();
1815}
1816
1817template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier815ed732016-09-16 00:00:48 +00001818void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1819 size_type __sz,
1820 size_type __reserve)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001821{
1822 if (__reserve > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001823 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001824 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001825 if (__fits_in_sso(__reserve))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001826 {
1827 __set_short_size(__sz);
1828 __p = __get_short_pointer();
1829 }
1830 else
1831 {
1832 size_type __cap = __recommend(__reserve);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001833 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001834 __set_long_pointer(__p);
1835 __set_long_cap(__cap+1);
1836 __set_long_size(__sz);
1837 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001838 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001839 traits_type::assign(__p[__sz], value_type());
1840}
1841
1842template <class _CharT, class _Traits, class _Allocator>
1843void
Howard Hinnantd17880b2013-06-28 16:59:19 +00001844basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001845{
1846 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001847 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001848 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001849 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001850 {
1851 __set_short_size(__sz);
1852 __p = __get_short_pointer();
1853 }
1854 else
1855 {
1856 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001857 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001858 __set_long_pointer(__p);
1859 __set_long_cap(__cap+1);
1860 __set_long_size(__sz);
1861 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001862 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001863 traits_type::assign(__p[__sz], value_type());
1864}
1865
1866template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00001867template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00001868basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001869 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001870{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001871 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001872 __init(__s, traits_type::length(__s));
Nikolas Klauserf2807732022-01-11 00:33:35 +01001873 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001874}
1875
1876template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001877inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001878basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001879 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001880{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001881 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
1882 __init(__s, __n);
1883 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001884}
1885
1886template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001887inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001888basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001889 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001890{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001891 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001892 __init(__s, __n);
Nikolas Klauserf2807732022-01-11 00:33:35 +01001893 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001894}
1895
1896template <class _CharT, class _Traits, class _Allocator>
1897basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001898 : __r_(__default_init_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001899{
1900 if (!__str.__is_long())
1901 __r_.first().__r = __str.__r_.first().__r;
1902 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001903 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1904 __str.__get_long_size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01001905 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001906}
1907
1908template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00001909basic_string<_CharT, _Traits, _Allocator>::basic_string(
1910 const basic_string& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001911 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001912{
1913 if (!__str.__is_long())
1914 __r_.first().__r = __str.__r_.first().__r;
1915 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001916 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1917 __str.__get_long_size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01001918 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001919}
1920
Martijn Vels5e7c9752020-03-04 17:52:46 -05001921template <class _CharT, class _Traits, class _Allocator>
1922void basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(
1923 const value_type* __s, size_type __sz) {
1924 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001925 if (__fits_in_sso(__sz)) {
Martijn Vels5e7c9752020-03-04 17:52:46 -05001926 __p = __get_short_pointer();
1927 __set_short_size(__sz);
1928 } else {
1929 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001930 __throw_length_error();
Martijn Vels5e7c9752020-03-04 17:52:46 -05001931 size_t __cap = __recommend(__sz);
1932 __p = __alloc_traits::allocate(__alloc(), __cap + 1);
1933 __set_long_pointer(__p);
1934 __set_long_cap(__cap + 1);
1935 __set_long_size(__sz);
1936 }
1937 traits_type::copy(_VSTD::__to_address(__p), __s, __sz + 1);
1938}
1939
Eric Fiselierfc92be82017-04-19 00:28:44 +00001940#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001941
1942template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001943inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001944basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +00001945#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +00001946 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowa80abc72015-06-03 19:56:43 +00001947#else
1948 _NOEXCEPT
1949#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001950 : __r_(_VSTD::move(__str.__r_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001951{
1952 __str.__zero();
Nikolas Klauserf2807732022-01-11 00:33:35 +01001953 _VSTD::__debug_db_insert_c(this);
Louis Dionneba400782020-10-02 15:02:52 -04001954#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauserf2807732022-01-11 00:33:35 +01001955 if (!__libcpp_is_constant_evaluated() && __is_long())
1956 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001957#endif
1958}
1959
1960template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001961inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001962basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001963 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001964{
Marshall Clowd2d24692014-07-17 15:32:20 +00001965 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001966 __init(_VSTD::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd2d24692014-07-17 15:32:20 +00001967 else
1968 {
1969 __r_.first().__r = __str.__r_.first().__r;
1970 __str.__zero();
1971 }
Nikolas Klauserf2807732022-01-11 00:33:35 +01001972 _VSTD::__debug_db_insert_c(this);
Louis Dionneba400782020-10-02 15:02:52 -04001973#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauserf2807732022-01-11 00:33:35 +01001974 if (!__libcpp_is_constant_evaluated() && __is_long())
1975 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001976#endif
1977}
1978
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001979#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001980
1981template <class _CharT, class _Traits, class _Allocator>
1982void
1983basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
1984{
1985 if (__n > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001986 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001987 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001988 if (__fits_in_sso(__n))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001989 {
1990 __set_short_size(__n);
1991 __p = __get_short_pointer();
1992 }
1993 else
1994 {
1995 size_type __cap = __recommend(__n);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001996 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001997 __set_long_pointer(__p);
1998 __set_long_cap(__cap+1);
1999 __set_long_size(__n);
2000 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002001 traits_type::assign(_VSTD::__to_address(__p), __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002002 traits_type::assign(__p[__n], value_type());
2003}
2004
2005template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002006inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002007basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002008 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002009{
2010 __init(__n, __c);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002011 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002012}
2013
2014template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002015template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00002016basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002017 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002018{
2019 __init(__n, __c);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002020 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002021}
2022
Howard Hinnantc51e1022010-05-11 19:42:16 +00002023template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00002024basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
2025 size_type __pos, size_type __n,
2026 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002027 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002028{
2029 size_type __str_sz = __str.size();
2030 if (__pos > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002031 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002032 __init(__str.data() + __pos, _VSTD::min(__n, __str_sz - __pos));
Nikolas Klauserf2807732022-01-11 00:33:35 +01002033 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002034}
2035
2036template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002037inline
Marshall Clow83445802016-04-07 18:13:41 +00002038basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +00002039 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002040 : __r_(__default_init_tag(), __a)
Marshall Clow83445802016-04-07 18:13:41 +00002041{
2042 size_type __str_sz = __str.size();
2043 if (__pos > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002044 __throw_out_of_range();
Marshall Clow83445802016-04-07 18:13:41 +00002045 __init(__str.data() + __pos, __str_sz - __pos);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002046 _VSTD::__debug_db_insert_c(this);
Marshall Clow83445802016-04-07 18:13:41 +00002047}
2048
2049template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002050template <class _Tp, class>
Marshall Clow78dbe462016-11-14 18:22:19 +00002051basic_string<_CharT, _Traits, _Allocator>::basic_string(
Marshall Clowe46031a2018-07-02 18:41:15 +00002052 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002053 : __r_(__default_init_tag(), __a)
Marshall Clow78dbe462016-11-14 18:22:19 +00002054{
Marshall Clowe46031a2018-07-02 18:41:15 +00002055 __self_view __sv0 = __t;
2056 __self_view __sv = __sv0.substr(__pos, __n);
Marshall Clow78dbe462016-11-14 18:22:19 +00002057 __init(__sv.data(), __sv.size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002058 _VSTD::__debug_db_insert_c(this);
Marshall Clow78dbe462016-11-14 18:22:19 +00002059}
2060
2061template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002062template <class _Tp, class>
2063basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002064 : __r_(__default_init_tag(), __default_init_tag())
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002065{
Marshall Clowe46031a2018-07-02 18:41:15 +00002066 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002067 __init(__sv.data(), __sv.size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002068 _VSTD::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002069}
2070
2071template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002072template <class _Tp, class>
2073basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002074 : __r_(__default_init_tag(), __a)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002075{
Marshall Clowe46031a2018-07-02 18:41:15 +00002076 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002077 __init(__sv.data(), __sv.size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002078 _VSTD::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002079}
2080
2081template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002082template <class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002083__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002084<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002085 __is_exactly_cpp17_input_iterator<_InputIterator>::value
2086>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002087basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
2088{
2089 __zero();
2090#ifndef _LIBCPP_NO_EXCEPTIONS
2091 try
2092 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002093#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002094 for (; __first != __last; ++__first)
2095 push_back(*__first);
2096#ifndef _LIBCPP_NO_EXCEPTIONS
2097 }
2098 catch (...)
2099 {
2100 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002101 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002102 throw;
2103 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002104#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002105}
2106
2107template <class _CharT, class _Traits, class _Allocator>
2108template <class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002109__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002110<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002111 __is_cpp17_forward_iterator<_ForwardIterator>::value
2112>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002113basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
2114{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002115 size_type __sz = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002116 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002117 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002118 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002119 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002120 {
2121 __set_short_size(__sz);
2122 __p = __get_short_pointer();
2123 }
2124 else
2125 {
2126 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002127 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002128 __set_long_pointer(__p);
2129 __set_long_cap(__cap+1);
2130 __set_long_size(__sz);
2131 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002132
2133#ifndef _LIBCPP_NO_EXCEPTIONS
2134 try
2135 {
2136#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002137 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002138 traits_type::assign(*__p, *__first);
2139 traits_type::assign(*__p, value_type());
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002140#ifndef _LIBCPP_NO_EXCEPTIONS
2141 }
2142 catch (...)
2143 {
2144 if (__is_long())
2145 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
2146 throw;
2147 }
2148#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002149}
2150
2151template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002152template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002153inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002154basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002155 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002156{
2157 __init(__first, __last);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002158 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002159}
2160
2161template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002162template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002163inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002164basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
2165 const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002166 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002167{
2168 __init(__first, __last);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002169 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002170}
2171
Eric Fiselierfc92be82017-04-19 00:28:44 +00002172#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002173
Howard Hinnantc51e1022010-05-11 19:42:16 +00002174template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002175inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002176basic_string<_CharT, _Traits, _Allocator>::basic_string(
2177 initializer_list<_CharT> __il)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002178 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002179{
2180 __init(__il.begin(), __il.end());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002181 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002182}
2183
2184template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002185inline
Eric Fiselier9d355982017-04-12 23:45:53 +00002186
Eric Fiselier812882b2017-02-17 01:17:10 +00002187basic_string<_CharT, _Traits, _Allocator>::basic_string(
2188 initializer_list<_CharT> __il, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002189 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002190{
2191 __init(__il.begin(), __il.end());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002192 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002193}
2194
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002195#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002196
Howard Hinnantc51e1022010-05-11 19:42:16 +00002197template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002198basic_string<_CharT, _Traits, _Allocator>::~basic_string()
2199{
Louis Dionneba400782020-10-02 15:02:52 -04002200#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002201 if (!__libcpp_is_constant_evaluated())
2202 __get_db()->__erase_c(this);
Howard Hinnant8ea98242013-08-23 17:37:05 +00002203#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002204 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002205 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002206}
2207
2208template <class _CharT, class _Traits, class _Allocator>
2209void
2210basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
2211 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002212 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 +00002213{
2214 size_type __ms = max_size();
2215 if (__delta_cap > __ms - __old_cap - 1)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002216 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002217 pointer __old_p = __get_pointer();
2218 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002219 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002220 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002221 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002222 __invalidate_all_iterators();
2223 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002224 traits_type::copy(_VSTD::__to_address(__p),
2225 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002226 if (__n_add != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002227 traits_type::copy(_VSTD::__to_address(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002228 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2229 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002230 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2231 _VSTD::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002232 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002233 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002234 __set_long_pointer(__p);
2235 __set_long_cap(__cap+1);
2236 __old_sz = __n_copy + __n_add + __sec_cp_sz;
2237 __set_long_size(__old_sz);
2238 traits_type::assign(__p[__old_sz], value_type());
2239}
2240
2241template <class _CharT, class _Traits, class _Allocator>
2242void
2243basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2244 size_type __n_copy, size_type __n_del, size_type __n_add)
2245{
2246 size_type __ms = max_size();
Marshall Clow2267c5d2013-11-06 14:24:38 +00002247 if (__delta_cap > __ms - __old_cap)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002248 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002249 pointer __old_p = __get_pointer();
2250 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002251 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002252 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002253 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002254 __invalidate_all_iterators();
2255 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002256 traits_type::copy(_VSTD::__to_address(__p),
2257 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002258 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2259 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002260 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2261 _VSTD::__to_address(__old_p) + __n_copy + __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002262 __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}
2268
2269// assign
2270
2271template <class _CharT, class _Traits, class _Allocator>
Martijn Vels596e3de2020-02-26 15:55:49 -05002272template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04002273basic_string<_CharT, _Traits, _Allocator>&
2274basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(
Martijn Vels596e3de2020-02-26 15:55:49 -05002275 const value_type* __s, size_type __n) {
2276 size_type __cap = __is_short ? __min_cap : __get_long_cap();
2277 if (__n < __cap) {
2278 pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
2279 __is_short ? __set_short_size(__n) : __set_long_size(__n);
2280 traits_type::copy(_VSTD::__to_address(__p), __s, __n);
2281 traits_type::assign(__p[__n], value_type());
2282 __invalidate_iterators_past(__n);
2283 } else {
2284 size_type __sz = __is_short ? __get_short_size() : __get_long_size();
2285 __grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
2286 }
Martijn Velsb6a08b62020-04-10 18:36:31 -04002287 return *this;
Martijn Vels596e3de2020-02-26 15:55:49 -05002288}
2289
2290template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002291basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002292basic_string<_CharT, _Traits, _Allocator>::__assign_external(
2293 const value_type* __s, size_type __n) {
2294 size_type __cap = capacity();
2295 if (__cap >= __n) {
2296 value_type* __p = _VSTD::__to_address(__get_pointer());
2297 traits_type::move(__p, __s, __n);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002298 return __null_terminate_at(__p, __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04002299 } else {
2300 size_type __sz = size();
2301 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002302 return *this;
Martijn Velsda7d94f2020-06-19 14:24:03 -04002303 }
Martijn Velsda7d94f2020-06-19 14:24:03 -04002304}
2305
2306template <class _CharT, class _Traits, class _Allocator>
2307basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002308basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002309{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002310 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Nikolas Klauser93826d12022-01-04 17:24:03 +01002311 return (__builtin_constant_p(__n) && __fits_in_sso(__n))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002312 ? __assign_short(__s, __n)
2313 : __assign_external(__s, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002314}
2315
2316template <class _CharT, class _Traits, class _Allocator>
2317basic_string<_CharT, _Traits, _Allocator>&
2318basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2319{
2320 size_type __cap = capacity();
2321 if (__cap < __n)
2322 {
2323 size_type __sz = size();
2324 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2325 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002326 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002327 traits_type::assign(__p, __n, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002328 return __null_terminate_at(__p, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002329}
2330
2331template <class _CharT, class _Traits, class _Allocator>
2332basic_string<_CharT, _Traits, _Allocator>&
2333basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2334{
2335 pointer __p;
2336 if (__is_long())
2337 {
2338 __p = __get_long_pointer();
2339 __set_long_size(1);
2340 }
2341 else
2342 {
2343 __p = __get_short_pointer();
2344 __set_short_size(1);
2345 }
2346 traits_type::assign(*__p, __c);
2347 traits_type::assign(*++__p, value_type());
2348 __invalidate_iterators_past(1);
2349 return *this;
2350}
2351
2352template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002353basic_string<_CharT, _Traits, _Allocator>&
2354basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2355{
Martijn Vels596e3de2020-02-26 15:55:49 -05002356 if (this != &__str) {
2357 __copy_assign_alloc(__str);
2358 if (!__is_long()) {
2359 if (!__str.__is_long()) {
Eric Fiselier03bbc922020-01-15 17:27:10 -05002360 __r_.first().__r = __str.__r_.first().__r;
Martijn Vels596e3de2020-02-26 15:55:49 -05002361 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002362 return __assign_no_alias<true>(__str.data(), __str.size());
Martijn Vels596e3de2020-02-26 15:55:49 -05002363 }
2364 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002365 return __assign_no_alias<false>(__str.data(), __str.size());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002366 }
Martijn Vels596e3de2020-02-26 15:55:49 -05002367 }
2368 return *this;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002369}
2370
Eric Fiselierfc92be82017-04-19 00:28:44 +00002371#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002372
2373template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002374inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002375void
2376basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002377 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002378{
2379 if (__alloc() != __str.__alloc())
2380 assign(__str);
2381 else
2382 __move_assign(__str, true_type());
2383}
2384
2385template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002386inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002387void
2388basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002389#if _LIBCPP_STD_VER > 14
2390 _NOEXCEPT
2391#else
Howard Hinnant3e276872011-06-03 18:40:47 +00002392 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002393#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002394{
marshall2ed41622019-11-27 07:13:00 -08002395 if (__is_long()) {
2396 __alloc_traits::deallocate(__alloc(), __get_long_pointer(),
2397 __get_long_cap());
2398#if _LIBCPP_STD_VER <= 14
2399 if (!is_nothrow_move_assignable<allocator_type>::value) {
2400 __set_short_size(0);
2401 traits_type::assign(__get_short_pointer()[0], value_type());
2402 }
2403#endif
2404 }
2405 __move_assign_alloc(__str);
2406 __r_.first() = __str.__r_.first();
2407 __str.__set_short_size(0);
2408 traits_type::assign(__str.__get_short_pointer()[0], value_type());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002409}
2410
2411template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002412inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002413basic_string<_CharT, _Traits, _Allocator>&
2414basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002415 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002416{
2417 __move_assign(__str, integral_constant<bool,
2418 __alloc_traits::propagate_on_container_move_assignment::value>());
2419 return *this;
2420}
2421
2422#endif
2423
2424template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002425template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002426__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002427<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002428 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002429 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002430>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002431basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2432{
Marshall Clow958362f2016-09-05 01:54:30 +00002433 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002434 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002435 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002436}
2437
2438template <class _CharT, class _Traits, class _Allocator>
2439template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002440__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002441<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002442 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002443 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002444>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002445basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2446{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002447 size_type __cap = capacity();
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002448 size_type __n = __string_is_trivial_iterator<_ForwardIterator>::value ?
2449 static_cast<size_type>(_VSTD::distance(__first, __last)) : 0;
2450
2451 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2452 (__cap >= __n || !__addr_in_range(*__first)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002453 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002454 if (__cap < __n)
2455 {
2456 size_type __sz = size();
2457 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2458 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002459 pointer __p = __get_pointer();
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002460 for (; __first != __last; ++__p, (void) ++__first)
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002461 traits_type::assign(*__p, *__first);
2462 traits_type::assign(*__p, value_type());
2463 __set_size(__n);
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04002464 __invalidate_iterators_past(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002465 }
2466 else
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002467 {
2468 const basic_string __temp(__first, __last, __alloc());
2469 assign(__temp.data(), __temp.size());
2470 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002471 return *this;
2472}
2473
2474template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002475basic_string<_CharT, _Traits, _Allocator>&
2476basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2477{
2478 size_type __sz = __str.size();
2479 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002480 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002481 return assign(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002482}
2483
2484template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002485template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002486__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002487<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002488 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
2489 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00002490 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002491>
Marshall Clow82513342016-09-24 22:45:42 +00002492basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002493{
Marshall Clow82513342016-09-24 22:45:42 +00002494 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002495 size_type __sz = __sv.size();
2496 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002497 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002498 return assign(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2499}
2500
2501
2502template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002503basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002504basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s) {
2505 return __assign_external(__s, traits_type::length(__s));
2506}
2507
2508template <class _CharT, class _Traits, class _Allocator>
2509basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002510basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002511{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002512 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Louis Dionne3e0c4332021-08-31 10:49:06 -04002513 return __builtin_constant_p(*__s)
Nikolas Klauser93826d12022-01-04 17:24:03 +01002514 ? (__fits_in_sso(traits_type::length(__s))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002515 ? __assign_short(__s, traits_type::length(__s))
2516 : __assign_external(__s, traits_type::length(__s)))
2517 : __assign_external(__s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002518}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002519// append
2520
2521template <class _CharT, class _Traits, class _Allocator>
2522basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002523basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002524{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002525 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002526 size_type __cap = capacity();
2527 size_type __sz = size();
2528 if (__cap - __sz >= __n)
2529 {
2530 if (__n)
2531 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002532 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002533 traits_type::copy(__p + __sz, __s, __n);
2534 __sz += __n;
2535 __set_size(__sz);
2536 traits_type::assign(__p[__sz], value_type());
2537 }
2538 }
2539 else
2540 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2541 return *this;
2542}
2543
2544template <class _CharT, class _Traits, class _Allocator>
2545basic_string<_CharT, _Traits, _Allocator>&
2546basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2547{
2548 if (__n)
2549 {
2550 size_type __cap = capacity();
2551 size_type __sz = size();
2552 if (__cap - __sz < __n)
2553 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2554 pointer __p = __get_pointer();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002555 traits_type::assign(_VSTD::__to_address(__p) + __sz, __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002556 __sz += __n;
2557 __set_size(__sz);
2558 traits_type::assign(__p[__sz], value_type());
2559 }
2560 return *this;
2561}
2562
2563template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00002564inline void
2565basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n)
2566{
2567 if (__n)
2568 {
2569 size_type __cap = capacity();
2570 size_type __sz = size();
2571 if (__cap - __sz < __n)
2572 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2573 pointer __p = __get_pointer();
2574 __sz += __n;
2575 __set_size(__sz);
2576 traits_type::assign(__p[__sz], value_type());
2577 }
2578}
2579
2580template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002581void
2582basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2583{
Howard Hinnant68bf1812013-04-30 21:44:48 +00002584 bool __is_short = !__is_long();
2585 size_type __cap;
2586 size_type __sz;
2587 if (__is_short)
2588 {
2589 __cap = __min_cap - 1;
2590 __sz = __get_short_size();
2591 }
2592 else
2593 {
2594 __cap = __get_long_cap() - 1;
2595 __sz = __get_long_size();
2596 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002597 if (__sz == __cap)
Howard Hinnant68bf1812013-04-30 21:44:48 +00002598 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002599 __grow_by(__cap, 1, __sz, __sz, 0);
Louis Dionne82d2b2c2021-11-16 11:26:56 -05002600 __is_short = false; // the string is always long after __grow_by
Howard Hinnant68bf1812013-04-30 21:44:48 +00002601 }
2602 pointer __p;
2603 if (__is_short)
2604 {
2605 __p = __get_short_pointer() + __sz;
2606 __set_short_size(__sz+1);
2607 }
2608 else
2609 {
2610 __p = __get_long_pointer() + __sz;
2611 __set_long_size(__sz+1);
2612 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002613 traits_type::assign(*__p, __c);
2614 traits_type::assign(*++__p, value_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002615}
2616
Howard Hinnantc51e1022010-05-11 19:42:16 +00002617template <class _CharT, class _Traits, class _Allocator>
2618template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002619__enable_if_t
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002620<
2621 __is_cpp17_forward_iterator<_ForwardIterator>::value,
2622 basic_string<_CharT, _Traits, _Allocator>&
2623>
2624basic_string<_CharT, _Traits, _Allocator>::append(
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002625 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002626{
2627 size_type __sz = size();
2628 size_type __cap = capacity();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002629 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002630 if (__n)
2631 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002632 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2633 !__addr_in_range(*__first))
Marshall Clow958362f2016-09-05 01:54:30 +00002634 {
2635 if (__cap - __sz < __n)
2636 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2637 pointer __p = __get_pointer() + __sz;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002638 for (; __first != __last; ++__p, (void) ++__first)
Marshall Clow958362f2016-09-05 01:54:30 +00002639 traits_type::assign(*__p, *__first);
2640 traits_type::assign(*__p, value_type());
2641 __set_size(__sz + __n);
2642 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002643 else
2644 {
2645 const basic_string __temp(__first, __last, __alloc());
2646 append(__temp.data(), __temp.size());
2647 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002648 }
2649 return *this;
2650}
2651
2652template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002653inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002654basic_string<_CharT, _Traits, _Allocator>&
2655basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2656{
2657 return append(__str.data(), __str.size());
2658}
2659
2660template <class _CharT, class _Traits, class _Allocator>
2661basic_string<_CharT, _Traits, _Allocator>&
2662basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2663{
2664 size_type __sz = __str.size();
2665 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002666 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002667 return append(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002668}
2669
2670template <class _CharT, class _Traits, class _Allocator>
Marshall Clow62953962016-10-03 23:40:48 +00002671template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002672 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002673 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002674 __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 +00002675 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002676 >
Marshall Clow82513342016-09-24 22:45:42 +00002677basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002678{
Marshall Clow82513342016-09-24 22:45:42 +00002679 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002680 size_type __sz = __sv.size();
2681 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002682 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002683 return append(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2684}
2685
2686template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002687basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002688basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002689{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002690 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002691 return append(__s, traits_type::length(__s));
2692}
2693
2694// insert
2695
2696template <class _CharT, class _Traits, class _Allocator>
2697basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002698basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002699{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002700 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002701 size_type __sz = size();
2702 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002703 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002704 size_type __cap = capacity();
2705 if (__cap - __sz >= __n)
2706 {
2707 if (__n)
2708 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002709 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002710 size_type __n_move = __sz - __pos;
2711 if (__n_move != 0)
2712 {
2713 if (__p + __pos <= __s && __s < __p + __sz)
2714 __s += __n;
2715 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2716 }
2717 traits_type::move(__p + __pos, __s, __n);
2718 __sz += __n;
2719 __set_size(__sz);
2720 traits_type::assign(__p[__sz], value_type());
2721 }
2722 }
2723 else
2724 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2725 return *this;
2726}
2727
2728template <class _CharT, class _Traits, class _Allocator>
2729basic_string<_CharT, _Traits, _Allocator>&
2730basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2731{
2732 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 if (__n)
2736 {
2737 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002738 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002739 if (__cap - __sz >= __n)
2740 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002741 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002742 size_type __n_move = __sz - __pos;
2743 if (__n_move != 0)
2744 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2745 }
2746 else
2747 {
2748 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002749 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002750 }
2751 traits_type::assign(__p + __pos, __n, __c);
2752 __sz += __n;
2753 __set_size(__sz);
2754 traits_type::assign(__p[__sz], value_type());
2755 }
2756 return *this;
2757}
2758
2759template <class _CharT, class _Traits, class _Allocator>
2760template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002761__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002762<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002763 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Marshall Clow039b2f02016-01-13 21:54:34 +00002764 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002765>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002766basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2767{
Nikolas Klausereed25832021-12-15 01:32:30 +01002768 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2769 "string::insert(iterator, range) called with an iterator not"
2770 " referring to this string");
2771 const basic_string __temp(__first, __last, __alloc());
2772 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002773}
2774
2775template <class _CharT, class _Traits, class _Allocator>
2776template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002777__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002778<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002779 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002780 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002781>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002782basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2783{
Nikolas Klausereed25832021-12-15 01:32:30 +01002784 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2785 "string::insert(iterator, range) called with an iterator not"
2786 " referring to this string");
2787
Howard Hinnantc51e1022010-05-11 19:42:16 +00002788 size_type __ip = static_cast<size_type>(__pos - begin());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002789 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002790 if (__n)
2791 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002792 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2793 !__addr_in_range(*__first))
2794 {
2795 size_type __sz = size();
2796 size_type __cap = capacity();
2797 value_type* __p;
2798 if (__cap - __sz >= __n)
2799 {
2800 __p = _VSTD::__to_address(__get_pointer());
2801 size_type __n_move = __sz - __ip;
2802 if (__n_move != 0)
2803 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
2804 }
2805 else
2806 {
2807 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
2808 __p = _VSTD::__to_address(__get_long_pointer());
2809 }
2810 __sz += __n;
2811 __set_size(__sz);
2812 traits_type::assign(__p[__sz], value_type());
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002813 for (__p += __ip; __first != __last; ++__p, (void) ++__first)
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002814 traits_type::assign(*__p, *__first);
2815 }
2816 else
Marshall Clow958362f2016-09-05 01:54:30 +00002817 {
2818 const basic_string __temp(__first, __last, __alloc());
2819 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
2820 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002821 }
2822 return begin() + __ip;
2823}
2824
2825template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002826inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002827basic_string<_CharT, _Traits, _Allocator>&
2828basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2829{
2830 return insert(__pos1, __str.data(), __str.size());
2831}
2832
2833template <class _CharT, class _Traits, class _Allocator>
2834basic_string<_CharT, _Traits, _Allocator>&
2835basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2836 size_type __pos2, size_type __n)
2837{
2838 size_type __str_sz = __str.size();
2839 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002840 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002841 return insert(__pos1, __str.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002842}
2843
2844template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002845template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002846__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002847<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002848 __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 +00002849 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002850>
Marshall Clow82513342016-09-24 22:45:42 +00002851basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002852 size_type __pos2, size_type __n)
2853{
Marshall Clow82513342016-09-24 22:45:42 +00002854 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002855 size_type __str_sz = __sv.size();
2856 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002857 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002858 return insert(__pos1, __sv.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
2859}
2860
2861template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002862basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002863basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002864{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002865 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002866 return insert(__pos, __s, traits_type::length(__s));
2867}
2868
2869template <class _CharT, class _Traits, class _Allocator>
2870typename basic_string<_CharT, _Traits, _Allocator>::iterator
2871basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2872{
Louis Dionnef4cc6fb2022-02-15 15:47:45 -05002873 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2874 "string::insert(iterator, character) called with an iterator not"
2875 " referring to this string");
2876
Howard Hinnantc51e1022010-05-11 19:42:16 +00002877 size_type __ip = static_cast<size_type>(__pos - begin());
2878 size_type __sz = size();
2879 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002880 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002881 if (__cap == __sz)
2882 {
2883 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002884 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002885 }
2886 else
2887 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002888 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002889 size_type __n_move = __sz - __ip;
2890 if (__n_move != 0)
2891 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2892 }
2893 traits_type::assign(__p[__ip], __c);
2894 traits_type::assign(__p[++__sz], value_type());
2895 __set_size(__sz);
2896 return begin() + static_cast<difference_type>(__ip);
2897}
2898
2899template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002900inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002901typename basic_string<_CharT, _Traits, _Allocator>::iterator
2902basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2903{
Nikolas Klausereed25832021-12-15 01:32:30 +01002904 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2905 "string::insert(iterator, n, value) called with an iterator not"
2906 " referring to this string");
2907 difference_type __p = __pos - begin();
2908 insert(static_cast<size_type>(__p), __n, __c);
2909 return begin() + __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002910}
2911
2912// replace
2913
2914template <class _CharT, class _Traits, class _Allocator>
2915basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002916basic_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 +00002917 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00002918{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002919 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002920 size_type __sz = size();
2921 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002922 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002923 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002924 size_type __cap = capacity();
2925 if (__cap - __sz + __n1 >= __n2)
2926 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002927 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002928 if (__n1 != __n2)
2929 {
2930 size_type __n_move = __sz - __pos - __n1;
2931 if (__n_move != 0)
2932 {
2933 if (__n1 > __n2)
2934 {
2935 traits_type::move(__p + __pos, __s, __n2);
2936 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002937 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002938 }
2939 if (__p + __pos < __s && __s < __p + __sz)
2940 {
2941 if (__p + __pos + __n1 <= __s)
2942 __s += __n2 - __n1;
2943 else // __p + __pos < __s < __p + __pos + __n1
2944 {
2945 traits_type::move(__p + __pos, __s, __n1);
2946 __pos += __n1;
2947 __s += __n2;
2948 __n2 -= __n1;
2949 __n1 = 0;
2950 }
2951 }
2952 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2953 }
2954 }
2955 traits_type::move(__p + __pos, __s, __n2);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002956 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002957 }
2958 else
2959 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
2960 return *this;
2961}
2962
2963template <class _CharT, class _Traits, class _Allocator>
2964basic_string<_CharT, _Traits, _Allocator>&
2965basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
2966{
2967 size_type __sz = size();
2968 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002969 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002970 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002971 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002972 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002973 if (__cap - __sz + __n1 >= __n2)
2974 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002975 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002976 if (__n1 != __n2)
2977 {
2978 size_type __n_move = __sz - __pos - __n1;
2979 if (__n_move != 0)
2980 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2981 }
2982 }
2983 else
2984 {
2985 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002986 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002987 }
2988 traits_type::assign(__p + __pos, __n2, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002989 return __null_terminate_at(__p, __sz - (__n1 - __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002990}
2991
2992template <class _CharT, class _Traits, class _Allocator>
2993template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002994__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002995<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002996 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002997 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002998>
Howard Hinnant990d6e82010-11-17 21:11:40 +00002999basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003000 _InputIterator __j1, _InputIterator __j2)
3001{
Marshall Clow958362f2016-09-05 01:54:30 +00003002 const basic_string __temp(__j1, __j2, __alloc());
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003003 return replace(__i1, __i2, __temp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003004}
3005
3006template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003007inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003008basic_string<_CharT, _Traits, _Allocator>&
3009basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
3010{
3011 return replace(__pos1, __n1, __str.data(), __str.size());
3012}
3013
3014template <class _CharT, class _Traits, class _Allocator>
3015basic_string<_CharT, _Traits, _Allocator>&
3016basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
3017 size_type __pos2, size_type __n2)
3018{
3019 size_type __str_sz = __str.size();
3020 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003021 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003022 return replace(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003023}
3024
3025template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003026template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003027__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003028<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003029 __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 +00003030 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003031>
Marshall Clow82513342016-09-24 22:45:42 +00003032basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003033 size_type __pos2, size_type __n2)
3034{
Marshall Clow82513342016-09-24 22:45:42 +00003035 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003036 size_type __str_sz = __sv.size();
3037 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003038 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003039 return replace(__pos1, __n1, __sv.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
3040}
3041
3042template <class _CharT, class _Traits, class _Allocator>
3043basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003044basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003045{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003046 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003047 return replace(__pos, __n1, __s, traits_type::length(__s));
3048}
3049
3050template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003051inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003052basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003053basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003054{
3055 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
3056 __str.data(), __str.size());
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 Hinnantd17880b2013-06-28 16:59:19 +00003062basic_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 +00003063{
3064 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
3065}
3066
3067template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003068inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003069basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003070basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003071{
3072 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
3073}
3074
3075template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003076inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003077basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003078basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003079{
3080 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
3081}
3082
3083// erase
3084
Martijn Velsa81fc792020-02-26 13:25:43 -05003085// 'externally instantiated' erase() implementation, called when __n != npos.
3086// Does not check __pos against size()
Howard Hinnantc51e1022010-05-11 19:42:16 +00003087template <class _CharT, class _Traits, class _Allocator>
Martijn Velsa81fc792020-02-26 13:25:43 -05003088void
3089basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(
3090 size_type __pos, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003091{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003092 if (__n)
3093 {
Martijn Velsa81fc792020-02-26 13:25:43 -05003094 size_type __sz = size();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003095 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003096 __n = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003097 size_type __n_move = __sz - __pos - __n;
3098 if (__n_move != 0)
3099 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003100 __null_terminate_at(__p, __sz - __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003101 }
Martijn Velsa81fc792020-02-26 13:25:43 -05003102}
3103
3104template <class _CharT, class _Traits, class _Allocator>
3105basic_string<_CharT, _Traits, _Allocator>&
3106basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos,
3107 size_type __n) {
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003108 if (__pos > size())
3109 __throw_out_of_range();
Martijn Velsa81fc792020-02-26 13:25:43 -05003110 if (__n == npos) {
3111 __erase_to_end(__pos);
3112 } else {
3113 __erase_external_with_move(__pos, __n);
3114 }
3115 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003116}
3117
3118template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003119inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003120typename basic_string<_CharT, _Traits, _Allocator>::iterator
3121basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
3122{
Nikolas Klausereed25832021-12-15 01:32:30 +01003123 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3124 "string::erase(iterator) called with an iterator not"
3125 " referring to this string");
3126
3127 _LIBCPP_ASSERT(__pos != end(), "string::erase(iterator) called with a non-dereferenceable iterator");
3128 iterator __b = begin();
3129 size_type __r = static_cast<size_type>(__pos - __b);
3130 erase(__r, 1);
3131 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003132}
3133
3134template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003135inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003136typename basic_string<_CharT, _Traits, _Allocator>::iterator
3137basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
3138{
Nikolas Klausereed25832021-12-15 01:32:30 +01003139 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
3140 "string::erase(iterator, iterator) called with an iterator not"
3141 " referring to this string");
3142
3143 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
3144 iterator __b = begin();
3145 size_type __r = static_cast<size_type>(__first - __b);
3146 erase(__r, static_cast<size_type>(__last - __first));
3147 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003148}
3149
3150template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003151inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003152void
3153basic_string<_CharT, _Traits, _Allocator>::pop_back()
3154{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003155 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003156 __erase_to_end(size() - 1);
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
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003162basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003163{
3164 __invalidate_all_iterators();
3165 if (__is_long())
3166 {
3167 traits_type::assign(*__get_long_pointer(), value_type());
3168 __set_long_size(0);
3169 }
3170 else
3171 {
3172 traits_type::assign(*__get_short_pointer(), value_type());
3173 __set_short_size(0);
3174 }
3175}
3176
3177template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003178inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003179void
3180basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
3181{
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003182 __null_terminate_at(_VSTD::__to_address(__get_pointer()), __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003183}
3184
3185template <class _CharT, class _Traits, class _Allocator>
3186void
3187basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
3188{
3189 size_type __sz = size();
3190 if (__n > __sz)
3191 append(__n - __sz, __c);
3192 else
3193 __erase_to_end(__n);
3194}
3195
3196template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00003197inline void
3198basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n)
3199{
3200 size_type __sz = size();
3201 if (__n > __sz) {
3202 __append_default_init(__n - __sz);
3203 } else
3204 __erase_to_end(__n);
3205}
3206
3207template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003208inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003209typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003210basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003211{
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003212 size_type __m = __alloc_traits::max_size(__alloc());
Eric Fiseliere9cc5922017-10-17 13:16:01 +00003213#ifdef _LIBCPP_BIG_ENDIAN
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003214 return (__m <= ~__long_mask ? __m : __m/2) - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003215#else
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003216 return __m - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003217#endif
3218}
3219
3220template <class _CharT, class _Traits, class _Allocator>
3221void
Marek Kurdejc9848142020-11-26 10:07:16 +01003222basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __requested_capacity)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003223{
Marek Kurdejc9848142020-11-26 10:07:16 +01003224 if (__requested_capacity > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003225 __throw_length_error();
Marek Kurdejc9848142020-11-26 10:07:16 +01003226
Louis Dionne05b6d3e2022-01-14 12:30:22 -05003227 // Make sure reserve(n) never shrinks. This is technically only required in C++20
3228 // and later (since P0966R1), however we provide consistent behavior in all Standard
3229 // modes because this function is instantiated in the shared library.
3230 if (__requested_capacity <= capacity())
3231 return;
Marek Kurdejc9848142020-11-26 10:07:16 +01003232
3233 size_type __target_capacity = _VSTD::max(__requested_capacity, size());
3234 __target_capacity = __recommend(__target_capacity);
3235 if (__target_capacity == capacity()) return;
3236
3237 __shrink_or_extend(__target_capacity);
3238}
3239
3240template <class _CharT, class _Traits, class _Allocator>
Louis Dionnee38b8642021-12-13 14:15:21 -05003241inline
Marek Kurdejc9848142020-11-26 10:07:16 +01003242void
3243basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT
3244{
3245 size_type __target_capacity = __recommend(size());
3246 if (__target_capacity == capacity()) return;
3247
3248 __shrink_or_extend(__target_capacity);
3249}
3250
3251template <class _CharT, class _Traits, class _Allocator>
Louis Dionnee38b8642021-12-13 14:15:21 -05003252inline
Marek Kurdejc9848142020-11-26 10:07:16 +01003253void
3254basic_string<_CharT, _Traits, _Allocator>::__shrink_or_extend(size_type __target_capacity)
3255{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003256 size_type __cap = capacity();
3257 size_type __sz = size();
Marek Kurdejc9848142020-11-26 10:07:16 +01003258
3259 pointer __new_data, __p;
3260 bool __was_long, __now_long;
3261 if (__target_capacity == __min_cap - 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003262 {
Marek Kurdejc9848142020-11-26 10:07:16 +01003263 __was_long = true;
3264 __now_long = false;
3265 __new_data = __get_short_pointer();
3266 __p = __get_long_pointer();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003267 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003268 else
3269 {
3270 if (__target_capacity > __cap)
3271 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3272 else
3273 {
3274 #ifndef _LIBCPP_NO_EXCEPTIONS
3275 try
3276 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003277 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003278 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3279 #ifndef _LIBCPP_NO_EXCEPTIONS
3280 }
3281 catch (...)
3282 {
3283 return;
3284 }
3285 #else // _LIBCPP_NO_EXCEPTIONS
3286 if (__new_data == nullptr)
3287 return;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003288 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003289 }
3290 __now_long = true;
3291 __was_long = __is_long();
3292 __p = __get_pointer();
3293 }
3294 traits_type::copy(_VSTD::__to_address(__new_data),
3295 _VSTD::__to_address(__p), size()+1);
3296 if (__was_long)
3297 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
3298 if (__now_long)
3299 {
3300 __set_long_cap(__target_capacity+1);
3301 __set_long_size(__sz);
3302 __set_long_pointer(__new_data);
3303 }
3304 else
3305 __set_short_size(__sz);
3306 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003307}
3308
3309template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003310inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003311typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003312basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003313{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003314 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003315 return *(data() + __pos);
3316}
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>::reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003321basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _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 *(__get_pointer() + __pos);
3325}
3326
3327template <class _CharT, class _Traits, class _Allocator>
3328typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3329basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3330{
3331 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003332 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003333 return (*this)[__n];
3334}
3335
3336template <class _CharT, class _Traits, class _Allocator>
3337typename basic_string<_CharT, _Traits, _Allocator>::reference
3338basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
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>
Eric Fiselierbea70602018-11-26 22:51:35 +00003346inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003347typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003348basic_string<_CharT, _Traits, _Allocator>::front() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003349{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003350 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003351 return *__get_pointer();
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>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003357basic_string<_CharT, _Traits, _Allocator>::front() const _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 *data();
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>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003366basic_string<_CharT, _Traits, _Allocator>::back() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003367{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003368 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003369 return *(__get_pointer() + size() - 1);
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>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003375basic_string<_CharT, _Traits, _Allocator>::back() const _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 *(data() + size() - 1);
3379}
3380
3381template <class _CharT, class _Traits, class _Allocator>
3382typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003383basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003384{
3385 size_type __sz = size();
3386 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003387 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003388 size_type __rlen = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003389 traits_type::copy(__s, data() + __pos, __rlen);
3390 return __rlen;
3391}
3392
3393template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003394inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003395basic_string<_CharT, _Traits, _Allocator>
3396basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3397{
3398 return basic_string(*this, __pos, __n, __alloc());
3399}
3400
3401template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003402inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003403void
3404basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003405#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00003406 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00003407#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00003408 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003409 __is_nothrow_swappable<allocator_type>::value)
3410#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003411{
Louis Dionneba400782020-10-02 15:02:52 -04003412#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01003413 if (!__libcpp_is_constant_evaluated()) {
3414 if (!__is_long())
3415 __get_db()->__invalidate_all(this);
3416 if (!__str.__is_long())
3417 __get_db()->__invalidate_all(&__str);
3418 __get_db()->swap(this, &__str);
3419 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00003420#endif
Eric Fiselier9bf691f2016-12-28 05:53:01 +00003421 _LIBCPP_ASSERT(
3422 __alloc_traits::propagate_on_container_swap::value ||
3423 __alloc_traits::is_always_equal::value ||
3424 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003425 _VSTD::swap(__r_.first(), __str.__r_.first());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05003426 _VSTD::__swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003427}
3428
3429// find
3430
3431template <class _Traits>
3432struct _LIBCPP_HIDDEN __traits_eq
3433{
3434 typedef typename _Traits::char_type char_type;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003435 _LIBCPP_INLINE_VISIBILITY
3436 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3437 {return _Traits::eq(__x, __y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003438};
3439
3440template<class _CharT, class _Traits, class _Allocator>
3441typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003442basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003443 size_type __pos,
3444 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003445{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003446 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003447 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003448 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003449}
3450
3451template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003452inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003453typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003454basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3455 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003456{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003457 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003458 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003459}
3460
3461template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003462template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003463__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003464<
3465 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3466 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003467>
Marshall Clowe46031a2018-07-02 18:41:15 +00003468basic_string<_CharT, _Traits, _Allocator>::find(const _Tp &__t,
zoecarver1997e0a2021-02-05 11:54:47 -08003469 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003470{
Marshall Clowe46031a2018-07-02 18:41:15 +00003471 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003472 return __str_find<value_type, size_type, traits_type, npos>
3473 (data(), size(), __sv.data(), __pos, __sv.size());
3474}
3475
3476template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003477inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003478typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003479basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003480 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003481{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003482 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003483 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003484 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003485}
3486
3487template<class _CharT, class _Traits, class _Allocator>
3488typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003489basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3490 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003491{
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(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003494}
3495
3496// rfind
3497
3498template<class _CharT, class _Traits, class _Allocator>
3499typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003500basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003501 size_type __pos,
3502 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003503{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003504 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003505 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003506 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003507}
3508
3509template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003510inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003511typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003512basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3513 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003514{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003515 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003516 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003517}
3518
3519template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003520template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003521__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003522<
3523 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3524 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003525>
Marshall Clowe46031a2018-07-02 18:41:15 +00003526basic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003527 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003528{
Marshall Clowe46031a2018-07-02 18:41:15 +00003529 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003530 return __str_rfind<value_type, size_type, traits_type, npos>
3531 (data(), size(), __sv.data(), __pos, __sv.size());
3532}
3533
3534template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003535inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003536typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003537basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003538 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003539{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003540 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003541 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003542 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003543}
3544
3545template<class _CharT, class _Traits, class _Allocator>
3546typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003547basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3548 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003549{
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(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003552}
3553
3554// find_first_of
3555
3556template<class _CharT, class _Traits, class _Allocator>
3557typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003558basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003559 size_type __pos,
3560 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003561{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003562 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003563 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003564 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003565}
3566
3567template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003568inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003569typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003570basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3571 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003572{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003573 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003574 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003575}
3576
3577template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003578template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003579__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003580<
3581 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3582 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003583>
Marshall Clowe46031a2018-07-02 18:41:15 +00003584basic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003585 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003586{
Marshall Clowe46031a2018-07-02 18:41:15 +00003587 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003588 return __str_find_first_of<value_type, size_type, traits_type, npos>
3589 (data(), size(), __sv.data(), __pos, __sv.size());
3590}
3591
3592template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003593inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003594typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003595basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003596 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003597{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003598 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003599 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003600 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003601}
3602
3603template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003604inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003605typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003606basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3607 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003608{
3609 return find(__c, __pos);
3610}
3611
3612// find_last_of
3613
3614template<class _CharT, class _Traits, class _Allocator>
3615typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003616basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003617 size_type __pos,
3618 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003619{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003620 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003621 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003622 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003623}
3624
3625template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003626inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003627typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003628basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3629 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003630{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003631 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003632 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003633}
3634
3635template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003636template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003637__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003638<
3639 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3640 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003641>
Marshall Clowe46031a2018-07-02 18:41:15 +00003642basic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003643 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003644{
Marshall Clowe46031a2018-07-02 18:41:15 +00003645 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003646 return __str_find_last_of<value_type, size_type, traits_type, npos>
3647 (data(), size(), __sv.data(), __pos, __sv.size());
3648}
3649
3650template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003651inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003652typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003653basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003654 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003655{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003656 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003657 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003658 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003659}
3660
3661template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003662inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003663typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003664basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3665 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003666{
3667 return rfind(__c, __pos);
3668}
3669
3670// find_first_not_of
3671
3672template<class _CharT, class _Traits, class _Allocator>
3673typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003674basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003675 size_type __pos,
3676 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003677{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003678 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003679 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003680 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003681}
3682
3683template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003684inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003685typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003686basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3687 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003688{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003689 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003690 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003691}
3692
3693template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003694template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003695__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003696<
3697 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3698 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003699>
Marshall Clowe46031a2018-07-02 18:41:15 +00003700basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003701 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003702{
Marshall Clowe46031a2018-07-02 18:41:15 +00003703 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003704 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3705 (data(), size(), __sv.data(), __pos, __sv.size());
3706}
3707
3708template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003709inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003710typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003711basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003712 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003713{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003714 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003715 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003716 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003717}
3718
3719template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003720inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003721typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003722basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3723 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003724{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003725 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003726 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003727}
3728
3729// find_last_not_of
3730
3731template<class _CharT, class _Traits, class _Allocator>
3732typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003733basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003734 size_type __pos,
3735 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003736{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003737 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003738 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003739 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003740}
3741
3742template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003743inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003744typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003745basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3746 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003747{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003748 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003749 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003750}
3751
3752template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003753template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003754__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003755<
3756 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3757 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003758>
Marshall Clowe46031a2018-07-02 18:41:15 +00003759basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003760 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003761{
Marshall Clowe46031a2018-07-02 18:41:15 +00003762 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003763 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3764 (data(), size(), __sv.data(), __pos, __sv.size());
3765}
3766
3767template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003768inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003769typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003770basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003771 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003772{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003773 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003774 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003775 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003776}
3777
3778template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003779inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003780typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003781basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3782 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003783{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003784 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003785 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003786}
3787
3788// compare
3789
3790template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003791template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003792__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003793<
3794 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3795 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003796>
zoecarver1997e0a2021-02-05 11:54:47 -08003797basic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003798{
Marshall Clowe46031a2018-07-02 18:41:15 +00003799 __self_view __sv = __t;
Howard Hinnantb0485532011-07-24 21:45:06 +00003800 size_t __lhs_sz = size();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003801 size_t __rhs_sz = __sv.size();
3802 int __result = traits_type::compare(data(), __sv.data(),
Howard Hinnantb0485532011-07-24 21:45:06 +00003803 _VSTD::min(__lhs_sz, __rhs_sz));
3804 if (__result != 0)
3805 return __result;
3806 if (__lhs_sz < __rhs_sz)
3807 return -1;
3808 if (__lhs_sz > __rhs_sz)
3809 return 1;
3810 return 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003811}
3812
3813template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003814inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003815int
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003816basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003817{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003818 return compare(__self_view(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003819}
3820
3821template <class _CharT, class _Traits, class _Allocator>
3822int
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003823basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3824 size_type __n1,
Howard Hinnantd17880b2013-06-28 16:59:19 +00003825 const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003826 size_type __n2) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003827{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003828 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003829 size_type __sz = size();
3830 if (__pos1 > __sz || __n2 == npos)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003831 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003832 size_type __rlen = _VSTD::min(__n1, __sz - __pos1);
3833 int __r = traits_type::compare(data() + __pos1, __s, _VSTD::min(__rlen, __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003834 if (__r == 0)
3835 {
3836 if (__rlen < __n2)
3837 __r = -1;
3838 else if (__rlen > __n2)
3839 __r = 1;
3840 }
3841 return __r;
3842}
3843
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003844template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003845template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003846__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003847<
3848 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3849 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003850>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003851basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3852 size_type __n1,
Marshall Clowe46031a2018-07-02 18:41:15 +00003853 const _Tp& __t) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003854{
Marshall Clowe46031a2018-07-02 18:41:15 +00003855 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003856 return compare(__pos1, __n1, __sv.data(), __sv.size());
3857}
3858
3859template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003860inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003861int
3862basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3863 size_type __n1,
3864 const basic_string& __str) const
3865{
3866 return compare(__pos1, __n1, __str.data(), __str.size());
3867}
3868
3869template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003870template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003871__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003872<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003873 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
3874 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00003875 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003876>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003877basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3878 size_type __n1,
Marshall Clow82513342016-09-24 22:45:42 +00003879 const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003880 size_type __pos2,
3881 size_type __n2) const
3882{
Marshall Clow82513342016-09-24 22:45:42 +00003883 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003884 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3885}
3886
3887template <class _CharT, class _Traits, class _Allocator>
3888int
3889basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3890 size_type __n1,
3891 const basic_string& __str,
3892 size_type __pos2,
3893 size_type __n2) const
3894{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003895 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003896}
3897
3898template <class _CharT, class _Traits, class _Allocator>
3899int
3900basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
3901{
3902 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3903 return compare(0, npos, __s, traits_type::length(__s));
3904}
3905
3906template <class _CharT, class _Traits, class _Allocator>
3907int
3908basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3909 size_type __n1,
3910 const value_type* __s) const
3911{
3912 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3913 return compare(__pos1, __n1, __s, traits_type::length(__s));
3914}
3915
Howard Hinnantc51e1022010-05-11 19:42:16 +00003916// __invariants
3917
3918template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003919inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003920bool
3921basic_string<_CharT, _Traits, _Allocator>::__invariants() const
3922{
3923 if (size() > capacity())
3924 return false;
3925 if (capacity() < __min_cap - 1)
3926 return false;
Bruce Mitchener170d8972020-11-24 12:53:53 -05003927 if (data() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003928 return false;
Louis Dionne663415f2020-10-05 16:16:13 -04003929 if (data()[size()] != value_type())
Howard Hinnantc51e1022010-05-11 19:42:16 +00003930 return false;
3931 return true;
3932}
3933
Vedant Kumar55e007e2018-03-08 21:15:26 +00003934// __clear_and_shrink
3935
3936template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003937inline
Louis Dionne173f29e2019-05-29 16:01:36 +00003938void
Marshall Clowe60a7182018-05-29 17:04:37 +00003939basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
Vedant Kumar55e007e2018-03-08 21:15:26 +00003940{
3941 clear();
3942 if(__is_long())
3943 {
3944 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
3945 __set_long_cap(0);
3946 __set_short_size(0);
Louis Dionne663415f2020-10-05 16:16:13 -04003947 traits_type::assign(*__get_short_pointer(), value_type());
Vedant Kumar55e007e2018-03-08 21:15:26 +00003948 }
Louis Dionne173f29e2019-05-29 16:01:36 +00003949}
Vedant Kumar55e007e2018-03-08 21:15:26 +00003950
Howard Hinnantc51e1022010-05-11 19:42:16 +00003951// operator==
3952
3953template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003954inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003955bool
3956operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003957 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003958{
Howard Hinnantaaeb1132013-04-22 23:55:13 +00003959 size_t __lhs_sz = __lhs.size();
3960 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
3961 __rhs.data(),
3962 __lhs_sz) == 0;
3963}
3964
3965template<class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003966inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaaeb1132013-04-22 23:55:13 +00003967bool
3968operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
3969 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
3970{
3971 size_t __lhs_sz = __lhs.size();
3972 if (__lhs_sz != __rhs.size())
3973 return false;
3974 const char* __lp = __lhs.data();
3975 const char* __rp = __rhs.data();
3976 if (__lhs.__is_long())
3977 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
3978 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
3979 if (*__lp != *__rp)
3980 return false;
3981 return true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003982}
3983
3984template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003985inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003986bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003987operator==(const _CharT* __lhs,
3988 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003989{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00003990 typedef basic_string<_CharT, _Traits, _Allocator> _String;
3991 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
3992 size_t __lhs_len = _Traits::length(__lhs);
3993 if (__lhs_len != __rhs.size()) return false;
3994 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003995}
3996
Howard Hinnantc51e1022010-05-11 19:42:16 +00003997template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003998inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003999bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004000operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
4001 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004002{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004003 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4004 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
4005 size_t __rhs_len = _Traits::length(__rhs);
4006 if (__rhs_len != __lhs.size()) return false;
4007 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004008}
4009
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004010template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004011inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004012bool
4013operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004014 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004015{
4016 return !(__lhs == __rhs);
4017}
4018
4019template<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
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004022operator!=(const _CharT* __lhs,
4023 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 basic_string<_CharT, _Traits, _Allocator>& __lhs,
4032 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004033{
4034 return !(__lhs == __rhs);
4035}
4036
4037// operator<
4038
4039template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004040inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004041bool
4042operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004043 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004044{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004045 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004046}
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
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004051operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4052 const _CharT* __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 _CharT* __lhs,
4061 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004062{
4063 return __rhs.compare(__lhs) > 0;
4064}
4065
Howard Hinnantc51e1022010-05-11 19:42:16 +00004066// operator>
4067
4068template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004069inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004070bool
4071operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004072 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004073{
4074 return __rhs < __lhs;
4075}
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
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004080operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4081 const _CharT* __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 _CharT* __lhs,
4090 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004091{
4092 return __rhs < __lhs;
4093}
4094
4095// operator<=
4096
4097template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004098inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004099bool
4100operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004101 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004102{
4103 return !(__rhs < __lhs);
4104}
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
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004109operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4110 const _CharT* __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 _CharT* __lhs,
4119 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004120{
4121 return !(__rhs < __lhs);
4122}
4123
4124// operator>=
4125
4126template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004127inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004128bool
4129operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004130 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004131{
4132 return !(__lhs < __rhs);
4133}
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
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004138operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4139 const _CharT* __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 _CharT* __lhs,
4148 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004149{
4150 return !(__lhs < __rhs);
4151}
4152
4153// operator +
4154
4155template<class _CharT, class _Traits, class _Allocator>
4156basic_string<_CharT, _Traits, _Allocator>
4157operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4158 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4159{
4160 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4161 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4162 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4163 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4164 __r.append(__rhs.data(), __rhs_sz);
4165 return __r;
4166}
4167
4168template<class _CharT, class _Traits, class _Allocator>
4169basic_string<_CharT, _Traits, _Allocator>
4170operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4171{
4172 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4173 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = _Traits::length(__lhs);
4174 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4175 __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz);
4176 __r.append(__rhs.data(), __rhs_sz);
4177 return __r;
4178}
4179
4180template<class _CharT, class _Traits, class _Allocator>
4181basic_string<_CharT, _Traits, _Allocator>
4182operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4183{
4184 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4185 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4186 __r.__init(&__lhs, 1, 1 + __rhs_sz);
4187 __r.append(__rhs.data(), __rhs_sz);
4188 return __r;
4189}
4190
4191template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004192inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004193basic_string<_CharT, _Traits, _Allocator>
4194operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
4195{
4196 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4197 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4198 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = _Traits::length(__rhs);
4199 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4200 __r.append(__rhs, __rhs_sz);
4201 return __r;
4202}
4203
4204template<class _CharT, class _Traits, class _Allocator>
4205basic_string<_CharT, _Traits, _Allocator>
4206operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
4207{
4208 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4209 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4210 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1);
4211 __r.push_back(__rhs);
4212 return __r;
4213}
4214
Eric Fiselierfc92be82017-04-19 00:28:44 +00004215#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004216
4217template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004218inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004219basic_string<_CharT, _Traits, _Allocator>
4220operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4221{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004222 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004223}
4224
4225template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004226inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004227basic_string<_CharT, _Traits, _Allocator>
4228operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4229{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004230 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004231}
4232
4233template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004234inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004235basic_string<_CharT, _Traits, _Allocator>
4236operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4237{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004238 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004239}
4240
4241template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004242inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004243basic_string<_CharT, _Traits, _Allocator>
4244operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4245{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004246 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004247}
4248
4249template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004250inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004251basic_string<_CharT, _Traits, _Allocator>
4252operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4253{
4254 __rhs.insert(__rhs.begin(), __lhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004255 return _VSTD::move(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004256}
4257
4258template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004259inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004260basic_string<_CharT, _Traits, _Allocator>
4261operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
4262{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004263 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004264}
4265
4266template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004267inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004268basic_string<_CharT, _Traits, _Allocator>
4269operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
4270{
4271 __lhs.push_back(__rhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004272 return _VSTD::move(__lhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004273}
4274
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004275#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004276
4277// swap
4278
4279template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004280inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004281void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004282swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +00004283 basic_string<_CharT, _Traits, _Allocator>& __rhs)
4284 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004285{
4286 __lhs.swap(__rhs);
4287}
4288
Bruce Mitchener170d8972020-11-24 12:53:53 -05004289_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = nullptr, int __base = 10);
4290_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = nullptr, int __base = 10);
4291_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = nullptr, int __base = 10);
4292_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = nullptr, int __base = 10);
4293_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004294
Bruce Mitchener170d8972020-11-24 12:53:53 -05004295_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = nullptr);
4296_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = nullptr);
4297_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004298
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004299_LIBCPP_FUNC_VIS string to_string(int __val);
4300_LIBCPP_FUNC_VIS string to_string(unsigned __val);
4301_LIBCPP_FUNC_VIS string to_string(long __val);
4302_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
4303_LIBCPP_FUNC_VIS string to_string(long long __val);
4304_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
4305_LIBCPP_FUNC_VIS string to_string(float __val);
4306_LIBCPP_FUNC_VIS string to_string(double __val);
4307_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004308
Louis Dionne89258142021-08-23 15:32:36 -04004309#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Bruce Mitchener170d8972020-11-24 12:53:53 -05004310_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4311_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4312_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4313_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4314_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004315
Bruce Mitchener170d8972020-11-24 12:53:53 -05004316_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = nullptr);
4317_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = nullptr);
4318_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004319
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004320_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
4321_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
4322_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
4323_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
4324_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
4325_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
4326_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
4327_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
4328_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Louis Dionne89258142021-08-23 15:32:36 -04004329#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004330
Howard Hinnantc51e1022010-05-11 19:42:16 +00004331template<class _CharT, class _Traits, class _Allocator>
Martin Storsjö88890c22021-02-22 01:13:13 +02004332_LIBCPP_TEMPLATE_DATA_VIS
Eric Fiselierc9fdaf12020-01-15 17:02:17 -05004333const typename basic_string<_CharT, _Traits, _Allocator>::size_type
4334 basic_string<_CharT, _Traits, _Allocator>::npos;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004335
Marshall Clow851b9ec2019-05-20 21:56:51 +00004336template <class _CharT, class _Allocator>
4337struct _LIBCPP_TEMPLATE_VIS
4338 hash<basic_string<_CharT, char_traits<_CharT>, _Allocator> >
4339 : public unary_function<
4340 basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004341{
4342 size_t
Marshall Clow851b9ec2019-05-20 21:56:51 +00004343 operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT
4344 { return __do_string_hash(__val.data(), __val.data() + __val.size()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004345};
4346
Howard Hinnantc51e1022010-05-11 19:42:16 +00004347
Howard Hinnantdc095972011-07-18 15:51:59 +00004348template<class _CharT, class _Traits, class _Allocator>
4349basic_ostream<_CharT, _Traits>&
4350operator<<(basic_ostream<_CharT, _Traits>& __os,
4351 const basic_string<_CharT, _Traits, _Allocator>& __str);
4352
4353template<class _CharT, class _Traits, class _Allocator>
4354basic_istream<_CharT, _Traits>&
4355operator>>(basic_istream<_CharT, _Traits>& __is,
4356 basic_string<_CharT, _Traits, _Allocator>& __str);
4357
4358template<class _CharT, class _Traits, class _Allocator>
4359basic_istream<_CharT, _Traits>&
4360getline(basic_istream<_CharT, _Traits>& __is,
4361 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4362
4363template<class _CharT, class _Traits, class _Allocator>
4364inline _LIBCPP_INLINE_VISIBILITY
4365basic_istream<_CharT, _Traits>&
4366getline(basic_istream<_CharT, _Traits>& __is,
4367 basic_string<_CharT, _Traits, _Allocator>& __str);
4368
Howard Hinnantdc095972011-07-18 15:51:59 +00004369template<class _CharT, class _Traits, class _Allocator>
4370inline _LIBCPP_INLINE_VISIBILITY
4371basic_istream<_CharT, _Traits>&
4372getline(basic_istream<_CharT, _Traits>&& __is,
4373 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4374
4375template<class _CharT, class _Traits, class _Allocator>
4376inline _LIBCPP_INLINE_VISIBILITY
4377basic_istream<_CharT, _Traits>&
4378getline(basic_istream<_CharT, _Traits>&& __is,
4379 basic_string<_CharT, _Traits, _Allocator>& __str);
4380
Marshall Clow29b53f22018-12-14 18:49:35 +00004381#if _LIBCPP_STD_VER > 17
Marek Kurdeja98b1412020-05-02 13:58:03 +02004382template <class _CharT, class _Traits, class _Allocator, class _Up>
Marshall Clow29b53f22018-12-14 18:49:35 +00004383inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004384 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4385 erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
4386 auto __old_size = __str.size();
4387 __str.erase(_VSTD::remove(__str.begin(), __str.end(), __v), __str.end());
4388 return __old_size - __str.size();
4389}
Marshall Clow29b53f22018-12-14 18:49:35 +00004390
Marek Kurdeja98b1412020-05-02 13:58:03 +02004391template <class _CharT, class _Traits, class _Allocator, class _Predicate>
Marshall Clow29b53f22018-12-14 18:49:35 +00004392inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004393 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4394 erase_if(basic_string<_CharT, _Traits, _Allocator>& __str,
4395 _Predicate __pred) {
4396 auto __old_size = __str.size();
4397 __str.erase(_VSTD::remove_if(__str.begin(), __str.end(), __pred),
4398 __str.end());
4399 return __old_size - __str.size();
4400}
Marshall Clow29b53f22018-12-14 18:49:35 +00004401#endif
4402
Louis Dionneba400782020-10-02 15:02:52 -04004403#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004404
4405template<class _CharT, class _Traits, class _Allocator>
4406bool
4407basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4408{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004409 return data() <= _VSTD::__to_address(__i->base()) &&
4410 _VSTD::__to_address(__i->base()) < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004411}
4412
4413template<class _CharT, class _Traits, class _Allocator>
4414bool
4415basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4416{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004417 return data() < _VSTD::__to_address(__i->base()) &&
4418 _VSTD::__to_address(__i->base()) <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004419}
4420
4421template<class _CharT, class _Traits, class _Allocator>
4422bool
4423basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4424{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004425 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004426 return data() <= __p && __p <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004427}
4428
4429template<class _CharT, class _Traits, class _Allocator>
4430bool
4431basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4432{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004433 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004434 return data() <= __p && __p < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004435}
4436
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004437#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004438
Louis Dionne173f29e2019-05-29 16:01:36 +00004439#if _LIBCPP_STD_VER > 11
Marshall Clowcba751f2013-07-23 17:05:24 +00004440// Literal suffixes for basic_string [basic.string.literals]
Marshall Clowac868372013-10-05 21:18:32 +00004441inline namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004442{
4443 inline namespace string_literals
4444 {
Howard Hinnant5c167562013-08-07 19:39:48 +00004445 inline _LIBCPP_INLINE_VISIBILITY
4446 basic_string<char> operator "" s( const char *__str, size_t __len )
4447 {
4448 return basic_string<char> (__str, __len);
4449 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004450
Louis Dionne89258142021-08-23 15:32:36 -04004451#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnant5c167562013-08-07 19:39:48 +00004452 inline _LIBCPP_INLINE_VISIBILITY
4453 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4454 {
4455 return basic_string<wchar_t> (__str, __len);
4456 }
Louis Dionne89258142021-08-23 15:32:36 -04004457#endif
Marshall Clowcba751f2013-07-23 17:05:24 +00004458
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -04004459#ifndef _LIBCPP_HAS_NO_CHAR8_T
Marshall Clow8732fed2018-12-11 04:35:44 +00004460 inline _LIBCPP_INLINE_VISIBILITY
4461 basic_string<char8_t> operator "" s(const char8_t *__str, size_t __len) _NOEXCEPT
4462 {
4463 return basic_string<char8_t> (__str, __len);
4464 }
4465#endif
4466
Howard Hinnant5c167562013-08-07 19:39:48 +00004467 inline _LIBCPP_INLINE_VISIBILITY
4468 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4469 {
4470 return basic_string<char16_t> (__str, __len);
4471 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004472
Howard Hinnant5c167562013-08-07 19:39:48 +00004473 inline _LIBCPP_INLINE_VISIBILITY
4474 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4475 {
4476 return basic_string<char32_t> (__str, __len);
4477 }
Nikolas Klauserd26407a2021-12-02 14:12:51 +01004478 } // namespace string_literals
4479} // namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004480#endif
4481
Howard Hinnantc51e1022010-05-11 19:42:16 +00004482_LIBCPP_END_NAMESPACE_STD
4483
Eric Fiselierf4433a32017-05-31 22:07:49 +00004484_LIBCPP_POP_MACROS
4485
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004486#endif // _LIBCPP_STRING