blob: 3616de8a214d88dab49becdef5177737e1f688cd [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
Nikolas Klauserf89bb932022-01-24 19:44:34 +0100621#ifndef _LIBCPP_ABI_NO_BASIC_STRING_BASE_CLASS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000622template <bool>
Louis Dionneb6aabd92021-08-19 12:21:06 -0400623struct __basic_string_common;
624
625template <>
626struct __basic_string_common<true> {
627 // Both are defined in string.cpp
628 _LIBCPP_NORETURN _LIBCPP_EXPORTED_FROM_ABI void __throw_length_error() const;
629 _LIBCPP_NORETURN _LIBCPP_EXPORTED_FROM_ABI void __throw_out_of_range() const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000630};
Nikolas Klauserf89bb932022-01-24 19:44:34 +0100631#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000632
Marshall Clow039b2f02016-01-13 21:54:34 +0000633template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400634struct __string_is_trivial_iterator : public false_type {};
635
636template <class _Tp>
637struct __string_is_trivial_iterator<_Tp*>
638 : public is_arithmetic<_Tp> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000639
Louis Dionne173f29e2019-05-29 16:01:36 +0000640template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400641struct __string_is_trivial_iterator<__wrap_iter<_Iter> >
642 : public __string_is_trivial_iterator<_Iter> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000643
Marshall Clow82513342016-09-24 22:45:42 +0000644template <class _CharT, class _Traits, class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500645struct __can_be_converted_to_string_view : public _BoolConstant<
646 is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
647 !is_convertible<const _Tp&, const _CharT*>::value
648 > {};
Marshall Clow82513342016-09-24 22:45:42 +0000649
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000650#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000651
652template <class _CharT, size_t = sizeof(_CharT)>
653struct __padding
654{
655 unsigned char __xx[sizeof(_CharT)-1];
656};
657
658template <class _CharT>
659struct __padding<_CharT, 1>
660{
661};
662
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400663#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000664
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400665#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800666typedef basic_string<char8_t> u8string;
667#endif
668
669#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
670typedef basic_string<char16_t> u16string;
671typedef basic_string<char32_t> u32string;
Louis Dionne96fc5f52021-09-09 11:25:10 -0400672#endif
Richard Smith256954d2020-11-11 17:12:18 -0800673
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000674template<class _CharT, class _Traits, class _Allocator>
Richard Smith256954d2020-11-11 17:12:18 -0800675class
676 _LIBCPP_TEMPLATE_VIS
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400677#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800678 _LIBCPP_PREFERRED_NAME(u8string)
679#endif
680#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
681 _LIBCPP_PREFERRED_NAME(u16string)
682 _LIBCPP_PREFERRED_NAME(u32string)
683#endif
684 basic_string
Nikolas Klauserf89bb932022-01-24 19:44:34 +0100685#ifndef _LIBCPP_ABI_NO_BASIC_STRING_BASE_CLASS
Louis Dionneb6aabd92021-08-19 12:21:06 -0400686 : private __basic_string_common<true> // This base class is historical, but it needs to remain for ABI compatibility
Nikolas Klauserf89bb932022-01-24 19:44:34 +0100687#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000688{
689public:
690 typedef basic_string __self;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000691 typedef basic_string_view<_CharT, _Traits> __self_view;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000692 typedef _Traits traits_type;
Marshall Clowa3a74e02017-03-15 18:41:11 +0000693 typedef _CharT value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000694 typedef _Allocator allocator_type;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000695 typedef allocator_traits<allocator_type> __alloc_traits;
696 typedef typename __alloc_traits::size_type size_type;
697 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant3e276872011-06-03 18:40:47 +0000698 typedef value_type& reference;
699 typedef const value_type& const_reference;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000700 typedef typename __alloc_traits::pointer pointer;
701 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000702
Marshall Clow79f33542018-03-21 00:36:05 +0000703 static_assert((!is_array<value_type>::value), "Character type of basic_string must not be an array");
704 static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string must be standard-layout");
705 static_assert(( is_trivial<value_type>::value), "Character type of basic_string must be trivial");
706 static_assert(( is_same<_CharT, typename traits_type::char_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000707 "traits_type::char_type must be the same type as CharT");
Marshall Clow79f33542018-03-21 00:36:05 +0000708 static_assert(( is_same<typename allocator_type::value_type, value_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000709 "Allocator::value_type must be same type as value_type");
Marshall Clowe46031a2018-07-02 18:41:15 +0000710
Howard Hinnantc51e1022010-05-11 19:42:16 +0000711 typedef __wrap_iter<pointer> iterator;
712 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000713 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
714 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000715
716private:
Howard Hinnant68bf1812013-04-30 21:44:48 +0000717
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000718#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000719
720 struct __long
721 {
722 pointer __data_;
723 size_type __size_;
724 size_type __cap_;
725 };
726
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000727#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000728 static const size_type __short_mask = 0x01;
729 static const size_type __long_mask = 0x1ul;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000730#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000731 static const size_type __short_mask = 0x80;
732 static const size_type __long_mask = ~(size_type(~0) >> 1);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400733#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +0000734
735 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
736 (sizeof(__long) - 1)/sizeof(value_type) : 2};
737
738 struct __short
739 {
740 value_type __data_[__min_cap];
741 struct
742 : __padding<value_type>
743 {
744 unsigned char __size_;
745 };
746 };
747
748#else
749
Howard Hinnantc51e1022010-05-11 19:42:16 +0000750 struct __long
751 {
752 size_type __cap_;
753 size_type __size_;
754 pointer __data_;
755 };
756
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000757#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000758 static const size_type __short_mask = 0x80;
759 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000760#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000761 static const size_type __short_mask = 0x01;
762 static const size_type __long_mask = 0x1ul;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400763#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +0000764
Howard Hinnantc51e1022010-05-11 19:42:16 +0000765 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
766 (sizeof(__long) - 1)/sizeof(value_type) : 2};
767
768 struct __short
769 {
770 union
771 {
772 unsigned char __size_;
Howard Hinnant49e145e2012-10-30 19:06:59 +0000773 value_type __lx;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000774 };
775 value_type __data_[__min_cap];
776 };
777
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400778#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000779
Howard Hinnant8ea98242013-08-23 17:37:05 +0000780 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000781
Howard Hinnant8ea98242013-08-23 17:37:05 +0000782 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000783
784 struct __raw
785 {
786 size_type __words[__n_words];
787 };
788
789 struct __rep
790 {
791 union
792 {
793 __long __l;
794 __short __s;
795 __raw __r;
796 };
797 };
798
799 __compressed_pair<__rep, allocator_type> __r_;
800
Howard Hinnantc51e1022010-05-11 19:42:16 +0000801public:
Martin Storsjö88890c22021-02-22 01:13:13 +0200802 _LIBCPP_TEMPLATE_DATA_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000803 static const size_type npos = -1;
804
Howard Hinnant3e276872011-06-03 18:40:47 +0000805 _LIBCPP_INLINE_VISIBILITY basic_string()
806 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000807
808 _LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a)
809#if _LIBCPP_STD_VER <= 14
810 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
811#else
812 _NOEXCEPT;
813#endif
814
Howard Hinnantc51e1022010-05-11 19:42:16 +0000815 basic_string(const basic_string& __str);
816 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clowa80abc72015-06-03 19:56:43 +0000817
Eric Fiselierfc92be82017-04-19 00:28:44 +0000818#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000819 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000820 basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +0000821#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +0000822 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000823#else
824 _NOEXCEPT;
825#endif
826
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000827 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000828 basic_string(basic_string&& __str, const allocator_type& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400829#endif // _LIBCPP_CXX03_LANG
Marshall Clowe46031a2018-07-02 18:41:15 +0000830
Louis Dionne9ce598d2021-09-08 09:14:43 -0400831 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Eric Fiseliera8567862018-07-17 05:48:48 +0000832 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5169d1c2019-12-16 19:03:23 -0500833 basic_string(const _CharT* __s) : __r_(__default_init_tag(), __default_init_tag()) {
Eric Fiseliera8567862018-07-17 05:48:48 +0000834 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
835 __init(__s, traits_type::length(__s));
Nikolas Klauserf2807732022-01-11 00:33:35 +0100836 _VSTD::__debug_db_insert_c(this);
Eric Fiseliera8567862018-07-17 05:48:48 +0000837 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000838
Louis Dionne9ce598d2021-09-08 09:14:43 -0400839 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000840 _LIBCPP_INLINE_VISIBILITY
841 basic_string(const _CharT* __s, const _Allocator& __a);
842
Marek Kurdej90d79712021-07-27 16:16:21 +0200843#if _LIBCPP_STD_VER > 20
844 basic_string(nullptr_t) = delete;
845#endif
846
Howard Hinnantcf823322010-12-17 14:46:43 +0000847 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000848 basic_string(const _CharT* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +0000849 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000850 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000851 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000852 basic_string(size_type __n, _CharT __c);
Marshall Clowe46031a2018-07-02 18:41:15 +0000853
Louis Dionne9ce598d2021-09-08 09:14:43 -0400854 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000855 _LIBCPP_INLINE_VISIBILITY
856 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
857
Marshall Clow83445802016-04-07 18:13:41 +0000858 basic_string(const basic_string& __str, size_type __pos, size_type __n,
Eric Fiselier812882b2017-02-17 01:17:10 +0000859 const _Allocator& __a = _Allocator());
Marshall Clow83445802016-04-07 18:13:41 +0000860 _LIBCPP_INLINE_VISIBILITY
861 basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +0000862 const _Allocator& __a = _Allocator());
Marshall Clowe46031a2018-07-02 18:41:15 +0000863
Louis Dionne9ce598d2021-09-08 09:14:43 -0400864 template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value> >
Shoaib Meenai69c57412017-03-02 03:02:50 +0000865 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselier812882b2017-02-17 01:17:10 +0000866 basic_string(const _Tp& __t, size_type __pos, size_type __n,
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500867 const allocator_type& __a = allocator_type());
Marshall Clowe46031a2018-07-02 18:41:15 +0000868
Louis Dionne9ce598d2021-09-08 09:14:43 -0400869 template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500870 !__is_same_uncvref<_Tp, basic_string>::value> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000871 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
872 explicit basic_string(const _Tp& __t);
873
Louis Dionne9ce598d2021-09-08 09:14:43 -0400874 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 +0000875 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
876 explicit basic_string(const _Tp& __t, const allocator_type& __a);
877
Louis Dionne9ce598d2021-09-08 09:14:43 -0400878 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000879 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000880 basic_string(_InputIterator __first, _InputIterator __last);
Louis Dionne9ce598d2021-09-08 09:14:43 -0400881 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000882 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000883 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000884#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000885 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000886 basic_string(initializer_list<_CharT> __il);
Howard Hinnantcf823322010-12-17 14:46:43 +0000887 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000888 basic_string(initializer_list<_CharT> __il, const _Allocator& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400889#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000890
Eric Fiselierd9a702a2016-10-31 03:42:50 +0000891 inline ~basic_string();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000892
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000893 _LIBCPP_INLINE_VISIBILITY
894 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
895
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000896 basic_string& operator=(const basic_string& __str);
Eric Fiselier5ec13b12017-01-23 21:24:58 +0000897
Louis Dionne9ce598d2021-09-08 09:14:43 -0400898 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 +0000899 basic_string& operator=(const _Tp& __t)
900 {__self_view __sv = __t; return assign(__sv);}
901
Eric Fiselierfc92be82017-04-19 00:28:44 +0000902#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000903 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000904 basic_string& operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000905 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiselierfc92be82017-04-19 00:28:44 +0000906 _LIBCPP_INLINE_VISIBILITY
907 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000908#endif
Howard Hinnantd17880b2013-06-28 16:59:19 +0000909 _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const value_type* __s) {return assign(__s);}
Marek Kurdej90d79712021-07-27 16:16:21 +0200910#if _LIBCPP_STD_VER > 20
911 basic_string& operator=(nullptr_t) = delete;
912#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000913 basic_string& operator=(value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000914
Louis Dionneba400782020-10-02 15:02:52 -0400915#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +0000916 _LIBCPP_INLINE_VISIBILITY
917 iterator begin() _NOEXCEPT
918 {return iterator(this, __get_pointer());}
919 _LIBCPP_INLINE_VISIBILITY
920 const_iterator begin() const _NOEXCEPT
921 {return const_iterator(this, __get_pointer());}
922 _LIBCPP_INLINE_VISIBILITY
923 iterator end() _NOEXCEPT
924 {return iterator(this, __get_pointer() + size());}
925 _LIBCPP_INLINE_VISIBILITY
926 const_iterator end() const _NOEXCEPT
927 {return const_iterator(this, __get_pointer() + size());}
928#else
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000929 _LIBCPP_INLINE_VISIBILITY
930 iterator begin() _NOEXCEPT
931 {return iterator(__get_pointer());}
932 _LIBCPP_INLINE_VISIBILITY
933 const_iterator begin() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000934 {return const_iterator(__get_pointer());}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000935 _LIBCPP_INLINE_VISIBILITY
936 iterator end() _NOEXCEPT
937 {return iterator(__get_pointer() + size());}
938 _LIBCPP_INLINE_VISIBILITY
939 const_iterator end() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000940 {return const_iterator(__get_pointer() + size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400941#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000942 _LIBCPP_INLINE_VISIBILITY
943 reverse_iterator rbegin() _NOEXCEPT
944 {return reverse_iterator(end());}
945 _LIBCPP_INLINE_VISIBILITY
946 const_reverse_iterator rbegin() const _NOEXCEPT
947 {return const_reverse_iterator(end());}
948 _LIBCPP_INLINE_VISIBILITY
949 reverse_iterator rend() _NOEXCEPT
950 {return reverse_iterator(begin());}
951 _LIBCPP_INLINE_VISIBILITY
952 const_reverse_iterator rend() const _NOEXCEPT
953 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000954
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000955 _LIBCPP_INLINE_VISIBILITY
956 const_iterator cbegin() const _NOEXCEPT
957 {return begin();}
958 _LIBCPP_INLINE_VISIBILITY
959 const_iterator cend() const _NOEXCEPT
960 {return end();}
961 _LIBCPP_INLINE_VISIBILITY
962 const_reverse_iterator crbegin() const _NOEXCEPT
963 {return rbegin();}
964 _LIBCPP_INLINE_VISIBILITY
965 const_reverse_iterator crend() const _NOEXCEPT
966 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000967
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000968 _LIBCPP_INLINE_VISIBILITY size_type size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000969 {return __is_long() ? __get_long_size() : __get_short_size();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000970 _LIBCPP_INLINE_VISIBILITY size_type length() const _NOEXCEPT {return size();}
971 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT;
972 _LIBCPP_INLINE_VISIBILITY size_type capacity() const _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +0000973 {return (__is_long() ? __get_long_cap()
974 : static_cast<size_type>(__min_cap)) - 1;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000975
976 void resize(size_type __n, value_type __c);
977 _LIBCPP_INLINE_VISIBILITY void resize(size_type __n) {resize(__n, value_type());}
978
Marek Kurdejc9848142020-11-26 10:07:16 +0100979 void reserve(size_type __requested_capacity);
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100980
981#if _LIBCPP_STD_VER > 20
982 template <class _Op>
983 _LIBCPP_HIDE_FROM_ABI constexpr
984 void resize_and_overwrite(size_type __n, _Op __op) {
985 __resize_default_init(__n);
Nikolas Klausera4a76a12022-01-20 13:53:59 +0100986 __erase_to_end(_VSTD::move(__op)(data(), _LIBCPP_AUTO_CAST(__n)));
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100987 }
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:
Nikolas Klauser93826d12022-01-04 17:24:03 +01001472 _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI static bool __fits_in_sso(size_type __sz) {
1473 // SSO is disabled during constant evaluation because `__is_long` isn't constexpr friendly
1474 return !__libcpp_is_constant_evaluated() && (__sz < __min_cap);
1475 }
1476
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001477 _LIBCPP_INLINE_VISIBILITY
1478 allocator_type& __alloc() _NOEXCEPT
1479 {return __r_.second();}
1480 _LIBCPP_INLINE_VISIBILITY
1481 const allocator_type& __alloc() const _NOEXCEPT
1482 {return __r_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001483
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001484#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001485
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001486 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001487 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001488# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001489 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001490# else
1491 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1492# endif
1493
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001494 _LIBCPP_INLINE_VISIBILITY
1495 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001496# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001497 {return __r_.first().__s.__size_ >> 1;}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001498# else
1499 {return __r_.first().__s.__size_;}
1500# endif
1501
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001502#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001503
1504 _LIBCPP_INLINE_VISIBILITY
1505 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001506# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001507 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1508# else
1509 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
1510# endif
1511
1512 _LIBCPP_INLINE_VISIBILITY
1513 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001514# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001515 {return __r_.first().__s.__size_;}
1516# else
1517 {return __r_.first().__s.__size_ >> 1;}
1518# endif
1519
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001520#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001521
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001522 _LIBCPP_INLINE_VISIBILITY
1523 void __set_long_size(size_type __s) _NOEXCEPT
1524 {__r_.first().__l.__size_ = __s;}
1525 _LIBCPP_INLINE_VISIBILITY
1526 size_type __get_long_size() const _NOEXCEPT
1527 {return __r_.first().__l.__size_;}
1528 _LIBCPP_INLINE_VISIBILITY
1529 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001530 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1531
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001532 _LIBCPP_INLINE_VISIBILITY
1533 void __set_long_cap(size_type __s) _NOEXCEPT
1534 {__r_.first().__l.__cap_ = __long_mask | __s;}
1535 _LIBCPP_INLINE_VISIBILITY
1536 size_type __get_long_cap() const _NOEXCEPT
Howard Hinnant28b24882011-12-01 20:21:04 +00001537 {return __r_.first().__l.__cap_ & size_type(~__long_mask);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001538
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001539 _LIBCPP_INLINE_VISIBILITY
1540 void __set_long_pointer(pointer __p) _NOEXCEPT
1541 {__r_.first().__l.__data_ = __p;}
1542 _LIBCPP_INLINE_VISIBILITY
1543 pointer __get_long_pointer() _NOEXCEPT
1544 {return __r_.first().__l.__data_;}
1545 _LIBCPP_INLINE_VISIBILITY
1546 const_pointer __get_long_pointer() const _NOEXCEPT
1547 {return __r_.first().__l.__data_;}
1548 _LIBCPP_INLINE_VISIBILITY
1549 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001550 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001551 _LIBCPP_INLINE_VISIBILITY
1552 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001553 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001554 _LIBCPP_INLINE_VISIBILITY
1555 pointer __get_pointer() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001556 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001557 _LIBCPP_INLINE_VISIBILITY
1558 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001559 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1560
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001561 _LIBCPP_INLINE_VISIBILITY
1562 void __zero() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001563 {
1564 size_type (&__a)[__n_words] = __r_.first().__r.__words;
1565 for (unsigned __i = 0; __i < __n_words; ++__i)
1566 __a[__i] = 0;
1567 }
1568
1569 template <size_type __a> static
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001570 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00001571 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +00001572 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001573 enum {__alignment = 16};
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001574 static _LIBCPP_INLINE_VISIBILITY
1575 size_type __recommend(size_type __s) _NOEXCEPT
Marshall Clow80584522018-02-07 21:30:17 +00001576 {
1577 if (__s < __min_cap) return static_cast<size_type>(__min_cap) - 1;
1578 size_type __guess = __align_it<sizeof(value_type) < __alignment ?
1579 __alignment/sizeof(value_type) : 1 > (__s+1) - 1;
1580 if (__guess == __min_cap) ++__guess;
1581 return __guess;
1582 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001583
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001584 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001585 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001586 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001587 void __init(const value_type* __s, size_type __sz);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001588 inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001589 void __init(size_type __n, value_type __c);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001590
Martijn Vels5e7c9752020-03-04 17:52:46 -05001591 // Slow path for the (inlined) copy constructor for 'long' strings.
1592 // Always externally instantiated and not inlined.
1593 // Requires that __s is zero terminated.
1594 // The main reason for this function to exist is because for unstable, we
1595 // want to allow inlining of the copy constructor. However, we don't want
1596 // to call the __init() functions as those are marked as inline which may
1597 // result in over-aggressive inlining by the compiler, where our aim is
1598 // to only inline the fast path code directly in the ctor.
1599 void __init_copy_ctor_external(const value_type* __s, size_type __sz);
1600
Howard Hinnantc51e1022010-05-11 19:42:16 +00001601 template <class _InputIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001602 inline
Louis Dionne9ce598d2021-09-08 09:14:43 -04001603 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001604 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001605 __is_exactly_cpp17_input_iterator<_InputIterator>::value
1606 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001607 __init(_InputIterator __first, _InputIterator __last);
1608
1609 template <class _ForwardIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001610 inline
Louis Dionne9ce598d2021-09-08 09:14:43 -04001611 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001612 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001613 __is_cpp17_forward_iterator<_ForwardIterator>::value
1614 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001615 __init(_ForwardIterator __first, _ForwardIterator __last);
1616
1617 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001618 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001619 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1620 size_type __n_copy, size_type __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00001621 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001622
Martijn Vels596e3de2020-02-26 15:55:49 -05001623 // __assign_no_alias is invoked for assignment operations where we
1624 // have proof that the input does not alias the current instance.
1625 // For example, operator=(basic_string) performs a 'self' check.
1626 template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04001627 basic_string& __assign_no_alias(const value_type* __s, size_type __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05001628
Howard Hinnantcf823322010-12-17 14:46:43 +00001629 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001630 void __erase_to_end(size_type __pos);
1631
Martijn Velsa81fc792020-02-26 13:25:43 -05001632 // __erase_external_with_move is invoked for erase() invocations where
1633 // `n ~= npos`, likely requiring memory moves on the string data.
1634 void __erase_external_with_move(size_type __pos, size_type __n);
1635
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001636 _LIBCPP_INLINE_VISIBILITY
1637 void __copy_assign_alloc(const basic_string& __str)
1638 {__copy_assign_alloc(__str, integral_constant<bool,
1639 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1640
1641 _LIBCPP_INLINE_VISIBILITY
1642 void __copy_assign_alloc(const basic_string& __str, true_type)
1643 {
Marshall Clowf258c202017-01-31 03:40:52 +00001644 if (__alloc() == __str.__alloc())
1645 __alloc() = __str.__alloc();
1646 else
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001647 {
Marshall Clowf258c202017-01-31 03:40:52 +00001648 if (!__str.__is_long())
1649 {
Vedant Kumar55e007e2018-03-08 21:15:26 +00001650 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001651 __alloc() = __str.__alloc();
1652 }
1653 else
1654 {
1655 allocator_type __a = __str.__alloc();
1656 pointer __p = __alloc_traits::allocate(__a, __str.__get_long_cap());
Vedant Kumar55e007e2018-03-08 21:15:26 +00001657 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001658 __alloc() = _VSTD::move(__a);
1659 __set_long_pointer(__p);
1660 __set_long_cap(__str.__get_long_cap());
1661 __set_long_size(__str.size());
1662 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001663 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001664 }
1665
1666 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001667 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001668 {}
1669
Eric Fiselierfc92be82017-04-19 00:28:44 +00001670#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +00001671 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001672 void __move_assign(basic_string& __str, false_type)
1673 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnantcf823322010-12-17 14:46:43 +00001674 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001675 void __move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001676#if _LIBCPP_STD_VER > 14
1677 _NOEXCEPT;
1678#else
Howard Hinnant3e276872011-06-03 18:40:47 +00001679 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001680#endif
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001681#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001682
1683 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001684 void
Howard Hinnantc2734962011-09-02 20:42:31 +00001685 __move_assign_alloc(basic_string& __str)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001686 _NOEXCEPT_(
1687 !__alloc_traits::propagate_on_container_move_assignment::value ||
1688 is_nothrow_move_assignable<allocator_type>::value)
1689 {__move_assign_alloc(__str, integral_constant<bool,
1690 __alloc_traits::propagate_on_container_move_assignment::value>());}
1691
1692 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00001693 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001694 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1695 {
1696 __alloc() = _VSTD::move(__c.__alloc());
1697 }
1698
1699 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001700 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001701 _NOEXCEPT
1702 {}
1703
Martijn Velsda7d94f2020-06-19 14:24:03 -04001704 basic_string& __assign_external(const value_type* __s);
1705 basic_string& __assign_external(const value_type* __s, size_type __n);
1706
1707 // Assigns the value in __s, guaranteed to be __n < __min_cap in length.
1708 inline basic_string& __assign_short(const value_type* __s, size_type __n) {
1709 pointer __p = __is_long()
1710 ? (__set_long_size(__n), __get_long_pointer())
1711 : (__set_short_size(__n), __get_short_pointer());
1712 traits_type::move(_VSTD::__to_address(__p), __s, __n);
1713 traits_type::assign(__p[__n], value_type());
1714 return *this;
1715 }
1716
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01001717 _LIBCPP_HIDE_FROM_ABI basic_string& __null_terminate_at(value_type* __p, size_type __newsz) {
1718 __set_size(__newsz);
1719 __invalidate_iterators_past(__newsz);
1720 traits_type::assign(__p[__newsz], value_type());
1721 return *this;
1722 }
1723
Howard Hinnantcf823322010-12-17 14:46:43 +00001724 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
1725 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(size_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001726
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001727 template<class _Tp>
1728 _LIBCPP_INLINE_VISIBILITY
1729 bool __addr_in_range(_Tp&& __t) const {
1730 const volatile void *__p = _VSTD::addressof(__t);
1731 return data() <= __p && __p <= data() + size();
1732 }
1733
Louis Dionned24191c2021-08-19 12:39:16 -04001734 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1735 void __throw_length_error() const {
Nikolas Klauserf89bb932022-01-24 19:44:34 +01001736 _VSTD::__throw_length_error("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001737 }
1738
1739 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1740 void __throw_out_of_range() const {
Nikolas Klauserf89bb932022-01-24 19:44:34 +01001741 _VSTD::__throw_out_of_range("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001742 }
1743
Howard Hinnantc51e1022010-05-11 19:42:16 +00001744 friend basic_string operator+<>(const basic_string&, const basic_string&);
1745 friend basic_string operator+<>(const value_type*, const basic_string&);
1746 friend basic_string operator+<>(value_type, const basic_string&);
1747 friend basic_string operator+<>(const basic_string&, const value_type*);
1748 friend basic_string operator+<>(const basic_string&, value_type);
1749};
1750
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001751// These declarations must appear before any functions are implicitly used
1752// so that they have the correct visibility specifier.
1753#ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
Louis Dionne89258142021-08-23 15:32:36 -04001754 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1755# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1756 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1757# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001758#else
Louis Dionne89258142021-08-23 15:32:36 -04001759 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1760# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1761 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1762# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001763#endif
1764
1765
Louis Dionned59f8a52021-08-17 11:59:07 -04001766#if _LIBCPP_STD_VER >= 17
Marshall Clowa0563332018-02-08 06:34:03 +00001767template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -05001768 class _CharT = __iter_value_type<_InputIterator>,
Marshall Clowa0563332018-02-08 06:34:03 +00001769 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001770 class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
1771 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowa0563332018-02-08 06:34:03 +00001772 >
1773basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
1774 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
Marshall Clowe46031a2018-07-02 18:41:15 +00001775
1776template<class _CharT,
1777 class _Traits,
1778 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001779 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowe46031a2018-07-02 18:41:15 +00001780 >
1781explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
1782 -> basic_string<_CharT, _Traits, _Allocator>;
1783
1784template<class _CharT,
1785 class _Traits,
1786 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001787 class = enable_if_t<__is_allocator<_Allocator>::value>,
Marshall Clowe46031a2018-07-02 18:41:15 +00001788 class _Sz = typename allocator_traits<_Allocator>::size_type
1789 >
1790basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
1791 -> basic_string<_CharT, _Traits, _Allocator>;
Marshall Clowa0563332018-02-08 06:34:03 +00001792#endif
1793
Howard Hinnantc51e1022010-05-11 19:42:16 +00001794template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001795inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001796void
1797basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
1798{
Louis Dionneba400782020-10-02 15:02:52 -04001799#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001800 if (!__libcpp_is_constant_evaluated())
1801 __get_db()->__invalidate_all(this);
Louis Dionneba400782020-10-02 15:02:52 -04001802#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001803}
1804
1805template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001806inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001807void
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001808basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type __pos)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001809{
Louis Dionneba400782020-10-02 15:02:52 -04001810#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001811 if (!__libcpp_is_constant_evaluated()) {
1812 __c_node* __c = __get_db()->__find_c_and_lock(this);
1813 if (__c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001814 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001815 const_pointer __new_last = __get_pointer() + __pos;
1816 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001817 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001818 --__p;
1819 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1820 if (__i->base() > __new_last)
1821 {
1822 (*__p)->__c_ = nullptr;
1823 if (--__c->end_ != __p)
1824 _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1825 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001826 }
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001827 __get_db()->unlock();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001828 }
1829 }
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001830#else
1831 (void)__pos;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001832#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001833}
1834
1835template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001836inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001837basic_string<_CharT, _Traits, _Allocator>::basic_string()
Marshall Clowe546cbb2015-06-04 02:05:41 +00001838 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001839 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001840{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001841 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001842 __zero();
1843}
1844
1845template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001846inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001847basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
Eric Fiseliere012bf22015-07-18 20:40:46 +00001848#if _LIBCPP_STD_VER <= 14
1849 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1850#else
1851 _NOEXCEPT
1852#endif
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001853: __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001854{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001855 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001856 __zero();
1857}
1858
1859template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier815ed732016-09-16 00:00:48 +00001860void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1861 size_type __sz,
1862 size_type __reserve)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001863{
1864 if (__reserve > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001865 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001866 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001867 if (__fits_in_sso(__reserve))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001868 {
1869 __set_short_size(__sz);
1870 __p = __get_short_pointer();
1871 }
1872 else
1873 {
1874 size_type __cap = __recommend(__reserve);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001875 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001876 __set_long_pointer(__p);
1877 __set_long_cap(__cap+1);
1878 __set_long_size(__sz);
1879 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001880 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001881 traits_type::assign(__p[__sz], value_type());
1882}
1883
1884template <class _CharT, class _Traits, class _Allocator>
1885void
Howard Hinnantd17880b2013-06-28 16:59:19 +00001886basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001887{
1888 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001889 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001890 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001891 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001892 {
1893 __set_short_size(__sz);
1894 __p = __get_short_pointer();
1895 }
1896 else
1897 {
1898 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001899 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001900 __set_long_pointer(__p);
1901 __set_long_cap(__cap+1);
1902 __set_long_size(__sz);
1903 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001904 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001905 traits_type::assign(__p[__sz], value_type());
1906}
1907
1908template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00001909template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00001910basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001911 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001912{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001913 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001914 __init(__s, traits_type::length(__s));
Nikolas Klauserf2807732022-01-11 00:33:35 +01001915 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001916}
1917
1918template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001919inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001920basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001921 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001922{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001923 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
1924 __init(__s, __n);
1925 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001926}
1927
1928template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001929inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001930basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001931 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001932{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001933 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001934 __init(__s, __n);
Nikolas Klauserf2807732022-01-11 00:33:35 +01001935 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001936}
1937
1938template <class _CharT, class _Traits, class _Allocator>
1939basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001940 : __r_(__default_init_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001941{
1942 if (!__str.__is_long())
1943 __r_.first().__r = __str.__r_.first().__r;
1944 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001945 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1946 __str.__get_long_size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01001947 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001948}
1949
1950template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00001951basic_string<_CharT, _Traits, _Allocator>::basic_string(
1952 const basic_string& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001953 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001954{
1955 if (!__str.__is_long())
1956 __r_.first().__r = __str.__r_.first().__r;
1957 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001958 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1959 __str.__get_long_size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01001960 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001961}
1962
Martijn Vels5e7c9752020-03-04 17:52:46 -05001963template <class _CharT, class _Traits, class _Allocator>
1964void basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(
1965 const value_type* __s, size_type __sz) {
1966 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001967 if (__fits_in_sso(__sz)) {
Martijn Vels5e7c9752020-03-04 17:52:46 -05001968 __p = __get_short_pointer();
1969 __set_short_size(__sz);
1970 } else {
1971 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001972 __throw_length_error();
Martijn Vels5e7c9752020-03-04 17:52:46 -05001973 size_t __cap = __recommend(__sz);
1974 __p = __alloc_traits::allocate(__alloc(), __cap + 1);
1975 __set_long_pointer(__p);
1976 __set_long_cap(__cap + 1);
1977 __set_long_size(__sz);
1978 }
1979 traits_type::copy(_VSTD::__to_address(__p), __s, __sz + 1);
1980}
1981
Eric Fiselierfc92be82017-04-19 00:28:44 +00001982#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001983
1984template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001985inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001986basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +00001987#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +00001988 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowa80abc72015-06-03 19:56:43 +00001989#else
1990 _NOEXCEPT
1991#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001992 : __r_(_VSTD::move(__str.__r_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001993{
1994 __str.__zero();
Nikolas Klauserf2807732022-01-11 00:33:35 +01001995 _VSTD::__debug_db_insert_c(this);
Louis Dionneba400782020-10-02 15:02:52 -04001996#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauserf2807732022-01-11 00:33:35 +01001997 if (!__libcpp_is_constant_evaluated() && __is_long())
1998 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001999#endif
2000}
2001
2002template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002003inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002004basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002005 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002006{
Marshall Clowd2d24692014-07-17 15:32:20 +00002007 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002008 __init(_VSTD::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd2d24692014-07-17 15:32:20 +00002009 else
2010 {
2011 __r_.first().__r = __str.__r_.first().__r;
2012 __str.__zero();
2013 }
Nikolas Klauserf2807732022-01-11 00:33:35 +01002014 _VSTD::__debug_db_insert_c(this);
Louis Dionneba400782020-10-02 15:02:52 -04002015#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauserf2807732022-01-11 00:33:35 +01002016 if (!__libcpp_is_constant_evaluated() && __is_long())
2017 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002018#endif
2019}
2020
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002021#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002022
2023template <class _CharT, class _Traits, class _Allocator>
2024void
2025basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
2026{
2027 if (__n > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002028 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002029 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002030 if (__fits_in_sso(__n))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002031 {
2032 __set_short_size(__n);
2033 __p = __get_short_pointer();
2034 }
2035 else
2036 {
2037 size_type __cap = __recommend(__n);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002038 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002039 __set_long_pointer(__p);
2040 __set_long_cap(__cap+1);
2041 __set_long_size(__n);
2042 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002043 traits_type::assign(_VSTD::__to_address(__p), __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002044 traits_type::assign(__p[__n], value_type());
2045}
2046
2047template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002048inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002049basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002050 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002051{
2052 __init(__n, __c);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002053 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002054}
2055
2056template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002057template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00002058basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002059 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002060{
2061 __init(__n, __c);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002062 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002063}
2064
Howard Hinnantc51e1022010-05-11 19:42:16 +00002065template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00002066basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
2067 size_type __pos, size_type __n,
2068 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002069 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002070{
2071 size_type __str_sz = __str.size();
2072 if (__pos > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002073 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002074 __init(__str.data() + __pos, _VSTD::min(__n, __str_sz - __pos));
Nikolas Klauserf2807732022-01-11 00:33:35 +01002075 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002076}
2077
2078template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002079inline
Marshall Clow83445802016-04-07 18:13:41 +00002080basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +00002081 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002082 : __r_(__default_init_tag(), __a)
Marshall Clow83445802016-04-07 18:13:41 +00002083{
2084 size_type __str_sz = __str.size();
2085 if (__pos > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002086 __throw_out_of_range();
Marshall Clow83445802016-04-07 18:13:41 +00002087 __init(__str.data() + __pos, __str_sz - __pos);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002088 _VSTD::__debug_db_insert_c(this);
Marshall Clow83445802016-04-07 18:13:41 +00002089}
2090
2091template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002092template <class _Tp, class>
Marshall Clow78dbe462016-11-14 18:22:19 +00002093basic_string<_CharT, _Traits, _Allocator>::basic_string(
Marshall Clowe46031a2018-07-02 18:41:15 +00002094 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002095 : __r_(__default_init_tag(), __a)
Marshall Clow78dbe462016-11-14 18:22:19 +00002096{
Marshall Clowe46031a2018-07-02 18:41:15 +00002097 __self_view __sv0 = __t;
2098 __self_view __sv = __sv0.substr(__pos, __n);
Marshall Clow78dbe462016-11-14 18:22:19 +00002099 __init(__sv.data(), __sv.size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002100 _VSTD::__debug_db_insert_c(this);
Marshall Clow78dbe462016-11-14 18:22:19 +00002101}
2102
2103template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002104template <class _Tp, class>
2105basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002106 : __r_(__default_init_tag(), __default_init_tag())
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002107{
Marshall Clowe46031a2018-07-02 18:41:15 +00002108 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002109 __init(__sv.data(), __sv.size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002110 _VSTD::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002111}
2112
2113template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002114template <class _Tp, class>
2115basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002116 : __r_(__default_init_tag(), __a)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002117{
Marshall Clowe46031a2018-07-02 18:41:15 +00002118 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002119 __init(__sv.data(), __sv.size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002120 _VSTD::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002121}
2122
2123template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002124template <class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002125__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002126<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002127 __is_exactly_cpp17_input_iterator<_InputIterator>::value
2128>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002129basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
2130{
2131 __zero();
2132#ifndef _LIBCPP_NO_EXCEPTIONS
2133 try
2134 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002135#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002136 for (; __first != __last; ++__first)
2137 push_back(*__first);
2138#ifndef _LIBCPP_NO_EXCEPTIONS
2139 }
2140 catch (...)
2141 {
2142 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002143 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002144 throw;
2145 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002146#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002147}
2148
2149template <class _CharT, class _Traits, class _Allocator>
2150template <class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002151__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002152<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002153 __is_cpp17_forward_iterator<_ForwardIterator>::value
2154>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002155basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
2156{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002157 size_type __sz = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002158 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002159 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002160 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002161 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002162 {
2163 __set_short_size(__sz);
2164 __p = __get_short_pointer();
2165 }
2166 else
2167 {
2168 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002169 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002170 __set_long_pointer(__p);
2171 __set_long_cap(__cap+1);
2172 __set_long_size(__sz);
2173 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002174
2175#ifndef _LIBCPP_NO_EXCEPTIONS
2176 try
2177 {
2178#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002179 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002180 traits_type::assign(*__p, *__first);
2181 traits_type::assign(*__p, value_type());
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002182#ifndef _LIBCPP_NO_EXCEPTIONS
2183 }
2184 catch (...)
2185 {
2186 if (__is_long())
2187 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
2188 throw;
2189 }
2190#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002191}
2192
2193template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002194template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002195inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002196basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002197 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002198{
2199 __init(__first, __last);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002200 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002201}
2202
2203template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002204template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002205inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002206basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
2207 const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002208 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002209{
2210 __init(__first, __last);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002211 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002212}
2213
Eric Fiselierfc92be82017-04-19 00:28:44 +00002214#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002215
Howard Hinnantc51e1022010-05-11 19:42:16 +00002216template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002217inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002218basic_string<_CharT, _Traits, _Allocator>::basic_string(
2219 initializer_list<_CharT> __il)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002220 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002221{
2222 __init(__il.begin(), __il.end());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002223 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002224}
2225
2226template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002227inline
Eric Fiselier9d355982017-04-12 23:45:53 +00002228
Eric Fiselier812882b2017-02-17 01:17:10 +00002229basic_string<_CharT, _Traits, _Allocator>::basic_string(
2230 initializer_list<_CharT> __il, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002231 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002232{
2233 __init(__il.begin(), __il.end());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002234 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002235}
2236
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002237#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002238
Howard Hinnantc51e1022010-05-11 19:42:16 +00002239template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002240basic_string<_CharT, _Traits, _Allocator>::~basic_string()
2241{
Louis Dionneba400782020-10-02 15:02:52 -04002242#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002243 if (!__libcpp_is_constant_evaluated())
2244 __get_db()->__erase_c(this);
Howard Hinnant8ea98242013-08-23 17:37:05 +00002245#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002246 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002247 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002248}
2249
2250template <class _CharT, class _Traits, class _Allocator>
2251void
2252basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
2253 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002254 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 +00002255{
2256 size_type __ms = max_size();
2257 if (__delta_cap > __ms - __old_cap - 1)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002258 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002259 pointer __old_p = __get_pointer();
2260 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002261 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002262 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002263 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002264 __invalidate_all_iterators();
2265 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002266 traits_type::copy(_VSTD::__to_address(__p),
2267 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002268 if (__n_add != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002269 traits_type::copy(_VSTD::__to_address(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002270 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2271 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002272 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2273 _VSTD::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002274 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002275 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002276 __set_long_pointer(__p);
2277 __set_long_cap(__cap+1);
2278 __old_sz = __n_copy + __n_add + __sec_cp_sz;
2279 __set_long_size(__old_sz);
2280 traits_type::assign(__p[__old_sz], value_type());
2281}
2282
2283template <class _CharT, class _Traits, class _Allocator>
2284void
2285basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2286 size_type __n_copy, size_type __n_del, size_type __n_add)
2287{
2288 size_type __ms = max_size();
Marshall Clow2267c5d2013-11-06 14:24:38 +00002289 if (__delta_cap > __ms - __old_cap)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002290 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002291 pointer __old_p = __get_pointer();
2292 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002293 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002294 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002295 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002296 __invalidate_all_iterators();
2297 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002298 traits_type::copy(_VSTD::__to_address(__p),
2299 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002300 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2301 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002302 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2303 _VSTD::__to_address(__old_p) + __n_copy + __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002304 __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002305 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002306 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002307 __set_long_pointer(__p);
2308 __set_long_cap(__cap+1);
2309}
2310
2311// assign
2312
2313template <class _CharT, class _Traits, class _Allocator>
Martijn Vels596e3de2020-02-26 15:55:49 -05002314template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04002315basic_string<_CharT, _Traits, _Allocator>&
2316basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(
Martijn Vels596e3de2020-02-26 15:55:49 -05002317 const value_type* __s, size_type __n) {
2318 size_type __cap = __is_short ? __min_cap : __get_long_cap();
2319 if (__n < __cap) {
2320 pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
2321 __is_short ? __set_short_size(__n) : __set_long_size(__n);
2322 traits_type::copy(_VSTD::__to_address(__p), __s, __n);
2323 traits_type::assign(__p[__n], value_type());
2324 __invalidate_iterators_past(__n);
2325 } else {
2326 size_type __sz = __is_short ? __get_short_size() : __get_long_size();
2327 __grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
2328 }
Martijn Velsb6a08b62020-04-10 18:36:31 -04002329 return *this;
Martijn Vels596e3de2020-02-26 15:55:49 -05002330}
2331
2332template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002333basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002334basic_string<_CharT, _Traits, _Allocator>::__assign_external(
2335 const value_type* __s, size_type __n) {
2336 size_type __cap = capacity();
2337 if (__cap >= __n) {
2338 value_type* __p = _VSTD::__to_address(__get_pointer());
2339 traits_type::move(__p, __s, __n);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002340 return __null_terminate_at(__p, __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04002341 } else {
2342 size_type __sz = size();
2343 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002344 return *this;
Martijn Velsda7d94f2020-06-19 14:24:03 -04002345 }
Martijn Velsda7d94f2020-06-19 14:24:03 -04002346}
2347
2348template <class _CharT, class _Traits, class _Allocator>
2349basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002350basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002351{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002352 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Nikolas Klauser93826d12022-01-04 17:24:03 +01002353 return (__builtin_constant_p(__n) && __fits_in_sso(__n))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002354 ? __assign_short(__s, __n)
2355 : __assign_external(__s, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002356}
2357
2358template <class _CharT, class _Traits, class _Allocator>
2359basic_string<_CharT, _Traits, _Allocator>&
2360basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2361{
2362 size_type __cap = capacity();
2363 if (__cap < __n)
2364 {
2365 size_type __sz = size();
2366 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2367 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002368 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002369 traits_type::assign(__p, __n, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002370 return __null_terminate_at(__p, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002371}
2372
2373template <class _CharT, class _Traits, class _Allocator>
2374basic_string<_CharT, _Traits, _Allocator>&
2375basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2376{
2377 pointer __p;
2378 if (__is_long())
2379 {
2380 __p = __get_long_pointer();
2381 __set_long_size(1);
2382 }
2383 else
2384 {
2385 __p = __get_short_pointer();
2386 __set_short_size(1);
2387 }
2388 traits_type::assign(*__p, __c);
2389 traits_type::assign(*++__p, value_type());
2390 __invalidate_iterators_past(1);
2391 return *this;
2392}
2393
2394template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002395basic_string<_CharT, _Traits, _Allocator>&
2396basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2397{
Martijn Vels596e3de2020-02-26 15:55:49 -05002398 if (this != &__str) {
2399 __copy_assign_alloc(__str);
2400 if (!__is_long()) {
2401 if (!__str.__is_long()) {
Eric Fiselier03bbc922020-01-15 17:27:10 -05002402 __r_.first().__r = __str.__r_.first().__r;
Martijn Vels596e3de2020-02-26 15:55:49 -05002403 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002404 return __assign_no_alias<true>(__str.data(), __str.size());
Martijn Vels596e3de2020-02-26 15:55:49 -05002405 }
2406 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002407 return __assign_no_alias<false>(__str.data(), __str.size());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002408 }
Martijn Vels596e3de2020-02-26 15:55:49 -05002409 }
2410 return *this;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002411}
2412
Eric Fiselierfc92be82017-04-19 00:28:44 +00002413#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002414
2415template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002416inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002417void
2418basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002419 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002420{
2421 if (__alloc() != __str.__alloc())
2422 assign(__str);
2423 else
2424 __move_assign(__str, true_type());
2425}
2426
2427template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002428inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002429void
2430basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002431#if _LIBCPP_STD_VER > 14
2432 _NOEXCEPT
2433#else
Howard Hinnant3e276872011-06-03 18:40:47 +00002434 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002435#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002436{
marshall2ed41622019-11-27 07:13:00 -08002437 if (__is_long()) {
2438 __alloc_traits::deallocate(__alloc(), __get_long_pointer(),
2439 __get_long_cap());
2440#if _LIBCPP_STD_VER <= 14
2441 if (!is_nothrow_move_assignable<allocator_type>::value) {
2442 __set_short_size(0);
2443 traits_type::assign(__get_short_pointer()[0], value_type());
2444 }
2445#endif
2446 }
2447 __move_assign_alloc(__str);
2448 __r_.first() = __str.__r_.first();
2449 __str.__set_short_size(0);
2450 traits_type::assign(__str.__get_short_pointer()[0], value_type());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002451}
2452
2453template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002454inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002455basic_string<_CharT, _Traits, _Allocator>&
2456basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002457 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002458{
2459 __move_assign(__str, integral_constant<bool,
2460 __alloc_traits::propagate_on_container_move_assignment::value>());
2461 return *this;
2462}
2463
2464#endif
2465
2466template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002467template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002468__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002469<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002470 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002471 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002472>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002473basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2474{
Marshall Clow958362f2016-09-05 01:54:30 +00002475 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002476 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002477 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002478}
2479
2480template <class _CharT, class _Traits, class _Allocator>
2481template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002482__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002483<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002484 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002485 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002486>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002487basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2488{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002489 size_type __cap = capacity();
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002490 size_type __n = __string_is_trivial_iterator<_ForwardIterator>::value ?
2491 static_cast<size_type>(_VSTD::distance(__first, __last)) : 0;
2492
2493 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2494 (__cap >= __n || !__addr_in_range(*__first)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002495 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002496 if (__cap < __n)
2497 {
2498 size_type __sz = size();
2499 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2500 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002501 pointer __p = __get_pointer();
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002502 for (; __first != __last; ++__p, (void) ++__first)
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002503 traits_type::assign(*__p, *__first);
2504 traits_type::assign(*__p, value_type());
2505 __set_size(__n);
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04002506 __invalidate_iterators_past(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002507 }
2508 else
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002509 {
2510 const basic_string __temp(__first, __last, __alloc());
2511 assign(__temp.data(), __temp.size());
2512 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002513 return *this;
2514}
2515
2516template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002517basic_string<_CharT, _Traits, _Allocator>&
2518basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2519{
2520 size_type __sz = __str.size();
2521 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002522 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002523 return assign(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002524}
2525
2526template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002527template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002528__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002529<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002530 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
2531 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00002532 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002533>
Marshall Clow82513342016-09-24 22:45:42 +00002534basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002535{
Marshall Clow82513342016-09-24 22:45:42 +00002536 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002537 size_type __sz = __sv.size();
2538 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002539 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002540 return assign(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2541}
2542
2543
2544template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002545basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002546basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s) {
2547 return __assign_external(__s, traits_type::length(__s));
2548}
2549
2550template <class _CharT, class _Traits, class _Allocator>
2551basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002552basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002553{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002554 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Louis Dionne3e0c4332021-08-31 10:49:06 -04002555 return __builtin_constant_p(*__s)
Nikolas Klauser93826d12022-01-04 17:24:03 +01002556 ? (__fits_in_sso(traits_type::length(__s))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002557 ? __assign_short(__s, traits_type::length(__s))
2558 : __assign_external(__s, traits_type::length(__s)))
2559 : __assign_external(__s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002560}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002561// append
2562
2563template <class _CharT, class _Traits, class _Allocator>
2564basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002565basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002566{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002567 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002568 size_type __cap = capacity();
2569 size_type __sz = size();
2570 if (__cap - __sz >= __n)
2571 {
2572 if (__n)
2573 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002574 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002575 traits_type::copy(__p + __sz, __s, __n);
2576 __sz += __n;
2577 __set_size(__sz);
2578 traits_type::assign(__p[__sz], value_type());
2579 }
2580 }
2581 else
2582 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2583 return *this;
2584}
2585
2586template <class _CharT, class _Traits, class _Allocator>
2587basic_string<_CharT, _Traits, _Allocator>&
2588basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2589{
2590 if (__n)
2591 {
2592 size_type __cap = capacity();
2593 size_type __sz = size();
2594 if (__cap - __sz < __n)
2595 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2596 pointer __p = __get_pointer();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002597 traits_type::assign(_VSTD::__to_address(__p) + __sz, __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002598 __sz += __n;
2599 __set_size(__sz);
2600 traits_type::assign(__p[__sz], value_type());
2601 }
2602 return *this;
2603}
2604
2605template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00002606inline void
2607basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n)
2608{
2609 if (__n)
2610 {
2611 size_type __cap = capacity();
2612 size_type __sz = size();
2613 if (__cap - __sz < __n)
2614 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2615 pointer __p = __get_pointer();
2616 __sz += __n;
2617 __set_size(__sz);
2618 traits_type::assign(__p[__sz], value_type());
2619 }
2620}
2621
2622template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002623void
2624basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2625{
Howard Hinnant68bf1812013-04-30 21:44:48 +00002626 bool __is_short = !__is_long();
2627 size_type __cap;
2628 size_type __sz;
2629 if (__is_short)
2630 {
2631 __cap = __min_cap - 1;
2632 __sz = __get_short_size();
2633 }
2634 else
2635 {
2636 __cap = __get_long_cap() - 1;
2637 __sz = __get_long_size();
2638 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002639 if (__sz == __cap)
Howard Hinnant68bf1812013-04-30 21:44:48 +00002640 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002641 __grow_by(__cap, 1, __sz, __sz, 0);
Louis Dionne82d2b2c2021-11-16 11:26:56 -05002642 __is_short = false; // the string is always long after __grow_by
Howard Hinnant68bf1812013-04-30 21:44:48 +00002643 }
2644 pointer __p;
2645 if (__is_short)
2646 {
2647 __p = __get_short_pointer() + __sz;
2648 __set_short_size(__sz+1);
2649 }
2650 else
2651 {
2652 __p = __get_long_pointer() + __sz;
2653 __set_long_size(__sz+1);
2654 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002655 traits_type::assign(*__p, __c);
2656 traits_type::assign(*++__p, value_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002657}
2658
Howard Hinnantc51e1022010-05-11 19:42:16 +00002659template <class _CharT, class _Traits, class _Allocator>
2660template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002661__enable_if_t
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002662<
2663 __is_cpp17_forward_iterator<_ForwardIterator>::value,
2664 basic_string<_CharT, _Traits, _Allocator>&
2665>
2666basic_string<_CharT, _Traits, _Allocator>::append(
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002667 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002668{
2669 size_type __sz = size();
2670 size_type __cap = capacity();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002671 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002672 if (__n)
2673 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002674 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2675 !__addr_in_range(*__first))
Marshall Clow958362f2016-09-05 01:54:30 +00002676 {
2677 if (__cap - __sz < __n)
2678 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2679 pointer __p = __get_pointer() + __sz;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002680 for (; __first != __last; ++__p, (void) ++__first)
Marshall Clow958362f2016-09-05 01:54:30 +00002681 traits_type::assign(*__p, *__first);
2682 traits_type::assign(*__p, value_type());
2683 __set_size(__sz + __n);
2684 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002685 else
2686 {
2687 const basic_string __temp(__first, __last, __alloc());
2688 append(__temp.data(), __temp.size());
2689 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002690 }
2691 return *this;
2692}
2693
2694template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002695inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002696basic_string<_CharT, _Traits, _Allocator>&
2697basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2698{
2699 return append(__str.data(), __str.size());
2700}
2701
2702template <class _CharT, class _Traits, class _Allocator>
2703basic_string<_CharT, _Traits, _Allocator>&
2704basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2705{
2706 size_type __sz = __str.size();
2707 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002708 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002709 return append(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002710}
2711
2712template <class _CharT, class _Traits, class _Allocator>
Marshall Clow62953962016-10-03 23:40:48 +00002713template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002714 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002715 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002716 __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 +00002717 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002718 >
Marshall Clow82513342016-09-24 22:45:42 +00002719basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002720{
Marshall Clow82513342016-09-24 22:45:42 +00002721 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002722 size_type __sz = __sv.size();
2723 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002724 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002725 return append(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2726}
2727
2728template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002729basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002730basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002731{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002732 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002733 return append(__s, traits_type::length(__s));
2734}
2735
2736// insert
2737
2738template <class _CharT, class _Traits, class _Allocator>
2739basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002740basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002741{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002742 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002743 size_type __sz = size();
2744 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002745 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002746 size_type __cap = capacity();
2747 if (__cap - __sz >= __n)
2748 {
2749 if (__n)
2750 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002751 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002752 size_type __n_move = __sz - __pos;
2753 if (__n_move != 0)
2754 {
2755 if (__p + __pos <= __s && __s < __p + __sz)
2756 __s += __n;
2757 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2758 }
2759 traits_type::move(__p + __pos, __s, __n);
2760 __sz += __n;
2761 __set_size(__sz);
2762 traits_type::assign(__p[__sz], value_type());
2763 }
2764 }
2765 else
2766 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2767 return *this;
2768}
2769
2770template <class _CharT, class _Traits, class _Allocator>
2771basic_string<_CharT, _Traits, _Allocator>&
2772basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2773{
2774 size_type __sz = size();
2775 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002776 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002777 if (__n)
2778 {
2779 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002780 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002781 if (__cap - __sz >= __n)
2782 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002783 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002784 size_type __n_move = __sz - __pos;
2785 if (__n_move != 0)
2786 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2787 }
2788 else
2789 {
2790 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002791 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002792 }
2793 traits_type::assign(__p + __pos, __n, __c);
2794 __sz += __n;
2795 __set_size(__sz);
2796 traits_type::assign(__p[__sz], value_type());
2797 }
2798 return *this;
2799}
2800
2801template <class _CharT, class _Traits, class _Allocator>
2802template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002803__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002804<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002805 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Marshall Clow039b2f02016-01-13 21:54:34 +00002806 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002807>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002808basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2809{
Nikolas Klausereed25832021-12-15 01:32:30 +01002810 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2811 "string::insert(iterator, range) called with an iterator not"
2812 " referring to this string");
2813 const basic_string __temp(__first, __last, __alloc());
2814 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002815}
2816
2817template <class _CharT, class _Traits, class _Allocator>
2818template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002819__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002820<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002821 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002822 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002823>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002824basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2825{
Nikolas Klausereed25832021-12-15 01:32:30 +01002826 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2827 "string::insert(iterator, range) called with an iterator not"
2828 " referring to this string");
2829
Howard Hinnantc51e1022010-05-11 19:42:16 +00002830 size_type __ip = static_cast<size_type>(__pos - begin());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002831 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002832 if (__n)
2833 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002834 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2835 !__addr_in_range(*__first))
2836 {
2837 size_type __sz = size();
2838 size_type __cap = capacity();
2839 value_type* __p;
2840 if (__cap - __sz >= __n)
2841 {
2842 __p = _VSTD::__to_address(__get_pointer());
2843 size_type __n_move = __sz - __ip;
2844 if (__n_move != 0)
2845 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
2846 }
2847 else
2848 {
2849 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
2850 __p = _VSTD::__to_address(__get_long_pointer());
2851 }
2852 __sz += __n;
2853 __set_size(__sz);
2854 traits_type::assign(__p[__sz], value_type());
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002855 for (__p += __ip; __first != __last; ++__p, (void) ++__first)
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002856 traits_type::assign(*__p, *__first);
2857 }
2858 else
Marshall Clow958362f2016-09-05 01:54:30 +00002859 {
2860 const basic_string __temp(__first, __last, __alloc());
2861 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
2862 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002863 }
2864 return begin() + __ip;
2865}
2866
2867template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002868inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002869basic_string<_CharT, _Traits, _Allocator>&
2870basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2871{
2872 return insert(__pos1, __str.data(), __str.size());
2873}
2874
2875template <class _CharT, class _Traits, class _Allocator>
2876basic_string<_CharT, _Traits, _Allocator>&
2877basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2878 size_type __pos2, size_type __n)
2879{
2880 size_type __str_sz = __str.size();
2881 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002882 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002883 return insert(__pos1, __str.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002884}
2885
2886template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002887template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002888__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002889<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002890 __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 +00002891 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002892>
Marshall Clow82513342016-09-24 22:45:42 +00002893basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002894 size_type __pos2, size_type __n)
2895{
Marshall Clow82513342016-09-24 22:45:42 +00002896 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002897 size_type __str_sz = __sv.size();
2898 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002899 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002900 return insert(__pos1, __sv.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
2901}
2902
2903template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002904basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002905basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002906{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002907 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002908 return insert(__pos, __s, traits_type::length(__s));
2909}
2910
2911template <class _CharT, class _Traits, class _Allocator>
2912typename basic_string<_CharT, _Traits, _Allocator>::iterator
2913basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2914{
2915 size_type __ip = static_cast<size_type>(__pos - begin());
2916 size_type __sz = size();
2917 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002918 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002919 if (__cap == __sz)
2920 {
2921 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002922 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002923 }
2924 else
2925 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002926 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002927 size_type __n_move = __sz - __ip;
2928 if (__n_move != 0)
2929 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2930 }
2931 traits_type::assign(__p[__ip], __c);
2932 traits_type::assign(__p[++__sz], value_type());
2933 __set_size(__sz);
2934 return begin() + static_cast<difference_type>(__ip);
2935}
2936
2937template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002938inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002939typename basic_string<_CharT, _Traits, _Allocator>::iterator
2940basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2941{
Nikolas Klausereed25832021-12-15 01:32:30 +01002942 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2943 "string::insert(iterator, n, value) called with an iterator not"
2944 " referring to this string");
2945 difference_type __p = __pos - begin();
2946 insert(static_cast<size_type>(__p), __n, __c);
2947 return begin() + __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002948}
2949
2950// replace
2951
2952template <class _CharT, class _Traits, class _Allocator>
2953basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002954basic_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 +00002955 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00002956{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002957 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002958 size_type __sz = size();
2959 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002960 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002961 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002962 size_type __cap = capacity();
2963 if (__cap - __sz + __n1 >= __n2)
2964 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002965 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002966 if (__n1 != __n2)
2967 {
2968 size_type __n_move = __sz - __pos - __n1;
2969 if (__n_move != 0)
2970 {
2971 if (__n1 > __n2)
2972 {
2973 traits_type::move(__p + __pos, __s, __n2);
2974 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002975 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002976 }
2977 if (__p + __pos < __s && __s < __p + __sz)
2978 {
2979 if (__p + __pos + __n1 <= __s)
2980 __s += __n2 - __n1;
2981 else // __p + __pos < __s < __p + __pos + __n1
2982 {
2983 traits_type::move(__p + __pos, __s, __n1);
2984 __pos += __n1;
2985 __s += __n2;
2986 __n2 -= __n1;
2987 __n1 = 0;
2988 }
2989 }
2990 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2991 }
2992 }
2993 traits_type::move(__p + __pos, __s, __n2);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002994 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002995 }
2996 else
2997 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
2998 return *this;
2999}
3000
3001template <class _CharT, class _Traits, class _Allocator>
3002basic_string<_CharT, _Traits, _Allocator>&
3003basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
3004{
3005 size_type __sz = size();
3006 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003007 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003008 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003009 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00003010 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003011 if (__cap - __sz + __n1 >= __n2)
3012 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003013 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003014 if (__n1 != __n2)
3015 {
3016 size_type __n_move = __sz - __pos - __n1;
3017 if (__n_move != 0)
3018 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3019 }
3020 }
3021 else
3022 {
3023 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003024 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003025 }
3026 traits_type::assign(__p + __pos, __n2, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003027 return __null_terminate_at(__p, __sz - (__n1 - __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003028}
3029
3030template <class _CharT, class _Traits, class _Allocator>
3031template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003032__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00003033<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003034 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003035 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003036>
Howard Hinnant990d6e82010-11-17 21:11:40 +00003037basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003038 _InputIterator __j1, _InputIterator __j2)
3039{
Marshall Clow958362f2016-09-05 01:54:30 +00003040 const basic_string __temp(__j1, __j2, __alloc());
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003041 return replace(__i1, __i2, __temp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003042}
3043
3044template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003045inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003046basic_string<_CharT, _Traits, _Allocator>&
3047basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
3048{
3049 return replace(__pos1, __n1, __str.data(), __str.size());
3050}
3051
3052template <class _CharT, class _Traits, class _Allocator>
3053basic_string<_CharT, _Traits, _Allocator>&
3054basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
3055 size_type __pos2, size_type __n2)
3056{
3057 size_type __str_sz = __str.size();
3058 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003059 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003060 return replace(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003061}
3062
3063template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003064template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003065__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003066<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003067 __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 +00003068 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003069>
Marshall Clow82513342016-09-24 22:45:42 +00003070basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003071 size_type __pos2, size_type __n2)
3072{
Marshall Clow82513342016-09-24 22:45:42 +00003073 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003074 size_type __str_sz = __sv.size();
3075 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003076 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003077 return replace(__pos1, __n1, __sv.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
3078}
3079
3080template <class _CharT, class _Traits, class _Allocator>
3081basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003082basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003083{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003084 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003085 return replace(__pos, __n1, __s, traits_type::length(__s));
3086}
3087
3088template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003089inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003090basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003091basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003092{
3093 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
3094 __str.data(), __str.size());
3095}
3096
3097template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003098inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003099basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003100basic_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 +00003101{
3102 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
3103}
3104
3105template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003106inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003107basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003108basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003109{
3110 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
3111}
3112
3113template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003114inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003115basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003116basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003117{
3118 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
3119}
3120
3121// erase
3122
Martijn Velsa81fc792020-02-26 13:25:43 -05003123// 'externally instantiated' erase() implementation, called when __n != npos.
3124// Does not check __pos against size()
Howard Hinnantc51e1022010-05-11 19:42:16 +00003125template <class _CharT, class _Traits, class _Allocator>
Martijn Velsa81fc792020-02-26 13:25:43 -05003126void
3127basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(
3128 size_type __pos, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003129{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003130 if (__n)
3131 {
Martijn Velsa81fc792020-02-26 13:25:43 -05003132 size_type __sz = size();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003133 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003134 __n = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003135 size_type __n_move = __sz - __pos - __n;
3136 if (__n_move != 0)
3137 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003138 __null_terminate_at(__p, __sz - __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003139 }
Martijn Velsa81fc792020-02-26 13:25:43 -05003140}
3141
3142template <class _CharT, class _Traits, class _Allocator>
3143basic_string<_CharT, _Traits, _Allocator>&
3144basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos,
3145 size_type __n) {
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003146 if (__pos > size())
3147 __throw_out_of_range();
Martijn Velsa81fc792020-02-26 13:25:43 -05003148 if (__n == npos) {
3149 __erase_to_end(__pos);
3150 } else {
3151 __erase_external_with_move(__pos, __n);
3152 }
3153 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003154}
3155
3156template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003157inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003158typename basic_string<_CharT, _Traits, _Allocator>::iterator
3159basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
3160{
Nikolas Klausereed25832021-12-15 01:32:30 +01003161 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3162 "string::erase(iterator) called with an iterator not"
3163 " referring to this string");
3164
3165 _LIBCPP_ASSERT(__pos != end(), "string::erase(iterator) called with a non-dereferenceable iterator");
3166 iterator __b = begin();
3167 size_type __r = static_cast<size_type>(__pos - __b);
3168 erase(__r, 1);
3169 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003170}
3171
3172template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003173inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003174typename basic_string<_CharT, _Traits, _Allocator>::iterator
3175basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
3176{
Nikolas Klausereed25832021-12-15 01:32:30 +01003177 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
3178 "string::erase(iterator, iterator) called with an iterator not"
3179 " referring to this string");
3180
3181 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
3182 iterator __b = begin();
3183 size_type __r = static_cast<size_type>(__first - __b);
3184 erase(__r, static_cast<size_type>(__last - __first));
3185 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003186}
3187
3188template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003189inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003190void
3191basic_string<_CharT, _Traits, _Allocator>::pop_back()
3192{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003193 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003194 __erase_to_end(size() - 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003195}
3196
3197template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003198inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003199void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003200basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003201{
3202 __invalidate_all_iterators();
3203 if (__is_long())
3204 {
3205 traits_type::assign(*__get_long_pointer(), value_type());
3206 __set_long_size(0);
3207 }
3208 else
3209 {
3210 traits_type::assign(*__get_short_pointer(), value_type());
3211 __set_short_size(0);
3212 }
3213}
3214
3215template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003216inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003217void
3218basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
3219{
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003220 __null_terminate_at(_VSTD::__to_address(__get_pointer()), __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003221}
3222
3223template <class _CharT, class _Traits, class _Allocator>
3224void
3225basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
3226{
3227 size_type __sz = size();
3228 if (__n > __sz)
3229 append(__n - __sz, __c);
3230 else
3231 __erase_to_end(__n);
3232}
3233
3234template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00003235inline void
3236basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n)
3237{
3238 size_type __sz = size();
3239 if (__n > __sz) {
3240 __append_default_init(__n - __sz);
3241 } else
3242 __erase_to_end(__n);
3243}
3244
3245template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003246inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003247typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003248basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003249{
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003250 size_type __m = __alloc_traits::max_size(__alloc());
Eric Fiseliere9cc5922017-10-17 13:16:01 +00003251#ifdef _LIBCPP_BIG_ENDIAN
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003252 return (__m <= ~__long_mask ? __m : __m/2) - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003253#else
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003254 return __m - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003255#endif
3256}
3257
3258template <class _CharT, class _Traits, class _Allocator>
3259void
Marek Kurdejc9848142020-11-26 10:07:16 +01003260basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __requested_capacity)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003261{
Marek Kurdejc9848142020-11-26 10:07:16 +01003262 if (__requested_capacity > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003263 __throw_length_error();
Marek Kurdejc9848142020-11-26 10:07:16 +01003264
Louis Dionne05b6d3e2022-01-14 12:30:22 -05003265 // Make sure reserve(n) never shrinks. This is technically only required in C++20
3266 // and later (since P0966R1), however we provide consistent behavior in all Standard
3267 // modes because this function is instantiated in the shared library.
3268 if (__requested_capacity <= capacity())
3269 return;
Marek Kurdejc9848142020-11-26 10:07:16 +01003270
3271 size_type __target_capacity = _VSTD::max(__requested_capacity, size());
3272 __target_capacity = __recommend(__target_capacity);
3273 if (__target_capacity == capacity()) return;
3274
3275 __shrink_or_extend(__target_capacity);
3276}
3277
3278template <class _CharT, class _Traits, class _Allocator>
Louis Dionnee38b8642021-12-13 14:15:21 -05003279inline
Marek Kurdejc9848142020-11-26 10:07:16 +01003280void
3281basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT
3282{
3283 size_type __target_capacity = __recommend(size());
3284 if (__target_capacity == capacity()) return;
3285
3286 __shrink_or_extend(__target_capacity);
3287}
3288
3289template <class _CharT, class _Traits, class _Allocator>
Louis Dionnee38b8642021-12-13 14:15:21 -05003290inline
Marek Kurdejc9848142020-11-26 10:07:16 +01003291void
3292basic_string<_CharT, _Traits, _Allocator>::__shrink_or_extend(size_type __target_capacity)
3293{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003294 size_type __cap = capacity();
3295 size_type __sz = size();
Marek Kurdejc9848142020-11-26 10:07:16 +01003296
3297 pointer __new_data, __p;
3298 bool __was_long, __now_long;
3299 if (__target_capacity == __min_cap - 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003300 {
Marek Kurdejc9848142020-11-26 10:07:16 +01003301 __was_long = true;
3302 __now_long = false;
3303 __new_data = __get_short_pointer();
3304 __p = __get_long_pointer();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003305 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003306 else
3307 {
3308 if (__target_capacity > __cap)
3309 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3310 else
3311 {
3312 #ifndef _LIBCPP_NO_EXCEPTIONS
3313 try
3314 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003315 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003316 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3317 #ifndef _LIBCPP_NO_EXCEPTIONS
3318 }
3319 catch (...)
3320 {
3321 return;
3322 }
3323 #else // _LIBCPP_NO_EXCEPTIONS
3324 if (__new_data == nullptr)
3325 return;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003326 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003327 }
3328 __now_long = true;
3329 __was_long = __is_long();
3330 __p = __get_pointer();
3331 }
3332 traits_type::copy(_VSTD::__to_address(__new_data),
3333 _VSTD::__to_address(__p), size()+1);
3334 if (__was_long)
3335 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
3336 if (__now_long)
3337 {
3338 __set_long_cap(__target_capacity+1);
3339 __set_long_size(__sz);
3340 __set_long_pointer(__new_data);
3341 }
3342 else
3343 __set_short_size(__sz);
3344 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003345}
3346
3347template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003348inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003349typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003350basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003351{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003352 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003353 return *(data() + __pos);
3354}
3355
3356template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003357inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003358typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003359basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003360{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003361 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003362 return *(__get_pointer() + __pos);
3363}
3364
3365template <class _CharT, class _Traits, class _Allocator>
3366typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3367basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3368{
3369 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003370 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003371 return (*this)[__n];
3372}
3373
3374template <class _CharT, class _Traits, class _Allocator>
3375typename basic_string<_CharT, _Traits, _Allocator>::reference
3376basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
3377{
3378 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003379 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003380 return (*this)[__n];
3381}
3382
3383template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003384inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003385typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003386basic_string<_CharT, _Traits, _Allocator>::front() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003387{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003388 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003389 return *__get_pointer();
3390}
3391
3392template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003393inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003394typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003395basic_string<_CharT, _Traits, _Allocator>::front() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003396{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003397 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003398 return *data();
3399}
3400
3401template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003402inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003403typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003404basic_string<_CharT, _Traits, _Allocator>::back() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003405{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003406 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003407 return *(__get_pointer() + size() - 1);
3408}
3409
3410template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003411inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003412typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003413basic_string<_CharT, _Traits, _Allocator>::back() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003414{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003415 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003416 return *(data() + size() - 1);
3417}
3418
3419template <class _CharT, class _Traits, class _Allocator>
3420typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003421basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003422{
3423 size_type __sz = size();
3424 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003425 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003426 size_type __rlen = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003427 traits_type::copy(__s, data() + __pos, __rlen);
3428 return __rlen;
3429}
3430
3431template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003432inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003433basic_string<_CharT, _Traits, _Allocator>
3434basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3435{
3436 return basic_string(*this, __pos, __n, __alloc());
3437}
3438
3439template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003440inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003441void
3442basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003443#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00003444 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00003445#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00003446 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003447 __is_nothrow_swappable<allocator_type>::value)
3448#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003449{
Louis Dionneba400782020-10-02 15:02:52 -04003450#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01003451 if (!__libcpp_is_constant_evaluated()) {
3452 if (!__is_long())
3453 __get_db()->__invalidate_all(this);
3454 if (!__str.__is_long())
3455 __get_db()->__invalidate_all(&__str);
3456 __get_db()->swap(this, &__str);
3457 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00003458#endif
Eric Fiselier9bf691f2016-12-28 05:53:01 +00003459 _LIBCPP_ASSERT(
3460 __alloc_traits::propagate_on_container_swap::value ||
3461 __alloc_traits::is_always_equal::value ||
3462 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003463 _VSTD::swap(__r_.first(), __str.__r_.first());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05003464 _VSTD::__swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003465}
3466
3467// find
3468
3469template <class _Traits>
3470struct _LIBCPP_HIDDEN __traits_eq
3471{
3472 typedef typename _Traits::char_type char_type;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003473 _LIBCPP_INLINE_VISIBILITY
3474 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3475 {return _Traits::eq(__x, __y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003476};
3477
3478template<class _CharT, class _Traits, class _Allocator>
3479typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003480basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003481 size_type __pos,
3482 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003483{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003484 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003485 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003486 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003487}
3488
3489template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003490inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003491typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003492basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3493 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003494{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003495 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003496 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003497}
3498
3499template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003500template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003501__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003502<
3503 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3504 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003505>
Marshall Clowe46031a2018-07-02 18:41:15 +00003506basic_string<_CharT, _Traits, _Allocator>::find(const _Tp &__t,
zoecarver1997e0a2021-02-05 11:54:47 -08003507 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003508{
Marshall Clowe46031a2018-07-02 18:41:15 +00003509 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003510 return __str_find<value_type, size_type, traits_type, npos>
3511 (data(), size(), __sv.data(), __pos, __sv.size());
3512}
3513
3514template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003515inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003516typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003517basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003518 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003519{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003520 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003521 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003522 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003523}
3524
3525template<class _CharT, class _Traits, class _Allocator>
3526typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003527basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3528 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003529{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003530 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003531 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003532}
3533
3534// rfind
3535
3536template<class _CharT, class _Traits, class _Allocator>
3537typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003538basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003539 size_type __pos,
3540 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003541{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003542 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003543 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003544 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003545}
3546
3547template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003548inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003549typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003550basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3551 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003552{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003553 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003554 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003555}
3556
3557template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003558template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003559__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003560<
3561 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3562 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003563>
Marshall Clowe46031a2018-07-02 18:41:15 +00003564basic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003565 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003566{
Marshall Clowe46031a2018-07-02 18:41:15 +00003567 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003568 return __str_rfind<value_type, size_type, traits_type, npos>
3569 (data(), size(), __sv.data(), __pos, __sv.size());
3570}
3571
3572template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003573inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003574typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003575basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003576 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003577{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003578 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003579 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003580 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003581}
3582
3583template<class _CharT, class _Traits, class _Allocator>
3584typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003585basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3586 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003587{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003588 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003589 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003590}
3591
3592// find_first_of
3593
3594template<class _CharT, class _Traits, class _Allocator>
3595typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003596basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003597 size_type __pos,
3598 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003599{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003600 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003601 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003602 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003603}
3604
3605template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003606inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003607typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003608basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3609 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003610{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003611 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003612 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003613}
3614
3615template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003616template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003617__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003618<
3619 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3620 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003621>
Marshall Clowe46031a2018-07-02 18:41:15 +00003622basic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003623 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003624{
Marshall Clowe46031a2018-07-02 18:41:15 +00003625 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003626 return __str_find_first_of<value_type, size_type, traits_type, npos>
3627 (data(), size(), __sv.data(), __pos, __sv.size());
3628}
3629
3630template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003631inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003632typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003633basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003634 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003635{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003636 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003637 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003638 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003639}
3640
3641template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003642inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003643typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003644basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3645 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003646{
3647 return find(__c, __pos);
3648}
3649
3650// find_last_of
3651
3652template<class _CharT, class _Traits, class _Allocator>
3653typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003654basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003655 size_type __pos,
3656 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003657{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003658 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003659 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003660 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003661}
3662
3663template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003664inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003665typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003666basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3667 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003668{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003669 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003670 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003671}
3672
3673template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003674template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003675__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003676<
3677 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3678 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003679>
Marshall Clowe46031a2018-07-02 18:41:15 +00003680basic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003681 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003682{
Marshall Clowe46031a2018-07-02 18:41:15 +00003683 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003684 return __str_find_last_of<value_type, size_type, traits_type, npos>
3685 (data(), size(), __sv.data(), __pos, __sv.size());
3686}
3687
3688template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003689inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003690typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003691basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003692 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003693{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003694 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003695 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003696 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003697}
3698
3699template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003700inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003701typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003702basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3703 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003704{
3705 return rfind(__c, __pos);
3706}
3707
3708// find_first_not_of
3709
3710template<class _CharT, class _Traits, class _Allocator>
3711typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003712basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003713 size_type __pos,
3714 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003715{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003716 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003717 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003718 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003719}
3720
3721template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003722inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003723typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003724basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3725 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003726{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003727 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003728 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003729}
3730
3731template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003732template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003733__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003734<
3735 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3736 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003737>
Marshall Clowe46031a2018-07-02 18:41:15 +00003738basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003739 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003740{
Marshall Clowe46031a2018-07-02 18:41:15 +00003741 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003742 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3743 (data(), size(), __sv.data(), __pos, __sv.size());
3744}
3745
3746template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003747inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003748typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003749basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003750 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003751{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003752 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003753 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003754 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003755}
3756
3757template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003758inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003759typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003760basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3761 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003762{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003763 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003764 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003765}
3766
3767// find_last_not_of
3768
3769template<class _CharT, class _Traits, class _Allocator>
3770typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003771basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003772 size_type __pos,
3773 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003774{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003775 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003776 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003777 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003778}
3779
3780template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003781inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003782typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003783basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3784 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003785{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003786 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003787 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003788}
3789
3790template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003791template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003792__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003793<
3794 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3795 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003796>
Marshall Clowe46031a2018-07-02 18:41:15 +00003797basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003798 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003799{
Marshall Clowe46031a2018-07-02 18:41:15 +00003800 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003801 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3802 (data(), size(), __sv.data(), __pos, __sv.size());
3803}
3804
3805template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003806inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003807typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003808basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003809 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003810{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003811 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003812 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003813 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003814}
3815
3816template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003817inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003818typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003819basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3820 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003821{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003822 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003823 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003824}
3825
3826// compare
3827
3828template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003829template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003830__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003831<
3832 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3833 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003834>
zoecarver1997e0a2021-02-05 11:54:47 -08003835basic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003836{
Marshall Clowe46031a2018-07-02 18:41:15 +00003837 __self_view __sv = __t;
Howard Hinnantb0485532011-07-24 21:45:06 +00003838 size_t __lhs_sz = size();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003839 size_t __rhs_sz = __sv.size();
3840 int __result = traits_type::compare(data(), __sv.data(),
Howard Hinnantb0485532011-07-24 21:45:06 +00003841 _VSTD::min(__lhs_sz, __rhs_sz));
3842 if (__result != 0)
3843 return __result;
3844 if (__lhs_sz < __rhs_sz)
3845 return -1;
3846 if (__lhs_sz > __rhs_sz)
3847 return 1;
3848 return 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003849}
3850
3851template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003852inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003853int
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003854basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003855{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003856 return compare(__self_view(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003857}
3858
3859template <class _CharT, class _Traits, class _Allocator>
3860int
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003861basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3862 size_type __n1,
Howard Hinnantd17880b2013-06-28 16:59:19 +00003863 const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003864 size_type __n2) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003865{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003866 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003867 size_type __sz = size();
3868 if (__pos1 > __sz || __n2 == npos)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003869 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003870 size_type __rlen = _VSTD::min(__n1, __sz - __pos1);
3871 int __r = traits_type::compare(data() + __pos1, __s, _VSTD::min(__rlen, __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003872 if (__r == 0)
3873 {
3874 if (__rlen < __n2)
3875 __r = -1;
3876 else if (__rlen > __n2)
3877 __r = 1;
3878 }
3879 return __r;
3880}
3881
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003882template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003883template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003884__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003885<
3886 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3887 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003888>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003889basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3890 size_type __n1,
Marshall Clowe46031a2018-07-02 18:41:15 +00003891 const _Tp& __t) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003892{
Marshall Clowe46031a2018-07-02 18:41:15 +00003893 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003894 return compare(__pos1, __n1, __sv.data(), __sv.size());
3895}
3896
3897template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003898inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003899int
3900basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3901 size_type __n1,
3902 const basic_string& __str) const
3903{
3904 return compare(__pos1, __n1, __str.data(), __str.size());
3905}
3906
3907template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003908template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003909__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003910<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003911 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
3912 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00003913 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003914>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003915basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3916 size_type __n1,
Marshall Clow82513342016-09-24 22:45:42 +00003917 const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003918 size_type __pos2,
3919 size_type __n2) const
3920{
Marshall Clow82513342016-09-24 22:45:42 +00003921 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003922 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3923}
3924
3925template <class _CharT, class _Traits, class _Allocator>
3926int
3927basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3928 size_type __n1,
3929 const basic_string& __str,
3930 size_type __pos2,
3931 size_type __n2) const
3932{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003933 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003934}
3935
3936template <class _CharT, class _Traits, class _Allocator>
3937int
3938basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
3939{
3940 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3941 return compare(0, npos, __s, traits_type::length(__s));
3942}
3943
3944template <class _CharT, class _Traits, class _Allocator>
3945int
3946basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3947 size_type __n1,
3948 const value_type* __s) const
3949{
3950 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3951 return compare(__pos1, __n1, __s, traits_type::length(__s));
3952}
3953
Howard Hinnantc51e1022010-05-11 19:42:16 +00003954// __invariants
3955
3956template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003957inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003958bool
3959basic_string<_CharT, _Traits, _Allocator>::__invariants() const
3960{
3961 if (size() > capacity())
3962 return false;
3963 if (capacity() < __min_cap - 1)
3964 return false;
Bruce Mitchener170d8972020-11-24 12:53:53 -05003965 if (data() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003966 return false;
Louis Dionne663415f2020-10-05 16:16:13 -04003967 if (data()[size()] != value_type())
Howard Hinnantc51e1022010-05-11 19:42:16 +00003968 return false;
3969 return true;
3970}
3971
Vedant Kumar55e007e2018-03-08 21:15:26 +00003972// __clear_and_shrink
3973
3974template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003975inline
Louis Dionne173f29e2019-05-29 16:01:36 +00003976void
Marshall Clowe60a7182018-05-29 17:04:37 +00003977basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
Vedant Kumar55e007e2018-03-08 21:15:26 +00003978{
3979 clear();
3980 if(__is_long())
3981 {
3982 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
3983 __set_long_cap(0);
3984 __set_short_size(0);
Louis Dionne663415f2020-10-05 16:16:13 -04003985 traits_type::assign(*__get_short_pointer(), value_type());
Vedant Kumar55e007e2018-03-08 21:15:26 +00003986 }
Louis Dionne173f29e2019-05-29 16:01:36 +00003987}
Vedant Kumar55e007e2018-03-08 21:15:26 +00003988
Howard Hinnantc51e1022010-05-11 19:42:16 +00003989// operator==
3990
3991template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003992inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003993bool
3994operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003995 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003996{
Howard Hinnantaaeb1132013-04-22 23:55:13 +00003997 size_t __lhs_sz = __lhs.size();
3998 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
3999 __rhs.data(),
4000 __lhs_sz) == 0;
4001}
4002
4003template<class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004004inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004005bool
4006operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
4007 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
4008{
4009 size_t __lhs_sz = __lhs.size();
4010 if (__lhs_sz != __rhs.size())
4011 return false;
4012 const char* __lp = __lhs.data();
4013 const char* __rp = __rhs.data();
4014 if (__lhs.__is_long())
4015 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
4016 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
4017 if (*__lp != *__rp)
4018 return false;
4019 return true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004020}
4021
4022template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004023inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004024bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004025operator==(const _CharT* __lhs,
4026 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004027{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004028 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4029 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
4030 size_t __lhs_len = _Traits::length(__lhs);
4031 if (__lhs_len != __rhs.size()) return false;
4032 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004033}
4034
Howard Hinnantc51e1022010-05-11 19:42:16 +00004035template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004036inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004037bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004038operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
4039 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004040{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004041 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4042 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
4043 size_t __rhs_len = _Traits::length(__rhs);
4044 if (__rhs_len != __lhs.size()) return false;
4045 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004046}
4047
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004048template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004049inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004050bool
4051operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004052 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004053{
4054 return !(__lhs == __rhs);
4055}
4056
4057template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004058inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004059bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004060operator!=(const _CharT* __lhs,
4061 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004062{
4063 return !(__lhs == __rhs);
4064}
4065
4066template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004067inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004068bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004069operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4070 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004071{
4072 return !(__lhs == __rhs);
4073}
4074
4075// operator<
4076
4077template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004078inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004079bool
4080operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004081 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004082{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004083 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004084}
4085
4086template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004087inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004088bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004089operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4090 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004091{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004092 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004093}
4094
4095template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004096inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004097bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004098operator< (const _CharT* __lhs,
4099 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004100{
4101 return __rhs.compare(__lhs) > 0;
4102}
4103
Howard Hinnantc51e1022010-05-11 19:42:16 +00004104// operator>
4105
4106template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004107inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004108bool
4109operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004110 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004111{
4112 return __rhs < __lhs;
4113}
4114
4115template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004116inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004117bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004118operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4119 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004120{
4121 return __rhs < __lhs;
4122}
4123
4124template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004125inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004126bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004127operator> (const _CharT* __lhs,
4128 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004129{
4130 return __rhs < __lhs;
4131}
4132
4133// operator<=
4134
4135template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004136inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004137bool
4138operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004139 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004140{
4141 return !(__rhs < __lhs);
4142}
4143
4144template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004145inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004146bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004147operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4148 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004149{
4150 return !(__rhs < __lhs);
4151}
4152
4153template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004154inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004155bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004156operator<=(const _CharT* __lhs,
4157 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004158{
4159 return !(__rhs < __lhs);
4160}
4161
4162// operator>=
4163
4164template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004165inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004166bool
4167operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004168 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004169{
4170 return !(__lhs < __rhs);
4171}
4172
4173template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004174inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004175bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004176operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4177 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004178{
4179 return !(__lhs < __rhs);
4180}
4181
4182template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004183inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004184bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004185operator>=(const _CharT* __lhs,
4186 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004187{
4188 return !(__lhs < __rhs);
4189}
4190
4191// operator +
4192
4193template<class _CharT, class _Traits, class _Allocator>
4194basic_string<_CharT, _Traits, _Allocator>
4195operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4196 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4197{
4198 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4199 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4200 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4201 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4202 __r.append(__rhs.data(), __rhs_sz);
4203 return __r;
4204}
4205
4206template<class _CharT, class _Traits, class _Allocator>
4207basic_string<_CharT, _Traits, _Allocator>
4208operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4209{
4210 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4211 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = _Traits::length(__lhs);
4212 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4213 __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz);
4214 __r.append(__rhs.data(), __rhs_sz);
4215 return __r;
4216}
4217
4218template<class _CharT, class _Traits, class _Allocator>
4219basic_string<_CharT, _Traits, _Allocator>
4220operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4221{
4222 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4223 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4224 __r.__init(&__lhs, 1, 1 + __rhs_sz);
4225 __r.append(__rhs.data(), __rhs_sz);
4226 return __r;
4227}
4228
4229template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004230inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004231basic_string<_CharT, _Traits, _Allocator>
4232operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
4233{
4234 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4235 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4236 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = _Traits::length(__rhs);
4237 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4238 __r.append(__rhs, __rhs_sz);
4239 return __r;
4240}
4241
4242template<class _CharT, class _Traits, class _Allocator>
4243basic_string<_CharT, _Traits, _Allocator>
4244operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
4245{
4246 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4247 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4248 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1);
4249 __r.push_back(__rhs);
4250 return __r;
4251}
4252
Eric Fiselierfc92be82017-04-19 00:28:44 +00004253#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004254
4255template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004256inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004257basic_string<_CharT, _Traits, _Allocator>
4258operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4259{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004260 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004261}
4262
4263template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004264inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004265basic_string<_CharT, _Traits, _Allocator>
4266operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4267{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004268 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004269}
4270
4271template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004272inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004273basic_string<_CharT, _Traits, _Allocator>
4274operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4275{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004276 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004277}
4278
4279template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004280inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004281basic_string<_CharT, _Traits, _Allocator>
4282operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4283{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004284 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004285}
4286
4287template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004288inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004289basic_string<_CharT, _Traits, _Allocator>
4290operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4291{
4292 __rhs.insert(__rhs.begin(), __lhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004293 return _VSTD::move(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004294}
4295
4296template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004297inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004298basic_string<_CharT, _Traits, _Allocator>
4299operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
4300{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004301 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004302}
4303
4304template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004305inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004306basic_string<_CharT, _Traits, _Allocator>
4307operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
4308{
4309 __lhs.push_back(__rhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004310 return _VSTD::move(__lhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004311}
4312
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004313#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004314
4315// swap
4316
4317template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004318inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004319void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004320swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +00004321 basic_string<_CharT, _Traits, _Allocator>& __rhs)
4322 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004323{
4324 __lhs.swap(__rhs);
4325}
4326
Bruce Mitchener170d8972020-11-24 12:53:53 -05004327_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = nullptr, int __base = 10);
4328_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = nullptr, int __base = 10);
4329_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = nullptr, int __base = 10);
4330_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = nullptr, int __base = 10);
4331_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004332
Bruce Mitchener170d8972020-11-24 12:53:53 -05004333_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = nullptr);
4334_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = nullptr);
4335_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004336
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004337_LIBCPP_FUNC_VIS string to_string(int __val);
4338_LIBCPP_FUNC_VIS string to_string(unsigned __val);
4339_LIBCPP_FUNC_VIS string to_string(long __val);
4340_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
4341_LIBCPP_FUNC_VIS string to_string(long long __val);
4342_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
4343_LIBCPP_FUNC_VIS string to_string(float __val);
4344_LIBCPP_FUNC_VIS string to_string(double __val);
4345_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004346
Louis Dionne89258142021-08-23 15:32:36 -04004347#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Bruce Mitchener170d8972020-11-24 12:53:53 -05004348_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4349_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4350_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4351_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4352_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004353
Bruce Mitchener170d8972020-11-24 12:53:53 -05004354_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = nullptr);
4355_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = nullptr);
4356_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004357
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004358_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
4359_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
4360_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
4361_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
4362_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
4363_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
4364_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
4365_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
4366_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Louis Dionne89258142021-08-23 15:32:36 -04004367#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004368
Howard Hinnantc51e1022010-05-11 19:42:16 +00004369template<class _CharT, class _Traits, class _Allocator>
Martin Storsjö88890c22021-02-22 01:13:13 +02004370_LIBCPP_TEMPLATE_DATA_VIS
Eric Fiselierc9fdaf12020-01-15 17:02:17 -05004371const typename basic_string<_CharT, _Traits, _Allocator>::size_type
4372 basic_string<_CharT, _Traits, _Allocator>::npos;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004373
Marshall Clow851b9ec2019-05-20 21:56:51 +00004374template <class _CharT, class _Allocator>
4375struct _LIBCPP_TEMPLATE_VIS
4376 hash<basic_string<_CharT, char_traits<_CharT>, _Allocator> >
4377 : public unary_function<
4378 basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004379{
4380 size_t
Marshall Clow851b9ec2019-05-20 21:56:51 +00004381 operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT
4382 { return __do_string_hash(__val.data(), __val.data() + __val.size()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004383};
4384
Howard Hinnantc51e1022010-05-11 19:42:16 +00004385
Howard Hinnantdc095972011-07-18 15:51:59 +00004386template<class _CharT, class _Traits, class _Allocator>
4387basic_ostream<_CharT, _Traits>&
4388operator<<(basic_ostream<_CharT, _Traits>& __os,
4389 const basic_string<_CharT, _Traits, _Allocator>& __str);
4390
4391template<class _CharT, class _Traits, class _Allocator>
4392basic_istream<_CharT, _Traits>&
4393operator>>(basic_istream<_CharT, _Traits>& __is,
4394 basic_string<_CharT, _Traits, _Allocator>& __str);
4395
4396template<class _CharT, class _Traits, class _Allocator>
4397basic_istream<_CharT, _Traits>&
4398getline(basic_istream<_CharT, _Traits>& __is,
4399 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4400
4401template<class _CharT, class _Traits, class _Allocator>
4402inline _LIBCPP_INLINE_VISIBILITY
4403basic_istream<_CharT, _Traits>&
4404getline(basic_istream<_CharT, _Traits>& __is,
4405 basic_string<_CharT, _Traits, _Allocator>& __str);
4406
Howard Hinnantdc095972011-07-18 15:51:59 +00004407template<class _CharT, class _Traits, class _Allocator>
4408inline _LIBCPP_INLINE_VISIBILITY
4409basic_istream<_CharT, _Traits>&
4410getline(basic_istream<_CharT, _Traits>&& __is,
4411 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4412
4413template<class _CharT, class _Traits, class _Allocator>
4414inline _LIBCPP_INLINE_VISIBILITY
4415basic_istream<_CharT, _Traits>&
4416getline(basic_istream<_CharT, _Traits>&& __is,
4417 basic_string<_CharT, _Traits, _Allocator>& __str);
4418
Marshall Clow29b53f22018-12-14 18:49:35 +00004419#if _LIBCPP_STD_VER > 17
Marek Kurdeja98b1412020-05-02 13:58:03 +02004420template <class _CharT, class _Traits, class _Allocator, class _Up>
Marshall Clow29b53f22018-12-14 18:49:35 +00004421inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004422 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4423 erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
4424 auto __old_size = __str.size();
4425 __str.erase(_VSTD::remove(__str.begin(), __str.end(), __v), __str.end());
4426 return __old_size - __str.size();
4427}
Marshall Clow29b53f22018-12-14 18:49:35 +00004428
Marek Kurdeja98b1412020-05-02 13:58:03 +02004429template <class _CharT, class _Traits, class _Allocator, class _Predicate>
Marshall Clow29b53f22018-12-14 18:49:35 +00004430inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004431 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4432 erase_if(basic_string<_CharT, _Traits, _Allocator>& __str,
4433 _Predicate __pred) {
4434 auto __old_size = __str.size();
4435 __str.erase(_VSTD::remove_if(__str.begin(), __str.end(), __pred),
4436 __str.end());
4437 return __old_size - __str.size();
4438}
Marshall Clow29b53f22018-12-14 18:49:35 +00004439#endif
4440
Louis Dionneba400782020-10-02 15:02:52 -04004441#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004442
4443template<class _CharT, class _Traits, class _Allocator>
4444bool
4445basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4446{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004447 return data() <= _VSTD::__to_address(__i->base()) &&
4448 _VSTD::__to_address(__i->base()) < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004449}
4450
4451template<class _CharT, class _Traits, class _Allocator>
4452bool
4453basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4454{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004455 return data() < _VSTD::__to_address(__i->base()) &&
4456 _VSTD::__to_address(__i->base()) <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004457}
4458
4459template<class _CharT, class _Traits, class _Allocator>
4460bool
4461basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4462{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004463 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004464 return data() <= __p && __p <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004465}
4466
4467template<class _CharT, class _Traits, class _Allocator>
4468bool
4469basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4470{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004471 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004472 return data() <= __p && __p < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004473}
4474
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004475#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004476
Louis Dionne173f29e2019-05-29 16:01:36 +00004477#if _LIBCPP_STD_VER > 11
Marshall Clowcba751f2013-07-23 17:05:24 +00004478// Literal suffixes for basic_string [basic.string.literals]
Marshall Clowac868372013-10-05 21:18:32 +00004479inline namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004480{
4481 inline namespace string_literals
4482 {
Howard Hinnant5c167562013-08-07 19:39:48 +00004483 inline _LIBCPP_INLINE_VISIBILITY
4484 basic_string<char> operator "" s( const char *__str, size_t __len )
4485 {
4486 return basic_string<char> (__str, __len);
4487 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004488
Louis Dionne89258142021-08-23 15:32:36 -04004489#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnant5c167562013-08-07 19:39:48 +00004490 inline _LIBCPP_INLINE_VISIBILITY
4491 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4492 {
4493 return basic_string<wchar_t> (__str, __len);
4494 }
Louis Dionne89258142021-08-23 15:32:36 -04004495#endif
Marshall Clowcba751f2013-07-23 17:05:24 +00004496
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -04004497#ifndef _LIBCPP_HAS_NO_CHAR8_T
Marshall Clow8732fed2018-12-11 04:35:44 +00004498 inline _LIBCPP_INLINE_VISIBILITY
4499 basic_string<char8_t> operator "" s(const char8_t *__str, size_t __len) _NOEXCEPT
4500 {
4501 return basic_string<char8_t> (__str, __len);
4502 }
4503#endif
4504
Howard Hinnant5c167562013-08-07 19:39:48 +00004505 inline _LIBCPP_INLINE_VISIBILITY
4506 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4507 {
4508 return basic_string<char16_t> (__str, __len);
4509 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004510
Howard Hinnant5c167562013-08-07 19:39:48 +00004511 inline _LIBCPP_INLINE_VISIBILITY
4512 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4513 {
4514 return basic_string<char32_t> (__str, __len);
4515 }
Nikolas Klauserd26407a2021-12-02 14:12:51 +01004516 } // namespace string_literals
4517} // namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004518#endif
4519
Howard Hinnantc51e1022010-05-11 19:42:16 +00004520_LIBCPP_END_NAMESPACE_STD
4521
Eric Fiselierf4433a32017-05-31 22:07:49 +00004522_LIBCPP_POP_MACROS
4523
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004524#endif // _LIBCPP_STRING