blob: fa42edde0aa150ddadadfabd899544882c2724d7 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
Louis Dionne9bd93882021-11-17 16:25:01 -05002//===----------------------------------------------------------------------===//
Howard Hinnantc51e1022010-05-11 19:42:16 +00003//
Chandler Carruth7642bb12019-01-19 08:50:56 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_STRING
11#define _LIBCPP_STRING
12
13/*
14 string synopsis
15
16namespace std
17{
18
19template <class stateT>
20class fpos
21{
22private:
23 stateT st;
24public:
25 fpos(streamoff = streamoff());
26
27 operator streamoff() const;
28
29 stateT state() const;
30 void state(stateT);
31
32 fpos& operator+=(streamoff);
33 fpos operator+ (streamoff) const;
34 fpos& operator-=(streamoff);
35 fpos operator- (streamoff) const;
36};
37
38template <class stateT> streamoff operator-(const fpos<stateT>& x, const fpos<stateT>& y);
39
40template <class stateT> bool operator==(const fpos<stateT>& x, const fpos<stateT>& y);
41template <class stateT> bool operator!=(const fpos<stateT>& x, const fpos<stateT>& y);
42
43template <class charT>
44struct char_traits
45{
46 typedef charT char_type;
47 typedef ... int_type;
48 typedef streamoff off_type;
49 typedef streampos pos_type;
50 typedef mbstate_t state_type;
51
Howard Hinnantaeb17d82011-05-29 19:57:12 +000052 static void assign(char_type& c1, const char_type& c2) noexcept;
Howard Hinnant270c00e2012-07-20 19:09:12 +000053 static constexpr bool eq(char_type c1, char_type c2) noexcept;
54 static constexpr bool lt(char_type c1, char_type c2) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000055
56 static int compare(const char_type* s1, const char_type* s2, size_t n);
57 static size_t length(const char_type* s);
58 static const char_type* find(const char_type* s, size_t n, const char_type& a);
59 static char_type* move(char_type* s1, const char_type* s2, size_t n);
60 static char_type* copy(char_type* s1, const char_type* s2, size_t n);
61 static char_type* assign(char_type* s, size_t n, char_type a);
62
Howard Hinnant270c00e2012-07-20 19:09:12 +000063 static constexpr int_type not_eof(int_type c) noexcept;
64 static constexpr char_type to_char_type(int_type c) noexcept;
65 static constexpr int_type to_int_type(char_type c) noexcept;
66 static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept;
67 static constexpr int_type eof() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000068};
69
70template <> struct char_traits<char>;
71template <> struct char_traits<wchar_t>;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +010072template <> struct char_traits<char8_t>; // C++20
73template <> struct char_traits<char16_t>;
74template <> struct char_traits<char32_t>;
Howard Hinnantc51e1022010-05-11 19:42:16 +000075
Howard Hinnant3b6579a2010-08-22 00:02:43 +000076template<class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
Howard Hinnantc51e1022010-05-11 19:42:16 +000077class basic_string
78{
Howard Hinnant3b6579a2010-08-22 00:02:43 +000079public:
80// types:
Howard Hinnantc51e1022010-05-11 19:42:16 +000081 typedef traits traits_type;
82 typedef typename traits_type::char_type value_type;
83 typedef Allocator allocator_type;
84 typedef typename allocator_type::size_type size_type;
85 typedef typename allocator_type::difference_type difference_type;
86 typedef typename allocator_type::reference reference;
87 typedef typename allocator_type::const_reference const_reference;
88 typedef typename allocator_type::pointer pointer;
89 typedef typename allocator_type::const_pointer const_pointer;
90 typedef implementation-defined iterator;
91 typedef implementation-defined const_iterator;
92 typedef std::reverse_iterator<iterator> reverse_iterator;
93 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
94
95 static const size_type npos = -1;
96
Howard Hinnant3e276872011-06-03 18:40:47 +000097 basic_string()
98 noexcept(is_nothrow_default_constructible<allocator_type>::value);
99 explicit basic_string(const allocator_type& a);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000100 basic_string(const basic_string& str);
Howard Hinnant3e276872011-06-03 18:40:47 +0000101 basic_string(basic_string&& str)
102 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clow83445802016-04-07 18:13:41 +0000103 basic_string(const basic_string& str, size_type pos,
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000104 const allocator_type& a = allocator_type());
Marshall Clow83445802016-04-07 18:13:41 +0000105 basic_string(const basic_string& str, size_type pos, size_type n,
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000106 const Allocator& a = Allocator());
Marshall Clow78dbe462016-11-14 18:22:19 +0000107 template<class T>
108 basic_string(const T& t, size_type pos, size_type n, const Allocator& a = Allocator()); // C++17
Marshall Clowe46031a2018-07-02 18:41:15 +0000109 template <class T>
110 explicit basic_string(const T& t, const Allocator& a = Allocator()); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000111 basic_string(const value_type* s, const allocator_type& a = allocator_type());
112 basic_string(const value_type* s, size_type n, const allocator_type& a = allocator_type());
Marek Kurdej90d79712021-07-27 16:16:21 +0200113 basic_string(nullptr_t) = delete; // C++2b
Howard Hinnantc51e1022010-05-11 19:42:16 +0000114 basic_string(size_type n, value_type c, const allocator_type& a = allocator_type());
115 template<class InputIterator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000116 basic_string(InputIterator begin, InputIterator end,
117 const allocator_type& a = allocator_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000118 basic_string(initializer_list<value_type>, const Allocator& = Allocator());
119 basic_string(const basic_string&, const Allocator&);
120 basic_string(basic_string&&, const Allocator&);
121
122 ~basic_string();
123
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000124 operator basic_string_view<charT, traits>() const noexcept;
125
Howard Hinnantc51e1022010-05-11 19:42:16 +0000126 basic_string& operator=(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000127 template <class T>
128 basic_string& operator=(const T& t); // C++17
Howard Hinnant3e276872011-06-03 18:40:47 +0000129 basic_string& operator=(basic_string&& str)
130 noexcept(
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000131 allocator_type::propagate_on_container_move_assignment::value ||
132 allocator_type::is_always_equal::value ); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000133 basic_string& operator=(const value_type* s);
Marek Kurdej90d79712021-07-27 16:16:21 +0200134 basic_string& operator=(nullptr_t) = delete; // C++2b
Howard Hinnantc51e1022010-05-11 19:42:16 +0000135 basic_string& operator=(value_type c);
136 basic_string& operator=(initializer_list<value_type>);
137
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000138 iterator begin() noexcept;
139 const_iterator begin() const noexcept;
140 iterator end() noexcept;
141 const_iterator end() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000142
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000143 reverse_iterator rbegin() noexcept;
144 const_reverse_iterator rbegin() const noexcept;
145 reverse_iterator rend() noexcept;
146 const_reverse_iterator rend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000147
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000148 const_iterator cbegin() const noexcept;
149 const_iterator cend() const noexcept;
150 const_reverse_iterator crbegin() const noexcept;
151 const_reverse_iterator crend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000152
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000153 size_type size() const noexcept;
154 size_type length() const noexcept;
155 size_type max_size() const noexcept;
156 size_type capacity() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000157
158 void resize(size_type n, value_type c);
159 void resize(size_type n);
160
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100161 template<class Operation>
162 constexpr void resize_and_overwrite(size_type n, Operation op); // since C++23
163
Marek Kurdejc9848142020-11-26 10:07:16 +0100164 void reserve(size_type res_arg);
165 void reserve(); // deprecated in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000166 void shrink_to_fit();
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000167 void clear() noexcept;
168 bool empty() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000169
170 const_reference operator[](size_type pos) const;
171 reference operator[](size_type pos);
172
173 const_reference at(size_type n) const;
174 reference at(size_type n);
175
176 basic_string& operator+=(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000177 template <class T>
178 basic_string& operator+=(const T& t); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000179 basic_string& operator+=(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000180 basic_string& operator+=(value_type c);
181 basic_string& operator+=(initializer_list<value_type>);
182
183 basic_string& append(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000184 template <class T>
185 basic_string& append(const T& t); // C++17
Marshall Clow8db7fb02014-03-04 19:17:19 +0000186 basic_string& append(const basic_string& str, size_type pos, size_type n=npos); //C++14
Marshall Clow82513342016-09-24 22:45:42 +0000187 template <class T>
188 basic_string& append(const T& t, size_type pos, size_type n=npos); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000189 basic_string& append(const value_type* s, size_type n);
190 basic_string& append(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000191 basic_string& append(size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000192 template<class InputIterator>
193 basic_string& append(InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000194 basic_string& append(initializer_list<value_type>);
195
196 void push_back(value_type c);
197 void pop_back();
198 reference front();
199 const_reference front() const;
200 reference back();
201 const_reference back() const;
202
203 basic_string& assign(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000204 template <class T>
205 basic_string& assign(const T& t); // C++17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000206 basic_string& assign(basic_string&& str);
Marshall Clow8db7fb02014-03-04 19:17:19 +0000207 basic_string& assign(const basic_string& str, size_type pos, size_type n=npos); // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000208 template <class T>
209 basic_string& assign(const T& t, size_type pos, size_type n=npos); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000210 basic_string& assign(const value_type* s, size_type n);
211 basic_string& assign(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000212 basic_string& assign(size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000213 template<class InputIterator>
214 basic_string& assign(InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000215 basic_string& assign(initializer_list<value_type>);
216
217 basic_string& insert(size_type pos1, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000218 template <class T>
219 basic_string& insert(size_type pos1, const T& t);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000220 basic_string& insert(size_type pos1, const basic_string& str,
221 size_type pos2, size_type n);
Marshall Clow82513342016-09-24 22:45:42 +0000222 template <class T>
223 basic_string& insert(size_type pos1, const T& t, size_type pos2, size_type n); // C++17
Marshall Clow8db7fb02014-03-04 19:17:19 +0000224 basic_string& insert(size_type pos, const value_type* s, size_type n=npos); //C++14
Howard Hinnantd17880b2013-06-28 16:59:19 +0000225 basic_string& insert(size_type pos, const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000226 basic_string& insert(size_type pos, size_type n, value_type c);
227 iterator insert(const_iterator p, value_type c);
228 iterator insert(const_iterator p, size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000229 template<class InputIterator>
230 iterator insert(const_iterator p, InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000231 iterator insert(const_iterator p, initializer_list<value_type>);
232
233 basic_string& erase(size_type pos = 0, size_type n = npos);
234 iterator erase(const_iterator position);
235 iterator erase(const_iterator first, const_iterator last);
236
237 basic_string& replace(size_type pos1, size_type n1, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000238 template <class T>
239 basic_string& replace(size_type pos1, size_type n1, const T& t); // C++17
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000240 basic_string& replace(size_type pos1, size_type n1, const basic_string& str,
Marshall Clow8db7fb02014-03-04 19:17:19 +0000241 size_type pos2, size_type n2=npos); // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000242 template <class T>
243 basic_string& replace(size_type pos1, size_type n1, const T& t,
244 size_type pos2, size_type n); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000245 basic_string& replace(size_type pos, size_type n1, const value_type* s, size_type n2);
246 basic_string& replace(size_type pos, size_type n1, const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000247 basic_string& replace(size_type pos, size_type n1, size_type n2, value_type c);
Howard Hinnant990d6e82010-11-17 21:11:40 +0000248 basic_string& replace(const_iterator i1, const_iterator i2, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000249 template <class T>
250 basic_string& replace(const_iterator i1, const_iterator i2, const T& t); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000251 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s, size_type n);
252 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s);
Howard Hinnant990d6e82010-11-17 21:11:40 +0000253 basic_string& replace(const_iterator i1, const_iterator i2, size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000254 template<class InputIterator>
Howard Hinnant990d6e82010-11-17 21:11:40 +0000255 basic_string& replace(const_iterator i1, const_iterator i2, InputIterator j1, InputIterator j2);
256 basic_string& replace(const_iterator i1, const_iterator i2, initializer_list<value_type>);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000257
Howard Hinnantd17880b2013-06-28 16:59:19 +0000258 size_type copy(value_type* s, size_type n, size_type pos = 0) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000259 basic_string substr(size_type pos = 0, size_type n = npos) const;
260
Howard Hinnant3e276872011-06-03 18:40:47 +0000261 void swap(basic_string& str)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000262 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
263 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000264
Howard Hinnantd17880b2013-06-28 16:59:19 +0000265 const value_type* c_str() const noexcept;
266 const value_type* data() const noexcept;
Marshall Clowd3c22392016-03-08 15:44:30 +0000267 value_type* data() noexcept; // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000268
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000269 allocator_type get_allocator() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000270
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000271 size_type find(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000272 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800273 size_type find(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000274 size_type find(const value_type* s, size_type pos, size_type n) const noexcept;
275 size_type find(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000276 size_type find(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000277
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000278 size_type rfind(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000279 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800280 size_type rfind(const T& t, size_type pos = npos) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000281 size_type rfind(const value_type* s, size_type pos, size_type n) const noexcept;
282 size_type rfind(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000283 size_type rfind(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000284
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000285 size_type find_first_of(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000286 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800287 size_type find_first_of(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000288 size_type find_first_of(const value_type* s, size_type pos, size_type n) const noexcept;
289 size_type find_first_of(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000290 size_type find_first_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000291
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000292 size_type find_last_of(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000293 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800294 size_type find_last_of(const T& t, size_type pos = npos) const noexcept noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000295 size_type find_last_of(const value_type* s, size_type pos, size_type n) const noexcept;
296 size_type find_last_of(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000297 size_type find_last_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000298
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000299 size_type find_first_not_of(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000300 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800301 size_type find_first_not_of(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000302 size_type find_first_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
303 size_type find_first_not_of(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000304 size_type find_first_not_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000305
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000306 size_type find_last_not_of(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000307 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800308 size_type find_last_not_of(const T& t, size_type pos = npos) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000309 size_type find_last_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
310 size_type find_last_not_of(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000311 size_type find_last_not_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000312
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000313 int compare(const basic_string& str) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000314 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800315 int compare(const T& t) const noexcept; // C++17, noexcept as an extension
Howard Hinnantc51e1022010-05-11 19:42:16 +0000316 int compare(size_type pos1, size_type n1, const basic_string& str) const;
Marshall Clowe46031a2018-07-02 18:41:15 +0000317 template <class T>
318 int compare(size_type pos1, size_type n1, const T& t) const; // C++17
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000319 int compare(size_type pos1, size_type n1, const basic_string& str,
Marshall Clow8db7fb02014-03-04 19:17:19 +0000320 size_type pos2, size_type n2=npos) const; // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000321 template <class T>
322 int compare(size_type pos1, size_type n1, const T& t,
323 size_type pos2, size_type n2=npos) const; // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000324 int compare(const value_type* s) const noexcept;
325 int compare(size_type pos1, size_type n1, const value_type* s) const;
326 int compare(size_type pos1, size_type n1, const value_type* s, size_type n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000327
Marek Kurdej24b4c512021-01-07 12:29:04 +0100328 bool starts_with(basic_string_view<charT, traits> sv) const noexcept; // C++20
329 bool starts_with(charT c) const noexcept; // C++20
330 bool starts_with(const charT* s) const; // C++20
331 bool ends_with(basic_string_view<charT, traits> sv) const noexcept; // C++20
332 bool ends_with(charT c) const noexcept; // C++20
333 bool ends_with(const charT* s) const; // C++20
Marshall Clow18c293b2017-12-04 20:11:38 +0000334
Wim Leflere023c3542021-01-19 14:33:30 -0500335 constexpr bool contains(basic_string_view<charT, traits> sv) const noexcept; // C++2b
336 constexpr bool contains(charT c) const noexcept; // C++2b
337 constexpr bool contains(const charT* s) const; // C++2b
338
Howard Hinnantc51e1022010-05-11 19:42:16 +0000339 bool __invariants() const;
340};
341
Marshall Clowa0563332018-02-08 06:34:03 +0000342template<class InputIterator,
343 class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
344basic_string(InputIterator, InputIterator, Allocator = Allocator())
345 -> basic_string<typename iterator_traits<InputIterator>::value_type,
346 char_traits<typename iterator_traits<InputIterator>::value_type>,
347 Allocator>; // C++17
348
Howard Hinnantc51e1022010-05-11 19:42:16 +0000349template<class charT, class traits, class Allocator>
350basic_string<charT, traits, Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000351operator+(const basic_string<charT, traits, Allocator>& lhs,
352 const basic_string<charT, traits, Allocator>& rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000353
354template<class charT, class traits, class Allocator>
355basic_string<charT, traits, Allocator>
356operator+(const charT* lhs , const basic_string<charT,traits,Allocator>&rhs);
357
358template<class charT, class traits, class Allocator>
359basic_string<charT, traits, Allocator>
360operator+(charT lhs, const basic_string<charT,traits,Allocator>& rhs);
361
362template<class charT, class traits, class Allocator>
363basic_string<charT, traits, Allocator>
364operator+(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs);
365
366template<class charT, class traits, class Allocator>
367basic_string<charT, traits, Allocator>
368operator+(const basic_string<charT, traits, Allocator>& lhs, charT rhs);
369
370template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000371bool operator==(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000372 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000373
374template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000375bool operator==(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000376
377template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000378bool operator==(const basic_string<charT,traits,Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000379
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000380template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000381bool operator!=(const basic_string<charT,traits,Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000382 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000383
384template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000385bool operator!=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000386
387template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000388bool operator!=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000389
390template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000391bool operator< (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000392 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000393
394template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000395bool operator< (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000396
397template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000398bool operator< (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000399
400template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000401bool operator> (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000402 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000403
404template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000405bool operator> (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000406
407template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000408bool operator> (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000409
410template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000411bool operator<=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000412 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000413
414template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000415bool operator<=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000416
417template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000418bool operator<=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000419
420template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000421bool operator>=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000422 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000423
424template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000425bool operator>=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000426
427template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000428bool operator>=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000429
430template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000431void swap(basic_string<charT, traits, Allocator>& lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +0000432 basic_string<charT, traits, Allocator>& rhs)
433 noexcept(noexcept(lhs.swap(rhs)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000434
435template<class charT, class traits, class Allocator>
436basic_istream<charT, traits>&
437operator>>(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
438
439template<class charT, class traits, class Allocator>
440basic_ostream<charT, traits>&
441operator<<(basic_ostream<charT, traits>& os, const basic_string<charT, traits, Allocator>& str);
442
443template<class charT, class traits, class Allocator>
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000444basic_istream<charT, traits>&
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000445getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str,
446 charT delim);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000447
448template<class charT, class traits, class Allocator>
449basic_istream<charT, traits>&
450getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
451
Marshall Clow29b53f22018-12-14 18:49:35 +0000452template<class charT, class traits, class Allocator, class U>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200453typename basic_string<charT, traits, Allocator>::size_type
454erase(basic_string<charT, traits, Allocator>& c, const U& value); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000455template<class charT, class traits, class Allocator, class Predicate>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200456typename basic_string<charT, traits, Allocator>::size_type
457erase_if(basic_string<charT, traits, Allocator>& c, Predicate pred); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000458
Howard Hinnantc51e1022010-05-11 19:42:16 +0000459typedef basic_string<char> string;
460typedef basic_string<wchar_t> wstring;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100461typedef basic_string<char8_t> u8string; // C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000462typedef basic_string<char16_t> u16string;
463typedef basic_string<char32_t> u32string;
464
Bruce Mitchener170d8972020-11-24 12:53:53 -0500465int stoi (const string& str, size_t* idx = nullptr, int base = 10);
466long stol (const string& str, size_t* idx = nullptr, int base = 10);
467unsigned long stoul (const string& str, size_t* idx = nullptr, int base = 10);
468long long stoll (const string& str, size_t* idx = nullptr, int base = 10);
469unsigned long long stoull(const string& str, size_t* idx = nullptr, int base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000470
Bruce Mitchener170d8972020-11-24 12:53:53 -0500471float stof (const string& str, size_t* idx = nullptr);
472double stod (const string& str, size_t* idx = nullptr);
473long double stold(const string& str, size_t* idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000474
475string to_string(int val);
476string to_string(unsigned val);
477string to_string(long val);
478string to_string(unsigned long val);
479string to_string(long long val);
480string to_string(unsigned long long val);
481string to_string(float val);
482string to_string(double val);
483string to_string(long double val);
484
Bruce Mitchener170d8972020-11-24 12:53:53 -0500485int stoi (const wstring& str, size_t* idx = nullptr, int base = 10);
486long stol (const wstring& str, size_t* idx = nullptr, int base = 10);
487unsigned long stoul (const wstring& str, size_t* idx = nullptr, int base = 10);
488long long stoll (const wstring& str, size_t* idx = nullptr, int base = 10);
489unsigned long long stoull(const wstring& str, size_t* idx = nullptr, int base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000490
Bruce Mitchener170d8972020-11-24 12:53:53 -0500491float stof (const wstring& str, size_t* idx = nullptr);
492double stod (const wstring& str, size_t* idx = nullptr);
493long double stold(const wstring& str, size_t* idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000494
495wstring to_wstring(int val);
496wstring to_wstring(unsigned val);
497wstring to_wstring(long val);
498wstring to_wstring(unsigned long val);
499wstring to_wstring(long long val);
500wstring to_wstring(unsigned long long val);
501wstring to_wstring(float val);
502wstring to_wstring(double val);
503wstring to_wstring(long double val);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000504
505template <> struct hash<string>;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100506template <> struct hash<u8string>; // C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000507template <> struct hash<u16string>;
508template <> struct hash<u32string>;
509template <> struct hash<wstring>;
510
Marshall Clowcba751f2013-07-23 17:05:24 +0000511basic_string<char> operator "" s( const char *str, size_t len ); // C++14
512basic_string<wchar_t> operator "" s( const wchar_t *str, size_t len ); // C++14
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100513basic_string<char8_t> operator "" s( const char8_t *str, size_t len ); // C++20
Marshall Clowcba751f2013-07-23 17:05:24 +0000514basic_string<char16_t> operator "" s( const char16_t *str, size_t len ); // C++14
515basic_string<char32_t> operator "" s( const char32_t *str, size_t len ); // C++14
516
Howard Hinnantc51e1022010-05-11 19:42:16 +0000517} // std
518
519*/
520
521#include <__config>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400522#include <__debug>
Nikolas Klauser80840272022-02-04 13:04:33 +0100523#include <__ios/fpos.h>
Louis Dionne77249522021-06-11 09:55:11 -0400524#include <__iterator/wrap_iter.h>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000525#include <algorithm>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400526#include <compare>
527#include <cstdio> // EOF
Louis Dionned24191c2021-08-19 12:39:16 -0400528#include <cstdlib>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400529#include <cstring>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400530#include <initializer_list>
531#include <iosfwd>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000532#include <iterator>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000533#include <memory>
534#include <stdexcept>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400535#include <string_view>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000536#include <type_traits>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400537#include <utility>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000538#include <version>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000539
Nikolas Klauser4c1bf592022-02-11 19:15:18 +0100540// TODO: remove these headers
541#include <__functional/binary_function.h>
542#include <__functional/invoke.h>
543#include <__functional/operations.h>
544#include <__functional/reference_wrapper.h>
545#include <__functional/unary_function.h>
546#include <__functional/weak_result_type.h>
547#include <new>
548#include <typeinfo>
549
Louis Dionne89258142021-08-23 15:32:36 -0400550#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Nikolas Klauser80840272022-02-04 13:04:33 +0100551# include <cwchar>
Louis Dionne89258142021-08-23 15:32:36 -0400552#endif
553
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400554#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
Nikolas Klauser80840272022-02-04 13:04:33 +0100555# include <cstdint>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400556#endif
Eric Fiselier14b6de92014-08-10 23:53:08 +0000557
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000558#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Arthur O'Dwyer6eeaa002022-02-01 20:16:40 -0500559# pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000560#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000561
Eric Fiselierf4433a32017-05-31 22:07:49 +0000562_LIBCPP_PUSH_MACROS
563#include <__undef_macros>
564
565
Howard Hinnantc51e1022010-05-11 19:42:16 +0000566_LIBCPP_BEGIN_NAMESPACE_STD
567
Howard Hinnantc51e1022010-05-11 19:42:16 +0000568// basic_string
569
570template<class _CharT, class _Traits, class _Allocator>
571basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000572operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
573 const basic_string<_CharT, _Traits, _Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000574
575template<class _CharT, class _Traits, class _Allocator>
576basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000577operator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000578
579template<class _CharT, class _Traits, class _Allocator>
580basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000581operator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000582
583template<class _CharT, class _Traits, class _Allocator>
Louis Dionne8f8d95d2018-11-21 17:31:55 +0000584inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000585basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000586operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000587
588template<class _CharT, class _Traits, class _Allocator>
589basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000590operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000591
Shoaib Meenaiea363712017-07-29 02:54:41 +0000592_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&))
593
Marshall Clow039b2f02016-01-13 21:54:34 +0000594template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400595struct __string_is_trivial_iterator : public false_type {};
596
597template <class _Tp>
598struct __string_is_trivial_iterator<_Tp*>
599 : public is_arithmetic<_Tp> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000600
Louis Dionne173f29e2019-05-29 16:01:36 +0000601template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400602struct __string_is_trivial_iterator<__wrap_iter<_Iter> >
603 : public __string_is_trivial_iterator<_Iter> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000604
Marshall Clow82513342016-09-24 22:45:42 +0000605template <class _CharT, class _Traits, class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500606struct __can_be_converted_to_string_view : public _BoolConstant<
607 is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
608 !is_convertible<const _Tp&, const _CharT*>::value
609 > {};
Marshall Clow82513342016-09-24 22:45:42 +0000610
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000611#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000612
613template <class _CharT, size_t = sizeof(_CharT)>
614struct __padding
615{
616 unsigned char __xx[sizeof(_CharT)-1];
617};
618
619template <class _CharT>
620struct __padding<_CharT, 1>
621{
622};
623
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400624#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000625
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400626#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800627typedef basic_string<char8_t> u8string;
628#endif
629
630#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
631typedef basic_string<char16_t> u16string;
632typedef basic_string<char32_t> u32string;
Louis Dionne96fc5f52021-09-09 11:25:10 -0400633#endif
Richard Smith256954d2020-11-11 17:12:18 -0800634
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000635template<class _CharT, class _Traits, class _Allocator>
Richard Smith256954d2020-11-11 17:12:18 -0800636class
637 _LIBCPP_TEMPLATE_VIS
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400638#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800639 _LIBCPP_PREFERRED_NAME(u8string)
640#endif
641#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
642 _LIBCPP_PREFERRED_NAME(u16string)
643 _LIBCPP_PREFERRED_NAME(u32string)
644#endif
645 basic_string
Howard Hinnantc51e1022010-05-11 19:42:16 +0000646{
647public:
648 typedef basic_string __self;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000649 typedef basic_string_view<_CharT, _Traits> __self_view;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000650 typedef _Traits traits_type;
Marshall Clowa3a74e02017-03-15 18:41:11 +0000651 typedef _CharT value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000652 typedef _Allocator allocator_type;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000653 typedef allocator_traits<allocator_type> __alloc_traits;
654 typedef typename __alloc_traits::size_type size_type;
655 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant3e276872011-06-03 18:40:47 +0000656 typedef value_type& reference;
657 typedef const value_type& const_reference;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000658 typedef typename __alloc_traits::pointer pointer;
659 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000660
Marshall Clow79f33542018-03-21 00:36:05 +0000661 static_assert((!is_array<value_type>::value), "Character type of basic_string must not be an array");
662 static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string must be standard-layout");
663 static_assert(( is_trivial<value_type>::value), "Character type of basic_string must be trivial");
664 static_assert(( is_same<_CharT, typename traits_type::char_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000665 "traits_type::char_type must be the same type as CharT");
Marshall Clow79f33542018-03-21 00:36:05 +0000666 static_assert(( is_same<typename allocator_type::value_type, value_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000667 "Allocator::value_type must be same type as value_type");
Marshall Clowe46031a2018-07-02 18:41:15 +0000668
Howard Hinnantc51e1022010-05-11 19:42:16 +0000669 typedef __wrap_iter<pointer> iterator;
670 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000671 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
672 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000673
674private:
Howard Hinnant68bf1812013-04-30 21:44:48 +0000675
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000676#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000677
678 struct __long
679 {
680 pointer __data_;
681 size_type __size_;
682 size_type __cap_;
683 };
684
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000685#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000686 static const size_type __short_mask = 0x01;
687 static const size_type __long_mask = 0x1ul;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000688#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000689 static const size_type __short_mask = 0x80;
690 static const size_type __long_mask = ~(size_type(~0) >> 1);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400691#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +0000692
693 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
694 (sizeof(__long) - 1)/sizeof(value_type) : 2};
695
696 struct __short
697 {
698 value_type __data_[__min_cap];
699 struct
700 : __padding<value_type>
701 {
702 unsigned char __size_;
703 };
704 };
705
706#else
707
Howard Hinnantc51e1022010-05-11 19:42:16 +0000708 struct __long
709 {
710 size_type __cap_;
711 size_type __size_;
712 pointer __data_;
713 };
714
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000715#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000716 static const size_type __short_mask = 0x80;
717 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000718#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000719 static const size_type __short_mask = 0x01;
720 static const size_type __long_mask = 0x1ul;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400721#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +0000722
Howard Hinnantc51e1022010-05-11 19:42:16 +0000723 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
724 (sizeof(__long) - 1)/sizeof(value_type) : 2};
725
726 struct __short
727 {
728 union
729 {
730 unsigned char __size_;
Howard Hinnant49e145e2012-10-30 19:06:59 +0000731 value_type __lx;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000732 };
733 value_type __data_[__min_cap];
734 };
735
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400736#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000737
Howard Hinnant8ea98242013-08-23 17:37:05 +0000738 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000739
Howard Hinnant8ea98242013-08-23 17:37:05 +0000740 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000741
742 struct __raw
743 {
744 size_type __words[__n_words];
745 };
746
747 struct __rep
748 {
749 union
750 {
751 __long __l;
752 __short __s;
753 __raw __r;
754 };
755 };
756
757 __compressed_pair<__rep, allocator_type> __r_;
758
Howard Hinnantc51e1022010-05-11 19:42:16 +0000759public:
Martin Storsjö88890c22021-02-22 01:13:13 +0200760 _LIBCPP_TEMPLATE_DATA_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000761 static const size_type npos = -1;
762
Howard Hinnant3e276872011-06-03 18:40:47 +0000763 _LIBCPP_INLINE_VISIBILITY basic_string()
764 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000765
766 _LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a)
767#if _LIBCPP_STD_VER <= 14
768 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
769#else
770 _NOEXCEPT;
771#endif
772
Howard Hinnantc51e1022010-05-11 19:42:16 +0000773 basic_string(const basic_string& __str);
774 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clowa80abc72015-06-03 19:56:43 +0000775
Eric Fiselierfc92be82017-04-19 00:28:44 +0000776#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000777 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000778 basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +0000779#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +0000780 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000781#else
782 _NOEXCEPT;
783#endif
784
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000785 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000786 basic_string(basic_string&& __str, const allocator_type& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400787#endif // _LIBCPP_CXX03_LANG
Marshall Clowe46031a2018-07-02 18:41:15 +0000788
Louis Dionne9ce598d2021-09-08 09:14:43 -0400789 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Eric Fiseliera8567862018-07-17 05:48:48 +0000790 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5169d1c2019-12-16 19:03:23 -0500791 basic_string(const _CharT* __s) : __r_(__default_init_tag(), __default_init_tag()) {
Eric Fiseliera8567862018-07-17 05:48:48 +0000792 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
793 __init(__s, traits_type::length(__s));
Nikolas Klauserf2807732022-01-11 00:33:35 +0100794 _VSTD::__debug_db_insert_c(this);
Eric Fiseliera8567862018-07-17 05:48:48 +0000795 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000796
Louis Dionne9ce598d2021-09-08 09:14:43 -0400797 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000798 _LIBCPP_INLINE_VISIBILITY
799 basic_string(const _CharT* __s, const _Allocator& __a);
800
Marek Kurdej90d79712021-07-27 16:16:21 +0200801#if _LIBCPP_STD_VER > 20
802 basic_string(nullptr_t) = delete;
803#endif
804
Howard Hinnantcf823322010-12-17 14:46:43 +0000805 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000806 basic_string(const _CharT* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +0000807 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000808 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000809 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000810 basic_string(size_type __n, _CharT __c);
Marshall Clowe46031a2018-07-02 18:41:15 +0000811
Louis Dionne9ce598d2021-09-08 09:14:43 -0400812 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000813 _LIBCPP_INLINE_VISIBILITY
814 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
815
Marshall Clow83445802016-04-07 18:13:41 +0000816 basic_string(const basic_string& __str, size_type __pos, size_type __n,
Eric Fiselier812882b2017-02-17 01:17:10 +0000817 const _Allocator& __a = _Allocator());
Marshall Clow83445802016-04-07 18:13:41 +0000818 _LIBCPP_INLINE_VISIBILITY
819 basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +0000820 const _Allocator& __a = _Allocator());
Marshall Clowe46031a2018-07-02 18:41:15 +0000821
Louis Dionne9ce598d2021-09-08 09:14:43 -0400822 template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value> >
Shoaib Meenai69c57412017-03-02 03:02:50 +0000823 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselier812882b2017-02-17 01:17:10 +0000824 basic_string(const _Tp& __t, size_type __pos, size_type __n,
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500825 const allocator_type& __a = allocator_type());
Marshall Clowe46031a2018-07-02 18:41:15 +0000826
Louis Dionne9ce598d2021-09-08 09:14:43 -0400827 template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500828 !__is_same_uncvref<_Tp, basic_string>::value> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000829 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
830 explicit basic_string(const _Tp& __t);
831
Louis Dionne9ce598d2021-09-08 09:14:43 -0400832 template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000833 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
834 explicit basic_string(const _Tp& __t, const allocator_type& __a);
835
Louis Dionne9ce598d2021-09-08 09:14:43 -0400836 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000837 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000838 basic_string(_InputIterator __first, _InputIterator __last);
Louis Dionne9ce598d2021-09-08 09:14:43 -0400839 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000840 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000841 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000842#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000843 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000844 basic_string(initializer_list<_CharT> __il);
Howard Hinnantcf823322010-12-17 14:46:43 +0000845 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000846 basic_string(initializer_list<_CharT> __il, const _Allocator& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400847#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000848
Eric Fiselierd9a702a2016-10-31 03:42:50 +0000849 inline ~basic_string();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000850
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000851 _LIBCPP_INLINE_VISIBILITY
852 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
853
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000854 basic_string& operator=(const basic_string& __str);
Eric Fiselier5ec13b12017-01-23 21:24:58 +0000855
Louis Dionne9ce598d2021-09-08 09:14:43 -0400856 template <class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000857 basic_string& operator=(const _Tp& __t)
858 {__self_view __sv = __t; return assign(__sv);}
859
Eric Fiselierfc92be82017-04-19 00:28:44 +0000860#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000861 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000862 basic_string& operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000863 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiselierfc92be82017-04-19 00:28:44 +0000864 _LIBCPP_INLINE_VISIBILITY
865 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000866#endif
Howard Hinnantd17880b2013-06-28 16:59:19 +0000867 _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const value_type* __s) {return assign(__s);}
Marek Kurdej90d79712021-07-27 16:16:21 +0200868#if _LIBCPP_STD_VER > 20
869 basic_string& operator=(nullptr_t) = delete;
870#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000871 basic_string& operator=(value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000872
Louis Dionneba400782020-10-02 15:02:52 -0400873#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +0000874 _LIBCPP_INLINE_VISIBILITY
875 iterator begin() _NOEXCEPT
876 {return iterator(this, __get_pointer());}
877 _LIBCPP_INLINE_VISIBILITY
878 const_iterator begin() const _NOEXCEPT
879 {return const_iterator(this, __get_pointer());}
880 _LIBCPP_INLINE_VISIBILITY
881 iterator end() _NOEXCEPT
882 {return iterator(this, __get_pointer() + size());}
883 _LIBCPP_INLINE_VISIBILITY
884 const_iterator end() const _NOEXCEPT
885 {return const_iterator(this, __get_pointer() + size());}
886#else
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000887 _LIBCPP_INLINE_VISIBILITY
888 iterator begin() _NOEXCEPT
889 {return iterator(__get_pointer());}
890 _LIBCPP_INLINE_VISIBILITY
891 const_iterator begin() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000892 {return const_iterator(__get_pointer());}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000893 _LIBCPP_INLINE_VISIBILITY
894 iterator end() _NOEXCEPT
895 {return iterator(__get_pointer() + size());}
896 _LIBCPP_INLINE_VISIBILITY
897 const_iterator end() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000898 {return const_iterator(__get_pointer() + size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400899#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000900 _LIBCPP_INLINE_VISIBILITY
901 reverse_iterator rbegin() _NOEXCEPT
902 {return reverse_iterator(end());}
903 _LIBCPP_INLINE_VISIBILITY
904 const_reverse_iterator rbegin() const _NOEXCEPT
905 {return const_reverse_iterator(end());}
906 _LIBCPP_INLINE_VISIBILITY
907 reverse_iterator rend() _NOEXCEPT
908 {return reverse_iterator(begin());}
909 _LIBCPP_INLINE_VISIBILITY
910 const_reverse_iterator rend() const _NOEXCEPT
911 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000912
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000913 _LIBCPP_INLINE_VISIBILITY
914 const_iterator cbegin() const _NOEXCEPT
915 {return begin();}
916 _LIBCPP_INLINE_VISIBILITY
917 const_iterator cend() const _NOEXCEPT
918 {return end();}
919 _LIBCPP_INLINE_VISIBILITY
920 const_reverse_iterator crbegin() const _NOEXCEPT
921 {return rbegin();}
922 _LIBCPP_INLINE_VISIBILITY
923 const_reverse_iterator crend() const _NOEXCEPT
924 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000925
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000926 _LIBCPP_INLINE_VISIBILITY size_type size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000927 {return __is_long() ? __get_long_size() : __get_short_size();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000928 _LIBCPP_INLINE_VISIBILITY size_type length() const _NOEXCEPT {return size();}
929 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT;
930 _LIBCPP_INLINE_VISIBILITY size_type capacity() const _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +0000931 {return (__is_long() ? __get_long_cap()
932 : static_cast<size_type>(__min_cap)) - 1;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000933
934 void resize(size_type __n, value_type __c);
935 _LIBCPP_INLINE_VISIBILITY void resize(size_type __n) {resize(__n, value_type());}
936
Marek Kurdejc9848142020-11-26 10:07:16 +0100937 void reserve(size_type __requested_capacity);
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100938
939#if _LIBCPP_STD_VER > 20
940 template <class _Op>
941 _LIBCPP_HIDE_FROM_ABI constexpr
942 void resize_and_overwrite(size_type __n, _Op __op) {
943 __resize_default_init(__n);
Nikolas Klausera4a76a12022-01-20 13:53:59 +0100944 __erase_to_end(_VSTD::move(__op)(data(), _LIBCPP_AUTO_CAST(__n)));
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100945 }
946#endif
947
Eric Fiselier451d5582018-11-26 20:15:38 +0000948 _LIBCPP_INLINE_VISIBILITY void __resize_default_init(size_type __n);
949
Marek Kurdejc9848142020-11-26 10:07:16 +0100950 _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_INLINE_VISIBILITY
951 void reserve() _NOEXCEPT {shrink_to_fit();}
Marshall Clow6ae12a82018-11-28 18:18:34 +0000952 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejc9848142020-11-26 10:07:16 +0100953 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000954 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000955 void clear() _NOEXCEPT;
Marshall Clowb7db4972017-11-15 20:02:27 +0000956 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
957 bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000958
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000959 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __pos) const _NOEXCEPT;
960 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000961
962 const_reference at(size_type __n) const;
963 reference at(size_type __n);
964
965 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const basic_string& __str) {return append(__str);}
Marshall Clowe46031a2018-07-02 18:41:15 +0000966
967 template <class _Tp>
968 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -0400969 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +0000970 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500971 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
972 && !__is_same_uncvref<_Tp, basic_string >::value,
Marshall Clowe46031a2018-07-02 18:41:15 +0000973 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500974 >
Marshall Clowe46031a2018-07-02 18:41:15 +0000975 operator+=(const _Tp& __t) {__self_view __sv = __t; return append(__sv);}
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000976 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const value_type* __s) {return append(__s);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000977 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(value_type __c) {push_back(__c); return *this;}
Eric Fiselierfc92be82017-04-19 00:28:44 +0000978#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000979 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(initializer_list<value_type> __il) {return append(__il);}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400980#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000981
Howard Hinnantcf823322010-12-17 14:46:43 +0000982 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000983 basic_string& append(const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000984
985 template <class _Tp>
986 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -0400987 __enable_if_t<
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500988 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
989 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clowe46031a2018-07-02 18:41:15 +0000990 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500991 >
Marshall Clowe46031a2018-07-02 18:41:15 +0000992 append(const _Tp& __t) { __self_view __sv = __t; return append(__sv.data(), __sv.size()); }
Marshall Clow8db7fb02014-03-04 19:17:19 +0000993 basic_string& append(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clowe46031a2018-07-02 18:41:15 +0000994
Marshall Clow62953962016-10-03 23:40:48 +0000995 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +0000996 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -0400997 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +0000998 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500999 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1000 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001001 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001002 >
Marshall Clow82513342016-09-24 22:45:42 +00001003 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001004 basic_string& append(const value_type* __s, size_type __n);
1005 basic_string& append(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001006 basic_string& append(size_type __n, value_type __c);
Eric Fiselier451d5582018-11-26 20:15:38 +00001007
1008 _LIBCPP_INLINE_VISIBILITY
1009 void __append_default_init(size_type __n);
1010
Howard Hinnantc51e1022010-05-11 19:42:16 +00001011 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001012 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001013 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001014 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001015 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001016 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001017 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001018 _LIBCPP_INLINE_VISIBILITY
1019 append(_InputIterator __first, _InputIterator __last) {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001020 const basic_string __temp(__first, __last, __alloc());
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001021 append(__temp.data(), __temp.size());
1022 return *this;
1023 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001024 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001025 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001026 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001027 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001028 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001029 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001030 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001031 _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001032 append(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001033
Eric Fiselierfc92be82017-04-19 00:28:44 +00001034#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001035 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001036 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001037#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001038
1039 void push_back(value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001040 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001041 void pop_back();
Marshall Clow05cf6692019-03-19 03:30:07 +00001042 _LIBCPP_INLINE_VISIBILITY reference front() _NOEXCEPT;
1043 _LIBCPP_INLINE_VISIBILITY const_reference front() const _NOEXCEPT;
1044 _LIBCPP_INLINE_VISIBILITY reference back() _NOEXCEPT;
1045 _LIBCPP_INLINE_VISIBILITY const_reference back() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001046
Marshall Clowe46031a2018-07-02 18:41:15 +00001047 template <class _Tp>
1048 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001049 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001050 <
1051 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1052 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001053 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001054 assign(const _Tp & __t) { __self_view __sv = __t; return assign(__sv.data(), __sv.size()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001055 _LIBCPP_INLINE_VISIBILITY
Marshall Clow95d5e9a2016-03-09 18:08:29 +00001056 basic_string& assign(const basic_string& __str) { return *this = __str; }
Eric Fiselierfc92be82017-04-19 00:28:44 +00001057#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001058 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001059 basic_string& assign(basic_string&& __str)
Marshall Clow5aa9e942015-10-05 16:17:34 +00001060 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001061 {*this = _VSTD::move(__str); return *this;}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001062#endif
Marshall Clow8db7fb02014-03-04 19:17:19 +00001063 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow62953962016-10-03 23:40:48 +00001064 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001065 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001066 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001067 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001068 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1069 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001070 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001071 >
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001072 assign(const _Tp & __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001073 basic_string& assign(const value_type* __s, size_type __n);
1074 basic_string& assign(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001075 basic_string& assign(size_type __n, value_type __c);
1076 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001077 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001078 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001079 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001080 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001081 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001082 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001083 assign(_InputIterator __first, _InputIterator __last);
1084 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001085 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001086 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001087 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001088 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001089 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001090 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001091 assign(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001092#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001093 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001094 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001095#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001096
Howard Hinnantcf823322010-12-17 14:46:43 +00001097 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001098 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001099
1100 template <class _Tp>
1101 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001102 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001103 <
1104 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1105 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001106 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001107 insert(size_type __pos1, const _Tp& __t)
1108 { __self_view __sv = __t; return insert(__pos1, __sv.data(), __sv.size()); }
1109
Marshall Clow82513342016-09-24 22:45:42 +00001110 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001111 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001112 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001113 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001114 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001115 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001116 >
Marshall Clow82513342016-09-24 22:45:42 +00001117 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Marshall Clow8db7fb02014-03-04 19:17:19 +00001118 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001119 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1120 basic_string& insert(size_type __pos, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001121 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1122 iterator insert(const_iterator __pos, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001123 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001124 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1125 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001126 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001127 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001128 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001129 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001130 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001131 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001132 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1133 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001134 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001135 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001136 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001137 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001138 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001139 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001140 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001141#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001142 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001143 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1144 {return insert(__pos, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001145#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001146
1147 basic_string& erase(size_type __pos = 0, size_type __n = npos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001148 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001149 iterator erase(const_iterator __pos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001150 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001151 iterator erase(const_iterator __first, const_iterator __last);
1152
Howard Hinnantcf823322010-12-17 14:46:43 +00001153 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001154 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001155
1156 template <class _Tp>
1157 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001158 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001159 <
1160 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1161 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001162 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001163 replace(size_type __pos1, size_type __n1, const _Tp& __t) { __self_view __sv = __t; return replace(__pos1, __n1, __sv.data(), __sv.size()); }
Marshall Clow8db7fb02014-03-04 19:17:19 +00001164 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2=npos);
Marshall Clow82513342016-09-24 22:45:42 +00001165 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001166 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001167 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001168 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001169 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001170 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001171 >
Marshall Clow82513342016-09-24 22:45:42 +00001172 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001173 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
1174 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001175 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001176 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001177 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001178
1179 template <class _Tp>
1180 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001181 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001182 <
1183 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1184 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001185 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001186 replace(const_iterator __i1, const_iterator __i2, const _Tp& __t) { __self_view __sv = __t; return replace(__i1 - begin(), __i2 - __i1, __sv); }
1187
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001188 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001189 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +00001190 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001191 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Howard Hinnantcf823322010-12-17 14:46:43 +00001192 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001193 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001194 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001195 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001196 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001197 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001198 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001199 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001200 >
Howard Hinnant990d6e82010-11-17 21:11:40 +00001201 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001202#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001203 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001204 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001205 {return replace(__i1, __i2, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001206#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001207
Howard Hinnantd17880b2013-06-28 16:59:19 +00001208 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
Howard Hinnantcf823322010-12-17 14:46:43 +00001209 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001210 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1211
Howard Hinnantcf823322010-12-17 14:46:43 +00001212 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001213 void swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001214#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00001215 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +00001216#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001217 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001218 __is_nothrow_swappable<allocator_type>::value);
1219#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001220
Howard Hinnantcf823322010-12-17 14:46:43 +00001221 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001222 const value_type* c_str() const _NOEXCEPT {return data();}
Howard Hinnantcf823322010-12-17 14:46:43 +00001223 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001224 const value_type* data() const _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Eric Fiselier4ee2a892017-11-20 20:23:27 +00001225#if _LIBCPP_STD_VER > 14 || defined(_LIBCPP_BUILDING_LIBRARY)
Marshall Clowd3c22392016-03-08 15:44:30 +00001226 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001227 value_type* data() _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Marshall Clowd3c22392016-03-08 15:44:30 +00001228#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001229
Howard Hinnantcf823322010-12-17 14:46:43 +00001230 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001231 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001232
Howard Hinnantcf823322010-12-17 14:46:43 +00001233 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001234 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001235
1236 template <class _Tp>
1237 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001238 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001239 <
1240 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1241 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001242 >
zoecarver1997e0a2021-02-05 11:54:47 -08001243 find(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001244 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001245 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001246 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001247 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001248
Howard Hinnantcf823322010-12-17 14:46:43 +00001249 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001250 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001251
1252 template <class _Tp>
1253 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001254 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001255 <
1256 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1257 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001258 >
zoecarver1997e0a2021-02-05 11:54:47 -08001259 rfind(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001260 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001261 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001262 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001263 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001264
Howard Hinnantcf823322010-12-17 14:46:43 +00001265 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001266 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001267
1268 template <class _Tp>
1269 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001270 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001271 <
1272 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1273 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001274 >
zoecarver1997e0a2021-02-05 11:54:47 -08001275 find_first_of(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001276 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001277 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001278 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001279 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001280 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001281
Howard Hinnantcf823322010-12-17 14:46:43 +00001282 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001283 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001284
1285 template <class _Tp>
1286 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001287 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001288 <
1289 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1290 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001291 >
zoecarver1997e0a2021-02-05 11:54:47 -08001292 find_last_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001293 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001294 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001295 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001296 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001297 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001298
Howard Hinnantcf823322010-12-17 14:46:43 +00001299 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001300 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001301
1302 template <class _Tp>
1303 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001304 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001305 <
1306 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1307 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001308 >
zoecarver1997e0a2021-02-05 11:54:47 -08001309 find_first_not_of(const _Tp &__t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001310 size_type find_first_not_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001311 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001312 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001313 _LIBCPP_INLINE_VISIBILITY
1314 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1315
1316 _LIBCPP_INLINE_VISIBILITY
1317 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001318
1319 template <class _Tp>
1320 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001321 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001322 <
1323 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1324 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001325 >
zoecarver1997e0a2021-02-05 11:54:47 -08001326 find_last_not_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001327 size_type find_last_not_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001328 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001329 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001330 _LIBCPP_INLINE_VISIBILITY
1331 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1332
1333 _LIBCPP_INLINE_VISIBILITY
1334 int compare(const basic_string& __str) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001335
1336 template <class _Tp>
1337 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001338 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001339 <
1340 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1341 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001342 >
zoecarver1997e0a2021-02-05 11:54:47 -08001343 compare(const _Tp &__t) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001344
1345 template <class _Tp>
1346 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001347 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001348 <
1349 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1350 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001351 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001352 compare(size_type __pos1, size_type __n1, const _Tp& __t) const;
1353
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001354 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001355 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Marshall Clow8db7fb02014-03-04 19:17:19 +00001356 int compare(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2=npos) const;
Marshall Clowe46031a2018-07-02 18:41:15 +00001357
Marshall Clow82513342016-09-24 22:45:42 +00001358 template <class _Tp>
Eric Fiselier7d4aa5c2016-10-14 05:29:46 +00001359 inline _LIBCPP_INLINE_VISIBILITY
Louis Dionne9ce598d2021-09-08 09:14:43 -04001360 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001361 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001362 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001363 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001364 >
Marshall Clow82513342016-09-24 22:45:42 +00001365 compare(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos) const;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001366 int compare(const value_type* __s) const _NOEXCEPT;
1367 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1368 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001369
Marshall Clow18c293b2017-12-04 20:11:38 +00001370#if _LIBCPP_STD_VER > 17
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001371 constexpr _LIBCPP_INLINE_VISIBILITY
1372 bool starts_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001373 { return __self_view(data(), size()).starts_with(__sv); }
1374
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001375 constexpr _LIBCPP_INLINE_VISIBILITY
1376 bool starts_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001377 { return !empty() && _Traits::eq(front(), __c); }
1378
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001379 constexpr _LIBCPP_INLINE_VISIBILITY
1380 bool starts_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001381 { return starts_with(__self_view(__s)); }
1382
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001383 constexpr _LIBCPP_INLINE_VISIBILITY
1384 bool ends_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001385 { return __self_view(data(), size()).ends_with( __sv); }
1386
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001387 constexpr _LIBCPP_INLINE_VISIBILITY
1388 bool ends_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001389 { return !empty() && _Traits::eq(back(), __c); }
1390
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001391 constexpr _LIBCPP_INLINE_VISIBILITY
1392 bool ends_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001393 { return ends_with(__self_view(__s)); }
1394#endif
1395
Wim Leflere023c3542021-01-19 14:33:30 -05001396#if _LIBCPP_STD_VER > 20
1397 constexpr _LIBCPP_INLINE_VISIBILITY
1398 bool contains(__self_view __sv) const noexcept
1399 { return __self_view(data(), size()).contains(__sv); }
1400
1401 constexpr _LIBCPP_INLINE_VISIBILITY
1402 bool contains(value_type __c) const noexcept
1403 { return __self_view(data(), size()).contains(__c); }
1404
1405 constexpr _LIBCPP_INLINE_VISIBILITY
1406 bool contains(const value_type* __s) const
1407 { return __self_view(data(), size()).contains(__s); }
1408#endif
1409
Howard Hinnantcf823322010-12-17 14:46:43 +00001410 _LIBCPP_INLINE_VISIBILITY bool __invariants() const;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001411
Marshall Clowe60a7182018-05-29 17:04:37 +00001412 _LIBCPP_INLINE_VISIBILITY void __clear_and_shrink() _NOEXCEPT;
Marshall Clow851b9ec2019-05-20 21:56:51 +00001413
Marek Kurdejc9848142020-11-26 10:07:16 +01001414 _LIBCPP_INLINE_VISIBILITY void __shrink_or_extend(size_type __target_capacity);
1415
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001416 _LIBCPP_INLINE_VISIBILITY
1417 bool __is_long() const _NOEXCEPT
1418 {return bool(__r_.first().__s.__size_ & __short_mask);}
1419
Louis Dionneba400782020-10-02 15:02:52 -04001420#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001421
1422 bool __dereferenceable(const const_iterator* __i) const;
1423 bool __decrementable(const const_iterator* __i) const;
1424 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1425 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1426
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001427#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001428
Howard Hinnantc51e1022010-05-11 19:42:16 +00001429private:
Nikolas Klauser93826d12022-01-04 17:24:03 +01001430 _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI static bool __fits_in_sso(size_type __sz) {
1431 // SSO is disabled during constant evaluation because `__is_long` isn't constexpr friendly
1432 return !__libcpp_is_constant_evaluated() && (__sz < __min_cap);
1433 }
1434
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001435 _LIBCPP_INLINE_VISIBILITY
1436 allocator_type& __alloc() _NOEXCEPT
1437 {return __r_.second();}
1438 _LIBCPP_INLINE_VISIBILITY
1439 const allocator_type& __alloc() const _NOEXCEPT
1440 {return __r_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001441
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001442#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001443
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001444 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001445 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001446# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001447 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001448# else
1449 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1450# endif
1451
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001452 _LIBCPP_INLINE_VISIBILITY
1453 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001454# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001455 {return __r_.first().__s.__size_ >> 1;}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001456# else
1457 {return __r_.first().__s.__size_;}
1458# endif
1459
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001460#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001461
1462 _LIBCPP_INLINE_VISIBILITY
1463 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001464# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001465 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1466# else
1467 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
1468# endif
1469
1470 _LIBCPP_INLINE_VISIBILITY
1471 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001472# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001473 {return __r_.first().__s.__size_;}
1474# else
1475 {return __r_.first().__s.__size_ >> 1;}
1476# endif
1477
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001478#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001479
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001480 _LIBCPP_INLINE_VISIBILITY
1481 void __set_long_size(size_type __s) _NOEXCEPT
1482 {__r_.first().__l.__size_ = __s;}
1483 _LIBCPP_INLINE_VISIBILITY
1484 size_type __get_long_size() const _NOEXCEPT
1485 {return __r_.first().__l.__size_;}
1486 _LIBCPP_INLINE_VISIBILITY
1487 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001488 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1489
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001490 _LIBCPP_INLINE_VISIBILITY
1491 void __set_long_cap(size_type __s) _NOEXCEPT
1492 {__r_.first().__l.__cap_ = __long_mask | __s;}
1493 _LIBCPP_INLINE_VISIBILITY
1494 size_type __get_long_cap() const _NOEXCEPT
Howard Hinnant28b24882011-12-01 20:21:04 +00001495 {return __r_.first().__l.__cap_ & size_type(~__long_mask);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001496
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001497 _LIBCPP_INLINE_VISIBILITY
1498 void __set_long_pointer(pointer __p) _NOEXCEPT
1499 {__r_.first().__l.__data_ = __p;}
1500 _LIBCPP_INLINE_VISIBILITY
1501 pointer __get_long_pointer() _NOEXCEPT
1502 {return __r_.first().__l.__data_;}
1503 _LIBCPP_INLINE_VISIBILITY
1504 const_pointer __get_long_pointer() const _NOEXCEPT
1505 {return __r_.first().__l.__data_;}
1506 _LIBCPP_INLINE_VISIBILITY
1507 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001508 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001509 _LIBCPP_INLINE_VISIBILITY
1510 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001511 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001512 _LIBCPP_INLINE_VISIBILITY
1513 pointer __get_pointer() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001514 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001515 _LIBCPP_INLINE_VISIBILITY
1516 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001517 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1518
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001519 _LIBCPP_INLINE_VISIBILITY
1520 void __zero() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001521 {
1522 size_type (&__a)[__n_words] = __r_.first().__r.__words;
1523 for (unsigned __i = 0; __i < __n_words; ++__i)
1524 __a[__i] = 0;
1525 }
1526
1527 template <size_type __a> static
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001528 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00001529 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +00001530 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001531 enum {__alignment = 16};
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001532 static _LIBCPP_INLINE_VISIBILITY
1533 size_type __recommend(size_type __s) _NOEXCEPT
Marshall Clow80584522018-02-07 21:30:17 +00001534 {
1535 if (__s < __min_cap) return static_cast<size_type>(__min_cap) - 1;
1536 size_type __guess = __align_it<sizeof(value_type) < __alignment ?
1537 __alignment/sizeof(value_type) : 1 > (__s+1) - 1;
1538 if (__guess == __min_cap) ++__guess;
1539 return __guess;
1540 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001541
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001542 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001543 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001544 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001545 void __init(const value_type* __s, size_type __sz);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001546 inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001547 void __init(size_type __n, value_type __c);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001548
Martijn Vels5e7c9752020-03-04 17:52:46 -05001549 // Slow path for the (inlined) copy constructor for 'long' strings.
1550 // Always externally instantiated and not inlined.
1551 // Requires that __s is zero terminated.
1552 // The main reason for this function to exist is because for unstable, we
1553 // want to allow inlining of the copy constructor. However, we don't want
1554 // to call the __init() functions as those are marked as inline which may
1555 // result in over-aggressive inlining by the compiler, where our aim is
1556 // to only inline the fast path code directly in the ctor.
1557 void __init_copy_ctor_external(const value_type* __s, size_type __sz);
1558
Howard Hinnantc51e1022010-05-11 19:42:16 +00001559 template <class _InputIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001560 inline
Louis Dionne9ce598d2021-09-08 09:14:43 -04001561 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001562 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001563 __is_exactly_cpp17_input_iterator<_InputIterator>::value
1564 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001565 __init(_InputIterator __first, _InputIterator __last);
1566
1567 template <class _ForwardIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001568 inline
Louis Dionne9ce598d2021-09-08 09:14:43 -04001569 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001570 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001571 __is_cpp17_forward_iterator<_ForwardIterator>::value
1572 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001573 __init(_ForwardIterator __first, _ForwardIterator __last);
1574
1575 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001576 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001577 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1578 size_type __n_copy, size_type __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00001579 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001580
Martijn Vels596e3de2020-02-26 15:55:49 -05001581 // __assign_no_alias is invoked for assignment operations where we
1582 // have proof that the input does not alias the current instance.
1583 // For example, operator=(basic_string) performs a 'self' check.
1584 template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04001585 basic_string& __assign_no_alias(const value_type* __s, size_type __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05001586
Howard Hinnantcf823322010-12-17 14:46:43 +00001587 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001588 void __erase_to_end(size_type __pos);
1589
Martijn Velsa81fc792020-02-26 13:25:43 -05001590 // __erase_external_with_move is invoked for erase() invocations where
1591 // `n ~= npos`, likely requiring memory moves on the string data.
1592 void __erase_external_with_move(size_type __pos, size_type __n);
1593
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001594 _LIBCPP_INLINE_VISIBILITY
1595 void __copy_assign_alloc(const basic_string& __str)
1596 {__copy_assign_alloc(__str, integral_constant<bool,
1597 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1598
1599 _LIBCPP_INLINE_VISIBILITY
1600 void __copy_assign_alloc(const basic_string& __str, true_type)
1601 {
Marshall Clowf258c202017-01-31 03:40:52 +00001602 if (__alloc() == __str.__alloc())
1603 __alloc() = __str.__alloc();
1604 else
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001605 {
Marshall Clowf258c202017-01-31 03:40:52 +00001606 if (!__str.__is_long())
1607 {
Vedant Kumar55e007e2018-03-08 21:15:26 +00001608 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001609 __alloc() = __str.__alloc();
1610 }
1611 else
1612 {
1613 allocator_type __a = __str.__alloc();
1614 pointer __p = __alloc_traits::allocate(__a, __str.__get_long_cap());
Vedant Kumar55e007e2018-03-08 21:15:26 +00001615 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001616 __alloc() = _VSTD::move(__a);
1617 __set_long_pointer(__p);
1618 __set_long_cap(__str.__get_long_cap());
1619 __set_long_size(__str.size());
1620 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001621 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001622 }
1623
1624 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001625 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001626 {}
1627
Eric Fiselierfc92be82017-04-19 00:28:44 +00001628#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +00001629 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001630 void __move_assign(basic_string& __str, false_type)
1631 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnantcf823322010-12-17 14:46:43 +00001632 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001633 void __move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001634#if _LIBCPP_STD_VER > 14
1635 _NOEXCEPT;
1636#else
Howard Hinnant3e276872011-06-03 18:40:47 +00001637 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001638#endif
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001639#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001640
1641 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001642 void
Howard Hinnantc2734962011-09-02 20:42:31 +00001643 __move_assign_alloc(basic_string& __str)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001644 _NOEXCEPT_(
1645 !__alloc_traits::propagate_on_container_move_assignment::value ||
1646 is_nothrow_move_assignable<allocator_type>::value)
1647 {__move_assign_alloc(__str, integral_constant<bool,
1648 __alloc_traits::propagate_on_container_move_assignment::value>());}
1649
1650 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00001651 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001652 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1653 {
1654 __alloc() = _VSTD::move(__c.__alloc());
1655 }
1656
1657 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001658 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001659 _NOEXCEPT
1660 {}
1661
Martijn Velsda7d94f2020-06-19 14:24:03 -04001662 basic_string& __assign_external(const value_type* __s);
1663 basic_string& __assign_external(const value_type* __s, size_type __n);
1664
1665 // Assigns the value in __s, guaranteed to be __n < __min_cap in length.
1666 inline basic_string& __assign_short(const value_type* __s, size_type __n) {
1667 pointer __p = __is_long()
1668 ? (__set_long_size(__n), __get_long_pointer())
1669 : (__set_short_size(__n), __get_short_pointer());
1670 traits_type::move(_VSTD::__to_address(__p), __s, __n);
1671 traits_type::assign(__p[__n], value_type());
1672 return *this;
1673 }
1674
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01001675 _LIBCPP_HIDE_FROM_ABI basic_string& __null_terminate_at(value_type* __p, size_type __newsz) {
1676 __set_size(__newsz);
1677 __invalidate_iterators_past(__newsz);
1678 traits_type::assign(__p[__newsz], value_type());
1679 return *this;
1680 }
1681
Howard Hinnantcf823322010-12-17 14:46:43 +00001682 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
1683 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(size_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001684
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001685 template<class _Tp>
1686 _LIBCPP_INLINE_VISIBILITY
1687 bool __addr_in_range(_Tp&& __t) const {
1688 const volatile void *__p = _VSTD::addressof(__t);
1689 return data() <= __p && __p <= data() + size();
1690 }
1691
Louis Dionned24191c2021-08-19 12:39:16 -04001692 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1693 void __throw_length_error() const {
Nikolas Klauserf89bb932022-01-24 19:44:34 +01001694 _VSTD::__throw_length_error("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001695 }
1696
1697 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1698 void __throw_out_of_range() const {
Nikolas Klauserf89bb932022-01-24 19:44:34 +01001699 _VSTD::__throw_out_of_range("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001700 }
1701
Howard Hinnantc51e1022010-05-11 19:42:16 +00001702 friend basic_string operator+<>(const basic_string&, const basic_string&);
1703 friend basic_string operator+<>(const value_type*, const basic_string&);
1704 friend basic_string operator+<>(value_type, const basic_string&);
1705 friend basic_string operator+<>(const basic_string&, const value_type*);
1706 friend basic_string operator+<>(const basic_string&, value_type);
1707};
1708
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001709// These declarations must appear before any functions are implicitly used
1710// so that they have the correct visibility specifier.
1711#ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
Louis Dionne89258142021-08-23 15:32:36 -04001712 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1713# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1714 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1715# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001716#else
Louis Dionne89258142021-08-23 15:32:36 -04001717 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1718# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1719 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1720# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001721#endif
1722
1723
Louis Dionned59f8a52021-08-17 11:59:07 -04001724#if _LIBCPP_STD_VER >= 17
Marshall Clowa0563332018-02-08 06:34:03 +00001725template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -05001726 class _CharT = __iter_value_type<_InputIterator>,
Marshall Clowa0563332018-02-08 06:34:03 +00001727 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001728 class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
1729 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowa0563332018-02-08 06:34:03 +00001730 >
1731basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
1732 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
Marshall Clowe46031a2018-07-02 18:41:15 +00001733
1734template<class _CharT,
1735 class _Traits,
1736 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001737 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowe46031a2018-07-02 18:41:15 +00001738 >
1739explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
1740 -> basic_string<_CharT, _Traits, _Allocator>;
1741
1742template<class _CharT,
1743 class _Traits,
1744 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001745 class = enable_if_t<__is_allocator<_Allocator>::value>,
Marshall Clowe46031a2018-07-02 18:41:15 +00001746 class _Sz = typename allocator_traits<_Allocator>::size_type
1747 >
1748basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
1749 -> basic_string<_CharT, _Traits, _Allocator>;
Marshall Clowa0563332018-02-08 06:34:03 +00001750#endif
1751
Howard Hinnantc51e1022010-05-11 19:42:16 +00001752template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001753inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001754void
1755basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
1756{
Louis Dionneba400782020-10-02 15:02:52 -04001757#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001758 if (!__libcpp_is_constant_evaluated())
1759 __get_db()->__invalidate_all(this);
Louis Dionneba400782020-10-02 15:02:52 -04001760#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001761}
1762
1763template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001764inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001765void
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001766basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type __pos)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001767{
Louis Dionneba400782020-10-02 15:02:52 -04001768#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001769 if (!__libcpp_is_constant_evaluated()) {
1770 __c_node* __c = __get_db()->__find_c_and_lock(this);
1771 if (__c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001772 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001773 const_pointer __new_last = __get_pointer() + __pos;
1774 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001775 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001776 --__p;
1777 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1778 if (__i->base() > __new_last)
1779 {
1780 (*__p)->__c_ = nullptr;
1781 if (--__c->end_ != __p)
1782 _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1783 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001784 }
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001785 __get_db()->unlock();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001786 }
1787 }
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001788#else
1789 (void)__pos;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001790#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001791}
1792
1793template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001794inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001795basic_string<_CharT, _Traits, _Allocator>::basic_string()
Marshall Clowe546cbb2015-06-04 02:05:41 +00001796 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001797 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001798{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001799 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001800 __zero();
1801}
1802
1803template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001804inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001805basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
Eric Fiseliere012bf22015-07-18 20:40:46 +00001806#if _LIBCPP_STD_VER <= 14
1807 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1808#else
1809 _NOEXCEPT
1810#endif
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001811: __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001812{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001813 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001814 __zero();
1815}
1816
1817template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier815ed732016-09-16 00:00:48 +00001818void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1819 size_type __sz,
1820 size_type __reserve)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001821{
1822 if (__reserve > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001823 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001824 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001825 if (__fits_in_sso(__reserve))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001826 {
1827 __set_short_size(__sz);
1828 __p = __get_short_pointer();
1829 }
1830 else
1831 {
1832 size_type __cap = __recommend(__reserve);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001833 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001834 __set_long_pointer(__p);
1835 __set_long_cap(__cap+1);
1836 __set_long_size(__sz);
1837 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001838 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001839 traits_type::assign(__p[__sz], value_type());
1840}
1841
1842template <class _CharT, class _Traits, class _Allocator>
1843void
Howard Hinnantd17880b2013-06-28 16:59:19 +00001844basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001845{
1846 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001847 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001848 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001849 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001850 {
1851 __set_short_size(__sz);
1852 __p = __get_short_pointer();
1853 }
1854 else
1855 {
1856 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001857 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001858 __set_long_pointer(__p);
1859 __set_long_cap(__cap+1);
1860 __set_long_size(__sz);
1861 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001862 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001863 traits_type::assign(__p[__sz], value_type());
1864}
1865
1866template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00001867template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00001868basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001869 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001870{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001871 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001872 __init(__s, traits_type::length(__s));
Nikolas Klauserf2807732022-01-11 00:33:35 +01001873 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001874}
1875
1876template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001877inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001878basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001879 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001880{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001881 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
1882 __init(__s, __n);
1883 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001884}
1885
1886template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001887inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001888basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001889 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001890{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001891 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001892 __init(__s, __n);
Nikolas Klauserf2807732022-01-11 00:33:35 +01001893 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001894}
1895
1896template <class _CharT, class _Traits, class _Allocator>
1897basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001898 : __r_(__default_init_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001899{
1900 if (!__str.__is_long())
1901 __r_.first().__r = __str.__r_.first().__r;
1902 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001903 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1904 __str.__get_long_size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01001905 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001906}
1907
1908template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00001909basic_string<_CharT, _Traits, _Allocator>::basic_string(
1910 const basic_string& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001911 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001912{
1913 if (!__str.__is_long())
1914 __r_.first().__r = __str.__r_.first().__r;
1915 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001916 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1917 __str.__get_long_size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01001918 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001919}
1920
Martijn Vels5e7c9752020-03-04 17:52:46 -05001921template <class _CharT, class _Traits, class _Allocator>
1922void basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(
1923 const value_type* __s, size_type __sz) {
1924 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001925 if (__fits_in_sso(__sz)) {
Martijn Vels5e7c9752020-03-04 17:52:46 -05001926 __p = __get_short_pointer();
1927 __set_short_size(__sz);
1928 } else {
1929 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001930 __throw_length_error();
Martijn Vels5e7c9752020-03-04 17:52:46 -05001931 size_t __cap = __recommend(__sz);
1932 __p = __alloc_traits::allocate(__alloc(), __cap + 1);
1933 __set_long_pointer(__p);
1934 __set_long_cap(__cap + 1);
1935 __set_long_size(__sz);
1936 }
1937 traits_type::copy(_VSTD::__to_address(__p), __s, __sz + 1);
1938}
1939
Eric Fiselierfc92be82017-04-19 00:28:44 +00001940#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001941
1942template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001943inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001944basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +00001945#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +00001946 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowa80abc72015-06-03 19:56:43 +00001947#else
1948 _NOEXCEPT
1949#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001950 : __r_(_VSTD::move(__str.__r_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001951{
1952 __str.__zero();
Nikolas Klauserf2807732022-01-11 00:33:35 +01001953 _VSTD::__debug_db_insert_c(this);
Louis Dionneba400782020-10-02 15:02:52 -04001954#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauserf2807732022-01-11 00:33:35 +01001955 if (!__libcpp_is_constant_evaluated() && __is_long())
1956 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001957#endif
1958}
1959
1960template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001961inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001962basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001963 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001964{
Marshall Clowd2d24692014-07-17 15:32:20 +00001965 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001966 __init(_VSTD::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd2d24692014-07-17 15:32:20 +00001967 else
1968 {
1969 __r_.first().__r = __str.__r_.first().__r;
1970 __str.__zero();
1971 }
Nikolas Klauserf2807732022-01-11 00:33:35 +01001972 _VSTD::__debug_db_insert_c(this);
Louis Dionneba400782020-10-02 15:02:52 -04001973#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauserf2807732022-01-11 00:33:35 +01001974 if (!__libcpp_is_constant_evaluated() && __is_long())
1975 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001976#endif
1977}
1978
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001979#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001980
1981template <class _CharT, class _Traits, class _Allocator>
1982void
1983basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
1984{
1985 if (__n > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001986 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001987 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001988 if (__fits_in_sso(__n))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001989 {
1990 __set_short_size(__n);
1991 __p = __get_short_pointer();
1992 }
1993 else
1994 {
1995 size_type __cap = __recommend(__n);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001996 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001997 __set_long_pointer(__p);
1998 __set_long_cap(__cap+1);
1999 __set_long_size(__n);
2000 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002001 traits_type::assign(_VSTD::__to_address(__p), __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002002 traits_type::assign(__p[__n], value_type());
2003}
2004
2005template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002006inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002007basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002008 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002009{
2010 __init(__n, __c);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002011 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002012}
2013
2014template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002015template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00002016basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002017 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002018{
2019 __init(__n, __c);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002020 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002021}
2022
Howard Hinnantc51e1022010-05-11 19:42:16 +00002023template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00002024basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
2025 size_type __pos, size_type __n,
2026 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002027 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002028{
2029 size_type __str_sz = __str.size();
2030 if (__pos > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002031 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002032 __init(__str.data() + __pos, _VSTD::min(__n, __str_sz - __pos));
Nikolas Klauserf2807732022-01-11 00:33:35 +01002033 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002034}
2035
2036template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002037inline
Marshall Clow83445802016-04-07 18:13:41 +00002038basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +00002039 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002040 : __r_(__default_init_tag(), __a)
Marshall Clow83445802016-04-07 18:13:41 +00002041{
2042 size_type __str_sz = __str.size();
2043 if (__pos > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002044 __throw_out_of_range();
Marshall Clow83445802016-04-07 18:13:41 +00002045 __init(__str.data() + __pos, __str_sz - __pos);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002046 _VSTD::__debug_db_insert_c(this);
Marshall Clow83445802016-04-07 18:13:41 +00002047}
2048
2049template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002050template <class _Tp, class>
Marshall Clow78dbe462016-11-14 18:22:19 +00002051basic_string<_CharT, _Traits, _Allocator>::basic_string(
Marshall Clowe46031a2018-07-02 18:41:15 +00002052 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002053 : __r_(__default_init_tag(), __a)
Marshall Clow78dbe462016-11-14 18:22:19 +00002054{
Marshall Clowe46031a2018-07-02 18:41:15 +00002055 __self_view __sv0 = __t;
2056 __self_view __sv = __sv0.substr(__pos, __n);
Marshall Clow78dbe462016-11-14 18:22:19 +00002057 __init(__sv.data(), __sv.size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002058 _VSTD::__debug_db_insert_c(this);
Marshall Clow78dbe462016-11-14 18:22:19 +00002059}
2060
2061template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002062template <class _Tp, class>
2063basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002064 : __r_(__default_init_tag(), __default_init_tag())
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002065{
Marshall Clowe46031a2018-07-02 18:41:15 +00002066 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002067 __init(__sv.data(), __sv.size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002068 _VSTD::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002069}
2070
2071template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002072template <class _Tp, class>
2073basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002074 : __r_(__default_init_tag(), __a)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002075{
Marshall Clowe46031a2018-07-02 18:41:15 +00002076 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002077 __init(__sv.data(), __sv.size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002078 _VSTD::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002079}
2080
2081template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002082template <class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002083__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002084<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002085 __is_exactly_cpp17_input_iterator<_InputIterator>::value
2086>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002087basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
2088{
2089 __zero();
2090#ifndef _LIBCPP_NO_EXCEPTIONS
2091 try
2092 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002093#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002094 for (; __first != __last; ++__first)
2095 push_back(*__first);
2096#ifndef _LIBCPP_NO_EXCEPTIONS
2097 }
2098 catch (...)
2099 {
2100 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002101 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002102 throw;
2103 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002104#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002105}
2106
2107template <class _CharT, class _Traits, class _Allocator>
2108template <class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002109__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002110<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002111 __is_cpp17_forward_iterator<_ForwardIterator>::value
2112>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002113basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
2114{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002115 size_type __sz = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002116 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002117 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002118 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002119 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002120 {
2121 __set_short_size(__sz);
2122 __p = __get_short_pointer();
2123 }
2124 else
2125 {
2126 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002127 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002128 __set_long_pointer(__p);
2129 __set_long_cap(__cap+1);
2130 __set_long_size(__sz);
2131 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002132
2133#ifndef _LIBCPP_NO_EXCEPTIONS
2134 try
2135 {
2136#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002137 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002138 traits_type::assign(*__p, *__first);
2139 traits_type::assign(*__p, value_type());
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002140#ifndef _LIBCPP_NO_EXCEPTIONS
2141 }
2142 catch (...)
2143 {
2144 if (__is_long())
2145 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
2146 throw;
2147 }
2148#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002149}
2150
2151template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002152template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002153inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002154basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002155 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002156{
2157 __init(__first, __last);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002158 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002159}
2160
2161template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002162template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002163inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002164basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
2165 const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002166 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002167{
2168 __init(__first, __last);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002169 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002170}
2171
Eric Fiselierfc92be82017-04-19 00:28:44 +00002172#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002173
Howard Hinnantc51e1022010-05-11 19:42:16 +00002174template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002175inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002176basic_string<_CharT, _Traits, _Allocator>::basic_string(
2177 initializer_list<_CharT> __il)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002178 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002179{
2180 __init(__il.begin(), __il.end());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002181 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002182}
2183
2184template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002185inline
Eric Fiselier9d355982017-04-12 23:45:53 +00002186
Eric Fiselier812882b2017-02-17 01:17:10 +00002187basic_string<_CharT, _Traits, _Allocator>::basic_string(
2188 initializer_list<_CharT> __il, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002189 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002190{
2191 __init(__il.begin(), __il.end());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002192 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002193}
2194
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002195#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002196
Howard Hinnantc51e1022010-05-11 19:42:16 +00002197template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002198basic_string<_CharT, _Traits, _Allocator>::~basic_string()
2199{
Louis Dionneba400782020-10-02 15:02:52 -04002200#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002201 if (!__libcpp_is_constant_evaluated())
2202 __get_db()->__erase_c(this);
Howard Hinnant8ea98242013-08-23 17:37:05 +00002203#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002204 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002205 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002206}
2207
2208template <class _CharT, class _Traits, class _Allocator>
2209void
2210basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
2211 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002212 size_type __n_copy, size_type __n_del, size_type __n_add, const value_type* __p_new_stuff)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002213{
2214 size_type __ms = max_size();
2215 if (__delta_cap > __ms - __old_cap - 1)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002216 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002217 pointer __old_p = __get_pointer();
2218 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002219 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002220 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002221 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002222 __invalidate_all_iterators();
2223 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002224 traits_type::copy(_VSTD::__to_address(__p),
2225 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002226 if (__n_add != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002227 traits_type::copy(_VSTD::__to_address(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002228 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2229 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002230 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2231 _VSTD::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002232 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002233 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002234 __set_long_pointer(__p);
2235 __set_long_cap(__cap+1);
2236 __old_sz = __n_copy + __n_add + __sec_cp_sz;
2237 __set_long_size(__old_sz);
2238 traits_type::assign(__p[__old_sz], value_type());
2239}
2240
2241template <class _CharT, class _Traits, class _Allocator>
2242void
2243basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2244 size_type __n_copy, size_type __n_del, size_type __n_add)
2245{
2246 size_type __ms = max_size();
Marshall Clow2267c5d2013-11-06 14:24:38 +00002247 if (__delta_cap > __ms - __old_cap)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002248 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002249 pointer __old_p = __get_pointer();
2250 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002251 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002252 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002253 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002254 __invalidate_all_iterators();
2255 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002256 traits_type::copy(_VSTD::__to_address(__p),
2257 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002258 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2259 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002260 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2261 _VSTD::__to_address(__old_p) + __n_copy + __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002262 __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002263 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002264 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002265 __set_long_pointer(__p);
2266 __set_long_cap(__cap+1);
2267}
2268
2269// assign
2270
2271template <class _CharT, class _Traits, class _Allocator>
Martijn Vels596e3de2020-02-26 15:55:49 -05002272template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04002273basic_string<_CharT, _Traits, _Allocator>&
2274basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(
Martijn Vels596e3de2020-02-26 15:55:49 -05002275 const value_type* __s, size_type __n) {
2276 size_type __cap = __is_short ? __min_cap : __get_long_cap();
2277 if (__n < __cap) {
2278 pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
2279 __is_short ? __set_short_size(__n) : __set_long_size(__n);
2280 traits_type::copy(_VSTD::__to_address(__p), __s, __n);
2281 traits_type::assign(__p[__n], value_type());
2282 __invalidate_iterators_past(__n);
2283 } else {
2284 size_type __sz = __is_short ? __get_short_size() : __get_long_size();
2285 __grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
2286 }
Martijn Velsb6a08b62020-04-10 18:36:31 -04002287 return *this;
Martijn Vels596e3de2020-02-26 15:55:49 -05002288}
2289
2290template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002291basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002292basic_string<_CharT, _Traits, _Allocator>::__assign_external(
2293 const value_type* __s, size_type __n) {
2294 size_type __cap = capacity();
2295 if (__cap >= __n) {
2296 value_type* __p = _VSTD::__to_address(__get_pointer());
2297 traits_type::move(__p, __s, __n);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002298 return __null_terminate_at(__p, __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04002299 } else {
2300 size_type __sz = size();
2301 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002302 return *this;
Martijn Velsda7d94f2020-06-19 14:24:03 -04002303 }
Martijn Velsda7d94f2020-06-19 14:24:03 -04002304}
2305
2306template <class _CharT, class _Traits, class _Allocator>
2307basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002308basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002309{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002310 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Nikolas Klauser93826d12022-01-04 17:24:03 +01002311 return (__builtin_constant_p(__n) && __fits_in_sso(__n))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002312 ? __assign_short(__s, __n)
2313 : __assign_external(__s, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002314}
2315
2316template <class _CharT, class _Traits, class _Allocator>
2317basic_string<_CharT, _Traits, _Allocator>&
2318basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2319{
2320 size_type __cap = capacity();
2321 if (__cap < __n)
2322 {
2323 size_type __sz = size();
2324 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2325 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002326 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002327 traits_type::assign(__p, __n, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002328 return __null_terminate_at(__p, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002329}
2330
2331template <class _CharT, class _Traits, class _Allocator>
2332basic_string<_CharT, _Traits, _Allocator>&
2333basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2334{
2335 pointer __p;
2336 if (__is_long())
2337 {
2338 __p = __get_long_pointer();
2339 __set_long_size(1);
2340 }
2341 else
2342 {
2343 __p = __get_short_pointer();
2344 __set_short_size(1);
2345 }
2346 traits_type::assign(*__p, __c);
2347 traits_type::assign(*++__p, value_type());
2348 __invalidate_iterators_past(1);
2349 return *this;
2350}
2351
2352template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002353basic_string<_CharT, _Traits, _Allocator>&
2354basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2355{
Martijn Vels596e3de2020-02-26 15:55:49 -05002356 if (this != &__str) {
2357 __copy_assign_alloc(__str);
2358 if (!__is_long()) {
2359 if (!__str.__is_long()) {
Eric Fiselier03bbc922020-01-15 17:27:10 -05002360 __r_.first().__r = __str.__r_.first().__r;
Martijn Vels596e3de2020-02-26 15:55:49 -05002361 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002362 return __assign_no_alias<true>(__str.data(), __str.size());
Martijn Vels596e3de2020-02-26 15:55:49 -05002363 }
2364 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002365 return __assign_no_alias<false>(__str.data(), __str.size());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002366 }
Martijn Vels596e3de2020-02-26 15:55:49 -05002367 }
2368 return *this;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002369}
2370
Eric Fiselierfc92be82017-04-19 00:28:44 +00002371#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002372
2373template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002374inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002375void
2376basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002377 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002378{
2379 if (__alloc() != __str.__alloc())
2380 assign(__str);
2381 else
2382 __move_assign(__str, true_type());
2383}
2384
2385template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002386inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002387void
2388basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002389#if _LIBCPP_STD_VER > 14
2390 _NOEXCEPT
2391#else
Howard Hinnant3e276872011-06-03 18:40:47 +00002392 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002393#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002394{
marshall2ed41622019-11-27 07:13:00 -08002395 if (__is_long()) {
2396 __alloc_traits::deallocate(__alloc(), __get_long_pointer(),
2397 __get_long_cap());
2398#if _LIBCPP_STD_VER <= 14
2399 if (!is_nothrow_move_assignable<allocator_type>::value) {
2400 __set_short_size(0);
2401 traits_type::assign(__get_short_pointer()[0], value_type());
2402 }
2403#endif
2404 }
2405 __move_assign_alloc(__str);
2406 __r_.first() = __str.__r_.first();
2407 __str.__set_short_size(0);
2408 traits_type::assign(__str.__get_short_pointer()[0], value_type());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002409}
2410
2411template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002412inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002413basic_string<_CharT, _Traits, _Allocator>&
2414basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002415 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002416{
2417 __move_assign(__str, integral_constant<bool,
2418 __alloc_traits::propagate_on_container_move_assignment::value>());
2419 return *this;
2420}
2421
2422#endif
2423
2424template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002425template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002426__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002427<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002428 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002429 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002430>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002431basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2432{
Marshall Clow958362f2016-09-05 01:54:30 +00002433 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002434 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002435 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002436}
2437
2438template <class _CharT, class _Traits, class _Allocator>
2439template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002440__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002441<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002442 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002443 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002444>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002445basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2446{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002447 size_type __cap = capacity();
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002448 size_type __n = __string_is_trivial_iterator<_ForwardIterator>::value ?
2449 static_cast<size_type>(_VSTD::distance(__first, __last)) : 0;
2450
2451 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2452 (__cap >= __n || !__addr_in_range(*__first)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002453 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002454 if (__cap < __n)
2455 {
2456 size_type __sz = size();
2457 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2458 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002459 pointer __p = __get_pointer();
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002460 for (; __first != __last; ++__p, (void) ++__first)
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002461 traits_type::assign(*__p, *__first);
2462 traits_type::assign(*__p, value_type());
2463 __set_size(__n);
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04002464 __invalidate_iterators_past(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002465 }
2466 else
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002467 {
2468 const basic_string __temp(__first, __last, __alloc());
2469 assign(__temp.data(), __temp.size());
2470 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002471 return *this;
2472}
2473
2474template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002475basic_string<_CharT, _Traits, _Allocator>&
2476basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2477{
2478 size_type __sz = __str.size();
2479 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002480 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002481 return assign(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002482}
2483
2484template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002485template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002486__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002487<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002488 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
2489 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00002490 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002491>
Marshall Clow82513342016-09-24 22:45:42 +00002492basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002493{
Marshall Clow82513342016-09-24 22:45:42 +00002494 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002495 size_type __sz = __sv.size();
2496 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002497 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002498 return assign(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2499}
2500
2501
2502template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002503basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002504basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s) {
2505 return __assign_external(__s, traits_type::length(__s));
2506}
2507
2508template <class _CharT, class _Traits, class _Allocator>
2509basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002510basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002511{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002512 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Louis Dionne3e0c4332021-08-31 10:49:06 -04002513 return __builtin_constant_p(*__s)
Nikolas Klauser93826d12022-01-04 17:24:03 +01002514 ? (__fits_in_sso(traits_type::length(__s))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002515 ? __assign_short(__s, traits_type::length(__s))
2516 : __assign_external(__s, traits_type::length(__s)))
2517 : __assign_external(__s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002518}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002519// append
2520
2521template <class _CharT, class _Traits, class _Allocator>
2522basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002523basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002524{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002525 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002526 size_type __cap = capacity();
2527 size_type __sz = size();
2528 if (__cap - __sz >= __n)
2529 {
2530 if (__n)
2531 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002532 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002533 traits_type::copy(__p + __sz, __s, __n);
2534 __sz += __n;
2535 __set_size(__sz);
2536 traits_type::assign(__p[__sz], value_type());
2537 }
2538 }
2539 else
2540 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2541 return *this;
2542}
2543
2544template <class _CharT, class _Traits, class _Allocator>
2545basic_string<_CharT, _Traits, _Allocator>&
2546basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2547{
2548 if (__n)
2549 {
2550 size_type __cap = capacity();
2551 size_type __sz = size();
2552 if (__cap - __sz < __n)
2553 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2554 pointer __p = __get_pointer();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002555 traits_type::assign(_VSTD::__to_address(__p) + __sz, __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002556 __sz += __n;
2557 __set_size(__sz);
2558 traits_type::assign(__p[__sz], value_type());
2559 }
2560 return *this;
2561}
2562
2563template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00002564inline void
2565basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n)
2566{
2567 if (__n)
2568 {
2569 size_type __cap = capacity();
2570 size_type __sz = size();
2571 if (__cap - __sz < __n)
2572 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2573 pointer __p = __get_pointer();
2574 __sz += __n;
2575 __set_size(__sz);
2576 traits_type::assign(__p[__sz], value_type());
2577 }
2578}
2579
2580template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002581void
2582basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2583{
Howard Hinnant68bf1812013-04-30 21:44:48 +00002584 bool __is_short = !__is_long();
2585 size_type __cap;
2586 size_type __sz;
2587 if (__is_short)
2588 {
2589 __cap = __min_cap - 1;
2590 __sz = __get_short_size();
2591 }
2592 else
2593 {
2594 __cap = __get_long_cap() - 1;
2595 __sz = __get_long_size();
2596 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002597 if (__sz == __cap)
Howard Hinnant68bf1812013-04-30 21:44:48 +00002598 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002599 __grow_by(__cap, 1, __sz, __sz, 0);
Louis Dionne82d2b2c2021-11-16 11:26:56 -05002600 __is_short = false; // the string is always long after __grow_by
Howard Hinnant68bf1812013-04-30 21:44:48 +00002601 }
2602 pointer __p;
2603 if (__is_short)
2604 {
2605 __p = __get_short_pointer() + __sz;
2606 __set_short_size(__sz+1);
2607 }
2608 else
2609 {
2610 __p = __get_long_pointer() + __sz;
2611 __set_long_size(__sz+1);
2612 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002613 traits_type::assign(*__p, __c);
2614 traits_type::assign(*++__p, value_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002615}
2616
Howard Hinnantc51e1022010-05-11 19:42:16 +00002617template <class _CharT, class _Traits, class _Allocator>
2618template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002619__enable_if_t
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002620<
2621 __is_cpp17_forward_iterator<_ForwardIterator>::value,
2622 basic_string<_CharT, _Traits, _Allocator>&
2623>
2624basic_string<_CharT, _Traits, _Allocator>::append(
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002625 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002626{
2627 size_type __sz = size();
2628 size_type __cap = capacity();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002629 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002630 if (__n)
2631 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002632 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2633 !__addr_in_range(*__first))
Marshall Clow958362f2016-09-05 01:54:30 +00002634 {
2635 if (__cap - __sz < __n)
2636 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2637 pointer __p = __get_pointer() + __sz;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002638 for (; __first != __last; ++__p, (void) ++__first)
Marshall Clow958362f2016-09-05 01:54:30 +00002639 traits_type::assign(*__p, *__first);
2640 traits_type::assign(*__p, value_type());
2641 __set_size(__sz + __n);
2642 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002643 else
2644 {
2645 const basic_string __temp(__first, __last, __alloc());
2646 append(__temp.data(), __temp.size());
2647 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002648 }
2649 return *this;
2650}
2651
2652template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002653inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002654basic_string<_CharT, _Traits, _Allocator>&
2655basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2656{
2657 return append(__str.data(), __str.size());
2658}
2659
2660template <class _CharT, class _Traits, class _Allocator>
2661basic_string<_CharT, _Traits, _Allocator>&
2662basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2663{
2664 size_type __sz = __str.size();
2665 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002666 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002667 return append(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002668}
2669
2670template <class _CharT, class _Traits, class _Allocator>
Marshall Clow62953962016-10-03 23:40:48 +00002671template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002672 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002673 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002674 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clow82513342016-09-24 22:45:42 +00002675 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002676 >
Marshall Clow82513342016-09-24 22:45:42 +00002677basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002678{
Marshall Clow82513342016-09-24 22:45:42 +00002679 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002680 size_type __sz = __sv.size();
2681 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002682 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002683 return append(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2684}
2685
2686template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002687basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002688basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002689{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002690 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002691 return append(__s, traits_type::length(__s));
2692}
2693
2694// insert
2695
2696template <class _CharT, class _Traits, class _Allocator>
2697basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002698basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002699{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002700 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002701 size_type __sz = size();
2702 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002703 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002704 size_type __cap = capacity();
2705 if (__cap - __sz >= __n)
2706 {
2707 if (__n)
2708 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002709 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002710 size_type __n_move = __sz - __pos;
2711 if (__n_move != 0)
2712 {
2713 if (__p + __pos <= __s && __s < __p + __sz)
2714 __s += __n;
2715 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2716 }
2717 traits_type::move(__p + __pos, __s, __n);
2718 __sz += __n;
2719 __set_size(__sz);
2720 traits_type::assign(__p[__sz], value_type());
2721 }
2722 }
2723 else
2724 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2725 return *this;
2726}
2727
2728template <class _CharT, class _Traits, class _Allocator>
2729basic_string<_CharT, _Traits, _Allocator>&
2730basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2731{
2732 size_type __sz = size();
2733 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002734 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002735 if (__n)
2736 {
2737 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002738 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002739 if (__cap - __sz >= __n)
2740 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002741 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002742 size_type __n_move = __sz - __pos;
2743 if (__n_move != 0)
2744 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2745 }
2746 else
2747 {
2748 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002749 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002750 }
2751 traits_type::assign(__p + __pos, __n, __c);
2752 __sz += __n;
2753 __set_size(__sz);
2754 traits_type::assign(__p[__sz], value_type());
2755 }
2756 return *this;
2757}
2758
2759template <class _CharT, class _Traits, class _Allocator>
2760template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002761__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002762<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002763 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Marshall Clow039b2f02016-01-13 21:54:34 +00002764 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002765>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002766basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2767{
Nikolas Klausereed25832021-12-15 01:32:30 +01002768 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2769 "string::insert(iterator, range) called with an iterator not"
2770 " referring to this string");
2771 const basic_string __temp(__first, __last, __alloc());
2772 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002773}
2774
2775template <class _CharT, class _Traits, class _Allocator>
2776template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002777__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002778<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002779 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002780 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002781>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002782basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2783{
Nikolas Klausereed25832021-12-15 01:32:30 +01002784 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2785 "string::insert(iterator, range) called with an iterator not"
2786 " referring to this string");
2787
Howard Hinnantc51e1022010-05-11 19:42:16 +00002788 size_type __ip = static_cast<size_type>(__pos - begin());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002789 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002790 if (__n)
2791 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002792 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2793 !__addr_in_range(*__first))
2794 {
2795 size_type __sz = size();
2796 size_type __cap = capacity();
2797 value_type* __p;
2798 if (__cap - __sz >= __n)
2799 {
2800 __p = _VSTD::__to_address(__get_pointer());
2801 size_type __n_move = __sz - __ip;
2802 if (__n_move != 0)
2803 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
2804 }
2805 else
2806 {
2807 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
2808 __p = _VSTD::__to_address(__get_long_pointer());
2809 }
2810 __sz += __n;
2811 __set_size(__sz);
2812 traits_type::assign(__p[__sz], value_type());
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002813 for (__p += __ip; __first != __last; ++__p, (void) ++__first)
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002814 traits_type::assign(*__p, *__first);
2815 }
2816 else
Marshall Clow958362f2016-09-05 01:54:30 +00002817 {
2818 const basic_string __temp(__first, __last, __alloc());
2819 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
2820 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002821 }
2822 return begin() + __ip;
2823}
2824
2825template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002826inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002827basic_string<_CharT, _Traits, _Allocator>&
2828basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2829{
2830 return insert(__pos1, __str.data(), __str.size());
2831}
2832
2833template <class _CharT, class _Traits, class _Allocator>
2834basic_string<_CharT, _Traits, _Allocator>&
2835basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2836 size_type __pos2, size_type __n)
2837{
2838 size_type __str_sz = __str.size();
2839 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002840 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002841 return insert(__pos1, __str.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002842}
2843
2844template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002845template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002846__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002847<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002848 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00002849 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002850>
Marshall Clow82513342016-09-24 22:45:42 +00002851basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002852 size_type __pos2, size_type __n)
2853{
Marshall Clow82513342016-09-24 22:45:42 +00002854 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002855 size_type __str_sz = __sv.size();
2856 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002857 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002858 return insert(__pos1, __sv.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
2859}
2860
2861template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002862basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002863basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002864{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002865 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002866 return insert(__pos, __s, traits_type::length(__s));
2867}
2868
2869template <class _CharT, class _Traits, class _Allocator>
2870typename basic_string<_CharT, _Traits, _Allocator>::iterator
2871basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2872{
2873 size_type __ip = static_cast<size_type>(__pos - begin());
2874 size_type __sz = size();
2875 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002876 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002877 if (__cap == __sz)
2878 {
2879 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002880 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002881 }
2882 else
2883 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002884 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002885 size_type __n_move = __sz - __ip;
2886 if (__n_move != 0)
2887 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2888 }
2889 traits_type::assign(__p[__ip], __c);
2890 traits_type::assign(__p[++__sz], value_type());
2891 __set_size(__sz);
2892 return begin() + static_cast<difference_type>(__ip);
2893}
2894
2895template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002896inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002897typename basic_string<_CharT, _Traits, _Allocator>::iterator
2898basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2899{
Nikolas Klausereed25832021-12-15 01:32:30 +01002900 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2901 "string::insert(iterator, n, value) called with an iterator not"
2902 " referring to this string");
2903 difference_type __p = __pos - begin();
2904 insert(static_cast<size_type>(__p), __n, __c);
2905 return begin() + __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002906}
2907
2908// replace
2909
2910template <class _CharT, class _Traits, class _Allocator>
2911basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002912basic_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 +00002913 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00002914{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002915 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002916 size_type __sz = size();
2917 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002918 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002919 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002920 size_type __cap = capacity();
2921 if (__cap - __sz + __n1 >= __n2)
2922 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002923 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002924 if (__n1 != __n2)
2925 {
2926 size_type __n_move = __sz - __pos - __n1;
2927 if (__n_move != 0)
2928 {
2929 if (__n1 > __n2)
2930 {
2931 traits_type::move(__p + __pos, __s, __n2);
2932 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002933 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002934 }
2935 if (__p + __pos < __s && __s < __p + __sz)
2936 {
2937 if (__p + __pos + __n1 <= __s)
2938 __s += __n2 - __n1;
2939 else // __p + __pos < __s < __p + __pos + __n1
2940 {
2941 traits_type::move(__p + __pos, __s, __n1);
2942 __pos += __n1;
2943 __s += __n2;
2944 __n2 -= __n1;
2945 __n1 = 0;
2946 }
2947 }
2948 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2949 }
2950 }
2951 traits_type::move(__p + __pos, __s, __n2);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002952 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002953 }
2954 else
2955 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
2956 return *this;
2957}
2958
2959template <class _CharT, class _Traits, class _Allocator>
2960basic_string<_CharT, _Traits, _Allocator>&
2961basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
2962{
2963 size_type __sz = size();
2964 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002965 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002966 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002967 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002968 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002969 if (__cap - __sz + __n1 >= __n2)
2970 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002971 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002972 if (__n1 != __n2)
2973 {
2974 size_type __n_move = __sz - __pos - __n1;
2975 if (__n_move != 0)
2976 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2977 }
2978 }
2979 else
2980 {
2981 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002982 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002983 }
2984 traits_type::assign(__p + __pos, __n2, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002985 return __null_terminate_at(__p, __sz - (__n1 - __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002986}
2987
2988template <class _CharT, class _Traits, class _Allocator>
2989template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002990__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002991<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002992 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002993 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002994>
Howard Hinnant990d6e82010-11-17 21:11:40 +00002995basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002996 _InputIterator __j1, _InputIterator __j2)
2997{
Marshall Clow958362f2016-09-05 01:54:30 +00002998 const basic_string __temp(__j1, __j2, __alloc());
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002999 return replace(__i1, __i2, __temp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003000}
3001
3002template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003003inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003004basic_string<_CharT, _Traits, _Allocator>&
3005basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
3006{
3007 return replace(__pos1, __n1, __str.data(), __str.size());
3008}
3009
3010template <class _CharT, class _Traits, class _Allocator>
3011basic_string<_CharT, _Traits, _Allocator>&
3012basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
3013 size_type __pos2, size_type __n2)
3014{
3015 size_type __str_sz = __str.size();
3016 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003017 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003018 return replace(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003019}
3020
3021template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003022template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003023__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003024<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003025 __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 +00003026 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003027>
Marshall Clow82513342016-09-24 22:45:42 +00003028basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003029 size_type __pos2, size_type __n2)
3030{
Marshall Clow82513342016-09-24 22:45:42 +00003031 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003032 size_type __str_sz = __sv.size();
3033 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003034 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003035 return replace(__pos1, __n1, __sv.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
3036}
3037
3038template <class _CharT, class _Traits, class _Allocator>
3039basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003040basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003041{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003042 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003043 return replace(__pos, __n1, __s, traits_type::length(__s));
3044}
3045
3046template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003047inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003048basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003049basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003050{
3051 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
3052 __str.data(), __str.size());
3053}
3054
3055template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003056inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003057basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003058basic_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 +00003059{
3060 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
3061}
3062
3063template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003064inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003065basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003066basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003067{
3068 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
3069}
3070
3071template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003072inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003073basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003074basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003075{
3076 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
3077}
3078
3079// erase
3080
Martijn Velsa81fc792020-02-26 13:25:43 -05003081// 'externally instantiated' erase() implementation, called when __n != npos.
3082// Does not check __pos against size()
Howard Hinnantc51e1022010-05-11 19:42:16 +00003083template <class _CharT, class _Traits, class _Allocator>
Martijn Velsa81fc792020-02-26 13:25:43 -05003084void
3085basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(
3086 size_type __pos, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003087{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003088 if (__n)
3089 {
Martijn Velsa81fc792020-02-26 13:25:43 -05003090 size_type __sz = size();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003091 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003092 __n = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003093 size_type __n_move = __sz - __pos - __n;
3094 if (__n_move != 0)
3095 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003096 __null_terminate_at(__p, __sz - __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003097 }
Martijn Velsa81fc792020-02-26 13:25:43 -05003098}
3099
3100template <class _CharT, class _Traits, class _Allocator>
3101basic_string<_CharT, _Traits, _Allocator>&
3102basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos,
3103 size_type __n) {
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003104 if (__pos > size())
3105 __throw_out_of_range();
Martijn Velsa81fc792020-02-26 13:25:43 -05003106 if (__n == npos) {
3107 __erase_to_end(__pos);
3108 } else {
3109 __erase_external_with_move(__pos, __n);
3110 }
3111 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003112}
3113
3114template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003115inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003116typename basic_string<_CharT, _Traits, _Allocator>::iterator
3117basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
3118{
Nikolas Klausereed25832021-12-15 01:32:30 +01003119 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3120 "string::erase(iterator) called with an iterator not"
3121 " referring to this string");
3122
3123 _LIBCPP_ASSERT(__pos != end(), "string::erase(iterator) called with a non-dereferenceable iterator");
3124 iterator __b = begin();
3125 size_type __r = static_cast<size_type>(__pos - __b);
3126 erase(__r, 1);
3127 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003128}
3129
3130template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003131inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003132typename basic_string<_CharT, _Traits, _Allocator>::iterator
3133basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
3134{
Nikolas Klausereed25832021-12-15 01:32:30 +01003135 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
3136 "string::erase(iterator, iterator) called with an iterator not"
3137 " referring to this string");
3138
3139 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
3140 iterator __b = begin();
3141 size_type __r = static_cast<size_type>(__first - __b);
3142 erase(__r, static_cast<size_type>(__last - __first));
3143 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003144}
3145
3146template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003147inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003148void
3149basic_string<_CharT, _Traits, _Allocator>::pop_back()
3150{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003151 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003152 __erase_to_end(size() - 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003153}
3154
3155template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003156inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003157void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003158basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003159{
3160 __invalidate_all_iterators();
3161 if (__is_long())
3162 {
3163 traits_type::assign(*__get_long_pointer(), value_type());
3164 __set_long_size(0);
3165 }
3166 else
3167 {
3168 traits_type::assign(*__get_short_pointer(), value_type());
3169 __set_short_size(0);
3170 }
3171}
3172
3173template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003174inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003175void
3176basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
3177{
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003178 __null_terminate_at(_VSTD::__to_address(__get_pointer()), __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003179}
3180
3181template <class _CharT, class _Traits, class _Allocator>
3182void
3183basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
3184{
3185 size_type __sz = size();
3186 if (__n > __sz)
3187 append(__n - __sz, __c);
3188 else
3189 __erase_to_end(__n);
3190}
3191
3192template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00003193inline void
3194basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n)
3195{
3196 size_type __sz = size();
3197 if (__n > __sz) {
3198 __append_default_init(__n - __sz);
3199 } else
3200 __erase_to_end(__n);
3201}
3202
3203template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003204inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003205typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003206basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003207{
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003208 size_type __m = __alloc_traits::max_size(__alloc());
Eric Fiseliere9cc5922017-10-17 13:16:01 +00003209#ifdef _LIBCPP_BIG_ENDIAN
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003210 return (__m <= ~__long_mask ? __m : __m/2) - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003211#else
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003212 return __m - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003213#endif
3214}
3215
3216template <class _CharT, class _Traits, class _Allocator>
3217void
Marek Kurdejc9848142020-11-26 10:07:16 +01003218basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __requested_capacity)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003219{
Marek Kurdejc9848142020-11-26 10:07:16 +01003220 if (__requested_capacity > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003221 __throw_length_error();
Marek Kurdejc9848142020-11-26 10:07:16 +01003222
Louis Dionne05b6d3e2022-01-14 12:30:22 -05003223 // Make sure reserve(n) never shrinks. This is technically only required in C++20
3224 // and later (since P0966R1), however we provide consistent behavior in all Standard
3225 // modes because this function is instantiated in the shared library.
3226 if (__requested_capacity <= capacity())
3227 return;
Marek Kurdejc9848142020-11-26 10:07:16 +01003228
3229 size_type __target_capacity = _VSTD::max(__requested_capacity, size());
3230 __target_capacity = __recommend(__target_capacity);
3231 if (__target_capacity == capacity()) return;
3232
3233 __shrink_or_extend(__target_capacity);
3234}
3235
3236template <class _CharT, class _Traits, class _Allocator>
Louis Dionnee38b8642021-12-13 14:15:21 -05003237inline
Marek Kurdejc9848142020-11-26 10:07:16 +01003238void
3239basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT
3240{
3241 size_type __target_capacity = __recommend(size());
3242 if (__target_capacity == capacity()) return;
3243
3244 __shrink_or_extend(__target_capacity);
3245}
3246
3247template <class _CharT, class _Traits, class _Allocator>
Louis Dionnee38b8642021-12-13 14:15:21 -05003248inline
Marek Kurdejc9848142020-11-26 10:07:16 +01003249void
3250basic_string<_CharT, _Traits, _Allocator>::__shrink_or_extend(size_type __target_capacity)
3251{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003252 size_type __cap = capacity();
3253 size_type __sz = size();
Marek Kurdejc9848142020-11-26 10:07:16 +01003254
3255 pointer __new_data, __p;
3256 bool __was_long, __now_long;
3257 if (__target_capacity == __min_cap - 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003258 {
Marek Kurdejc9848142020-11-26 10:07:16 +01003259 __was_long = true;
3260 __now_long = false;
3261 __new_data = __get_short_pointer();
3262 __p = __get_long_pointer();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003263 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003264 else
3265 {
3266 if (__target_capacity > __cap)
3267 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3268 else
3269 {
3270 #ifndef _LIBCPP_NO_EXCEPTIONS
3271 try
3272 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003273 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003274 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3275 #ifndef _LIBCPP_NO_EXCEPTIONS
3276 }
3277 catch (...)
3278 {
3279 return;
3280 }
3281 #else // _LIBCPP_NO_EXCEPTIONS
3282 if (__new_data == nullptr)
3283 return;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003284 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003285 }
3286 __now_long = true;
3287 __was_long = __is_long();
3288 __p = __get_pointer();
3289 }
3290 traits_type::copy(_VSTD::__to_address(__new_data),
3291 _VSTD::__to_address(__p), size()+1);
3292 if (__was_long)
3293 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
3294 if (__now_long)
3295 {
3296 __set_long_cap(__target_capacity+1);
3297 __set_long_size(__sz);
3298 __set_long_pointer(__new_data);
3299 }
3300 else
3301 __set_short_size(__sz);
3302 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003303}
3304
3305template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003306inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003307typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003308basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003309{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003310 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003311 return *(data() + __pos);
3312}
3313
3314template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003315inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003316typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003317basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003318{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003319 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003320 return *(__get_pointer() + __pos);
3321}
3322
3323template <class _CharT, class _Traits, class _Allocator>
3324typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3325basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3326{
3327 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003328 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003329 return (*this)[__n];
3330}
3331
3332template <class _CharT, class _Traits, class _Allocator>
3333typename basic_string<_CharT, _Traits, _Allocator>::reference
3334basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
3335{
3336 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003337 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003338 return (*this)[__n];
3339}
3340
3341template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003342inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003343typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003344basic_string<_CharT, _Traits, _Allocator>::front() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003345{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003346 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003347 return *__get_pointer();
3348}
3349
3350template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003351inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003352typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003353basic_string<_CharT, _Traits, _Allocator>::front() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003354{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003355 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003356 return *data();
3357}
3358
3359template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003360inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003361typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003362basic_string<_CharT, _Traits, _Allocator>::back() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003363{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003364 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003365 return *(__get_pointer() + size() - 1);
3366}
3367
3368template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003369inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003370typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003371basic_string<_CharT, _Traits, _Allocator>::back() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003372{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003373 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003374 return *(data() + size() - 1);
3375}
3376
3377template <class _CharT, class _Traits, class _Allocator>
3378typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003379basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003380{
3381 size_type __sz = size();
3382 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003383 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003384 size_type __rlen = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003385 traits_type::copy(__s, data() + __pos, __rlen);
3386 return __rlen;
3387}
3388
3389template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003390inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003391basic_string<_CharT, _Traits, _Allocator>
3392basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3393{
3394 return basic_string(*this, __pos, __n, __alloc());
3395}
3396
3397template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003398inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003399void
3400basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003401#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00003402 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00003403#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00003404 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003405 __is_nothrow_swappable<allocator_type>::value)
3406#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003407{
Louis Dionneba400782020-10-02 15:02:52 -04003408#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01003409 if (!__libcpp_is_constant_evaluated()) {
3410 if (!__is_long())
3411 __get_db()->__invalidate_all(this);
3412 if (!__str.__is_long())
3413 __get_db()->__invalidate_all(&__str);
3414 __get_db()->swap(this, &__str);
3415 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00003416#endif
Eric Fiselier9bf691f2016-12-28 05:53:01 +00003417 _LIBCPP_ASSERT(
3418 __alloc_traits::propagate_on_container_swap::value ||
3419 __alloc_traits::is_always_equal::value ||
3420 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003421 _VSTD::swap(__r_.first(), __str.__r_.first());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05003422 _VSTD::__swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003423}
3424
3425// find
3426
3427template <class _Traits>
3428struct _LIBCPP_HIDDEN __traits_eq
3429{
3430 typedef typename _Traits::char_type char_type;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003431 _LIBCPP_INLINE_VISIBILITY
3432 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3433 {return _Traits::eq(__x, __y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003434};
3435
3436template<class _CharT, class _Traits, class _Allocator>
3437typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003438basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003439 size_type __pos,
3440 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003441{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003442 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003443 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003444 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003445}
3446
3447template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003448inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003449typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003450basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3451 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003452{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003453 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003454 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003455}
3456
3457template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003458template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003459__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003460<
3461 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3462 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003463>
Marshall Clowe46031a2018-07-02 18:41:15 +00003464basic_string<_CharT, _Traits, _Allocator>::find(const _Tp &__t,
zoecarver1997e0a2021-02-05 11:54:47 -08003465 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003466{
Marshall Clowe46031a2018-07-02 18:41:15 +00003467 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003468 return __str_find<value_type, size_type, traits_type, npos>
3469 (data(), size(), __sv.data(), __pos, __sv.size());
3470}
3471
3472template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003473inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003474typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003475basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003476 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003477{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003478 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003479 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003480 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003481}
3482
3483template<class _CharT, class _Traits, class _Allocator>
3484typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003485basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3486 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003487{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003488 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003489 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003490}
3491
3492// rfind
3493
3494template<class _CharT, class _Traits, class _Allocator>
3495typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003496basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003497 size_type __pos,
3498 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003499{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003500 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003501 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003502 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003503}
3504
3505template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003506inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003507typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003508basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3509 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003510{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003511 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003512 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003513}
3514
3515template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003516template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003517__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003518<
3519 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3520 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003521>
Marshall Clowe46031a2018-07-02 18:41:15 +00003522basic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003523 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003524{
Marshall Clowe46031a2018-07-02 18:41:15 +00003525 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003526 return __str_rfind<value_type, size_type, traits_type, npos>
3527 (data(), size(), __sv.data(), __pos, __sv.size());
3528}
3529
3530template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003531inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003532typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003533basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003534 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003535{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003536 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003537 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003538 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003539}
3540
3541template<class _CharT, class _Traits, class _Allocator>
3542typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003543basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3544 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003545{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003546 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003547 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003548}
3549
3550// find_first_of
3551
3552template<class _CharT, class _Traits, class _Allocator>
3553typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003554basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003555 size_type __pos,
3556 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003557{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003558 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003559 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003560 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003561}
3562
3563template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003564inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003565typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003566basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3567 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003568{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003569 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003570 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003571}
3572
3573template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003574template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003575__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003576<
3577 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3578 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003579>
Marshall Clowe46031a2018-07-02 18:41:15 +00003580basic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003581 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003582{
Marshall Clowe46031a2018-07-02 18:41:15 +00003583 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003584 return __str_find_first_of<value_type, size_type, traits_type, npos>
3585 (data(), size(), __sv.data(), __pos, __sv.size());
3586}
3587
3588template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003589inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003590typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003591basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003592 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003593{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003594 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003595 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003596 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003597}
3598
3599template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003600inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003601typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003602basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3603 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003604{
3605 return find(__c, __pos);
3606}
3607
3608// find_last_of
3609
3610template<class _CharT, class _Traits, class _Allocator>
3611typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003612basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003613 size_type __pos,
3614 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003615{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003616 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003617 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003618 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003619}
3620
3621template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003622inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003623typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003624basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3625 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003626{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003627 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003628 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003629}
3630
3631template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003632template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003633__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003634<
3635 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3636 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003637>
Marshall Clowe46031a2018-07-02 18:41:15 +00003638basic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003639 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003640{
Marshall Clowe46031a2018-07-02 18:41:15 +00003641 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003642 return __str_find_last_of<value_type, size_type, traits_type, npos>
3643 (data(), size(), __sv.data(), __pos, __sv.size());
3644}
3645
3646template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003647inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003648typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003649basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003650 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003651{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003652 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003653 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003654 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003655}
3656
3657template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003658inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003659typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003660basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3661 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003662{
3663 return rfind(__c, __pos);
3664}
3665
3666// find_first_not_of
3667
3668template<class _CharT, class _Traits, class _Allocator>
3669typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003670basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003671 size_type __pos,
3672 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003673{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003674 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003675 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003676 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003677}
3678
3679template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003680inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003681typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003682basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3683 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003684{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003685 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003686 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003687}
3688
3689template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003690template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003691__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003692<
3693 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3694 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003695>
Marshall Clowe46031a2018-07-02 18:41:15 +00003696basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003697 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003698{
Marshall Clowe46031a2018-07-02 18:41:15 +00003699 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003700 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3701 (data(), size(), __sv.data(), __pos, __sv.size());
3702}
3703
3704template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003705inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003706typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003707basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003708 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003709{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003710 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003711 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003712 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003713}
3714
3715template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003716inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003717typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003718basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3719 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003720{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003721 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003722 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003723}
3724
3725// find_last_not_of
3726
3727template<class _CharT, class _Traits, class _Allocator>
3728typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003729basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003730 size_type __pos,
3731 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003732{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003733 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003734 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003735 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003736}
3737
3738template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003739inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003740typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003741basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3742 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003743{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003744 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003745 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003746}
3747
3748template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003749template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003750__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003751<
3752 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3753 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003754>
Marshall Clowe46031a2018-07-02 18:41:15 +00003755basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003756 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003757{
Marshall Clowe46031a2018-07-02 18:41:15 +00003758 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003759 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3760 (data(), size(), __sv.data(), __pos, __sv.size());
3761}
3762
3763template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003764inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003765typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003766basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003767 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003768{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003769 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003770 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003771 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003772}
3773
3774template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003775inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003776typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003777basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3778 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003779{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003780 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003781 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003782}
3783
3784// compare
3785
3786template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003787template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003788__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003789<
3790 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3791 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003792>
zoecarver1997e0a2021-02-05 11:54:47 -08003793basic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003794{
Marshall Clowe46031a2018-07-02 18:41:15 +00003795 __self_view __sv = __t;
Howard Hinnantb0485532011-07-24 21:45:06 +00003796 size_t __lhs_sz = size();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003797 size_t __rhs_sz = __sv.size();
3798 int __result = traits_type::compare(data(), __sv.data(),
Howard Hinnantb0485532011-07-24 21:45:06 +00003799 _VSTD::min(__lhs_sz, __rhs_sz));
3800 if (__result != 0)
3801 return __result;
3802 if (__lhs_sz < __rhs_sz)
3803 return -1;
3804 if (__lhs_sz > __rhs_sz)
3805 return 1;
3806 return 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003807}
3808
3809template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003810inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003811int
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003812basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003813{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003814 return compare(__self_view(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003815}
3816
3817template <class _CharT, class _Traits, class _Allocator>
3818int
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003819basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3820 size_type __n1,
Howard Hinnantd17880b2013-06-28 16:59:19 +00003821 const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003822 size_type __n2) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003823{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003824 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003825 size_type __sz = size();
3826 if (__pos1 > __sz || __n2 == npos)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003827 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003828 size_type __rlen = _VSTD::min(__n1, __sz - __pos1);
3829 int __r = traits_type::compare(data() + __pos1, __s, _VSTD::min(__rlen, __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003830 if (__r == 0)
3831 {
3832 if (__rlen < __n2)
3833 __r = -1;
3834 else if (__rlen > __n2)
3835 __r = 1;
3836 }
3837 return __r;
3838}
3839
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003840template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003841template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003842__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003843<
3844 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3845 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003846>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003847basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3848 size_type __n1,
Marshall Clowe46031a2018-07-02 18:41:15 +00003849 const _Tp& __t) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003850{
Marshall Clowe46031a2018-07-02 18:41:15 +00003851 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003852 return compare(__pos1, __n1, __sv.data(), __sv.size());
3853}
3854
3855template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003856inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003857int
3858basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3859 size_type __n1,
3860 const basic_string& __str) const
3861{
3862 return compare(__pos1, __n1, __str.data(), __str.size());
3863}
3864
3865template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003866template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003867__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003868<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003869 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
3870 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00003871 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003872>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003873basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3874 size_type __n1,
Marshall Clow82513342016-09-24 22:45:42 +00003875 const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003876 size_type __pos2,
3877 size_type __n2) const
3878{
Marshall Clow82513342016-09-24 22:45:42 +00003879 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003880 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3881}
3882
3883template <class _CharT, class _Traits, class _Allocator>
3884int
3885basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3886 size_type __n1,
3887 const basic_string& __str,
3888 size_type __pos2,
3889 size_type __n2) const
3890{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003891 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003892}
3893
3894template <class _CharT, class _Traits, class _Allocator>
3895int
3896basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
3897{
3898 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3899 return compare(0, npos, __s, traits_type::length(__s));
3900}
3901
3902template <class _CharT, class _Traits, class _Allocator>
3903int
3904basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3905 size_type __n1,
3906 const value_type* __s) const
3907{
3908 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3909 return compare(__pos1, __n1, __s, traits_type::length(__s));
3910}
3911
Howard Hinnantc51e1022010-05-11 19:42:16 +00003912// __invariants
3913
3914template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003915inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003916bool
3917basic_string<_CharT, _Traits, _Allocator>::__invariants() const
3918{
3919 if (size() > capacity())
3920 return false;
3921 if (capacity() < __min_cap - 1)
3922 return false;
Bruce Mitchener170d8972020-11-24 12:53:53 -05003923 if (data() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003924 return false;
Louis Dionne663415f2020-10-05 16:16:13 -04003925 if (data()[size()] != value_type())
Howard Hinnantc51e1022010-05-11 19:42:16 +00003926 return false;
3927 return true;
3928}
3929
Vedant Kumar55e007e2018-03-08 21:15:26 +00003930// __clear_and_shrink
3931
3932template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003933inline
Louis Dionne173f29e2019-05-29 16:01:36 +00003934void
Marshall Clowe60a7182018-05-29 17:04:37 +00003935basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
Vedant Kumar55e007e2018-03-08 21:15:26 +00003936{
3937 clear();
3938 if(__is_long())
3939 {
3940 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
3941 __set_long_cap(0);
3942 __set_short_size(0);
Louis Dionne663415f2020-10-05 16:16:13 -04003943 traits_type::assign(*__get_short_pointer(), value_type());
Vedant Kumar55e007e2018-03-08 21:15:26 +00003944 }
Louis Dionne173f29e2019-05-29 16:01:36 +00003945}
Vedant Kumar55e007e2018-03-08 21:15:26 +00003946
Howard Hinnantc51e1022010-05-11 19:42:16 +00003947// operator==
3948
3949template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003950inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003951bool
3952operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003953 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003954{
Howard Hinnantaaeb1132013-04-22 23:55:13 +00003955 size_t __lhs_sz = __lhs.size();
3956 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
3957 __rhs.data(),
3958 __lhs_sz) == 0;
3959}
3960
3961template<class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003962inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaaeb1132013-04-22 23:55:13 +00003963bool
3964operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
3965 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
3966{
3967 size_t __lhs_sz = __lhs.size();
3968 if (__lhs_sz != __rhs.size())
3969 return false;
3970 const char* __lp = __lhs.data();
3971 const char* __rp = __rhs.data();
3972 if (__lhs.__is_long())
3973 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
3974 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
3975 if (*__lp != *__rp)
3976 return false;
3977 return true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003978}
3979
3980template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003981inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003982bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003983operator==(const _CharT* __lhs,
3984 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003985{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00003986 typedef basic_string<_CharT, _Traits, _Allocator> _String;
3987 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
3988 size_t __lhs_len = _Traits::length(__lhs);
3989 if (__lhs_len != __rhs.size()) return false;
3990 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003991}
3992
Howard Hinnantc51e1022010-05-11 19:42:16 +00003993template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003994inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003995bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003996operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
3997 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003998{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00003999 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4000 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
4001 size_t __rhs_len = _Traits::length(__rhs);
4002 if (__rhs_len != __lhs.size()) return false;
4003 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004004}
4005
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004006template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004007inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004008bool
4009operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004010 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004011{
4012 return !(__lhs == __rhs);
4013}
4014
4015template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004016inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004017bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004018operator!=(const _CharT* __lhs,
4019 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004020{
4021 return !(__lhs == __rhs);
4022}
4023
4024template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004025inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004026bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004027operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4028 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004029{
4030 return !(__lhs == __rhs);
4031}
4032
4033// operator<
4034
4035template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004036inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004037bool
4038operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004039 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004040{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004041 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004042}
4043
4044template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004045inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004046bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004047operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4048 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004049{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004050 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004051}
4052
4053template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004054inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004055bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004056operator< (const _CharT* __lhs,
4057 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004058{
4059 return __rhs.compare(__lhs) > 0;
4060}
4061
Howard Hinnantc51e1022010-05-11 19:42:16 +00004062// operator>
4063
4064template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004065inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004066bool
4067operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004068 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004069{
4070 return __rhs < __lhs;
4071}
4072
4073template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004074inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004075bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004076operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4077 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004078{
4079 return __rhs < __lhs;
4080}
4081
4082template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004083inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004084bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004085operator> (const _CharT* __lhs,
4086 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004087{
4088 return __rhs < __lhs;
4089}
4090
4091// operator<=
4092
4093template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004094inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004095bool
4096operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004097 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004098{
4099 return !(__rhs < __lhs);
4100}
4101
4102template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004103inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004104bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004105operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4106 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004107{
4108 return !(__rhs < __lhs);
4109}
4110
4111template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004112inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004113bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004114operator<=(const _CharT* __lhs,
4115 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004116{
4117 return !(__rhs < __lhs);
4118}
4119
4120// operator>=
4121
4122template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004123inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004124bool
4125operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004126 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004127{
4128 return !(__lhs < __rhs);
4129}
4130
4131template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004132inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004133bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004134operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4135 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004136{
4137 return !(__lhs < __rhs);
4138}
4139
4140template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004141inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004142bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004143operator>=(const _CharT* __lhs,
4144 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004145{
4146 return !(__lhs < __rhs);
4147}
4148
4149// operator +
4150
4151template<class _CharT, class _Traits, class _Allocator>
4152basic_string<_CharT, _Traits, _Allocator>
4153operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4154 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4155{
4156 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4157 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4158 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4159 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4160 __r.append(__rhs.data(), __rhs_sz);
4161 return __r;
4162}
4163
4164template<class _CharT, class _Traits, class _Allocator>
4165basic_string<_CharT, _Traits, _Allocator>
4166operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4167{
4168 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4169 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = _Traits::length(__lhs);
4170 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4171 __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz);
4172 __r.append(__rhs.data(), __rhs_sz);
4173 return __r;
4174}
4175
4176template<class _CharT, class _Traits, class _Allocator>
4177basic_string<_CharT, _Traits, _Allocator>
4178operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4179{
4180 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4181 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4182 __r.__init(&__lhs, 1, 1 + __rhs_sz);
4183 __r.append(__rhs.data(), __rhs_sz);
4184 return __r;
4185}
4186
4187template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004188inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004189basic_string<_CharT, _Traits, _Allocator>
4190operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
4191{
4192 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4193 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4194 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = _Traits::length(__rhs);
4195 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4196 __r.append(__rhs, __rhs_sz);
4197 return __r;
4198}
4199
4200template<class _CharT, class _Traits, class _Allocator>
4201basic_string<_CharT, _Traits, _Allocator>
4202operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
4203{
4204 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4205 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4206 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1);
4207 __r.push_back(__rhs);
4208 return __r;
4209}
4210
Eric Fiselierfc92be82017-04-19 00:28:44 +00004211#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004212
4213template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004214inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004215basic_string<_CharT, _Traits, _Allocator>
4216operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4217{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004218 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004219}
4220
4221template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004222inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004223basic_string<_CharT, _Traits, _Allocator>
4224operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4225{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004226 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004227}
4228
4229template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004230inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004231basic_string<_CharT, _Traits, _Allocator>
4232operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4233{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004234 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004235}
4236
4237template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004238inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004239basic_string<_CharT, _Traits, _Allocator>
4240operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4241{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004242 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004243}
4244
4245template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004246inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004247basic_string<_CharT, _Traits, _Allocator>
4248operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4249{
4250 __rhs.insert(__rhs.begin(), __lhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004251 return _VSTD::move(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004252}
4253
4254template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004255inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004256basic_string<_CharT, _Traits, _Allocator>
4257operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
4258{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004259 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004260}
4261
4262template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004263inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004264basic_string<_CharT, _Traits, _Allocator>
4265operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
4266{
4267 __lhs.push_back(__rhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004268 return _VSTD::move(__lhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004269}
4270
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004271#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004272
4273// swap
4274
4275template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004276inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004277void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004278swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +00004279 basic_string<_CharT, _Traits, _Allocator>& __rhs)
4280 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004281{
4282 __lhs.swap(__rhs);
4283}
4284
Bruce Mitchener170d8972020-11-24 12:53:53 -05004285_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = nullptr, int __base = 10);
4286_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = nullptr, int __base = 10);
4287_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = nullptr, int __base = 10);
4288_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = nullptr, int __base = 10);
4289_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004290
Bruce Mitchener170d8972020-11-24 12:53:53 -05004291_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = nullptr);
4292_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = nullptr);
4293_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004294
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004295_LIBCPP_FUNC_VIS string to_string(int __val);
4296_LIBCPP_FUNC_VIS string to_string(unsigned __val);
4297_LIBCPP_FUNC_VIS string to_string(long __val);
4298_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
4299_LIBCPP_FUNC_VIS string to_string(long long __val);
4300_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
4301_LIBCPP_FUNC_VIS string to_string(float __val);
4302_LIBCPP_FUNC_VIS string to_string(double __val);
4303_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004304
Louis Dionne89258142021-08-23 15:32:36 -04004305#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Bruce Mitchener170d8972020-11-24 12:53:53 -05004306_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4307_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4308_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4309_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4310_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004311
Bruce Mitchener170d8972020-11-24 12:53:53 -05004312_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = nullptr);
4313_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = nullptr);
4314_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004315
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004316_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
4317_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
4318_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
4319_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
4320_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
4321_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
4322_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
4323_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
4324_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Louis Dionne89258142021-08-23 15:32:36 -04004325#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004326
Howard Hinnantc51e1022010-05-11 19:42:16 +00004327template<class _CharT, class _Traits, class _Allocator>
Martin Storsjö88890c22021-02-22 01:13:13 +02004328_LIBCPP_TEMPLATE_DATA_VIS
Eric Fiselierc9fdaf12020-01-15 17:02:17 -05004329const typename basic_string<_CharT, _Traits, _Allocator>::size_type
4330 basic_string<_CharT, _Traits, _Allocator>::npos;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004331
Marshall Clow851b9ec2019-05-20 21:56:51 +00004332template <class _CharT, class _Allocator>
4333struct _LIBCPP_TEMPLATE_VIS
4334 hash<basic_string<_CharT, char_traits<_CharT>, _Allocator> >
4335 : public unary_function<
4336 basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004337{
4338 size_t
Marshall Clow851b9ec2019-05-20 21:56:51 +00004339 operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT
4340 { return __do_string_hash(__val.data(), __val.data() + __val.size()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004341};
4342
Howard Hinnantc51e1022010-05-11 19:42:16 +00004343
Howard Hinnantdc095972011-07-18 15:51:59 +00004344template<class _CharT, class _Traits, class _Allocator>
4345basic_ostream<_CharT, _Traits>&
4346operator<<(basic_ostream<_CharT, _Traits>& __os,
4347 const basic_string<_CharT, _Traits, _Allocator>& __str);
4348
4349template<class _CharT, class _Traits, class _Allocator>
4350basic_istream<_CharT, _Traits>&
4351operator>>(basic_istream<_CharT, _Traits>& __is,
4352 basic_string<_CharT, _Traits, _Allocator>& __str);
4353
4354template<class _CharT, class _Traits, class _Allocator>
4355basic_istream<_CharT, _Traits>&
4356getline(basic_istream<_CharT, _Traits>& __is,
4357 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4358
4359template<class _CharT, class _Traits, class _Allocator>
4360inline _LIBCPP_INLINE_VISIBILITY
4361basic_istream<_CharT, _Traits>&
4362getline(basic_istream<_CharT, _Traits>& __is,
4363 basic_string<_CharT, _Traits, _Allocator>& __str);
4364
Howard Hinnantdc095972011-07-18 15:51:59 +00004365template<class _CharT, class _Traits, class _Allocator>
4366inline _LIBCPP_INLINE_VISIBILITY
4367basic_istream<_CharT, _Traits>&
4368getline(basic_istream<_CharT, _Traits>&& __is,
4369 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4370
4371template<class _CharT, class _Traits, class _Allocator>
4372inline _LIBCPP_INLINE_VISIBILITY
4373basic_istream<_CharT, _Traits>&
4374getline(basic_istream<_CharT, _Traits>&& __is,
4375 basic_string<_CharT, _Traits, _Allocator>& __str);
4376
Marshall Clow29b53f22018-12-14 18:49:35 +00004377#if _LIBCPP_STD_VER > 17
Marek Kurdeja98b1412020-05-02 13:58:03 +02004378template <class _CharT, class _Traits, class _Allocator, class _Up>
Marshall Clow29b53f22018-12-14 18:49:35 +00004379inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004380 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4381 erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
4382 auto __old_size = __str.size();
4383 __str.erase(_VSTD::remove(__str.begin(), __str.end(), __v), __str.end());
4384 return __old_size - __str.size();
4385}
Marshall Clow29b53f22018-12-14 18:49:35 +00004386
Marek Kurdeja98b1412020-05-02 13:58:03 +02004387template <class _CharT, class _Traits, class _Allocator, class _Predicate>
Marshall Clow29b53f22018-12-14 18:49:35 +00004388inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004389 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4390 erase_if(basic_string<_CharT, _Traits, _Allocator>& __str,
4391 _Predicate __pred) {
4392 auto __old_size = __str.size();
4393 __str.erase(_VSTD::remove_if(__str.begin(), __str.end(), __pred),
4394 __str.end());
4395 return __old_size - __str.size();
4396}
Marshall Clow29b53f22018-12-14 18:49:35 +00004397#endif
4398
Louis Dionneba400782020-10-02 15:02:52 -04004399#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004400
4401template<class _CharT, class _Traits, class _Allocator>
4402bool
4403basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4404{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004405 return data() <= _VSTD::__to_address(__i->base()) &&
4406 _VSTD::__to_address(__i->base()) < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004407}
4408
4409template<class _CharT, class _Traits, class _Allocator>
4410bool
4411basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4412{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004413 return data() < _VSTD::__to_address(__i->base()) &&
4414 _VSTD::__to_address(__i->base()) <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004415}
4416
4417template<class _CharT, class _Traits, class _Allocator>
4418bool
4419basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4420{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004421 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004422 return data() <= __p && __p <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004423}
4424
4425template<class _CharT, class _Traits, class _Allocator>
4426bool
4427basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4428{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004429 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004430 return data() <= __p && __p < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004431}
4432
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004433#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004434
Louis Dionne173f29e2019-05-29 16:01:36 +00004435#if _LIBCPP_STD_VER > 11
Marshall Clowcba751f2013-07-23 17:05:24 +00004436// Literal suffixes for basic_string [basic.string.literals]
Marshall Clowac868372013-10-05 21:18:32 +00004437inline namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004438{
4439 inline namespace string_literals
4440 {
Howard Hinnant5c167562013-08-07 19:39:48 +00004441 inline _LIBCPP_INLINE_VISIBILITY
4442 basic_string<char> operator "" s( const char *__str, size_t __len )
4443 {
4444 return basic_string<char> (__str, __len);
4445 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004446
Louis Dionne89258142021-08-23 15:32:36 -04004447#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnant5c167562013-08-07 19:39:48 +00004448 inline _LIBCPP_INLINE_VISIBILITY
4449 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4450 {
4451 return basic_string<wchar_t> (__str, __len);
4452 }
Louis Dionne89258142021-08-23 15:32:36 -04004453#endif
Marshall Clowcba751f2013-07-23 17:05:24 +00004454
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -04004455#ifndef _LIBCPP_HAS_NO_CHAR8_T
Marshall Clow8732fed2018-12-11 04:35:44 +00004456 inline _LIBCPP_INLINE_VISIBILITY
4457 basic_string<char8_t> operator "" s(const char8_t *__str, size_t __len) _NOEXCEPT
4458 {
4459 return basic_string<char8_t> (__str, __len);
4460 }
4461#endif
4462
Howard Hinnant5c167562013-08-07 19:39:48 +00004463 inline _LIBCPP_INLINE_VISIBILITY
4464 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4465 {
4466 return basic_string<char16_t> (__str, __len);
4467 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004468
Howard Hinnant5c167562013-08-07 19:39:48 +00004469 inline _LIBCPP_INLINE_VISIBILITY
4470 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4471 {
4472 return basic_string<char32_t> (__str, __len);
4473 }
Nikolas Klauserd26407a2021-12-02 14:12:51 +01004474 } // namespace string_literals
4475} // namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004476#endif
4477
Howard Hinnantc51e1022010-05-11 19:42:16 +00004478_LIBCPP_END_NAMESPACE_STD
4479
Eric Fiselierf4433a32017-05-31 22:07:49 +00004480_LIBCPP_POP_MACROS
4481
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004482#endif // _LIBCPP_STRING