blob: a0056745f5490082cb3a2759f7fd1ab5a5187610 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
Louis Dionne9bd93882021-11-17 16:25:01 -05002//===----------------------------------------------------------------------===//
Howard Hinnantc51e1022010-05-11 19:42:16 +00003//
Chandler Carruth7642bb12019-01-19 08:50:56 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_STRING
11#define _LIBCPP_STRING
12
13/*
14 string synopsis
15
16namespace std
17{
18
19template <class stateT>
20class fpos
21{
22private:
23 stateT st;
24public:
25 fpos(streamoff = streamoff());
26
27 operator streamoff() const;
28
29 stateT state() const;
30 void state(stateT);
31
32 fpos& operator+=(streamoff);
33 fpos operator+ (streamoff) const;
34 fpos& operator-=(streamoff);
35 fpos operator- (streamoff) const;
36};
37
38template <class stateT> streamoff operator-(const fpos<stateT>& x, const fpos<stateT>& y);
39
40template <class stateT> bool operator==(const fpos<stateT>& x, const fpos<stateT>& y);
41template <class stateT> bool operator!=(const fpos<stateT>& x, const fpos<stateT>& y);
42
43template <class charT>
44struct char_traits
45{
46 typedef charT char_type;
47 typedef ... int_type;
48 typedef streamoff off_type;
49 typedef streampos pos_type;
50 typedef mbstate_t state_type;
51
Howard Hinnantaeb17d82011-05-29 19:57:12 +000052 static void assign(char_type& c1, const char_type& c2) noexcept;
Howard Hinnant270c00e2012-07-20 19:09:12 +000053 static constexpr bool eq(char_type c1, char_type c2) noexcept;
54 static constexpr bool lt(char_type c1, char_type c2) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000055
56 static int compare(const char_type* s1, const char_type* s2, size_t n);
57 static size_t length(const char_type* s);
58 static const char_type* find(const char_type* s, size_t n, const char_type& a);
59 static char_type* move(char_type* s1, const char_type* s2, size_t n);
60 static char_type* copy(char_type* s1, const char_type* s2, size_t n);
61 static char_type* assign(char_type* s, size_t n, char_type a);
62
Howard Hinnant270c00e2012-07-20 19:09:12 +000063 static constexpr int_type not_eof(int_type c) noexcept;
64 static constexpr char_type to_char_type(int_type c) noexcept;
65 static constexpr int_type to_int_type(char_type c) noexcept;
66 static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept;
67 static constexpr int_type eof() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000068};
69
70template <> struct char_traits<char>;
71template <> struct char_traits<wchar_t>;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +010072template <> struct char_traits<char8_t>; // C++20
73template <> struct char_traits<char16_t>;
74template <> struct char_traits<char32_t>;
Howard Hinnantc51e1022010-05-11 19:42:16 +000075
Howard Hinnant3b6579a2010-08-22 00:02:43 +000076template<class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
Howard Hinnantc51e1022010-05-11 19:42:16 +000077class basic_string
78{
Howard Hinnant3b6579a2010-08-22 00:02:43 +000079public:
80// types:
Howard Hinnantc51e1022010-05-11 19:42:16 +000081 typedef traits traits_type;
82 typedef typename traits_type::char_type value_type;
83 typedef Allocator allocator_type;
84 typedef typename allocator_type::size_type size_type;
85 typedef typename allocator_type::difference_type difference_type;
86 typedef typename allocator_type::reference reference;
87 typedef typename allocator_type::const_reference const_reference;
88 typedef typename allocator_type::pointer pointer;
89 typedef typename allocator_type::const_pointer const_pointer;
90 typedef implementation-defined iterator;
91 typedef implementation-defined const_iterator;
92 typedef std::reverse_iterator<iterator> reverse_iterator;
93 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
94
95 static const size_type npos = -1;
96
Howard Hinnant3e276872011-06-03 18:40:47 +000097 basic_string()
98 noexcept(is_nothrow_default_constructible<allocator_type>::value);
99 explicit basic_string(const allocator_type& a);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000100 basic_string(const basic_string& str);
Howard Hinnant3e276872011-06-03 18:40:47 +0000101 basic_string(basic_string&& str)
102 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clow83445802016-04-07 18:13:41 +0000103 basic_string(const basic_string& str, size_type pos,
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000104 const allocator_type& a = allocator_type());
Marshall Clow83445802016-04-07 18:13:41 +0000105 basic_string(const basic_string& str, size_type pos, size_type n,
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000106 const Allocator& a = Allocator());
Marshall Clow78dbe462016-11-14 18:22:19 +0000107 template<class T>
108 basic_string(const T& t, size_type pos, size_type n, const Allocator& a = Allocator()); // C++17
Marshall Clowe46031a2018-07-02 18:41:15 +0000109 template <class T>
110 explicit basic_string(const T& t, const Allocator& a = Allocator()); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000111 basic_string(const value_type* s, const allocator_type& a = allocator_type());
112 basic_string(const value_type* s, size_type n, const allocator_type& a = allocator_type());
Marek Kurdej90d79712021-07-27 16:16:21 +0200113 basic_string(nullptr_t) = delete; // C++2b
Howard Hinnantc51e1022010-05-11 19:42:16 +0000114 basic_string(size_type n, value_type c, const allocator_type& a = allocator_type());
115 template<class InputIterator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000116 basic_string(InputIterator begin, InputIterator end,
117 const allocator_type& a = allocator_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000118 basic_string(initializer_list<value_type>, const Allocator& = Allocator());
119 basic_string(const basic_string&, const Allocator&);
120 basic_string(basic_string&&, const Allocator&);
121
122 ~basic_string();
123
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000124 operator basic_string_view<charT, traits>() const noexcept;
125
Howard Hinnantc51e1022010-05-11 19:42:16 +0000126 basic_string& operator=(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000127 template <class T>
128 basic_string& operator=(const T& t); // C++17
Howard Hinnant3e276872011-06-03 18:40:47 +0000129 basic_string& operator=(basic_string&& str)
130 noexcept(
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000131 allocator_type::propagate_on_container_move_assignment::value ||
132 allocator_type::is_always_equal::value ); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000133 basic_string& operator=(const value_type* s);
Marek Kurdej90d79712021-07-27 16:16:21 +0200134 basic_string& operator=(nullptr_t) = delete; // C++2b
Howard Hinnantc51e1022010-05-11 19:42:16 +0000135 basic_string& operator=(value_type c);
136 basic_string& operator=(initializer_list<value_type>);
137
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000138 iterator begin() noexcept;
139 const_iterator begin() const noexcept;
140 iterator end() noexcept;
141 const_iterator end() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000142
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000143 reverse_iterator rbegin() noexcept;
144 const_reverse_iterator rbegin() const noexcept;
145 reverse_iterator rend() noexcept;
146 const_reverse_iterator rend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000147
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000148 const_iterator cbegin() const noexcept;
149 const_iterator cend() const noexcept;
150 const_reverse_iterator crbegin() const noexcept;
151 const_reverse_iterator crend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000152
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000153 size_type size() const noexcept;
154 size_type length() const noexcept;
155 size_type max_size() const noexcept;
156 size_type capacity() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000157
158 void resize(size_type n, value_type c);
159 void resize(size_type n);
160
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100161 template<class Operation>
162 constexpr void resize_and_overwrite(size_type n, Operation op); // since C++23
163
Marek Kurdejc9848142020-11-26 10:07:16 +0100164 void reserve(size_type res_arg);
165 void reserve(); // deprecated in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000166 void shrink_to_fit();
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000167 void clear() noexcept;
168 bool empty() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000169
170 const_reference operator[](size_type pos) const;
171 reference operator[](size_type pos);
172
173 const_reference at(size_type n) const;
174 reference at(size_type n);
175
176 basic_string& operator+=(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000177 template <class T>
178 basic_string& operator+=(const T& t); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000179 basic_string& operator+=(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000180 basic_string& operator+=(value_type c);
181 basic_string& operator+=(initializer_list<value_type>);
182
183 basic_string& append(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000184 template <class T>
185 basic_string& append(const T& t); // C++17
Marshall Clow8db7fb02014-03-04 19:17:19 +0000186 basic_string& append(const basic_string& str, size_type pos, size_type n=npos); //C++14
Marshall Clow82513342016-09-24 22:45:42 +0000187 template <class T>
188 basic_string& append(const T& t, size_type pos, size_type n=npos); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000189 basic_string& append(const value_type* s, size_type n);
190 basic_string& append(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000191 basic_string& append(size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000192 template<class InputIterator>
193 basic_string& append(InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000194 basic_string& append(initializer_list<value_type>);
195
196 void push_back(value_type c);
197 void pop_back();
198 reference front();
199 const_reference front() const;
200 reference back();
201 const_reference back() const;
202
203 basic_string& assign(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000204 template <class T>
205 basic_string& assign(const T& t); // C++17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000206 basic_string& assign(basic_string&& str);
Marshall Clow8db7fb02014-03-04 19:17:19 +0000207 basic_string& assign(const basic_string& str, size_type pos, size_type n=npos); // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000208 template <class T>
209 basic_string& assign(const T& t, size_type pos, size_type n=npos); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000210 basic_string& assign(const value_type* s, size_type n);
211 basic_string& assign(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000212 basic_string& assign(size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000213 template<class InputIterator>
214 basic_string& assign(InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000215 basic_string& assign(initializer_list<value_type>);
216
217 basic_string& insert(size_type pos1, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000218 template <class T>
219 basic_string& insert(size_type pos1, const T& t);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000220 basic_string& insert(size_type pos1, const basic_string& str,
221 size_type pos2, size_type n);
Marshall Clow82513342016-09-24 22:45:42 +0000222 template <class T>
223 basic_string& insert(size_type pos1, const T& t, size_type pos2, size_type n); // C++17
Marshall Clow8db7fb02014-03-04 19:17:19 +0000224 basic_string& insert(size_type pos, const value_type* s, size_type n=npos); //C++14
Howard Hinnantd17880b2013-06-28 16:59:19 +0000225 basic_string& insert(size_type pos, const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000226 basic_string& insert(size_type pos, size_type n, value_type c);
227 iterator insert(const_iterator p, value_type c);
228 iterator insert(const_iterator p, size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000229 template<class InputIterator>
230 iterator insert(const_iterator p, InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000231 iterator insert(const_iterator p, initializer_list<value_type>);
232
233 basic_string& erase(size_type pos = 0, size_type n = npos);
234 iterator erase(const_iterator position);
235 iterator erase(const_iterator first, const_iterator last);
236
237 basic_string& replace(size_type pos1, size_type n1, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000238 template <class T>
239 basic_string& replace(size_type pos1, size_type n1, const T& t); // C++17
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000240 basic_string& replace(size_type pos1, size_type n1, const basic_string& str,
Marshall Clow8db7fb02014-03-04 19:17:19 +0000241 size_type pos2, size_type n2=npos); // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000242 template <class T>
243 basic_string& replace(size_type pos1, size_type n1, const T& t,
244 size_type pos2, size_type n); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000245 basic_string& replace(size_type pos, size_type n1, const value_type* s, size_type n2);
246 basic_string& replace(size_type pos, size_type n1, const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000247 basic_string& replace(size_type pos, size_type n1, size_type n2, value_type c);
Howard Hinnant990d6e82010-11-17 21:11:40 +0000248 basic_string& replace(const_iterator i1, const_iterator i2, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000249 template <class T>
250 basic_string& replace(const_iterator i1, const_iterator i2, const T& t); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000251 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s, size_type n);
252 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s);
Howard Hinnant990d6e82010-11-17 21:11:40 +0000253 basic_string& replace(const_iterator i1, const_iterator i2, size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000254 template<class InputIterator>
Howard Hinnant990d6e82010-11-17 21:11:40 +0000255 basic_string& replace(const_iterator i1, const_iterator i2, InputIterator j1, InputIterator j2);
256 basic_string& replace(const_iterator i1, const_iterator i2, initializer_list<value_type>);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000257
Howard Hinnantd17880b2013-06-28 16:59:19 +0000258 size_type copy(value_type* s, size_type n, size_type pos = 0) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000259 basic_string substr(size_type pos = 0, size_type n = npos) const;
260
Howard Hinnant3e276872011-06-03 18:40:47 +0000261 void swap(basic_string& str)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000262 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
263 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000264
Howard Hinnantd17880b2013-06-28 16:59:19 +0000265 const value_type* c_str() const noexcept;
266 const value_type* data() const noexcept;
Marshall Clowd3c22392016-03-08 15:44:30 +0000267 value_type* data() noexcept; // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000268
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000269 allocator_type get_allocator() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000270
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000271 size_type find(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000272 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800273 size_type find(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000274 size_type find(const value_type* s, size_type pos, size_type n) const noexcept;
275 size_type find(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000276 size_type find(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000277
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000278 size_type rfind(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000279 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800280 size_type rfind(const T& t, size_type pos = npos) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000281 size_type rfind(const value_type* s, size_type pos, size_type n) const noexcept;
282 size_type rfind(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000283 size_type rfind(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000284
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000285 size_type find_first_of(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000286 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800287 size_type find_first_of(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000288 size_type find_first_of(const value_type* s, size_type pos, size_type n) const noexcept;
289 size_type find_first_of(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000290 size_type find_first_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000291
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000292 size_type find_last_of(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000293 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800294 size_type find_last_of(const T& t, size_type pos = npos) const noexcept noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000295 size_type find_last_of(const value_type* s, size_type pos, size_type n) const noexcept;
296 size_type find_last_of(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000297 size_type find_last_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000298
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000299 size_type find_first_not_of(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000300 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800301 size_type find_first_not_of(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000302 size_type find_first_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
303 size_type find_first_not_of(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000304 size_type find_first_not_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000305
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000306 size_type find_last_not_of(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000307 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800308 size_type find_last_not_of(const T& t, size_type pos = npos) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000309 size_type find_last_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
310 size_type find_last_not_of(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000311 size_type find_last_not_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000312
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000313 int compare(const basic_string& str) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000314 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800315 int compare(const T& t) const noexcept; // C++17, noexcept as an extension
Howard Hinnantc51e1022010-05-11 19:42:16 +0000316 int compare(size_type pos1, size_type n1, const basic_string& str) const;
Marshall Clowe46031a2018-07-02 18:41:15 +0000317 template <class T>
318 int compare(size_type pos1, size_type n1, const T& t) const; // C++17
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000319 int compare(size_type pos1, size_type n1, const basic_string& str,
Marshall Clow8db7fb02014-03-04 19:17:19 +0000320 size_type pos2, size_type n2=npos) const; // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000321 template <class T>
322 int compare(size_type pos1, size_type n1, const T& t,
323 size_type pos2, size_type n2=npos) const; // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000324 int compare(const value_type* s) const noexcept;
325 int compare(size_type pos1, size_type n1, const value_type* s) const;
326 int compare(size_type pos1, size_type n1, const value_type* s, size_type n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000327
Marek Kurdej24b4c512021-01-07 12:29:04 +0100328 bool starts_with(basic_string_view<charT, traits> sv) const noexcept; // C++20
329 bool starts_with(charT c) const noexcept; // C++20
330 bool starts_with(const charT* s) const; // C++20
331 bool ends_with(basic_string_view<charT, traits> sv) const noexcept; // C++20
332 bool ends_with(charT c) const noexcept; // C++20
333 bool ends_with(const charT* s) const; // C++20
Marshall Clow18c293b2017-12-04 20:11:38 +0000334
Wim Leflere023c3542021-01-19 14:33:30 -0500335 constexpr bool contains(basic_string_view<charT, traits> sv) const noexcept; // C++2b
336 constexpr bool contains(charT c) const noexcept; // C++2b
337 constexpr bool contains(const charT* s) const; // C++2b
338
Howard Hinnantc51e1022010-05-11 19:42:16 +0000339 bool __invariants() const;
340};
341
Marshall Clowa0563332018-02-08 06:34:03 +0000342template<class InputIterator,
343 class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
344basic_string(InputIterator, InputIterator, Allocator = Allocator())
345 -> basic_string<typename iterator_traits<InputIterator>::value_type,
346 char_traits<typename iterator_traits<InputIterator>::value_type>,
347 Allocator>; // C++17
348
Howard Hinnantc51e1022010-05-11 19:42:16 +0000349template<class charT, class traits, class Allocator>
350basic_string<charT, traits, Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000351operator+(const basic_string<charT, traits, Allocator>& lhs,
352 const basic_string<charT, traits, Allocator>& rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000353
354template<class charT, class traits, class Allocator>
355basic_string<charT, traits, Allocator>
356operator+(const charT* lhs , const basic_string<charT,traits,Allocator>&rhs);
357
358template<class charT, class traits, class Allocator>
359basic_string<charT, traits, Allocator>
360operator+(charT lhs, const basic_string<charT,traits,Allocator>& rhs);
361
362template<class charT, class traits, class Allocator>
363basic_string<charT, traits, Allocator>
364operator+(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs);
365
366template<class charT, class traits, class Allocator>
367basic_string<charT, traits, Allocator>
368operator+(const basic_string<charT, traits, Allocator>& lhs, charT rhs);
369
370template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000371bool operator==(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000372 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000373
374template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000375bool operator==(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000376
377template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000378bool operator==(const basic_string<charT,traits,Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000379
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000380template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000381bool operator!=(const basic_string<charT,traits,Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000382 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000383
384template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000385bool operator!=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000386
387template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000388bool operator!=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000389
390template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000391bool operator< (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000392 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000393
394template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000395bool operator< (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000396
397template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000398bool operator< (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000399
400template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000401bool operator> (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000402 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000403
404template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000405bool operator> (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000406
407template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000408bool operator> (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000409
410template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000411bool operator<=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000412 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000413
414template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000415bool operator<=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000416
417template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000418bool operator<=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000419
420template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000421bool operator>=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000422 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000423
424template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000425bool operator>=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000426
427template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000428bool operator>=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000429
430template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000431void swap(basic_string<charT, traits, Allocator>& lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +0000432 basic_string<charT, traits, Allocator>& rhs)
433 noexcept(noexcept(lhs.swap(rhs)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000434
435template<class charT, class traits, class Allocator>
436basic_istream<charT, traits>&
437operator>>(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
438
439template<class charT, class traits, class Allocator>
440basic_ostream<charT, traits>&
441operator<<(basic_ostream<charT, traits>& os, const basic_string<charT, traits, Allocator>& str);
442
443template<class charT, class traits, class Allocator>
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000444basic_istream<charT, traits>&
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000445getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str,
446 charT delim);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000447
448template<class charT, class traits, class Allocator>
449basic_istream<charT, traits>&
450getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
451
Marshall Clow29b53f22018-12-14 18:49:35 +0000452template<class charT, class traits, class Allocator, class U>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200453typename basic_string<charT, traits, Allocator>::size_type
454erase(basic_string<charT, traits, Allocator>& c, const U& value); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000455template<class charT, class traits, class Allocator, class Predicate>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200456typename basic_string<charT, traits, Allocator>::size_type
457erase_if(basic_string<charT, traits, Allocator>& c, Predicate pred); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000458
Howard Hinnantc51e1022010-05-11 19:42:16 +0000459typedef basic_string<char> string;
460typedef basic_string<wchar_t> wstring;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100461typedef basic_string<char8_t> u8string; // C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000462typedef basic_string<char16_t> u16string;
463typedef basic_string<char32_t> u32string;
464
Bruce Mitchener170d8972020-11-24 12:53:53 -0500465int stoi (const string& str, size_t* idx = nullptr, int base = 10);
466long stol (const string& str, size_t* idx = nullptr, int base = 10);
467unsigned long stoul (const string& str, size_t* idx = nullptr, int base = 10);
468long long stoll (const string& str, size_t* idx = nullptr, int base = 10);
469unsigned long long stoull(const string& str, size_t* idx = nullptr, int base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000470
Bruce Mitchener170d8972020-11-24 12:53:53 -0500471float stof (const string& str, size_t* idx = nullptr);
472double stod (const string& str, size_t* idx = nullptr);
473long double stold(const string& str, size_t* idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000474
475string to_string(int val);
476string to_string(unsigned val);
477string to_string(long val);
478string to_string(unsigned long val);
479string to_string(long long val);
480string to_string(unsigned long long val);
481string to_string(float val);
482string to_string(double val);
483string to_string(long double val);
484
Bruce Mitchener170d8972020-11-24 12:53:53 -0500485int stoi (const wstring& str, size_t* idx = nullptr, int base = 10);
486long stol (const wstring& str, size_t* idx = nullptr, int base = 10);
487unsigned long stoul (const wstring& str, size_t* idx = nullptr, int base = 10);
488long long stoll (const wstring& str, size_t* idx = nullptr, int base = 10);
489unsigned long long stoull(const wstring& str, size_t* idx = nullptr, int base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000490
Bruce Mitchener170d8972020-11-24 12:53:53 -0500491float stof (const wstring& str, size_t* idx = nullptr);
492double stod (const wstring& str, size_t* idx = nullptr);
493long double stold(const wstring& str, size_t* idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000494
495wstring to_wstring(int val);
496wstring to_wstring(unsigned val);
497wstring to_wstring(long val);
498wstring to_wstring(unsigned long val);
499wstring to_wstring(long long val);
500wstring to_wstring(unsigned long long val);
501wstring to_wstring(float val);
502wstring to_wstring(double val);
503wstring to_wstring(long double val);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000504
505template <> struct hash<string>;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100506template <> struct hash<u8string>; // C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000507template <> struct hash<u16string>;
508template <> struct hash<u32string>;
509template <> struct hash<wstring>;
510
Marshall Clowcba751f2013-07-23 17:05:24 +0000511basic_string<char> operator "" s( const char *str, size_t len ); // C++14
512basic_string<wchar_t> operator "" s( const wchar_t *str, size_t len ); // C++14
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100513basic_string<char8_t> operator "" s( const char8_t *str, size_t len ); // C++20
Marshall Clowcba751f2013-07-23 17:05:24 +0000514basic_string<char16_t> operator "" s( const char16_t *str, size_t len ); // C++14
515basic_string<char32_t> operator "" s( const char32_t *str, size_t len ); // C++14
516
Howard Hinnantc51e1022010-05-11 19:42:16 +0000517} // std
518
519*/
520
Nikolas Klauserf210d8a2022-02-15 18:18:08 +0100521#include <__algorithm/max.h>
522#include <__algorithm/min.h>
523#include <__algorithm/remove.h>
524#include <__algorithm/remove_if.h>
Louis Dionneb4fce352022-03-25 12:55:36 -0400525#include <__assert> // all public C++ headers provide the assertion handler
Howard Hinnantc51e1022010-05-11 19:42:16 +0000526#include <__config>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400527#include <__debug>
Nikolas Klauser80840272022-02-04 13:04:33 +0100528#include <__ios/fpos.h>
Louis Dionne77249522021-06-11 09:55:11 -0400529#include <__iterator/wrap_iter.h>
Nikolas Klauser8fccd622022-03-05 19:17:07 +0100530#include <__utility/auto_cast.h>
531#include <__utility/move.h>
532#include <__utility/swap.h>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400533#include <compare>
534#include <cstdio> // EOF
Louis Dionned24191c2021-08-19 12:39:16 -0400535#include <cstdlib>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400536#include <cstring>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400537#include <initializer_list>
538#include <iosfwd>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000539#include <iterator>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000540#include <memory>
541#include <stdexcept>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400542#include <string_view>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000543#include <type_traits>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000544#include <version>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000545
Nikolas Klauser4c1bf592022-02-11 19:15:18 +0100546// TODO: remove these headers
547#include <__functional/binary_function.h>
548#include <__functional/invoke.h>
549#include <__functional/operations.h>
550#include <__functional/reference_wrapper.h>
551#include <__functional/unary_function.h>
552#include <__functional/weak_result_type.h>
553#include <new>
554#include <typeinfo>
555
Louis Dionne89258142021-08-23 15:32:36 -0400556#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Nikolas Klauser80840272022-02-04 13:04:33 +0100557# include <cwchar>
Louis Dionne89258142021-08-23 15:32:36 -0400558#endif
559
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400560#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
Nikolas Klauser80840272022-02-04 13:04:33 +0100561# include <cstdint>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400562#endif
Eric Fiselier14b6de92014-08-10 23:53:08 +0000563
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000564#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Arthur O'Dwyer6eeaa002022-02-01 20:16:40 -0500565# pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000566#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000567
Eric Fiselierf4433a32017-05-31 22:07:49 +0000568_LIBCPP_PUSH_MACROS
569#include <__undef_macros>
570
571
Howard Hinnantc51e1022010-05-11 19:42:16 +0000572_LIBCPP_BEGIN_NAMESPACE_STD
573
Howard Hinnantc51e1022010-05-11 19:42:16 +0000574// basic_string
575
576template<class _CharT, class _Traits, class _Allocator>
577basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000578operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
579 const basic_string<_CharT, _Traits, _Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000580
581template<class _CharT, class _Traits, class _Allocator>
582basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000583operator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000584
585template<class _CharT, class _Traits, class _Allocator>
586basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000587operator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000588
589template<class _CharT, class _Traits, class _Allocator>
Louis Dionne8f8d95d2018-11-21 17:31:55 +0000590inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000591basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000592operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000593
594template<class _CharT, class _Traits, class _Allocator>
595basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000596operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000597
Shoaib Meenaiea363712017-07-29 02:54:41 +0000598_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&))
599
Marshall Clow039b2f02016-01-13 21:54:34 +0000600template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400601struct __string_is_trivial_iterator : public false_type {};
602
603template <class _Tp>
604struct __string_is_trivial_iterator<_Tp*>
605 : public is_arithmetic<_Tp> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000606
Louis Dionne173f29e2019-05-29 16:01:36 +0000607template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400608struct __string_is_trivial_iterator<__wrap_iter<_Iter> >
609 : public __string_is_trivial_iterator<_Iter> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000610
Marshall Clow82513342016-09-24 22:45:42 +0000611template <class _CharT, class _Traits, class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500612struct __can_be_converted_to_string_view : public _BoolConstant<
613 is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
614 !is_convertible<const _Tp&, const _CharT*>::value
615 > {};
Marshall Clow82513342016-09-24 22:45:42 +0000616
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];
Azat Khuzhin1995f722022-04-08 16:13:46 -0400690 unsigned char __padding[sizeof(value_type) - 1];
691 unsigned char __size_;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000692 };
693
694#else
695
Howard Hinnantc51e1022010-05-11 19:42:16 +0000696 struct __long
697 {
698 size_type __cap_;
699 size_type __size_;
700 pointer __data_;
701 };
702
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000703#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000704 static const size_type __short_mask = 0x80;
705 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000706#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000707 static const size_type __short_mask = 0x01;
708 static const size_type __long_mask = 0x1ul;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400709#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +0000710
Howard Hinnantc51e1022010-05-11 19:42:16 +0000711 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
712 (sizeof(__long) - 1)/sizeof(value_type) : 2};
713
714 struct __short
715 {
716 union
717 {
718 unsigned char __size_;
Howard Hinnant49e145e2012-10-30 19:06:59 +0000719 value_type __lx;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000720 };
721 value_type __data_[__min_cap];
722 };
723
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400724#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000725
Howard Hinnant8ea98242013-08-23 17:37:05 +0000726 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000727
Howard Hinnant8ea98242013-08-23 17:37:05 +0000728 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000729
730 struct __raw
731 {
732 size_type __words[__n_words];
733 };
734
735 struct __rep
736 {
737 union
738 {
739 __long __l;
740 __short __s;
741 __raw __r;
742 };
743 };
744
745 __compressed_pair<__rep, allocator_type> __r_;
746
Howard Hinnantc51e1022010-05-11 19:42:16 +0000747public:
Martin Storsjö88890c22021-02-22 01:13:13 +0200748 _LIBCPP_TEMPLATE_DATA_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000749 static const size_type npos = -1;
750
Howard Hinnant3e276872011-06-03 18:40:47 +0000751 _LIBCPP_INLINE_VISIBILITY basic_string()
752 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000753
754 _LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a)
755#if _LIBCPP_STD_VER <= 14
756 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
757#else
758 _NOEXCEPT;
759#endif
760
Howard Hinnantc51e1022010-05-11 19:42:16 +0000761 basic_string(const basic_string& __str);
762 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clowa80abc72015-06-03 19:56:43 +0000763
Eric Fiselierfc92be82017-04-19 00:28:44 +0000764#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000765 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000766 basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +0000767#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +0000768 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000769#else
770 _NOEXCEPT;
771#endif
772
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000773 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000774 basic_string(basic_string&& __str, const allocator_type& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400775#endif // _LIBCPP_CXX03_LANG
Marshall Clowe46031a2018-07-02 18:41:15 +0000776
Louis Dionne9ce598d2021-09-08 09:14:43 -0400777 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Eric Fiseliera8567862018-07-17 05:48:48 +0000778 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5169d1c2019-12-16 19:03:23 -0500779 basic_string(const _CharT* __s) : __r_(__default_init_tag(), __default_init_tag()) {
Eric Fiseliera8567862018-07-17 05:48:48 +0000780 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
781 __init(__s, traits_type::length(__s));
Nikolas Klauserf2807732022-01-11 00:33:35 +0100782 _VSTD::__debug_db_insert_c(this);
Eric Fiseliera8567862018-07-17 05:48:48 +0000783 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000784
Louis Dionne9ce598d2021-09-08 09:14:43 -0400785 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000786 _LIBCPP_INLINE_VISIBILITY
787 basic_string(const _CharT* __s, const _Allocator& __a);
788
Marek Kurdej90d79712021-07-27 16:16:21 +0200789#if _LIBCPP_STD_VER > 20
790 basic_string(nullptr_t) = delete;
791#endif
792
Howard Hinnantcf823322010-12-17 14:46:43 +0000793 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000794 basic_string(const _CharT* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +0000795 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000796 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000797 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000798 basic_string(size_type __n, _CharT __c);
Marshall Clowe46031a2018-07-02 18:41:15 +0000799
Louis Dionne9ce598d2021-09-08 09:14:43 -0400800 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000801 _LIBCPP_INLINE_VISIBILITY
802 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
803
Marshall Clow83445802016-04-07 18:13:41 +0000804 basic_string(const basic_string& __str, size_type __pos, size_type __n,
Eric Fiselier812882b2017-02-17 01:17:10 +0000805 const _Allocator& __a = _Allocator());
Marshall Clow83445802016-04-07 18:13:41 +0000806 _LIBCPP_INLINE_VISIBILITY
807 basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +0000808 const _Allocator& __a = _Allocator());
Marshall Clowe46031a2018-07-02 18:41:15 +0000809
Louis Dionne9ce598d2021-09-08 09:14:43 -0400810 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 +0000811 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselier812882b2017-02-17 01:17:10 +0000812 basic_string(const _Tp& __t, size_type __pos, size_type __n,
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500813 const allocator_type& __a = allocator_type());
Marshall Clowe46031a2018-07-02 18:41:15 +0000814
Louis Dionne9ce598d2021-09-08 09:14:43 -0400815 template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500816 !__is_same_uncvref<_Tp, basic_string>::value> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000817 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
818 explicit basic_string(const _Tp& __t);
819
Louis Dionne9ce598d2021-09-08 09:14:43 -0400820 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 +0000821 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
822 explicit basic_string(const _Tp& __t, const allocator_type& __a);
823
Louis Dionne9ce598d2021-09-08 09:14:43 -0400824 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000825 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000826 basic_string(_InputIterator __first, _InputIterator __last);
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, const allocator_type& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000830#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000831 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000832 basic_string(initializer_list<_CharT> __il);
Howard Hinnantcf823322010-12-17 14:46:43 +0000833 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000834 basic_string(initializer_list<_CharT> __il, const _Allocator& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400835#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000836
Eric Fiselierd9a702a2016-10-31 03:42:50 +0000837 inline ~basic_string();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000838
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000839 _LIBCPP_INLINE_VISIBILITY
840 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
841
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000842 basic_string& operator=(const basic_string& __str);
Eric Fiselier5ec13b12017-01-23 21:24:58 +0000843
Louis Dionne9ce598d2021-09-08 09:14:43 -0400844 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 +0000845 basic_string& operator=(const _Tp& __t)
846 {__self_view __sv = __t; return assign(__sv);}
847
Eric Fiselierfc92be82017-04-19 00:28:44 +0000848#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000849 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000850 basic_string& operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000851 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiselierfc92be82017-04-19 00:28:44 +0000852 _LIBCPP_INLINE_VISIBILITY
853 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000854#endif
Howard Hinnantd17880b2013-06-28 16:59:19 +0000855 _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const value_type* __s) {return assign(__s);}
Marek Kurdej90d79712021-07-27 16:16:21 +0200856#if _LIBCPP_STD_VER > 20
857 basic_string& operator=(nullptr_t) = delete;
858#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000859 basic_string& operator=(value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000860
Louis Dionneba400782020-10-02 15:02:52 -0400861#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +0000862 _LIBCPP_INLINE_VISIBILITY
863 iterator begin() _NOEXCEPT
864 {return iterator(this, __get_pointer());}
865 _LIBCPP_INLINE_VISIBILITY
866 const_iterator begin() const _NOEXCEPT
867 {return const_iterator(this, __get_pointer());}
868 _LIBCPP_INLINE_VISIBILITY
869 iterator end() _NOEXCEPT
870 {return iterator(this, __get_pointer() + size());}
871 _LIBCPP_INLINE_VISIBILITY
872 const_iterator end() const _NOEXCEPT
873 {return const_iterator(this, __get_pointer() + size());}
874#else
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000875 _LIBCPP_INLINE_VISIBILITY
876 iterator begin() _NOEXCEPT
877 {return iterator(__get_pointer());}
878 _LIBCPP_INLINE_VISIBILITY
879 const_iterator begin() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000880 {return const_iterator(__get_pointer());}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000881 _LIBCPP_INLINE_VISIBILITY
882 iterator end() _NOEXCEPT
883 {return iterator(__get_pointer() + size());}
884 _LIBCPP_INLINE_VISIBILITY
885 const_iterator end() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000886 {return const_iterator(__get_pointer() + size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400887#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000888 _LIBCPP_INLINE_VISIBILITY
889 reverse_iterator rbegin() _NOEXCEPT
890 {return reverse_iterator(end());}
891 _LIBCPP_INLINE_VISIBILITY
892 const_reverse_iterator rbegin() const _NOEXCEPT
893 {return const_reverse_iterator(end());}
894 _LIBCPP_INLINE_VISIBILITY
895 reverse_iterator rend() _NOEXCEPT
896 {return reverse_iterator(begin());}
897 _LIBCPP_INLINE_VISIBILITY
898 const_reverse_iterator rend() const _NOEXCEPT
899 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000900
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000901 _LIBCPP_INLINE_VISIBILITY
902 const_iterator cbegin() const _NOEXCEPT
903 {return begin();}
904 _LIBCPP_INLINE_VISIBILITY
905 const_iterator cend() const _NOEXCEPT
906 {return end();}
907 _LIBCPP_INLINE_VISIBILITY
908 const_reverse_iterator crbegin() const _NOEXCEPT
909 {return rbegin();}
910 _LIBCPP_INLINE_VISIBILITY
911 const_reverse_iterator crend() const _NOEXCEPT
912 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000913
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000914 _LIBCPP_INLINE_VISIBILITY size_type size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000915 {return __is_long() ? __get_long_size() : __get_short_size();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000916 _LIBCPP_INLINE_VISIBILITY size_type length() const _NOEXCEPT {return size();}
917 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT;
918 _LIBCPP_INLINE_VISIBILITY size_type capacity() const _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +0000919 {return (__is_long() ? __get_long_cap()
920 : static_cast<size_type>(__min_cap)) - 1;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000921
922 void resize(size_type __n, value_type __c);
923 _LIBCPP_INLINE_VISIBILITY void resize(size_type __n) {resize(__n, value_type());}
924
Marek Kurdejc9848142020-11-26 10:07:16 +0100925 void reserve(size_type __requested_capacity);
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100926
927#if _LIBCPP_STD_VER > 20
928 template <class _Op>
929 _LIBCPP_HIDE_FROM_ABI constexpr
930 void resize_and_overwrite(size_type __n, _Op __op) {
931 __resize_default_init(__n);
Nikolas Klausera4a76a12022-01-20 13:53:59 +0100932 __erase_to_end(_VSTD::move(__op)(data(), _LIBCPP_AUTO_CAST(__n)));
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100933 }
934#endif
935
Eric Fiselier451d5582018-11-26 20:15:38 +0000936 _LIBCPP_INLINE_VISIBILITY void __resize_default_init(size_type __n);
937
Marek Kurdejc9848142020-11-26 10:07:16 +0100938 _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_INLINE_VISIBILITY
939 void reserve() _NOEXCEPT {shrink_to_fit();}
Marshall Clow6ae12a82018-11-28 18:18:34 +0000940 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejc9848142020-11-26 10:07:16 +0100941 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000942 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000943 void clear() _NOEXCEPT;
Marshall Clowb7db4972017-11-15 20:02:27 +0000944 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
945 bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000946
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000947 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __pos) const _NOEXCEPT;
948 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000949
950 const_reference at(size_type __n) const;
951 reference at(size_type __n);
952
953 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const basic_string& __str) {return append(__str);}
Marshall Clowe46031a2018-07-02 18:41:15 +0000954
955 template <class _Tp>
956 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -0400957 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +0000958 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500959 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
960 && !__is_same_uncvref<_Tp, basic_string >::value,
Marshall Clowe46031a2018-07-02 18:41:15 +0000961 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500962 >
Marshall Clowe46031a2018-07-02 18:41:15 +0000963 operator+=(const _Tp& __t) {__self_view __sv = __t; return append(__sv);}
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000964 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const value_type* __s) {return append(__s);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000965 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(value_type __c) {push_back(__c); return *this;}
Eric Fiselierfc92be82017-04-19 00:28:44 +0000966#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000967 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(initializer_list<value_type> __il) {return append(__il);}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400968#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000969
Howard Hinnantcf823322010-12-17 14:46:43 +0000970 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000971 basic_string& append(const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000972
973 template <class _Tp>
974 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -0400975 __enable_if_t<
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500976 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
977 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clowe46031a2018-07-02 18:41:15 +0000978 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500979 >
Marshall Clowe46031a2018-07-02 18:41:15 +0000980 append(const _Tp& __t) { __self_view __sv = __t; return append(__sv.data(), __sv.size()); }
Marshall Clow8db7fb02014-03-04 19:17:19 +0000981 basic_string& append(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clowe46031a2018-07-02 18:41:15 +0000982
Marshall Clow62953962016-10-03 23:40:48 +0000983 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +0000984 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -0400985 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +0000986 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500987 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
988 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +0000989 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500990 >
Marshall Clow82513342016-09-24 22:45:42 +0000991 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +0000992 basic_string& append(const value_type* __s, size_type __n);
993 basic_string& append(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000994 basic_string& append(size_type __n, value_type __c);
Eric Fiselier451d5582018-11-26 20:15:38 +0000995
996 _LIBCPP_INLINE_VISIBILITY
997 void __append_default_init(size_type __n);
998
Howard Hinnantc51e1022010-05-11 19:42:16 +0000999 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001000 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001001 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001002 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001003 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001004 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001005 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001006 _LIBCPP_INLINE_VISIBILITY
1007 append(_InputIterator __first, _InputIterator __last) {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001008 const basic_string __temp(__first, __last, __alloc());
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001009 append(__temp.data(), __temp.size());
1010 return *this;
1011 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001012 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001013 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001014 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001015 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001016 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001017 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001018 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001019 _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001020 append(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001021
Eric Fiselierfc92be82017-04-19 00:28:44 +00001022#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001023 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001024 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001025#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001026
1027 void push_back(value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001028 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001029 void pop_back();
Marshall Clow05cf6692019-03-19 03:30:07 +00001030 _LIBCPP_INLINE_VISIBILITY reference front() _NOEXCEPT;
1031 _LIBCPP_INLINE_VISIBILITY const_reference front() const _NOEXCEPT;
1032 _LIBCPP_INLINE_VISIBILITY reference back() _NOEXCEPT;
1033 _LIBCPP_INLINE_VISIBILITY const_reference back() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001034
Marshall Clowe46031a2018-07-02 18:41:15 +00001035 template <class _Tp>
1036 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001037 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001038 <
1039 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1040 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001041 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001042 assign(const _Tp & __t) { __self_view __sv = __t; return assign(__sv.data(), __sv.size()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001043 _LIBCPP_INLINE_VISIBILITY
Marshall Clow95d5e9a2016-03-09 18:08:29 +00001044 basic_string& assign(const basic_string& __str) { return *this = __str; }
Eric Fiselierfc92be82017-04-19 00:28:44 +00001045#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001046 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001047 basic_string& assign(basic_string&& __str)
Marshall Clow5aa9e942015-10-05 16:17:34 +00001048 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001049 {*this = _VSTD::move(__str); return *this;}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001050#endif
Marshall Clow8db7fb02014-03-04 19:17:19 +00001051 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow62953962016-10-03 23:40:48 +00001052 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001053 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001054 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001055 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001056 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1057 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001058 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001059 >
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001060 assign(const _Tp & __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001061 basic_string& assign(const value_type* __s, size_type __n);
1062 basic_string& assign(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001063 basic_string& assign(size_type __n, value_type __c);
1064 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001065 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001066 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001067 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001068 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001069 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001070 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001071 assign(_InputIterator __first, _InputIterator __last);
1072 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001073 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001074 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001075 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001076 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001077 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001078 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001079 assign(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001080#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001081 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001082 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001083#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001084
Howard Hinnantcf823322010-12-17 14:46:43 +00001085 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001086 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001087
1088 template <class _Tp>
1089 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001090 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001091 <
1092 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1093 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001094 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001095 insert(size_type __pos1, const _Tp& __t)
1096 { __self_view __sv = __t; return insert(__pos1, __sv.data(), __sv.size()); }
1097
Marshall Clow82513342016-09-24 22:45:42 +00001098 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001099 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001100 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001101 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001102 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001103 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001104 >
Marshall Clow82513342016-09-24 22:45:42 +00001105 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Marshall Clow8db7fb02014-03-04 19:17:19 +00001106 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001107 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1108 basic_string& insert(size_type __pos, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001109 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1110 iterator insert(const_iterator __pos, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001111 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001112 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1113 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001114 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001115 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001116 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001117 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001118 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001119 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001120 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1121 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001122 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001123 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001124 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001125 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001126 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001127 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001128 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001129#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001130 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001131 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1132 {return insert(__pos, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001133#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001134
1135 basic_string& erase(size_type __pos = 0, size_type __n = npos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001136 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001137 iterator erase(const_iterator __pos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001138 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001139 iterator erase(const_iterator __first, const_iterator __last);
1140
Howard Hinnantcf823322010-12-17 14:46:43 +00001141 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001142 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001143
1144 template <class _Tp>
1145 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001146 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001147 <
1148 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1149 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001150 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001151 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 +00001152 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 +00001153 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001154 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001155 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001156 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001157 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001158 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001159 >
Marshall Clow82513342016-09-24 22:45:42 +00001160 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001161 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
1162 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001163 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001164 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001165 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001166
1167 template <class _Tp>
1168 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001169 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001170 <
1171 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1172 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001173 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001174 replace(const_iterator __i1, const_iterator __i2, const _Tp& __t) { __self_view __sv = __t; return replace(__i1 - begin(), __i2 - __i1, __sv); }
1175
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001176 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001177 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +00001178 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001179 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Howard Hinnantcf823322010-12-17 14:46:43 +00001180 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001181 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001182 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001183 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001184 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001185 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001186 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001187 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001188 >
Howard Hinnant990d6e82010-11-17 21:11:40 +00001189 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001190#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001191 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001192 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001193 {return replace(__i1, __i2, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001194#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001195
Howard Hinnantd17880b2013-06-28 16:59:19 +00001196 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
Howard Hinnantcf823322010-12-17 14:46:43 +00001197 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001198 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1199
Howard Hinnantcf823322010-12-17 14:46:43 +00001200 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001201 void swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001202#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00001203 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +00001204#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001205 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001206 __is_nothrow_swappable<allocator_type>::value);
1207#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001208
Howard Hinnantcf823322010-12-17 14:46:43 +00001209 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001210 const value_type* c_str() const _NOEXCEPT {return data();}
Howard Hinnantcf823322010-12-17 14:46:43 +00001211 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001212 const value_type* data() const _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Eric Fiselier4ee2a892017-11-20 20:23:27 +00001213#if _LIBCPP_STD_VER > 14 || defined(_LIBCPP_BUILDING_LIBRARY)
Marshall Clowd3c22392016-03-08 15:44:30 +00001214 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001215 value_type* data() _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Marshall Clowd3c22392016-03-08 15:44:30 +00001216#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001217
Howard Hinnantcf823322010-12-17 14:46:43 +00001218 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001219 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
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 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001223
1224 template <class _Tp>
1225 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001226 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001227 <
1228 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1229 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001230 >
zoecarver1997e0a2021-02-05 11:54:47 -08001231 find(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001232 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001233 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001234 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001235 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001236
Howard Hinnantcf823322010-12-17 14:46:43 +00001237 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001238 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001239
1240 template <class _Tp>
1241 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001242 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001243 <
1244 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1245 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001246 >
zoecarver1997e0a2021-02-05 11:54:47 -08001247 rfind(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001248 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001249 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001250 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001251 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001252
Howard Hinnantcf823322010-12-17 14:46:43 +00001253 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001254 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001255
1256 template <class _Tp>
1257 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001258 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001259 <
1260 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1261 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001262 >
zoecarver1997e0a2021-02-05 11:54:47 -08001263 find_first_of(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001264 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001265 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001266 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001267 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001268 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001269
Howard Hinnantcf823322010-12-17 14:46:43 +00001270 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001271 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001272
1273 template <class _Tp>
1274 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001275 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001276 <
1277 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1278 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001279 >
zoecarver1997e0a2021-02-05 11:54:47 -08001280 find_last_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001281 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001282 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001283 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001284 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001285 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001286
Howard Hinnantcf823322010-12-17 14:46:43 +00001287 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001288 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001289
1290 template <class _Tp>
1291 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001292 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001293 <
1294 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1295 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001296 >
zoecarver1997e0a2021-02-05 11:54:47 -08001297 find_first_not_of(const _Tp &__t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001298 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 +00001299 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001300 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001301 _LIBCPP_INLINE_VISIBILITY
1302 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1303
1304 _LIBCPP_INLINE_VISIBILITY
1305 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001306
1307 template <class _Tp>
1308 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001309 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001310 <
1311 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1312 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001313 >
zoecarver1997e0a2021-02-05 11:54:47 -08001314 find_last_not_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001315 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 +00001316 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001317 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001318 _LIBCPP_INLINE_VISIBILITY
1319 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1320
1321 _LIBCPP_INLINE_VISIBILITY
1322 int compare(const basic_string& __str) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001323
1324 template <class _Tp>
1325 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001326 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001327 <
1328 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1329 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001330 >
zoecarver1997e0a2021-02-05 11:54:47 -08001331 compare(const _Tp &__t) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001332
1333 template <class _Tp>
1334 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001335 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001336 <
1337 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1338 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001339 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001340 compare(size_type __pos1, size_type __n1, const _Tp& __t) const;
1341
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001342 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001343 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Marshall Clow8db7fb02014-03-04 19:17:19 +00001344 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 +00001345
Marshall Clow82513342016-09-24 22:45:42 +00001346 template <class _Tp>
Eric Fiselier7d4aa5c2016-10-14 05:29:46 +00001347 inline _LIBCPP_INLINE_VISIBILITY
Louis Dionne9ce598d2021-09-08 09:14:43 -04001348 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001349 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001350 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001351 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001352 >
Marshall Clow82513342016-09-24 22:45:42 +00001353 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 +00001354 int compare(const value_type* __s) const _NOEXCEPT;
1355 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1356 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001357
Marshall Clow18c293b2017-12-04 20:11:38 +00001358#if _LIBCPP_STD_VER > 17
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001359 constexpr _LIBCPP_INLINE_VISIBILITY
1360 bool starts_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001361 { return __self_view(data(), size()).starts_with(__sv); }
1362
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001363 constexpr _LIBCPP_INLINE_VISIBILITY
1364 bool starts_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001365 { return !empty() && _Traits::eq(front(), __c); }
1366
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001367 constexpr _LIBCPP_INLINE_VISIBILITY
1368 bool starts_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001369 { return starts_with(__self_view(__s)); }
1370
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001371 constexpr _LIBCPP_INLINE_VISIBILITY
1372 bool ends_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001373 { return __self_view(data(), size()).ends_with( __sv); }
1374
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001375 constexpr _LIBCPP_INLINE_VISIBILITY
1376 bool ends_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001377 { return !empty() && _Traits::eq(back(), __c); }
1378
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001379 constexpr _LIBCPP_INLINE_VISIBILITY
1380 bool ends_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001381 { return ends_with(__self_view(__s)); }
1382#endif
1383
Wim Leflere023c3542021-01-19 14:33:30 -05001384#if _LIBCPP_STD_VER > 20
1385 constexpr _LIBCPP_INLINE_VISIBILITY
1386 bool contains(__self_view __sv) const noexcept
1387 { return __self_view(data(), size()).contains(__sv); }
1388
1389 constexpr _LIBCPP_INLINE_VISIBILITY
1390 bool contains(value_type __c) const noexcept
1391 { return __self_view(data(), size()).contains(__c); }
1392
1393 constexpr _LIBCPP_INLINE_VISIBILITY
1394 bool contains(const value_type* __s) const
1395 { return __self_view(data(), size()).contains(__s); }
1396#endif
1397
Howard Hinnantcf823322010-12-17 14:46:43 +00001398 _LIBCPP_INLINE_VISIBILITY bool __invariants() const;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001399
Marshall Clowe60a7182018-05-29 17:04:37 +00001400 _LIBCPP_INLINE_VISIBILITY void __clear_and_shrink() _NOEXCEPT;
Marshall Clow851b9ec2019-05-20 21:56:51 +00001401
Marek Kurdejc9848142020-11-26 10:07:16 +01001402 _LIBCPP_INLINE_VISIBILITY void __shrink_or_extend(size_type __target_capacity);
1403
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001404 _LIBCPP_INLINE_VISIBILITY
1405 bool __is_long() const _NOEXCEPT
1406 {return bool(__r_.first().__s.__size_ & __short_mask);}
1407
Louis Dionneba400782020-10-02 15:02:52 -04001408#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001409
1410 bool __dereferenceable(const const_iterator* __i) const;
1411 bool __decrementable(const const_iterator* __i) const;
1412 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1413 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1414
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001415#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001416
Howard Hinnantc51e1022010-05-11 19:42:16 +00001417private:
Nikolas Klauser93826d12022-01-04 17:24:03 +01001418 _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI static bool __fits_in_sso(size_type __sz) {
1419 // SSO is disabled during constant evaluation because `__is_long` isn't constexpr friendly
1420 return !__libcpp_is_constant_evaluated() && (__sz < __min_cap);
1421 }
1422
Nikolas Klauser48d680d2022-02-26 13:28:33 +01001423 template <class _ForwardIterator>
1424 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
1425 iterator __insert_from_safe_copy(size_type __n, size_type __ip, _ForwardIterator __first, _ForwardIterator __last) {
1426 size_type __sz = size();
1427 size_type __cap = capacity();
1428 value_type* __p;
1429 if (__cap - __sz >= __n)
1430 {
1431 __p = std::__to_address(__get_pointer());
1432 size_type __n_move = __sz - __ip;
1433 if (__n_move != 0)
1434 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
1435 }
1436 else
1437 {
1438 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
1439 __p = std::__to_address(__get_long_pointer());
1440 }
1441 __sz += __n;
1442 __set_size(__sz);
1443 traits_type::assign(__p[__sz], value_type());
1444 for (__p += __ip; __first != __last; ++__p, ++__first)
1445 traits_type::assign(*__p, *__first);
1446
1447 return begin() + __ip;
1448 }
1449
1450 _LIBCPP_HIDE_FROM_ABI allocator_type& __alloc() _NOEXCEPT { return __r_.second(); }
1451 _LIBCPP_HIDE_FROM_ABI const allocator_type& __alloc() const _NOEXCEPT { return __r_.second(); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001452
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001453#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001454
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001455 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001456 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001457# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001458 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001459# else
1460 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1461# endif
1462
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001463 _LIBCPP_INLINE_VISIBILITY
1464 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001465# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001466 {return __r_.first().__s.__size_ >> 1;}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001467# else
1468 {return __r_.first().__s.__size_;}
1469# endif
1470
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001471#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001472
1473 _LIBCPP_INLINE_VISIBILITY
1474 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001475# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001476 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1477# else
1478 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
1479# endif
1480
1481 _LIBCPP_INLINE_VISIBILITY
1482 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001483# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001484 {return __r_.first().__s.__size_;}
1485# else
1486 {return __r_.first().__s.__size_ >> 1;}
1487# endif
1488
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001489#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001490
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001491 _LIBCPP_INLINE_VISIBILITY
1492 void __set_long_size(size_type __s) _NOEXCEPT
1493 {__r_.first().__l.__size_ = __s;}
1494 _LIBCPP_INLINE_VISIBILITY
1495 size_type __get_long_size() const _NOEXCEPT
1496 {return __r_.first().__l.__size_;}
1497 _LIBCPP_INLINE_VISIBILITY
1498 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001499 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1500
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001501 _LIBCPP_INLINE_VISIBILITY
1502 void __set_long_cap(size_type __s) _NOEXCEPT
1503 {__r_.first().__l.__cap_ = __long_mask | __s;}
1504 _LIBCPP_INLINE_VISIBILITY
1505 size_type __get_long_cap() const _NOEXCEPT
Howard Hinnant28b24882011-12-01 20:21:04 +00001506 {return __r_.first().__l.__cap_ & size_type(~__long_mask);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001507
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001508 _LIBCPP_INLINE_VISIBILITY
1509 void __set_long_pointer(pointer __p) _NOEXCEPT
1510 {__r_.first().__l.__data_ = __p;}
1511 _LIBCPP_INLINE_VISIBILITY
1512 pointer __get_long_pointer() _NOEXCEPT
1513 {return __r_.first().__l.__data_;}
1514 _LIBCPP_INLINE_VISIBILITY
1515 const_pointer __get_long_pointer() const _NOEXCEPT
1516 {return __r_.first().__l.__data_;}
1517 _LIBCPP_INLINE_VISIBILITY
1518 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001519 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001520 _LIBCPP_INLINE_VISIBILITY
1521 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001522 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001523 _LIBCPP_INLINE_VISIBILITY
1524 pointer __get_pointer() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001525 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001526 _LIBCPP_INLINE_VISIBILITY
1527 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001528 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1529
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001530 _LIBCPP_INLINE_VISIBILITY
1531 void __zero() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001532 {
1533 size_type (&__a)[__n_words] = __r_.first().__r.__words;
1534 for (unsigned __i = 0; __i < __n_words; ++__i)
1535 __a[__i] = 0;
1536 }
1537
1538 template <size_type __a> static
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001539 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00001540 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +00001541 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001542 enum {__alignment = 16};
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001543 static _LIBCPP_INLINE_VISIBILITY
1544 size_type __recommend(size_type __s) _NOEXCEPT
Marshall Clow80584522018-02-07 21:30:17 +00001545 {
1546 if (__s < __min_cap) return static_cast<size_type>(__min_cap) - 1;
1547 size_type __guess = __align_it<sizeof(value_type) < __alignment ?
1548 __alignment/sizeof(value_type) : 1 > (__s+1) - 1;
1549 if (__guess == __min_cap) ++__guess;
1550 return __guess;
1551 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001552
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001553 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001554 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001555 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001556 void __init(const value_type* __s, size_type __sz);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001557 inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001558 void __init(size_type __n, value_type __c);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001559
Martijn Vels5e7c9752020-03-04 17:52:46 -05001560 // Slow path for the (inlined) copy constructor for 'long' strings.
1561 // Always externally instantiated and not inlined.
1562 // Requires that __s is zero terminated.
1563 // The main reason for this function to exist is because for unstable, we
1564 // want to allow inlining of the copy constructor. However, we don't want
1565 // to call the __init() functions as those are marked as inline which may
1566 // result in over-aggressive inlining by the compiler, where our aim is
1567 // to only inline the fast path code directly in the ctor.
1568 void __init_copy_ctor_external(const value_type* __s, size_type __sz);
1569
Howard Hinnantc51e1022010-05-11 19:42:16 +00001570 template <class _InputIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001571 inline
Louis Dionne9ce598d2021-09-08 09:14:43 -04001572 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001573 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001574 __is_exactly_cpp17_input_iterator<_InputIterator>::value
1575 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001576 __init(_InputIterator __first, _InputIterator __last);
1577
1578 template <class _ForwardIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001579 inline
Louis Dionne9ce598d2021-09-08 09:14:43 -04001580 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001581 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001582 __is_cpp17_forward_iterator<_ForwardIterator>::value
1583 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001584 __init(_ForwardIterator __first, _ForwardIterator __last);
1585
1586 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001587 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001588 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1589 size_type __n_copy, size_type __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00001590 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001591
Martijn Vels596e3de2020-02-26 15:55:49 -05001592 // __assign_no_alias is invoked for assignment operations where we
1593 // have proof that the input does not alias the current instance.
1594 // For example, operator=(basic_string) performs a 'self' check.
1595 template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04001596 basic_string& __assign_no_alias(const value_type* __s, size_type __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05001597
Howard Hinnantcf823322010-12-17 14:46:43 +00001598 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001599 void __erase_to_end(size_type __pos);
1600
Martijn Velsa81fc792020-02-26 13:25:43 -05001601 // __erase_external_with_move is invoked for erase() invocations where
1602 // `n ~= npos`, likely requiring memory moves on the string data.
1603 void __erase_external_with_move(size_type __pos, size_type __n);
1604
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001605 _LIBCPP_INLINE_VISIBILITY
1606 void __copy_assign_alloc(const basic_string& __str)
1607 {__copy_assign_alloc(__str, integral_constant<bool,
1608 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1609
1610 _LIBCPP_INLINE_VISIBILITY
1611 void __copy_assign_alloc(const basic_string& __str, true_type)
1612 {
Marshall Clowf258c202017-01-31 03:40:52 +00001613 if (__alloc() == __str.__alloc())
1614 __alloc() = __str.__alloc();
1615 else
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001616 {
Marshall Clowf258c202017-01-31 03:40:52 +00001617 if (!__str.__is_long())
1618 {
Vedant Kumar55e007e2018-03-08 21:15:26 +00001619 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001620 __alloc() = __str.__alloc();
1621 }
1622 else
1623 {
1624 allocator_type __a = __str.__alloc();
1625 pointer __p = __alloc_traits::allocate(__a, __str.__get_long_cap());
Vedant Kumar55e007e2018-03-08 21:15:26 +00001626 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001627 __alloc() = _VSTD::move(__a);
1628 __set_long_pointer(__p);
1629 __set_long_cap(__str.__get_long_cap());
1630 __set_long_size(__str.size());
1631 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001632 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001633 }
1634
1635 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001636 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001637 {}
1638
Eric Fiselierfc92be82017-04-19 00:28:44 +00001639#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +00001640 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001641 void __move_assign(basic_string& __str, false_type)
1642 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnantcf823322010-12-17 14:46:43 +00001643 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001644 void __move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001645#if _LIBCPP_STD_VER > 14
1646 _NOEXCEPT;
1647#else
Howard Hinnant3e276872011-06-03 18:40:47 +00001648 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001649#endif
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001650#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001651
1652 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001653 void
Howard Hinnantc2734962011-09-02 20:42:31 +00001654 __move_assign_alloc(basic_string& __str)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001655 _NOEXCEPT_(
1656 !__alloc_traits::propagate_on_container_move_assignment::value ||
1657 is_nothrow_move_assignable<allocator_type>::value)
1658 {__move_assign_alloc(__str, integral_constant<bool,
1659 __alloc_traits::propagate_on_container_move_assignment::value>());}
1660
1661 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00001662 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001663 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1664 {
1665 __alloc() = _VSTD::move(__c.__alloc());
1666 }
1667
1668 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001669 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001670 _NOEXCEPT
1671 {}
1672
Martijn Velsda7d94f2020-06-19 14:24:03 -04001673 basic_string& __assign_external(const value_type* __s);
1674 basic_string& __assign_external(const value_type* __s, size_type __n);
1675
1676 // Assigns the value in __s, guaranteed to be __n < __min_cap in length.
1677 inline basic_string& __assign_short(const value_type* __s, size_type __n) {
1678 pointer __p = __is_long()
1679 ? (__set_long_size(__n), __get_long_pointer())
1680 : (__set_short_size(__n), __get_short_pointer());
1681 traits_type::move(_VSTD::__to_address(__p), __s, __n);
1682 traits_type::assign(__p[__n], value_type());
1683 return *this;
1684 }
1685
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01001686 _LIBCPP_HIDE_FROM_ABI basic_string& __null_terminate_at(value_type* __p, size_type __newsz) {
1687 __set_size(__newsz);
1688 __invalidate_iterators_past(__newsz);
1689 traits_type::assign(__p[__newsz], value_type());
1690 return *this;
1691 }
1692
Howard Hinnantcf823322010-12-17 14:46:43 +00001693 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
1694 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(size_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001695
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001696 template<class _Tp>
1697 _LIBCPP_INLINE_VISIBILITY
1698 bool __addr_in_range(_Tp&& __t) const {
1699 const volatile void *__p = _VSTD::addressof(__t);
1700 return data() <= __p && __p <= data() + size();
1701 }
1702
Louis Dionned24191c2021-08-19 12:39:16 -04001703 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1704 void __throw_length_error() const {
Nikolas Klauserf89bb932022-01-24 19:44:34 +01001705 _VSTD::__throw_length_error("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001706 }
1707
1708 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1709 void __throw_out_of_range() const {
Nikolas Klauserf89bb932022-01-24 19:44:34 +01001710 _VSTD::__throw_out_of_range("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001711 }
1712
Howard Hinnantc51e1022010-05-11 19:42:16 +00001713 friend basic_string operator+<>(const basic_string&, const basic_string&);
1714 friend basic_string operator+<>(const value_type*, const basic_string&);
1715 friend basic_string operator+<>(value_type, const basic_string&);
1716 friend basic_string operator+<>(const basic_string&, const value_type*);
1717 friend basic_string operator+<>(const basic_string&, value_type);
1718};
1719
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001720// These declarations must appear before any functions are implicitly used
1721// so that they have the correct visibility specifier.
1722#ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
Louis Dionne89258142021-08-23 15:32:36 -04001723 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1724# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1725 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1726# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001727#else
Louis Dionne89258142021-08-23 15:32:36 -04001728 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1729# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1730 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1731# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001732#endif
1733
1734
Louis Dionned59f8a52021-08-17 11:59:07 -04001735#if _LIBCPP_STD_VER >= 17
Marshall Clowa0563332018-02-08 06:34:03 +00001736template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -05001737 class _CharT = __iter_value_type<_InputIterator>,
Marshall Clowa0563332018-02-08 06:34:03 +00001738 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001739 class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
1740 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowa0563332018-02-08 06:34:03 +00001741 >
1742basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
1743 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
Marshall Clowe46031a2018-07-02 18:41:15 +00001744
1745template<class _CharT,
1746 class _Traits,
1747 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001748 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowe46031a2018-07-02 18:41:15 +00001749 >
1750explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
1751 -> basic_string<_CharT, _Traits, _Allocator>;
1752
1753template<class _CharT,
1754 class _Traits,
1755 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001756 class = enable_if_t<__is_allocator<_Allocator>::value>,
Marshall Clowe46031a2018-07-02 18:41:15 +00001757 class _Sz = typename allocator_traits<_Allocator>::size_type
1758 >
1759basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
1760 -> basic_string<_CharT, _Traits, _Allocator>;
Marshall Clowa0563332018-02-08 06:34:03 +00001761#endif
1762
Howard Hinnantc51e1022010-05-11 19:42:16 +00001763template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001764inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001765void
1766basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
1767{
Louis Dionneba400782020-10-02 15:02:52 -04001768#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001769 if (!__libcpp_is_constant_evaluated())
1770 __get_db()->__invalidate_all(this);
Louis Dionneba400782020-10-02 15:02:52 -04001771#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001772}
1773
1774template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001775inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001776void
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001777basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type __pos)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001778{
Louis Dionneba400782020-10-02 15:02:52 -04001779#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001780 if (!__libcpp_is_constant_evaluated()) {
1781 __c_node* __c = __get_db()->__find_c_and_lock(this);
1782 if (__c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001783 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001784 const_pointer __new_last = __get_pointer() + __pos;
1785 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001786 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001787 --__p;
1788 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1789 if (__i->base() > __new_last)
1790 {
1791 (*__p)->__c_ = nullptr;
1792 if (--__c->end_ != __p)
1793 _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1794 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001795 }
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001796 __get_db()->unlock();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001797 }
1798 }
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001799#else
1800 (void)__pos;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001801#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001802}
1803
1804template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001805inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001806basic_string<_CharT, _Traits, _Allocator>::basic_string()
Marshall Clowe546cbb2015-06-04 02:05:41 +00001807 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001808 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001809{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001810 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001811 __zero();
1812}
1813
1814template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001815inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001816basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
Eric Fiseliere012bf22015-07-18 20:40:46 +00001817#if _LIBCPP_STD_VER <= 14
1818 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1819#else
1820 _NOEXCEPT
1821#endif
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001822: __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001823{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001824 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001825 __zero();
1826}
1827
1828template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier815ed732016-09-16 00:00:48 +00001829void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1830 size_type __sz,
1831 size_type __reserve)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001832{
1833 if (__reserve > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001834 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001835 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001836 if (__fits_in_sso(__reserve))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001837 {
1838 __set_short_size(__sz);
1839 __p = __get_short_pointer();
1840 }
1841 else
1842 {
1843 size_type __cap = __recommend(__reserve);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001844 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001845 __set_long_pointer(__p);
1846 __set_long_cap(__cap+1);
1847 __set_long_size(__sz);
1848 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001849 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001850 traits_type::assign(__p[__sz], value_type());
1851}
1852
1853template <class _CharT, class _Traits, class _Allocator>
1854void
Howard Hinnantd17880b2013-06-28 16:59:19 +00001855basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001856{
1857 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001858 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001859 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001860 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001861 {
1862 __set_short_size(__sz);
1863 __p = __get_short_pointer();
1864 }
1865 else
1866 {
1867 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001868 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001869 __set_long_pointer(__p);
1870 __set_long_cap(__cap+1);
1871 __set_long_size(__sz);
1872 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001873 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001874 traits_type::assign(__p[__sz], value_type());
1875}
1876
1877template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00001878template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00001879basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, 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(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001883 __init(__s, traits_type::length(__s));
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>
Eric Fiselierbea70602018-11-26 22:51:35 +00001888inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001889basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001890 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001891{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001892 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
1893 __init(__s, __n);
1894 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001895}
1896
1897template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001898inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001899basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001900 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001901{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001902 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001903 __init(__s, __n);
Nikolas Klauserf2807732022-01-11 00:33:35 +01001904 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001905}
1906
1907template <class _CharT, class _Traits, class _Allocator>
1908basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001909 : __r_(__default_init_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001910{
1911 if (!__str.__is_long())
1912 __r_.first().__r = __str.__r_.first().__r;
1913 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001914 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1915 __str.__get_long_size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01001916 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001917}
1918
1919template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00001920basic_string<_CharT, _Traits, _Allocator>::basic_string(
1921 const basic_string& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001922 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001923{
1924 if (!__str.__is_long())
1925 __r_.first().__r = __str.__r_.first().__r;
1926 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001927 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1928 __str.__get_long_size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01001929 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001930}
1931
Martijn Vels5e7c9752020-03-04 17:52:46 -05001932template <class _CharT, class _Traits, class _Allocator>
1933void basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(
1934 const value_type* __s, size_type __sz) {
1935 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001936 if (__fits_in_sso(__sz)) {
Martijn Vels5e7c9752020-03-04 17:52:46 -05001937 __p = __get_short_pointer();
1938 __set_short_size(__sz);
1939 } else {
1940 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001941 __throw_length_error();
Martijn Vels5e7c9752020-03-04 17:52:46 -05001942 size_t __cap = __recommend(__sz);
1943 __p = __alloc_traits::allocate(__alloc(), __cap + 1);
1944 __set_long_pointer(__p);
1945 __set_long_cap(__cap + 1);
1946 __set_long_size(__sz);
1947 }
1948 traits_type::copy(_VSTD::__to_address(__p), __s, __sz + 1);
1949}
1950
Eric Fiselierfc92be82017-04-19 00:28:44 +00001951#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001952
1953template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001954inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001955basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +00001956#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +00001957 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowa80abc72015-06-03 19:56:43 +00001958#else
1959 _NOEXCEPT
1960#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001961 : __r_(_VSTD::move(__str.__r_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001962{
1963 __str.__zero();
Nikolas Klauserf2807732022-01-11 00:33:35 +01001964 _VSTD::__debug_db_insert_c(this);
Louis Dionneba400782020-10-02 15:02:52 -04001965#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauserf2807732022-01-11 00:33:35 +01001966 if (!__libcpp_is_constant_evaluated() && __is_long())
1967 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001968#endif
1969}
1970
1971template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001972inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001973basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001974 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001975{
Marshall Clowd2d24692014-07-17 15:32:20 +00001976 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001977 __init(_VSTD::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd2d24692014-07-17 15:32:20 +00001978 else
1979 {
1980 __r_.first().__r = __str.__r_.first().__r;
1981 __str.__zero();
1982 }
Nikolas Klauserf2807732022-01-11 00:33:35 +01001983 _VSTD::__debug_db_insert_c(this);
Louis Dionneba400782020-10-02 15:02:52 -04001984#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauserf2807732022-01-11 00:33:35 +01001985 if (!__libcpp_is_constant_evaluated() && __is_long())
1986 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001987#endif
1988}
1989
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001990#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001991
1992template <class _CharT, class _Traits, class _Allocator>
1993void
1994basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
1995{
1996 if (__n > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001997 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001998 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001999 if (__fits_in_sso(__n))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002000 {
2001 __set_short_size(__n);
2002 __p = __get_short_pointer();
2003 }
2004 else
2005 {
2006 size_type __cap = __recommend(__n);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002007 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002008 __set_long_pointer(__p);
2009 __set_long_cap(__cap+1);
2010 __set_long_size(__n);
2011 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002012 traits_type::assign(_VSTD::__to_address(__p), __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002013 traits_type::assign(__p[__n], value_type());
2014}
2015
2016template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002017inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002018basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002019 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002020{
2021 __init(__n, __c);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002022 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002023}
2024
2025template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002026template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00002027basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002028 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002029{
2030 __init(__n, __c);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002031 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002032}
2033
Howard Hinnantc51e1022010-05-11 19:42:16 +00002034template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00002035basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
2036 size_type __pos, size_type __n,
2037 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002038 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002039{
2040 size_type __str_sz = __str.size();
2041 if (__pos > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002042 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002043 __init(__str.data() + __pos, _VSTD::min(__n, __str_sz - __pos));
Nikolas Klauserf2807732022-01-11 00:33:35 +01002044 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002045}
2046
2047template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002048inline
Marshall Clow83445802016-04-07 18:13:41 +00002049basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +00002050 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002051 : __r_(__default_init_tag(), __a)
Marshall Clow83445802016-04-07 18:13:41 +00002052{
2053 size_type __str_sz = __str.size();
2054 if (__pos > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002055 __throw_out_of_range();
Marshall Clow83445802016-04-07 18:13:41 +00002056 __init(__str.data() + __pos, __str_sz - __pos);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002057 _VSTD::__debug_db_insert_c(this);
Marshall Clow83445802016-04-07 18:13:41 +00002058}
2059
2060template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002061template <class _Tp, class>
Marshall Clow78dbe462016-11-14 18:22:19 +00002062basic_string<_CharT, _Traits, _Allocator>::basic_string(
Marshall Clowe46031a2018-07-02 18:41:15 +00002063 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002064 : __r_(__default_init_tag(), __a)
Marshall Clow78dbe462016-11-14 18:22:19 +00002065{
Marshall Clowe46031a2018-07-02 18:41:15 +00002066 __self_view __sv0 = __t;
2067 __self_view __sv = __sv0.substr(__pos, __n);
Marshall Clow78dbe462016-11-14 18:22:19 +00002068 __init(__sv.data(), __sv.size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002069 _VSTD::__debug_db_insert_c(this);
Marshall Clow78dbe462016-11-14 18:22:19 +00002070}
2071
2072template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002073template <class _Tp, class>
2074basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002075 : __r_(__default_init_tag(), __default_init_tag())
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002076{
Marshall Clowe46031a2018-07-02 18:41:15 +00002077 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002078 __init(__sv.data(), __sv.size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002079 _VSTD::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002080}
2081
2082template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002083template <class _Tp, class>
2084basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002085 : __r_(__default_init_tag(), __a)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002086{
Marshall Clowe46031a2018-07-02 18:41:15 +00002087 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002088 __init(__sv.data(), __sv.size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002089 _VSTD::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002090}
2091
2092template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002093template <class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002094__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002095<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002096 __is_exactly_cpp17_input_iterator<_InputIterator>::value
2097>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002098basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
2099{
2100 __zero();
2101#ifndef _LIBCPP_NO_EXCEPTIONS
2102 try
2103 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002104#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002105 for (; __first != __last; ++__first)
2106 push_back(*__first);
2107#ifndef _LIBCPP_NO_EXCEPTIONS
2108 }
2109 catch (...)
2110 {
2111 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002112 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002113 throw;
2114 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002115#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002116}
2117
2118template <class _CharT, class _Traits, class _Allocator>
2119template <class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002120__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002121<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002122 __is_cpp17_forward_iterator<_ForwardIterator>::value
2123>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002124basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
2125{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002126 size_type __sz = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002127 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002128 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002129 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002130 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002131 {
2132 __set_short_size(__sz);
2133 __p = __get_short_pointer();
2134 }
2135 else
2136 {
2137 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002138 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002139 __set_long_pointer(__p);
2140 __set_long_cap(__cap+1);
2141 __set_long_size(__sz);
2142 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002143
2144#ifndef _LIBCPP_NO_EXCEPTIONS
2145 try
2146 {
2147#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002148 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002149 traits_type::assign(*__p, *__first);
2150 traits_type::assign(*__p, value_type());
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002151#ifndef _LIBCPP_NO_EXCEPTIONS
2152 }
2153 catch (...)
2154 {
2155 if (__is_long())
2156 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
2157 throw;
2158 }
2159#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002160}
2161
2162template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002163template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002164inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002165basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002166 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002167{
2168 __init(__first, __last);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002169 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002170}
2171
2172template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002173template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002174inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002175basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
2176 const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002177 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002178{
2179 __init(__first, __last);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002180 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002181}
2182
Eric Fiselierfc92be82017-04-19 00:28:44 +00002183#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002184
Howard Hinnantc51e1022010-05-11 19:42:16 +00002185template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002186inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002187basic_string<_CharT, _Traits, _Allocator>::basic_string(
2188 initializer_list<_CharT> __il)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002189 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002190{
2191 __init(__il.begin(), __il.end());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002192 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002193}
2194
2195template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002196inline
Eric Fiselier9d355982017-04-12 23:45:53 +00002197
Eric Fiselier812882b2017-02-17 01:17:10 +00002198basic_string<_CharT, _Traits, _Allocator>::basic_string(
2199 initializer_list<_CharT> __il, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002200 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002201{
2202 __init(__il.begin(), __il.end());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002203 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002204}
2205
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002206#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002207
Howard Hinnantc51e1022010-05-11 19:42:16 +00002208template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002209basic_string<_CharT, _Traits, _Allocator>::~basic_string()
2210{
Louis Dionneba400782020-10-02 15:02:52 -04002211#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002212 if (!__libcpp_is_constant_evaluated())
2213 __get_db()->__erase_c(this);
Howard Hinnant8ea98242013-08-23 17:37:05 +00002214#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002215 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002216 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002217}
2218
2219template <class _CharT, class _Traits, class _Allocator>
2220void
2221basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
2222 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002223 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 +00002224{
2225 size_type __ms = max_size();
2226 if (__delta_cap > __ms - __old_cap - 1)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002227 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002228 pointer __old_p = __get_pointer();
2229 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002230 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002231 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002232 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002233 __invalidate_all_iterators();
2234 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002235 traits_type::copy(_VSTD::__to_address(__p),
2236 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002237 if (__n_add != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002238 traits_type::copy(_VSTD::__to_address(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002239 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2240 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002241 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2242 _VSTD::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002243 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002244 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002245 __set_long_pointer(__p);
2246 __set_long_cap(__cap+1);
2247 __old_sz = __n_copy + __n_add + __sec_cp_sz;
2248 __set_long_size(__old_sz);
2249 traits_type::assign(__p[__old_sz], value_type());
2250}
2251
2252template <class _CharT, class _Traits, class _Allocator>
2253void
2254basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2255 size_type __n_copy, size_type __n_del, size_type __n_add)
2256{
2257 size_type __ms = max_size();
Marshall Clow2267c5d2013-11-06 14:24:38 +00002258 if (__delta_cap > __ms - __old_cap)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002259 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002260 pointer __old_p = __get_pointer();
2261 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002262 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002263 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002264 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002265 __invalidate_all_iterators();
2266 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002267 traits_type::copy(_VSTD::__to_address(__p),
2268 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002269 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2270 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002271 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2272 _VSTD::__to_address(__old_p) + __n_copy + __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002273 __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002274 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002275 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002276 __set_long_pointer(__p);
2277 __set_long_cap(__cap+1);
2278}
2279
2280// assign
2281
2282template <class _CharT, class _Traits, class _Allocator>
Martijn Vels596e3de2020-02-26 15:55:49 -05002283template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04002284basic_string<_CharT, _Traits, _Allocator>&
2285basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(
Martijn Vels596e3de2020-02-26 15:55:49 -05002286 const value_type* __s, size_type __n) {
Louis Dionne6209e9f2022-03-03 13:39:12 -05002287 size_type __cap = __is_short ? static_cast<size_type>(__min_cap) : __get_long_cap();
Martijn Vels596e3de2020-02-26 15:55:49 -05002288 if (__n < __cap) {
2289 pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
2290 __is_short ? __set_short_size(__n) : __set_long_size(__n);
2291 traits_type::copy(_VSTD::__to_address(__p), __s, __n);
2292 traits_type::assign(__p[__n], value_type());
2293 __invalidate_iterators_past(__n);
2294 } else {
2295 size_type __sz = __is_short ? __get_short_size() : __get_long_size();
2296 __grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
2297 }
Martijn Velsb6a08b62020-04-10 18:36:31 -04002298 return *this;
Martijn Vels596e3de2020-02-26 15:55:49 -05002299}
2300
2301template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002302basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002303basic_string<_CharT, _Traits, _Allocator>::__assign_external(
2304 const value_type* __s, size_type __n) {
2305 size_type __cap = capacity();
2306 if (__cap >= __n) {
2307 value_type* __p = _VSTD::__to_address(__get_pointer());
2308 traits_type::move(__p, __s, __n);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002309 return __null_terminate_at(__p, __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04002310 } else {
2311 size_type __sz = size();
2312 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002313 return *this;
Martijn Velsda7d94f2020-06-19 14:24:03 -04002314 }
Martijn Velsda7d94f2020-06-19 14:24:03 -04002315}
2316
2317template <class _CharT, class _Traits, class _Allocator>
2318basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002319basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002320{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002321 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Nikolas Klauser93826d12022-01-04 17:24:03 +01002322 return (__builtin_constant_p(__n) && __fits_in_sso(__n))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002323 ? __assign_short(__s, __n)
2324 : __assign_external(__s, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002325}
2326
2327template <class _CharT, class _Traits, class _Allocator>
2328basic_string<_CharT, _Traits, _Allocator>&
2329basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2330{
2331 size_type __cap = capacity();
2332 if (__cap < __n)
2333 {
2334 size_type __sz = size();
2335 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2336 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002337 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002338 traits_type::assign(__p, __n, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002339 return __null_terminate_at(__p, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002340}
2341
2342template <class _CharT, class _Traits, class _Allocator>
2343basic_string<_CharT, _Traits, _Allocator>&
2344basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2345{
2346 pointer __p;
2347 if (__is_long())
2348 {
2349 __p = __get_long_pointer();
2350 __set_long_size(1);
2351 }
2352 else
2353 {
2354 __p = __get_short_pointer();
2355 __set_short_size(1);
2356 }
2357 traits_type::assign(*__p, __c);
2358 traits_type::assign(*++__p, value_type());
2359 __invalidate_iterators_past(1);
2360 return *this;
2361}
2362
2363template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002364basic_string<_CharT, _Traits, _Allocator>&
2365basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2366{
Martijn Vels596e3de2020-02-26 15:55:49 -05002367 if (this != &__str) {
2368 __copy_assign_alloc(__str);
2369 if (!__is_long()) {
2370 if (!__str.__is_long()) {
Eric Fiselier03bbc922020-01-15 17:27:10 -05002371 __r_.first().__r = __str.__r_.first().__r;
Martijn Vels596e3de2020-02-26 15:55:49 -05002372 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002373 return __assign_no_alias<true>(__str.data(), __str.size());
Martijn Vels596e3de2020-02-26 15:55:49 -05002374 }
2375 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002376 return __assign_no_alias<false>(__str.data(), __str.size());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002377 }
Martijn Vels596e3de2020-02-26 15:55:49 -05002378 }
2379 return *this;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002380}
2381
Eric Fiselierfc92be82017-04-19 00:28:44 +00002382#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002383
2384template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002385inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002386void
2387basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002388 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002389{
2390 if (__alloc() != __str.__alloc())
2391 assign(__str);
2392 else
2393 __move_assign(__str, true_type());
2394}
2395
2396template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002397inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002398void
2399basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002400#if _LIBCPP_STD_VER > 14
2401 _NOEXCEPT
2402#else
Howard Hinnant3e276872011-06-03 18:40:47 +00002403 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002404#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002405{
marshall2ed41622019-11-27 07:13:00 -08002406 if (__is_long()) {
2407 __alloc_traits::deallocate(__alloc(), __get_long_pointer(),
2408 __get_long_cap());
2409#if _LIBCPP_STD_VER <= 14
2410 if (!is_nothrow_move_assignable<allocator_type>::value) {
2411 __set_short_size(0);
2412 traits_type::assign(__get_short_pointer()[0], value_type());
2413 }
2414#endif
2415 }
2416 __move_assign_alloc(__str);
2417 __r_.first() = __str.__r_.first();
2418 __str.__set_short_size(0);
2419 traits_type::assign(__str.__get_short_pointer()[0], value_type());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002420}
2421
2422template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002423inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002424basic_string<_CharT, _Traits, _Allocator>&
2425basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002426 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002427{
2428 __move_assign(__str, integral_constant<bool,
2429 __alloc_traits::propagate_on_container_move_assignment::value>());
2430 return *this;
2431}
2432
2433#endif
2434
2435template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002436template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002437__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002438<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002439 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002440 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002441>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002442basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2443{
Marshall Clow958362f2016-09-05 01:54:30 +00002444 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002445 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002446 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002447}
2448
2449template <class _CharT, class _Traits, class _Allocator>
2450template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002451__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002452<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002453 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002454 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002455>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002456basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2457{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002458 size_type __cap = capacity();
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002459 size_type __n = __string_is_trivial_iterator<_ForwardIterator>::value ?
2460 static_cast<size_type>(_VSTD::distance(__first, __last)) : 0;
2461
2462 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2463 (__cap >= __n || !__addr_in_range(*__first)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002464 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002465 if (__cap < __n)
2466 {
2467 size_type __sz = size();
2468 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2469 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002470 pointer __p = __get_pointer();
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002471 for (; __first != __last; ++__p, (void) ++__first)
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002472 traits_type::assign(*__p, *__first);
2473 traits_type::assign(*__p, value_type());
2474 __set_size(__n);
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04002475 __invalidate_iterators_past(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002476 }
2477 else
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002478 {
2479 const basic_string __temp(__first, __last, __alloc());
2480 assign(__temp.data(), __temp.size());
2481 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002482 return *this;
2483}
2484
2485template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002486basic_string<_CharT, _Traits, _Allocator>&
2487basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2488{
2489 size_type __sz = __str.size();
2490 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002491 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002492 return assign(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002493}
2494
2495template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002496template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002497__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002498<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002499 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
2500 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00002501 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002502>
Marshall Clow82513342016-09-24 22:45:42 +00002503basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002504{
Marshall Clow82513342016-09-24 22:45:42 +00002505 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002506 size_type __sz = __sv.size();
2507 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002508 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002509 return assign(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2510}
2511
2512
2513template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002514basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002515basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s) {
2516 return __assign_external(__s, traits_type::length(__s));
2517}
2518
2519template <class _CharT, class _Traits, class _Allocator>
2520basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002521basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002522{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002523 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Louis Dionne3e0c4332021-08-31 10:49:06 -04002524 return __builtin_constant_p(*__s)
Nikolas Klauser93826d12022-01-04 17:24:03 +01002525 ? (__fits_in_sso(traits_type::length(__s))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002526 ? __assign_short(__s, traits_type::length(__s))
2527 : __assign_external(__s, traits_type::length(__s)))
2528 : __assign_external(__s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002529}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002530// append
2531
2532template <class _CharT, class _Traits, class _Allocator>
2533basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002534basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002535{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002536 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002537 size_type __cap = capacity();
2538 size_type __sz = size();
2539 if (__cap - __sz >= __n)
2540 {
2541 if (__n)
2542 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002543 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002544 traits_type::copy(__p + __sz, __s, __n);
2545 __sz += __n;
2546 __set_size(__sz);
2547 traits_type::assign(__p[__sz], value_type());
2548 }
2549 }
2550 else
2551 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2552 return *this;
2553}
2554
2555template <class _CharT, class _Traits, class _Allocator>
2556basic_string<_CharT, _Traits, _Allocator>&
2557basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2558{
2559 if (__n)
2560 {
2561 size_type __cap = capacity();
2562 size_type __sz = size();
2563 if (__cap - __sz < __n)
2564 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2565 pointer __p = __get_pointer();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002566 traits_type::assign(_VSTD::__to_address(__p) + __sz, __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002567 __sz += __n;
2568 __set_size(__sz);
2569 traits_type::assign(__p[__sz], value_type());
2570 }
2571 return *this;
2572}
2573
2574template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00002575inline void
2576basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n)
2577{
2578 if (__n)
2579 {
2580 size_type __cap = capacity();
2581 size_type __sz = size();
2582 if (__cap - __sz < __n)
2583 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2584 pointer __p = __get_pointer();
2585 __sz += __n;
2586 __set_size(__sz);
2587 traits_type::assign(__p[__sz], value_type());
2588 }
2589}
2590
2591template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002592void
2593basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2594{
Howard Hinnant68bf1812013-04-30 21:44:48 +00002595 bool __is_short = !__is_long();
2596 size_type __cap;
2597 size_type __sz;
2598 if (__is_short)
2599 {
2600 __cap = __min_cap - 1;
2601 __sz = __get_short_size();
2602 }
2603 else
2604 {
2605 __cap = __get_long_cap() - 1;
2606 __sz = __get_long_size();
2607 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002608 if (__sz == __cap)
Howard Hinnant68bf1812013-04-30 21:44:48 +00002609 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002610 __grow_by(__cap, 1, __sz, __sz, 0);
Louis Dionne82d2b2c2021-11-16 11:26:56 -05002611 __is_short = false; // the string is always long after __grow_by
Howard Hinnant68bf1812013-04-30 21:44:48 +00002612 }
2613 pointer __p;
2614 if (__is_short)
2615 {
2616 __p = __get_short_pointer() + __sz;
2617 __set_short_size(__sz+1);
2618 }
2619 else
2620 {
2621 __p = __get_long_pointer() + __sz;
2622 __set_long_size(__sz+1);
2623 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002624 traits_type::assign(*__p, __c);
2625 traits_type::assign(*++__p, value_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002626}
2627
Howard Hinnantc51e1022010-05-11 19:42:16 +00002628template <class _CharT, class _Traits, class _Allocator>
2629template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002630__enable_if_t
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002631<
2632 __is_cpp17_forward_iterator<_ForwardIterator>::value,
2633 basic_string<_CharT, _Traits, _Allocator>&
2634>
2635basic_string<_CharT, _Traits, _Allocator>::append(
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002636 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002637{
2638 size_type __sz = size();
2639 size_type __cap = capacity();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002640 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002641 if (__n)
2642 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002643 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2644 !__addr_in_range(*__first))
Marshall Clow958362f2016-09-05 01:54:30 +00002645 {
2646 if (__cap - __sz < __n)
2647 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2648 pointer __p = __get_pointer() + __sz;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002649 for (; __first != __last; ++__p, (void) ++__first)
Marshall Clow958362f2016-09-05 01:54:30 +00002650 traits_type::assign(*__p, *__first);
2651 traits_type::assign(*__p, value_type());
2652 __set_size(__sz + __n);
2653 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002654 else
2655 {
2656 const basic_string __temp(__first, __last, __alloc());
2657 append(__temp.data(), __temp.size());
2658 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002659 }
2660 return *this;
2661}
2662
2663template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002664inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002665basic_string<_CharT, _Traits, _Allocator>&
2666basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2667{
2668 return append(__str.data(), __str.size());
2669}
2670
2671template <class _CharT, class _Traits, class _Allocator>
2672basic_string<_CharT, _Traits, _Allocator>&
2673basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2674{
2675 size_type __sz = __str.size();
2676 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002677 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002678 return append(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002679}
2680
2681template <class _CharT, class _Traits, class _Allocator>
Marshall Clow62953962016-10-03 23:40:48 +00002682template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002683 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002684 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002685 __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 +00002686 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002687 >
Marshall Clow82513342016-09-24 22:45:42 +00002688basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002689{
Marshall Clow82513342016-09-24 22:45:42 +00002690 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002691 size_type __sz = __sv.size();
2692 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002693 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002694 return append(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2695}
2696
2697template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002698basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002699basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002700{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002701 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002702 return append(__s, traits_type::length(__s));
2703}
2704
2705// insert
2706
2707template <class _CharT, class _Traits, class _Allocator>
2708basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002709basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002710{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002711 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002712 size_type __sz = size();
2713 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002714 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002715 size_type __cap = capacity();
2716 if (__cap - __sz >= __n)
2717 {
2718 if (__n)
2719 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002720 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002721 size_type __n_move = __sz - __pos;
2722 if (__n_move != 0)
2723 {
2724 if (__p + __pos <= __s && __s < __p + __sz)
2725 __s += __n;
2726 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2727 }
2728 traits_type::move(__p + __pos, __s, __n);
2729 __sz += __n;
2730 __set_size(__sz);
2731 traits_type::assign(__p[__sz], value_type());
2732 }
2733 }
2734 else
2735 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2736 return *this;
2737}
2738
2739template <class _CharT, class _Traits, class _Allocator>
2740basic_string<_CharT, _Traits, _Allocator>&
2741basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2742{
2743 size_type __sz = size();
2744 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002745 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002746 if (__n)
2747 {
2748 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002749 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002750 if (__cap - __sz >= __n)
2751 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002752 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002753 size_type __n_move = __sz - __pos;
2754 if (__n_move != 0)
2755 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2756 }
2757 else
2758 {
2759 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002760 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002761 }
2762 traits_type::assign(__p + __pos, __n, __c);
2763 __sz += __n;
2764 __set_size(__sz);
2765 traits_type::assign(__p[__sz], value_type());
2766 }
2767 return *this;
2768}
2769
2770template <class _CharT, class _Traits, class _Allocator>
2771template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002772__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002773<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002774 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Marshall Clow039b2f02016-01-13 21:54:34 +00002775 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002776>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002777basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2778{
Nikolas Klausereed25832021-12-15 01:32:30 +01002779 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2780 "string::insert(iterator, range) called with an iterator not"
2781 " referring to this string");
2782 const basic_string __temp(__first, __last, __alloc());
2783 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002784}
2785
2786template <class _CharT, class _Traits, class _Allocator>
2787template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002788__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002789<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002790 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002791 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002792>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002793basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2794{
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002795 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2796 "string::insert(iterator, range) called with an iterator not referring to this string");
Nikolas Klausereed25832021-12-15 01:32:30 +01002797
Howard Hinnantc51e1022010-05-11 19:42:16 +00002798 size_type __ip = static_cast<size_type>(__pos - begin());
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002799 size_type __n = static_cast<size_type>(std::distance(__first, __last));
2800 if (__n == 0)
2801 return begin() + __ip;
2802
2803 if (__string_is_trivial_iterator<_ForwardIterator>::value && !__addr_in_range(*__first))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002804 {
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002805 return __insert_from_safe_copy(__n, __ip, __first, __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002806 }
Nikolas Klauser48d680d2022-02-26 13:28:33 +01002807 else
2808 {
2809 const basic_string __temp(__first, __last, __alloc());
2810 return __insert_from_safe_copy(__n, __ip, __temp.begin(), __temp.end());
2811 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002812}
2813
2814template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002815inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002816basic_string<_CharT, _Traits, _Allocator>&
2817basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2818{
2819 return insert(__pos1, __str.data(), __str.size());
2820}
2821
2822template <class _CharT, class _Traits, class _Allocator>
2823basic_string<_CharT, _Traits, _Allocator>&
2824basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2825 size_type __pos2, size_type __n)
2826{
2827 size_type __str_sz = __str.size();
2828 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002829 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002830 return insert(__pos1, __str.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002831}
2832
2833template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002834template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002835__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002836<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002837 __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 +00002838 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002839>
Marshall Clow82513342016-09-24 22:45:42 +00002840basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002841 size_type __pos2, size_type __n)
2842{
Marshall Clow82513342016-09-24 22:45:42 +00002843 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002844 size_type __str_sz = __sv.size();
2845 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002846 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002847 return insert(__pos1, __sv.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
2848}
2849
2850template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002851basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002852basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002853{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002854 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002855 return insert(__pos, __s, traits_type::length(__s));
2856}
2857
2858template <class _CharT, class _Traits, class _Allocator>
2859typename basic_string<_CharT, _Traits, _Allocator>::iterator
2860basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2861{
Louis Dionnef4cc6fb2022-02-15 15:47:45 -05002862 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2863 "string::insert(iterator, character) called with an iterator not"
2864 " referring to this string");
2865
Howard Hinnantc51e1022010-05-11 19:42:16 +00002866 size_type __ip = static_cast<size_type>(__pos - begin());
2867 size_type __sz = size();
2868 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002869 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002870 if (__cap == __sz)
2871 {
2872 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002873 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002874 }
2875 else
2876 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002877 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002878 size_type __n_move = __sz - __ip;
2879 if (__n_move != 0)
2880 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2881 }
2882 traits_type::assign(__p[__ip], __c);
2883 traits_type::assign(__p[++__sz], value_type());
2884 __set_size(__sz);
2885 return begin() + static_cast<difference_type>(__ip);
2886}
2887
2888template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002889inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002890typename basic_string<_CharT, _Traits, _Allocator>::iterator
2891basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2892{
Nikolas Klausereed25832021-12-15 01:32:30 +01002893 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2894 "string::insert(iterator, n, value) called with an iterator not"
2895 " referring to this string");
2896 difference_type __p = __pos - begin();
2897 insert(static_cast<size_type>(__p), __n, __c);
2898 return begin() + __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002899}
2900
2901// replace
2902
2903template <class _CharT, class _Traits, class _Allocator>
2904basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002905basic_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 +00002906 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00002907{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002908 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002909 size_type __sz = size();
2910 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002911 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002912 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002913 size_type __cap = capacity();
2914 if (__cap - __sz + __n1 >= __n2)
2915 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002916 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002917 if (__n1 != __n2)
2918 {
2919 size_type __n_move = __sz - __pos - __n1;
2920 if (__n_move != 0)
2921 {
2922 if (__n1 > __n2)
2923 {
2924 traits_type::move(__p + __pos, __s, __n2);
2925 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002926 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002927 }
2928 if (__p + __pos < __s && __s < __p + __sz)
2929 {
2930 if (__p + __pos + __n1 <= __s)
2931 __s += __n2 - __n1;
2932 else // __p + __pos < __s < __p + __pos + __n1
2933 {
2934 traits_type::move(__p + __pos, __s, __n1);
2935 __pos += __n1;
2936 __s += __n2;
2937 __n2 -= __n1;
2938 __n1 = 0;
2939 }
2940 }
2941 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2942 }
2943 }
2944 traits_type::move(__p + __pos, __s, __n2);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002945 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002946 }
2947 else
2948 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
2949 return *this;
2950}
2951
2952template <class _CharT, class _Traits, class _Allocator>
2953basic_string<_CharT, _Traits, _Allocator>&
2954basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
2955{
2956 size_type __sz = size();
2957 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002958 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002959 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002960 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002961 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002962 if (__cap - __sz + __n1 >= __n2)
2963 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002964 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002965 if (__n1 != __n2)
2966 {
2967 size_type __n_move = __sz - __pos - __n1;
2968 if (__n_move != 0)
2969 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2970 }
2971 }
2972 else
2973 {
2974 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002975 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002976 }
2977 traits_type::assign(__p + __pos, __n2, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002978 return __null_terminate_at(__p, __sz - (__n1 - __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002979}
2980
2981template <class _CharT, class _Traits, class _Allocator>
2982template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002983__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002984<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002985 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002986 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002987>
Howard Hinnant990d6e82010-11-17 21:11:40 +00002988basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002989 _InputIterator __j1, _InputIterator __j2)
2990{
Marshall Clow958362f2016-09-05 01:54:30 +00002991 const basic_string __temp(__j1, __j2, __alloc());
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002992 return replace(__i1, __i2, __temp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002993}
2994
2995template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002996inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002997basic_string<_CharT, _Traits, _Allocator>&
2998basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
2999{
3000 return replace(__pos1, __n1, __str.data(), __str.size());
3001}
3002
3003template <class _CharT, class _Traits, class _Allocator>
3004basic_string<_CharT, _Traits, _Allocator>&
3005basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
3006 size_type __pos2, size_type __n2)
3007{
3008 size_type __str_sz = __str.size();
3009 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003010 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003011 return replace(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003012}
3013
3014template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003015template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003016__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003017<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003018 __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 +00003019 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003020>
Marshall Clow82513342016-09-24 22:45:42 +00003021basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003022 size_type __pos2, size_type __n2)
3023{
Marshall Clow82513342016-09-24 22:45:42 +00003024 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003025 size_type __str_sz = __sv.size();
3026 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003027 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003028 return replace(__pos1, __n1, __sv.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
3029}
3030
3031template <class _CharT, class _Traits, class _Allocator>
3032basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003033basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003034{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003035 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003036 return replace(__pos, __n1, __s, traits_type::length(__s));
3037}
3038
3039template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003040inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003041basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003042basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003043{
3044 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
3045 __str.data(), __str.size());
3046}
3047
3048template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003049inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003050basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003051basic_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 +00003052{
3053 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
3054}
3055
3056template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003057inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003058basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003059basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003060{
3061 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
3062}
3063
3064template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003065inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003066basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003067basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003068{
3069 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
3070}
3071
3072// erase
3073
Martijn Velsa81fc792020-02-26 13:25:43 -05003074// 'externally instantiated' erase() implementation, called when __n != npos.
3075// Does not check __pos against size()
Howard Hinnantc51e1022010-05-11 19:42:16 +00003076template <class _CharT, class _Traits, class _Allocator>
Martijn Velsa81fc792020-02-26 13:25:43 -05003077void
3078basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(
3079 size_type __pos, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003080{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003081 if (__n)
3082 {
Martijn Velsa81fc792020-02-26 13:25:43 -05003083 size_type __sz = size();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003084 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003085 __n = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003086 size_type __n_move = __sz - __pos - __n;
3087 if (__n_move != 0)
3088 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003089 __null_terminate_at(__p, __sz - __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003090 }
Martijn Velsa81fc792020-02-26 13:25:43 -05003091}
3092
3093template <class _CharT, class _Traits, class _Allocator>
3094basic_string<_CharT, _Traits, _Allocator>&
3095basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos,
3096 size_type __n) {
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003097 if (__pos > size())
3098 __throw_out_of_range();
Martijn Velsa81fc792020-02-26 13:25:43 -05003099 if (__n == npos) {
3100 __erase_to_end(__pos);
3101 } else {
3102 __erase_external_with_move(__pos, __n);
3103 }
3104 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003105}
3106
3107template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003108inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003109typename basic_string<_CharT, _Traits, _Allocator>::iterator
3110basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
3111{
Nikolas Klausereed25832021-12-15 01:32:30 +01003112 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3113 "string::erase(iterator) called with an iterator not"
3114 " referring to this string");
3115
3116 _LIBCPP_ASSERT(__pos != end(), "string::erase(iterator) called with a non-dereferenceable iterator");
3117 iterator __b = begin();
3118 size_type __r = static_cast<size_type>(__pos - __b);
3119 erase(__r, 1);
3120 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003121}
3122
3123template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003124inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003125typename basic_string<_CharT, _Traits, _Allocator>::iterator
3126basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
3127{
Nikolas Klausereed25832021-12-15 01:32:30 +01003128 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
3129 "string::erase(iterator, iterator) called with an iterator not"
3130 " referring to this string");
3131
3132 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
3133 iterator __b = begin();
3134 size_type __r = static_cast<size_type>(__first - __b);
3135 erase(__r, static_cast<size_type>(__last - __first));
3136 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003137}
3138
3139template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003140inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003141void
3142basic_string<_CharT, _Traits, _Allocator>::pop_back()
3143{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003144 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003145 __erase_to_end(size() - 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003146}
3147
3148template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003149inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003150void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003151basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003152{
3153 __invalidate_all_iterators();
3154 if (__is_long())
3155 {
3156 traits_type::assign(*__get_long_pointer(), value_type());
3157 __set_long_size(0);
3158 }
3159 else
3160 {
3161 traits_type::assign(*__get_short_pointer(), value_type());
3162 __set_short_size(0);
3163 }
3164}
3165
3166template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003167inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003168void
3169basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
3170{
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003171 __null_terminate_at(_VSTD::__to_address(__get_pointer()), __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003172}
3173
3174template <class _CharT, class _Traits, class _Allocator>
3175void
3176basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
3177{
3178 size_type __sz = size();
3179 if (__n > __sz)
3180 append(__n - __sz, __c);
3181 else
3182 __erase_to_end(__n);
3183}
3184
3185template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00003186inline void
3187basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n)
3188{
3189 size_type __sz = size();
3190 if (__n > __sz) {
3191 __append_default_init(__n - __sz);
3192 } else
3193 __erase_to_end(__n);
3194}
3195
3196template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003197inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003198typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003199basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003200{
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003201 size_type __m = __alloc_traits::max_size(__alloc());
Eric Fiseliere9cc5922017-10-17 13:16:01 +00003202#ifdef _LIBCPP_BIG_ENDIAN
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003203 return (__m <= ~__long_mask ? __m : __m/2) - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003204#else
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003205 return __m - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003206#endif
3207}
3208
3209template <class _CharT, class _Traits, class _Allocator>
3210void
Marek Kurdejc9848142020-11-26 10:07:16 +01003211basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __requested_capacity)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003212{
Marek Kurdejc9848142020-11-26 10:07:16 +01003213 if (__requested_capacity > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003214 __throw_length_error();
Marek Kurdejc9848142020-11-26 10:07:16 +01003215
Louis Dionne05b6d3e2022-01-14 12:30:22 -05003216 // Make sure reserve(n) never shrinks. This is technically only required in C++20
3217 // and later (since P0966R1), however we provide consistent behavior in all Standard
3218 // modes because this function is instantiated in the shared library.
3219 if (__requested_capacity <= capacity())
3220 return;
Marek Kurdejc9848142020-11-26 10:07:16 +01003221
3222 size_type __target_capacity = _VSTD::max(__requested_capacity, size());
3223 __target_capacity = __recommend(__target_capacity);
3224 if (__target_capacity == capacity()) return;
3225
3226 __shrink_or_extend(__target_capacity);
3227}
3228
3229template <class _CharT, class _Traits, class _Allocator>
Louis Dionnee38b8642021-12-13 14:15:21 -05003230inline
Marek Kurdejc9848142020-11-26 10:07:16 +01003231void
3232basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT
3233{
3234 size_type __target_capacity = __recommend(size());
3235 if (__target_capacity == capacity()) return;
3236
3237 __shrink_or_extend(__target_capacity);
3238}
3239
3240template <class _CharT, class _Traits, class _Allocator>
Louis Dionnee38b8642021-12-13 14:15:21 -05003241inline
Marek Kurdejc9848142020-11-26 10:07:16 +01003242void
3243basic_string<_CharT, _Traits, _Allocator>::__shrink_or_extend(size_type __target_capacity)
3244{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003245 size_type __cap = capacity();
3246 size_type __sz = size();
Marek Kurdejc9848142020-11-26 10:07:16 +01003247
3248 pointer __new_data, __p;
3249 bool __was_long, __now_long;
3250 if (__target_capacity == __min_cap - 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003251 {
Marek Kurdejc9848142020-11-26 10:07:16 +01003252 __was_long = true;
3253 __now_long = false;
3254 __new_data = __get_short_pointer();
3255 __p = __get_long_pointer();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003256 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003257 else
3258 {
3259 if (__target_capacity > __cap)
3260 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3261 else
3262 {
3263 #ifndef _LIBCPP_NO_EXCEPTIONS
3264 try
3265 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003266 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003267 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3268 #ifndef _LIBCPP_NO_EXCEPTIONS
3269 }
3270 catch (...)
3271 {
3272 return;
3273 }
3274 #else // _LIBCPP_NO_EXCEPTIONS
3275 if (__new_data == nullptr)
3276 return;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003277 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003278 }
3279 __now_long = true;
3280 __was_long = __is_long();
3281 __p = __get_pointer();
3282 }
3283 traits_type::copy(_VSTD::__to_address(__new_data),
3284 _VSTD::__to_address(__p), size()+1);
3285 if (__was_long)
3286 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
3287 if (__now_long)
3288 {
3289 __set_long_cap(__target_capacity+1);
3290 __set_long_size(__sz);
3291 __set_long_pointer(__new_data);
3292 }
3293 else
3294 __set_short_size(__sz);
3295 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003296}
3297
3298template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003299inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003300typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003301basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003302{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003303 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003304 return *(data() + __pos);
3305}
3306
3307template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003308inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003309typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003310basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003311{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003312 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003313 return *(__get_pointer() + __pos);
3314}
3315
3316template <class _CharT, class _Traits, class _Allocator>
3317typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3318basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3319{
3320 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003321 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003322 return (*this)[__n];
3323}
3324
3325template <class _CharT, class _Traits, class _Allocator>
3326typename basic_string<_CharT, _Traits, _Allocator>::reference
3327basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
3328{
3329 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003330 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003331 return (*this)[__n];
3332}
3333
3334template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003335inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003336typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003337basic_string<_CharT, _Traits, _Allocator>::front() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003338{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003339 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003340 return *__get_pointer();
3341}
3342
3343template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003344inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003345typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003346basic_string<_CharT, _Traits, _Allocator>::front() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003347{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003348 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003349 return *data();
3350}
3351
3352template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003353inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003354typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003355basic_string<_CharT, _Traits, _Allocator>::back() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003356{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003357 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003358 return *(__get_pointer() + size() - 1);
3359}
3360
3361template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003362inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003363typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003364basic_string<_CharT, _Traits, _Allocator>::back() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003365{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003366 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003367 return *(data() + size() - 1);
3368}
3369
3370template <class _CharT, class _Traits, class _Allocator>
3371typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003372basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003373{
3374 size_type __sz = size();
3375 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003376 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003377 size_type __rlen = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003378 traits_type::copy(__s, data() + __pos, __rlen);
3379 return __rlen;
3380}
3381
3382template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003383inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003384basic_string<_CharT, _Traits, _Allocator>
3385basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3386{
3387 return basic_string(*this, __pos, __n, __alloc());
3388}
3389
3390template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003391inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003392void
3393basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003394#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00003395 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00003396#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00003397 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003398 __is_nothrow_swappable<allocator_type>::value)
3399#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003400{
Louis Dionneba400782020-10-02 15:02:52 -04003401#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01003402 if (!__libcpp_is_constant_evaluated()) {
3403 if (!__is_long())
3404 __get_db()->__invalidate_all(this);
3405 if (!__str.__is_long())
3406 __get_db()->__invalidate_all(&__str);
3407 __get_db()->swap(this, &__str);
3408 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00003409#endif
Eric Fiselier9bf691f2016-12-28 05:53:01 +00003410 _LIBCPP_ASSERT(
3411 __alloc_traits::propagate_on_container_swap::value ||
3412 __alloc_traits::is_always_equal::value ||
3413 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003414 _VSTD::swap(__r_.first(), __str.__r_.first());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05003415 _VSTD::__swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003416}
3417
3418// find
3419
3420template <class _Traits>
3421struct _LIBCPP_HIDDEN __traits_eq
3422{
3423 typedef typename _Traits::char_type char_type;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003424 _LIBCPP_INLINE_VISIBILITY
3425 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3426 {return _Traits::eq(__x, __y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003427};
3428
3429template<class _CharT, class _Traits, class _Allocator>
3430typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003431basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003432 size_type __pos,
3433 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003434{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003435 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003436 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003437 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003438}
3439
3440template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003441inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003442typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003443basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3444 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003445{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003446 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003447 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003448}
3449
3450template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003451template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003452__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003453<
3454 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3455 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003456>
Marshall Clowe46031a2018-07-02 18:41:15 +00003457basic_string<_CharT, _Traits, _Allocator>::find(const _Tp &__t,
zoecarver1997e0a2021-02-05 11:54:47 -08003458 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003459{
Marshall Clowe46031a2018-07-02 18:41:15 +00003460 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003461 return __str_find<value_type, size_type, traits_type, npos>
3462 (data(), size(), __sv.data(), __pos, __sv.size());
3463}
3464
3465template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003466inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003467typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003468basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003469 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003470{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003471 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003472 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003473 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003474}
3475
3476template<class _CharT, class _Traits, class _Allocator>
3477typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003478basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3479 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003480{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003481 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003482 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003483}
3484
3485// rfind
3486
3487template<class _CharT, class _Traits, class _Allocator>
3488typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003489basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003490 size_type __pos,
3491 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003492{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003493 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003494 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003495 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003496}
3497
3498template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003499inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003500typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003501basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3502 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003503{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003504 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003505 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003506}
3507
3508template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003509template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003510__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003511<
3512 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3513 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003514>
Marshall Clowe46031a2018-07-02 18:41:15 +00003515basic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003516 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003517{
Marshall Clowe46031a2018-07-02 18:41:15 +00003518 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003519 return __str_rfind<value_type, size_type, traits_type, npos>
3520 (data(), size(), __sv.data(), __pos, __sv.size());
3521}
3522
3523template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003524inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003525typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003526basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003527 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003528{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003529 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003530 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003531 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003532}
3533
3534template<class _CharT, class _Traits, class _Allocator>
3535typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003536basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3537 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003538{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003539 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003540 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003541}
3542
3543// find_first_of
3544
3545template<class _CharT, class _Traits, class _Allocator>
3546typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003547basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003548 size_type __pos,
3549 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003550{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003551 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003552 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003553 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003554}
3555
3556template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003557inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003558typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003559basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3560 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003561{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003562 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003563 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003564}
3565
3566template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003567template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003568__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003569<
3570 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3571 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003572>
Marshall Clowe46031a2018-07-02 18:41:15 +00003573basic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003574 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003575{
Marshall Clowe46031a2018-07-02 18:41:15 +00003576 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003577 return __str_find_first_of<value_type, size_type, traits_type, npos>
3578 (data(), size(), __sv.data(), __pos, __sv.size());
3579}
3580
3581template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003582inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003583typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003584basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003585 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003586{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003587 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003588 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003589 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003590}
3591
3592template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003593inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003594typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003595basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3596 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003597{
3598 return find(__c, __pos);
3599}
3600
3601// find_last_of
3602
3603template<class _CharT, class _Traits, class _Allocator>
3604typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003605basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003606 size_type __pos,
3607 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003608{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003609 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003610 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003611 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003612}
3613
3614template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003615inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003616typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003617basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3618 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003619{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003620 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003621 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003622}
3623
3624template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003625template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003626__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003627<
3628 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3629 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003630>
Marshall Clowe46031a2018-07-02 18:41:15 +00003631basic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003632 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003633{
Marshall Clowe46031a2018-07-02 18:41:15 +00003634 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003635 return __str_find_last_of<value_type, size_type, traits_type, npos>
3636 (data(), size(), __sv.data(), __pos, __sv.size());
3637}
3638
3639template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003640inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003641typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003642basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003643 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003644{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003645 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003646 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003647 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003648}
3649
3650template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003651inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003652typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003653basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3654 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003655{
3656 return rfind(__c, __pos);
3657}
3658
3659// find_first_not_of
3660
3661template<class _CharT, class _Traits, class _Allocator>
3662typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003663basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003664 size_type __pos,
3665 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003666{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003667 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003668 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003669 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003670}
3671
3672template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003673inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003674typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003675basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3676 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003677{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003678 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003679 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003680}
3681
3682template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003683template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003684__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003685<
3686 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3687 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003688>
Marshall Clowe46031a2018-07-02 18:41:15 +00003689basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003690 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003691{
Marshall Clowe46031a2018-07-02 18:41:15 +00003692 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003693 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3694 (data(), size(), __sv.data(), __pos, __sv.size());
3695}
3696
3697template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003698inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003699typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003700basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003701 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003702{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003703 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003704 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003705 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003706}
3707
3708template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003709inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003710typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003711basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3712 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003713{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003714 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003715 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003716}
3717
3718// find_last_not_of
3719
3720template<class _CharT, class _Traits, class _Allocator>
3721typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003722basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003723 size_type __pos,
3724 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003725{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003726 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003727 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003728 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003729}
3730
3731template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003732inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003733typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003734basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3735 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003736{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003737 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003738 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003739}
3740
3741template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003742template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003743__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003744<
3745 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3746 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003747>
Marshall Clowe46031a2018-07-02 18:41:15 +00003748basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003749 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003750{
Marshall Clowe46031a2018-07-02 18:41:15 +00003751 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003752 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3753 (data(), size(), __sv.data(), __pos, __sv.size());
3754}
3755
3756template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003757inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003758typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003759basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003760 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003761{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003762 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003763 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003764 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003765}
3766
3767template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003768inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003769typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003770basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3771 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003772{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003773 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003774 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003775}
3776
3777// compare
3778
3779template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003780template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003781__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003782<
3783 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3784 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003785>
zoecarver1997e0a2021-02-05 11:54:47 -08003786basic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003787{
Marshall Clowe46031a2018-07-02 18:41:15 +00003788 __self_view __sv = __t;
Howard Hinnantb0485532011-07-24 21:45:06 +00003789 size_t __lhs_sz = size();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003790 size_t __rhs_sz = __sv.size();
3791 int __result = traits_type::compare(data(), __sv.data(),
Howard Hinnantb0485532011-07-24 21:45:06 +00003792 _VSTD::min(__lhs_sz, __rhs_sz));
3793 if (__result != 0)
3794 return __result;
3795 if (__lhs_sz < __rhs_sz)
3796 return -1;
3797 if (__lhs_sz > __rhs_sz)
3798 return 1;
3799 return 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003800}
3801
3802template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003803inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003804int
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003805basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003806{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003807 return compare(__self_view(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003808}
3809
3810template <class _CharT, class _Traits, class _Allocator>
3811int
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003812basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3813 size_type __n1,
Howard Hinnantd17880b2013-06-28 16:59:19 +00003814 const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003815 size_type __n2) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003816{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003817 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003818 size_type __sz = size();
3819 if (__pos1 > __sz || __n2 == npos)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003820 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003821 size_type __rlen = _VSTD::min(__n1, __sz - __pos1);
3822 int __r = traits_type::compare(data() + __pos1, __s, _VSTD::min(__rlen, __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003823 if (__r == 0)
3824 {
3825 if (__rlen < __n2)
3826 __r = -1;
3827 else if (__rlen > __n2)
3828 __r = 1;
3829 }
3830 return __r;
3831}
3832
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003833template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003834template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003835__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003836<
3837 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3838 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003839>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003840basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3841 size_type __n1,
Marshall Clowe46031a2018-07-02 18:41:15 +00003842 const _Tp& __t) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003843{
Marshall Clowe46031a2018-07-02 18:41:15 +00003844 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003845 return compare(__pos1, __n1, __sv.data(), __sv.size());
3846}
3847
3848template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003849inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003850int
3851basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3852 size_type __n1,
3853 const basic_string& __str) const
3854{
3855 return compare(__pos1, __n1, __str.data(), __str.size());
3856}
3857
3858template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003859template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003860__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003861<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003862 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
3863 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00003864 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003865>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003866basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3867 size_type __n1,
Marshall Clow82513342016-09-24 22:45:42 +00003868 const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003869 size_type __pos2,
3870 size_type __n2) const
3871{
Marshall Clow82513342016-09-24 22:45:42 +00003872 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003873 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3874}
3875
3876template <class _CharT, class _Traits, class _Allocator>
3877int
3878basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3879 size_type __n1,
3880 const basic_string& __str,
3881 size_type __pos2,
3882 size_type __n2) const
3883{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003884 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003885}
3886
3887template <class _CharT, class _Traits, class _Allocator>
3888int
3889basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
3890{
3891 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3892 return compare(0, npos, __s, traits_type::length(__s));
3893}
3894
3895template <class _CharT, class _Traits, class _Allocator>
3896int
3897basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3898 size_type __n1,
3899 const value_type* __s) const
3900{
3901 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3902 return compare(__pos1, __n1, __s, traits_type::length(__s));
3903}
3904
Howard Hinnantc51e1022010-05-11 19:42:16 +00003905// __invariants
3906
3907template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003908inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003909bool
3910basic_string<_CharT, _Traits, _Allocator>::__invariants() const
3911{
3912 if (size() > capacity())
3913 return false;
3914 if (capacity() < __min_cap - 1)
3915 return false;
Bruce Mitchener170d8972020-11-24 12:53:53 -05003916 if (data() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003917 return false;
Louis Dionne663415f2020-10-05 16:16:13 -04003918 if (data()[size()] != value_type())
Howard Hinnantc51e1022010-05-11 19:42:16 +00003919 return false;
3920 return true;
3921}
3922
Vedant Kumar55e007e2018-03-08 21:15:26 +00003923// __clear_and_shrink
3924
3925template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003926inline
Louis Dionne173f29e2019-05-29 16:01:36 +00003927void
Marshall Clowe60a7182018-05-29 17:04:37 +00003928basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
Vedant Kumar55e007e2018-03-08 21:15:26 +00003929{
3930 clear();
3931 if(__is_long())
3932 {
3933 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
3934 __set_long_cap(0);
3935 __set_short_size(0);
Louis Dionne663415f2020-10-05 16:16:13 -04003936 traits_type::assign(*__get_short_pointer(), value_type());
Vedant Kumar55e007e2018-03-08 21:15:26 +00003937 }
Louis Dionne173f29e2019-05-29 16:01:36 +00003938}
Vedant Kumar55e007e2018-03-08 21:15:26 +00003939
Howard Hinnantc51e1022010-05-11 19:42:16 +00003940// operator==
3941
3942template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003943inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003944bool
3945operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003946 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003947{
Howard Hinnantaaeb1132013-04-22 23:55:13 +00003948 size_t __lhs_sz = __lhs.size();
3949 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
3950 __rhs.data(),
3951 __lhs_sz) == 0;
3952}
3953
3954template<class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003955inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaaeb1132013-04-22 23:55:13 +00003956bool
3957operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
3958 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
3959{
3960 size_t __lhs_sz = __lhs.size();
3961 if (__lhs_sz != __rhs.size())
3962 return false;
3963 const char* __lp = __lhs.data();
3964 const char* __rp = __rhs.data();
3965 if (__lhs.__is_long())
3966 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
3967 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
3968 if (*__lp != *__rp)
3969 return false;
3970 return true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003971}
3972
3973template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003974inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003975bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003976operator==(const _CharT* __lhs,
3977 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003978{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00003979 typedef basic_string<_CharT, _Traits, _Allocator> _String;
3980 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
3981 size_t __lhs_len = _Traits::length(__lhs);
3982 if (__lhs_len != __rhs.size()) return false;
3983 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003984}
3985
Howard Hinnantc51e1022010-05-11 19:42:16 +00003986template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003987inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003988bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003989operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
3990 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003991{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00003992 typedef basic_string<_CharT, _Traits, _Allocator> _String;
3993 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
3994 size_t __rhs_len = _Traits::length(__rhs);
3995 if (__rhs_len != __lhs.size()) return false;
3996 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003997}
3998
Howard Hinnant3b6579a2010-08-22 00:02:43 +00003999template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004000inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004001bool
4002operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004003 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004004{
4005 return !(__lhs == __rhs);
4006}
4007
4008template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004009inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004010bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004011operator!=(const _CharT* __lhs,
4012 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004013{
4014 return !(__lhs == __rhs);
4015}
4016
4017template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004018inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004019bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004020operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4021 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004022{
4023 return !(__lhs == __rhs);
4024}
4025
4026// operator<
4027
4028template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004029inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004030bool
4031operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004032 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004033{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004034 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004035}
4036
4037template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004038inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004039bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004040operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4041 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004042{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004043 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004044}
4045
4046template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004047inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004048bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004049operator< (const _CharT* __lhs,
4050 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004051{
4052 return __rhs.compare(__lhs) > 0;
4053}
4054
Howard Hinnantc51e1022010-05-11 19:42:16 +00004055// operator>
4056
4057template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004058inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004059bool
4060operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004061 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004062{
4063 return __rhs < __lhs;
4064}
4065
4066template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004067inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004068bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004069operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4070 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004071{
4072 return __rhs < __lhs;
4073}
4074
4075template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004076inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004077bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004078operator> (const _CharT* __lhs,
4079 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004080{
4081 return __rhs < __lhs;
4082}
4083
4084// operator<=
4085
4086template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004087inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004088bool
4089operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004090 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004091{
4092 return !(__rhs < __lhs);
4093}
4094
4095template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004096inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004097bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004098operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4099 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004100{
4101 return !(__rhs < __lhs);
4102}
4103
4104template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004105inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004106bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004107operator<=(const _CharT* __lhs,
4108 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004109{
4110 return !(__rhs < __lhs);
4111}
4112
4113// operator>=
4114
4115template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004116inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004117bool
4118operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004119 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004120{
4121 return !(__lhs < __rhs);
4122}
4123
4124template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004125inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004126bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004127operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4128 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004129{
4130 return !(__lhs < __rhs);
4131}
4132
4133template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004134inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004135bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004136operator>=(const _CharT* __lhs,
4137 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004138{
4139 return !(__lhs < __rhs);
4140}
4141
4142// operator +
4143
4144template<class _CharT, class _Traits, class _Allocator>
4145basic_string<_CharT, _Traits, _Allocator>
4146operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4147 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4148{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004149 using _String = basic_string<_CharT, _Traits, _Allocator>;
4150 _String __r(_String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4151 typename _String::size_type __lhs_sz = __lhs.size();
4152 typename _String::size_type __rhs_sz = __rhs.size();
Howard Hinnantc51e1022010-05-11 19:42:16 +00004153 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4154 __r.append(__rhs.data(), __rhs_sz);
4155 return __r;
4156}
4157
4158template<class _CharT, class _Traits, class _Allocator>
4159basic_string<_CharT, _Traits, _Allocator>
4160operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4161{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004162 using _String = basic_string<_CharT, _Traits, _Allocator>;
4163 _String __r(_String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator()));
4164 typename _String::size_type __lhs_sz = _Traits::length(__lhs);
4165 typename _String::size_type __rhs_sz = __rhs.size();
Howard Hinnantc51e1022010-05-11 19:42:16 +00004166 __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz);
4167 __r.append(__rhs.data(), __rhs_sz);
4168 return __r;
4169}
4170
4171template<class _CharT, class _Traits, class _Allocator>
4172basic_string<_CharT, _Traits, _Allocator>
4173operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4174{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004175 using _String = basic_string<_CharT, _Traits, _Allocator>;
4176 _String __r(_String::__alloc_traits::select_on_container_copy_construction(__rhs.get_allocator()));
4177 typename _String::size_type __rhs_sz = __rhs.size();
Howard Hinnantc51e1022010-05-11 19:42:16 +00004178 __r.__init(&__lhs, 1, 1 + __rhs_sz);
4179 __r.append(__rhs.data(), __rhs_sz);
4180 return __r;
4181}
4182
4183template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004184inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004185basic_string<_CharT, _Traits, _Allocator>
4186operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
4187{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004188 using _String = basic_string<_CharT, _Traits, _Allocator>;
4189 _String __r(_String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4190 typename _String::size_type __lhs_sz = __lhs.size();
4191 typename _String::size_type __rhs_sz = _Traits::length(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004192 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4193 __r.append(__rhs, __rhs_sz);
4194 return __r;
4195}
4196
4197template<class _CharT, class _Traits, class _Allocator>
4198basic_string<_CharT, _Traits, _Allocator>
4199operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
4200{
Nikolas Klauser38631bf2022-02-11 19:24:31 +01004201 using _String = basic_string<_CharT, _Traits, _Allocator>;
4202 _String __r(_String::__alloc_traits::select_on_container_copy_construction(__lhs.get_allocator()));
4203 typename _String::size_type __lhs_sz = __lhs.size();
Howard Hinnantc51e1022010-05-11 19:42:16 +00004204 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1);
4205 __r.push_back(__rhs);
4206 return __r;
4207}
4208
Eric Fiselierfc92be82017-04-19 00:28:44 +00004209#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004210
4211template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004212inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004213basic_string<_CharT, _Traits, _Allocator>
4214operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4215{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004216 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004217}
4218
4219template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004220inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004221basic_string<_CharT, _Traits, _Allocator>
4222operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4223{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004224 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004225}
4226
4227template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004228inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004229basic_string<_CharT, _Traits, _Allocator>
4230operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4231{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004232 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004233}
4234
4235template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004236inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004237basic_string<_CharT, _Traits, _Allocator>
4238operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4239{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004240 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004241}
4242
4243template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004244inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004245basic_string<_CharT, _Traits, _Allocator>
4246operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4247{
4248 __rhs.insert(__rhs.begin(), __lhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004249 return _VSTD::move(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004250}
4251
4252template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004253inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004254basic_string<_CharT, _Traits, _Allocator>
4255operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
4256{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004257 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004258}
4259
4260template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004261inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004262basic_string<_CharT, _Traits, _Allocator>
4263operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
4264{
4265 __lhs.push_back(__rhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004266 return _VSTD::move(__lhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004267}
4268
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004269#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004270
4271// swap
4272
4273template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004274inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004275void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004276swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +00004277 basic_string<_CharT, _Traits, _Allocator>& __rhs)
4278 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004279{
4280 __lhs.swap(__rhs);
4281}
4282
Bruce Mitchener170d8972020-11-24 12:53:53 -05004283_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = nullptr, int __base = 10);
4284_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = nullptr, int __base = 10);
4285_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = nullptr, int __base = 10);
4286_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = nullptr, int __base = 10);
4287_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004288
Bruce Mitchener170d8972020-11-24 12:53:53 -05004289_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = nullptr);
4290_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = nullptr);
4291_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004292
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004293_LIBCPP_FUNC_VIS string to_string(int __val);
4294_LIBCPP_FUNC_VIS string to_string(unsigned __val);
4295_LIBCPP_FUNC_VIS string to_string(long __val);
4296_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
4297_LIBCPP_FUNC_VIS string to_string(long long __val);
4298_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
4299_LIBCPP_FUNC_VIS string to_string(float __val);
4300_LIBCPP_FUNC_VIS string to_string(double __val);
4301_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004302
Louis Dionne89258142021-08-23 15:32:36 -04004303#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Bruce Mitchener170d8972020-11-24 12:53:53 -05004304_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4305_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4306_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4307_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4308_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004309
Bruce Mitchener170d8972020-11-24 12:53:53 -05004310_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = nullptr);
4311_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = nullptr);
4312_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004313
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004314_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
4315_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
4316_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
4317_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
4318_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
4319_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
4320_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
4321_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
4322_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Louis Dionne89258142021-08-23 15:32:36 -04004323#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004324
Howard Hinnantc51e1022010-05-11 19:42:16 +00004325template<class _CharT, class _Traits, class _Allocator>
Martin Storsjö88890c22021-02-22 01:13:13 +02004326_LIBCPP_TEMPLATE_DATA_VIS
Eric Fiselierc9fdaf12020-01-15 17:02:17 -05004327const typename basic_string<_CharT, _Traits, _Allocator>::size_type
4328 basic_string<_CharT, _Traits, _Allocator>::npos;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004329
Marshall Clow851b9ec2019-05-20 21:56:51 +00004330template <class _CharT, class _Allocator>
4331struct _LIBCPP_TEMPLATE_VIS
4332 hash<basic_string<_CharT, char_traits<_CharT>, _Allocator> >
4333 : public unary_function<
4334 basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004335{
4336 size_t
Marshall Clow851b9ec2019-05-20 21:56:51 +00004337 operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT
4338 { return __do_string_hash(__val.data(), __val.data() + __val.size()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004339};
4340
Howard Hinnantc51e1022010-05-11 19:42:16 +00004341
Howard Hinnantdc095972011-07-18 15:51:59 +00004342template<class _CharT, class _Traits, class _Allocator>
4343basic_ostream<_CharT, _Traits>&
4344operator<<(basic_ostream<_CharT, _Traits>& __os,
4345 const basic_string<_CharT, _Traits, _Allocator>& __str);
4346
4347template<class _CharT, class _Traits, class _Allocator>
4348basic_istream<_CharT, _Traits>&
4349operator>>(basic_istream<_CharT, _Traits>& __is,
4350 basic_string<_CharT, _Traits, _Allocator>& __str);
4351
4352template<class _CharT, class _Traits, class _Allocator>
4353basic_istream<_CharT, _Traits>&
4354getline(basic_istream<_CharT, _Traits>& __is,
4355 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4356
4357template<class _CharT, class _Traits, class _Allocator>
4358inline _LIBCPP_INLINE_VISIBILITY
4359basic_istream<_CharT, _Traits>&
4360getline(basic_istream<_CharT, _Traits>& __is,
4361 basic_string<_CharT, _Traits, _Allocator>& __str);
4362
Howard Hinnantdc095972011-07-18 15:51:59 +00004363template<class _CharT, class _Traits, class _Allocator>
4364inline _LIBCPP_INLINE_VISIBILITY
4365basic_istream<_CharT, _Traits>&
4366getline(basic_istream<_CharT, _Traits>&& __is,
4367 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4368
4369template<class _CharT, class _Traits, class _Allocator>
4370inline _LIBCPP_INLINE_VISIBILITY
4371basic_istream<_CharT, _Traits>&
4372getline(basic_istream<_CharT, _Traits>&& __is,
4373 basic_string<_CharT, _Traits, _Allocator>& __str);
4374
Marshall Clow29b53f22018-12-14 18:49:35 +00004375#if _LIBCPP_STD_VER > 17
Marek Kurdeja98b1412020-05-02 13:58:03 +02004376template <class _CharT, class _Traits, class _Allocator, class _Up>
Marshall Clow29b53f22018-12-14 18:49:35 +00004377inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004378 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4379 erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
4380 auto __old_size = __str.size();
4381 __str.erase(_VSTD::remove(__str.begin(), __str.end(), __v), __str.end());
4382 return __old_size - __str.size();
4383}
Marshall Clow29b53f22018-12-14 18:49:35 +00004384
Marek Kurdeja98b1412020-05-02 13:58:03 +02004385template <class _CharT, class _Traits, class _Allocator, class _Predicate>
Marshall Clow29b53f22018-12-14 18:49:35 +00004386inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004387 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4388 erase_if(basic_string<_CharT, _Traits, _Allocator>& __str,
4389 _Predicate __pred) {
4390 auto __old_size = __str.size();
4391 __str.erase(_VSTD::remove_if(__str.begin(), __str.end(), __pred),
4392 __str.end());
4393 return __old_size - __str.size();
4394}
Marshall Clow29b53f22018-12-14 18:49:35 +00004395#endif
4396
Louis Dionneba400782020-10-02 15:02:52 -04004397#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004398
4399template<class _CharT, class _Traits, class _Allocator>
4400bool
4401basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4402{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004403 return data() <= _VSTD::__to_address(__i->base()) &&
4404 _VSTD::__to_address(__i->base()) < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004405}
4406
4407template<class _CharT, class _Traits, class _Allocator>
4408bool
4409basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4410{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004411 return data() < _VSTD::__to_address(__i->base()) &&
4412 _VSTD::__to_address(__i->base()) <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004413}
4414
4415template<class _CharT, class _Traits, class _Allocator>
4416bool
4417basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4418{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004419 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004420 return data() <= __p && __p <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004421}
4422
4423template<class _CharT, class _Traits, class _Allocator>
4424bool
4425basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4426{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004427 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004428 return data() <= __p && __p < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004429}
4430
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004431#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004432
Louis Dionne173f29e2019-05-29 16:01:36 +00004433#if _LIBCPP_STD_VER > 11
Marshall Clowcba751f2013-07-23 17:05:24 +00004434// Literal suffixes for basic_string [basic.string.literals]
Marshall Clowac868372013-10-05 21:18:32 +00004435inline namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004436{
4437 inline namespace string_literals
4438 {
Howard Hinnant5c167562013-08-07 19:39:48 +00004439 inline _LIBCPP_INLINE_VISIBILITY
4440 basic_string<char> operator "" s( const char *__str, size_t __len )
4441 {
4442 return basic_string<char> (__str, __len);
4443 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004444
Louis Dionne89258142021-08-23 15:32:36 -04004445#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnant5c167562013-08-07 19:39:48 +00004446 inline _LIBCPP_INLINE_VISIBILITY
4447 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4448 {
4449 return basic_string<wchar_t> (__str, __len);
4450 }
Louis Dionne89258142021-08-23 15:32:36 -04004451#endif
Marshall Clowcba751f2013-07-23 17:05:24 +00004452
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -04004453#ifndef _LIBCPP_HAS_NO_CHAR8_T
Marshall Clow8732fed2018-12-11 04:35:44 +00004454 inline _LIBCPP_INLINE_VISIBILITY
4455 basic_string<char8_t> operator "" s(const char8_t *__str, size_t __len) _NOEXCEPT
4456 {
4457 return basic_string<char8_t> (__str, __len);
4458 }
4459#endif
4460
Howard Hinnant5c167562013-08-07 19:39:48 +00004461 inline _LIBCPP_INLINE_VISIBILITY
4462 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4463 {
4464 return basic_string<char16_t> (__str, __len);
4465 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004466
Howard Hinnant5c167562013-08-07 19:39:48 +00004467 inline _LIBCPP_INLINE_VISIBILITY
4468 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4469 {
4470 return basic_string<char32_t> (__str, __len);
4471 }
Nikolas Klauserd26407a2021-12-02 14:12:51 +01004472 } // namespace string_literals
4473} // namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004474#endif
4475
Howard Hinnantc51e1022010-05-11 19:42:16 +00004476_LIBCPP_END_NAMESPACE_STD
4477
Eric Fiselierf4433a32017-05-31 22:07:49 +00004478_LIBCPP_POP_MACROS
4479
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004480#endif // _LIBCPP_STRING