blob: 4f3815f53e7b1e5bcc6f95dbb451d99e2847cbf8 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
Louis Dionne9bd93882021-11-17 16:25:01 -05002//===----------------------------------------------------------------------===//
Howard Hinnantc51e1022010-05-11 19:42:16 +00003//
Chandler Carruth7642bb12019-01-19 08:50:56 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_STRING
11#define _LIBCPP_STRING
12
13/*
14 string synopsis
15
16namespace std
17{
18
19template <class stateT>
20class fpos
21{
22private:
23 stateT st;
24public:
25 fpos(streamoff = streamoff());
26
27 operator streamoff() const;
28
29 stateT state() const;
30 void state(stateT);
31
32 fpos& operator+=(streamoff);
33 fpos operator+ (streamoff) const;
34 fpos& operator-=(streamoff);
35 fpos operator- (streamoff) const;
36};
37
38template <class stateT> streamoff operator-(const fpos<stateT>& x, const fpos<stateT>& y);
39
40template <class stateT> bool operator==(const fpos<stateT>& x, const fpos<stateT>& y);
41template <class stateT> bool operator!=(const fpos<stateT>& x, const fpos<stateT>& y);
42
43template <class charT>
44struct char_traits
45{
46 typedef charT char_type;
47 typedef ... int_type;
48 typedef streamoff off_type;
49 typedef streampos pos_type;
50 typedef mbstate_t state_type;
51
Howard Hinnantaeb17d82011-05-29 19:57:12 +000052 static void assign(char_type& c1, const char_type& c2) noexcept;
Howard Hinnant270c00e2012-07-20 19:09:12 +000053 static constexpr bool eq(char_type c1, char_type c2) noexcept;
54 static constexpr bool lt(char_type c1, char_type c2) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000055
56 static int compare(const char_type* s1, const char_type* s2, size_t n);
57 static size_t length(const char_type* s);
58 static const char_type* find(const char_type* s, size_t n, const char_type& a);
59 static char_type* move(char_type* s1, const char_type* s2, size_t n);
60 static char_type* copy(char_type* s1, const char_type* s2, size_t n);
61 static char_type* assign(char_type* s, size_t n, char_type a);
62
Howard Hinnant270c00e2012-07-20 19:09:12 +000063 static constexpr int_type not_eof(int_type c) noexcept;
64 static constexpr char_type to_char_type(int_type c) noexcept;
65 static constexpr int_type to_int_type(char_type c) noexcept;
66 static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept;
67 static constexpr int_type eof() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000068};
69
70template <> struct char_traits<char>;
71template <> struct char_traits<wchar_t>;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +010072template <> struct char_traits<char8_t>; // C++20
73template <> struct char_traits<char16_t>;
74template <> struct char_traits<char32_t>;
Howard Hinnantc51e1022010-05-11 19:42:16 +000075
Howard Hinnant3b6579a2010-08-22 00:02:43 +000076template<class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
Howard Hinnantc51e1022010-05-11 19:42:16 +000077class basic_string
78{
Howard Hinnant3b6579a2010-08-22 00:02:43 +000079public:
80// types:
Howard Hinnantc51e1022010-05-11 19:42:16 +000081 typedef traits traits_type;
82 typedef typename traits_type::char_type value_type;
83 typedef Allocator allocator_type;
84 typedef typename allocator_type::size_type size_type;
85 typedef typename allocator_type::difference_type difference_type;
86 typedef typename allocator_type::reference reference;
87 typedef typename allocator_type::const_reference const_reference;
88 typedef typename allocator_type::pointer pointer;
89 typedef typename allocator_type::const_pointer const_pointer;
90 typedef implementation-defined iterator;
91 typedef implementation-defined const_iterator;
92 typedef std::reverse_iterator<iterator> reverse_iterator;
93 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
94
95 static const size_type npos = -1;
96
Howard Hinnant3e276872011-06-03 18:40:47 +000097 basic_string()
98 noexcept(is_nothrow_default_constructible<allocator_type>::value);
99 explicit basic_string(const allocator_type& a);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000100 basic_string(const basic_string& str);
Howard Hinnant3e276872011-06-03 18:40:47 +0000101 basic_string(basic_string&& str)
102 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clow83445802016-04-07 18:13:41 +0000103 basic_string(const basic_string& str, size_type pos,
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000104 const allocator_type& a = allocator_type());
Marshall Clow83445802016-04-07 18:13:41 +0000105 basic_string(const basic_string& str, size_type pos, size_type n,
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000106 const Allocator& a = Allocator());
Marshall Clow78dbe462016-11-14 18:22:19 +0000107 template<class T>
108 basic_string(const T& t, size_type pos, size_type n, const Allocator& a = Allocator()); // C++17
Marshall Clowe46031a2018-07-02 18:41:15 +0000109 template <class T>
110 explicit basic_string(const T& t, const Allocator& a = Allocator()); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000111 basic_string(const value_type* s, const allocator_type& a = allocator_type());
112 basic_string(const value_type* s, size_type n, const allocator_type& a = allocator_type());
Marek Kurdej90d79712021-07-27 16:16:21 +0200113 basic_string(nullptr_t) = delete; // C++2b
Howard Hinnantc51e1022010-05-11 19:42:16 +0000114 basic_string(size_type n, value_type c, const allocator_type& a = allocator_type());
115 template<class InputIterator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000116 basic_string(InputIterator begin, InputIterator end,
117 const allocator_type& a = allocator_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000118 basic_string(initializer_list<value_type>, const Allocator& = Allocator());
119 basic_string(const basic_string&, const Allocator&);
120 basic_string(basic_string&&, const Allocator&);
121
122 ~basic_string();
123
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000124 operator basic_string_view<charT, traits>() const noexcept;
125
Howard Hinnantc51e1022010-05-11 19:42:16 +0000126 basic_string& operator=(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000127 template <class T>
128 basic_string& operator=(const T& t); // C++17
Howard Hinnant3e276872011-06-03 18:40:47 +0000129 basic_string& operator=(basic_string&& str)
130 noexcept(
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000131 allocator_type::propagate_on_container_move_assignment::value ||
132 allocator_type::is_always_equal::value ); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000133 basic_string& operator=(const value_type* s);
Marek Kurdej90d79712021-07-27 16:16:21 +0200134 basic_string& operator=(nullptr_t) = delete; // C++2b
Howard Hinnantc51e1022010-05-11 19:42:16 +0000135 basic_string& operator=(value_type c);
136 basic_string& operator=(initializer_list<value_type>);
137
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000138 iterator begin() noexcept;
139 const_iterator begin() const noexcept;
140 iterator end() noexcept;
141 const_iterator end() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000142
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000143 reverse_iterator rbegin() noexcept;
144 const_reverse_iterator rbegin() const noexcept;
145 reverse_iterator rend() noexcept;
146 const_reverse_iterator rend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000147
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000148 const_iterator cbegin() const noexcept;
149 const_iterator cend() const noexcept;
150 const_reverse_iterator crbegin() const noexcept;
151 const_reverse_iterator crend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000152
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000153 size_type size() const noexcept;
154 size_type length() const noexcept;
155 size_type max_size() const noexcept;
156 size_type capacity() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000157
158 void resize(size_type n, value_type c);
159 void resize(size_type n);
160
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100161 template<class Operation>
162 constexpr void resize_and_overwrite(size_type n, Operation op); // since C++23
163
Marek Kurdejc9848142020-11-26 10:07:16 +0100164 void reserve(size_type res_arg);
165 void reserve(); // deprecated in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000166 void shrink_to_fit();
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000167 void clear() noexcept;
168 bool empty() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000169
170 const_reference operator[](size_type pos) const;
171 reference operator[](size_type pos);
172
173 const_reference at(size_type n) const;
174 reference at(size_type n);
175
176 basic_string& operator+=(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000177 template <class T>
178 basic_string& operator+=(const T& t); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000179 basic_string& operator+=(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000180 basic_string& operator+=(value_type c);
181 basic_string& operator+=(initializer_list<value_type>);
182
183 basic_string& append(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000184 template <class T>
185 basic_string& append(const T& t); // C++17
Marshall Clow8db7fb02014-03-04 19:17:19 +0000186 basic_string& append(const basic_string& str, size_type pos, size_type n=npos); //C++14
Marshall Clow82513342016-09-24 22:45:42 +0000187 template <class T>
188 basic_string& append(const T& t, size_type pos, size_type n=npos); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000189 basic_string& append(const value_type* s, size_type n);
190 basic_string& append(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000191 basic_string& append(size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000192 template<class InputIterator>
193 basic_string& append(InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000194 basic_string& append(initializer_list<value_type>);
195
196 void push_back(value_type c);
197 void pop_back();
198 reference front();
199 const_reference front() const;
200 reference back();
201 const_reference back() const;
202
203 basic_string& assign(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000204 template <class T>
205 basic_string& assign(const T& t); // C++17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000206 basic_string& assign(basic_string&& str);
Marshall Clow8db7fb02014-03-04 19:17:19 +0000207 basic_string& assign(const basic_string& str, size_type pos, size_type n=npos); // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000208 template <class T>
209 basic_string& assign(const T& t, size_type pos, size_type n=npos); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000210 basic_string& assign(const value_type* s, size_type n);
211 basic_string& assign(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000212 basic_string& assign(size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000213 template<class InputIterator>
214 basic_string& assign(InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000215 basic_string& assign(initializer_list<value_type>);
216
217 basic_string& insert(size_type pos1, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000218 template <class T>
219 basic_string& insert(size_type pos1, const T& t);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000220 basic_string& insert(size_type pos1, const basic_string& str,
221 size_type pos2, size_type n);
Marshall Clow82513342016-09-24 22:45:42 +0000222 template <class T>
223 basic_string& insert(size_type pos1, const T& t, size_type pos2, size_type n); // C++17
Marshall Clow8db7fb02014-03-04 19:17:19 +0000224 basic_string& insert(size_type pos, const value_type* s, size_type n=npos); //C++14
Howard Hinnantd17880b2013-06-28 16:59:19 +0000225 basic_string& insert(size_type pos, const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000226 basic_string& insert(size_type pos, size_type n, value_type c);
227 iterator insert(const_iterator p, value_type c);
228 iterator insert(const_iterator p, size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000229 template<class InputIterator>
230 iterator insert(const_iterator p, InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000231 iterator insert(const_iterator p, initializer_list<value_type>);
232
233 basic_string& erase(size_type pos = 0, size_type n = npos);
234 iterator erase(const_iterator position);
235 iterator erase(const_iterator first, const_iterator last);
236
237 basic_string& replace(size_type pos1, size_type n1, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000238 template <class T>
239 basic_string& replace(size_type pos1, size_type n1, const T& t); // C++17
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000240 basic_string& replace(size_type pos1, size_type n1, const basic_string& str,
Marshall Clow8db7fb02014-03-04 19:17:19 +0000241 size_type pos2, size_type n2=npos); // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000242 template <class T>
243 basic_string& replace(size_type pos1, size_type n1, const T& t,
244 size_type pos2, size_type n); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000245 basic_string& replace(size_type pos, size_type n1, const value_type* s, size_type n2);
246 basic_string& replace(size_type pos, size_type n1, const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000247 basic_string& replace(size_type pos, size_type n1, size_type n2, value_type c);
Howard Hinnant990d6e82010-11-17 21:11:40 +0000248 basic_string& replace(const_iterator i1, const_iterator i2, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000249 template <class T>
250 basic_string& replace(const_iterator i1, const_iterator i2, const T& t); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000251 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s, size_type n);
252 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s);
Howard Hinnant990d6e82010-11-17 21:11:40 +0000253 basic_string& replace(const_iterator i1, const_iterator i2, size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000254 template<class InputIterator>
Howard Hinnant990d6e82010-11-17 21:11:40 +0000255 basic_string& replace(const_iterator i1, const_iterator i2, InputIterator j1, InputIterator j2);
256 basic_string& replace(const_iterator i1, const_iterator i2, initializer_list<value_type>);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000257
Howard Hinnantd17880b2013-06-28 16:59:19 +0000258 size_type copy(value_type* s, size_type n, size_type pos = 0) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000259 basic_string substr(size_type pos = 0, size_type n = npos) const;
260
Howard Hinnant3e276872011-06-03 18:40:47 +0000261 void swap(basic_string& str)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000262 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
263 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000264
Howard Hinnantd17880b2013-06-28 16:59:19 +0000265 const value_type* c_str() const noexcept;
266 const value_type* data() const noexcept;
Marshall Clowd3c22392016-03-08 15:44:30 +0000267 value_type* data() noexcept; // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000268
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000269 allocator_type get_allocator() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000270
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000271 size_type find(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000272 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800273 size_type find(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000274 size_type find(const value_type* s, size_type pos, size_type n) const noexcept;
275 size_type find(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000276 size_type find(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000277
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000278 size_type rfind(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000279 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800280 size_type rfind(const T& t, size_type pos = npos) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000281 size_type rfind(const value_type* s, size_type pos, size_type n) const noexcept;
282 size_type rfind(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000283 size_type rfind(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000284
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000285 size_type find_first_of(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000286 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800287 size_type find_first_of(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000288 size_type find_first_of(const value_type* s, size_type pos, size_type n) const noexcept;
289 size_type find_first_of(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000290 size_type find_first_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000291
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000292 size_type find_last_of(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000293 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800294 size_type find_last_of(const T& t, size_type pos = npos) const noexcept noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000295 size_type find_last_of(const value_type* s, size_type pos, size_type n) const noexcept;
296 size_type find_last_of(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000297 size_type find_last_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000298
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000299 size_type find_first_not_of(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000300 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800301 size_type find_first_not_of(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000302 size_type find_first_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
303 size_type find_first_not_of(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000304 size_type find_first_not_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000305
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000306 size_type find_last_not_of(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000307 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800308 size_type find_last_not_of(const T& t, size_type pos = npos) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000309 size_type find_last_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
310 size_type find_last_not_of(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000311 size_type find_last_not_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000312
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000313 int compare(const basic_string& str) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000314 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800315 int compare(const T& t) const noexcept; // C++17, noexcept as an extension
Howard Hinnantc51e1022010-05-11 19:42:16 +0000316 int compare(size_type pos1, size_type n1, const basic_string& str) const;
Marshall Clowe46031a2018-07-02 18:41:15 +0000317 template <class T>
318 int compare(size_type pos1, size_type n1, const T& t) const; // C++17
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000319 int compare(size_type pos1, size_type n1, const basic_string& str,
Marshall Clow8db7fb02014-03-04 19:17:19 +0000320 size_type pos2, size_type n2=npos) const; // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000321 template <class T>
322 int compare(size_type pos1, size_type n1, const T& t,
323 size_type pos2, size_type n2=npos) const; // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000324 int compare(const value_type* s) const noexcept;
325 int compare(size_type pos1, size_type n1, const value_type* s) const;
326 int compare(size_type pos1, size_type n1, const value_type* s, size_type n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000327
Marek Kurdej24b4c512021-01-07 12:29:04 +0100328 bool starts_with(basic_string_view<charT, traits> sv) const noexcept; // C++20
329 bool starts_with(charT c) const noexcept; // C++20
330 bool starts_with(const charT* s) const; // C++20
331 bool ends_with(basic_string_view<charT, traits> sv) const noexcept; // C++20
332 bool ends_with(charT c) const noexcept; // C++20
333 bool ends_with(const charT* s) const; // C++20
Marshall Clow18c293b2017-12-04 20:11:38 +0000334
Wim Leflere023c3542021-01-19 14:33:30 -0500335 constexpr bool contains(basic_string_view<charT, traits> sv) const noexcept; // C++2b
336 constexpr bool contains(charT c) const noexcept; // C++2b
337 constexpr bool contains(const charT* s) const; // C++2b
338
Howard Hinnantc51e1022010-05-11 19:42:16 +0000339 bool __invariants() const;
340};
341
Marshall Clowa0563332018-02-08 06:34:03 +0000342template<class InputIterator,
343 class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
344basic_string(InputIterator, InputIterator, Allocator = Allocator())
345 -> basic_string<typename iterator_traits<InputIterator>::value_type,
346 char_traits<typename iterator_traits<InputIterator>::value_type>,
347 Allocator>; // C++17
348
Howard Hinnantc51e1022010-05-11 19:42:16 +0000349template<class charT, class traits, class Allocator>
350basic_string<charT, traits, Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000351operator+(const basic_string<charT, traits, Allocator>& lhs,
352 const basic_string<charT, traits, Allocator>& rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000353
354template<class charT, class traits, class Allocator>
355basic_string<charT, traits, Allocator>
356operator+(const charT* lhs , const basic_string<charT,traits,Allocator>&rhs);
357
358template<class charT, class traits, class Allocator>
359basic_string<charT, traits, Allocator>
360operator+(charT lhs, const basic_string<charT,traits,Allocator>& rhs);
361
362template<class charT, class traits, class Allocator>
363basic_string<charT, traits, Allocator>
364operator+(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs);
365
366template<class charT, class traits, class Allocator>
367basic_string<charT, traits, Allocator>
368operator+(const basic_string<charT, traits, Allocator>& lhs, charT rhs);
369
370template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000371bool operator==(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000372 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000373
374template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000375bool operator==(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000376
377template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000378bool operator==(const basic_string<charT,traits,Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000379
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000380template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000381bool operator!=(const basic_string<charT,traits,Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000382 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000383
384template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000385bool operator!=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000386
387template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000388bool operator!=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000389
390template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000391bool operator< (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000392 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000393
394template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000395bool operator< (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000396
397template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000398bool operator< (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000399
400template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000401bool operator> (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000402 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000403
404template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000405bool operator> (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000406
407template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000408bool operator> (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000409
410template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000411bool operator<=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000412 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000413
414template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000415bool operator<=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000416
417template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000418bool operator<=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000419
420template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000421bool operator>=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000422 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000423
424template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000425bool operator>=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000426
427template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000428bool operator>=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000429
430template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000431void swap(basic_string<charT, traits, Allocator>& lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +0000432 basic_string<charT, traits, Allocator>& rhs)
433 noexcept(noexcept(lhs.swap(rhs)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000434
435template<class charT, class traits, class Allocator>
436basic_istream<charT, traits>&
437operator>>(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
438
439template<class charT, class traits, class Allocator>
440basic_ostream<charT, traits>&
441operator<<(basic_ostream<charT, traits>& os, const basic_string<charT, traits, Allocator>& str);
442
443template<class charT, class traits, class Allocator>
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000444basic_istream<charT, traits>&
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000445getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str,
446 charT delim);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000447
448template<class charT, class traits, class Allocator>
449basic_istream<charT, traits>&
450getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
451
Marshall Clow29b53f22018-12-14 18:49:35 +0000452template<class charT, class traits, class Allocator, class U>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200453typename basic_string<charT, traits, Allocator>::size_type
454erase(basic_string<charT, traits, Allocator>& c, const U& value); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000455template<class charT, class traits, class Allocator, class Predicate>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200456typename basic_string<charT, traits, Allocator>::size_type
457erase_if(basic_string<charT, traits, Allocator>& c, Predicate pred); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000458
Howard Hinnantc51e1022010-05-11 19:42:16 +0000459typedef basic_string<char> string;
460typedef basic_string<wchar_t> wstring;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100461typedef basic_string<char8_t> u8string; // C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000462typedef basic_string<char16_t> u16string;
463typedef basic_string<char32_t> u32string;
464
Bruce Mitchener170d8972020-11-24 12:53:53 -0500465int stoi (const string& str, size_t* idx = nullptr, int base = 10);
466long stol (const string& str, size_t* idx = nullptr, int base = 10);
467unsigned long stoul (const string& str, size_t* idx = nullptr, int base = 10);
468long long stoll (const string& str, size_t* idx = nullptr, int base = 10);
469unsigned long long stoull(const string& str, size_t* idx = nullptr, int base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000470
Bruce Mitchener170d8972020-11-24 12:53:53 -0500471float stof (const string& str, size_t* idx = nullptr);
472double stod (const string& str, size_t* idx = nullptr);
473long double stold(const string& str, size_t* idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000474
475string to_string(int val);
476string to_string(unsigned val);
477string to_string(long val);
478string to_string(unsigned long val);
479string to_string(long long val);
480string to_string(unsigned long long val);
481string to_string(float val);
482string to_string(double val);
483string to_string(long double val);
484
Bruce Mitchener170d8972020-11-24 12:53:53 -0500485int stoi (const wstring& str, size_t* idx = nullptr, int base = 10);
486long stol (const wstring& str, size_t* idx = nullptr, int base = 10);
487unsigned long stoul (const wstring& str, size_t* idx = nullptr, int base = 10);
488long long stoll (const wstring& str, size_t* idx = nullptr, int base = 10);
489unsigned long long stoull(const wstring& str, size_t* idx = nullptr, int base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000490
Bruce Mitchener170d8972020-11-24 12:53:53 -0500491float stof (const wstring& str, size_t* idx = nullptr);
492double stod (const wstring& str, size_t* idx = nullptr);
493long double stold(const wstring& str, size_t* idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000494
495wstring to_wstring(int val);
496wstring to_wstring(unsigned val);
497wstring to_wstring(long val);
498wstring to_wstring(unsigned long val);
499wstring to_wstring(long long val);
500wstring to_wstring(unsigned long long val);
501wstring to_wstring(float val);
502wstring to_wstring(double val);
503wstring to_wstring(long double val);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000504
505template <> struct hash<string>;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100506template <> struct hash<u8string>; // C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000507template <> struct hash<u16string>;
508template <> struct hash<u32string>;
509template <> struct hash<wstring>;
510
Marshall Clowcba751f2013-07-23 17:05:24 +0000511basic_string<char> operator "" s( const char *str, size_t len ); // C++14
512basic_string<wchar_t> operator "" s( const wchar_t *str, size_t len ); // C++14
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100513basic_string<char8_t> operator "" s( const char8_t *str, size_t len ); // C++20
Marshall Clowcba751f2013-07-23 17:05:24 +0000514basic_string<char16_t> operator "" s( const char16_t *str, size_t len ); // C++14
515basic_string<char32_t> operator "" s( const char32_t *str, size_t len ); // C++14
516
Howard Hinnantc51e1022010-05-11 19:42:16 +0000517} // std
518
519*/
520
521#include <__config>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400522#include <__debug>
523#include <__functional_base>
Nikolas Klauser80840272022-02-04 13:04:33 +0100524#include <__ios/fpos.h>
Louis Dionne77249522021-06-11 09:55:11 -0400525#include <__iterator/wrap_iter.h>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000526#include <algorithm>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400527#include <compare>
528#include <cstdio> // EOF
Louis Dionned24191c2021-08-19 12:39:16 -0400529#include <cstdlib>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400530#include <cstring>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400531#include <initializer_list>
532#include <iosfwd>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000533#include <iterator>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000534#include <memory>
535#include <stdexcept>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400536#include <string_view>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000537#include <type_traits>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400538#include <utility>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000539#include <version>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000540
Louis Dionne89258142021-08-23 15:32:36 -0400541#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Nikolas Klauser80840272022-02-04 13:04:33 +0100542# include <cwchar>
Louis Dionne89258142021-08-23 15:32:36 -0400543#endif
544
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400545#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
Nikolas Klauser80840272022-02-04 13:04:33 +0100546# include <cstdint>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400547#endif
Eric Fiselier14b6de92014-08-10 23:53:08 +0000548
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000549#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Arthur O'Dwyer6eeaa002022-02-01 20:16:40 -0500550# pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000551#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000552
Eric Fiselierf4433a32017-05-31 22:07:49 +0000553_LIBCPP_PUSH_MACROS
554#include <__undef_macros>
555
556
Howard Hinnantc51e1022010-05-11 19:42:16 +0000557_LIBCPP_BEGIN_NAMESPACE_STD
558
Howard Hinnantc51e1022010-05-11 19:42:16 +0000559// basic_string
560
561template<class _CharT, class _Traits, class _Allocator>
562basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000563operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
564 const basic_string<_CharT, _Traits, _Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000565
566template<class _CharT, class _Traits, class _Allocator>
567basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000568operator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000569
570template<class _CharT, class _Traits, class _Allocator>
571basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000572operator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000573
574template<class _CharT, class _Traits, class _Allocator>
Louis Dionne8f8d95d2018-11-21 17:31:55 +0000575inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000576basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000577operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000578
579template<class _CharT, class _Traits, class _Allocator>
580basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000581operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000582
Shoaib Meenaiea363712017-07-29 02:54:41 +0000583_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&))
584
Marshall Clow039b2f02016-01-13 21:54:34 +0000585template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400586struct __string_is_trivial_iterator : public false_type {};
587
588template <class _Tp>
589struct __string_is_trivial_iterator<_Tp*>
590 : public is_arithmetic<_Tp> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000591
Louis Dionne173f29e2019-05-29 16:01:36 +0000592template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400593struct __string_is_trivial_iterator<__wrap_iter<_Iter> >
594 : public __string_is_trivial_iterator<_Iter> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000595
Marshall Clow82513342016-09-24 22:45:42 +0000596template <class _CharT, class _Traits, class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500597struct __can_be_converted_to_string_view : public _BoolConstant<
598 is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
599 !is_convertible<const _Tp&, const _CharT*>::value
600 > {};
Marshall Clow82513342016-09-24 22:45:42 +0000601
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000602#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000603
604template <class _CharT, size_t = sizeof(_CharT)>
605struct __padding
606{
607 unsigned char __xx[sizeof(_CharT)-1];
608};
609
610template <class _CharT>
611struct __padding<_CharT, 1>
612{
613};
614
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400615#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000616
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400617#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800618typedef basic_string<char8_t> u8string;
619#endif
620
621#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
622typedef basic_string<char16_t> u16string;
623typedef basic_string<char32_t> u32string;
Louis Dionne96fc5f52021-09-09 11:25:10 -0400624#endif
Richard Smith256954d2020-11-11 17:12:18 -0800625
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000626template<class _CharT, class _Traits, class _Allocator>
Richard Smith256954d2020-11-11 17:12:18 -0800627class
628 _LIBCPP_TEMPLATE_VIS
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400629#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800630 _LIBCPP_PREFERRED_NAME(u8string)
631#endif
632#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
633 _LIBCPP_PREFERRED_NAME(u16string)
634 _LIBCPP_PREFERRED_NAME(u32string)
635#endif
636 basic_string
Howard Hinnantc51e1022010-05-11 19:42:16 +0000637{
638public:
639 typedef basic_string __self;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000640 typedef basic_string_view<_CharT, _Traits> __self_view;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000641 typedef _Traits traits_type;
Marshall Clowa3a74e02017-03-15 18:41:11 +0000642 typedef _CharT value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000643 typedef _Allocator allocator_type;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000644 typedef allocator_traits<allocator_type> __alloc_traits;
645 typedef typename __alloc_traits::size_type size_type;
646 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant3e276872011-06-03 18:40:47 +0000647 typedef value_type& reference;
648 typedef const value_type& const_reference;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000649 typedef typename __alloc_traits::pointer pointer;
650 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000651
Marshall Clow79f33542018-03-21 00:36:05 +0000652 static_assert((!is_array<value_type>::value), "Character type of basic_string must not be an array");
653 static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string must be standard-layout");
654 static_assert(( is_trivial<value_type>::value), "Character type of basic_string must be trivial");
655 static_assert(( is_same<_CharT, typename traits_type::char_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000656 "traits_type::char_type must be the same type as CharT");
Marshall Clow79f33542018-03-21 00:36:05 +0000657 static_assert(( is_same<typename allocator_type::value_type, value_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000658 "Allocator::value_type must be same type as value_type");
Marshall Clowe46031a2018-07-02 18:41:15 +0000659
Howard Hinnantc51e1022010-05-11 19:42:16 +0000660 typedef __wrap_iter<pointer> iterator;
661 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000662 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
663 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000664
665private:
Howard Hinnant68bf1812013-04-30 21:44:48 +0000666
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000667#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000668
669 struct __long
670 {
671 pointer __data_;
672 size_type __size_;
673 size_type __cap_;
674 };
675
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000676#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000677 static const size_type __short_mask = 0x01;
678 static const size_type __long_mask = 0x1ul;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000679#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000680 static const size_type __short_mask = 0x80;
681 static const size_type __long_mask = ~(size_type(~0) >> 1);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400682#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +0000683
684 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
685 (sizeof(__long) - 1)/sizeof(value_type) : 2};
686
687 struct __short
688 {
689 value_type __data_[__min_cap];
690 struct
691 : __padding<value_type>
692 {
693 unsigned char __size_;
694 };
695 };
696
697#else
698
Howard Hinnantc51e1022010-05-11 19:42:16 +0000699 struct __long
700 {
701 size_type __cap_;
702 size_type __size_;
703 pointer __data_;
704 };
705
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000706#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000707 static const size_type __short_mask = 0x80;
708 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000709#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000710 static const size_type __short_mask = 0x01;
711 static const size_type __long_mask = 0x1ul;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400712#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +0000713
Howard Hinnantc51e1022010-05-11 19:42:16 +0000714 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
715 (sizeof(__long) - 1)/sizeof(value_type) : 2};
716
717 struct __short
718 {
719 union
720 {
721 unsigned char __size_;
Howard Hinnant49e145e2012-10-30 19:06:59 +0000722 value_type __lx;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000723 };
724 value_type __data_[__min_cap];
725 };
726
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400727#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000728
Howard Hinnant8ea98242013-08-23 17:37:05 +0000729 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000730
Howard Hinnant8ea98242013-08-23 17:37:05 +0000731 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000732
733 struct __raw
734 {
735 size_type __words[__n_words];
736 };
737
738 struct __rep
739 {
740 union
741 {
742 __long __l;
743 __short __s;
744 __raw __r;
745 };
746 };
747
748 __compressed_pair<__rep, allocator_type> __r_;
749
Howard Hinnantc51e1022010-05-11 19:42:16 +0000750public:
Martin Storsjö88890c22021-02-22 01:13:13 +0200751 _LIBCPP_TEMPLATE_DATA_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000752 static const size_type npos = -1;
753
Howard Hinnant3e276872011-06-03 18:40:47 +0000754 _LIBCPP_INLINE_VISIBILITY basic_string()
755 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000756
757 _LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a)
758#if _LIBCPP_STD_VER <= 14
759 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
760#else
761 _NOEXCEPT;
762#endif
763
Howard Hinnantc51e1022010-05-11 19:42:16 +0000764 basic_string(const basic_string& __str);
765 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clowa80abc72015-06-03 19:56:43 +0000766
Eric Fiselierfc92be82017-04-19 00:28:44 +0000767#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000768 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000769 basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +0000770#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +0000771 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000772#else
773 _NOEXCEPT;
774#endif
775
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000776 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000777 basic_string(basic_string&& __str, const allocator_type& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400778#endif // _LIBCPP_CXX03_LANG
Marshall Clowe46031a2018-07-02 18:41:15 +0000779
Louis Dionne9ce598d2021-09-08 09:14:43 -0400780 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Eric Fiseliera8567862018-07-17 05:48:48 +0000781 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5169d1c2019-12-16 19:03:23 -0500782 basic_string(const _CharT* __s) : __r_(__default_init_tag(), __default_init_tag()) {
Eric Fiseliera8567862018-07-17 05:48:48 +0000783 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
784 __init(__s, traits_type::length(__s));
Nikolas Klauserf2807732022-01-11 00:33:35 +0100785 _VSTD::__debug_db_insert_c(this);
Eric Fiseliera8567862018-07-17 05:48:48 +0000786 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000787
Louis Dionne9ce598d2021-09-08 09:14:43 -0400788 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000789 _LIBCPP_INLINE_VISIBILITY
790 basic_string(const _CharT* __s, const _Allocator& __a);
791
Marek Kurdej90d79712021-07-27 16:16:21 +0200792#if _LIBCPP_STD_VER > 20
793 basic_string(nullptr_t) = delete;
794#endif
795
Howard Hinnantcf823322010-12-17 14:46:43 +0000796 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000797 basic_string(const _CharT* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +0000798 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000799 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000800 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000801 basic_string(size_type __n, _CharT __c);
Marshall Clowe46031a2018-07-02 18:41:15 +0000802
Louis Dionne9ce598d2021-09-08 09:14:43 -0400803 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000804 _LIBCPP_INLINE_VISIBILITY
805 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
806
Marshall Clow83445802016-04-07 18:13:41 +0000807 basic_string(const basic_string& __str, size_type __pos, size_type __n,
Eric Fiselier812882b2017-02-17 01:17:10 +0000808 const _Allocator& __a = _Allocator());
Marshall Clow83445802016-04-07 18:13:41 +0000809 _LIBCPP_INLINE_VISIBILITY
810 basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +0000811 const _Allocator& __a = _Allocator());
Marshall Clowe46031a2018-07-02 18:41:15 +0000812
Louis Dionne9ce598d2021-09-08 09:14:43 -0400813 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 +0000814 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselier812882b2017-02-17 01:17:10 +0000815 basic_string(const _Tp& __t, size_type __pos, size_type __n,
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500816 const allocator_type& __a = allocator_type());
Marshall Clowe46031a2018-07-02 18:41:15 +0000817
Louis Dionne9ce598d2021-09-08 09:14:43 -0400818 template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500819 !__is_same_uncvref<_Tp, basic_string>::value> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000820 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
821 explicit basic_string(const _Tp& __t);
822
Louis Dionne9ce598d2021-09-08 09:14:43 -0400823 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 +0000824 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
825 explicit basic_string(const _Tp& __t, const allocator_type& __a);
826
Louis Dionne9ce598d2021-09-08 09:14:43 -0400827 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000828 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000829 basic_string(_InputIterator __first, _InputIterator __last);
Louis Dionne9ce598d2021-09-08 09:14:43 -0400830 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000831 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000832 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000833#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000834 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000835 basic_string(initializer_list<_CharT> __il);
Howard Hinnantcf823322010-12-17 14:46:43 +0000836 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000837 basic_string(initializer_list<_CharT> __il, const _Allocator& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400838#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000839
Eric Fiselierd9a702a2016-10-31 03:42:50 +0000840 inline ~basic_string();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000841
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000842 _LIBCPP_INLINE_VISIBILITY
843 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
844
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000845 basic_string& operator=(const basic_string& __str);
Eric Fiselier5ec13b12017-01-23 21:24:58 +0000846
Louis Dionne9ce598d2021-09-08 09:14:43 -0400847 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 +0000848 basic_string& operator=(const _Tp& __t)
849 {__self_view __sv = __t; return assign(__sv);}
850
Eric Fiselierfc92be82017-04-19 00:28:44 +0000851#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000852 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000853 basic_string& operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000854 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiselierfc92be82017-04-19 00:28:44 +0000855 _LIBCPP_INLINE_VISIBILITY
856 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000857#endif
Howard Hinnantd17880b2013-06-28 16:59:19 +0000858 _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const value_type* __s) {return assign(__s);}
Marek Kurdej90d79712021-07-27 16:16:21 +0200859#if _LIBCPP_STD_VER > 20
860 basic_string& operator=(nullptr_t) = delete;
861#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000862 basic_string& operator=(value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000863
Louis Dionneba400782020-10-02 15:02:52 -0400864#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +0000865 _LIBCPP_INLINE_VISIBILITY
866 iterator begin() _NOEXCEPT
867 {return iterator(this, __get_pointer());}
868 _LIBCPP_INLINE_VISIBILITY
869 const_iterator begin() const _NOEXCEPT
870 {return const_iterator(this, __get_pointer());}
871 _LIBCPP_INLINE_VISIBILITY
872 iterator end() _NOEXCEPT
873 {return iterator(this, __get_pointer() + size());}
874 _LIBCPP_INLINE_VISIBILITY
875 const_iterator end() const _NOEXCEPT
876 {return const_iterator(this, __get_pointer() + size());}
877#else
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000878 _LIBCPP_INLINE_VISIBILITY
879 iterator begin() _NOEXCEPT
880 {return iterator(__get_pointer());}
881 _LIBCPP_INLINE_VISIBILITY
882 const_iterator begin() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000883 {return const_iterator(__get_pointer());}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000884 _LIBCPP_INLINE_VISIBILITY
885 iterator end() _NOEXCEPT
886 {return iterator(__get_pointer() + size());}
887 _LIBCPP_INLINE_VISIBILITY
888 const_iterator end() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000889 {return const_iterator(__get_pointer() + size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400890#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000891 _LIBCPP_INLINE_VISIBILITY
892 reverse_iterator rbegin() _NOEXCEPT
893 {return reverse_iterator(end());}
894 _LIBCPP_INLINE_VISIBILITY
895 const_reverse_iterator rbegin() const _NOEXCEPT
896 {return const_reverse_iterator(end());}
897 _LIBCPP_INLINE_VISIBILITY
898 reverse_iterator rend() _NOEXCEPT
899 {return reverse_iterator(begin());}
900 _LIBCPP_INLINE_VISIBILITY
901 const_reverse_iterator rend() const _NOEXCEPT
902 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000903
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000904 _LIBCPP_INLINE_VISIBILITY
905 const_iterator cbegin() const _NOEXCEPT
906 {return begin();}
907 _LIBCPP_INLINE_VISIBILITY
908 const_iterator cend() const _NOEXCEPT
909 {return end();}
910 _LIBCPP_INLINE_VISIBILITY
911 const_reverse_iterator crbegin() const _NOEXCEPT
912 {return rbegin();}
913 _LIBCPP_INLINE_VISIBILITY
914 const_reverse_iterator crend() const _NOEXCEPT
915 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000916
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000917 _LIBCPP_INLINE_VISIBILITY size_type size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000918 {return __is_long() ? __get_long_size() : __get_short_size();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000919 _LIBCPP_INLINE_VISIBILITY size_type length() const _NOEXCEPT {return size();}
920 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT;
921 _LIBCPP_INLINE_VISIBILITY size_type capacity() const _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +0000922 {return (__is_long() ? __get_long_cap()
923 : static_cast<size_type>(__min_cap)) - 1;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000924
925 void resize(size_type __n, value_type __c);
926 _LIBCPP_INLINE_VISIBILITY void resize(size_type __n) {resize(__n, value_type());}
927
Marek Kurdejc9848142020-11-26 10:07:16 +0100928 void reserve(size_type __requested_capacity);
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100929
930#if _LIBCPP_STD_VER > 20
931 template <class _Op>
932 _LIBCPP_HIDE_FROM_ABI constexpr
933 void resize_and_overwrite(size_type __n, _Op __op) {
934 __resize_default_init(__n);
Nikolas Klausera4a76a12022-01-20 13:53:59 +0100935 __erase_to_end(_VSTD::move(__op)(data(), _LIBCPP_AUTO_CAST(__n)));
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100936 }
937#endif
938
Eric Fiselier451d5582018-11-26 20:15:38 +0000939 _LIBCPP_INLINE_VISIBILITY void __resize_default_init(size_type __n);
940
Marek Kurdejc9848142020-11-26 10:07:16 +0100941 _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_INLINE_VISIBILITY
942 void reserve() _NOEXCEPT {shrink_to_fit();}
Marshall Clow6ae12a82018-11-28 18:18:34 +0000943 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejc9848142020-11-26 10:07:16 +0100944 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000945 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000946 void clear() _NOEXCEPT;
Marshall Clowb7db4972017-11-15 20:02:27 +0000947 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
948 bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000949
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000950 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __pos) const _NOEXCEPT;
951 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000952
953 const_reference at(size_type __n) const;
954 reference at(size_type __n);
955
956 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const basic_string& __str) {return append(__str);}
Marshall Clowe46031a2018-07-02 18:41:15 +0000957
958 template <class _Tp>
959 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -0400960 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +0000961 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500962 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
963 && !__is_same_uncvref<_Tp, basic_string >::value,
Marshall Clowe46031a2018-07-02 18:41:15 +0000964 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500965 >
Marshall Clowe46031a2018-07-02 18:41:15 +0000966 operator+=(const _Tp& __t) {__self_view __sv = __t; return append(__sv);}
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000967 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const value_type* __s) {return append(__s);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000968 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(value_type __c) {push_back(__c); return *this;}
Eric Fiselierfc92be82017-04-19 00:28:44 +0000969#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000970 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(initializer_list<value_type> __il) {return append(__il);}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400971#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000972
Howard Hinnantcf823322010-12-17 14:46:43 +0000973 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000974 basic_string& append(const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000975
976 template <class _Tp>
977 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -0400978 __enable_if_t<
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500979 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
980 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clowe46031a2018-07-02 18:41:15 +0000981 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500982 >
Marshall Clowe46031a2018-07-02 18:41:15 +0000983 append(const _Tp& __t) { __self_view __sv = __t; return append(__sv.data(), __sv.size()); }
Marshall Clow8db7fb02014-03-04 19:17:19 +0000984 basic_string& append(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clowe46031a2018-07-02 18:41:15 +0000985
Marshall Clow62953962016-10-03 23:40:48 +0000986 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +0000987 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -0400988 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +0000989 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500990 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
991 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +0000992 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500993 >
Marshall Clow82513342016-09-24 22:45:42 +0000994 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +0000995 basic_string& append(const value_type* __s, size_type __n);
996 basic_string& append(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000997 basic_string& append(size_type __n, value_type __c);
Eric Fiselier451d5582018-11-26 20:15:38 +0000998
999 _LIBCPP_INLINE_VISIBILITY
1000 void __append_default_init(size_type __n);
1001
Howard Hinnantc51e1022010-05-11 19:42:16 +00001002 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001003 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001004 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001005 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001006 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001007 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001008 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001009 _LIBCPP_INLINE_VISIBILITY
1010 append(_InputIterator __first, _InputIterator __last) {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001011 const basic_string __temp(__first, __last, __alloc());
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001012 append(__temp.data(), __temp.size());
1013 return *this;
1014 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001015 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001016 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001017 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001018 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001019 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001020 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001021 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001022 _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001023 append(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001024
Eric Fiselierfc92be82017-04-19 00:28:44 +00001025#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001026 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001027 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001028#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001029
1030 void push_back(value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001031 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001032 void pop_back();
Marshall Clow05cf6692019-03-19 03:30:07 +00001033 _LIBCPP_INLINE_VISIBILITY reference front() _NOEXCEPT;
1034 _LIBCPP_INLINE_VISIBILITY const_reference front() const _NOEXCEPT;
1035 _LIBCPP_INLINE_VISIBILITY reference back() _NOEXCEPT;
1036 _LIBCPP_INLINE_VISIBILITY const_reference back() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001037
Marshall Clowe46031a2018-07-02 18:41:15 +00001038 template <class _Tp>
1039 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001040 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001041 <
1042 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1043 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001044 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001045 assign(const _Tp & __t) { __self_view __sv = __t; return assign(__sv.data(), __sv.size()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001046 _LIBCPP_INLINE_VISIBILITY
Marshall Clow95d5e9a2016-03-09 18:08:29 +00001047 basic_string& assign(const basic_string& __str) { return *this = __str; }
Eric Fiselierfc92be82017-04-19 00:28:44 +00001048#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001049 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001050 basic_string& assign(basic_string&& __str)
Marshall Clow5aa9e942015-10-05 16:17:34 +00001051 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001052 {*this = _VSTD::move(__str); return *this;}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001053#endif
Marshall Clow8db7fb02014-03-04 19:17:19 +00001054 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow62953962016-10-03 23:40:48 +00001055 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001056 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001057 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001058 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001059 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1060 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001061 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001062 >
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001063 assign(const _Tp & __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001064 basic_string& assign(const value_type* __s, size_type __n);
1065 basic_string& assign(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001066 basic_string& assign(size_type __n, value_type __c);
1067 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001068 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001069 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001070 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001071 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001072 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001073 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001074 assign(_InputIterator __first, _InputIterator __last);
1075 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001076 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001077 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001078 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001079 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001080 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001081 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001082 assign(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001083#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001084 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001085 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001086#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001087
Howard Hinnantcf823322010-12-17 14:46:43 +00001088 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001089 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001090
1091 template <class _Tp>
1092 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001093 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001094 <
1095 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1096 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001097 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001098 insert(size_type __pos1, const _Tp& __t)
1099 { __self_view __sv = __t; return insert(__pos1, __sv.data(), __sv.size()); }
1100
Marshall Clow82513342016-09-24 22:45:42 +00001101 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001102 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001103 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001104 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001105 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001106 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001107 >
Marshall Clow82513342016-09-24 22:45:42 +00001108 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Marshall Clow8db7fb02014-03-04 19:17:19 +00001109 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001110 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1111 basic_string& insert(size_type __pos, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001112 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1113 iterator insert(const_iterator __pos, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001114 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001115 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1116 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001117 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001118 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001119 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001120 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001121 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001122 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001123 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1124 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001125 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001126 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001127 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001128 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001129 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001130 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001131 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001132#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001133 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001134 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1135 {return insert(__pos, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001136#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001137
1138 basic_string& erase(size_type __pos = 0, size_type __n = npos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001139 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001140 iterator erase(const_iterator __pos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001141 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001142 iterator erase(const_iterator __first, const_iterator __last);
1143
Howard Hinnantcf823322010-12-17 14:46:43 +00001144 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001145 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001146
1147 template <class _Tp>
1148 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001149 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001150 <
1151 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1152 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001153 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001154 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 +00001155 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 +00001156 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001157 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001158 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001159 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001160 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001161 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001162 >
Marshall Clow82513342016-09-24 22:45:42 +00001163 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001164 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
1165 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001166 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001167 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001168 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001169
1170 template <class _Tp>
1171 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001172 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001173 <
1174 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1175 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001176 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001177 replace(const_iterator __i1, const_iterator __i2, const _Tp& __t) { __self_view __sv = __t; return replace(__i1 - begin(), __i2 - __i1, __sv); }
1178
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001179 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001180 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +00001181 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001182 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Howard Hinnantcf823322010-12-17 14:46:43 +00001183 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001184 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001185 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001186 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001187 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001188 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001189 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001190 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001191 >
Howard Hinnant990d6e82010-11-17 21:11:40 +00001192 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001193#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001194 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001195 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001196 {return replace(__i1, __i2, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001197#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001198
Howard Hinnantd17880b2013-06-28 16:59:19 +00001199 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
Howard Hinnantcf823322010-12-17 14:46:43 +00001200 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001201 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1202
Howard Hinnantcf823322010-12-17 14:46:43 +00001203 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001204 void swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001205#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00001206 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +00001207#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001208 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001209 __is_nothrow_swappable<allocator_type>::value);
1210#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001211
Howard Hinnantcf823322010-12-17 14:46:43 +00001212 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001213 const value_type* c_str() const _NOEXCEPT {return data();}
Howard Hinnantcf823322010-12-17 14:46:43 +00001214 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001215 const value_type* data() const _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Eric Fiselier4ee2a892017-11-20 20:23:27 +00001216#if _LIBCPP_STD_VER > 14 || defined(_LIBCPP_BUILDING_LIBRARY)
Marshall Clowd3c22392016-03-08 15:44:30 +00001217 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001218 value_type* data() _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Marshall Clowd3c22392016-03-08 15:44:30 +00001219#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001220
Howard Hinnantcf823322010-12-17 14:46:43 +00001221 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001222 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001223
Howard Hinnantcf823322010-12-17 14:46:43 +00001224 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001225 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001226
1227 template <class _Tp>
1228 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001229 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001230 <
1231 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1232 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001233 >
zoecarver1997e0a2021-02-05 11:54:47 -08001234 find(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001235 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001236 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001237 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001238 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001239
Howard Hinnantcf823322010-12-17 14:46:43 +00001240 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001241 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001242
1243 template <class _Tp>
1244 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001245 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001246 <
1247 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1248 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001249 >
zoecarver1997e0a2021-02-05 11:54:47 -08001250 rfind(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001251 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001252 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001253 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001254 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001255
Howard Hinnantcf823322010-12-17 14:46:43 +00001256 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001257 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001258
1259 template <class _Tp>
1260 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001261 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001262 <
1263 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1264 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001265 >
zoecarver1997e0a2021-02-05 11:54:47 -08001266 find_first_of(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001267 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001268 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001269 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001270 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001271 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001272
Howard Hinnantcf823322010-12-17 14:46:43 +00001273 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001274 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001275
1276 template <class _Tp>
1277 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001278 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001279 <
1280 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1281 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001282 >
zoecarver1997e0a2021-02-05 11:54:47 -08001283 find_last_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001284 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001285 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001286 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001287 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001288 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001289
Howard Hinnantcf823322010-12-17 14:46:43 +00001290 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001291 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001292
1293 template <class _Tp>
1294 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001295 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001296 <
1297 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1298 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001299 >
zoecarver1997e0a2021-02-05 11:54:47 -08001300 find_first_not_of(const _Tp &__t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001301 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 +00001302 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001303 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001304 _LIBCPP_INLINE_VISIBILITY
1305 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1306
1307 _LIBCPP_INLINE_VISIBILITY
1308 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001309
1310 template <class _Tp>
1311 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001312 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001313 <
1314 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1315 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001316 >
zoecarver1997e0a2021-02-05 11:54:47 -08001317 find_last_not_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001318 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 +00001319 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001320 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001321 _LIBCPP_INLINE_VISIBILITY
1322 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1323
1324 _LIBCPP_INLINE_VISIBILITY
1325 int compare(const basic_string& __str) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001326
1327 template <class _Tp>
1328 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001329 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001330 <
1331 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1332 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001333 >
zoecarver1997e0a2021-02-05 11:54:47 -08001334 compare(const _Tp &__t) 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 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001343 compare(size_type __pos1, size_type __n1, const _Tp& __t) const;
1344
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001345 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001346 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Marshall Clow8db7fb02014-03-04 19:17:19 +00001347 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 +00001348
Marshall Clow82513342016-09-24 22:45:42 +00001349 template <class _Tp>
Eric Fiselier7d4aa5c2016-10-14 05:29:46 +00001350 inline _LIBCPP_INLINE_VISIBILITY
Louis Dionne9ce598d2021-09-08 09:14:43 -04001351 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001352 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001353 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001354 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001355 >
Marshall Clow82513342016-09-24 22:45:42 +00001356 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 +00001357 int compare(const value_type* __s) const _NOEXCEPT;
1358 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1359 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001360
Marshall Clow18c293b2017-12-04 20:11:38 +00001361#if _LIBCPP_STD_VER > 17
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001362 constexpr _LIBCPP_INLINE_VISIBILITY
1363 bool starts_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001364 { return __self_view(data(), size()).starts_with(__sv); }
1365
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001366 constexpr _LIBCPP_INLINE_VISIBILITY
1367 bool starts_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001368 { return !empty() && _Traits::eq(front(), __c); }
1369
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001370 constexpr _LIBCPP_INLINE_VISIBILITY
1371 bool starts_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001372 { return starts_with(__self_view(__s)); }
1373
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001374 constexpr _LIBCPP_INLINE_VISIBILITY
1375 bool ends_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001376 { return __self_view(data(), size()).ends_with( __sv); }
1377
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001378 constexpr _LIBCPP_INLINE_VISIBILITY
1379 bool ends_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001380 { return !empty() && _Traits::eq(back(), __c); }
1381
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001382 constexpr _LIBCPP_INLINE_VISIBILITY
1383 bool ends_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001384 { return ends_with(__self_view(__s)); }
1385#endif
1386
Wim Leflere023c3542021-01-19 14:33:30 -05001387#if _LIBCPP_STD_VER > 20
1388 constexpr _LIBCPP_INLINE_VISIBILITY
1389 bool contains(__self_view __sv) const noexcept
1390 { return __self_view(data(), size()).contains(__sv); }
1391
1392 constexpr _LIBCPP_INLINE_VISIBILITY
1393 bool contains(value_type __c) const noexcept
1394 { return __self_view(data(), size()).contains(__c); }
1395
1396 constexpr _LIBCPP_INLINE_VISIBILITY
1397 bool contains(const value_type* __s) const
1398 { return __self_view(data(), size()).contains(__s); }
1399#endif
1400
Howard Hinnantcf823322010-12-17 14:46:43 +00001401 _LIBCPP_INLINE_VISIBILITY bool __invariants() const;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001402
Marshall Clowe60a7182018-05-29 17:04:37 +00001403 _LIBCPP_INLINE_VISIBILITY void __clear_and_shrink() _NOEXCEPT;
Marshall Clow851b9ec2019-05-20 21:56:51 +00001404
Marek Kurdejc9848142020-11-26 10:07:16 +01001405 _LIBCPP_INLINE_VISIBILITY void __shrink_or_extend(size_type __target_capacity);
1406
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001407 _LIBCPP_INLINE_VISIBILITY
1408 bool __is_long() const _NOEXCEPT
1409 {return bool(__r_.first().__s.__size_ & __short_mask);}
1410
Louis Dionneba400782020-10-02 15:02:52 -04001411#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001412
1413 bool __dereferenceable(const const_iterator* __i) const;
1414 bool __decrementable(const const_iterator* __i) const;
1415 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1416 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1417
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001418#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001419
Howard Hinnantc51e1022010-05-11 19:42:16 +00001420private:
Nikolas Klauser93826d12022-01-04 17:24:03 +01001421 _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI static bool __fits_in_sso(size_type __sz) {
1422 // SSO is disabled during constant evaluation because `__is_long` isn't constexpr friendly
1423 return !__libcpp_is_constant_evaluated() && (__sz < __min_cap);
1424 }
1425
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001426 _LIBCPP_INLINE_VISIBILITY
1427 allocator_type& __alloc() _NOEXCEPT
1428 {return __r_.second();}
1429 _LIBCPP_INLINE_VISIBILITY
1430 const allocator_type& __alloc() const _NOEXCEPT
1431 {return __r_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001432
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001433#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001434
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001435 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001436 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001437# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001438 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001439# else
1440 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1441# endif
1442
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001443 _LIBCPP_INLINE_VISIBILITY
1444 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001445# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001446 {return __r_.first().__s.__size_ >> 1;}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001447# else
1448 {return __r_.first().__s.__size_;}
1449# endif
1450
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001451#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001452
1453 _LIBCPP_INLINE_VISIBILITY
1454 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001455# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001456 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1457# else
1458 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
1459# endif
1460
1461 _LIBCPP_INLINE_VISIBILITY
1462 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001463# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001464 {return __r_.first().__s.__size_;}
1465# else
1466 {return __r_.first().__s.__size_ >> 1;}
1467# endif
1468
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001469#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001470
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001471 _LIBCPP_INLINE_VISIBILITY
1472 void __set_long_size(size_type __s) _NOEXCEPT
1473 {__r_.first().__l.__size_ = __s;}
1474 _LIBCPP_INLINE_VISIBILITY
1475 size_type __get_long_size() const _NOEXCEPT
1476 {return __r_.first().__l.__size_;}
1477 _LIBCPP_INLINE_VISIBILITY
1478 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001479 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1480
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001481 _LIBCPP_INLINE_VISIBILITY
1482 void __set_long_cap(size_type __s) _NOEXCEPT
1483 {__r_.first().__l.__cap_ = __long_mask | __s;}
1484 _LIBCPP_INLINE_VISIBILITY
1485 size_type __get_long_cap() const _NOEXCEPT
Howard Hinnant28b24882011-12-01 20:21:04 +00001486 {return __r_.first().__l.__cap_ & size_type(~__long_mask);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001487
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001488 _LIBCPP_INLINE_VISIBILITY
1489 void __set_long_pointer(pointer __p) _NOEXCEPT
1490 {__r_.first().__l.__data_ = __p;}
1491 _LIBCPP_INLINE_VISIBILITY
1492 pointer __get_long_pointer() _NOEXCEPT
1493 {return __r_.first().__l.__data_;}
1494 _LIBCPP_INLINE_VISIBILITY
1495 const_pointer __get_long_pointer() const _NOEXCEPT
1496 {return __r_.first().__l.__data_;}
1497 _LIBCPP_INLINE_VISIBILITY
1498 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001499 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001500 _LIBCPP_INLINE_VISIBILITY
1501 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001502 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001503 _LIBCPP_INLINE_VISIBILITY
1504 pointer __get_pointer() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001505 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001506 _LIBCPP_INLINE_VISIBILITY
1507 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001508 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1509
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001510 _LIBCPP_INLINE_VISIBILITY
1511 void __zero() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001512 {
1513 size_type (&__a)[__n_words] = __r_.first().__r.__words;
1514 for (unsigned __i = 0; __i < __n_words; ++__i)
1515 __a[__i] = 0;
1516 }
1517
1518 template <size_type __a> static
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001519 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00001520 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +00001521 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001522 enum {__alignment = 16};
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001523 static _LIBCPP_INLINE_VISIBILITY
1524 size_type __recommend(size_type __s) _NOEXCEPT
Marshall Clow80584522018-02-07 21:30:17 +00001525 {
1526 if (__s < __min_cap) return static_cast<size_type>(__min_cap) - 1;
1527 size_type __guess = __align_it<sizeof(value_type) < __alignment ?
1528 __alignment/sizeof(value_type) : 1 > (__s+1) - 1;
1529 if (__guess == __min_cap) ++__guess;
1530 return __guess;
1531 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001532
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001533 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001534 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001535 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001536 void __init(const value_type* __s, size_type __sz);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001537 inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001538 void __init(size_type __n, value_type __c);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001539
Martijn Vels5e7c9752020-03-04 17:52:46 -05001540 // Slow path for the (inlined) copy constructor for 'long' strings.
1541 // Always externally instantiated and not inlined.
1542 // Requires that __s is zero terminated.
1543 // The main reason for this function to exist is because for unstable, we
1544 // want to allow inlining of the copy constructor. However, we don't want
1545 // to call the __init() functions as those are marked as inline which may
1546 // result in over-aggressive inlining by the compiler, where our aim is
1547 // to only inline the fast path code directly in the ctor.
1548 void __init_copy_ctor_external(const value_type* __s, size_type __sz);
1549
Howard Hinnantc51e1022010-05-11 19:42:16 +00001550 template <class _InputIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001551 inline
Louis Dionne9ce598d2021-09-08 09:14:43 -04001552 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001553 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001554 __is_exactly_cpp17_input_iterator<_InputIterator>::value
1555 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001556 __init(_InputIterator __first, _InputIterator __last);
1557
1558 template <class _ForwardIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001559 inline
Louis Dionne9ce598d2021-09-08 09:14:43 -04001560 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001561 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001562 __is_cpp17_forward_iterator<_ForwardIterator>::value
1563 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001564 __init(_ForwardIterator __first, _ForwardIterator __last);
1565
1566 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001567 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001568 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1569 size_type __n_copy, size_type __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00001570 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001571
Martijn Vels596e3de2020-02-26 15:55:49 -05001572 // __assign_no_alias is invoked for assignment operations where we
1573 // have proof that the input does not alias the current instance.
1574 // For example, operator=(basic_string) performs a 'self' check.
1575 template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04001576 basic_string& __assign_no_alias(const value_type* __s, size_type __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05001577
Howard Hinnantcf823322010-12-17 14:46:43 +00001578 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001579 void __erase_to_end(size_type __pos);
1580
Martijn Velsa81fc792020-02-26 13:25:43 -05001581 // __erase_external_with_move is invoked for erase() invocations where
1582 // `n ~= npos`, likely requiring memory moves on the string data.
1583 void __erase_external_with_move(size_type __pos, size_type __n);
1584
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001585 _LIBCPP_INLINE_VISIBILITY
1586 void __copy_assign_alloc(const basic_string& __str)
1587 {__copy_assign_alloc(__str, integral_constant<bool,
1588 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1589
1590 _LIBCPP_INLINE_VISIBILITY
1591 void __copy_assign_alloc(const basic_string& __str, true_type)
1592 {
Marshall Clowf258c202017-01-31 03:40:52 +00001593 if (__alloc() == __str.__alloc())
1594 __alloc() = __str.__alloc();
1595 else
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001596 {
Marshall Clowf258c202017-01-31 03:40:52 +00001597 if (!__str.__is_long())
1598 {
Vedant Kumar55e007e2018-03-08 21:15:26 +00001599 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001600 __alloc() = __str.__alloc();
1601 }
1602 else
1603 {
1604 allocator_type __a = __str.__alloc();
1605 pointer __p = __alloc_traits::allocate(__a, __str.__get_long_cap());
Vedant Kumar55e007e2018-03-08 21:15:26 +00001606 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001607 __alloc() = _VSTD::move(__a);
1608 __set_long_pointer(__p);
1609 __set_long_cap(__str.__get_long_cap());
1610 __set_long_size(__str.size());
1611 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001612 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001613 }
1614
1615 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001616 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001617 {}
1618
Eric Fiselierfc92be82017-04-19 00:28:44 +00001619#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +00001620 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001621 void __move_assign(basic_string& __str, false_type)
1622 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnantcf823322010-12-17 14:46:43 +00001623 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001624 void __move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001625#if _LIBCPP_STD_VER > 14
1626 _NOEXCEPT;
1627#else
Howard Hinnant3e276872011-06-03 18:40:47 +00001628 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001629#endif
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001630#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001631
1632 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001633 void
Howard Hinnantc2734962011-09-02 20:42:31 +00001634 __move_assign_alloc(basic_string& __str)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001635 _NOEXCEPT_(
1636 !__alloc_traits::propagate_on_container_move_assignment::value ||
1637 is_nothrow_move_assignable<allocator_type>::value)
1638 {__move_assign_alloc(__str, integral_constant<bool,
1639 __alloc_traits::propagate_on_container_move_assignment::value>());}
1640
1641 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00001642 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001643 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1644 {
1645 __alloc() = _VSTD::move(__c.__alloc());
1646 }
1647
1648 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001649 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001650 _NOEXCEPT
1651 {}
1652
Martijn Velsda7d94f2020-06-19 14:24:03 -04001653 basic_string& __assign_external(const value_type* __s);
1654 basic_string& __assign_external(const value_type* __s, size_type __n);
1655
1656 // Assigns the value in __s, guaranteed to be __n < __min_cap in length.
1657 inline basic_string& __assign_short(const value_type* __s, size_type __n) {
1658 pointer __p = __is_long()
1659 ? (__set_long_size(__n), __get_long_pointer())
1660 : (__set_short_size(__n), __get_short_pointer());
1661 traits_type::move(_VSTD::__to_address(__p), __s, __n);
1662 traits_type::assign(__p[__n], value_type());
1663 return *this;
1664 }
1665
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01001666 _LIBCPP_HIDE_FROM_ABI basic_string& __null_terminate_at(value_type* __p, size_type __newsz) {
1667 __set_size(__newsz);
1668 __invalidate_iterators_past(__newsz);
1669 traits_type::assign(__p[__newsz], value_type());
1670 return *this;
1671 }
1672
Howard Hinnantcf823322010-12-17 14:46:43 +00001673 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
1674 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(size_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001675
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001676 template<class _Tp>
1677 _LIBCPP_INLINE_VISIBILITY
1678 bool __addr_in_range(_Tp&& __t) const {
1679 const volatile void *__p = _VSTD::addressof(__t);
1680 return data() <= __p && __p <= data() + size();
1681 }
1682
Louis Dionned24191c2021-08-19 12:39:16 -04001683 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1684 void __throw_length_error() const {
Nikolas Klauserf89bb932022-01-24 19:44:34 +01001685 _VSTD::__throw_length_error("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001686 }
1687
1688 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1689 void __throw_out_of_range() const {
Nikolas Klauserf89bb932022-01-24 19:44:34 +01001690 _VSTD::__throw_out_of_range("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001691 }
1692
Howard Hinnantc51e1022010-05-11 19:42:16 +00001693 friend basic_string operator+<>(const basic_string&, const basic_string&);
1694 friend basic_string operator+<>(const value_type*, const basic_string&);
1695 friend basic_string operator+<>(value_type, const basic_string&);
1696 friend basic_string operator+<>(const basic_string&, const value_type*);
1697 friend basic_string operator+<>(const basic_string&, value_type);
1698};
1699
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001700// These declarations must appear before any functions are implicitly used
1701// so that they have the correct visibility specifier.
1702#ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
Louis Dionne89258142021-08-23 15:32:36 -04001703 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1704# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1705 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1706# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001707#else
Louis Dionne89258142021-08-23 15:32:36 -04001708 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1709# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1710 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1711# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001712#endif
1713
1714
Louis Dionned59f8a52021-08-17 11:59:07 -04001715#if _LIBCPP_STD_VER >= 17
Marshall Clowa0563332018-02-08 06:34:03 +00001716template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -05001717 class _CharT = __iter_value_type<_InputIterator>,
Marshall Clowa0563332018-02-08 06:34:03 +00001718 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001719 class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
1720 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowa0563332018-02-08 06:34:03 +00001721 >
1722basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
1723 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
Marshall Clowe46031a2018-07-02 18:41:15 +00001724
1725template<class _CharT,
1726 class _Traits,
1727 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001728 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowe46031a2018-07-02 18:41:15 +00001729 >
1730explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
1731 -> basic_string<_CharT, _Traits, _Allocator>;
1732
1733template<class _CharT,
1734 class _Traits,
1735 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001736 class = enable_if_t<__is_allocator<_Allocator>::value>,
Marshall Clowe46031a2018-07-02 18:41:15 +00001737 class _Sz = typename allocator_traits<_Allocator>::size_type
1738 >
1739basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
1740 -> basic_string<_CharT, _Traits, _Allocator>;
Marshall Clowa0563332018-02-08 06:34:03 +00001741#endif
1742
Howard Hinnantc51e1022010-05-11 19:42:16 +00001743template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001744inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001745void
1746basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
1747{
Louis Dionneba400782020-10-02 15:02:52 -04001748#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001749 if (!__libcpp_is_constant_evaluated())
1750 __get_db()->__invalidate_all(this);
Louis Dionneba400782020-10-02 15:02:52 -04001751#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001752}
1753
1754template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001755inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001756void
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001757basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type __pos)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001758{
Louis Dionneba400782020-10-02 15:02:52 -04001759#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001760 if (!__libcpp_is_constant_evaluated()) {
1761 __c_node* __c = __get_db()->__find_c_and_lock(this);
1762 if (__c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001763 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001764 const_pointer __new_last = __get_pointer() + __pos;
1765 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001766 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001767 --__p;
1768 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1769 if (__i->base() > __new_last)
1770 {
1771 (*__p)->__c_ = nullptr;
1772 if (--__c->end_ != __p)
1773 _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1774 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001775 }
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001776 __get_db()->unlock();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001777 }
1778 }
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001779#else
1780 (void)__pos;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001781#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001782}
1783
1784template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001785inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001786basic_string<_CharT, _Traits, _Allocator>::basic_string()
Marshall Clowe546cbb2015-06-04 02:05:41 +00001787 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001788 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001789{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001790 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001791 __zero();
1792}
1793
1794template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001795inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001796basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
Eric Fiseliere012bf22015-07-18 20:40:46 +00001797#if _LIBCPP_STD_VER <= 14
1798 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1799#else
1800 _NOEXCEPT
1801#endif
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001802: __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001803{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001804 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001805 __zero();
1806}
1807
1808template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier815ed732016-09-16 00:00:48 +00001809void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1810 size_type __sz,
1811 size_type __reserve)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001812{
1813 if (__reserve > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001814 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001815 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001816 if (__fits_in_sso(__reserve))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001817 {
1818 __set_short_size(__sz);
1819 __p = __get_short_pointer();
1820 }
1821 else
1822 {
1823 size_type __cap = __recommend(__reserve);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001824 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001825 __set_long_pointer(__p);
1826 __set_long_cap(__cap+1);
1827 __set_long_size(__sz);
1828 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001829 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001830 traits_type::assign(__p[__sz], value_type());
1831}
1832
1833template <class _CharT, class _Traits, class _Allocator>
1834void
Howard Hinnantd17880b2013-06-28 16:59:19 +00001835basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001836{
1837 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001838 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001839 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001840 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001841 {
1842 __set_short_size(__sz);
1843 __p = __get_short_pointer();
1844 }
1845 else
1846 {
1847 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001848 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001849 __set_long_pointer(__p);
1850 __set_long_cap(__cap+1);
1851 __set_long_size(__sz);
1852 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001853 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001854 traits_type::assign(__p[__sz], value_type());
1855}
1856
1857template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00001858template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00001859basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001860 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001861{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001862 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001863 __init(__s, traits_type::length(__s));
Nikolas Klauserf2807732022-01-11 00:33:35 +01001864 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001865}
1866
1867template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001868inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001869basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001870 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001871{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001872 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
1873 __init(__s, __n);
1874 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001875}
1876
1877template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001878inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001879basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001880 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001881{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001882 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001883 __init(__s, __n);
Nikolas Klauserf2807732022-01-11 00:33:35 +01001884 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001885}
1886
1887template <class _CharT, class _Traits, class _Allocator>
1888basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001889 : __r_(__default_init_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001890{
1891 if (!__str.__is_long())
1892 __r_.first().__r = __str.__r_.first().__r;
1893 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001894 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1895 __str.__get_long_size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01001896 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001897}
1898
1899template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00001900basic_string<_CharT, _Traits, _Allocator>::basic_string(
1901 const basic_string& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001902 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001903{
1904 if (!__str.__is_long())
1905 __r_.first().__r = __str.__r_.first().__r;
1906 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001907 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1908 __str.__get_long_size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01001909 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001910}
1911
Martijn Vels5e7c9752020-03-04 17:52:46 -05001912template <class _CharT, class _Traits, class _Allocator>
1913void basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(
1914 const value_type* __s, size_type __sz) {
1915 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001916 if (__fits_in_sso(__sz)) {
Martijn Vels5e7c9752020-03-04 17:52:46 -05001917 __p = __get_short_pointer();
1918 __set_short_size(__sz);
1919 } else {
1920 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001921 __throw_length_error();
Martijn Vels5e7c9752020-03-04 17:52:46 -05001922 size_t __cap = __recommend(__sz);
1923 __p = __alloc_traits::allocate(__alloc(), __cap + 1);
1924 __set_long_pointer(__p);
1925 __set_long_cap(__cap + 1);
1926 __set_long_size(__sz);
1927 }
1928 traits_type::copy(_VSTD::__to_address(__p), __s, __sz + 1);
1929}
1930
Eric Fiselierfc92be82017-04-19 00:28:44 +00001931#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001932
1933template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001934inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001935basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +00001936#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +00001937 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowa80abc72015-06-03 19:56:43 +00001938#else
1939 _NOEXCEPT
1940#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001941 : __r_(_VSTD::move(__str.__r_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001942{
1943 __str.__zero();
Nikolas Klauserf2807732022-01-11 00:33:35 +01001944 _VSTD::__debug_db_insert_c(this);
Louis Dionneba400782020-10-02 15:02:52 -04001945#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauserf2807732022-01-11 00:33:35 +01001946 if (!__libcpp_is_constant_evaluated() && __is_long())
1947 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001948#endif
1949}
1950
1951template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001952inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001953basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001954 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001955{
Marshall Clowd2d24692014-07-17 15:32:20 +00001956 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001957 __init(_VSTD::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd2d24692014-07-17 15:32:20 +00001958 else
1959 {
1960 __r_.first().__r = __str.__r_.first().__r;
1961 __str.__zero();
1962 }
Nikolas Klauserf2807732022-01-11 00:33:35 +01001963 _VSTD::__debug_db_insert_c(this);
Louis Dionneba400782020-10-02 15:02:52 -04001964#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauserf2807732022-01-11 00:33:35 +01001965 if (!__libcpp_is_constant_evaluated() && __is_long())
1966 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001967#endif
1968}
1969
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001970#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001971
1972template <class _CharT, class _Traits, class _Allocator>
1973void
1974basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
1975{
1976 if (__n > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001977 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001978 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001979 if (__fits_in_sso(__n))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001980 {
1981 __set_short_size(__n);
1982 __p = __get_short_pointer();
1983 }
1984 else
1985 {
1986 size_type __cap = __recommend(__n);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001987 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001988 __set_long_pointer(__p);
1989 __set_long_cap(__cap+1);
1990 __set_long_size(__n);
1991 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001992 traits_type::assign(_VSTD::__to_address(__p), __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001993 traits_type::assign(__p[__n], value_type());
1994}
1995
1996template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001997inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001998basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001999 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002000{
2001 __init(__n, __c);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002002 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002003}
2004
2005template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002006template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00002007basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002008 : __r_(__default_init_tag(), __a)
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
Howard Hinnantc51e1022010-05-11 19:42:16 +00002014template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00002015basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
2016 size_type __pos, size_type __n,
2017 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002018 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002019{
2020 size_type __str_sz = __str.size();
2021 if (__pos > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002022 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002023 __init(__str.data() + __pos, _VSTD::min(__n, __str_sz - __pos));
Nikolas Klauserf2807732022-01-11 00:33:35 +01002024 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002025}
2026
2027template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002028inline
Marshall Clow83445802016-04-07 18:13:41 +00002029basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +00002030 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002031 : __r_(__default_init_tag(), __a)
Marshall Clow83445802016-04-07 18:13:41 +00002032{
2033 size_type __str_sz = __str.size();
2034 if (__pos > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002035 __throw_out_of_range();
Marshall Clow83445802016-04-07 18:13:41 +00002036 __init(__str.data() + __pos, __str_sz - __pos);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002037 _VSTD::__debug_db_insert_c(this);
Marshall Clow83445802016-04-07 18:13:41 +00002038}
2039
2040template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002041template <class _Tp, class>
Marshall Clow78dbe462016-11-14 18:22:19 +00002042basic_string<_CharT, _Traits, _Allocator>::basic_string(
Marshall Clowe46031a2018-07-02 18:41:15 +00002043 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002044 : __r_(__default_init_tag(), __a)
Marshall Clow78dbe462016-11-14 18:22:19 +00002045{
Marshall Clowe46031a2018-07-02 18:41:15 +00002046 __self_view __sv0 = __t;
2047 __self_view __sv = __sv0.substr(__pos, __n);
Marshall Clow78dbe462016-11-14 18:22:19 +00002048 __init(__sv.data(), __sv.size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002049 _VSTD::__debug_db_insert_c(this);
Marshall Clow78dbe462016-11-14 18:22:19 +00002050}
2051
2052template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002053template <class _Tp, class>
2054basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002055 : __r_(__default_init_tag(), __default_init_tag())
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002056{
Marshall Clowe46031a2018-07-02 18:41:15 +00002057 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002058 __init(__sv.data(), __sv.size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002059 _VSTD::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002060}
2061
2062template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002063template <class _Tp, class>
2064basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002065 : __r_(__default_init_tag(), __a)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002066{
Marshall Clowe46031a2018-07-02 18:41:15 +00002067 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002068 __init(__sv.data(), __sv.size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002069 _VSTD::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002070}
2071
2072template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002073template <class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002074__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002075<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002076 __is_exactly_cpp17_input_iterator<_InputIterator>::value
2077>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002078basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
2079{
2080 __zero();
2081#ifndef _LIBCPP_NO_EXCEPTIONS
2082 try
2083 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002084#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002085 for (; __first != __last; ++__first)
2086 push_back(*__first);
2087#ifndef _LIBCPP_NO_EXCEPTIONS
2088 }
2089 catch (...)
2090 {
2091 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002092 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002093 throw;
2094 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002095#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002096}
2097
2098template <class _CharT, class _Traits, class _Allocator>
2099template <class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002100__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002101<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002102 __is_cpp17_forward_iterator<_ForwardIterator>::value
2103>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002104basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
2105{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002106 size_type __sz = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002107 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002108 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002109 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002110 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002111 {
2112 __set_short_size(__sz);
2113 __p = __get_short_pointer();
2114 }
2115 else
2116 {
2117 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002118 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002119 __set_long_pointer(__p);
2120 __set_long_cap(__cap+1);
2121 __set_long_size(__sz);
2122 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002123
2124#ifndef _LIBCPP_NO_EXCEPTIONS
2125 try
2126 {
2127#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002128 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002129 traits_type::assign(*__p, *__first);
2130 traits_type::assign(*__p, value_type());
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002131#ifndef _LIBCPP_NO_EXCEPTIONS
2132 }
2133 catch (...)
2134 {
2135 if (__is_long())
2136 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
2137 throw;
2138 }
2139#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002140}
2141
2142template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002143template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002144inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002145basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002146 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002147{
2148 __init(__first, __last);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002149 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002150}
2151
2152template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002153template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002154inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002155basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
2156 const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002157 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002158{
2159 __init(__first, __last);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002160 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002161}
2162
Eric Fiselierfc92be82017-04-19 00:28:44 +00002163#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002164
Howard Hinnantc51e1022010-05-11 19:42:16 +00002165template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002166inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002167basic_string<_CharT, _Traits, _Allocator>::basic_string(
2168 initializer_list<_CharT> __il)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002169 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002170{
2171 __init(__il.begin(), __il.end());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002172 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002173}
2174
2175template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002176inline
Eric Fiselier9d355982017-04-12 23:45:53 +00002177
Eric Fiselier812882b2017-02-17 01:17:10 +00002178basic_string<_CharT, _Traits, _Allocator>::basic_string(
2179 initializer_list<_CharT> __il, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002180 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002181{
2182 __init(__il.begin(), __il.end());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002183 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002184}
2185
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002186#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002187
Howard Hinnantc51e1022010-05-11 19:42:16 +00002188template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002189basic_string<_CharT, _Traits, _Allocator>::~basic_string()
2190{
Louis Dionneba400782020-10-02 15:02:52 -04002191#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002192 if (!__libcpp_is_constant_evaluated())
2193 __get_db()->__erase_c(this);
Howard Hinnant8ea98242013-08-23 17:37:05 +00002194#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002195 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002196 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002197}
2198
2199template <class _CharT, class _Traits, class _Allocator>
2200void
2201basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
2202 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002203 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 +00002204{
2205 size_type __ms = max_size();
2206 if (__delta_cap > __ms - __old_cap - 1)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002207 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002208 pointer __old_p = __get_pointer();
2209 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002210 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002211 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002212 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002213 __invalidate_all_iterators();
2214 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002215 traits_type::copy(_VSTD::__to_address(__p),
2216 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002217 if (__n_add != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002218 traits_type::copy(_VSTD::__to_address(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002219 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2220 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002221 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2222 _VSTD::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002223 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002224 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002225 __set_long_pointer(__p);
2226 __set_long_cap(__cap+1);
2227 __old_sz = __n_copy + __n_add + __sec_cp_sz;
2228 __set_long_size(__old_sz);
2229 traits_type::assign(__p[__old_sz], value_type());
2230}
2231
2232template <class _CharT, class _Traits, class _Allocator>
2233void
2234basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2235 size_type __n_copy, size_type __n_del, size_type __n_add)
2236{
2237 size_type __ms = max_size();
Marshall Clow2267c5d2013-11-06 14:24:38 +00002238 if (__delta_cap > __ms - __old_cap)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002239 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002240 pointer __old_p = __get_pointer();
2241 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002242 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002243 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002244 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002245 __invalidate_all_iterators();
2246 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002247 traits_type::copy(_VSTD::__to_address(__p),
2248 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002249 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2250 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002251 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2252 _VSTD::__to_address(__old_p) + __n_copy + __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002253 __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002254 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002255 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002256 __set_long_pointer(__p);
2257 __set_long_cap(__cap+1);
2258}
2259
2260// assign
2261
2262template <class _CharT, class _Traits, class _Allocator>
Martijn Vels596e3de2020-02-26 15:55:49 -05002263template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04002264basic_string<_CharT, _Traits, _Allocator>&
2265basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(
Martijn Vels596e3de2020-02-26 15:55:49 -05002266 const value_type* __s, size_type __n) {
2267 size_type __cap = __is_short ? __min_cap : __get_long_cap();
2268 if (__n < __cap) {
2269 pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
2270 __is_short ? __set_short_size(__n) : __set_long_size(__n);
2271 traits_type::copy(_VSTD::__to_address(__p), __s, __n);
2272 traits_type::assign(__p[__n], value_type());
2273 __invalidate_iterators_past(__n);
2274 } else {
2275 size_type __sz = __is_short ? __get_short_size() : __get_long_size();
2276 __grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
2277 }
Martijn Velsb6a08b62020-04-10 18:36:31 -04002278 return *this;
Martijn Vels596e3de2020-02-26 15:55:49 -05002279}
2280
2281template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002282basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002283basic_string<_CharT, _Traits, _Allocator>::__assign_external(
2284 const value_type* __s, size_type __n) {
2285 size_type __cap = capacity();
2286 if (__cap >= __n) {
2287 value_type* __p = _VSTD::__to_address(__get_pointer());
2288 traits_type::move(__p, __s, __n);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002289 return __null_terminate_at(__p, __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04002290 } else {
2291 size_type __sz = size();
2292 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002293 return *this;
Martijn Velsda7d94f2020-06-19 14:24:03 -04002294 }
Martijn Velsda7d94f2020-06-19 14:24:03 -04002295}
2296
2297template <class _CharT, class _Traits, class _Allocator>
2298basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002299basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002300{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002301 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Nikolas Klauser93826d12022-01-04 17:24:03 +01002302 return (__builtin_constant_p(__n) && __fits_in_sso(__n))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002303 ? __assign_short(__s, __n)
2304 : __assign_external(__s, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002305}
2306
2307template <class _CharT, class _Traits, class _Allocator>
2308basic_string<_CharT, _Traits, _Allocator>&
2309basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2310{
2311 size_type __cap = capacity();
2312 if (__cap < __n)
2313 {
2314 size_type __sz = size();
2315 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2316 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002317 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002318 traits_type::assign(__p, __n, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002319 return __null_terminate_at(__p, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002320}
2321
2322template <class _CharT, class _Traits, class _Allocator>
2323basic_string<_CharT, _Traits, _Allocator>&
2324basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2325{
2326 pointer __p;
2327 if (__is_long())
2328 {
2329 __p = __get_long_pointer();
2330 __set_long_size(1);
2331 }
2332 else
2333 {
2334 __p = __get_short_pointer();
2335 __set_short_size(1);
2336 }
2337 traits_type::assign(*__p, __c);
2338 traits_type::assign(*++__p, value_type());
2339 __invalidate_iterators_past(1);
2340 return *this;
2341}
2342
2343template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002344basic_string<_CharT, _Traits, _Allocator>&
2345basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2346{
Martijn Vels596e3de2020-02-26 15:55:49 -05002347 if (this != &__str) {
2348 __copy_assign_alloc(__str);
2349 if (!__is_long()) {
2350 if (!__str.__is_long()) {
Eric Fiselier03bbc922020-01-15 17:27:10 -05002351 __r_.first().__r = __str.__r_.first().__r;
Martijn Vels596e3de2020-02-26 15:55:49 -05002352 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002353 return __assign_no_alias<true>(__str.data(), __str.size());
Martijn Vels596e3de2020-02-26 15:55:49 -05002354 }
2355 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002356 return __assign_no_alias<false>(__str.data(), __str.size());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002357 }
Martijn Vels596e3de2020-02-26 15:55:49 -05002358 }
2359 return *this;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002360}
2361
Eric Fiselierfc92be82017-04-19 00:28:44 +00002362#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002363
2364template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002365inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002366void
2367basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002368 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002369{
2370 if (__alloc() != __str.__alloc())
2371 assign(__str);
2372 else
2373 __move_assign(__str, true_type());
2374}
2375
2376template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002377inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002378void
2379basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002380#if _LIBCPP_STD_VER > 14
2381 _NOEXCEPT
2382#else
Howard Hinnant3e276872011-06-03 18:40:47 +00002383 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002384#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002385{
marshall2ed41622019-11-27 07:13:00 -08002386 if (__is_long()) {
2387 __alloc_traits::deallocate(__alloc(), __get_long_pointer(),
2388 __get_long_cap());
2389#if _LIBCPP_STD_VER <= 14
2390 if (!is_nothrow_move_assignable<allocator_type>::value) {
2391 __set_short_size(0);
2392 traits_type::assign(__get_short_pointer()[0], value_type());
2393 }
2394#endif
2395 }
2396 __move_assign_alloc(__str);
2397 __r_.first() = __str.__r_.first();
2398 __str.__set_short_size(0);
2399 traits_type::assign(__str.__get_short_pointer()[0], value_type());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002400}
2401
2402template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002403inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002404basic_string<_CharT, _Traits, _Allocator>&
2405basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002406 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002407{
2408 __move_assign(__str, integral_constant<bool,
2409 __alloc_traits::propagate_on_container_move_assignment::value>());
2410 return *this;
2411}
2412
2413#endif
2414
2415template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002416template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002417__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002418<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002419 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002420 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002421>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002422basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2423{
Marshall Clow958362f2016-09-05 01:54:30 +00002424 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002425 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002426 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002427}
2428
2429template <class _CharT, class _Traits, class _Allocator>
2430template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002431__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002432<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002433 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002434 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002435>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002436basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2437{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002438 size_type __cap = capacity();
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002439 size_type __n = __string_is_trivial_iterator<_ForwardIterator>::value ?
2440 static_cast<size_type>(_VSTD::distance(__first, __last)) : 0;
2441
2442 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2443 (__cap >= __n || !__addr_in_range(*__first)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002444 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002445 if (__cap < __n)
2446 {
2447 size_type __sz = size();
2448 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2449 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002450 pointer __p = __get_pointer();
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002451 for (; __first != __last; ++__p, (void) ++__first)
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002452 traits_type::assign(*__p, *__first);
2453 traits_type::assign(*__p, value_type());
2454 __set_size(__n);
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04002455 __invalidate_iterators_past(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002456 }
2457 else
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002458 {
2459 const basic_string __temp(__first, __last, __alloc());
2460 assign(__temp.data(), __temp.size());
2461 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002462 return *this;
2463}
2464
2465template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002466basic_string<_CharT, _Traits, _Allocator>&
2467basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2468{
2469 size_type __sz = __str.size();
2470 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002471 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002472 return assign(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002473}
2474
2475template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002476template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002477__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002478<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002479 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
2480 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00002481 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002482>
Marshall Clow82513342016-09-24 22:45:42 +00002483basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002484{
Marshall Clow82513342016-09-24 22:45:42 +00002485 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002486 size_type __sz = __sv.size();
2487 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002488 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002489 return assign(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2490}
2491
2492
2493template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002494basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002495basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s) {
2496 return __assign_external(__s, traits_type::length(__s));
2497}
2498
2499template <class _CharT, class _Traits, class _Allocator>
2500basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002501basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002502{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002503 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Louis Dionne3e0c4332021-08-31 10:49:06 -04002504 return __builtin_constant_p(*__s)
Nikolas Klauser93826d12022-01-04 17:24:03 +01002505 ? (__fits_in_sso(traits_type::length(__s))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002506 ? __assign_short(__s, traits_type::length(__s))
2507 : __assign_external(__s, traits_type::length(__s)))
2508 : __assign_external(__s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002509}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002510// append
2511
2512template <class _CharT, class _Traits, class _Allocator>
2513basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002514basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002515{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002516 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002517 size_type __cap = capacity();
2518 size_type __sz = size();
2519 if (__cap - __sz >= __n)
2520 {
2521 if (__n)
2522 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002523 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002524 traits_type::copy(__p + __sz, __s, __n);
2525 __sz += __n;
2526 __set_size(__sz);
2527 traits_type::assign(__p[__sz], value_type());
2528 }
2529 }
2530 else
2531 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2532 return *this;
2533}
2534
2535template <class _CharT, class _Traits, class _Allocator>
2536basic_string<_CharT, _Traits, _Allocator>&
2537basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2538{
2539 if (__n)
2540 {
2541 size_type __cap = capacity();
2542 size_type __sz = size();
2543 if (__cap - __sz < __n)
2544 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2545 pointer __p = __get_pointer();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002546 traits_type::assign(_VSTD::__to_address(__p) + __sz, __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002547 __sz += __n;
2548 __set_size(__sz);
2549 traits_type::assign(__p[__sz], value_type());
2550 }
2551 return *this;
2552}
2553
2554template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00002555inline void
2556basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n)
2557{
2558 if (__n)
2559 {
2560 size_type __cap = capacity();
2561 size_type __sz = size();
2562 if (__cap - __sz < __n)
2563 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2564 pointer __p = __get_pointer();
2565 __sz += __n;
2566 __set_size(__sz);
2567 traits_type::assign(__p[__sz], value_type());
2568 }
2569}
2570
2571template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002572void
2573basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2574{
Howard Hinnant68bf1812013-04-30 21:44:48 +00002575 bool __is_short = !__is_long();
2576 size_type __cap;
2577 size_type __sz;
2578 if (__is_short)
2579 {
2580 __cap = __min_cap - 1;
2581 __sz = __get_short_size();
2582 }
2583 else
2584 {
2585 __cap = __get_long_cap() - 1;
2586 __sz = __get_long_size();
2587 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002588 if (__sz == __cap)
Howard Hinnant68bf1812013-04-30 21:44:48 +00002589 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002590 __grow_by(__cap, 1, __sz, __sz, 0);
Louis Dionne82d2b2c2021-11-16 11:26:56 -05002591 __is_short = false; // the string is always long after __grow_by
Howard Hinnant68bf1812013-04-30 21:44:48 +00002592 }
2593 pointer __p;
2594 if (__is_short)
2595 {
2596 __p = __get_short_pointer() + __sz;
2597 __set_short_size(__sz+1);
2598 }
2599 else
2600 {
2601 __p = __get_long_pointer() + __sz;
2602 __set_long_size(__sz+1);
2603 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002604 traits_type::assign(*__p, __c);
2605 traits_type::assign(*++__p, value_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002606}
2607
Howard Hinnantc51e1022010-05-11 19:42:16 +00002608template <class _CharT, class _Traits, class _Allocator>
2609template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002610__enable_if_t
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002611<
2612 __is_cpp17_forward_iterator<_ForwardIterator>::value,
2613 basic_string<_CharT, _Traits, _Allocator>&
2614>
2615basic_string<_CharT, _Traits, _Allocator>::append(
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002616 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002617{
2618 size_type __sz = size();
2619 size_type __cap = capacity();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002620 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002621 if (__n)
2622 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002623 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2624 !__addr_in_range(*__first))
Marshall Clow958362f2016-09-05 01:54:30 +00002625 {
2626 if (__cap - __sz < __n)
2627 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2628 pointer __p = __get_pointer() + __sz;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002629 for (; __first != __last; ++__p, (void) ++__first)
Marshall Clow958362f2016-09-05 01:54:30 +00002630 traits_type::assign(*__p, *__first);
2631 traits_type::assign(*__p, value_type());
2632 __set_size(__sz + __n);
2633 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002634 else
2635 {
2636 const basic_string __temp(__first, __last, __alloc());
2637 append(__temp.data(), __temp.size());
2638 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002639 }
2640 return *this;
2641}
2642
2643template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002644inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002645basic_string<_CharT, _Traits, _Allocator>&
2646basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2647{
2648 return append(__str.data(), __str.size());
2649}
2650
2651template <class _CharT, class _Traits, class _Allocator>
2652basic_string<_CharT, _Traits, _Allocator>&
2653basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2654{
2655 size_type __sz = __str.size();
2656 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002657 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002658 return append(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002659}
2660
2661template <class _CharT, class _Traits, class _Allocator>
Marshall Clow62953962016-10-03 23:40:48 +00002662template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002663 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002664 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002665 __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 +00002666 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002667 >
Marshall Clow82513342016-09-24 22:45:42 +00002668basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002669{
Marshall Clow82513342016-09-24 22:45:42 +00002670 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002671 size_type __sz = __sv.size();
2672 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002673 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002674 return append(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2675}
2676
2677template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002678basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002679basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002680{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002681 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002682 return append(__s, traits_type::length(__s));
2683}
2684
2685// insert
2686
2687template <class _CharT, class _Traits, class _Allocator>
2688basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002689basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002690{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002691 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002692 size_type __sz = size();
2693 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002694 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002695 size_type __cap = capacity();
2696 if (__cap - __sz >= __n)
2697 {
2698 if (__n)
2699 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002700 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002701 size_type __n_move = __sz - __pos;
2702 if (__n_move != 0)
2703 {
2704 if (__p + __pos <= __s && __s < __p + __sz)
2705 __s += __n;
2706 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2707 }
2708 traits_type::move(__p + __pos, __s, __n);
2709 __sz += __n;
2710 __set_size(__sz);
2711 traits_type::assign(__p[__sz], value_type());
2712 }
2713 }
2714 else
2715 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2716 return *this;
2717}
2718
2719template <class _CharT, class _Traits, class _Allocator>
2720basic_string<_CharT, _Traits, _Allocator>&
2721basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2722{
2723 size_type __sz = size();
2724 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002725 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002726 if (__n)
2727 {
2728 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002729 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002730 if (__cap - __sz >= __n)
2731 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002732 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002733 size_type __n_move = __sz - __pos;
2734 if (__n_move != 0)
2735 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2736 }
2737 else
2738 {
2739 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002740 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002741 }
2742 traits_type::assign(__p + __pos, __n, __c);
2743 __sz += __n;
2744 __set_size(__sz);
2745 traits_type::assign(__p[__sz], value_type());
2746 }
2747 return *this;
2748}
2749
2750template <class _CharT, class _Traits, class _Allocator>
2751template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002752__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002753<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002754 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Marshall Clow039b2f02016-01-13 21:54:34 +00002755 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002756>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002757basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2758{
Nikolas Klausereed25832021-12-15 01:32:30 +01002759 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2760 "string::insert(iterator, range) called with an iterator not"
2761 " referring to this string");
2762 const basic_string __temp(__first, __last, __alloc());
2763 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002764}
2765
2766template <class _CharT, class _Traits, class _Allocator>
2767template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002768__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002769<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002770 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002771 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002772>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002773basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2774{
Nikolas Klausereed25832021-12-15 01:32:30 +01002775 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2776 "string::insert(iterator, range) called with an iterator not"
2777 " referring to this string");
2778
Howard Hinnantc51e1022010-05-11 19:42:16 +00002779 size_type __ip = static_cast<size_type>(__pos - begin());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002780 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002781 if (__n)
2782 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002783 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2784 !__addr_in_range(*__first))
2785 {
2786 size_type __sz = size();
2787 size_type __cap = capacity();
2788 value_type* __p;
2789 if (__cap - __sz >= __n)
2790 {
2791 __p = _VSTD::__to_address(__get_pointer());
2792 size_type __n_move = __sz - __ip;
2793 if (__n_move != 0)
2794 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
2795 }
2796 else
2797 {
2798 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
2799 __p = _VSTD::__to_address(__get_long_pointer());
2800 }
2801 __sz += __n;
2802 __set_size(__sz);
2803 traits_type::assign(__p[__sz], value_type());
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002804 for (__p += __ip; __first != __last; ++__p, (void) ++__first)
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002805 traits_type::assign(*__p, *__first);
2806 }
2807 else
Marshall Clow958362f2016-09-05 01:54:30 +00002808 {
2809 const basic_string __temp(__first, __last, __alloc());
2810 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
2811 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002812 }
2813 return begin() + __ip;
2814}
2815
2816template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002817inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002818basic_string<_CharT, _Traits, _Allocator>&
2819basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2820{
2821 return insert(__pos1, __str.data(), __str.size());
2822}
2823
2824template <class _CharT, class _Traits, class _Allocator>
2825basic_string<_CharT, _Traits, _Allocator>&
2826basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2827 size_type __pos2, size_type __n)
2828{
2829 size_type __str_sz = __str.size();
2830 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002831 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002832 return insert(__pos1, __str.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002833}
2834
2835template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002836template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002837__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002838<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002839 __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 +00002840 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002841>
Marshall Clow82513342016-09-24 22:45:42 +00002842basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002843 size_type __pos2, size_type __n)
2844{
Marshall Clow82513342016-09-24 22:45:42 +00002845 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002846 size_type __str_sz = __sv.size();
2847 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002848 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002849 return insert(__pos1, __sv.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
2850}
2851
2852template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002853basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002854basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002855{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002856 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002857 return insert(__pos, __s, traits_type::length(__s));
2858}
2859
2860template <class _CharT, class _Traits, class _Allocator>
2861typename basic_string<_CharT, _Traits, _Allocator>::iterator
2862basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2863{
2864 size_type __ip = static_cast<size_type>(__pos - begin());
2865 size_type __sz = size();
2866 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002867 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002868 if (__cap == __sz)
2869 {
2870 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002871 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002872 }
2873 else
2874 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002875 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002876 size_type __n_move = __sz - __ip;
2877 if (__n_move != 0)
2878 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2879 }
2880 traits_type::assign(__p[__ip], __c);
2881 traits_type::assign(__p[++__sz], value_type());
2882 __set_size(__sz);
2883 return begin() + static_cast<difference_type>(__ip);
2884}
2885
2886template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002887inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002888typename basic_string<_CharT, _Traits, _Allocator>::iterator
2889basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2890{
Nikolas Klausereed25832021-12-15 01:32:30 +01002891 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2892 "string::insert(iterator, n, value) called with an iterator not"
2893 " referring to this string");
2894 difference_type __p = __pos - begin();
2895 insert(static_cast<size_type>(__p), __n, __c);
2896 return begin() + __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002897}
2898
2899// replace
2900
2901template <class _CharT, class _Traits, class _Allocator>
2902basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002903basic_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 +00002904 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00002905{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002906 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002907 size_type __sz = size();
2908 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002909 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002910 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002911 size_type __cap = capacity();
2912 if (__cap - __sz + __n1 >= __n2)
2913 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002914 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002915 if (__n1 != __n2)
2916 {
2917 size_type __n_move = __sz - __pos - __n1;
2918 if (__n_move != 0)
2919 {
2920 if (__n1 > __n2)
2921 {
2922 traits_type::move(__p + __pos, __s, __n2);
2923 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002924 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002925 }
2926 if (__p + __pos < __s && __s < __p + __sz)
2927 {
2928 if (__p + __pos + __n1 <= __s)
2929 __s += __n2 - __n1;
2930 else // __p + __pos < __s < __p + __pos + __n1
2931 {
2932 traits_type::move(__p + __pos, __s, __n1);
2933 __pos += __n1;
2934 __s += __n2;
2935 __n2 -= __n1;
2936 __n1 = 0;
2937 }
2938 }
2939 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2940 }
2941 }
2942 traits_type::move(__p + __pos, __s, __n2);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002943 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002944 }
2945 else
2946 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
2947 return *this;
2948}
2949
2950template <class _CharT, class _Traits, class _Allocator>
2951basic_string<_CharT, _Traits, _Allocator>&
2952basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
2953{
2954 size_type __sz = size();
2955 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002956 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002957 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002958 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002959 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002960 if (__cap - __sz + __n1 >= __n2)
2961 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002962 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002963 if (__n1 != __n2)
2964 {
2965 size_type __n_move = __sz - __pos - __n1;
2966 if (__n_move != 0)
2967 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2968 }
2969 }
2970 else
2971 {
2972 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002973 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002974 }
2975 traits_type::assign(__p + __pos, __n2, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002976 return __null_terminate_at(__p, __sz - (__n1 - __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002977}
2978
2979template <class _CharT, class _Traits, class _Allocator>
2980template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002981__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002982<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002983 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002984 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002985>
Howard Hinnant990d6e82010-11-17 21:11:40 +00002986basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002987 _InputIterator __j1, _InputIterator __j2)
2988{
Marshall Clow958362f2016-09-05 01:54:30 +00002989 const basic_string __temp(__j1, __j2, __alloc());
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002990 return replace(__i1, __i2, __temp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002991}
2992
2993template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002994inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002995basic_string<_CharT, _Traits, _Allocator>&
2996basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
2997{
2998 return replace(__pos1, __n1, __str.data(), __str.size());
2999}
3000
3001template <class _CharT, class _Traits, class _Allocator>
3002basic_string<_CharT, _Traits, _Allocator>&
3003basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
3004 size_type __pos2, size_type __n2)
3005{
3006 size_type __str_sz = __str.size();
3007 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003008 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003009 return replace(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003010}
3011
3012template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003013template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003014__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003015<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003016 __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 +00003017 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003018>
Marshall Clow82513342016-09-24 22:45:42 +00003019basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003020 size_type __pos2, size_type __n2)
3021{
Marshall Clow82513342016-09-24 22:45:42 +00003022 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003023 size_type __str_sz = __sv.size();
3024 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003025 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003026 return replace(__pos1, __n1, __sv.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
3027}
3028
3029template <class _CharT, class _Traits, class _Allocator>
3030basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003031basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003032{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003033 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003034 return replace(__pos, __n1, __s, traits_type::length(__s));
3035}
3036
3037template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003038inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003039basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003040basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003041{
3042 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
3043 __str.data(), __str.size());
3044}
3045
3046template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003047inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003048basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003049basic_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 +00003050{
3051 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
3052}
3053
3054template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003055inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003056basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003057basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003058{
3059 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
3060}
3061
3062template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003063inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003064basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003065basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003066{
3067 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
3068}
3069
3070// erase
3071
Martijn Velsa81fc792020-02-26 13:25:43 -05003072// 'externally instantiated' erase() implementation, called when __n != npos.
3073// Does not check __pos against size()
Howard Hinnantc51e1022010-05-11 19:42:16 +00003074template <class _CharT, class _Traits, class _Allocator>
Martijn Velsa81fc792020-02-26 13:25:43 -05003075void
3076basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(
3077 size_type __pos, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003078{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003079 if (__n)
3080 {
Martijn Velsa81fc792020-02-26 13:25:43 -05003081 size_type __sz = size();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003082 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003083 __n = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003084 size_type __n_move = __sz - __pos - __n;
3085 if (__n_move != 0)
3086 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003087 __null_terminate_at(__p, __sz - __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003088 }
Martijn Velsa81fc792020-02-26 13:25:43 -05003089}
3090
3091template <class _CharT, class _Traits, class _Allocator>
3092basic_string<_CharT, _Traits, _Allocator>&
3093basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos,
3094 size_type __n) {
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003095 if (__pos > size())
3096 __throw_out_of_range();
Martijn Velsa81fc792020-02-26 13:25:43 -05003097 if (__n == npos) {
3098 __erase_to_end(__pos);
3099 } else {
3100 __erase_external_with_move(__pos, __n);
3101 }
3102 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003103}
3104
3105template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003106inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003107typename basic_string<_CharT, _Traits, _Allocator>::iterator
3108basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
3109{
Nikolas Klausereed25832021-12-15 01:32:30 +01003110 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3111 "string::erase(iterator) called with an iterator not"
3112 " referring to this string");
3113
3114 _LIBCPP_ASSERT(__pos != end(), "string::erase(iterator) called with a non-dereferenceable iterator");
3115 iterator __b = begin();
3116 size_type __r = static_cast<size_type>(__pos - __b);
3117 erase(__r, 1);
3118 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003119}
3120
3121template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003122inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003123typename basic_string<_CharT, _Traits, _Allocator>::iterator
3124basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
3125{
Nikolas Klausereed25832021-12-15 01:32:30 +01003126 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
3127 "string::erase(iterator, iterator) called with an iterator not"
3128 " referring to this string");
3129
3130 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
3131 iterator __b = begin();
3132 size_type __r = static_cast<size_type>(__first - __b);
3133 erase(__r, static_cast<size_type>(__last - __first));
3134 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003135}
3136
3137template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003138inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003139void
3140basic_string<_CharT, _Traits, _Allocator>::pop_back()
3141{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003142 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003143 __erase_to_end(size() - 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003144}
3145
3146template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003147inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003148void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003149basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003150{
3151 __invalidate_all_iterators();
3152 if (__is_long())
3153 {
3154 traits_type::assign(*__get_long_pointer(), value_type());
3155 __set_long_size(0);
3156 }
3157 else
3158 {
3159 traits_type::assign(*__get_short_pointer(), value_type());
3160 __set_short_size(0);
3161 }
3162}
3163
3164template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003165inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003166void
3167basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
3168{
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003169 __null_terminate_at(_VSTD::__to_address(__get_pointer()), __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003170}
3171
3172template <class _CharT, class _Traits, class _Allocator>
3173void
3174basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
3175{
3176 size_type __sz = size();
3177 if (__n > __sz)
3178 append(__n - __sz, __c);
3179 else
3180 __erase_to_end(__n);
3181}
3182
3183template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00003184inline void
3185basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n)
3186{
3187 size_type __sz = size();
3188 if (__n > __sz) {
3189 __append_default_init(__n - __sz);
3190 } else
3191 __erase_to_end(__n);
3192}
3193
3194template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003195inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003196typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003197basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003198{
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003199 size_type __m = __alloc_traits::max_size(__alloc());
Eric Fiseliere9cc5922017-10-17 13:16:01 +00003200#ifdef _LIBCPP_BIG_ENDIAN
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003201 return (__m <= ~__long_mask ? __m : __m/2) - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003202#else
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003203 return __m - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003204#endif
3205}
3206
3207template <class _CharT, class _Traits, class _Allocator>
3208void
Marek Kurdejc9848142020-11-26 10:07:16 +01003209basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __requested_capacity)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003210{
Marek Kurdejc9848142020-11-26 10:07:16 +01003211 if (__requested_capacity > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003212 __throw_length_error();
Marek Kurdejc9848142020-11-26 10:07:16 +01003213
Louis Dionne05b6d3e2022-01-14 12:30:22 -05003214 // Make sure reserve(n) never shrinks. This is technically only required in C++20
3215 // and later (since P0966R1), however we provide consistent behavior in all Standard
3216 // modes because this function is instantiated in the shared library.
3217 if (__requested_capacity <= capacity())
3218 return;
Marek Kurdejc9848142020-11-26 10:07:16 +01003219
3220 size_type __target_capacity = _VSTD::max(__requested_capacity, size());
3221 __target_capacity = __recommend(__target_capacity);
3222 if (__target_capacity == capacity()) return;
3223
3224 __shrink_or_extend(__target_capacity);
3225}
3226
3227template <class _CharT, class _Traits, class _Allocator>
Louis Dionnee38b8642021-12-13 14:15:21 -05003228inline
Marek Kurdejc9848142020-11-26 10:07:16 +01003229void
3230basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT
3231{
3232 size_type __target_capacity = __recommend(size());
3233 if (__target_capacity == capacity()) return;
3234
3235 __shrink_or_extend(__target_capacity);
3236}
3237
3238template <class _CharT, class _Traits, class _Allocator>
Louis Dionnee38b8642021-12-13 14:15:21 -05003239inline
Marek Kurdejc9848142020-11-26 10:07:16 +01003240void
3241basic_string<_CharT, _Traits, _Allocator>::__shrink_or_extend(size_type __target_capacity)
3242{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003243 size_type __cap = capacity();
3244 size_type __sz = size();
Marek Kurdejc9848142020-11-26 10:07:16 +01003245
3246 pointer __new_data, __p;
3247 bool __was_long, __now_long;
3248 if (__target_capacity == __min_cap - 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003249 {
Marek Kurdejc9848142020-11-26 10:07:16 +01003250 __was_long = true;
3251 __now_long = false;
3252 __new_data = __get_short_pointer();
3253 __p = __get_long_pointer();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003254 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003255 else
3256 {
3257 if (__target_capacity > __cap)
3258 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3259 else
3260 {
3261 #ifndef _LIBCPP_NO_EXCEPTIONS
3262 try
3263 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003264 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003265 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3266 #ifndef _LIBCPP_NO_EXCEPTIONS
3267 }
3268 catch (...)
3269 {
3270 return;
3271 }
3272 #else // _LIBCPP_NO_EXCEPTIONS
3273 if (__new_data == nullptr)
3274 return;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003275 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003276 }
3277 __now_long = true;
3278 __was_long = __is_long();
3279 __p = __get_pointer();
3280 }
3281 traits_type::copy(_VSTD::__to_address(__new_data),
3282 _VSTD::__to_address(__p), size()+1);
3283 if (__was_long)
3284 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
3285 if (__now_long)
3286 {
3287 __set_long_cap(__target_capacity+1);
3288 __set_long_size(__sz);
3289 __set_long_pointer(__new_data);
3290 }
3291 else
3292 __set_short_size(__sz);
3293 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003294}
3295
3296template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003297inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003298typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003299basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003300{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003301 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003302 return *(data() + __pos);
3303}
3304
3305template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003306inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003307typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003308basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003309{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003310 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003311 return *(__get_pointer() + __pos);
3312}
3313
3314template <class _CharT, class _Traits, class _Allocator>
3315typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3316basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3317{
3318 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003319 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003320 return (*this)[__n];
3321}
3322
3323template <class _CharT, class _Traits, class _Allocator>
3324typename basic_string<_CharT, _Traits, _Allocator>::reference
3325basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
3326{
3327 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003328 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003329 return (*this)[__n];
3330}
3331
3332template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003333inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003334typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003335basic_string<_CharT, _Traits, _Allocator>::front() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003336{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003337 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003338 return *__get_pointer();
3339}
3340
3341template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003342inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003343typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003344basic_string<_CharT, _Traits, _Allocator>::front() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003345{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003346 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003347 return *data();
3348}
3349
3350template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003351inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003352typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003353basic_string<_CharT, _Traits, _Allocator>::back() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003354{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003355 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003356 return *(__get_pointer() + size() - 1);
3357}
3358
3359template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003360inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003361typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003362basic_string<_CharT, _Traits, _Allocator>::back() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003363{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003364 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003365 return *(data() + size() - 1);
3366}
3367
3368template <class _CharT, class _Traits, class _Allocator>
3369typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003370basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003371{
3372 size_type __sz = size();
3373 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003374 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003375 size_type __rlen = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003376 traits_type::copy(__s, data() + __pos, __rlen);
3377 return __rlen;
3378}
3379
3380template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003381inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003382basic_string<_CharT, _Traits, _Allocator>
3383basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3384{
3385 return basic_string(*this, __pos, __n, __alloc());
3386}
3387
3388template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003389inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003390void
3391basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003392#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00003393 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00003394#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00003395 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003396 __is_nothrow_swappable<allocator_type>::value)
3397#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003398{
Louis Dionneba400782020-10-02 15:02:52 -04003399#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01003400 if (!__libcpp_is_constant_evaluated()) {
3401 if (!__is_long())
3402 __get_db()->__invalidate_all(this);
3403 if (!__str.__is_long())
3404 __get_db()->__invalidate_all(&__str);
3405 __get_db()->swap(this, &__str);
3406 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00003407#endif
Eric Fiselier9bf691f2016-12-28 05:53:01 +00003408 _LIBCPP_ASSERT(
3409 __alloc_traits::propagate_on_container_swap::value ||
3410 __alloc_traits::is_always_equal::value ||
3411 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003412 _VSTD::swap(__r_.first(), __str.__r_.first());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05003413 _VSTD::__swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003414}
3415
3416// find
3417
3418template <class _Traits>
3419struct _LIBCPP_HIDDEN __traits_eq
3420{
3421 typedef typename _Traits::char_type char_type;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003422 _LIBCPP_INLINE_VISIBILITY
3423 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3424 {return _Traits::eq(__x, __y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003425};
3426
3427template<class _CharT, class _Traits, class _Allocator>
3428typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003429basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003430 size_type __pos,
3431 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003432{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003433 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003434 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003435 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003436}
3437
3438template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003439inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003440typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003441basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3442 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003443{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003444 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003445 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003446}
3447
3448template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003449template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003450__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003451<
3452 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3453 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003454>
Marshall Clowe46031a2018-07-02 18:41:15 +00003455basic_string<_CharT, _Traits, _Allocator>::find(const _Tp &__t,
zoecarver1997e0a2021-02-05 11:54:47 -08003456 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003457{
Marshall Clowe46031a2018-07-02 18:41:15 +00003458 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003459 return __str_find<value_type, size_type, traits_type, npos>
3460 (data(), size(), __sv.data(), __pos, __sv.size());
3461}
3462
3463template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003464inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003465typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003466basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003467 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003468{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003469 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003470 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003471 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003472}
3473
3474template<class _CharT, class _Traits, class _Allocator>
3475typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003476basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3477 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003478{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003479 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003480 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003481}
3482
3483// rfind
3484
3485template<class _CharT, class _Traits, class _Allocator>
3486typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003487basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003488 size_type __pos,
3489 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003490{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003491 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003492 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003493 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003494}
3495
3496template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003497inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003498typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003499basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3500 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003501{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003502 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003503 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003504}
3505
3506template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003507template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003508__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003509<
3510 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3511 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003512>
Marshall Clowe46031a2018-07-02 18:41:15 +00003513basic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003514 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003515{
Marshall Clowe46031a2018-07-02 18:41:15 +00003516 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003517 return __str_rfind<value_type, size_type, traits_type, npos>
3518 (data(), size(), __sv.data(), __pos, __sv.size());
3519}
3520
3521template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003522inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003523typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003524basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003525 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003526{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003527 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003528 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003529 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003530}
3531
3532template<class _CharT, class _Traits, class _Allocator>
3533typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003534basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3535 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003536{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003537 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003538 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003539}
3540
3541// find_first_of
3542
3543template<class _CharT, class _Traits, class _Allocator>
3544typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003545basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003546 size_type __pos,
3547 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003548{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003549 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003550 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003551 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003552}
3553
3554template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003555inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003556typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003557basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3558 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003559{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003560 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003561 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003562}
3563
3564template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003565template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003566__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003567<
3568 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3569 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003570>
Marshall Clowe46031a2018-07-02 18:41:15 +00003571basic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003572 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003573{
Marshall Clowe46031a2018-07-02 18:41:15 +00003574 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003575 return __str_find_first_of<value_type, size_type, traits_type, npos>
3576 (data(), size(), __sv.data(), __pos, __sv.size());
3577}
3578
3579template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003580inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003581typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003582basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003583 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003584{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003585 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003586 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003587 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003588}
3589
3590template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003591inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003592typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003593basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3594 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003595{
3596 return find(__c, __pos);
3597}
3598
3599// find_last_of
3600
3601template<class _CharT, class _Traits, class _Allocator>
3602typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003603basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003604 size_type __pos,
3605 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003606{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003607 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003608 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003609 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003610}
3611
3612template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003613inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003614typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003615basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3616 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003617{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003618 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003619 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003620}
3621
3622template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003623template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003624__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003625<
3626 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3627 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003628>
Marshall Clowe46031a2018-07-02 18:41:15 +00003629basic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003630 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003631{
Marshall Clowe46031a2018-07-02 18:41:15 +00003632 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003633 return __str_find_last_of<value_type, size_type, traits_type, npos>
3634 (data(), size(), __sv.data(), __pos, __sv.size());
3635}
3636
3637template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003638inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003639typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003640basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003641 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003642{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003643 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003644 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003645 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003646}
3647
3648template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003649inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003650typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003651basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3652 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003653{
3654 return rfind(__c, __pos);
3655}
3656
3657// find_first_not_of
3658
3659template<class _CharT, class _Traits, class _Allocator>
3660typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003661basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003662 size_type __pos,
3663 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003664{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003665 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003666 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003667 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003668}
3669
3670template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003671inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003672typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003673basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3674 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003675{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003676 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003677 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003678}
3679
3680template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003681template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003682__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003683<
3684 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3685 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003686>
Marshall Clowe46031a2018-07-02 18:41:15 +00003687basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003688 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003689{
Marshall Clowe46031a2018-07-02 18:41:15 +00003690 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003691 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3692 (data(), size(), __sv.data(), __pos, __sv.size());
3693}
3694
3695template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003696inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003697typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003698basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003699 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003700{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003701 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003702 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003703 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003704}
3705
3706template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003707inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003708typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003709basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3710 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003711{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003712 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003713 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003714}
3715
3716// find_last_not_of
3717
3718template<class _CharT, class _Traits, class _Allocator>
3719typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003720basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003721 size_type __pos,
3722 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003723{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003724 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003725 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003726 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003727}
3728
3729template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003730inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003731typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003732basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3733 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003734{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003735 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003736 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003737}
3738
3739template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003740template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003741__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003742<
3743 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3744 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003745>
Marshall Clowe46031a2018-07-02 18:41:15 +00003746basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003747 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003748{
Marshall Clowe46031a2018-07-02 18:41:15 +00003749 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003750 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3751 (data(), size(), __sv.data(), __pos, __sv.size());
3752}
3753
3754template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003755inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003756typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003757basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003758 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003759{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003760 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003761 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003762 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003763}
3764
3765template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003766inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003767typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003768basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3769 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003770{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003771 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003772 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003773}
3774
3775// compare
3776
3777template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003778template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003779__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003780<
3781 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3782 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003783>
zoecarver1997e0a2021-02-05 11:54:47 -08003784basic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003785{
Marshall Clowe46031a2018-07-02 18:41:15 +00003786 __self_view __sv = __t;
Howard Hinnantb0485532011-07-24 21:45:06 +00003787 size_t __lhs_sz = size();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003788 size_t __rhs_sz = __sv.size();
3789 int __result = traits_type::compare(data(), __sv.data(),
Howard Hinnantb0485532011-07-24 21:45:06 +00003790 _VSTD::min(__lhs_sz, __rhs_sz));
3791 if (__result != 0)
3792 return __result;
3793 if (__lhs_sz < __rhs_sz)
3794 return -1;
3795 if (__lhs_sz > __rhs_sz)
3796 return 1;
3797 return 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003798}
3799
3800template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003801inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003802int
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003803basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003804{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003805 return compare(__self_view(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003806}
3807
3808template <class _CharT, class _Traits, class _Allocator>
3809int
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003810basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3811 size_type __n1,
Howard Hinnantd17880b2013-06-28 16:59:19 +00003812 const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003813 size_type __n2) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003814{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003815 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003816 size_type __sz = size();
3817 if (__pos1 > __sz || __n2 == npos)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003818 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003819 size_type __rlen = _VSTD::min(__n1, __sz - __pos1);
3820 int __r = traits_type::compare(data() + __pos1, __s, _VSTD::min(__rlen, __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003821 if (__r == 0)
3822 {
3823 if (__rlen < __n2)
3824 __r = -1;
3825 else if (__rlen > __n2)
3826 __r = 1;
3827 }
3828 return __r;
3829}
3830
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003831template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003832template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003833__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003834<
3835 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3836 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003837>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003838basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3839 size_type __n1,
Marshall Clowe46031a2018-07-02 18:41:15 +00003840 const _Tp& __t) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003841{
Marshall Clowe46031a2018-07-02 18:41:15 +00003842 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003843 return compare(__pos1, __n1, __sv.data(), __sv.size());
3844}
3845
3846template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003847inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003848int
3849basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3850 size_type __n1,
3851 const basic_string& __str) const
3852{
3853 return compare(__pos1, __n1, __str.data(), __str.size());
3854}
3855
3856template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003857template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003858__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003859<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003860 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
3861 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00003862 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003863>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003864basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3865 size_type __n1,
Marshall Clow82513342016-09-24 22:45:42 +00003866 const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003867 size_type __pos2,
3868 size_type __n2) const
3869{
Marshall Clow82513342016-09-24 22:45:42 +00003870 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003871 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3872}
3873
3874template <class _CharT, class _Traits, class _Allocator>
3875int
3876basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3877 size_type __n1,
3878 const basic_string& __str,
3879 size_type __pos2,
3880 size_type __n2) const
3881{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003882 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003883}
3884
3885template <class _CharT, class _Traits, class _Allocator>
3886int
3887basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
3888{
3889 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3890 return compare(0, npos, __s, traits_type::length(__s));
3891}
3892
3893template <class _CharT, class _Traits, class _Allocator>
3894int
3895basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3896 size_type __n1,
3897 const value_type* __s) const
3898{
3899 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3900 return compare(__pos1, __n1, __s, traits_type::length(__s));
3901}
3902
Howard Hinnantc51e1022010-05-11 19:42:16 +00003903// __invariants
3904
3905template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003906inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003907bool
3908basic_string<_CharT, _Traits, _Allocator>::__invariants() const
3909{
3910 if (size() > capacity())
3911 return false;
3912 if (capacity() < __min_cap - 1)
3913 return false;
Bruce Mitchener170d8972020-11-24 12:53:53 -05003914 if (data() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003915 return false;
Louis Dionne663415f2020-10-05 16:16:13 -04003916 if (data()[size()] != value_type())
Howard Hinnantc51e1022010-05-11 19:42:16 +00003917 return false;
3918 return true;
3919}
3920
Vedant Kumar55e007e2018-03-08 21:15:26 +00003921// __clear_and_shrink
3922
3923template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003924inline
Louis Dionne173f29e2019-05-29 16:01:36 +00003925void
Marshall Clowe60a7182018-05-29 17:04:37 +00003926basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
Vedant Kumar55e007e2018-03-08 21:15:26 +00003927{
3928 clear();
3929 if(__is_long())
3930 {
3931 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
3932 __set_long_cap(0);
3933 __set_short_size(0);
Louis Dionne663415f2020-10-05 16:16:13 -04003934 traits_type::assign(*__get_short_pointer(), value_type());
Vedant Kumar55e007e2018-03-08 21:15:26 +00003935 }
Louis Dionne173f29e2019-05-29 16:01:36 +00003936}
Vedant Kumar55e007e2018-03-08 21:15:26 +00003937
Howard Hinnantc51e1022010-05-11 19:42:16 +00003938// operator==
3939
3940template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003941inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003942bool
3943operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003944 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003945{
Howard Hinnantaaeb1132013-04-22 23:55:13 +00003946 size_t __lhs_sz = __lhs.size();
3947 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
3948 __rhs.data(),
3949 __lhs_sz) == 0;
3950}
3951
3952template<class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003953inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaaeb1132013-04-22 23:55:13 +00003954bool
3955operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
3956 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
3957{
3958 size_t __lhs_sz = __lhs.size();
3959 if (__lhs_sz != __rhs.size())
3960 return false;
3961 const char* __lp = __lhs.data();
3962 const char* __rp = __rhs.data();
3963 if (__lhs.__is_long())
3964 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
3965 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
3966 if (*__lp != *__rp)
3967 return false;
3968 return true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003969}
3970
3971template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003972inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003973bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003974operator==(const _CharT* __lhs,
3975 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003976{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00003977 typedef basic_string<_CharT, _Traits, _Allocator> _String;
3978 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
3979 size_t __lhs_len = _Traits::length(__lhs);
3980 if (__lhs_len != __rhs.size()) return false;
3981 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003982}
3983
Howard Hinnantc51e1022010-05-11 19:42:16 +00003984template<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 basic_string<_CharT,_Traits,_Allocator>& __lhs,
3988 const _CharT* __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(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
3992 size_t __rhs_len = _Traits::length(__rhs);
3993 if (__rhs_len != __lhs.size()) return false;
3994 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003995}
3996
Howard Hinnant3b6579a2010-08-22 00:02:43 +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
4000operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004001 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004002{
4003 return !(__lhs == __rhs);
4004}
4005
4006template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004007inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004008bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004009operator!=(const _CharT* __lhs,
4010 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004011{
4012 return !(__lhs == __rhs);
4013}
4014
4015template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004016inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004017bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004018operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4019 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004020{
4021 return !(__lhs == __rhs);
4022}
4023
4024// operator<
4025
4026template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004027inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004028bool
4029operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004030 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004031{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004032 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004033}
4034
4035template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004036inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004037bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004038operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4039 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004040{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004041 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004042}
4043
4044template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004045inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004046bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004047operator< (const _CharT* __lhs,
4048 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004049{
4050 return __rhs.compare(__lhs) > 0;
4051}
4052
Howard Hinnantc51e1022010-05-11 19:42:16 +00004053// operator>
4054
4055template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004056inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004057bool
4058operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004059 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004060{
4061 return __rhs < __lhs;
4062}
4063
4064template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004065inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004066bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004067operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4068 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004069{
4070 return __rhs < __lhs;
4071}
4072
4073template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004074inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004075bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004076operator> (const _CharT* __lhs,
4077 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004078{
4079 return __rhs < __lhs;
4080}
4081
4082// operator<=
4083
4084template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004085inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004086bool
4087operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004088 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004089{
4090 return !(__rhs < __lhs);
4091}
4092
4093template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004094inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004095bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004096operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4097 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004098{
4099 return !(__rhs < __lhs);
4100}
4101
4102template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004103inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004104bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004105operator<=(const _CharT* __lhs,
4106 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004107{
4108 return !(__rhs < __lhs);
4109}
4110
4111// operator>=
4112
4113template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004114inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004115bool
4116operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004117 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004118{
4119 return !(__lhs < __rhs);
4120}
4121
4122template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004123inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004124bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004125operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4126 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004127{
4128 return !(__lhs < __rhs);
4129}
4130
4131template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004132inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004133bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004134operator>=(const _CharT* __lhs,
4135 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004136{
4137 return !(__lhs < __rhs);
4138}
4139
4140// operator +
4141
4142template<class _CharT, class _Traits, class _Allocator>
4143basic_string<_CharT, _Traits, _Allocator>
4144operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4145 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4146{
4147 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4148 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4149 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4150 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4151 __r.append(__rhs.data(), __rhs_sz);
4152 return __r;
4153}
4154
4155template<class _CharT, class _Traits, class _Allocator>
4156basic_string<_CharT, _Traits, _Allocator>
4157operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4158{
4159 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4160 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = _Traits::length(__lhs);
4161 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4162 __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz);
4163 __r.append(__rhs.data(), __rhs_sz);
4164 return __r;
4165}
4166
4167template<class _CharT, class _Traits, class _Allocator>
4168basic_string<_CharT, _Traits, _Allocator>
4169operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4170{
4171 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4172 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4173 __r.__init(&__lhs, 1, 1 + __rhs_sz);
4174 __r.append(__rhs.data(), __rhs_sz);
4175 return __r;
4176}
4177
4178template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004179inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004180basic_string<_CharT, _Traits, _Allocator>
4181operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
4182{
4183 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4184 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4185 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = _Traits::length(__rhs);
4186 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4187 __r.append(__rhs, __rhs_sz);
4188 return __r;
4189}
4190
4191template<class _CharT, class _Traits, class _Allocator>
4192basic_string<_CharT, _Traits, _Allocator>
4193operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
4194{
4195 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4196 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4197 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1);
4198 __r.push_back(__rhs);
4199 return __r;
4200}
4201
Eric Fiselierfc92be82017-04-19 00:28:44 +00004202#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004203
4204template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004205inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004206basic_string<_CharT, _Traits, _Allocator>
4207operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4208{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004209 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004210}
4211
4212template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004213inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004214basic_string<_CharT, _Traits, _Allocator>
4215operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4216{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004217 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004218}
4219
4220template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004221inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004222basic_string<_CharT, _Traits, _Allocator>
4223operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4224{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004225 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004226}
4227
4228template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004229inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004230basic_string<_CharT, _Traits, _Allocator>
4231operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4232{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004233 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004234}
4235
4236template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004237inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004238basic_string<_CharT, _Traits, _Allocator>
4239operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4240{
4241 __rhs.insert(__rhs.begin(), __lhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004242 return _VSTD::move(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004243}
4244
4245template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004246inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004247basic_string<_CharT, _Traits, _Allocator>
4248operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
4249{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004250 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004251}
4252
4253template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004254inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004255basic_string<_CharT, _Traits, _Allocator>
4256operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
4257{
4258 __lhs.push_back(__rhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004259 return _VSTD::move(__lhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004260}
4261
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004262#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004263
4264// swap
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 +00004268void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004269swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +00004270 basic_string<_CharT, _Traits, _Allocator>& __rhs)
4271 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004272{
4273 __lhs.swap(__rhs);
4274}
4275
Bruce Mitchener170d8972020-11-24 12:53:53 -05004276_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = nullptr, int __base = 10);
4277_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = nullptr, int __base = 10);
4278_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = nullptr, int __base = 10);
4279_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = nullptr, int __base = 10);
4280_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004281
Bruce Mitchener170d8972020-11-24 12:53:53 -05004282_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = nullptr);
4283_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = nullptr);
4284_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004285
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004286_LIBCPP_FUNC_VIS string to_string(int __val);
4287_LIBCPP_FUNC_VIS string to_string(unsigned __val);
4288_LIBCPP_FUNC_VIS string to_string(long __val);
4289_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
4290_LIBCPP_FUNC_VIS string to_string(long long __val);
4291_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
4292_LIBCPP_FUNC_VIS string to_string(float __val);
4293_LIBCPP_FUNC_VIS string to_string(double __val);
4294_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004295
Louis Dionne89258142021-08-23 15:32:36 -04004296#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Bruce Mitchener170d8972020-11-24 12:53:53 -05004297_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4298_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4299_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4300_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4301_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004302
Bruce Mitchener170d8972020-11-24 12:53:53 -05004303_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = nullptr);
4304_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = nullptr);
4305_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004306
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004307_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
4308_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
4309_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
4310_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
4311_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
4312_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
4313_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
4314_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
4315_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Louis Dionne89258142021-08-23 15:32:36 -04004316#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004317
Howard Hinnantc51e1022010-05-11 19:42:16 +00004318template<class _CharT, class _Traits, class _Allocator>
Martin Storsjö88890c22021-02-22 01:13:13 +02004319_LIBCPP_TEMPLATE_DATA_VIS
Eric Fiselierc9fdaf12020-01-15 17:02:17 -05004320const typename basic_string<_CharT, _Traits, _Allocator>::size_type
4321 basic_string<_CharT, _Traits, _Allocator>::npos;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004322
Marshall Clow851b9ec2019-05-20 21:56:51 +00004323template <class _CharT, class _Allocator>
4324struct _LIBCPP_TEMPLATE_VIS
4325 hash<basic_string<_CharT, char_traits<_CharT>, _Allocator> >
4326 : public unary_function<
4327 basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004328{
4329 size_t
Marshall Clow851b9ec2019-05-20 21:56:51 +00004330 operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT
4331 { return __do_string_hash(__val.data(), __val.data() + __val.size()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004332};
4333
Howard Hinnantc51e1022010-05-11 19:42:16 +00004334
Howard Hinnantdc095972011-07-18 15:51:59 +00004335template<class _CharT, class _Traits, class _Allocator>
4336basic_ostream<_CharT, _Traits>&
4337operator<<(basic_ostream<_CharT, _Traits>& __os,
4338 const basic_string<_CharT, _Traits, _Allocator>& __str);
4339
4340template<class _CharT, class _Traits, class _Allocator>
4341basic_istream<_CharT, _Traits>&
4342operator>>(basic_istream<_CharT, _Traits>& __is,
4343 basic_string<_CharT, _Traits, _Allocator>& __str);
4344
4345template<class _CharT, class _Traits, class _Allocator>
4346basic_istream<_CharT, _Traits>&
4347getline(basic_istream<_CharT, _Traits>& __is,
4348 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4349
4350template<class _CharT, class _Traits, class _Allocator>
4351inline _LIBCPP_INLINE_VISIBILITY
4352basic_istream<_CharT, _Traits>&
4353getline(basic_istream<_CharT, _Traits>& __is,
4354 basic_string<_CharT, _Traits, _Allocator>& __str);
4355
Howard Hinnantdc095972011-07-18 15:51:59 +00004356template<class _CharT, class _Traits, class _Allocator>
4357inline _LIBCPP_INLINE_VISIBILITY
4358basic_istream<_CharT, _Traits>&
4359getline(basic_istream<_CharT, _Traits>&& __is,
4360 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4361
4362template<class _CharT, class _Traits, class _Allocator>
4363inline _LIBCPP_INLINE_VISIBILITY
4364basic_istream<_CharT, _Traits>&
4365getline(basic_istream<_CharT, _Traits>&& __is,
4366 basic_string<_CharT, _Traits, _Allocator>& __str);
4367
Marshall Clow29b53f22018-12-14 18:49:35 +00004368#if _LIBCPP_STD_VER > 17
Marek Kurdeja98b1412020-05-02 13:58:03 +02004369template <class _CharT, class _Traits, class _Allocator, class _Up>
Marshall Clow29b53f22018-12-14 18:49:35 +00004370inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004371 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4372 erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
4373 auto __old_size = __str.size();
4374 __str.erase(_VSTD::remove(__str.begin(), __str.end(), __v), __str.end());
4375 return __old_size - __str.size();
4376}
Marshall Clow29b53f22018-12-14 18:49:35 +00004377
Marek Kurdeja98b1412020-05-02 13:58:03 +02004378template <class _CharT, class _Traits, class _Allocator, class _Predicate>
Marshall Clow29b53f22018-12-14 18:49:35 +00004379inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004380 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4381 erase_if(basic_string<_CharT, _Traits, _Allocator>& __str,
4382 _Predicate __pred) {
4383 auto __old_size = __str.size();
4384 __str.erase(_VSTD::remove_if(__str.begin(), __str.end(), __pred),
4385 __str.end());
4386 return __old_size - __str.size();
4387}
Marshall Clow29b53f22018-12-14 18:49:35 +00004388#endif
4389
Louis Dionneba400782020-10-02 15:02:52 -04004390#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004391
4392template<class _CharT, class _Traits, class _Allocator>
4393bool
4394basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4395{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004396 return data() <= _VSTD::__to_address(__i->base()) &&
4397 _VSTD::__to_address(__i->base()) < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004398}
4399
4400template<class _CharT, class _Traits, class _Allocator>
4401bool
4402basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4403{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004404 return data() < _VSTD::__to_address(__i->base()) &&
4405 _VSTD::__to_address(__i->base()) <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004406}
4407
4408template<class _CharT, class _Traits, class _Allocator>
4409bool
4410basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4411{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004412 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004413 return data() <= __p && __p <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004414}
4415
4416template<class _CharT, class _Traits, class _Allocator>
4417bool
4418basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4419{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004420 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004421 return data() <= __p && __p < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004422}
4423
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004424#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004425
Louis Dionne173f29e2019-05-29 16:01:36 +00004426#if _LIBCPP_STD_VER > 11
Marshall Clowcba751f2013-07-23 17:05:24 +00004427// Literal suffixes for basic_string [basic.string.literals]
Marshall Clowac868372013-10-05 21:18:32 +00004428inline namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004429{
4430 inline namespace string_literals
4431 {
Howard Hinnant5c167562013-08-07 19:39:48 +00004432 inline _LIBCPP_INLINE_VISIBILITY
4433 basic_string<char> operator "" s( const char *__str, size_t __len )
4434 {
4435 return basic_string<char> (__str, __len);
4436 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004437
Louis Dionne89258142021-08-23 15:32:36 -04004438#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnant5c167562013-08-07 19:39:48 +00004439 inline _LIBCPP_INLINE_VISIBILITY
4440 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4441 {
4442 return basic_string<wchar_t> (__str, __len);
4443 }
Louis Dionne89258142021-08-23 15:32:36 -04004444#endif
Marshall Clowcba751f2013-07-23 17:05:24 +00004445
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -04004446#ifndef _LIBCPP_HAS_NO_CHAR8_T
Marshall Clow8732fed2018-12-11 04:35:44 +00004447 inline _LIBCPP_INLINE_VISIBILITY
4448 basic_string<char8_t> operator "" s(const char8_t *__str, size_t __len) _NOEXCEPT
4449 {
4450 return basic_string<char8_t> (__str, __len);
4451 }
4452#endif
4453
Howard Hinnant5c167562013-08-07 19:39:48 +00004454 inline _LIBCPP_INLINE_VISIBILITY
4455 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4456 {
4457 return basic_string<char16_t> (__str, __len);
4458 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004459
Howard Hinnant5c167562013-08-07 19:39:48 +00004460 inline _LIBCPP_INLINE_VISIBILITY
4461 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4462 {
4463 return basic_string<char32_t> (__str, __len);
4464 }
Nikolas Klauserd26407a2021-12-02 14:12:51 +01004465 } // namespace string_literals
4466} // namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004467#endif
4468
Howard Hinnantc51e1022010-05-11 19:42:16 +00004469_LIBCPP_END_NAMESPACE_STD
4470
Eric Fiselierf4433a32017-05-31 22:07:49 +00004471_LIBCPP_POP_MACROS
4472
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004473#endif // _LIBCPP_STRING