blob: 1b803bb8bd8033a1f2fac6fc59159539a4d5de06 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
Louis Dionne9bd93882021-11-17 16:25:01 -05002//===----------------------------------------------------------------------===//
Howard Hinnantc51e1022010-05-11 19:42:16 +00003//
Chandler Carruth7642bb12019-01-19 08:50:56 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_STRING
11#define _LIBCPP_STRING
12
13/*
14 string synopsis
15
16namespace std
17{
18
19template <class stateT>
20class fpos
21{
22private:
23 stateT st;
24public:
25 fpos(streamoff = streamoff());
26
27 operator streamoff() const;
28
29 stateT state() const;
30 void state(stateT);
31
32 fpos& operator+=(streamoff);
33 fpos operator+ (streamoff) const;
34 fpos& operator-=(streamoff);
35 fpos operator- (streamoff) const;
36};
37
38template <class stateT> streamoff operator-(const fpos<stateT>& x, const fpos<stateT>& y);
39
40template <class stateT> bool operator==(const fpos<stateT>& x, const fpos<stateT>& y);
41template <class stateT> bool operator!=(const fpos<stateT>& x, const fpos<stateT>& y);
42
43template <class charT>
44struct char_traits
45{
46 typedef charT char_type;
47 typedef ... int_type;
48 typedef streamoff off_type;
49 typedef streampos pos_type;
50 typedef mbstate_t state_type;
51
Howard Hinnantaeb17d82011-05-29 19:57:12 +000052 static void assign(char_type& c1, const char_type& c2) noexcept;
Howard Hinnant270c00e2012-07-20 19:09:12 +000053 static constexpr bool eq(char_type c1, char_type c2) noexcept;
54 static constexpr bool lt(char_type c1, char_type c2) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000055
56 static int compare(const char_type* s1, const char_type* s2, size_t n);
57 static size_t length(const char_type* s);
58 static const char_type* find(const char_type* s, size_t n, const char_type& a);
59 static char_type* move(char_type* s1, const char_type* s2, size_t n);
60 static char_type* copy(char_type* s1, const char_type* s2, size_t n);
61 static char_type* assign(char_type* s, size_t n, char_type a);
62
Howard Hinnant270c00e2012-07-20 19:09:12 +000063 static constexpr int_type not_eof(int_type c) noexcept;
64 static constexpr char_type to_char_type(int_type c) noexcept;
65 static constexpr int_type to_int_type(char_type c) noexcept;
66 static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept;
67 static constexpr int_type eof() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000068};
69
70template <> struct char_traits<char>;
71template <> struct char_traits<wchar_t>;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +010072template <> struct char_traits<char8_t>; // C++20
73template <> struct char_traits<char16_t>;
74template <> struct char_traits<char32_t>;
Howard Hinnantc51e1022010-05-11 19:42:16 +000075
Howard Hinnant3b6579a2010-08-22 00:02:43 +000076template<class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
Howard Hinnantc51e1022010-05-11 19:42:16 +000077class basic_string
78{
Howard Hinnant3b6579a2010-08-22 00:02:43 +000079public:
80// types:
Howard Hinnantc51e1022010-05-11 19:42:16 +000081 typedef traits traits_type;
82 typedef typename traits_type::char_type value_type;
83 typedef Allocator allocator_type;
84 typedef typename allocator_type::size_type size_type;
85 typedef typename allocator_type::difference_type difference_type;
86 typedef typename allocator_type::reference reference;
87 typedef typename allocator_type::const_reference const_reference;
88 typedef typename allocator_type::pointer pointer;
89 typedef typename allocator_type::const_pointer const_pointer;
90 typedef implementation-defined iterator;
91 typedef implementation-defined const_iterator;
92 typedef std::reverse_iterator<iterator> reverse_iterator;
93 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
94
95 static const size_type npos = -1;
96
Howard Hinnant3e276872011-06-03 18:40:47 +000097 basic_string()
98 noexcept(is_nothrow_default_constructible<allocator_type>::value);
99 explicit basic_string(const allocator_type& a);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000100 basic_string(const basic_string& str);
Howard Hinnant3e276872011-06-03 18:40:47 +0000101 basic_string(basic_string&& str)
102 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clow83445802016-04-07 18:13:41 +0000103 basic_string(const basic_string& str, size_type pos,
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000104 const allocator_type& a = allocator_type());
Marshall Clow83445802016-04-07 18:13:41 +0000105 basic_string(const basic_string& str, size_type pos, size_type n,
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000106 const Allocator& a = Allocator());
Marshall Clow78dbe462016-11-14 18:22:19 +0000107 template<class T>
108 basic_string(const T& t, size_type pos, size_type n, const Allocator& a = Allocator()); // C++17
Marshall Clowe46031a2018-07-02 18:41:15 +0000109 template <class T>
110 explicit basic_string(const T& t, const Allocator& a = Allocator()); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000111 basic_string(const value_type* s, const allocator_type& a = allocator_type());
112 basic_string(const value_type* s, size_type n, const allocator_type& a = allocator_type());
Marek Kurdej90d79712021-07-27 16:16:21 +0200113 basic_string(nullptr_t) = delete; // C++2b
Howard Hinnantc51e1022010-05-11 19:42:16 +0000114 basic_string(size_type n, value_type c, const allocator_type& a = allocator_type());
115 template<class InputIterator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000116 basic_string(InputIterator begin, InputIterator end,
117 const allocator_type& a = allocator_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000118 basic_string(initializer_list<value_type>, const Allocator& = Allocator());
119 basic_string(const basic_string&, const Allocator&);
120 basic_string(basic_string&&, const Allocator&);
121
122 ~basic_string();
123
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000124 operator basic_string_view<charT, traits>() const noexcept;
125
Howard Hinnantc51e1022010-05-11 19:42:16 +0000126 basic_string& operator=(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000127 template <class T>
128 basic_string& operator=(const T& t); // C++17
Howard Hinnant3e276872011-06-03 18:40:47 +0000129 basic_string& operator=(basic_string&& str)
130 noexcept(
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000131 allocator_type::propagate_on_container_move_assignment::value ||
132 allocator_type::is_always_equal::value ); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000133 basic_string& operator=(const value_type* s);
Marek Kurdej90d79712021-07-27 16:16:21 +0200134 basic_string& operator=(nullptr_t) = delete; // C++2b
Howard Hinnantc51e1022010-05-11 19:42:16 +0000135 basic_string& operator=(value_type c);
136 basic_string& operator=(initializer_list<value_type>);
137
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000138 iterator begin() noexcept;
139 const_iterator begin() const noexcept;
140 iterator end() noexcept;
141 const_iterator end() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000142
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000143 reverse_iterator rbegin() noexcept;
144 const_reverse_iterator rbegin() const noexcept;
145 reverse_iterator rend() noexcept;
146 const_reverse_iterator rend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000147
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000148 const_iterator cbegin() const noexcept;
149 const_iterator cend() const noexcept;
150 const_reverse_iterator crbegin() const noexcept;
151 const_reverse_iterator crend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000152
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000153 size_type size() const noexcept;
154 size_type length() const noexcept;
155 size_type max_size() const noexcept;
156 size_type capacity() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000157
158 void resize(size_type n, value_type c);
159 void resize(size_type n);
160
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100161 template<class Operation>
162 constexpr void resize_and_overwrite(size_type n, Operation op); // since C++23
163
Marek Kurdejc9848142020-11-26 10:07:16 +0100164 void reserve(size_type res_arg);
165 void reserve(); // deprecated in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000166 void shrink_to_fit();
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000167 void clear() noexcept;
168 bool empty() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000169
170 const_reference operator[](size_type pos) const;
171 reference operator[](size_type pos);
172
173 const_reference at(size_type n) const;
174 reference at(size_type n);
175
176 basic_string& operator+=(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000177 template <class T>
178 basic_string& operator+=(const T& t); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000179 basic_string& operator+=(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000180 basic_string& operator+=(value_type c);
181 basic_string& operator+=(initializer_list<value_type>);
182
183 basic_string& append(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000184 template <class T>
185 basic_string& append(const T& t); // C++17
Marshall Clow8db7fb02014-03-04 19:17:19 +0000186 basic_string& append(const basic_string& str, size_type pos, size_type n=npos); //C++14
Marshall Clow82513342016-09-24 22:45:42 +0000187 template <class T>
188 basic_string& append(const T& t, size_type pos, size_type n=npos); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000189 basic_string& append(const value_type* s, size_type n);
190 basic_string& append(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000191 basic_string& append(size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000192 template<class InputIterator>
193 basic_string& append(InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000194 basic_string& append(initializer_list<value_type>);
195
196 void push_back(value_type c);
197 void pop_back();
198 reference front();
199 const_reference front() const;
200 reference back();
201 const_reference back() const;
202
203 basic_string& assign(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000204 template <class T>
205 basic_string& assign(const T& t); // C++17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000206 basic_string& assign(basic_string&& str);
Marshall Clow8db7fb02014-03-04 19:17:19 +0000207 basic_string& assign(const basic_string& str, size_type pos, size_type n=npos); // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000208 template <class T>
209 basic_string& assign(const T& t, size_type pos, size_type n=npos); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000210 basic_string& assign(const value_type* s, size_type n);
211 basic_string& assign(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000212 basic_string& assign(size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000213 template<class InputIterator>
214 basic_string& assign(InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000215 basic_string& assign(initializer_list<value_type>);
216
217 basic_string& insert(size_type pos1, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000218 template <class T>
219 basic_string& insert(size_type pos1, const T& t);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000220 basic_string& insert(size_type pos1, const basic_string& str,
221 size_type pos2, size_type n);
Marshall Clow82513342016-09-24 22:45:42 +0000222 template <class T>
223 basic_string& insert(size_type pos1, const T& t, size_type pos2, size_type n); // C++17
Marshall Clow8db7fb02014-03-04 19:17:19 +0000224 basic_string& insert(size_type pos, const value_type* s, size_type n=npos); //C++14
Howard Hinnantd17880b2013-06-28 16:59:19 +0000225 basic_string& insert(size_type pos, const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000226 basic_string& insert(size_type pos, size_type n, value_type c);
227 iterator insert(const_iterator p, value_type c);
228 iterator insert(const_iterator p, size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000229 template<class InputIterator>
230 iterator insert(const_iterator p, InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000231 iterator insert(const_iterator p, initializer_list<value_type>);
232
233 basic_string& erase(size_type pos = 0, size_type n = npos);
234 iterator erase(const_iterator position);
235 iterator erase(const_iterator first, const_iterator last);
236
237 basic_string& replace(size_type pos1, size_type n1, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000238 template <class T>
239 basic_string& replace(size_type pos1, size_type n1, const T& t); // C++17
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000240 basic_string& replace(size_type pos1, size_type n1, const basic_string& str,
Marshall Clow8db7fb02014-03-04 19:17:19 +0000241 size_type pos2, size_type n2=npos); // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000242 template <class T>
243 basic_string& replace(size_type pos1, size_type n1, const T& t,
244 size_type pos2, size_type n); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000245 basic_string& replace(size_type pos, size_type n1, const value_type* s, size_type n2);
246 basic_string& replace(size_type pos, size_type n1, const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000247 basic_string& replace(size_type pos, size_type n1, size_type n2, value_type c);
Howard Hinnant990d6e82010-11-17 21:11:40 +0000248 basic_string& replace(const_iterator i1, const_iterator i2, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000249 template <class T>
250 basic_string& replace(const_iterator i1, const_iterator i2, const T& t); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000251 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s, size_type n);
252 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s);
Howard Hinnant990d6e82010-11-17 21:11:40 +0000253 basic_string& replace(const_iterator i1, const_iterator i2, size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000254 template<class InputIterator>
Howard Hinnant990d6e82010-11-17 21:11:40 +0000255 basic_string& replace(const_iterator i1, const_iterator i2, InputIterator j1, InputIterator j2);
256 basic_string& replace(const_iterator i1, const_iterator i2, initializer_list<value_type>);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000257
Howard Hinnantd17880b2013-06-28 16:59:19 +0000258 size_type copy(value_type* s, size_type n, size_type pos = 0) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000259 basic_string substr(size_type pos = 0, size_type n = npos) const;
260
Howard Hinnant3e276872011-06-03 18:40:47 +0000261 void swap(basic_string& str)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000262 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
263 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000264
Howard Hinnantd17880b2013-06-28 16:59:19 +0000265 const value_type* c_str() const noexcept;
266 const value_type* data() const noexcept;
Marshall Clowd3c22392016-03-08 15:44:30 +0000267 value_type* data() noexcept; // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000268
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000269 allocator_type get_allocator() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000270
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000271 size_type find(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000272 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800273 size_type find(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000274 size_type find(const value_type* s, size_type pos, size_type n) const noexcept;
275 size_type find(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000276 size_type find(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000277
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000278 size_type rfind(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000279 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800280 size_type rfind(const T& t, size_type pos = npos) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000281 size_type rfind(const value_type* s, size_type pos, size_type n) const noexcept;
282 size_type rfind(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000283 size_type rfind(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000284
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000285 size_type find_first_of(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000286 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800287 size_type find_first_of(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000288 size_type find_first_of(const value_type* s, size_type pos, size_type n) const noexcept;
289 size_type find_first_of(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000290 size_type find_first_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000291
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000292 size_type find_last_of(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000293 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800294 size_type find_last_of(const T& t, size_type pos = npos) const noexcept noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000295 size_type find_last_of(const value_type* s, size_type pos, size_type n) const noexcept;
296 size_type find_last_of(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000297 size_type find_last_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000298
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000299 size_type find_first_not_of(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000300 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800301 size_type find_first_not_of(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000302 size_type find_first_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
303 size_type find_first_not_of(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000304 size_type find_first_not_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000305
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000306 size_type find_last_not_of(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000307 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800308 size_type find_last_not_of(const T& t, size_type pos = npos) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000309 size_type find_last_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
310 size_type find_last_not_of(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000311 size_type find_last_not_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000312
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000313 int compare(const basic_string& str) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000314 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800315 int compare(const T& t) const noexcept; // C++17, noexcept as an extension
Howard Hinnantc51e1022010-05-11 19:42:16 +0000316 int compare(size_type pos1, size_type n1, const basic_string& str) const;
Marshall Clowe46031a2018-07-02 18:41:15 +0000317 template <class T>
318 int compare(size_type pos1, size_type n1, const T& t) const; // C++17
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000319 int compare(size_type pos1, size_type n1, const basic_string& str,
Marshall Clow8db7fb02014-03-04 19:17:19 +0000320 size_type pos2, size_type n2=npos) const; // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000321 template <class T>
322 int compare(size_type pos1, size_type n1, const T& t,
323 size_type pos2, size_type n2=npos) const; // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000324 int compare(const value_type* s) const noexcept;
325 int compare(size_type pos1, size_type n1, const value_type* s) const;
326 int compare(size_type pos1, size_type n1, const value_type* s, size_type n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000327
Marek Kurdej24b4c512021-01-07 12:29:04 +0100328 bool starts_with(basic_string_view<charT, traits> sv) const noexcept; // C++20
329 bool starts_with(charT c) const noexcept; // C++20
330 bool starts_with(const charT* s) const; // C++20
331 bool ends_with(basic_string_view<charT, traits> sv) const noexcept; // C++20
332 bool ends_with(charT c) const noexcept; // C++20
333 bool ends_with(const charT* s) const; // C++20
Marshall Clow18c293b2017-12-04 20:11:38 +0000334
Wim Leflere023c3542021-01-19 14:33:30 -0500335 constexpr bool contains(basic_string_view<charT, traits> sv) const noexcept; // C++2b
336 constexpr bool contains(charT c) const noexcept; // C++2b
337 constexpr bool contains(const charT* s) const; // C++2b
338
Howard Hinnantc51e1022010-05-11 19:42:16 +0000339 bool __invariants() const;
340};
341
Marshall Clowa0563332018-02-08 06:34:03 +0000342template<class InputIterator,
343 class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
344basic_string(InputIterator, InputIterator, Allocator = Allocator())
345 -> basic_string<typename iterator_traits<InputIterator>::value_type,
346 char_traits<typename iterator_traits<InputIterator>::value_type>,
347 Allocator>; // C++17
348
Howard Hinnantc51e1022010-05-11 19:42:16 +0000349template<class charT, class traits, class Allocator>
350basic_string<charT, traits, Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000351operator+(const basic_string<charT, traits, Allocator>& lhs,
352 const basic_string<charT, traits, Allocator>& rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000353
354template<class charT, class traits, class Allocator>
355basic_string<charT, traits, Allocator>
356operator+(const charT* lhs , const basic_string<charT,traits,Allocator>&rhs);
357
358template<class charT, class traits, class Allocator>
359basic_string<charT, traits, Allocator>
360operator+(charT lhs, const basic_string<charT,traits,Allocator>& rhs);
361
362template<class charT, class traits, class Allocator>
363basic_string<charT, traits, Allocator>
364operator+(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs);
365
366template<class charT, class traits, class Allocator>
367basic_string<charT, traits, Allocator>
368operator+(const basic_string<charT, traits, Allocator>& lhs, charT rhs);
369
370template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000371bool operator==(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000372 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000373
374template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000375bool operator==(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000376
377template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000378bool operator==(const basic_string<charT,traits,Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000379
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000380template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000381bool operator!=(const basic_string<charT,traits,Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000382 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000383
384template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000385bool operator!=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000386
387template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000388bool operator!=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000389
390template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000391bool operator< (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000392 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000393
394template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000395bool operator< (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000396
397template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000398bool operator< (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000399
400template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000401bool operator> (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000402 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000403
404template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000405bool operator> (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000406
407template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000408bool operator> (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000409
410template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000411bool operator<=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000412 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000413
414template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000415bool operator<=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000416
417template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000418bool operator<=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000419
420template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000421bool operator>=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000422 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000423
424template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000425bool operator>=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000426
427template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000428bool operator>=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000429
430template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000431void swap(basic_string<charT, traits, Allocator>& lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +0000432 basic_string<charT, traits, Allocator>& rhs)
433 noexcept(noexcept(lhs.swap(rhs)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000434
435template<class charT, class traits, class Allocator>
436basic_istream<charT, traits>&
437operator>>(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
438
439template<class charT, class traits, class Allocator>
440basic_ostream<charT, traits>&
441operator<<(basic_ostream<charT, traits>& os, const basic_string<charT, traits, Allocator>& str);
442
443template<class charT, class traits, class Allocator>
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000444basic_istream<charT, traits>&
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000445getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str,
446 charT delim);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000447
448template<class charT, class traits, class Allocator>
449basic_istream<charT, traits>&
450getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
451
Marshall Clow29b53f22018-12-14 18:49:35 +0000452template<class charT, class traits, class Allocator, class U>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200453typename basic_string<charT, traits, Allocator>::size_type
454erase(basic_string<charT, traits, Allocator>& c, const U& value); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000455template<class charT, class traits, class Allocator, class Predicate>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200456typename basic_string<charT, traits, Allocator>::size_type
457erase_if(basic_string<charT, traits, Allocator>& c, Predicate pred); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000458
Howard Hinnantc51e1022010-05-11 19:42:16 +0000459typedef basic_string<char> string;
460typedef basic_string<wchar_t> wstring;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100461typedef basic_string<char8_t> u8string; // C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000462typedef basic_string<char16_t> u16string;
463typedef basic_string<char32_t> u32string;
464
Bruce Mitchener170d8972020-11-24 12:53:53 -0500465int stoi (const string& str, size_t* idx = nullptr, int base = 10);
466long stol (const string& str, size_t* idx = nullptr, int base = 10);
467unsigned long stoul (const string& str, size_t* idx = nullptr, int base = 10);
468long long stoll (const string& str, size_t* idx = nullptr, int base = 10);
469unsigned long long stoull(const string& str, size_t* idx = nullptr, int base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000470
Bruce Mitchener170d8972020-11-24 12:53:53 -0500471float stof (const string& str, size_t* idx = nullptr);
472double stod (const string& str, size_t* idx = nullptr);
473long double stold(const string& str, size_t* idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000474
475string to_string(int val);
476string to_string(unsigned val);
477string to_string(long val);
478string to_string(unsigned long val);
479string to_string(long long val);
480string to_string(unsigned long long val);
481string to_string(float val);
482string to_string(double val);
483string to_string(long double val);
484
Bruce Mitchener170d8972020-11-24 12:53:53 -0500485int stoi (const wstring& str, size_t* idx = nullptr, int base = 10);
486long stol (const wstring& str, size_t* idx = nullptr, int base = 10);
487unsigned long stoul (const wstring& str, size_t* idx = nullptr, int base = 10);
488long long stoll (const wstring& str, size_t* idx = nullptr, int base = 10);
489unsigned long long stoull(const wstring& str, size_t* idx = nullptr, int base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000490
Bruce Mitchener170d8972020-11-24 12:53:53 -0500491float stof (const wstring& str, size_t* idx = nullptr);
492double stod (const wstring& str, size_t* idx = nullptr);
493long double stold(const wstring& str, size_t* idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000494
495wstring to_wstring(int val);
496wstring to_wstring(unsigned val);
497wstring to_wstring(long val);
498wstring to_wstring(unsigned long val);
499wstring to_wstring(long long val);
500wstring to_wstring(unsigned long long val);
501wstring to_wstring(float val);
502wstring to_wstring(double val);
503wstring to_wstring(long double val);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000504
505template <> struct hash<string>;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100506template <> struct hash<u8string>; // C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000507template <> struct hash<u16string>;
508template <> struct hash<u32string>;
509template <> struct hash<wstring>;
510
Marshall Clowcba751f2013-07-23 17:05:24 +0000511basic_string<char> operator "" s( const char *str, size_t len ); // C++14
512basic_string<wchar_t> operator "" s( const wchar_t *str, size_t len ); // C++14
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100513basic_string<char8_t> operator "" s( const char8_t *str, size_t len ); // C++20
Marshall Clowcba751f2013-07-23 17:05:24 +0000514basic_string<char16_t> operator "" s( const char16_t *str, size_t len ); // C++14
515basic_string<char32_t> operator "" s( const char32_t *str, size_t len ); // C++14
516
Howard Hinnantc51e1022010-05-11 19:42:16 +0000517} // std
518
519*/
520
521#include <__config>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400522#include <__debug>
523#include <__functional_base>
Louis Dionne77249522021-06-11 09:55:11 -0400524#include <__iterator/wrap_iter.h>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000525#include <algorithm>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400526#include <compare>
527#include <cstdio> // EOF
Louis Dionned24191c2021-08-19 12:39:16 -0400528#include <cstdlib>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400529#include <cstring>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400530#include <initializer_list>
531#include <iosfwd>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000532#include <iterator>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000533#include <memory>
534#include <stdexcept>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400535#include <string_view>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000536#include <type_traits>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400537#include <utility>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000538#include <version>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000539
Louis Dionne89258142021-08-23 15:32:36 -0400540#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
541# include <cwchar>
542#endif
543
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400544#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
545# include <cstdint>
546#endif
Eric Fiselier14b6de92014-08-10 23:53:08 +0000547
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000548#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000549#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000550#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000551
Eric Fiselierf4433a32017-05-31 22:07:49 +0000552_LIBCPP_PUSH_MACROS
553#include <__undef_macros>
554
555
Howard Hinnantc51e1022010-05-11 19:42:16 +0000556_LIBCPP_BEGIN_NAMESPACE_STD
557
558// fpos
559
560template <class _StateT>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000561class _LIBCPP_TEMPLATE_VIS fpos
Howard Hinnantc51e1022010-05-11 19:42:16 +0000562{
563private:
564 _StateT __st_;
565 streamoff __off_;
566public:
567 _LIBCPP_INLINE_VISIBILITY fpos(streamoff __off = streamoff()) : __st_(), __off_(__off) {}
568
569 _LIBCPP_INLINE_VISIBILITY operator streamoff() const {return __off_;}
570
571 _LIBCPP_INLINE_VISIBILITY _StateT state() const {return __st_;}
572 _LIBCPP_INLINE_VISIBILITY void state(_StateT __st) {__st_ = __st;}
573
574 _LIBCPP_INLINE_VISIBILITY fpos& operator+=(streamoff __off) {__off_ += __off; return *this;}
575 _LIBCPP_INLINE_VISIBILITY fpos operator+ (streamoff __off) const {fpos __t(*this); __t += __off; return __t;}
576 _LIBCPP_INLINE_VISIBILITY fpos& operator-=(streamoff __off) {__off_ -= __off; return *this;}
577 _LIBCPP_INLINE_VISIBILITY fpos operator- (streamoff __off) const {fpos __t(*this); __t -= __off; return __t;}
578};
579
580template <class _StateT>
581inline _LIBCPP_INLINE_VISIBILITY
582streamoff operator-(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
583 {return streamoff(__x) - streamoff(__y);}
584
585template <class _StateT>
586inline _LIBCPP_INLINE_VISIBILITY
587bool operator==(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
588 {return streamoff(__x) == streamoff(__y);}
589
590template <class _StateT>
591inline _LIBCPP_INLINE_VISIBILITY
592bool operator!=(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
593 {return streamoff(__x) != streamoff(__y);}
594
Howard Hinnantc51e1022010-05-11 19:42:16 +0000595// basic_string
596
597template<class _CharT, class _Traits, class _Allocator>
598basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000599operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
600 const basic_string<_CharT, _Traits, _Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000601
602template<class _CharT, class _Traits, class _Allocator>
603basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000604operator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000605
606template<class _CharT, class _Traits, class _Allocator>
607basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000608operator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000609
610template<class _CharT, class _Traits, class _Allocator>
Louis Dionne8f8d95d2018-11-21 17:31:55 +0000611inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000612basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000613operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000614
615template<class _CharT, class _Traits, class _Allocator>
616basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000617operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000618
Shoaib Meenaiea363712017-07-29 02:54:41 +0000619_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&))
620
Howard Hinnantc51e1022010-05-11 19:42:16 +0000621template <bool>
Louis Dionneb6aabd92021-08-19 12:21:06 -0400622struct __basic_string_common;
623
624template <>
625struct __basic_string_common<true> {
626 // Both are defined in string.cpp
627 _LIBCPP_NORETURN _LIBCPP_EXPORTED_FROM_ABI void __throw_length_error() const;
628 _LIBCPP_NORETURN _LIBCPP_EXPORTED_FROM_ABI void __throw_out_of_range() const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000629};
630
Marshall Clow039b2f02016-01-13 21:54:34 +0000631template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400632struct __string_is_trivial_iterator : public false_type {};
633
634template <class _Tp>
635struct __string_is_trivial_iterator<_Tp*>
636 : public is_arithmetic<_Tp> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000637
Louis Dionne173f29e2019-05-29 16:01:36 +0000638template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400639struct __string_is_trivial_iterator<__wrap_iter<_Iter> >
640 : public __string_is_trivial_iterator<_Iter> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000641
Marshall Clow82513342016-09-24 22:45:42 +0000642template <class _CharT, class _Traits, class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500643struct __can_be_converted_to_string_view : public _BoolConstant<
644 is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
645 !is_convertible<const _Tp&, const _CharT*>::value
646 > {};
Marshall Clow82513342016-09-24 22:45:42 +0000647
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000648#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000649
650template <class _CharT, size_t = sizeof(_CharT)>
651struct __padding
652{
653 unsigned char __xx[sizeof(_CharT)-1];
654};
655
656template <class _CharT>
657struct __padding<_CharT, 1>
658{
659};
660
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400661#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000662
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400663#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800664typedef basic_string<char8_t> u8string;
665#endif
666
667#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
668typedef basic_string<char16_t> u16string;
669typedef basic_string<char32_t> u32string;
Louis Dionne96fc5f52021-09-09 11:25:10 -0400670#endif
Richard Smith256954d2020-11-11 17:12:18 -0800671
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000672template<class _CharT, class _Traits, class _Allocator>
Richard Smith256954d2020-11-11 17:12:18 -0800673class
674 _LIBCPP_TEMPLATE_VIS
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400675#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800676 _LIBCPP_PREFERRED_NAME(u8string)
677#endif
678#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
679 _LIBCPP_PREFERRED_NAME(u16string)
680 _LIBCPP_PREFERRED_NAME(u32string)
681#endif
682 basic_string
Louis Dionneb6aabd92021-08-19 12:21:06 -0400683 : private __basic_string_common<true> // This base class is historical, but it needs to remain for ABI compatibility
Howard Hinnantc51e1022010-05-11 19:42:16 +0000684{
685public:
686 typedef basic_string __self;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000687 typedef basic_string_view<_CharT, _Traits> __self_view;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000688 typedef _Traits traits_type;
Marshall Clowa3a74e02017-03-15 18:41:11 +0000689 typedef _CharT value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000690 typedef _Allocator allocator_type;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000691 typedef allocator_traits<allocator_type> __alloc_traits;
692 typedef typename __alloc_traits::size_type size_type;
693 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant3e276872011-06-03 18:40:47 +0000694 typedef value_type& reference;
695 typedef const value_type& const_reference;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000696 typedef typename __alloc_traits::pointer pointer;
697 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000698
Marshall Clow79f33542018-03-21 00:36:05 +0000699 static_assert((!is_array<value_type>::value), "Character type of basic_string must not be an array");
700 static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string must be standard-layout");
701 static_assert(( is_trivial<value_type>::value), "Character type of basic_string must be trivial");
702 static_assert(( is_same<_CharT, typename traits_type::char_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000703 "traits_type::char_type must be the same type as CharT");
Marshall Clow79f33542018-03-21 00:36:05 +0000704 static_assert(( is_same<typename allocator_type::value_type, value_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000705 "Allocator::value_type must be same type as value_type");
Marshall Clowe46031a2018-07-02 18:41:15 +0000706
Howard Hinnantc51e1022010-05-11 19:42:16 +0000707 typedef __wrap_iter<pointer> iterator;
708 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000709 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
710 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000711
712private:
Howard Hinnant68bf1812013-04-30 21:44:48 +0000713
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000714#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000715
716 struct __long
717 {
718 pointer __data_;
719 size_type __size_;
720 size_type __cap_;
721 };
722
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000723#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000724 static const size_type __short_mask = 0x01;
725 static const size_type __long_mask = 0x1ul;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000726#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000727 static const size_type __short_mask = 0x80;
728 static const size_type __long_mask = ~(size_type(~0) >> 1);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400729#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +0000730
731 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
732 (sizeof(__long) - 1)/sizeof(value_type) : 2};
733
734 struct __short
735 {
736 value_type __data_[__min_cap];
737 struct
738 : __padding<value_type>
739 {
740 unsigned char __size_;
741 };
742 };
743
744#else
745
Howard Hinnantc51e1022010-05-11 19:42:16 +0000746 struct __long
747 {
748 size_type __cap_;
749 size_type __size_;
750 pointer __data_;
751 };
752
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000753#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000754 static const size_type __short_mask = 0x80;
755 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000756#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000757 static const size_type __short_mask = 0x01;
758 static const size_type __long_mask = 0x1ul;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400759#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +0000760
Howard Hinnantc51e1022010-05-11 19:42:16 +0000761 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
762 (sizeof(__long) - 1)/sizeof(value_type) : 2};
763
764 struct __short
765 {
766 union
767 {
768 unsigned char __size_;
Howard Hinnant49e145e2012-10-30 19:06:59 +0000769 value_type __lx;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000770 };
771 value_type __data_[__min_cap];
772 };
773
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400774#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000775
Howard Hinnant8ea98242013-08-23 17:37:05 +0000776 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000777
Howard Hinnant8ea98242013-08-23 17:37:05 +0000778 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000779
780 struct __raw
781 {
782 size_type __words[__n_words];
783 };
784
785 struct __rep
786 {
787 union
788 {
789 __long __l;
790 __short __s;
791 __raw __r;
792 };
793 };
794
795 __compressed_pair<__rep, allocator_type> __r_;
796
Howard Hinnantc51e1022010-05-11 19:42:16 +0000797public:
Martin Storsjö88890c22021-02-22 01:13:13 +0200798 _LIBCPP_TEMPLATE_DATA_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000799 static const size_type npos = -1;
800
Howard Hinnant3e276872011-06-03 18:40:47 +0000801 _LIBCPP_INLINE_VISIBILITY basic_string()
802 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000803
804 _LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a)
805#if _LIBCPP_STD_VER <= 14
806 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
807#else
808 _NOEXCEPT;
809#endif
810
Howard Hinnantc51e1022010-05-11 19:42:16 +0000811 basic_string(const basic_string& __str);
812 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clowa80abc72015-06-03 19:56:43 +0000813
Eric Fiselierfc92be82017-04-19 00:28:44 +0000814#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000815 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000816 basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +0000817#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +0000818 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000819#else
820 _NOEXCEPT;
821#endif
822
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000823 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000824 basic_string(basic_string&& __str, const allocator_type& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400825#endif // _LIBCPP_CXX03_LANG
Marshall Clowe46031a2018-07-02 18:41:15 +0000826
Louis Dionne9ce598d2021-09-08 09:14:43 -0400827 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Eric Fiseliera8567862018-07-17 05:48:48 +0000828 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5169d1c2019-12-16 19:03:23 -0500829 basic_string(const _CharT* __s) : __r_(__default_init_tag(), __default_init_tag()) {
Eric Fiseliera8567862018-07-17 05:48:48 +0000830 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
831 __init(__s, traits_type::length(__s));
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +0100832#if _LIBCPP_DEBUG_LEVEL == 2
833 if (!__libcpp_is_constant_evaluated())
834 __get_db()->__insert_c(this);
835#endif
Eric Fiseliera8567862018-07-17 05:48:48 +0000836 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000837
Louis Dionne9ce598d2021-09-08 09:14:43 -0400838 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000839 _LIBCPP_INLINE_VISIBILITY
840 basic_string(const _CharT* __s, const _Allocator& __a);
841
Marek Kurdej90d79712021-07-27 16:16:21 +0200842#if _LIBCPP_STD_VER > 20
843 basic_string(nullptr_t) = delete;
844#endif
845
Howard Hinnantcf823322010-12-17 14:46:43 +0000846 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000847 basic_string(const _CharT* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +0000848 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000849 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000850 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000851 basic_string(size_type __n, _CharT __c);
Marshall Clowe46031a2018-07-02 18:41:15 +0000852
Louis Dionne9ce598d2021-09-08 09:14:43 -0400853 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000854 _LIBCPP_INLINE_VISIBILITY
855 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
856
Marshall Clow83445802016-04-07 18:13:41 +0000857 basic_string(const basic_string& __str, size_type __pos, size_type __n,
Eric Fiselier812882b2017-02-17 01:17:10 +0000858 const _Allocator& __a = _Allocator());
Marshall Clow83445802016-04-07 18:13:41 +0000859 _LIBCPP_INLINE_VISIBILITY
860 basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +0000861 const _Allocator& __a = _Allocator());
Marshall Clowe46031a2018-07-02 18:41:15 +0000862
Louis Dionne9ce598d2021-09-08 09:14:43 -0400863 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 +0000864 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselier812882b2017-02-17 01:17:10 +0000865 basic_string(const _Tp& __t, size_type __pos, size_type __n,
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500866 const allocator_type& __a = allocator_type());
Marshall Clowe46031a2018-07-02 18:41:15 +0000867
Louis Dionne9ce598d2021-09-08 09:14:43 -0400868 template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500869 !__is_same_uncvref<_Tp, basic_string>::value> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000870 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
871 explicit basic_string(const _Tp& __t);
872
Louis Dionne9ce598d2021-09-08 09:14:43 -0400873 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 +0000874 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
875 explicit basic_string(const _Tp& __t, const allocator_type& __a);
876
Louis Dionne9ce598d2021-09-08 09:14:43 -0400877 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000878 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000879 basic_string(_InputIterator __first, _InputIterator __last);
Louis Dionne9ce598d2021-09-08 09:14:43 -0400880 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000881 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000882 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000883#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000884 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000885 basic_string(initializer_list<_CharT> __il);
Howard Hinnantcf823322010-12-17 14:46:43 +0000886 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000887 basic_string(initializer_list<_CharT> __il, const _Allocator& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400888#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000889
Eric Fiselierd9a702a2016-10-31 03:42:50 +0000890 inline ~basic_string();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000891
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000892 _LIBCPP_INLINE_VISIBILITY
893 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
894
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000895 basic_string& operator=(const basic_string& __str);
Eric Fiselier5ec13b12017-01-23 21:24:58 +0000896
Louis Dionne9ce598d2021-09-08 09:14:43 -0400897 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 +0000898 basic_string& operator=(const _Tp& __t)
899 {__self_view __sv = __t; return assign(__sv);}
900
Eric Fiselierfc92be82017-04-19 00:28:44 +0000901#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000902 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000903 basic_string& operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000904 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiselierfc92be82017-04-19 00:28:44 +0000905 _LIBCPP_INLINE_VISIBILITY
906 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000907#endif
Howard Hinnantd17880b2013-06-28 16:59:19 +0000908 _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const value_type* __s) {return assign(__s);}
Marek Kurdej90d79712021-07-27 16:16:21 +0200909#if _LIBCPP_STD_VER > 20
910 basic_string& operator=(nullptr_t) = delete;
911#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000912 basic_string& operator=(value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000913
Louis Dionneba400782020-10-02 15:02:52 -0400914#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +0000915 _LIBCPP_INLINE_VISIBILITY
916 iterator begin() _NOEXCEPT
917 {return iterator(this, __get_pointer());}
918 _LIBCPP_INLINE_VISIBILITY
919 const_iterator begin() const _NOEXCEPT
920 {return const_iterator(this, __get_pointer());}
921 _LIBCPP_INLINE_VISIBILITY
922 iterator end() _NOEXCEPT
923 {return iterator(this, __get_pointer() + size());}
924 _LIBCPP_INLINE_VISIBILITY
925 const_iterator end() const _NOEXCEPT
926 {return const_iterator(this, __get_pointer() + size());}
927#else
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000928 _LIBCPP_INLINE_VISIBILITY
929 iterator begin() _NOEXCEPT
930 {return iterator(__get_pointer());}
931 _LIBCPP_INLINE_VISIBILITY
932 const_iterator begin() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000933 {return const_iterator(__get_pointer());}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000934 _LIBCPP_INLINE_VISIBILITY
935 iterator end() _NOEXCEPT
936 {return iterator(__get_pointer() + size());}
937 _LIBCPP_INLINE_VISIBILITY
938 const_iterator end() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000939 {return const_iterator(__get_pointer() + size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400940#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000941 _LIBCPP_INLINE_VISIBILITY
942 reverse_iterator rbegin() _NOEXCEPT
943 {return reverse_iterator(end());}
944 _LIBCPP_INLINE_VISIBILITY
945 const_reverse_iterator rbegin() const _NOEXCEPT
946 {return const_reverse_iterator(end());}
947 _LIBCPP_INLINE_VISIBILITY
948 reverse_iterator rend() _NOEXCEPT
949 {return reverse_iterator(begin());}
950 _LIBCPP_INLINE_VISIBILITY
951 const_reverse_iterator rend() const _NOEXCEPT
952 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000953
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000954 _LIBCPP_INLINE_VISIBILITY
955 const_iterator cbegin() const _NOEXCEPT
956 {return begin();}
957 _LIBCPP_INLINE_VISIBILITY
958 const_iterator cend() const _NOEXCEPT
959 {return end();}
960 _LIBCPP_INLINE_VISIBILITY
961 const_reverse_iterator crbegin() const _NOEXCEPT
962 {return rbegin();}
963 _LIBCPP_INLINE_VISIBILITY
964 const_reverse_iterator crend() const _NOEXCEPT
965 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000966
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000967 _LIBCPP_INLINE_VISIBILITY size_type size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000968 {return __is_long() ? __get_long_size() : __get_short_size();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000969 _LIBCPP_INLINE_VISIBILITY size_type length() const _NOEXCEPT {return size();}
970 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT;
971 _LIBCPP_INLINE_VISIBILITY size_type capacity() const _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +0000972 {return (__is_long() ? __get_long_cap()
973 : static_cast<size_type>(__min_cap)) - 1;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000974
975 void resize(size_type __n, value_type __c);
976 _LIBCPP_INLINE_VISIBILITY void resize(size_type __n) {resize(__n, value_type());}
977
Marek Kurdejc9848142020-11-26 10:07:16 +0100978 void reserve(size_type __requested_capacity);
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100979
980#if _LIBCPP_STD_VER > 20
981 template <class _Op>
982 _LIBCPP_HIDE_FROM_ABI constexpr
983 void resize_and_overwrite(size_type __n, _Op __op) {
984 __resize_default_init(__n);
985 pointer __data = data();
986 __erase_to_end(_VSTD::move(__op)(__data, __n));
987 }
988#endif
989
Eric Fiselier451d5582018-11-26 20:15:38 +0000990 _LIBCPP_INLINE_VISIBILITY void __resize_default_init(size_type __n);
991
Marek Kurdejc9848142020-11-26 10:07:16 +0100992 _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_INLINE_VISIBILITY
993 void reserve() _NOEXCEPT {shrink_to_fit();}
Marshall Clow6ae12a82018-11-28 18:18:34 +0000994 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejc9848142020-11-26 10:07:16 +0100995 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000996 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000997 void clear() _NOEXCEPT;
Marshall Clowb7db4972017-11-15 20:02:27 +0000998 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
999 bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001000
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001001 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __pos) const _NOEXCEPT;
1002 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001003
1004 const_reference at(size_type __n) const;
1005 reference at(size_type __n);
1006
1007 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const basic_string& __str) {return append(__str);}
Marshall Clowe46031a2018-07-02 18:41:15 +00001008
1009 template <class _Tp>
1010 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001011 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001012 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001013 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1014 && !__is_same_uncvref<_Tp, basic_string >::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001015 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001016 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001017 operator+=(const _Tp& __t) {__self_view __sv = __t; return append(__sv);}
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001018 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const value_type* __s) {return append(__s);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001019 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(value_type __c) {push_back(__c); return *this;}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001020#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001021 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(initializer_list<value_type> __il) {return append(__il);}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001022#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001023
Howard Hinnantcf823322010-12-17 14:46:43 +00001024 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001025 basic_string& append(const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001026
1027 template <class _Tp>
1028 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001029 __enable_if_t<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001030 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1031 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001032 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001033 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001034 append(const _Tp& __t) { __self_view __sv = __t; return append(__sv.data(), __sv.size()); }
Marshall Clow8db7fb02014-03-04 19:17:19 +00001035 basic_string& append(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clowe46031a2018-07-02 18:41:15 +00001036
Marshall Clow62953962016-10-03 23:40:48 +00001037 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001038 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001039 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001040 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001041 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1042 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001043 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001044 >
Marshall Clow82513342016-09-24 22:45:42 +00001045 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001046 basic_string& append(const value_type* __s, size_type __n);
1047 basic_string& append(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001048 basic_string& append(size_type __n, value_type __c);
Eric Fiselier451d5582018-11-26 20:15:38 +00001049
1050 _LIBCPP_INLINE_VISIBILITY
1051 void __append_default_init(size_type __n);
1052
Howard Hinnantc51e1022010-05-11 19:42:16 +00001053 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001054 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001055 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001056 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001057 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001058 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001059 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001060 _LIBCPP_INLINE_VISIBILITY
1061 append(_InputIterator __first, _InputIterator __last) {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001062 const basic_string __temp(__first, __last, __alloc());
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001063 append(__temp.data(), __temp.size());
1064 return *this;
1065 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001066 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001067 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001068 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001069 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001070 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001071 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001072 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001073 _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001074 append(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001075
Eric Fiselierfc92be82017-04-19 00:28:44 +00001076#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001077 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001078 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001079#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001080
1081 void push_back(value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001082 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001083 void pop_back();
Marshall Clow05cf6692019-03-19 03:30:07 +00001084 _LIBCPP_INLINE_VISIBILITY reference front() _NOEXCEPT;
1085 _LIBCPP_INLINE_VISIBILITY const_reference front() const _NOEXCEPT;
1086 _LIBCPP_INLINE_VISIBILITY reference back() _NOEXCEPT;
1087 _LIBCPP_INLINE_VISIBILITY const_reference back() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001088
Marshall Clowe46031a2018-07-02 18:41:15 +00001089 template <class _Tp>
1090 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001091 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001092 <
1093 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1094 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001095 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001096 assign(const _Tp & __t) { __self_view __sv = __t; return assign(__sv.data(), __sv.size()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001097 _LIBCPP_INLINE_VISIBILITY
Marshall Clow95d5e9a2016-03-09 18:08:29 +00001098 basic_string& assign(const basic_string& __str) { return *this = __str; }
Eric Fiselierfc92be82017-04-19 00:28:44 +00001099#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001100 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001101 basic_string& assign(basic_string&& __str)
Marshall Clow5aa9e942015-10-05 16:17:34 +00001102 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001103 {*this = _VSTD::move(__str); return *this;}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001104#endif
Marshall Clow8db7fb02014-03-04 19:17:19 +00001105 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow62953962016-10-03 23:40:48 +00001106 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001107 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001108 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001109 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001110 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1111 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001112 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001113 >
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001114 assign(const _Tp & __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001115 basic_string& assign(const value_type* __s, size_type __n);
1116 basic_string& assign(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001117 basic_string& assign(size_type __n, value_type __c);
1118 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001119 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001120 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001121 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001122 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001123 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001124 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001125 assign(_InputIterator __first, _InputIterator __last);
1126 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001127 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001128 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001129 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001130 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001131 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001132 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001133 assign(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001134#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001135 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001136 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001137#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001138
Howard Hinnantcf823322010-12-17 14:46:43 +00001139 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001140 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001141
1142 template <class _Tp>
1143 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001144 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001145 <
1146 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1147 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001148 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001149 insert(size_type __pos1, const _Tp& __t)
1150 { __self_view __sv = __t; return insert(__pos1, __sv.data(), __sv.size()); }
1151
Marshall Clow82513342016-09-24 22:45:42 +00001152 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001153 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001154 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001155 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001156 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001157 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001158 >
Marshall Clow82513342016-09-24 22:45:42 +00001159 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Marshall Clow8db7fb02014-03-04 19:17:19 +00001160 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001161 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1162 basic_string& insert(size_type __pos, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001163 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1164 iterator insert(const_iterator __pos, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001165 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001166 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1167 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001168 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001169 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001170 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001171 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001172 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001173 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001174 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1175 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001176 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001177 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001178 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001179 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001180 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001181 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001182 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001183#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001184 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001185 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1186 {return insert(__pos, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001187#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001188
1189 basic_string& erase(size_type __pos = 0, size_type __n = npos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001190 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001191 iterator erase(const_iterator __pos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001192 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001193 iterator erase(const_iterator __first, const_iterator __last);
1194
Howard Hinnantcf823322010-12-17 14:46:43 +00001195 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001196 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001197
1198 template <class _Tp>
1199 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001200 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001201 <
1202 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1203 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001204 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001205 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 +00001206 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 +00001207 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001208 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001209 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001210 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001211 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001212 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001213 >
Marshall Clow82513342016-09-24 22:45:42 +00001214 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001215 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
1216 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001217 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001218 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001219 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001220
1221 template <class _Tp>
1222 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001223 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001224 <
1225 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1226 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001227 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001228 replace(const_iterator __i1, const_iterator __i2, const _Tp& __t) { __self_view __sv = __t; return replace(__i1 - begin(), __i2 - __i1, __sv); }
1229
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001230 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001231 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +00001232 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001233 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Howard Hinnantcf823322010-12-17 14:46:43 +00001234 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001235 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001236 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001237 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001238 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001239 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001240 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001241 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001242 >
Howard Hinnant990d6e82010-11-17 21:11:40 +00001243 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001244#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001245 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001246 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001247 {return replace(__i1, __i2, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001248#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001249
Howard Hinnantd17880b2013-06-28 16:59:19 +00001250 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
Howard Hinnantcf823322010-12-17 14:46:43 +00001251 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001252 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1253
Howard Hinnantcf823322010-12-17 14:46:43 +00001254 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001255 void swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001256#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00001257 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +00001258#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001259 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001260 __is_nothrow_swappable<allocator_type>::value);
1261#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001262
Howard Hinnantcf823322010-12-17 14:46:43 +00001263 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001264 const value_type* c_str() const _NOEXCEPT {return data();}
Howard Hinnantcf823322010-12-17 14:46:43 +00001265 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001266 const value_type* data() const _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Eric Fiselier4ee2a892017-11-20 20:23:27 +00001267#if _LIBCPP_STD_VER > 14 || defined(_LIBCPP_BUILDING_LIBRARY)
Marshall Clowd3c22392016-03-08 15:44:30 +00001268 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001269 value_type* data() _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Marshall Clowd3c22392016-03-08 15:44:30 +00001270#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001271
Howard Hinnantcf823322010-12-17 14:46:43 +00001272 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001273 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001274
Howard Hinnantcf823322010-12-17 14:46:43 +00001275 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001276 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001277
1278 template <class _Tp>
1279 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001280 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001281 <
1282 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1283 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001284 >
zoecarver1997e0a2021-02-05 11:54:47 -08001285 find(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001286 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001287 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001288 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001289 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001290
Howard Hinnantcf823322010-12-17 14:46:43 +00001291 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001292 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001293
1294 template <class _Tp>
1295 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001296 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001297 <
1298 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1299 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001300 >
zoecarver1997e0a2021-02-05 11:54:47 -08001301 rfind(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001302 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001303 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001304 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001305 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001306
Howard Hinnantcf823322010-12-17 14:46:43 +00001307 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001308 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001309
1310 template <class _Tp>
1311 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001312 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001313 <
1314 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1315 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001316 >
zoecarver1997e0a2021-02-05 11:54:47 -08001317 find_first_of(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001318 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001319 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001320 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001321 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001322 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001323
Howard Hinnantcf823322010-12-17 14:46:43 +00001324 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001325 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001326
1327 template <class _Tp>
1328 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001329 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001330 <
1331 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1332 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001333 >
zoecarver1997e0a2021-02-05 11:54:47 -08001334 find_last_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001335 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001336 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001337 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001338 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001339 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001340
Howard Hinnantcf823322010-12-17 14:46:43 +00001341 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001342 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001343
1344 template <class _Tp>
1345 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001346 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001347 <
1348 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1349 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001350 >
zoecarver1997e0a2021-02-05 11:54:47 -08001351 find_first_not_of(const _Tp &__t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001352 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 +00001353 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001354 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001355 _LIBCPP_INLINE_VISIBILITY
1356 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1357
1358 _LIBCPP_INLINE_VISIBILITY
1359 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001360
1361 template <class _Tp>
1362 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001363 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001364 <
1365 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1366 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001367 >
zoecarver1997e0a2021-02-05 11:54:47 -08001368 find_last_not_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001369 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 +00001370 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001371 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001372 _LIBCPP_INLINE_VISIBILITY
1373 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1374
1375 _LIBCPP_INLINE_VISIBILITY
1376 int compare(const basic_string& __str) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001377
1378 template <class _Tp>
1379 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001380 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001381 <
1382 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1383 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001384 >
zoecarver1997e0a2021-02-05 11:54:47 -08001385 compare(const _Tp &__t) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001386
1387 template <class _Tp>
1388 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001389 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001390 <
1391 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1392 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001393 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001394 compare(size_type __pos1, size_type __n1, const _Tp& __t) const;
1395
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001396 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001397 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Marshall Clow8db7fb02014-03-04 19:17:19 +00001398 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 +00001399
Marshall Clow82513342016-09-24 22:45:42 +00001400 template <class _Tp>
Eric Fiselier7d4aa5c2016-10-14 05:29:46 +00001401 inline _LIBCPP_INLINE_VISIBILITY
Louis Dionne9ce598d2021-09-08 09:14:43 -04001402 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001403 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001404 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001405 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001406 >
Marshall Clow82513342016-09-24 22:45:42 +00001407 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 +00001408 int compare(const value_type* __s) const _NOEXCEPT;
1409 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1410 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001411
Marshall Clow18c293b2017-12-04 20:11:38 +00001412#if _LIBCPP_STD_VER > 17
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001413 constexpr _LIBCPP_INLINE_VISIBILITY
1414 bool starts_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001415 { return __self_view(data(), size()).starts_with(__sv); }
1416
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001417 constexpr _LIBCPP_INLINE_VISIBILITY
1418 bool starts_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001419 { return !empty() && _Traits::eq(front(), __c); }
1420
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001421 constexpr _LIBCPP_INLINE_VISIBILITY
1422 bool starts_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001423 { return starts_with(__self_view(__s)); }
1424
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001425 constexpr _LIBCPP_INLINE_VISIBILITY
1426 bool ends_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001427 { return __self_view(data(), size()).ends_with( __sv); }
1428
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001429 constexpr _LIBCPP_INLINE_VISIBILITY
1430 bool ends_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001431 { return !empty() && _Traits::eq(back(), __c); }
1432
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001433 constexpr _LIBCPP_INLINE_VISIBILITY
1434 bool ends_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001435 { return ends_with(__self_view(__s)); }
1436#endif
1437
Wim Leflere023c3542021-01-19 14:33:30 -05001438#if _LIBCPP_STD_VER > 20
1439 constexpr _LIBCPP_INLINE_VISIBILITY
1440 bool contains(__self_view __sv) const noexcept
1441 { return __self_view(data(), size()).contains(__sv); }
1442
1443 constexpr _LIBCPP_INLINE_VISIBILITY
1444 bool contains(value_type __c) const noexcept
1445 { return __self_view(data(), size()).contains(__c); }
1446
1447 constexpr _LIBCPP_INLINE_VISIBILITY
1448 bool contains(const value_type* __s) const
1449 { return __self_view(data(), size()).contains(__s); }
1450#endif
1451
Howard Hinnantcf823322010-12-17 14:46:43 +00001452 _LIBCPP_INLINE_VISIBILITY bool __invariants() const;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001453
Marshall Clowe60a7182018-05-29 17:04:37 +00001454 _LIBCPP_INLINE_VISIBILITY void __clear_and_shrink() _NOEXCEPT;
Marshall Clow851b9ec2019-05-20 21:56:51 +00001455
Marek Kurdejc9848142020-11-26 10:07:16 +01001456 _LIBCPP_INLINE_VISIBILITY void __shrink_or_extend(size_type __target_capacity);
1457
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001458 _LIBCPP_INLINE_VISIBILITY
1459 bool __is_long() const _NOEXCEPT
1460 {return bool(__r_.first().__s.__size_ & __short_mask);}
1461
Louis Dionneba400782020-10-02 15:02:52 -04001462#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001463
1464 bool __dereferenceable(const const_iterator* __i) const;
1465 bool __decrementable(const const_iterator* __i) const;
1466 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1467 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1468
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001469#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001470
Howard Hinnantc51e1022010-05-11 19:42:16 +00001471private:
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001472 _LIBCPP_INLINE_VISIBILITY
1473 allocator_type& __alloc() _NOEXCEPT
1474 {return __r_.second();}
1475 _LIBCPP_INLINE_VISIBILITY
1476 const allocator_type& __alloc() const _NOEXCEPT
1477 {return __r_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001478
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001479#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001480
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001481 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001482 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001483# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001484 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001485# else
1486 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1487# endif
1488
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001489 _LIBCPP_INLINE_VISIBILITY
1490 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001491# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001492 {return __r_.first().__s.__size_ >> 1;}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001493# else
1494 {return __r_.first().__s.__size_;}
1495# endif
1496
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001497#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001498
1499 _LIBCPP_INLINE_VISIBILITY
1500 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001501# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001502 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1503# else
1504 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
1505# endif
1506
1507 _LIBCPP_INLINE_VISIBILITY
1508 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001509# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001510 {return __r_.first().__s.__size_;}
1511# else
1512 {return __r_.first().__s.__size_ >> 1;}
1513# endif
1514
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001515#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001516
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001517 _LIBCPP_INLINE_VISIBILITY
1518 void __set_long_size(size_type __s) _NOEXCEPT
1519 {__r_.first().__l.__size_ = __s;}
1520 _LIBCPP_INLINE_VISIBILITY
1521 size_type __get_long_size() const _NOEXCEPT
1522 {return __r_.first().__l.__size_;}
1523 _LIBCPP_INLINE_VISIBILITY
1524 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001525 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1526
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001527 _LIBCPP_INLINE_VISIBILITY
1528 void __set_long_cap(size_type __s) _NOEXCEPT
1529 {__r_.first().__l.__cap_ = __long_mask | __s;}
1530 _LIBCPP_INLINE_VISIBILITY
1531 size_type __get_long_cap() const _NOEXCEPT
Howard Hinnant28b24882011-12-01 20:21:04 +00001532 {return __r_.first().__l.__cap_ & size_type(~__long_mask);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001533
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001534 _LIBCPP_INLINE_VISIBILITY
1535 void __set_long_pointer(pointer __p) _NOEXCEPT
1536 {__r_.first().__l.__data_ = __p;}
1537 _LIBCPP_INLINE_VISIBILITY
1538 pointer __get_long_pointer() _NOEXCEPT
1539 {return __r_.first().__l.__data_;}
1540 _LIBCPP_INLINE_VISIBILITY
1541 const_pointer __get_long_pointer() const _NOEXCEPT
1542 {return __r_.first().__l.__data_;}
1543 _LIBCPP_INLINE_VISIBILITY
1544 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001545 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001546 _LIBCPP_INLINE_VISIBILITY
1547 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001548 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001549 _LIBCPP_INLINE_VISIBILITY
1550 pointer __get_pointer() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001551 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001552 _LIBCPP_INLINE_VISIBILITY
1553 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001554 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1555
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001556 _LIBCPP_INLINE_VISIBILITY
1557 void __zero() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001558 {
1559 size_type (&__a)[__n_words] = __r_.first().__r.__words;
1560 for (unsigned __i = 0; __i < __n_words; ++__i)
1561 __a[__i] = 0;
1562 }
1563
1564 template <size_type __a> static
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001565 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00001566 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +00001567 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001568 enum {__alignment = 16};
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001569 static _LIBCPP_INLINE_VISIBILITY
1570 size_type __recommend(size_type __s) _NOEXCEPT
Marshall Clow80584522018-02-07 21:30:17 +00001571 {
1572 if (__s < __min_cap) return static_cast<size_type>(__min_cap) - 1;
1573 size_type __guess = __align_it<sizeof(value_type) < __alignment ?
1574 __alignment/sizeof(value_type) : 1 > (__s+1) - 1;
1575 if (__guess == __min_cap) ++__guess;
1576 return __guess;
1577 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001578
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001579 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001580 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001581 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001582 void __init(const value_type* __s, size_type __sz);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001583 inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001584 void __init(size_type __n, value_type __c);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001585
Martijn Vels5e7c9752020-03-04 17:52:46 -05001586 // Slow path for the (inlined) copy constructor for 'long' strings.
1587 // Always externally instantiated and not inlined.
1588 // Requires that __s is zero terminated.
1589 // The main reason for this function to exist is because for unstable, we
1590 // want to allow inlining of the copy constructor. However, we don't want
1591 // to call the __init() functions as those are marked as inline which may
1592 // result in over-aggressive inlining by the compiler, where our aim is
1593 // to only inline the fast path code directly in the ctor.
1594 void __init_copy_ctor_external(const value_type* __s, size_type __sz);
1595
Howard Hinnantc51e1022010-05-11 19:42:16 +00001596 template <class _InputIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001597 inline
Louis Dionne9ce598d2021-09-08 09:14:43 -04001598 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001599 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001600 __is_exactly_cpp17_input_iterator<_InputIterator>::value
1601 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001602 __init(_InputIterator __first, _InputIterator __last);
1603
1604 template <class _ForwardIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001605 inline
Louis Dionne9ce598d2021-09-08 09:14:43 -04001606 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001607 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001608 __is_cpp17_forward_iterator<_ForwardIterator>::value
1609 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001610 __init(_ForwardIterator __first, _ForwardIterator __last);
1611
1612 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001613 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001614 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1615 size_type __n_copy, size_type __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00001616 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001617
Martijn Vels596e3de2020-02-26 15:55:49 -05001618 // __assign_no_alias is invoked for assignment operations where we
1619 // have proof that the input does not alias the current instance.
1620 // For example, operator=(basic_string) performs a 'self' check.
1621 template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04001622 basic_string& __assign_no_alias(const value_type* __s, size_type __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05001623
Howard Hinnantcf823322010-12-17 14:46:43 +00001624 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001625 void __erase_to_end(size_type __pos);
1626
Martijn Velsa81fc792020-02-26 13:25:43 -05001627 // __erase_external_with_move is invoked for erase() invocations where
1628 // `n ~= npos`, likely requiring memory moves on the string data.
1629 void __erase_external_with_move(size_type __pos, size_type __n);
1630
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001631 _LIBCPP_INLINE_VISIBILITY
1632 void __copy_assign_alloc(const basic_string& __str)
1633 {__copy_assign_alloc(__str, integral_constant<bool,
1634 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1635
1636 _LIBCPP_INLINE_VISIBILITY
1637 void __copy_assign_alloc(const basic_string& __str, true_type)
1638 {
Marshall Clowf258c202017-01-31 03:40:52 +00001639 if (__alloc() == __str.__alloc())
1640 __alloc() = __str.__alloc();
1641 else
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001642 {
Marshall Clowf258c202017-01-31 03:40:52 +00001643 if (!__str.__is_long())
1644 {
Vedant Kumar55e007e2018-03-08 21:15:26 +00001645 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001646 __alloc() = __str.__alloc();
1647 }
1648 else
1649 {
1650 allocator_type __a = __str.__alloc();
1651 pointer __p = __alloc_traits::allocate(__a, __str.__get_long_cap());
Vedant Kumar55e007e2018-03-08 21:15:26 +00001652 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001653 __alloc() = _VSTD::move(__a);
1654 __set_long_pointer(__p);
1655 __set_long_cap(__str.__get_long_cap());
1656 __set_long_size(__str.size());
1657 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001658 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001659 }
1660
1661 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001662 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001663 {}
1664
Eric Fiselierfc92be82017-04-19 00:28:44 +00001665#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +00001666 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001667 void __move_assign(basic_string& __str, false_type)
1668 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnantcf823322010-12-17 14:46:43 +00001669 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001670 void __move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001671#if _LIBCPP_STD_VER > 14
1672 _NOEXCEPT;
1673#else
Howard Hinnant3e276872011-06-03 18:40:47 +00001674 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001675#endif
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001676#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001677
1678 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001679 void
Howard Hinnantc2734962011-09-02 20:42:31 +00001680 __move_assign_alloc(basic_string& __str)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001681 _NOEXCEPT_(
1682 !__alloc_traits::propagate_on_container_move_assignment::value ||
1683 is_nothrow_move_assignable<allocator_type>::value)
1684 {__move_assign_alloc(__str, integral_constant<bool,
1685 __alloc_traits::propagate_on_container_move_assignment::value>());}
1686
1687 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00001688 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001689 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1690 {
1691 __alloc() = _VSTD::move(__c.__alloc());
1692 }
1693
1694 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001695 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001696 _NOEXCEPT
1697 {}
1698
Martijn Velsda7d94f2020-06-19 14:24:03 -04001699 basic_string& __assign_external(const value_type* __s);
1700 basic_string& __assign_external(const value_type* __s, size_type __n);
1701
1702 // Assigns the value in __s, guaranteed to be __n < __min_cap in length.
1703 inline basic_string& __assign_short(const value_type* __s, size_type __n) {
1704 pointer __p = __is_long()
1705 ? (__set_long_size(__n), __get_long_pointer())
1706 : (__set_short_size(__n), __get_short_pointer());
1707 traits_type::move(_VSTD::__to_address(__p), __s, __n);
1708 traits_type::assign(__p[__n], value_type());
1709 return *this;
1710 }
1711
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01001712 _LIBCPP_HIDE_FROM_ABI basic_string& __null_terminate_at(value_type* __p, size_type __newsz) {
1713 __set_size(__newsz);
1714 __invalidate_iterators_past(__newsz);
1715 traits_type::assign(__p[__newsz], value_type());
1716 return *this;
1717 }
1718
Howard Hinnantcf823322010-12-17 14:46:43 +00001719 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
1720 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(size_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001721
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001722 template<class _Tp>
1723 _LIBCPP_INLINE_VISIBILITY
1724 bool __addr_in_range(_Tp&& __t) const {
1725 const volatile void *__p = _VSTD::addressof(__t);
1726 return data() <= __p && __p <= data() + size();
1727 }
1728
Louis Dionned24191c2021-08-19 12:39:16 -04001729 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1730 void __throw_length_error() const {
1731#ifndef _LIBCPP_NO_EXCEPTIONS
1732 __basic_string_common<true>::__throw_length_error();
1733#else
1734 _VSTD::abort();
1735#endif
1736 }
1737
1738 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1739 void __throw_out_of_range() const {
1740#ifndef _LIBCPP_NO_EXCEPTIONS
1741 __basic_string_common<true>::__throw_out_of_range();
1742#else
1743 _VSTD::abort();
1744#endif
1745 }
1746
Howard Hinnantc51e1022010-05-11 19:42:16 +00001747 friend basic_string operator+<>(const basic_string&, const basic_string&);
1748 friend basic_string operator+<>(const value_type*, const basic_string&);
1749 friend basic_string operator+<>(value_type, const basic_string&);
1750 friend basic_string operator+<>(const basic_string&, const value_type*);
1751 friend basic_string operator+<>(const basic_string&, value_type);
1752};
1753
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001754// These declarations must appear before any functions are implicitly used
1755// so that they have the correct visibility specifier.
1756#ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
Louis Dionne89258142021-08-23 15:32:36 -04001757 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1758# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1759 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1760# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001761#else
Louis Dionne89258142021-08-23 15:32:36 -04001762 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1763# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1764 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1765# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001766#endif
1767
1768
Louis Dionned59f8a52021-08-17 11:59:07 -04001769#if _LIBCPP_STD_VER >= 17
Marshall Clowa0563332018-02-08 06:34:03 +00001770template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -05001771 class _CharT = __iter_value_type<_InputIterator>,
Marshall Clowa0563332018-02-08 06:34:03 +00001772 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001773 class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
1774 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowa0563332018-02-08 06:34:03 +00001775 >
1776basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
1777 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
Marshall Clowe46031a2018-07-02 18:41:15 +00001778
1779template<class _CharT,
1780 class _Traits,
1781 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001782 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowe46031a2018-07-02 18:41:15 +00001783 >
1784explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
1785 -> basic_string<_CharT, _Traits, _Allocator>;
1786
1787template<class _CharT,
1788 class _Traits,
1789 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001790 class = enable_if_t<__is_allocator<_Allocator>::value>,
Marshall Clowe46031a2018-07-02 18:41:15 +00001791 class _Sz = typename allocator_traits<_Allocator>::size_type
1792 >
1793basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
1794 -> basic_string<_CharT, _Traits, _Allocator>;
Marshall Clowa0563332018-02-08 06:34:03 +00001795#endif
1796
Howard Hinnantc51e1022010-05-11 19:42:16 +00001797template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001798inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001799void
1800basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
1801{
Louis Dionneba400782020-10-02 15:02:52 -04001802#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001803 if (!__libcpp_is_constant_evaluated())
1804 __get_db()->__invalidate_all(this);
Louis Dionneba400782020-10-02 15:02:52 -04001805#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001806}
1807
1808template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001809inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001810void
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001811basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type __pos)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001812{
Louis Dionneba400782020-10-02 15:02:52 -04001813#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001814 if (!__libcpp_is_constant_evaluated()) {
1815 __c_node* __c = __get_db()->__find_c_and_lock(this);
1816 if (__c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001817 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001818 const_pointer __new_last = __get_pointer() + __pos;
1819 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001820 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001821 --__p;
1822 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1823 if (__i->base() > __new_last)
1824 {
1825 (*__p)->__c_ = nullptr;
1826 if (--__c->end_ != __p)
1827 _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1828 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001829 }
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001830 __get_db()->unlock();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001831 }
1832 }
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001833#else
1834 (void)__pos;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001835#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001836}
1837
1838template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001839inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001840basic_string<_CharT, _Traits, _Allocator>::basic_string()
Marshall Clowe546cbb2015-06-04 02:05:41 +00001841 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001842 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001843{
Louis Dionneba400782020-10-02 15:02:52 -04001844#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001845 if (!__libcpp_is_constant_evaluated())
1846 __get_db()->__insert_c(this);
Howard Hinnant8ea98242013-08-23 17:37:05 +00001847#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001848 __zero();
1849}
1850
1851template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001852inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001853basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
Eric Fiseliere012bf22015-07-18 20:40:46 +00001854#if _LIBCPP_STD_VER <= 14
1855 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1856#else
1857 _NOEXCEPT
1858#endif
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001859: __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001860{
Louis Dionneba400782020-10-02 15:02:52 -04001861#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001862 if (!__libcpp_is_constant_evaluated())
1863 __get_db()->__insert_c(this);
Howard Hinnant8ea98242013-08-23 17:37:05 +00001864#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001865 __zero();
1866}
1867
1868template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier815ed732016-09-16 00:00:48 +00001869void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1870 size_type __sz,
1871 size_type __reserve)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001872{
1873 if (__reserve > max_size())
1874 this->__throw_length_error();
1875 pointer __p;
1876 if (__reserve < __min_cap)
1877 {
1878 __set_short_size(__sz);
1879 __p = __get_short_pointer();
1880 }
1881 else
1882 {
1883 size_type __cap = __recommend(__reserve);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001884 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001885 __set_long_pointer(__p);
1886 __set_long_cap(__cap+1);
1887 __set_long_size(__sz);
1888 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001889 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001890 traits_type::assign(__p[__sz], value_type());
1891}
1892
1893template <class _CharT, class _Traits, class _Allocator>
1894void
Howard Hinnantd17880b2013-06-28 16:59:19 +00001895basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001896{
1897 if (__sz > max_size())
1898 this->__throw_length_error();
1899 pointer __p;
1900 if (__sz < __min_cap)
1901 {
1902 __set_short_size(__sz);
1903 __p = __get_short_pointer();
1904 }
1905 else
1906 {
1907 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001908 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001909 __set_long_pointer(__p);
1910 __set_long_cap(__cap+1);
1911 __set_long_size(__sz);
1912 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001913 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001914 traits_type::assign(__p[__sz], value_type());
1915}
1916
1917template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00001918template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00001919basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001920 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001921{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001922 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001923 __init(__s, traits_type::length(__s));
Louis Dionneba400782020-10-02 15:02:52 -04001924#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001925 if (!__libcpp_is_constant_evaluated())
1926 __get_db()->__insert_c(this);
Howard Hinnant8ea98242013-08-23 17:37:05 +00001927#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001928}
1929
1930template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001931inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001932basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001933 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001934{
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001935 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
1936 __init(__s, __n);
Louis Dionneba400782020-10-02 15:02:52 -04001937#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001938 if (!__libcpp_is_constant_evaluated())
1939 __get_db()->__insert_c(this);
Howard Hinnant8ea98242013-08-23 17:37:05 +00001940#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001941}
1942
1943template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001944inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001945basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001946 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001947{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001948 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001949 __init(__s, __n);
Louis Dionneba400782020-10-02 15:02:52 -04001950#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001951 if (!__libcpp_is_constant_evaluated())
1952 __get_db()->__insert_c(this);
Howard Hinnant8ea98242013-08-23 17:37:05 +00001953#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001954}
1955
1956template <class _CharT, class _Traits, class _Allocator>
1957basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001958 : __r_(__default_init_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001959{
1960 if (!__str.__is_long())
1961 __r_.first().__r = __str.__r_.first().__r;
1962 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001963 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1964 __str.__get_long_size());
1965
Louis Dionneba400782020-10-02 15:02:52 -04001966#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001967 if (!__libcpp_is_constant_evaluated())
1968 __get_db()->__insert_c(this);
Howard Hinnant8ea98242013-08-23 17:37:05 +00001969#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001970}
1971
1972template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00001973basic_string<_CharT, _Traits, _Allocator>::basic_string(
1974 const basic_string& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001975 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001976{
1977 if (!__str.__is_long())
1978 __r_.first().__r = __str.__r_.first().__r;
1979 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001980 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1981 __str.__get_long_size());
Louis Dionneba400782020-10-02 15:02:52 -04001982#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001983 if (!__libcpp_is_constant_evaluated())
1984 __get_db()->__insert_c(this);
Howard Hinnant8ea98242013-08-23 17:37:05 +00001985#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001986}
1987
Martijn Vels5e7c9752020-03-04 17:52:46 -05001988template <class _CharT, class _Traits, class _Allocator>
1989void basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(
1990 const value_type* __s, size_type __sz) {
1991 pointer __p;
1992 if (__sz < __min_cap) {
1993 __p = __get_short_pointer();
1994 __set_short_size(__sz);
1995 } else {
1996 if (__sz > max_size())
1997 this->__throw_length_error();
1998 size_t __cap = __recommend(__sz);
1999 __p = __alloc_traits::allocate(__alloc(), __cap + 1);
2000 __set_long_pointer(__p);
2001 __set_long_cap(__cap + 1);
2002 __set_long_size(__sz);
2003 }
2004 traits_type::copy(_VSTD::__to_address(__p), __s, __sz + 1);
2005}
2006
Eric Fiselierfc92be82017-04-19 00:28:44 +00002007#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002008
2009template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002010inline
Howard Hinnant3e276872011-06-03 18:40:47 +00002011basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +00002012#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +00002013 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowa80abc72015-06-03 19:56:43 +00002014#else
2015 _NOEXCEPT
2016#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002017 : __r_(_VSTD::move(__str.__r_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002018{
2019 __str.__zero();
Louis Dionneba400782020-10-02 15:02:52 -04002020#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002021 if (!__libcpp_is_constant_evaluated()) {
2022 __get_db()->__insert_c(this);
2023 if (__is_long())
2024 __get_db()->swap(this, &__str);
2025 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002026#endif
2027}
2028
2029template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002030inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002031basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002032 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002033{
Marshall Clowd2d24692014-07-17 15:32:20 +00002034 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002035 __init(_VSTD::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd2d24692014-07-17 15:32:20 +00002036 else
2037 {
2038 __r_.first().__r = __str.__r_.first().__r;
2039 __str.__zero();
2040 }
Louis Dionneba400782020-10-02 15:02:52 -04002041#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002042 if (!__libcpp_is_constant_evaluated()) {
2043 __get_db()->__insert_c(this);
2044 if (__is_long())
2045 __get_db()->swap(this, &__str);
2046 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002047#endif
2048}
2049
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002050#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002051
2052template <class _CharT, class _Traits, class _Allocator>
2053void
2054basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
2055{
2056 if (__n > max_size())
2057 this->__throw_length_error();
2058 pointer __p;
2059 if (__n < __min_cap)
2060 {
2061 __set_short_size(__n);
2062 __p = __get_short_pointer();
2063 }
2064 else
2065 {
2066 size_type __cap = __recommend(__n);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002067 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002068 __set_long_pointer(__p);
2069 __set_long_cap(__cap+1);
2070 __set_long_size(__n);
2071 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002072 traits_type::assign(_VSTD::__to_address(__p), __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002073 traits_type::assign(__p[__n], value_type());
2074}
2075
2076template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002077inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002078basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002079 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002080{
2081 __init(__n, __c);
Louis Dionneba400782020-10-02 15:02:52 -04002082#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002083 if (!__libcpp_is_constant_evaluated())
2084 __get_db()->__insert_c(this);
Howard Hinnant8ea98242013-08-23 17:37:05 +00002085#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002086}
2087
2088template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002089template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00002090basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002091 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002092{
2093 __init(__n, __c);
Louis Dionneba400782020-10-02 15:02:52 -04002094#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002095 if (!__libcpp_is_constant_evaluated())
2096 __get_db()->__insert_c(this);
Howard Hinnant8ea98242013-08-23 17:37:05 +00002097#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002098}
2099
Howard Hinnantc51e1022010-05-11 19:42:16 +00002100template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00002101basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
2102 size_type __pos, size_type __n,
2103 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002104 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002105{
2106 size_type __str_sz = __str.size();
2107 if (__pos > __str_sz)
2108 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002109 __init(__str.data() + __pos, _VSTD::min(__n, __str_sz - __pos));
Louis Dionneba400782020-10-02 15:02:52 -04002110#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002111 if (!__libcpp_is_constant_evaluated())
2112 __get_db()->__insert_c(this);
Howard Hinnant8ea98242013-08-23 17:37:05 +00002113#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002114}
2115
2116template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002117inline
Marshall Clow83445802016-04-07 18:13:41 +00002118basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +00002119 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002120 : __r_(__default_init_tag(), __a)
Marshall Clow83445802016-04-07 18:13:41 +00002121{
2122 size_type __str_sz = __str.size();
2123 if (__pos > __str_sz)
2124 this->__throw_out_of_range();
2125 __init(__str.data() + __pos, __str_sz - __pos);
Louis Dionneba400782020-10-02 15:02:52 -04002126#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002127 if (!__libcpp_is_constant_evaluated())
2128 __get_db()->__insert_c(this);
Marshall Clow83445802016-04-07 18:13:41 +00002129#endif
2130}
2131
2132template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002133template <class _Tp, class>
Marshall Clow78dbe462016-11-14 18:22:19 +00002134basic_string<_CharT, _Traits, _Allocator>::basic_string(
Marshall Clowe46031a2018-07-02 18:41:15 +00002135 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002136 : __r_(__default_init_tag(), __a)
Marshall Clow78dbe462016-11-14 18:22:19 +00002137{
Marshall Clowe46031a2018-07-02 18:41:15 +00002138 __self_view __sv0 = __t;
2139 __self_view __sv = __sv0.substr(__pos, __n);
Marshall Clow78dbe462016-11-14 18:22:19 +00002140 __init(__sv.data(), __sv.size());
Louis Dionneba400782020-10-02 15:02:52 -04002141#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002142 if (!__libcpp_is_constant_evaluated())
2143 __get_db()->__insert_c(this);
Eric Fiselier812882b2017-02-17 01:17:10 +00002144#endif
Marshall Clow78dbe462016-11-14 18:22:19 +00002145}
2146
2147template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002148template <class _Tp, class>
2149basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002150 : __r_(__default_init_tag(), __default_init_tag())
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002151{
Marshall Clowe46031a2018-07-02 18:41:15 +00002152 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002153 __init(__sv.data(), __sv.size());
Louis Dionneba400782020-10-02 15:02:52 -04002154#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002155 if (!__libcpp_is_constant_evaluated())
2156 __get_db()->__insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002157#endif
2158}
2159
2160template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002161template <class _Tp, class>
2162basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002163 : __r_(__default_init_tag(), __a)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002164{
Marshall Clowe46031a2018-07-02 18:41:15 +00002165 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002166 __init(__sv.data(), __sv.size());
Louis Dionneba400782020-10-02 15:02:52 -04002167#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002168 if (!__libcpp_is_constant_evaluated())
2169 __get_db()->__insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002170#endif
2171}
2172
2173template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002174template <class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002175__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002176<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002177 __is_exactly_cpp17_input_iterator<_InputIterator>::value
2178>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002179basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
2180{
2181 __zero();
2182#ifndef _LIBCPP_NO_EXCEPTIONS
2183 try
2184 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002185#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002186 for (; __first != __last; ++__first)
2187 push_back(*__first);
2188#ifndef _LIBCPP_NO_EXCEPTIONS
2189 }
2190 catch (...)
2191 {
2192 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002193 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002194 throw;
2195 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002196#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002197}
2198
2199template <class _CharT, class _Traits, class _Allocator>
2200template <class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002201__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002202<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002203 __is_cpp17_forward_iterator<_ForwardIterator>::value
2204>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002205basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
2206{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002207 size_type __sz = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002208 if (__sz > max_size())
2209 this->__throw_length_error();
2210 pointer __p;
2211 if (__sz < __min_cap)
2212 {
2213 __set_short_size(__sz);
2214 __p = __get_short_pointer();
2215 }
2216 else
2217 {
2218 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002219 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002220 __set_long_pointer(__p);
2221 __set_long_cap(__cap+1);
2222 __set_long_size(__sz);
2223 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002224
2225#ifndef _LIBCPP_NO_EXCEPTIONS
2226 try
2227 {
2228#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002229 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002230 traits_type::assign(*__p, *__first);
2231 traits_type::assign(*__p, value_type());
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002232#ifndef _LIBCPP_NO_EXCEPTIONS
2233 }
2234 catch (...)
2235 {
2236 if (__is_long())
2237 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
2238 throw;
2239 }
2240#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002241}
2242
2243template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002244template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002245inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002246basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002247 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002248{
2249 __init(__first, __last);
Louis Dionneba400782020-10-02 15:02:52 -04002250#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002251 if (!__libcpp_is_constant_evaluated())
2252 __get_db()->__insert_c(this);
Howard Hinnant8ea98242013-08-23 17:37:05 +00002253#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002254}
2255
2256template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002257template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002258inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002259basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
2260 const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002261 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002262{
2263 __init(__first, __last);
Louis Dionneba400782020-10-02 15:02:52 -04002264#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002265 if (!__libcpp_is_constant_evaluated())
2266 __get_db()->__insert_c(this);
Howard Hinnant8ea98242013-08-23 17:37:05 +00002267#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002268}
2269
Eric Fiselierfc92be82017-04-19 00:28:44 +00002270#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002271
Howard Hinnantc51e1022010-05-11 19:42:16 +00002272template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002273inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002274basic_string<_CharT, _Traits, _Allocator>::basic_string(
2275 initializer_list<_CharT> __il)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002276 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002277{
2278 __init(__il.begin(), __il.end());
Louis Dionneba400782020-10-02 15:02:52 -04002279#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002280 if (!__libcpp_is_constant_evaluated())
2281 __get_db()->__insert_c(this);
Howard Hinnant8ea98242013-08-23 17:37:05 +00002282#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002283}
2284
2285template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002286inline
Eric Fiselier9d355982017-04-12 23:45:53 +00002287
Eric Fiselier812882b2017-02-17 01:17:10 +00002288basic_string<_CharT, _Traits, _Allocator>::basic_string(
2289 initializer_list<_CharT> __il, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002290 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002291{
2292 __init(__il.begin(), __il.end());
Louis Dionneba400782020-10-02 15:02:52 -04002293#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002294 if (!__libcpp_is_constant_evaluated())
2295 __get_db()->__insert_c(this);
Howard Hinnant8ea98242013-08-23 17:37:05 +00002296#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002297}
2298
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002299#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002300
Howard Hinnantc51e1022010-05-11 19:42:16 +00002301template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002302basic_string<_CharT, _Traits, _Allocator>::~basic_string()
2303{
Louis Dionneba400782020-10-02 15:02:52 -04002304#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002305 if (!__libcpp_is_constant_evaluated())
2306 __get_db()->__erase_c(this);
Howard Hinnant8ea98242013-08-23 17:37:05 +00002307#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002308 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002309 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002310}
2311
2312template <class _CharT, class _Traits, class _Allocator>
2313void
2314basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
2315 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002316 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 +00002317{
2318 size_type __ms = max_size();
2319 if (__delta_cap > __ms - __old_cap - 1)
2320 this->__throw_length_error();
2321 pointer __old_p = __get_pointer();
2322 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002323 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002324 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002325 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002326 __invalidate_all_iterators();
2327 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002328 traits_type::copy(_VSTD::__to_address(__p),
2329 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002330 if (__n_add != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002331 traits_type::copy(_VSTD::__to_address(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002332 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2333 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002334 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2335 _VSTD::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002336 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002337 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002338 __set_long_pointer(__p);
2339 __set_long_cap(__cap+1);
2340 __old_sz = __n_copy + __n_add + __sec_cp_sz;
2341 __set_long_size(__old_sz);
2342 traits_type::assign(__p[__old_sz], value_type());
2343}
2344
2345template <class _CharT, class _Traits, class _Allocator>
2346void
2347basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2348 size_type __n_copy, size_type __n_del, size_type __n_add)
2349{
2350 size_type __ms = max_size();
Marshall Clow2267c5d2013-11-06 14:24:38 +00002351 if (__delta_cap > __ms - __old_cap)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002352 this->__throw_length_error();
2353 pointer __old_p = __get_pointer();
2354 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002355 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002356 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002357 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002358 __invalidate_all_iterators();
2359 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002360 traits_type::copy(_VSTD::__to_address(__p),
2361 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002362 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2363 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002364 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2365 _VSTD::__to_address(__old_p) + __n_copy + __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002366 __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002367 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002368 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002369 __set_long_pointer(__p);
2370 __set_long_cap(__cap+1);
2371}
2372
2373// assign
2374
2375template <class _CharT, class _Traits, class _Allocator>
Martijn Vels596e3de2020-02-26 15:55:49 -05002376template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04002377basic_string<_CharT, _Traits, _Allocator>&
2378basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(
Martijn Vels596e3de2020-02-26 15:55:49 -05002379 const value_type* __s, size_type __n) {
2380 size_type __cap = __is_short ? __min_cap : __get_long_cap();
2381 if (__n < __cap) {
2382 pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
2383 __is_short ? __set_short_size(__n) : __set_long_size(__n);
2384 traits_type::copy(_VSTD::__to_address(__p), __s, __n);
2385 traits_type::assign(__p[__n], value_type());
2386 __invalidate_iterators_past(__n);
2387 } else {
2388 size_type __sz = __is_short ? __get_short_size() : __get_long_size();
2389 __grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
2390 }
Martijn Velsb6a08b62020-04-10 18:36:31 -04002391 return *this;
Martijn Vels596e3de2020-02-26 15:55:49 -05002392}
2393
2394template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002395basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002396basic_string<_CharT, _Traits, _Allocator>::__assign_external(
2397 const value_type* __s, size_type __n) {
2398 size_type __cap = capacity();
2399 if (__cap >= __n) {
2400 value_type* __p = _VSTD::__to_address(__get_pointer());
2401 traits_type::move(__p, __s, __n);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002402 return __null_terminate_at(__p, __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04002403 } else {
2404 size_type __sz = size();
2405 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002406 return *this;
Martijn Velsda7d94f2020-06-19 14:24:03 -04002407 }
Martijn Velsda7d94f2020-06-19 14:24:03 -04002408}
2409
2410template <class _CharT, class _Traits, class _Allocator>
2411basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002412basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002413{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002414 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Louis Dionne3e0c4332021-08-31 10:49:06 -04002415 return (__builtin_constant_p(__n) && __n < __min_cap)
Martijn Velsda7d94f2020-06-19 14:24:03 -04002416 ? __assign_short(__s, __n)
2417 : __assign_external(__s, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002418}
2419
2420template <class _CharT, class _Traits, class _Allocator>
2421basic_string<_CharT, _Traits, _Allocator>&
2422basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2423{
2424 size_type __cap = capacity();
2425 if (__cap < __n)
2426 {
2427 size_type __sz = size();
2428 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2429 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002430 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002431 traits_type::assign(__p, __n, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002432 return __null_terminate_at(__p, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002433}
2434
2435template <class _CharT, class _Traits, class _Allocator>
2436basic_string<_CharT, _Traits, _Allocator>&
2437basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2438{
2439 pointer __p;
2440 if (__is_long())
2441 {
2442 __p = __get_long_pointer();
2443 __set_long_size(1);
2444 }
2445 else
2446 {
2447 __p = __get_short_pointer();
2448 __set_short_size(1);
2449 }
2450 traits_type::assign(*__p, __c);
2451 traits_type::assign(*++__p, value_type());
2452 __invalidate_iterators_past(1);
2453 return *this;
2454}
2455
2456template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002457basic_string<_CharT, _Traits, _Allocator>&
2458basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2459{
Martijn Vels596e3de2020-02-26 15:55:49 -05002460 if (this != &__str) {
2461 __copy_assign_alloc(__str);
2462 if (!__is_long()) {
2463 if (!__str.__is_long()) {
Eric Fiselier03bbc922020-01-15 17:27:10 -05002464 __r_.first().__r = __str.__r_.first().__r;
Martijn Vels596e3de2020-02-26 15:55:49 -05002465 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002466 return __assign_no_alias<true>(__str.data(), __str.size());
Martijn Vels596e3de2020-02-26 15:55:49 -05002467 }
2468 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002469 return __assign_no_alias<false>(__str.data(), __str.size());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002470 }
Martijn Vels596e3de2020-02-26 15:55:49 -05002471 }
2472 return *this;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002473}
2474
Eric Fiselierfc92be82017-04-19 00:28:44 +00002475#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002476
2477template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002478inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002479void
2480basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002481 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002482{
2483 if (__alloc() != __str.__alloc())
2484 assign(__str);
2485 else
2486 __move_assign(__str, true_type());
2487}
2488
2489template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002490inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002491void
2492basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002493#if _LIBCPP_STD_VER > 14
2494 _NOEXCEPT
2495#else
Howard Hinnant3e276872011-06-03 18:40:47 +00002496 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002497#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002498{
marshall2ed41622019-11-27 07:13:00 -08002499 if (__is_long()) {
2500 __alloc_traits::deallocate(__alloc(), __get_long_pointer(),
2501 __get_long_cap());
2502#if _LIBCPP_STD_VER <= 14
2503 if (!is_nothrow_move_assignable<allocator_type>::value) {
2504 __set_short_size(0);
2505 traits_type::assign(__get_short_pointer()[0], value_type());
2506 }
2507#endif
2508 }
2509 __move_assign_alloc(__str);
2510 __r_.first() = __str.__r_.first();
2511 __str.__set_short_size(0);
2512 traits_type::assign(__str.__get_short_pointer()[0], value_type());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002513}
2514
2515template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002516inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002517basic_string<_CharT, _Traits, _Allocator>&
2518basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002519 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002520{
2521 __move_assign(__str, integral_constant<bool,
2522 __alloc_traits::propagate_on_container_move_assignment::value>());
2523 return *this;
2524}
2525
2526#endif
2527
2528template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002529template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002530__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002531<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002532 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002533 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002534>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002535basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2536{
Marshall Clow958362f2016-09-05 01:54:30 +00002537 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002538 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002539 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002540}
2541
2542template <class _CharT, class _Traits, class _Allocator>
2543template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002544__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002545<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002546 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002547 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002548>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002549basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2550{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002551 size_type __cap = capacity();
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002552 size_type __n = __string_is_trivial_iterator<_ForwardIterator>::value ?
2553 static_cast<size_type>(_VSTD::distance(__first, __last)) : 0;
2554
2555 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2556 (__cap >= __n || !__addr_in_range(*__first)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002557 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002558 if (__cap < __n)
2559 {
2560 size_type __sz = size();
2561 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2562 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002563 pointer __p = __get_pointer();
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002564 for (; __first != __last; ++__p, (void) ++__first)
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002565 traits_type::assign(*__p, *__first);
2566 traits_type::assign(*__p, value_type());
2567 __set_size(__n);
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04002568 __invalidate_iterators_past(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002569 }
2570 else
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002571 {
2572 const basic_string __temp(__first, __last, __alloc());
2573 assign(__temp.data(), __temp.size());
2574 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002575 return *this;
2576}
2577
2578template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002579basic_string<_CharT, _Traits, _Allocator>&
2580basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2581{
2582 size_type __sz = __str.size();
2583 if (__pos > __sz)
2584 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002585 return assign(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002586}
2587
2588template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002589template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002590__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002591<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002592 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
2593 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00002594 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002595>
Marshall Clow82513342016-09-24 22:45:42 +00002596basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002597{
Marshall Clow82513342016-09-24 22:45:42 +00002598 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002599 size_type __sz = __sv.size();
2600 if (__pos > __sz)
2601 this->__throw_out_of_range();
2602 return assign(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2603}
2604
2605
2606template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002607basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002608basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s) {
2609 return __assign_external(__s, traits_type::length(__s));
2610}
2611
2612template <class _CharT, class _Traits, class _Allocator>
2613basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002614basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002615{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002616 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Louis Dionne3e0c4332021-08-31 10:49:06 -04002617 return __builtin_constant_p(*__s)
Martijn Velsda7d94f2020-06-19 14:24:03 -04002618 ? (traits_type::length(__s) < __min_cap
2619 ? __assign_short(__s, traits_type::length(__s))
2620 : __assign_external(__s, traits_type::length(__s)))
2621 : __assign_external(__s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002622}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002623// append
2624
2625template <class _CharT, class _Traits, class _Allocator>
2626basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002627basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002628{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002629 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002630 size_type __cap = capacity();
2631 size_type __sz = size();
2632 if (__cap - __sz >= __n)
2633 {
2634 if (__n)
2635 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002636 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002637 traits_type::copy(__p + __sz, __s, __n);
2638 __sz += __n;
2639 __set_size(__sz);
2640 traits_type::assign(__p[__sz], value_type());
2641 }
2642 }
2643 else
2644 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2645 return *this;
2646}
2647
2648template <class _CharT, class _Traits, class _Allocator>
2649basic_string<_CharT, _Traits, _Allocator>&
2650basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2651{
2652 if (__n)
2653 {
2654 size_type __cap = capacity();
2655 size_type __sz = size();
2656 if (__cap - __sz < __n)
2657 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2658 pointer __p = __get_pointer();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002659 traits_type::assign(_VSTD::__to_address(__p) + __sz, __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002660 __sz += __n;
2661 __set_size(__sz);
2662 traits_type::assign(__p[__sz], value_type());
2663 }
2664 return *this;
2665}
2666
2667template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00002668inline void
2669basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n)
2670{
2671 if (__n)
2672 {
2673 size_type __cap = capacity();
2674 size_type __sz = size();
2675 if (__cap - __sz < __n)
2676 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2677 pointer __p = __get_pointer();
2678 __sz += __n;
2679 __set_size(__sz);
2680 traits_type::assign(__p[__sz], value_type());
2681 }
2682}
2683
2684template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002685void
2686basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2687{
Howard Hinnant68bf1812013-04-30 21:44:48 +00002688 bool __is_short = !__is_long();
2689 size_type __cap;
2690 size_type __sz;
2691 if (__is_short)
2692 {
2693 __cap = __min_cap - 1;
2694 __sz = __get_short_size();
2695 }
2696 else
2697 {
2698 __cap = __get_long_cap() - 1;
2699 __sz = __get_long_size();
2700 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002701 if (__sz == __cap)
Howard Hinnant68bf1812013-04-30 21:44:48 +00002702 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002703 __grow_by(__cap, 1, __sz, __sz, 0);
Louis Dionne82d2b2c2021-11-16 11:26:56 -05002704 __is_short = false; // the string is always long after __grow_by
Howard Hinnant68bf1812013-04-30 21:44:48 +00002705 }
2706 pointer __p;
2707 if (__is_short)
2708 {
2709 __p = __get_short_pointer() + __sz;
2710 __set_short_size(__sz+1);
2711 }
2712 else
2713 {
2714 __p = __get_long_pointer() + __sz;
2715 __set_long_size(__sz+1);
2716 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002717 traits_type::assign(*__p, __c);
2718 traits_type::assign(*++__p, value_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002719}
2720
Howard Hinnantc51e1022010-05-11 19:42:16 +00002721template <class _CharT, class _Traits, class _Allocator>
2722template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002723__enable_if_t
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002724<
2725 __is_cpp17_forward_iterator<_ForwardIterator>::value,
2726 basic_string<_CharT, _Traits, _Allocator>&
2727>
2728basic_string<_CharT, _Traits, _Allocator>::append(
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002729 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002730{
2731 size_type __sz = size();
2732 size_type __cap = capacity();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002733 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002734 if (__n)
2735 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002736 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2737 !__addr_in_range(*__first))
Marshall Clow958362f2016-09-05 01:54:30 +00002738 {
2739 if (__cap - __sz < __n)
2740 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2741 pointer __p = __get_pointer() + __sz;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002742 for (; __first != __last; ++__p, (void) ++__first)
Marshall Clow958362f2016-09-05 01:54:30 +00002743 traits_type::assign(*__p, *__first);
2744 traits_type::assign(*__p, value_type());
2745 __set_size(__sz + __n);
2746 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002747 else
2748 {
2749 const basic_string __temp(__first, __last, __alloc());
2750 append(__temp.data(), __temp.size());
2751 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002752 }
2753 return *this;
2754}
2755
2756template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002757inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002758basic_string<_CharT, _Traits, _Allocator>&
2759basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2760{
2761 return append(__str.data(), __str.size());
2762}
2763
2764template <class _CharT, class _Traits, class _Allocator>
2765basic_string<_CharT, _Traits, _Allocator>&
2766basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2767{
2768 size_type __sz = __str.size();
2769 if (__pos > __sz)
2770 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002771 return append(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002772}
2773
2774template <class _CharT, class _Traits, class _Allocator>
Marshall Clow62953962016-10-03 23:40:48 +00002775template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002776 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002777 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002778 __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 +00002779 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002780 >
Marshall Clow82513342016-09-24 22:45:42 +00002781basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002782{
Marshall Clow82513342016-09-24 22:45:42 +00002783 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002784 size_type __sz = __sv.size();
2785 if (__pos > __sz)
2786 this->__throw_out_of_range();
2787 return append(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2788}
2789
2790template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002791basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002792basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002793{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002794 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002795 return append(__s, traits_type::length(__s));
2796}
2797
2798// insert
2799
2800template <class _CharT, class _Traits, class _Allocator>
2801basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002802basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002803{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002804 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002805 size_type __sz = size();
2806 if (__pos > __sz)
2807 this->__throw_out_of_range();
2808 size_type __cap = capacity();
2809 if (__cap - __sz >= __n)
2810 {
2811 if (__n)
2812 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002813 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002814 size_type __n_move = __sz - __pos;
2815 if (__n_move != 0)
2816 {
2817 if (__p + __pos <= __s && __s < __p + __sz)
2818 __s += __n;
2819 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2820 }
2821 traits_type::move(__p + __pos, __s, __n);
2822 __sz += __n;
2823 __set_size(__sz);
2824 traits_type::assign(__p[__sz], value_type());
2825 }
2826 }
2827 else
2828 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2829 return *this;
2830}
2831
2832template <class _CharT, class _Traits, class _Allocator>
2833basic_string<_CharT, _Traits, _Allocator>&
2834basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2835{
2836 size_type __sz = size();
2837 if (__pos > __sz)
2838 this->__throw_out_of_range();
2839 if (__n)
2840 {
2841 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002842 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002843 if (__cap - __sz >= __n)
2844 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002845 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002846 size_type __n_move = __sz - __pos;
2847 if (__n_move != 0)
2848 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2849 }
2850 else
2851 {
2852 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002853 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002854 }
2855 traits_type::assign(__p + __pos, __n, __c);
2856 __sz += __n;
2857 __set_size(__sz);
2858 traits_type::assign(__p[__sz], value_type());
2859 }
2860 return *this;
2861}
2862
2863template <class _CharT, class _Traits, class _Allocator>
2864template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002865__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002866<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002867 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Marshall Clow039b2f02016-01-13 21:54:34 +00002868 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002869>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002870basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2871{
Nikolas Klausereed25832021-12-15 01:32:30 +01002872 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2873 "string::insert(iterator, range) called with an iterator not"
2874 " referring to this string");
2875 const basic_string __temp(__first, __last, __alloc());
2876 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002877}
2878
2879template <class _CharT, class _Traits, class _Allocator>
2880template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002881__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002882<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002883 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002884 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002885>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002886basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2887{
Nikolas Klausereed25832021-12-15 01:32:30 +01002888 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2889 "string::insert(iterator, range) called with an iterator not"
2890 " referring to this string");
2891
Howard Hinnantc51e1022010-05-11 19:42:16 +00002892 size_type __ip = static_cast<size_type>(__pos - begin());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002893 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002894 if (__n)
2895 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002896 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2897 !__addr_in_range(*__first))
2898 {
2899 size_type __sz = size();
2900 size_type __cap = capacity();
2901 value_type* __p;
2902 if (__cap - __sz >= __n)
2903 {
2904 __p = _VSTD::__to_address(__get_pointer());
2905 size_type __n_move = __sz - __ip;
2906 if (__n_move != 0)
2907 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
2908 }
2909 else
2910 {
2911 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
2912 __p = _VSTD::__to_address(__get_long_pointer());
2913 }
2914 __sz += __n;
2915 __set_size(__sz);
2916 traits_type::assign(__p[__sz], value_type());
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002917 for (__p += __ip; __first != __last; ++__p, (void) ++__first)
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002918 traits_type::assign(*__p, *__first);
2919 }
2920 else
Marshall Clow958362f2016-09-05 01:54:30 +00002921 {
2922 const basic_string __temp(__first, __last, __alloc());
2923 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
2924 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002925 }
2926 return begin() + __ip;
2927}
2928
2929template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002930inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002931basic_string<_CharT, _Traits, _Allocator>&
2932basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2933{
2934 return insert(__pos1, __str.data(), __str.size());
2935}
2936
2937template <class _CharT, class _Traits, class _Allocator>
2938basic_string<_CharT, _Traits, _Allocator>&
2939basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2940 size_type __pos2, size_type __n)
2941{
2942 size_type __str_sz = __str.size();
2943 if (__pos2 > __str_sz)
2944 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002945 return insert(__pos1, __str.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002946}
2947
2948template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002949template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002950__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002951<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002952 __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 +00002953 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002954>
Marshall Clow82513342016-09-24 22:45:42 +00002955basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002956 size_type __pos2, size_type __n)
2957{
Marshall Clow82513342016-09-24 22:45:42 +00002958 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002959 size_type __str_sz = __sv.size();
2960 if (__pos2 > __str_sz)
2961 this->__throw_out_of_range();
2962 return insert(__pos1, __sv.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
2963}
2964
2965template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002966basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002967basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002968{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002969 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002970 return insert(__pos, __s, traits_type::length(__s));
2971}
2972
2973template <class _CharT, class _Traits, class _Allocator>
2974typename basic_string<_CharT, _Traits, _Allocator>::iterator
2975basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2976{
2977 size_type __ip = static_cast<size_type>(__pos - begin());
2978 size_type __sz = size();
2979 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002980 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002981 if (__cap == __sz)
2982 {
2983 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002984 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002985 }
2986 else
2987 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002988 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002989 size_type __n_move = __sz - __ip;
2990 if (__n_move != 0)
2991 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2992 }
2993 traits_type::assign(__p[__ip], __c);
2994 traits_type::assign(__p[++__sz], value_type());
2995 __set_size(__sz);
2996 return begin() + static_cast<difference_type>(__ip);
2997}
2998
2999template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003000inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003001typename basic_string<_CharT, _Traits, _Allocator>::iterator
3002basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
3003{
Nikolas Klausereed25832021-12-15 01:32:30 +01003004 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3005 "string::insert(iterator, n, value) called with an iterator not"
3006 " referring to this string");
3007 difference_type __p = __pos - begin();
3008 insert(static_cast<size_type>(__p), __n, __c);
3009 return begin() + __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003010}
3011
3012// replace
3013
3014template <class _CharT, class _Traits, class _Allocator>
3015basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003016basic_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 +00003017 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00003018{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003019 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003020 size_type __sz = size();
3021 if (__pos > __sz)
3022 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003023 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003024 size_type __cap = capacity();
3025 if (__cap - __sz + __n1 >= __n2)
3026 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003027 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003028 if (__n1 != __n2)
3029 {
3030 size_type __n_move = __sz - __pos - __n1;
3031 if (__n_move != 0)
3032 {
3033 if (__n1 > __n2)
3034 {
3035 traits_type::move(__p + __pos, __s, __n2);
3036 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003037 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003038 }
3039 if (__p + __pos < __s && __s < __p + __sz)
3040 {
3041 if (__p + __pos + __n1 <= __s)
3042 __s += __n2 - __n1;
3043 else // __p + __pos < __s < __p + __pos + __n1
3044 {
3045 traits_type::move(__p + __pos, __s, __n1);
3046 __pos += __n1;
3047 __s += __n2;
3048 __n2 -= __n1;
3049 __n1 = 0;
3050 }
3051 }
3052 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3053 }
3054 }
3055 traits_type::move(__p + __pos, __s, __n2);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003056 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003057 }
3058 else
3059 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
3060 return *this;
3061}
3062
3063template <class _CharT, class _Traits, class _Allocator>
3064basic_string<_CharT, _Traits, _Allocator>&
3065basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
3066{
3067 size_type __sz = size();
3068 if (__pos > __sz)
3069 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003070 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003071 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00003072 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003073 if (__cap - __sz + __n1 >= __n2)
3074 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003075 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003076 if (__n1 != __n2)
3077 {
3078 size_type __n_move = __sz - __pos - __n1;
3079 if (__n_move != 0)
3080 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3081 }
3082 }
3083 else
3084 {
3085 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003086 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003087 }
3088 traits_type::assign(__p + __pos, __n2, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003089 return __null_terminate_at(__p, __sz - (__n1 - __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003090}
3091
3092template <class _CharT, class _Traits, class _Allocator>
3093template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003094__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00003095<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003096 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003097 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003098>
Howard Hinnant990d6e82010-11-17 21:11:40 +00003099basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003100 _InputIterator __j1, _InputIterator __j2)
3101{
Marshall Clow958362f2016-09-05 01:54:30 +00003102 const basic_string __temp(__j1, __j2, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00003103 return this->replace(__i1, __i2, __temp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003104}
3105
3106template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003107inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003108basic_string<_CharT, _Traits, _Allocator>&
3109basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
3110{
3111 return replace(__pos1, __n1, __str.data(), __str.size());
3112}
3113
3114template <class _CharT, class _Traits, class _Allocator>
3115basic_string<_CharT, _Traits, _Allocator>&
3116basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
3117 size_type __pos2, size_type __n2)
3118{
3119 size_type __str_sz = __str.size();
3120 if (__pos2 > __str_sz)
3121 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003122 return replace(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003123}
3124
3125template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003126template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003127__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003128<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003129 __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 +00003130 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003131>
Marshall Clow82513342016-09-24 22:45:42 +00003132basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003133 size_type __pos2, size_type __n2)
3134{
Marshall Clow82513342016-09-24 22:45:42 +00003135 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003136 size_type __str_sz = __sv.size();
3137 if (__pos2 > __str_sz)
3138 this->__throw_out_of_range();
3139 return replace(__pos1, __n1, __sv.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
3140}
3141
3142template <class _CharT, class _Traits, class _Allocator>
3143basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003144basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003145{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003146 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003147 return replace(__pos, __n1, __s, traits_type::length(__s));
3148}
3149
3150template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003151inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003152basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003153basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003154{
3155 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
3156 __str.data(), __str.size());
3157}
3158
3159template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003160inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003161basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003162basic_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 +00003163{
3164 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
3165}
3166
3167template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003168inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003169basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003170basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003171{
3172 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
3173}
3174
3175template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003176inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003177basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003178basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003179{
3180 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
3181}
3182
3183// erase
3184
Martijn Velsa81fc792020-02-26 13:25:43 -05003185// 'externally instantiated' erase() implementation, called when __n != npos.
3186// Does not check __pos against size()
Howard Hinnantc51e1022010-05-11 19:42:16 +00003187template <class _CharT, class _Traits, class _Allocator>
Martijn Velsa81fc792020-02-26 13:25:43 -05003188void
3189basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(
3190 size_type __pos, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003191{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003192 if (__n)
3193 {
Martijn Velsa81fc792020-02-26 13:25:43 -05003194 size_type __sz = size();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003195 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003196 __n = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003197 size_type __n_move = __sz - __pos - __n;
3198 if (__n_move != 0)
3199 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003200 __null_terminate_at(__p, __sz - __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003201 }
Martijn Velsa81fc792020-02-26 13:25:43 -05003202}
3203
3204template <class _CharT, class _Traits, class _Allocator>
3205basic_string<_CharT, _Traits, _Allocator>&
3206basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos,
3207 size_type __n) {
3208 if (__pos > size()) this->__throw_out_of_range();
3209 if (__n == npos) {
3210 __erase_to_end(__pos);
3211 } else {
3212 __erase_external_with_move(__pos, __n);
3213 }
3214 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003215}
3216
3217template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003218inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003219typename basic_string<_CharT, _Traits, _Allocator>::iterator
3220basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
3221{
Nikolas Klausereed25832021-12-15 01:32:30 +01003222 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3223 "string::erase(iterator) called with an iterator not"
3224 " referring to this string");
3225
3226 _LIBCPP_ASSERT(__pos != end(), "string::erase(iterator) called with a non-dereferenceable iterator");
3227 iterator __b = begin();
3228 size_type __r = static_cast<size_type>(__pos - __b);
3229 erase(__r, 1);
3230 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003231}
3232
3233template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003234inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003235typename basic_string<_CharT, _Traits, _Allocator>::iterator
3236basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
3237{
Nikolas Klausereed25832021-12-15 01:32:30 +01003238 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
3239 "string::erase(iterator, iterator) called with an iterator not"
3240 " referring to this string");
3241
3242 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
3243 iterator __b = begin();
3244 size_type __r = static_cast<size_type>(__first - __b);
3245 erase(__r, static_cast<size_type>(__last - __first));
3246 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003247}
3248
3249template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003250inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003251void
3252basic_string<_CharT, _Traits, _Allocator>::pop_back()
3253{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003254 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003255 __erase_to_end(size() - 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003256}
3257
3258template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003259inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003260void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003261basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003262{
3263 __invalidate_all_iterators();
3264 if (__is_long())
3265 {
3266 traits_type::assign(*__get_long_pointer(), value_type());
3267 __set_long_size(0);
3268 }
3269 else
3270 {
3271 traits_type::assign(*__get_short_pointer(), value_type());
3272 __set_short_size(0);
3273 }
3274}
3275
3276template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003277inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003278void
3279basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
3280{
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003281 __null_terminate_at(_VSTD::__to_address(__get_pointer()), __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003282}
3283
3284template <class _CharT, class _Traits, class _Allocator>
3285void
3286basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
3287{
3288 size_type __sz = size();
3289 if (__n > __sz)
3290 append(__n - __sz, __c);
3291 else
3292 __erase_to_end(__n);
3293}
3294
3295template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00003296inline void
3297basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n)
3298{
3299 size_type __sz = size();
3300 if (__n > __sz) {
3301 __append_default_init(__n - __sz);
3302 } else
3303 __erase_to_end(__n);
3304}
3305
3306template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003307inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003308typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003309basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003310{
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003311 size_type __m = __alloc_traits::max_size(__alloc());
Eric Fiseliere9cc5922017-10-17 13:16:01 +00003312#ifdef _LIBCPP_BIG_ENDIAN
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003313 return (__m <= ~__long_mask ? __m : __m/2) - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003314#else
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003315 return __m - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003316#endif
3317}
3318
3319template <class _CharT, class _Traits, class _Allocator>
3320void
Marek Kurdejc9848142020-11-26 10:07:16 +01003321basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __requested_capacity)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003322{
Marek Kurdejc9848142020-11-26 10:07:16 +01003323 if (__requested_capacity > max_size())
Howard Hinnantc51e1022010-05-11 19:42:16 +00003324 this->__throw_length_error();
Marek Kurdejc9848142020-11-26 10:07:16 +01003325
3326#if _LIBCPP_STD_VER > 17
3327 // Reserve never shrinks as of C++20.
3328 if (__requested_capacity <= capacity()) return;
3329#endif
3330
3331 size_type __target_capacity = _VSTD::max(__requested_capacity, size());
3332 __target_capacity = __recommend(__target_capacity);
3333 if (__target_capacity == capacity()) return;
3334
3335 __shrink_or_extend(__target_capacity);
3336}
3337
3338template <class _CharT, class _Traits, class _Allocator>
Louis Dionnee38b8642021-12-13 14:15:21 -05003339inline
Marek Kurdejc9848142020-11-26 10:07:16 +01003340void
3341basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT
3342{
3343 size_type __target_capacity = __recommend(size());
3344 if (__target_capacity == capacity()) return;
3345
3346 __shrink_or_extend(__target_capacity);
3347}
3348
3349template <class _CharT, class _Traits, class _Allocator>
Louis Dionnee38b8642021-12-13 14:15:21 -05003350inline
Marek Kurdejc9848142020-11-26 10:07:16 +01003351void
3352basic_string<_CharT, _Traits, _Allocator>::__shrink_or_extend(size_type __target_capacity)
3353{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003354 size_type __cap = capacity();
3355 size_type __sz = size();
Marek Kurdejc9848142020-11-26 10:07:16 +01003356
3357 pointer __new_data, __p;
3358 bool __was_long, __now_long;
3359 if (__target_capacity == __min_cap - 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003360 {
Marek Kurdejc9848142020-11-26 10:07:16 +01003361 __was_long = true;
3362 __now_long = false;
3363 __new_data = __get_short_pointer();
3364 __p = __get_long_pointer();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003365 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003366 else
3367 {
3368 if (__target_capacity > __cap)
3369 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3370 else
3371 {
3372 #ifndef _LIBCPP_NO_EXCEPTIONS
3373 try
3374 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003375 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003376 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3377 #ifndef _LIBCPP_NO_EXCEPTIONS
3378 }
3379 catch (...)
3380 {
3381 return;
3382 }
3383 #else // _LIBCPP_NO_EXCEPTIONS
3384 if (__new_data == nullptr)
3385 return;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003386 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003387 }
3388 __now_long = true;
3389 __was_long = __is_long();
3390 __p = __get_pointer();
3391 }
3392 traits_type::copy(_VSTD::__to_address(__new_data),
3393 _VSTD::__to_address(__p), size()+1);
3394 if (__was_long)
3395 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
3396 if (__now_long)
3397 {
3398 __set_long_cap(__target_capacity+1);
3399 __set_long_size(__sz);
3400 __set_long_pointer(__new_data);
3401 }
3402 else
3403 __set_short_size(__sz);
3404 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003405}
3406
3407template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003408inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003409typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003410basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003411{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003412 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003413 return *(data() + __pos);
3414}
3415
3416template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003417inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003418typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003419basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003420{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003421 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003422 return *(__get_pointer() + __pos);
3423}
3424
3425template <class _CharT, class _Traits, class _Allocator>
3426typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3427basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3428{
3429 if (__n >= size())
3430 this->__throw_out_of_range();
3431 return (*this)[__n];
3432}
3433
3434template <class _CharT, class _Traits, class _Allocator>
3435typename basic_string<_CharT, _Traits, _Allocator>::reference
3436basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
3437{
3438 if (__n >= size())
3439 this->__throw_out_of_range();
3440 return (*this)[__n];
3441}
3442
3443template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003444inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003445typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003446basic_string<_CharT, _Traits, _Allocator>::front() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003447{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003448 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003449 return *__get_pointer();
3450}
3451
3452template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003453inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003454typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003455basic_string<_CharT, _Traits, _Allocator>::front() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003456{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003457 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003458 return *data();
3459}
3460
3461template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003462inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003463typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003464basic_string<_CharT, _Traits, _Allocator>::back() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003465{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003466 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003467 return *(__get_pointer() + size() - 1);
3468}
3469
3470template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003471inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003472typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003473basic_string<_CharT, _Traits, _Allocator>::back() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003474{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003475 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003476 return *(data() + size() - 1);
3477}
3478
3479template <class _CharT, class _Traits, class _Allocator>
3480typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003481basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003482{
3483 size_type __sz = size();
3484 if (__pos > __sz)
3485 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003486 size_type __rlen = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003487 traits_type::copy(__s, data() + __pos, __rlen);
3488 return __rlen;
3489}
3490
3491template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003492inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003493basic_string<_CharT, _Traits, _Allocator>
3494basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3495{
3496 return basic_string(*this, __pos, __n, __alloc());
3497}
3498
3499template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003500inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003501void
3502basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003503#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00003504 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00003505#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00003506 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003507 __is_nothrow_swappable<allocator_type>::value)
3508#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003509{
Louis Dionneba400782020-10-02 15:02:52 -04003510#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01003511 if (!__libcpp_is_constant_evaluated()) {
3512 if (!__is_long())
3513 __get_db()->__invalidate_all(this);
3514 if (!__str.__is_long())
3515 __get_db()->__invalidate_all(&__str);
3516 __get_db()->swap(this, &__str);
3517 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00003518#endif
Eric Fiselier9bf691f2016-12-28 05:53:01 +00003519 _LIBCPP_ASSERT(
3520 __alloc_traits::propagate_on_container_swap::value ||
3521 __alloc_traits::is_always_equal::value ||
3522 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003523 _VSTD::swap(__r_.first(), __str.__r_.first());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05003524 _VSTD::__swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003525}
3526
3527// find
3528
3529template <class _Traits>
3530struct _LIBCPP_HIDDEN __traits_eq
3531{
3532 typedef typename _Traits::char_type char_type;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003533 _LIBCPP_INLINE_VISIBILITY
3534 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3535 {return _Traits::eq(__x, __y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003536};
3537
3538template<class _CharT, class _Traits, class _Allocator>
3539typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003540basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003541 size_type __pos,
3542 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003543{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003544 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003545 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003546 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003547}
3548
3549template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003550inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003551typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003552basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3553 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003554{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003555 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003556 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003557}
3558
3559template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003560template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003561__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003562<
3563 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3564 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003565>
Marshall Clowe46031a2018-07-02 18:41:15 +00003566basic_string<_CharT, _Traits, _Allocator>::find(const _Tp &__t,
zoecarver1997e0a2021-02-05 11:54:47 -08003567 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003568{
Marshall Clowe46031a2018-07-02 18:41:15 +00003569 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003570 return __str_find<value_type, size_type, traits_type, npos>
3571 (data(), size(), __sv.data(), __pos, __sv.size());
3572}
3573
3574template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003575inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003576typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003577basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003578 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003579{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003580 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003581 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003582 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003583}
3584
3585template<class _CharT, class _Traits, class _Allocator>
3586typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003587basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3588 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003589{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003590 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003591 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003592}
3593
3594// rfind
3595
3596template<class _CharT, class _Traits, class _Allocator>
3597typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003598basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003599 size_type __pos,
3600 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003601{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003602 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003603 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003604 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003605}
3606
3607template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003608inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003609typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003610basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3611 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003612{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003613 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003614 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003615}
3616
3617template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003618template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003619__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003620<
3621 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3622 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003623>
Marshall Clowe46031a2018-07-02 18:41:15 +00003624basic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003625 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003626{
Marshall Clowe46031a2018-07-02 18:41:15 +00003627 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003628 return __str_rfind<value_type, size_type, traits_type, npos>
3629 (data(), size(), __sv.data(), __pos, __sv.size());
3630}
3631
3632template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003633inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003634typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003635basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003636 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003637{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003638 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003639 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003640 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003641}
3642
3643template<class _CharT, class _Traits, class _Allocator>
3644typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003645basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3646 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003647{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003648 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003649 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003650}
3651
3652// find_first_of
3653
3654template<class _CharT, class _Traits, class _Allocator>
3655typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003656basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003657 size_type __pos,
3658 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003659{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003660 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003661 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003662 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003663}
3664
3665template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003666inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003667typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003668basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3669 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003670{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003671 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003672 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003673}
3674
3675template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003676template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003677__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003678<
3679 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3680 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003681>
Marshall Clowe46031a2018-07-02 18:41:15 +00003682basic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003683 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003684{
Marshall Clowe46031a2018-07-02 18:41:15 +00003685 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003686 return __str_find_first_of<value_type, size_type, traits_type, npos>
3687 (data(), size(), __sv.data(), __pos, __sv.size());
3688}
3689
3690template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003691inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003692typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003693basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003694 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003695{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003696 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003697 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003698 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003699}
3700
3701template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003702inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003703typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003704basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3705 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003706{
3707 return find(__c, __pos);
3708}
3709
3710// find_last_of
3711
3712template<class _CharT, class _Traits, class _Allocator>
3713typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003714basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003715 size_type __pos,
3716 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003717{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003718 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003719 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003720 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003721}
3722
3723template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003724inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003725typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003726basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3727 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003728{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003729 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003730 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003731}
3732
3733template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003734template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003735__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003736<
3737 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3738 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003739>
Marshall Clowe46031a2018-07-02 18:41:15 +00003740basic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003741 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003742{
Marshall Clowe46031a2018-07-02 18:41:15 +00003743 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003744 return __str_find_last_of<value_type, size_type, traits_type, npos>
3745 (data(), size(), __sv.data(), __pos, __sv.size());
3746}
3747
3748template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003749inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003750typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003751basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003752 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003753{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003754 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003755 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003756 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003757}
3758
3759template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003760inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003761typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003762basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3763 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003764{
3765 return rfind(__c, __pos);
3766}
3767
3768// find_first_not_of
3769
3770template<class _CharT, class _Traits, class _Allocator>
3771typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003772basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003773 size_type __pos,
3774 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003775{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003776 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003777 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003778 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003779}
3780
3781template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003782inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003783typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003784basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3785 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003786{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003787 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003788 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003789}
3790
3791template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003792template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003793__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003794<
3795 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3796 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003797>
Marshall Clowe46031a2018-07-02 18:41:15 +00003798basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003799 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003800{
Marshall Clowe46031a2018-07-02 18:41:15 +00003801 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003802 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3803 (data(), size(), __sv.data(), __pos, __sv.size());
3804}
3805
3806template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003807inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003808typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003809basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003810 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003811{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003812 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003813 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003814 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003815}
3816
3817template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003818inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003819typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003820basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3821 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003822{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003823 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003824 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003825}
3826
3827// find_last_not_of
3828
3829template<class _CharT, class _Traits, class _Allocator>
3830typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003831basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003832 size_type __pos,
3833 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003834{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003835 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003836 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003837 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003838}
3839
3840template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003841inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003842typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003843basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3844 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003845{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003846 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003847 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003848}
3849
3850template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003851template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003852__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003853<
3854 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3855 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003856>
Marshall Clowe46031a2018-07-02 18:41:15 +00003857basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003858 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003859{
Marshall Clowe46031a2018-07-02 18:41:15 +00003860 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003861 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3862 (data(), size(), __sv.data(), __pos, __sv.size());
3863}
3864
3865template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003866inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003867typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003868basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003869 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003870{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003871 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003872 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003873 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003874}
3875
3876template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003877inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003878typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003879basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3880 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003881{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003882 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003883 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003884}
3885
3886// compare
3887
3888template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003889template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003890__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003891<
3892 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3893 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003894>
zoecarver1997e0a2021-02-05 11:54:47 -08003895basic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003896{
Marshall Clowe46031a2018-07-02 18:41:15 +00003897 __self_view __sv = __t;
Howard Hinnantb0485532011-07-24 21:45:06 +00003898 size_t __lhs_sz = size();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003899 size_t __rhs_sz = __sv.size();
3900 int __result = traits_type::compare(data(), __sv.data(),
Howard Hinnantb0485532011-07-24 21:45:06 +00003901 _VSTD::min(__lhs_sz, __rhs_sz));
3902 if (__result != 0)
3903 return __result;
3904 if (__lhs_sz < __rhs_sz)
3905 return -1;
3906 if (__lhs_sz > __rhs_sz)
3907 return 1;
3908 return 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003909}
3910
3911template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003912inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003913int
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003914basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003915{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003916 return compare(__self_view(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003917}
3918
3919template <class _CharT, class _Traits, class _Allocator>
3920int
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003921basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3922 size_type __n1,
Howard Hinnantd17880b2013-06-28 16:59:19 +00003923 const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003924 size_type __n2) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003925{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003926 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003927 size_type __sz = size();
3928 if (__pos1 > __sz || __n2 == npos)
3929 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003930 size_type __rlen = _VSTD::min(__n1, __sz - __pos1);
3931 int __r = traits_type::compare(data() + __pos1, __s, _VSTD::min(__rlen, __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003932 if (__r == 0)
3933 {
3934 if (__rlen < __n2)
3935 __r = -1;
3936 else if (__rlen > __n2)
3937 __r = 1;
3938 }
3939 return __r;
3940}
3941
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003942template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003943template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003944__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003945<
3946 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3947 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003948>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003949basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3950 size_type __n1,
Marshall Clowe46031a2018-07-02 18:41:15 +00003951 const _Tp& __t) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003952{
Marshall Clowe46031a2018-07-02 18:41:15 +00003953 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003954 return compare(__pos1, __n1, __sv.data(), __sv.size());
3955}
3956
3957template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003958inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003959int
3960basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3961 size_type __n1,
3962 const basic_string& __str) const
3963{
3964 return compare(__pos1, __n1, __str.data(), __str.size());
3965}
3966
3967template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003968template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003969__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003970<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003971 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
3972 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00003973 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003974>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003975basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3976 size_type __n1,
Marshall Clow82513342016-09-24 22:45:42 +00003977 const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003978 size_type __pos2,
3979 size_type __n2) const
3980{
Marshall Clow82513342016-09-24 22:45:42 +00003981 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003982 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3983}
3984
3985template <class _CharT, class _Traits, class _Allocator>
3986int
3987basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3988 size_type __n1,
3989 const basic_string& __str,
3990 size_type __pos2,
3991 size_type __n2) const
3992{
3993 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
3994}
3995
3996template <class _CharT, class _Traits, class _Allocator>
3997int
3998basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
3999{
4000 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
4001 return compare(0, npos, __s, traits_type::length(__s));
4002}
4003
4004template <class _CharT, class _Traits, class _Allocator>
4005int
4006basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
4007 size_type __n1,
4008 const value_type* __s) const
4009{
4010 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
4011 return compare(__pos1, __n1, __s, traits_type::length(__s));
4012}
4013
Howard Hinnantc51e1022010-05-11 19:42:16 +00004014// __invariants
4015
4016template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004017inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004018bool
4019basic_string<_CharT, _Traits, _Allocator>::__invariants() const
4020{
4021 if (size() > capacity())
4022 return false;
4023 if (capacity() < __min_cap - 1)
4024 return false;
Bruce Mitchener170d8972020-11-24 12:53:53 -05004025 if (data() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00004026 return false;
Louis Dionne663415f2020-10-05 16:16:13 -04004027 if (data()[size()] != value_type())
Howard Hinnantc51e1022010-05-11 19:42:16 +00004028 return false;
4029 return true;
4030}
4031
Vedant Kumar55e007e2018-03-08 21:15:26 +00004032// __clear_and_shrink
4033
4034template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004035inline
Louis Dionne173f29e2019-05-29 16:01:36 +00004036void
Marshall Clowe60a7182018-05-29 17:04:37 +00004037basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
Vedant Kumar55e007e2018-03-08 21:15:26 +00004038{
4039 clear();
4040 if(__is_long())
4041 {
4042 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
4043 __set_long_cap(0);
4044 __set_short_size(0);
Louis Dionne663415f2020-10-05 16:16:13 -04004045 traits_type::assign(*__get_short_pointer(), value_type());
Vedant Kumar55e007e2018-03-08 21:15:26 +00004046 }
Louis Dionne173f29e2019-05-29 16:01:36 +00004047}
Vedant Kumar55e007e2018-03-08 21:15:26 +00004048
Howard Hinnantc51e1022010-05-11 19:42:16 +00004049// operator==
4050
4051template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004052inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004053bool
4054operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004055 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004056{
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004057 size_t __lhs_sz = __lhs.size();
4058 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
4059 __rhs.data(),
4060 __lhs_sz) == 0;
4061}
4062
4063template<class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004064inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004065bool
4066operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
4067 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
4068{
4069 size_t __lhs_sz = __lhs.size();
4070 if (__lhs_sz != __rhs.size())
4071 return false;
4072 const char* __lp = __lhs.data();
4073 const char* __rp = __rhs.data();
4074 if (__lhs.__is_long())
4075 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
4076 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
4077 if (*__lp != *__rp)
4078 return false;
4079 return true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004080}
4081
4082template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004083inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004084bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004085operator==(const _CharT* __lhs,
4086 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004087{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004088 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4089 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
4090 size_t __lhs_len = _Traits::length(__lhs);
4091 if (__lhs_len != __rhs.size()) return false;
4092 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004093}
4094
Howard Hinnantc51e1022010-05-11 19:42:16 +00004095template<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{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004101 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4102 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
4103 size_t __rhs_len = _Traits::length(__rhs);
4104 if (__rhs_len != __lhs.size()) return false;
4105 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004106}
4107
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004108template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004109inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004110bool
4111operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004112 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004113{
4114 return !(__lhs == __rhs);
4115}
4116
4117template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004118inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004119bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004120operator!=(const _CharT* __lhs,
4121 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004122{
4123 return !(__lhs == __rhs);
4124}
4125
4126template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004127inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004128bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004129operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4130 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004131{
4132 return !(__lhs == __rhs);
4133}
4134
4135// operator<
4136
4137template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004138inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004139bool
4140operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004141 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004142{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004143 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004144}
4145
4146template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004147inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004148bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004149operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4150 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004151{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004152 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004153}
4154
4155template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004156inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004157bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004158operator< (const _CharT* __lhs,
4159 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004160{
4161 return __rhs.compare(__lhs) > 0;
4162}
4163
Howard Hinnantc51e1022010-05-11 19:42:16 +00004164// operator>
4165
4166template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004167inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004168bool
4169operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004170 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004171{
4172 return __rhs < __lhs;
4173}
4174
4175template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004176inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004177bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004178operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4179 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004180{
4181 return __rhs < __lhs;
4182}
4183
4184template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004185inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004186bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004187operator> (const _CharT* __lhs,
4188 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004189{
4190 return __rhs < __lhs;
4191}
4192
4193// operator<=
4194
4195template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004196inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004197bool
4198operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004199 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004200{
4201 return !(__rhs < __lhs);
4202}
4203
4204template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004205inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004206bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004207operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4208 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004209{
4210 return !(__rhs < __lhs);
4211}
4212
4213template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004214inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004215bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004216operator<=(const _CharT* __lhs,
4217 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004218{
4219 return !(__rhs < __lhs);
4220}
4221
4222// operator>=
4223
4224template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004225inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004226bool
4227operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004228 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004229{
4230 return !(__lhs < __rhs);
4231}
4232
4233template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004234inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004235bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004236operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4237 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004238{
4239 return !(__lhs < __rhs);
4240}
4241
4242template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004243inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004244bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004245operator>=(const _CharT* __lhs,
4246 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004247{
4248 return !(__lhs < __rhs);
4249}
4250
4251// operator +
4252
4253template<class _CharT, class _Traits, class _Allocator>
4254basic_string<_CharT, _Traits, _Allocator>
4255operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4256 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4257{
4258 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4259 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4260 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4261 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4262 __r.append(__rhs.data(), __rhs_sz);
4263 return __r;
4264}
4265
4266template<class _CharT, class _Traits, class _Allocator>
4267basic_string<_CharT, _Traits, _Allocator>
4268operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4269{
4270 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4271 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = _Traits::length(__lhs);
4272 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4273 __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz);
4274 __r.append(__rhs.data(), __rhs_sz);
4275 return __r;
4276}
4277
4278template<class _CharT, class _Traits, class _Allocator>
4279basic_string<_CharT, _Traits, _Allocator>
4280operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4281{
4282 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4283 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4284 __r.__init(&__lhs, 1, 1 + __rhs_sz);
4285 __r.append(__rhs.data(), __rhs_sz);
4286 return __r;
4287}
4288
4289template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004290inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004291basic_string<_CharT, _Traits, _Allocator>
4292operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
4293{
4294 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4295 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4296 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = _Traits::length(__rhs);
4297 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4298 __r.append(__rhs, __rhs_sz);
4299 return __r;
4300}
4301
4302template<class _CharT, class _Traits, class _Allocator>
4303basic_string<_CharT, _Traits, _Allocator>
4304operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
4305{
4306 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4307 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4308 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1);
4309 __r.push_back(__rhs);
4310 return __r;
4311}
4312
Eric Fiselierfc92be82017-04-19 00:28:44 +00004313#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004314
4315template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004316inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004317basic_string<_CharT, _Traits, _Allocator>
4318operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4319{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004320 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004321}
4322
4323template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004324inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004325basic_string<_CharT, _Traits, _Allocator>
4326operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4327{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004328 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004329}
4330
4331template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004332inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004333basic_string<_CharT, _Traits, _Allocator>
4334operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4335{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004336 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004337}
4338
4339template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004340inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004341basic_string<_CharT, _Traits, _Allocator>
4342operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4343{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004344 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004345}
4346
4347template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004348inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004349basic_string<_CharT, _Traits, _Allocator>
4350operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4351{
4352 __rhs.insert(__rhs.begin(), __lhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004353 return _VSTD::move(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004354}
4355
4356template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004357inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004358basic_string<_CharT, _Traits, _Allocator>
4359operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
4360{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004361 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004362}
4363
4364template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004365inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004366basic_string<_CharT, _Traits, _Allocator>
4367operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
4368{
4369 __lhs.push_back(__rhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004370 return _VSTD::move(__lhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004371}
4372
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004373#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004374
4375// swap
4376
4377template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004378inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004379void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004380swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +00004381 basic_string<_CharT, _Traits, _Allocator>& __rhs)
4382 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004383{
4384 __lhs.swap(__rhs);
4385}
4386
Bruce Mitchener170d8972020-11-24 12:53:53 -05004387_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = nullptr, int __base = 10);
4388_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = nullptr, int __base = 10);
4389_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = nullptr, int __base = 10);
4390_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = nullptr, int __base = 10);
4391_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004392
Bruce Mitchener170d8972020-11-24 12:53:53 -05004393_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = nullptr);
4394_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = nullptr);
4395_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004396
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004397_LIBCPP_FUNC_VIS string to_string(int __val);
4398_LIBCPP_FUNC_VIS string to_string(unsigned __val);
4399_LIBCPP_FUNC_VIS string to_string(long __val);
4400_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
4401_LIBCPP_FUNC_VIS string to_string(long long __val);
4402_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
4403_LIBCPP_FUNC_VIS string to_string(float __val);
4404_LIBCPP_FUNC_VIS string to_string(double __val);
4405_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004406
Louis Dionne89258142021-08-23 15:32:36 -04004407#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Bruce Mitchener170d8972020-11-24 12:53:53 -05004408_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4409_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4410_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4411_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4412_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004413
Bruce Mitchener170d8972020-11-24 12:53:53 -05004414_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = nullptr);
4415_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = nullptr);
4416_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004417
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004418_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
4419_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
4420_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
4421_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
4422_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
4423_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
4424_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
4425_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
4426_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Louis Dionne89258142021-08-23 15:32:36 -04004427#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004428
Howard Hinnantc51e1022010-05-11 19:42:16 +00004429template<class _CharT, class _Traits, class _Allocator>
Martin Storsjö88890c22021-02-22 01:13:13 +02004430_LIBCPP_TEMPLATE_DATA_VIS
Eric Fiselierc9fdaf12020-01-15 17:02:17 -05004431const typename basic_string<_CharT, _Traits, _Allocator>::size_type
4432 basic_string<_CharT, _Traits, _Allocator>::npos;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004433
Marshall Clow851b9ec2019-05-20 21:56:51 +00004434template <class _CharT, class _Allocator>
4435struct _LIBCPP_TEMPLATE_VIS
4436 hash<basic_string<_CharT, char_traits<_CharT>, _Allocator> >
4437 : public unary_function<
4438 basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004439{
4440 size_t
Marshall Clow851b9ec2019-05-20 21:56:51 +00004441 operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT
4442 { return __do_string_hash(__val.data(), __val.data() + __val.size()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004443};
4444
Howard Hinnantc51e1022010-05-11 19:42:16 +00004445
Howard Hinnantdc095972011-07-18 15:51:59 +00004446template<class _CharT, class _Traits, class _Allocator>
4447basic_ostream<_CharT, _Traits>&
4448operator<<(basic_ostream<_CharT, _Traits>& __os,
4449 const basic_string<_CharT, _Traits, _Allocator>& __str);
4450
4451template<class _CharT, class _Traits, class _Allocator>
4452basic_istream<_CharT, _Traits>&
4453operator>>(basic_istream<_CharT, _Traits>& __is,
4454 basic_string<_CharT, _Traits, _Allocator>& __str);
4455
4456template<class _CharT, class _Traits, class _Allocator>
4457basic_istream<_CharT, _Traits>&
4458getline(basic_istream<_CharT, _Traits>& __is,
4459 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4460
4461template<class _CharT, class _Traits, class _Allocator>
4462inline _LIBCPP_INLINE_VISIBILITY
4463basic_istream<_CharT, _Traits>&
4464getline(basic_istream<_CharT, _Traits>& __is,
4465 basic_string<_CharT, _Traits, _Allocator>& __str);
4466
Howard Hinnantdc095972011-07-18 15:51:59 +00004467template<class _CharT, class _Traits, class _Allocator>
4468inline _LIBCPP_INLINE_VISIBILITY
4469basic_istream<_CharT, _Traits>&
4470getline(basic_istream<_CharT, _Traits>&& __is,
4471 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4472
4473template<class _CharT, class _Traits, class _Allocator>
4474inline _LIBCPP_INLINE_VISIBILITY
4475basic_istream<_CharT, _Traits>&
4476getline(basic_istream<_CharT, _Traits>&& __is,
4477 basic_string<_CharT, _Traits, _Allocator>& __str);
4478
Marshall Clow29b53f22018-12-14 18:49:35 +00004479#if _LIBCPP_STD_VER > 17
Marek Kurdeja98b1412020-05-02 13:58:03 +02004480template <class _CharT, class _Traits, class _Allocator, class _Up>
Marshall Clow29b53f22018-12-14 18:49:35 +00004481inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004482 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4483 erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
4484 auto __old_size = __str.size();
4485 __str.erase(_VSTD::remove(__str.begin(), __str.end(), __v), __str.end());
4486 return __old_size - __str.size();
4487}
Marshall Clow29b53f22018-12-14 18:49:35 +00004488
Marek Kurdeja98b1412020-05-02 13:58:03 +02004489template <class _CharT, class _Traits, class _Allocator, class _Predicate>
Marshall Clow29b53f22018-12-14 18:49:35 +00004490inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004491 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4492 erase_if(basic_string<_CharT, _Traits, _Allocator>& __str,
4493 _Predicate __pred) {
4494 auto __old_size = __str.size();
4495 __str.erase(_VSTD::remove_if(__str.begin(), __str.end(), __pred),
4496 __str.end());
4497 return __old_size - __str.size();
4498}
Marshall Clow29b53f22018-12-14 18:49:35 +00004499#endif
4500
Louis Dionneba400782020-10-02 15:02:52 -04004501#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004502
4503template<class _CharT, class _Traits, class _Allocator>
4504bool
4505basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4506{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004507 return this->data() <= _VSTD::__to_address(__i->base()) &&
4508 _VSTD::__to_address(__i->base()) < this->data() + this->size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004509}
4510
4511template<class _CharT, class _Traits, class _Allocator>
4512bool
4513basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4514{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004515 return this->data() < _VSTD::__to_address(__i->base()) &&
4516 _VSTD::__to_address(__i->base()) <= this->data() + this->size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004517}
4518
4519template<class _CharT, class _Traits, class _Allocator>
4520bool
4521basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4522{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004523 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Howard Hinnant8ea98242013-08-23 17:37:05 +00004524 return this->data() <= __p && __p <= this->data() + this->size();
4525}
4526
4527template<class _CharT, class _Traits, class _Allocator>
4528bool
4529basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4530{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004531 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Howard Hinnant8ea98242013-08-23 17:37:05 +00004532 return this->data() <= __p && __p < this->data() + this->size();
4533}
4534
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004535#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004536
Louis Dionne173f29e2019-05-29 16:01:36 +00004537#if _LIBCPP_STD_VER > 11
Marshall Clowcba751f2013-07-23 17:05:24 +00004538// Literal suffixes for basic_string [basic.string.literals]
Marshall Clowac868372013-10-05 21:18:32 +00004539inline namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004540{
4541 inline namespace string_literals
4542 {
Howard Hinnant5c167562013-08-07 19:39:48 +00004543 inline _LIBCPP_INLINE_VISIBILITY
4544 basic_string<char> operator "" s( const char *__str, size_t __len )
4545 {
4546 return basic_string<char> (__str, __len);
4547 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004548
Louis Dionne89258142021-08-23 15:32:36 -04004549#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnant5c167562013-08-07 19:39:48 +00004550 inline _LIBCPP_INLINE_VISIBILITY
4551 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4552 {
4553 return basic_string<wchar_t> (__str, __len);
4554 }
Louis Dionne89258142021-08-23 15:32:36 -04004555#endif
Marshall Clowcba751f2013-07-23 17:05:24 +00004556
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -04004557#ifndef _LIBCPP_HAS_NO_CHAR8_T
Marshall Clow8732fed2018-12-11 04:35:44 +00004558 inline _LIBCPP_INLINE_VISIBILITY
4559 basic_string<char8_t> operator "" s(const char8_t *__str, size_t __len) _NOEXCEPT
4560 {
4561 return basic_string<char8_t> (__str, __len);
4562 }
4563#endif
4564
Howard Hinnant5c167562013-08-07 19:39:48 +00004565 inline _LIBCPP_INLINE_VISIBILITY
4566 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4567 {
4568 return basic_string<char16_t> (__str, __len);
4569 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004570
Howard Hinnant5c167562013-08-07 19:39:48 +00004571 inline _LIBCPP_INLINE_VISIBILITY
4572 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4573 {
4574 return basic_string<char32_t> (__str, __len);
4575 }
Nikolas Klauserd26407a2021-12-02 14:12:51 +01004576 } // namespace string_literals
4577} // namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004578#endif
4579
Howard Hinnantc51e1022010-05-11 19:42:16 +00004580_LIBCPP_END_NAMESPACE_STD
4581
Eric Fiselierf4433a32017-05-31 22:07:49 +00004582_LIBCPP_POP_MACROS
4583
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004584#endif // _LIBCPP_STRING