blob: 3e7f2e9fe6aa34bdff5a55252511f98a3361e67f [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
Louis Dionne9bd93882021-11-17 16:25:01 -05002//===----------------------------------------------------------------------===//
Howard Hinnantc51e1022010-05-11 19:42:16 +00003//
Chandler Carruth7642bb12019-01-19 08:50:56 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_STRING
11#define _LIBCPP_STRING
12
13/*
14 string synopsis
15
16namespace std
17{
18
19template <class stateT>
20class fpos
21{
22private:
23 stateT st;
24public:
25 fpos(streamoff = streamoff());
26
27 operator streamoff() const;
28
29 stateT state() const;
30 void state(stateT);
31
32 fpos& operator+=(streamoff);
33 fpos operator+ (streamoff) const;
34 fpos& operator-=(streamoff);
35 fpos operator- (streamoff) const;
36};
37
38template <class stateT> streamoff operator-(const fpos<stateT>& x, const fpos<stateT>& y);
39
40template <class stateT> bool operator==(const fpos<stateT>& x, const fpos<stateT>& y);
41template <class stateT> bool operator!=(const fpos<stateT>& x, const fpos<stateT>& y);
42
43template <class charT>
44struct char_traits
45{
46 typedef charT char_type;
47 typedef ... int_type;
48 typedef streamoff off_type;
49 typedef streampos pos_type;
50 typedef mbstate_t state_type;
51
Howard Hinnantaeb17d82011-05-29 19:57:12 +000052 static void assign(char_type& c1, const char_type& c2) noexcept;
Howard Hinnant270c00e2012-07-20 19:09:12 +000053 static constexpr bool eq(char_type c1, char_type c2) noexcept;
54 static constexpr bool lt(char_type c1, char_type c2) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000055
56 static int compare(const char_type* s1, const char_type* s2, size_t n);
57 static size_t length(const char_type* s);
58 static const char_type* find(const char_type* s, size_t n, const char_type& a);
59 static char_type* move(char_type* s1, const char_type* s2, size_t n);
60 static char_type* copy(char_type* s1, const char_type* s2, size_t n);
61 static char_type* assign(char_type* s, size_t n, char_type a);
62
Howard Hinnant270c00e2012-07-20 19:09:12 +000063 static constexpr int_type not_eof(int_type c) noexcept;
64 static constexpr char_type to_char_type(int_type c) noexcept;
65 static constexpr int_type to_int_type(char_type c) noexcept;
66 static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept;
67 static constexpr int_type eof() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000068};
69
70template <> struct char_traits<char>;
71template <> struct char_traits<wchar_t>;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +010072template <> struct char_traits<char8_t>; // C++20
73template <> struct char_traits<char16_t>;
74template <> struct char_traits<char32_t>;
Howard Hinnantc51e1022010-05-11 19:42:16 +000075
Howard Hinnant3b6579a2010-08-22 00:02:43 +000076template<class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
Howard Hinnantc51e1022010-05-11 19:42:16 +000077class basic_string
78{
Howard Hinnant3b6579a2010-08-22 00:02:43 +000079public:
80// types:
Howard Hinnantc51e1022010-05-11 19:42:16 +000081 typedef traits traits_type;
82 typedef typename traits_type::char_type value_type;
83 typedef Allocator allocator_type;
84 typedef typename allocator_type::size_type size_type;
85 typedef typename allocator_type::difference_type difference_type;
86 typedef typename allocator_type::reference reference;
87 typedef typename allocator_type::const_reference const_reference;
88 typedef typename allocator_type::pointer pointer;
89 typedef typename allocator_type::const_pointer const_pointer;
90 typedef implementation-defined iterator;
91 typedef implementation-defined const_iterator;
92 typedef std::reverse_iterator<iterator> reverse_iterator;
93 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
94
95 static const size_type npos = -1;
96
Howard Hinnant3e276872011-06-03 18:40:47 +000097 basic_string()
98 noexcept(is_nothrow_default_constructible<allocator_type>::value);
99 explicit basic_string(const allocator_type& a);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000100 basic_string(const basic_string& str);
Howard Hinnant3e276872011-06-03 18:40:47 +0000101 basic_string(basic_string&& str)
102 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clow83445802016-04-07 18:13:41 +0000103 basic_string(const basic_string& str, size_type pos,
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000104 const allocator_type& a = allocator_type());
Marshall Clow83445802016-04-07 18:13:41 +0000105 basic_string(const basic_string& str, size_type pos, size_type n,
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000106 const Allocator& a = Allocator());
Marshall Clow78dbe462016-11-14 18:22:19 +0000107 template<class T>
108 basic_string(const T& t, size_type pos, size_type n, const Allocator& a = Allocator()); // C++17
Marshall Clowe46031a2018-07-02 18:41:15 +0000109 template <class T>
110 explicit basic_string(const T& t, const Allocator& a = Allocator()); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000111 basic_string(const value_type* s, const allocator_type& a = allocator_type());
112 basic_string(const value_type* s, size_type n, const allocator_type& a = allocator_type());
Marek Kurdej90d79712021-07-27 16:16:21 +0200113 basic_string(nullptr_t) = delete; // C++2b
Howard Hinnantc51e1022010-05-11 19:42:16 +0000114 basic_string(size_type n, value_type c, const allocator_type& a = allocator_type());
115 template<class InputIterator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000116 basic_string(InputIterator begin, InputIterator end,
117 const allocator_type& a = allocator_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000118 basic_string(initializer_list<value_type>, const Allocator& = Allocator());
119 basic_string(const basic_string&, const Allocator&);
120 basic_string(basic_string&&, const Allocator&);
121
122 ~basic_string();
123
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000124 operator basic_string_view<charT, traits>() const noexcept;
125
Howard Hinnantc51e1022010-05-11 19:42:16 +0000126 basic_string& operator=(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000127 template <class T>
128 basic_string& operator=(const T& t); // C++17
Howard Hinnant3e276872011-06-03 18:40:47 +0000129 basic_string& operator=(basic_string&& str)
130 noexcept(
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000131 allocator_type::propagate_on_container_move_assignment::value ||
132 allocator_type::is_always_equal::value ); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000133 basic_string& operator=(const value_type* s);
Marek Kurdej90d79712021-07-27 16:16:21 +0200134 basic_string& operator=(nullptr_t) = delete; // C++2b
Howard Hinnantc51e1022010-05-11 19:42:16 +0000135 basic_string& operator=(value_type c);
136 basic_string& operator=(initializer_list<value_type>);
137
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000138 iterator begin() noexcept;
139 const_iterator begin() const noexcept;
140 iterator end() noexcept;
141 const_iterator end() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000142
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000143 reverse_iterator rbegin() noexcept;
144 const_reverse_iterator rbegin() const noexcept;
145 reverse_iterator rend() noexcept;
146 const_reverse_iterator rend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000147
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000148 const_iterator cbegin() const noexcept;
149 const_iterator cend() const noexcept;
150 const_reverse_iterator crbegin() const noexcept;
151 const_reverse_iterator crend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000152
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000153 size_type size() const noexcept;
154 size_type length() const noexcept;
155 size_type max_size() const noexcept;
156 size_type capacity() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000157
158 void resize(size_type n, value_type c);
159 void resize(size_type n);
160
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100161 template<class Operation>
162 constexpr void resize_and_overwrite(size_type n, Operation op); // since C++23
163
Marek Kurdejc9848142020-11-26 10:07:16 +0100164 void reserve(size_type res_arg);
165 void reserve(); // deprecated in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000166 void shrink_to_fit();
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000167 void clear() noexcept;
168 bool empty() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000169
170 const_reference operator[](size_type pos) const;
171 reference operator[](size_type pos);
172
173 const_reference at(size_type n) const;
174 reference at(size_type n);
175
176 basic_string& operator+=(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000177 template <class T>
178 basic_string& operator+=(const T& t); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000179 basic_string& operator+=(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000180 basic_string& operator+=(value_type c);
181 basic_string& operator+=(initializer_list<value_type>);
182
183 basic_string& append(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000184 template <class T>
185 basic_string& append(const T& t); // C++17
Marshall Clow8db7fb02014-03-04 19:17:19 +0000186 basic_string& append(const basic_string& str, size_type pos, size_type n=npos); //C++14
Marshall Clow82513342016-09-24 22:45:42 +0000187 template <class T>
188 basic_string& append(const T& t, size_type pos, size_type n=npos); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000189 basic_string& append(const value_type* s, size_type n);
190 basic_string& append(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000191 basic_string& append(size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000192 template<class InputIterator>
193 basic_string& append(InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000194 basic_string& append(initializer_list<value_type>);
195
196 void push_back(value_type c);
197 void pop_back();
198 reference front();
199 const_reference front() const;
200 reference back();
201 const_reference back() const;
202
203 basic_string& assign(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000204 template <class T>
205 basic_string& assign(const T& t); // C++17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000206 basic_string& assign(basic_string&& str);
Marshall Clow8db7fb02014-03-04 19:17:19 +0000207 basic_string& assign(const basic_string& str, size_type pos, size_type n=npos); // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000208 template <class T>
209 basic_string& assign(const T& t, size_type pos, size_type n=npos); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000210 basic_string& assign(const value_type* s, size_type n);
211 basic_string& assign(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000212 basic_string& assign(size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000213 template<class InputIterator>
214 basic_string& assign(InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000215 basic_string& assign(initializer_list<value_type>);
216
217 basic_string& insert(size_type pos1, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000218 template <class T>
219 basic_string& insert(size_type pos1, const T& t);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000220 basic_string& insert(size_type pos1, const basic_string& str,
221 size_type pos2, size_type n);
Marshall Clow82513342016-09-24 22:45:42 +0000222 template <class T>
223 basic_string& insert(size_type pos1, const T& t, size_type pos2, size_type n); // C++17
Marshall Clow8db7fb02014-03-04 19:17:19 +0000224 basic_string& insert(size_type pos, const value_type* s, size_type n=npos); //C++14
Howard Hinnantd17880b2013-06-28 16:59:19 +0000225 basic_string& insert(size_type pos, const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000226 basic_string& insert(size_type pos, size_type n, value_type c);
227 iterator insert(const_iterator p, value_type c);
228 iterator insert(const_iterator p, size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000229 template<class InputIterator>
230 iterator insert(const_iterator p, InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000231 iterator insert(const_iterator p, initializer_list<value_type>);
232
233 basic_string& erase(size_type pos = 0, size_type n = npos);
234 iterator erase(const_iterator position);
235 iterator erase(const_iterator first, const_iterator last);
236
237 basic_string& replace(size_type pos1, size_type n1, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000238 template <class T>
239 basic_string& replace(size_type pos1, size_type n1, const T& t); // C++17
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000240 basic_string& replace(size_type pos1, size_type n1, const basic_string& str,
Marshall Clow8db7fb02014-03-04 19:17:19 +0000241 size_type pos2, size_type n2=npos); // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000242 template <class T>
243 basic_string& replace(size_type pos1, size_type n1, const T& t,
244 size_type pos2, size_type n); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000245 basic_string& replace(size_type pos, size_type n1, const value_type* s, size_type n2);
246 basic_string& replace(size_type pos, size_type n1, const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000247 basic_string& replace(size_type pos, size_type n1, size_type n2, value_type c);
Howard Hinnant990d6e82010-11-17 21:11:40 +0000248 basic_string& replace(const_iterator i1, const_iterator i2, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000249 template <class T>
250 basic_string& replace(const_iterator i1, const_iterator i2, const T& t); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000251 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s, size_type n);
252 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s);
Howard Hinnant990d6e82010-11-17 21:11:40 +0000253 basic_string& replace(const_iterator i1, const_iterator i2, size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000254 template<class InputIterator>
Howard Hinnant990d6e82010-11-17 21:11:40 +0000255 basic_string& replace(const_iterator i1, const_iterator i2, InputIterator j1, InputIterator j2);
256 basic_string& replace(const_iterator i1, const_iterator i2, initializer_list<value_type>);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000257
Howard Hinnantd17880b2013-06-28 16:59:19 +0000258 size_type copy(value_type* s, size_type n, size_type pos = 0) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000259 basic_string substr(size_type pos = 0, size_type n = npos) const;
260
Howard Hinnant3e276872011-06-03 18:40:47 +0000261 void swap(basic_string& str)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000262 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
263 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000264
Howard Hinnantd17880b2013-06-28 16:59:19 +0000265 const value_type* c_str() const noexcept;
266 const value_type* data() const noexcept;
Marshall Clowd3c22392016-03-08 15:44:30 +0000267 value_type* data() noexcept; // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000268
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000269 allocator_type get_allocator() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000270
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000271 size_type find(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000272 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800273 size_type find(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000274 size_type find(const value_type* s, size_type pos, size_type n) const noexcept;
275 size_type find(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000276 size_type find(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000277
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000278 size_type rfind(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000279 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800280 size_type rfind(const T& t, size_type pos = npos) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000281 size_type rfind(const value_type* s, size_type pos, size_type n) const noexcept;
282 size_type rfind(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000283 size_type rfind(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000284
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000285 size_type find_first_of(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000286 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800287 size_type find_first_of(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000288 size_type find_first_of(const value_type* s, size_type pos, size_type n) const noexcept;
289 size_type find_first_of(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000290 size_type find_first_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000291
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000292 size_type find_last_of(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000293 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800294 size_type find_last_of(const T& t, size_type pos = npos) const noexcept noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000295 size_type find_last_of(const value_type* s, size_type pos, size_type n) const noexcept;
296 size_type find_last_of(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000297 size_type find_last_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000298
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000299 size_type find_first_not_of(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000300 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800301 size_type find_first_not_of(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000302 size_type find_first_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
303 size_type find_first_not_of(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000304 size_type find_first_not_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000305
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000306 size_type find_last_not_of(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000307 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800308 size_type find_last_not_of(const T& t, size_type pos = npos) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000309 size_type find_last_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
310 size_type find_last_not_of(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000311 size_type find_last_not_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000312
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000313 int compare(const basic_string& str) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000314 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800315 int compare(const T& t) const noexcept; // C++17, noexcept as an extension
Howard Hinnantc51e1022010-05-11 19:42:16 +0000316 int compare(size_type pos1, size_type n1, const basic_string& str) const;
Marshall Clowe46031a2018-07-02 18:41:15 +0000317 template <class T>
318 int compare(size_type pos1, size_type n1, const T& t) const; // C++17
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000319 int compare(size_type pos1, size_type n1, const basic_string& str,
Marshall Clow8db7fb02014-03-04 19:17:19 +0000320 size_type pos2, size_type n2=npos) const; // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000321 template <class T>
322 int compare(size_type pos1, size_type n1, const T& t,
323 size_type pos2, size_type n2=npos) const; // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000324 int compare(const value_type* s) const noexcept;
325 int compare(size_type pos1, size_type n1, const value_type* s) const;
326 int compare(size_type pos1, size_type n1, const value_type* s, size_type n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000327
Marek Kurdej24b4c512021-01-07 12:29:04 +0100328 bool starts_with(basic_string_view<charT, traits> sv) const noexcept; // C++20
329 bool starts_with(charT c) const noexcept; // C++20
330 bool starts_with(const charT* s) const; // C++20
331 bool ends_with(basic_string_view<charT, traits> sv) const noexcept; // C++20
332 bool ends_with(charT c) const noexcept; // C++20
333 bool ends_with(const charT* s) const; // C++20
Marshall Clow18c293b2017-12-04 20:11:38 +0000334
Wim Leflere023c3542021-01-19 14:33:30 -0500335 constexpr bool contains(basic_string_view<charT, traits> sv) const noexcept; // C++2b
336 constexpr bool contains(charT c) const noexcept; // C++2b
337 constexpr bool contains(const charT* s) const; // C++2b
338
Howard Hinnantc51e1022010-05-11 19:42:16 +0000339 bool __invariants() const;
340};
341
Marshall Clowa0563332018-02-08 06:34:03 +0000342template<class InputIterator,
343 class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
344basic_string(InputIterator, InputIterator, Allocator = Allocator())
345 -> basic_string<typename iterator_traits<InputIterator>::value_type,
346 char_traits<typename iterator_traits<InputIterator>::value_type>,
347 Allocator>; // C++17
348
Howard Hinnantc51e1022010-05-11 19:42:16 +0000349template<class charT, class traits, class Allocator>
350basic_string<charT, traits, Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000351operator+(const basic_string<charT, traits, Allocator>& lhs,
352 const basic_string<charT, traits, Allocator>& rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000353
354template<class charT, class traits, class Allocator>
355basic_string<charT, traits, Allocator>
356operator+(const charT* lhs , const basic_string<charT,traits,Allocator>&rhs);
357
358template<class charT, class traits, class Allocator>
359basic_string<charT, traits, Allocator>
360operator+(charT lhs, const basic_string<charT,traits,Allocator>& rhs);
361
362template<class charT, class traits, class Allocator>
363basic_string<charT, traits, Allocator>
364operator+(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs);
365
366template<class charT, class traits, class Allocator>
367basic_string<charT, traits, Allocator>
368operator+(const basic_string<charT, traits, Allocator>& lhs, charT rhs);
369
370template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000371bool operator==(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000372 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000373
374template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000375bool operator==(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000376
377template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000378bool operator==(const basic_string<charT,traits,Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000379
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000380template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000381bool operator!=(const basic_string<charT,traits,Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000382 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000383
384template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000385bool operator!=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000386
387template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000388bool operator!=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000389
390template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000391bool operator< (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000392 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000393
394template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000395bool operator< (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000396
397template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000398bool operator< (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000399
400template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000401bool operator> (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000402 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000403
404template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000405bool operator> (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000406
407template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000408bool operator> (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000409
410template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000411bool operator<=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000412 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000413
414template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000415bool operator<=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000416
417template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000418bool operator<=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000419
420template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000421bool operator>=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000422 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000423
424template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000425bool operator>=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000426
427template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000428bool operator>=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000429
430template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000431void swap(basic_string<charT, traits, Allocator>& lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +0000432 basic_string<charT, traits, Allocator>& rhs)
433 noexcept(noexcept(lhs.swap(rhs)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000434
435template<class charT, class traits, class Allocator>
436basic_istream<charT, traits>&
437operator>>(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
438
439template<class charT, class traits, class Allocator>
440basic_ostream<charT, traits>&
441operator<<(basic_ostream<charT, traits>& os, const basic_string<charT, traits, Allocator>& str);
442
443template<class charT, class traits, class Allocator>
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000444basic_istream<charT, traits>&
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000445getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str,
446 charT delim);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000447
448template<class charT, class traits, class Allocator>
449basic_istream<charT, traits>&
450getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
451
Marshall Clow29b53f22018-12-14 18:49:35 +0000452template<class charT, class traits, class Allocator, class U>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200453typename basic_string<charT, traits, Allocator>::size_type
454erase(basic_string<charT, traits, Allocator>& c, const U& value); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000455template<class charT, class traits, class Allocator, class Predicate>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200456typename basic_string<charT, traits, Allocator>::size_type
457erase_if(basic_string<charT, traits, Allocator>& c, Predicate pred); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000458
Howard Hinnantc51e1022010-05-11 19:42:16 +0000459typedef basic_string<char> string;
460typedef basic_string<wchar_t> wstring;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100461typedef basic_string<char8_t> u8string; // C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000462typedef basic_string<char16_t> u16string;
463typedef basic_string<char32_t> u32string;
464
Bruce Mitchener170d8972020-11-24 12:53:53 -0500465int stoi (const string& str, size_t* idx = nullptr, int base = 10);
466long stol (const string& str, size_t* idx = nullptr, int base = 10);
467unsigned long stoul (const string& str, size_t* idx = nullptr, int base = 10);
468long long stoll (const string& str, size_t* idx = nullptr, int base = 10);
469unsigned long long stoull(const string& str, size_t* idx = nullptr, int base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000470
Bruce Mitchener170d8972020-11-24 12:53:53 -0500471float stof (const string& str, size_t* idx = nullptr);
472double stod (const string& str, size_t* idx = nullptr);
473long double stold(const string& str, size_t* idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000474
475string to_string(int val);
476string to_string(unsigned val);
477string to_string(long val);
478string to_string(unsigned long val);
479string to_string(long long val);
480string to_string(unsigned long long val);
481string to_string(float val);
482string to_string(double val);
483string to_string(long double val);
484
Bruce Mitchener170d8972020-11-24 12:53:53 -0500485int stoi (const wstring& str, size_t* idx = nullptr, int base = 10);
486long stol (const wstring& str, size_t* idx = nullptr, int base = 10);
487unsigned long stoul (const wstring& str, size_t* idx = nullptr, int base = 10);
488long long stoll (const wstring& str, size_t* idx = nullptr, int base = 10);
489unsigned long long stoull(const wstring& str, size_t* idx = nullptr, int base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000490
Bruce Mitchener170d8972020-11-24 12:53:53 -0500491float stof (const wstring& str, size_t* idx = nullptr);
492double stod (const wstring& str, size_t* idx = nullptr);
493long double stold(const wstring& str, size_t* idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000494
495wstring to_wstring(int val);
496wstring to_wstring(unsigned val);
497wstring to_wstring(long val);
498wstring to_wstring(unsigned long val);
499wstring to_wstring(long long val);
500wstring to_wstring(unsigned long long val);
501wstring to_wstring(float val);
502wstring to_wstring(double val);
503wstring to_wstring(long double val);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000504
505template <> struct hash<string>;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100506template <> struct hash<u8string>; // C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000507template <> struct hash<u16string>;
508template <> struct hash<u32string>;
509template <> struct hash<wstring>;
510
Marshall Clowcba751f2013-07-23 17:05:24 +0000511basic_string<char> operator "" s( const char *str, size_t len ); // C++14
512basic_string<wchar_t> operator "" s( const wchar_t *str, size_t len ); // C++14
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100513basic_string<char8_t> operator "" s( const char8_t *str, size_t len ); // C++20
Marshall Clowcba751f2013-07-23 17:05:24 +0000514basic_string<char16_t> operator "" s( const char16_t *str, size_t len ); // C++14
515basic_string<char32_t> operator "" s( const char32_t *str, size_t len ); // C++14
516
Howard Hinnantc51e1022010-05-11 19:42:16 +0000517} // std
518
519*/
520
Nikolas Klauserf210d8a2022-02-15 18:18:08 +0100521#include <__algorithm/max.h>
522#include <__algorithm/min.h>
523#include <__algorithm/remove.h>
524#include <__algorithm/remove_if.h>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000525#include <__config>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400526#include <__debug>
Nikolas Klauser80840272022-02-04 13:04:33 +0100527#include <__ios/fpos.h>
Louis Dionne77249522021-06-11 09:55:11 -0400528#include <__iterator/wrap_iter.h>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400529#include <compare>
530#include <cstdio> // EOF
Louis Dionned24191c2021-08-19 12:39:16 -0400531#include <cstdlib>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400532#include <cstring>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400533#include <initializer_list>
534#include <iosfwd>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000535#include <iterator>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000536#include <memory>
537#include <stdexcept>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400538#include <string_view>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000539#include <type_traits>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400540#include <utility>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000541#include <version>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000542
Nikolas Klauser4c1bf592022-02-11 19:15:18 +0100543// TODO: remove these headers
544#include <__functional/binary_function.h>
545#include <__functional/invoke.h>
546#include <__functional/operations.h>
547#include <__functional/reference_wrapper.h>
548#include <__functional/unary_function.h>
549#include <__functional/weak_result_type.h>
550#include <new>
551#include <typeinfo>
552
Louis Dionne89258142021-08-23 15:32:36 -0400553#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Nikolas Klauser80840272022-02-04 13:04:33 +0100554# include <cwchar>
Louis Dionne89258142021-08-23 15:32:36 -0400555#endif
556
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400557#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
Nikolas Klauser80840272022-02-04 13:04:33 +0100558# include <cstdint>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400559#endif
Eric Fiselier14b6de92014-08-10 23:53:08 +0000560
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000561#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Arthur O'Dwyer6eeaa002022-02-01 20:16:40 -0500562# pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000563#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000564
Eric Fiselierf4433a32017-05-31 22:07:49 +0000565_LIBCPP_PUSH_MACROS
566#include <__undef_macros>
567
568
Howard Hinnantc51e1022010-05-11 19:42:16 +0000569_LIBCPP_BEGIN_NAMESPACE_STD
570
Howard Hinnantc51e1022010-05-11 19:42:16 +0000571// basic_string
572
573template<class _CharT, class _Traits, class _Allocator>
574basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000575operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
576 const basic_string<_CharT, _Traits, _Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000577
578template<class _CharT, class _Traits, class _Allocator>
579basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000580operator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000581
582template<class _CharT, class _Traits, class _Allocator>
583basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000584operator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000585
586template<class _CharT, class _Traits, class _Allocator>
Louis Dionne8f8d95d2018-11-21 17:31:55 +0000587inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000588basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000589operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000590
591template<class _CharT, class _Traits, class _Allocator>
592basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000593operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000594
Shoaib Meenaiea363712017-07-29 02:54:41 +0000595_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&))
596
Marshall Clow039b2f02016-01-13 21:54:34 +0000597template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400598struct __string_is_trivial_iterator : public false_type {};
599
600template <class _Tp>
601struct __string_is_trivial_iterator<_Tp*>
602 : public is_arithmetic<_Tp> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000603
Louis Dionne173f29e2019-05-29 16:01:36 +0000604template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400605struct __string_is_trivial_iterator<__wrap_iter<_Iter> >
606 : public __string_is_trivial_iterator<_Iter> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000607
Marshall Clow82513342016-09-24 22:45:42 +0000608template <class _CharT, class _Traits, class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500609struct __can_be_converted_to_string_view : public _BoolConstant<
610 is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
611 !is_convertible<const _Tp&, const _CharT*>::value
612 > {};
Marshall Clow82513342016-09-24 22:45:42 +0000613
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000614#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000615
616template <class _CharT, size_t = sizeof(_CharT)>
617struct __padding
618{
619 unsigned char __xx[sizeof(_CharT)-1];
620};
621
622template <class _CharT>
623struct __padding<_CharT, 1>
624{
625};
626
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400627#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000628
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400629#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800630typedef basic_string<char8_t> u8string;
631#endif
632
633#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
634typedef basic_string<char16_t> u16string;
635typedef basic_string<char32_t> u32string;
Louis Dionne96fc5f52021-09-09 11:25:10 -0400636#endif
Richard Smith256954d2020-11-11 17:12:18 -0800637
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000638template<class _CharT, class _Traits, class _Allocator>
Richard Smith256954d2020-11-11 17:12:18 -0800639class
640 _LIBCPP_TEMPLATE_VIS
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400641#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800642 _LIBCPP_PREFERRED_NAME(u8string)
643#endif
644#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
645 _LIBCPP_PREFERRED_NAME(u16string)
646 _LIBCPP_PREFERRED_NAME(u32string)
647#endif
648 basic_string
Howard Hinnantc51e1022010-05-11 19:42:16 +0000649{
650public:
651 typedef basic_string __self;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000652 typedef basic_string_view<_CharT, _Traits> __self_view;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000653 typedef _Traits traits_type;
Marshall Clowa3a74e02017-03-15 18:41:11 +0000654 typedef _CharT value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000655 typedef _Allocator allocator_type;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000656 typedef allocator_traits<allocator_type> __alloc_traits;
657 typedef typename __alloc_traits::size_type size_type;
658 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant3e276872011-06-03 18:40:47 +0000659 typedef value_type& reference;
660 typedef const value_type& const_reference;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000661 typedef typename __alloc_traits::pointer pointer;
662 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000663
Marshall Clow79f33542018-03-21 00:36:05 +0000664 static_assert((!is_array<value_type>::value), "Character type of basic_string must not be an array");
665 static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string must be standard-layout");
666 static_assert(( is_trivial<value_type>::value), "Character type of basic_string must be trivial");
667 static_assert(( is_same<_CharT, typename traits_type::char_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000668 "traits_type::char_type must be the same type as CharT");
Marshall Clow79f33542018-03-21 00:36:05 +0000669 static_assert(( is_same<typename allocator_type::value_type, value_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000670 "Allocator::value_type must be same type as value_type");
Marshall Clowe46031a2018-07-02 18:41:15 +0000671
Howard Hinnantc51e1022010-05-11 19:42:16 +0000672 typedef __wrap_iter<pointer> iterator;
673 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000674 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
675 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000676
677private:
Howard Hinnant68bf1812013-04-30 21:44:48 +0000678
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000679#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000680
681 struct __long
682 {
683 pointer __data_;
684 size_type __size_;
685 size_type __cap_;
686 };
687
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000688#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000689 static const size_type __short_mask = 0x01;
690 static const size_type __long_mask = 0x1ul;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000691#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000692 static const size_type __short_mask = 0x80;
693 static const size_type __long_mask = ~(size_type(~0) >> 1);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400694#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +0000695
696 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
697 (sizeof(__long) - 1)/sizeof(value_type) : 2};
698
699 struct __short
700 {
701 value_type __data_[__min_cap];
702 struct
703 : __padding<value_type>
704 {
705 unsigned char __size_;
706 };
707 };
708
709#else
710
Howard Hinnantc51e1022010-05-11 19:42:16 +0000711 struct __long
712 {
713 size_type __cap_;
714 size_type __size_;
715 pointer __data_;
716 };
717
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000718#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000719 static const size_type __short_mask = 0x80;
720 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000721#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000722 static const size_type __short_mask = 0x01;
723 static const size_type __long_mask = 0x1ul;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400724#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +0000725
Howard Hinnantc51e1022010-05-11 19:42:16 +0000726 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
727 (sizeof(__long) - 1)/sizeof(value_type) : 2};
728
729 struct __short
730 {
731 union
732 {
733 unsigned char __size_;
Howard Hinnant49e145e2012-10-30 19:06:59 +0000734 value_type __lx;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000735 };
736 value_type __data_[__min_cap];
737 };
738
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400739#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000740
Howard Hinnant8ea98242013-08-23 17:37:05 +0000741 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000742
Howard Hinnant8ea98242013-08-23 17:37:05 +0000743 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000744
745 struct __raw
746 {
747 size_type __words[__n_words];
748 };
749
750 struct __rep
751 {
752 union
753 {
754 __long __l;
755 __short __s;
756 __raw __r;
757 };
758 };
759
760 __compressed_pair<__rep, allocator_type> __r_;
761
Howard Hinnantc51e1022010-05-11 19:42:16 +0000762public:
Martin Storsjö88890c22021-02-22 01:13:13 +0200763 _LIBCPP_TEMPLATE_DATA_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000764 static const size_type npos = -1;
765
Howard Hinnant3e276872011-06-03 18:40:47 +0000766 _LIBCPP_INLINE_VISIBILITY basic_string()
767 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000768
769 _LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a)
770#if _LIBCPP_STD_VER <= 14
771 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
772#else
773 _NOEXCEPT;
774#endif
775
Howard Hinnantc51e1022010-05-11 19:42:16 +0000776 basic_string(const basic_string& __str);
777 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clowa80abc72015-06-03 19:56:43 +0000778
Eric Fiselierfc92be82017-04-19 00:28:44 +0000779#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000780 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000781 basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +0000782#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +0000783 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000784#else
785 _NOEXCEPT;
786#endif
787
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000788 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000789 basic_string(basic_string&& __str, const allocator_type& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400790#endif // _LIBCPP_CXX03_LANG
Marshall Clowe46031a2018-07-02 18:41:15 +0000791
Louis Dionne9ce598d2021-09-08 09:14:43 -0400792 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Eric Fiseliera8567862018-07-17 05:48:48 +0000793 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5169d1c2019-12-16 19:03:23 -0500794 basic_string(const _CharT* __s) : __r_(__default_init_tag(), __default_init_tag()) {
Eric Fiseliera8567862018-07-17 05:48:48 +0000795 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
796 __init(__s, traits_type::length(__s));
Nikolas Klauserf2807732022-01-11 00:33:35 +0100797 _VSTD::__debug_db_insert_c(this);
Eric Fiseliera8567862018-07-17 05:48:48 +0000798 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000799
Louis Dionne9ce598d2021-09-08 09:14:43 -0400800 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000801 _LIBCPP_INLINE_VISIBILITY
802 basic_string(const _CharT* __s, const _Allocator& __a);
803
Marek Kurdej90d79712021-07-27 16:16:21 +0200804#if _LIBCPP_STD_VER > 20
805 basic_string(nullptr_t) = delete;
806#endif
807
Howard Hinnantcf823322010-12-17 14:46:43 +0000808 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000809 basic_string(const _CharT* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +0000810 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000811 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000812 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000813 basic_string(size_type __n, _CharT __c);
Marshall Clowe46031a2018-07-02 18:41:15 +0000814
Louis Dionne9ce598d2021-09-08 09:14:43 -0400815 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000816 _LIBCPP_INLINE_VISIBILITY
817 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
818
Marshall Clow83445802016-04-07 18:13:41 +0000819 basic_string(const basic_string& __str, size_type __pos, size_type __n,
Eric Fiselier812882b2017-02-17 01:17:10 +0000820 const _Allocator& __a = _Allocator());
Marshall Clow83445802016-04-07 18:13:41 +0000821 _LIBCPP_INLINE_VISIBILITY
822 basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +0000823 const _Allocator& __a = _Allocator());
Marshall Clowe46031a2018-07-02 18:41:15 +0000824
Louis Dionne9ce598d2021-09-08 09:14:43 -0400825 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 +0000826 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselier812882b2017-02-17 01:17:10 +0000827 basic_string(const _Tp& __t, size_type __pos, size_type __n,
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500828 const allocator_type& __a = allocator_type());
Marshall Clowe46031a2018-07-02 18:41:15 +0000829
Louis Dionne9ce598d2021-09-08 09:14:43 -0400830 template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500831 !__is_same_uncvref<_Tp, basic_string>::value> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000832 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
833 explicit basic_string(const _Tp& __t);
834
Louis Dionne9ce598d2021-09-08 09:14:43 -0400835 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 +0000836 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
837 explicit basic_string(const _Tp& __t, const allocator_type& __a);
838
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);
Louis Dionne9ce598d2021-09-08 09:14:43 -0400842 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000843 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000844 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000845#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000846 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000847 basic_string(initializer_list<_CharT> __il);
Howard Hinnantcf823322010-12-17 14:46:43 +0000848 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000849 basic_string(initializer_list<_CharT> __il, const _Allocator& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400850#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000851
Eric Fiselierd9a702a2016-10-31 03:42:50 +0000852 inline ~basic_string();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000853
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000854 _LIBCPP_INLINE_VISIBILITY
855 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
856
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000857 basic_string& operator=(const basic_string& __str);
Eric Fiselier5ec13b12017-01-23 21:24:58 +0000858
Louis Dionne9ce598d2021-09-08 09:14:43 -0400859 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 +0000860 basic_string& operator=(const _Tp& __t)
861 {__self_view __sv = __t; return assign(__sv);}
862
Eric Fiselierfc92be82017-04-19 00:28:44 +0000863#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000864 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000865 basic_string& operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000866 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiselierfc92be82017-04-19 00:28:44 +0000867 _LIBCPP_INLINE_VISIBILITY
868 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000869#endif
Howard Hinnantd17880b2013-06-28 16:59:19 +0000870 _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const value_type* __s) {return assign(__s);}
Marek Kurdej90d79712021-07-27 16:16:21 +0200871#if _LIBCPP_STD_VER > 20
872 basic_string& operator=(nullptr_t) = delete;
873#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000874 basic_string& operator=(value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000875
Louis Dionneba400782020-10-02 15:02:52 -0400876#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +0000877 _LIBCPP_INLINE_VISIBILITY
878 iterator begin() _NOEXCEPT
879 {return iterator(this, __get_pointer());}
880 _LIBCPP_INLINE_VISIBILITY
881 const_iterator begin() const _NOEXCEPT
882 {return const_iterator(this, __get_pointer());}
883 _LIBCPP_INLINE_VISIBILITY
884 iterator end() _NOEXCEPT
885 {return iterator(this, __get_pointer() + size());}
886 _LIBCPP_INLINE_VISIBILITY
887 const_iterator end() const _NOEXCEPT
888 {return const_iterator(this, __get_pointer() + size());}
889#else
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000890 _LIBCPP_INLINE_VISIBILITY
891 iterator begin() _NOEXCEPT
892 {return iterator(__get_pointer());}
893 _LIBCPP_INLINE_VISIBILITY
894 const_iterator begin() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000895 {return const_iterator(__get_pointer());}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000896 _LIBCPP_INLINE_VISIBILITY
897 iterator end() _NOEXCEPT
898 {return iterator(__get_pointer() + size());}
899 _LIBCPP_INLINE_VISIBILITY
900 const_iterator end() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000901 {return const_iterator(__get_pointer() + size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400902#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000903 _LIBCPP_INLINE_VISIBILITY
904 reverse_iterator rbegin() _NOEXCEPT
905 {return reverse_iterator(end());}
906 _LIBCPP_INLINE_VISIBILITY
907 const_reverse_iterator rbegin() const _NOEXCEPT
908 {return const_reverse_iterator(end());}
909 _LIBCPP_INLINE_VISIBILITY
910 reverse_iterator rend() _NOEXCEPT
911 {return reverse_iterator(begin());}
912 _LIBCPP_INLINE_VISIBILITY
913 const_reverse_iterator rend() const _NOEXCEPT
914 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000915
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000916 _LIBCPP_INLINE_VISIBILITY
917 const_iterator cbegin() const _NOEXCEPT
918 {return begin();}
919 _LIBCPP_INLINE_VISIBILITY
920 const_iterator cend() const _NOEXCEPT
921 {return end();}
922 _LIBCPP_INLINE_VISIBILITY
923 const_reverse_iterator crbegin() const _NOEXCEPT
924 {return rbegin();}
925 _LIBCPP_INLINE_VISIBILITY
926 const_reverse_iterator crend() const _NOEXCEPT
927 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000928
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000929 _LIBCPP_INLINE_VISIBILITY size_type size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000930 {return __is_long() ? __get_long_size() : __get_short_size();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000931 _LIBCPP_INLINE_VISIBILITY size_type length() const _NOEXCEPT {return size();}
932 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT;
933 _LIBCPP_INLINE_VISIBILITY size_type capacity() const _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +0000934 {return (__is_long() ? __get_long_cap()
935 : static_cast<size_type>(__min_cap)) - 1;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000936
937 void resize(size_type __n, value_type __c);
938 _LIBCPP_INLINE_VISIBILITY void resize(size_type __n) {resize(__n, value_type());}
939
Marek Kurdejc9848142020-11-26 10:07:16 +0100940 void reserve(size_type __requested_capacity);
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100941
942#if _LIBCPP_STD_VER > 20
943 template <class _Op>
944 _LIBCPP_HIDE_FROM_ABI constexpr
945 void resize_and_overwrite(size_type __n, _Op __op) {
946 __resize_default_init(__n);
Nikolas Klausera4a76a12022-01-20 13:53:59 +0100947 __erase_to_end(_VSTD::move(__op)(data(), _LIBCPP_AUTO_CAST(__n)));
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100948 }
949#endif
950
Eric Fiselier451d5582018-11-26 20:15:38 +0000951 _LIBCPP_INLINE_VISIBILITY void __resize_default_init(size_type __n);
952
Marek Kurdejc9848142020-11-26 10:07:16 +0100953 _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_INLINE_VISIBILITY
954 void reserve() _NOEXCEPT {shrink_to_fit();}
Marshall Clow6ae12a82018-11-28 18:18:34 +0000955 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejc9848142020-11-26 10:07:16 +0100956 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000957 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000958 void clear() _NOEXCEPT;
Marshall Clowb7db4972017-11-15 20:02:27 +0000959 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
960 bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000961
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000962 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __pos) const _NOEXCEPT;
963 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000964
965 const_reference at(size_type __n) const;
966 reference at(size_type __n);
967
968 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const basic_string& __str) {return append(__str);}
Marshall Clowe46031a2018-07-02 18:41:15 +0000969
970 template <class _Tp>
971 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -0400972 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +0000973 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500974 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
975 && !__is_same_uncvref<_Tp, basic_string >::value,
Marshall Clowe46031a2018-07-02 18:41:15 +0000976 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500977 >
Marshall Clowe46031a2018-07-02 18:41:15 +0000978 operator+=(const _Tp& __t) {__self_view __sv = __t; return append(__sv);}
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000979 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const value_type* __s) {return append(__s);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000980 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(value_type __c) {push_back(__c); return *this;}
Eric Fiselierfc92be82017-04-19 00:28:44 +0000981#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000982 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(initializer_list<value_type> __il) {return append(__il);}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400983#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000984
Howard Hinnantcf823322010-12-17 14:46:43 +0000985 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000986 basic_string& append(const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000987
988 template <class _Tp>
989 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -0400990 __enable_if_t<
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500991 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
992 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clowe46031a2018-07-02 18:41:15 +0000993 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500994 >
Marshall Clowe46031a2018-07-02 18:41:15 +0000995 append(const _Tp& __t) { __self_view __sv = __t; return append(__sv.data(), __sv.size()); }
Marshall Clow8db7fb02014-03-04 19:17:19 +0000996 basic_string& append(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clowe46031a2018-07-02 18:41:15 +0000997
Marshall Clow62953962016-10-03 23:40:48 +0000998 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +0000999 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001000 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001001 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001002 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1003 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001004 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001005 >
Marshall Clow82513342016-09-24 22:45:42 +00001006 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001007 basic_string& append(const value_type* __s, size_type __n);
1008 basic_string& append(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001009 basic_string& append(size_type __n, value_type __c);
Eric Fiselier451d5582018-11-26 20:15:38 +00001010
1011 _LIBCPP_INLINE_VISIBILITY
1012 void __append_default_init(size_type __n);
1013
Howard Hinnantc51e1022010-05-11 19:42:16 +00001014 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001015 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001016 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001017 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001018 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001019 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001020 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001021 _LIBCPP_INLINE_VISIBILITY
1022 append(_InputIterator __first, _InputIterator __last) {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001023 const basic_string __temp(__first, __last, __alloc());
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001024 append(__temp.data(), __temp.size());
1025 return *this;
1026 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001027 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001028 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001029 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001030 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001031 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001032 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001033 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001034 _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001035 append(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001036
Eric Fiselierfc92be82017-04-19 00:28:44 +00001037#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001038 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001039 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001040#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001041
1042 void push_back(value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001043 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001044 void pop_back();
Marshall Clow05cf6692019-03-19 03:30:07 +00001045 _LIBCPP_INLINE_VISIBILITY reference front() _NOEXCEPT;
1046 _LIBCPP_INLINE_VISIBILITY const_reference front() const _NOEXCEPT;
1047 _LIBCPP_INLINE_VISIBILITY reference back() _NOEXCEPT;
1048 _LIBCPP_INLINE_VISIBILITY const_reference back() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001049
Marshall Clowe46031a2018-07-02 18:41:15 +00001050 template <class _Tp>
1051 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001052 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001053 <
1054 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1055 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001056 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001057 assign(const _Tp & __t) { __self_view __sv = __t; return assign(__sv.data(), __sv.size()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001058 _LIBCPP_INLINE_VISIBILITY
Marshall Clow95d5e9a2016-03-09 18:08:29 +00001059 basic_string& assign(const basic_string& __str) { return *this = __str; }
Eric Fiselierfc92be82017-04-19 00:28:44 +00001060#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001061 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001062 basic_string& assign(basic_string&& __str)
Marshall Clow5aa9e942015-10-05 16:17:34 +00001063 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001064 {*this = _VSTD::move(__str); return *this;}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001065#endif
Marshall Clow8db7fb02014-03-04 19:17:19 +00001066 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow62953962016-10-03 23:40:48 +00001067 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001068 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001069 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001070 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001071 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1072 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001073 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001074 >
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001075 assign(const _Tp & __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001076 basic_string& assign(const value_type* __s, size_type __n);
1077 basic_string& assign(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001078 basic_string& assign(size_type __n, value_type __c);
1079 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001080 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001081 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001082 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001083 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001084 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001085 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001086 assign(_InputIterator __first, _InputIterator __last);
1087 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001088 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001089 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001090 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001091 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001092 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001093 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001094 assign(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001095#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001096 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001097 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001098#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001099
Howard Hinnantcf823322010-12-17 14:46:43 +00001100 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001101 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001102
1103 template <class _Tp>
1104 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001105 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001106 <
1107 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1108 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001109 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001110 insert(size_type __pos1, const _Tp& __t)
1111 { __self_view __sv = __t; return insert(__pos1, __sv.data(), __sv.size()); }
1112
Marshall Clow82513342016-09-24 22:45:42 +00001113 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001114 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001115 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001116 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001117 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001118 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001119 >
Marshall Clow82513342016-09-24 22:45:42 +00001120 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Marshall Clow8db7fb02014-03-04 19:17:19 +00001121 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001122 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1123 basic_string& insert(size_type __pos, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001124 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1125 iterator insert(const_iterator __pos, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001126 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001127 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1128 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001129 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001130 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001131 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001132 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001133 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001134 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001135 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1136 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001137 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001138 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001139 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001140 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001141 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001142 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001143 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001144#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001145 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001146 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1147 {return insert(__pos, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001148#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001149
1150 basic_string& erase(size_type __pos = 0, size_type __n = npos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001151 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001152 iterator erase(const_iterator __pos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001153 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001154 iterator erase(const_iterator __first, const_iterator __last);
1155
Howard Hinnantcf823322010-12-17 14:46:43 +00001156 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001157 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001158
1159 template <class _Tp>
1160 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001161 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001162 <
1163 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1164 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001165 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001166 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 +00001167 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 +00001168 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001169 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001170 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001171 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001172 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001173 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001174 >
Marshall Clow82513342016-09-24 22:45:42 +00001175 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001176 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
1177 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001178 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001179 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001180 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001181
1182 template <class _Tp>
1183 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001184 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001185 <
1186 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1187 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001188 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001189 replace(const_iterator __i1, const_iterator __i2, const _Tp& __t) { __self_view __sv = __t; return replace(__i1 - begin(), __i2 - __i1, __sv); }
1190
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001191 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001192 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +00001193 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001194 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Howard Hinnantcf823322010-12-17 14:46:43 +00001195 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001196 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001197 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001198 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001199 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001200 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001201 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001202 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001203 >
Howard Hinnant990d6e82010-11-17 21:11:40 +00001204 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001205#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001206 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001207 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001208 {return replace(__i1, __i2, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001209#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001210
Howard Hinnantd17880b2013-06-28 16:59:19 +00001211 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
Howard Hinnantcf823322010-12-17 14:46:43 +00001212 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001213 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1214
Howard Hinnantcf823322010-12-17 14:46:43 +00001215 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001216 void swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001217#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00001218 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +00001219#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001220 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001221 __is_nothrow_swappable<allocator_type>::value);
1222#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001223
Howard Hinnantcf823322010-12-17 14:46:43 +00001224 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001225 const value_type* c_str() const _NOEXCEPT {return data();}
Howard Hinnantcf823322010-12-17 14:46:43 +00001226 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001227 const value_type* data() const _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Eric Fiselier4ee2a892017-11-20 20:23:27 +00001228#if _LIBCPP_STD_VER > 14 || defined(_LIBCPP_BUILDING_LIBRARY)
Marshall Clowd3c22392016-03-08 15:44:30 +00001229 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001230 value_type* data() _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Marshall Clowd3c22392016-03-08 15:44:30 +00001231#endif
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 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001235
Howard Hinnantcf823322010-12-17 14:46:43 +00001236 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001237 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001238
1239 template <class _Tp>
1240 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001241 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001242 <
1243 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1244 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001245 >
zoecarver1997e0a2021-02-05 11:54:47 -08001246 find(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001247 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001248 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001249 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001250 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001251
Howard Hinnantcf823322010-12-17 14:46:43 +00001252 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001253 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001254
1255 template <class _Tp>
1256 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001257 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001258 <
1259 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1260 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001261 >
zoecarver1997e0a2021-02-05 11:54:47 -08001262 rfind(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001263 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001264 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001265 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001266 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001267
Howard Hinnantcf823322010-12-17 14:46:43 +00001268 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001269 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001270
1271 template <class _Tp>
1272 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001273 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001274 <
1275 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1276 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001277 >
zoecarver1997e0a2021-02-05 11:54:47 -08001278 find_first_of(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001279 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001280 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001281 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001282 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001283 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001284
Howard Hinnantcf823322010-12-17 14:46:43 +00001285 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001286 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001287
1288 template <class _Tp>
1289 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001290 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001291 <
1292 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1293 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001294 >
zoecarver1997e0a2021-02-05 11:54:47 -08001295 find_last_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001296 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001297 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001298 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001299 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001300 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001301
Howard Hinnantcf823322010-12-17 14:46:43 +00001302 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001303 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001304
1305 template <class _Tp>
1306 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001307 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001308 <
1309 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1310 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001311 >
zoecarver1997e0a2021-02-05 11:54:47 -08001312 find_first_not_of(const _Tp &__t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001313 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 +00001314 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001315 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001316 _LIBCPP_INLINE_VISIBILITY
1317 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1318
1319 _LIBCPP_INLINE_VISIBILITY
1320 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001321
1322 template <class _Tp>
1323 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001324 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001325 <
1326 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1327 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001328 >
zoecarver1997e0a2021-02-05 11:54:47 -08001329 find_last_not_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001330 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 +00001331 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001332 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001333 _LIBCPP_INLINE_VISIBILITY
1334 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1335
1336 _LIBCPP_INLINE_VISIBILITY
1337 int compare(const basic_string& __str) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001338
1339 template <class _Tp>
1340 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001341 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001342 <
1343 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1344 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001345 >
zoecarver1997e0a2021-02-05 11:54:47 -08001346 compare(const _Tp &__t) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001347
1348 template <class _Tp>
1349 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001350 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001351 <
1352 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1353 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001354 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001355 compare(size_type __pos1, size_type __n1, const _Tp& __t) const;
1356
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001357 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001358 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Marshall Clow8db7fb02014-03-04 19:17:19 +00001359 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 +00001360
Marshall Clow82513342016-09-24 22:45:42 +00001361 template <class _Tp>
Eric Fiselier7d4aa5c2016-10-14 05:29:46 +00001362 inline _LIBCPP_INLINE_VISIBILITY
Louis Dionne9ce598d2021-09-08 09:14:43 -04001363 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001364 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001365 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001366 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001367 >
Marshall Clow82513342016-09-24 22:45:42 +00001368 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 +00001369 int compare(const value_type* __s) const _NOEXCEPT;
1370 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1371 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001372
Marshall Clow18c293b2017-12-04 20:11:38 +00001373#if _LIBCPP_STD_VER > 17
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001374 constexpr _LIBCPP_INLINE_VISIBILITY
1375 bool starts_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001376 { return __self_view(data(), size()).starts_with(__sv); }
1377
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001378 constexpr _LIBCPP_INLINE_VISIBILITY
1379 bool starts_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001380 { return !empty() && _Traits::eq(front(), __c); }
1381
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001382 constexpr _LIBCPP_INLINE_VISIBILITY
1383 bool starts_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001384 { return starts_with(__self_view(__s)); }
1385
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001386 constexpr _LIBCPP_INLINE_VISIBILITY
1387 bool ends_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001388 { return __self_view(data(), size()).ends_with( __sv); }
1389
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001390 constexpr _LIBCPP_INLINE_VISIBILITY
1391 bool ends_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001392 { return !empty() && _Traits::eq(back(), __c); }
1393
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001394 constexpr _LIBCPP_INLINE_VISIBILITY
1395 bool ends_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001396 { return ends_with(__self_view(__s)); }
1397#endif
1398
Wim Leflere023c3542021-01-19 14:33:30 -05001399#if _LIBCPP_STD_VER > 20
1400 constexpr _LIBCPP_INLINE_VISIBILITY
1401 bool contains(__self_view __sv) const noexcept
1402 { return __self_view(data(), size()).contains(__sv); }
1403
1404 constexpr _LIBCPP_INLINE_VISIBILITY
1405 bool contains(value_type __c) const noexcept
1406 { return __self_view(data(), size()).contains(__c); }
1407
1408 constexpr _LIBCPP_INLINE_VISIBILITY
1409 bool contains(const value_type* __s) const
1410 { return __self_view(data(), size()).contains(__s); }
1411#endif
1412
Howard Hinnantcf823322010-12-17 14:46:43 +00001413 _LIBCPP_INLINE_VISIBILITY bool __invariants() const;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001414
Marshall Clowe60a7182018-05-29 17:04:37 +00001415 _LIBCPP_INLINE_VISIBILITY void __clear_and_shrink() _NOEXCEPT;
Marshall Clow851b9ec2019-05-20 21:56:51 +00001416
Marek Kurdejc9848142020-11-26 10:07:16 +01001417 _LIBCPP_INLINE_VISIBILITY void __shrink_or_extend(size_type __target_capacity);
1418
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001419 _LIBCPP_INLINE_VISIBILITY
1420 bool __is_long() const _NOEXCEPT
1421 {return bool(__r_.first().__s.__size_ & __short_mask);}
1422
Louis Dionneba400782020-10-02 15:02:52 -04001423#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001424
1425 bool __dereferenceable(const const_iterator* __i) const;
1426 bool __decrementable(const const_iterator* __i) const;
1427 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1428 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1429
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001430#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001431
Howard Hinnantc51e1022010-05-11 19:42:16 +00001432private:
Nikolas Klauser93826d12022-01-04 17:24:03 +01001433 _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI static bool __fits_in_sso(size_type __sz) {
1434 // SSO is disabled during constant evaluation because `__is_long` isn't constexpr friendly
1435 return !__libcpp_is_constant_evaluated() && (__sz < __min_cap);
1436 }
1437
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001438 _LIBCPP_INLINE_VISIBILITY
1439 allocator_type& __alloc() _NOEXCEPT
1440 {return __r_.second();}
1441 _LIBCPP_INLINE_VISIBILITY
1442 const allocator_type& __alloc() const _NOEXCEPT
1443 {return __r_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001444
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001445#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001446
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001447 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001448 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001449# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001450 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001451# else
1452 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1453# endif
1454
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001455 _LIBCPP_INLINE_VISIBILITY
1456 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001457# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001458 {return __r_.first().__s.__size_ >> 1;}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001459# else
1460 {return __r_.first().__s.__size_;}
1461# endif
1462
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001463#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001464
1465 _LIBCPP_INLINE_VISIBILITY
1466 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001467# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001468 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1469# else
1470 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
1471# endif
1472
1473 _LIBCPP_INLINE_VISIBILITY
1474 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001475# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001476 {return __r_.first().__s.__size_;}
1477# else
1478 {return __r_.first().__s.__size_ >> 1;}
1479# endif
1480
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001481#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001482
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001483 _LIBCPP_INLINE_VISIBILITY
1484 void __set_long_size(size_type __s) _NOEXCEPT
1485 {__r_.first().__l.__size_ = __s;}
1486 _LIBCPP_INLINE_VISIBILITY
1487 size_type __get_long_size() const _NOEXCEPT
1488 {return __r_.first().__l.__size_;}
1489 _LIBCPP_INLINE_VISIBILITY
1490 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001491 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1492
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001493 _LIBCPP_INLINE_VISIBILITY
1494 void __set_long_cap(size_type __s) _NOEXCEPT
1495 {__r_.first().__l.__cap_ = __long_mask | __s;}
1496 _LIBCPP_INLINE_VISIBILITY
1497 size_type __get_long_cap() const _NOEXCEPT
Howard Hinnant28b24882011-12-01 20:21:04 +00001498 {return __r_.first().__l.__cap_ & size_type(~__long_mask);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001499
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001500 _LIBCPP_INLINE_VISIBILITY
1501 void __set_long_pointer(pointer __p) _NOEXCEPT
1502 {__r_.first().__l.__data_ = __p;}
1503 _LIBCPP_INLINE_VISIBILITY
1504 pointer __get_long_pointer() _NOEXCEPT
1505 {return __r_.first().__l.__data_;}
1506 _LIBCPP_INLINE_VISIBILITY
1507 const_pointer __get_long_pointer() const _NOEXCEPT
1508 {return __r_.first().__l.__data_;}
1509 _LIBCPP_INLINE_VISIBILITY
1510 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001511 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001512 _LIBCPP_INLINE_VISIBILITY
1513 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001514 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001515 _LIBCPP_INLINE_VISIBILITY
1516 pointer __get_pointer() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001517 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001518 _LIBCPP_INLINE_VISIBILITY
1519 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001520 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1521
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001522 _LIBCPP_INLINE_VISIBILITY
1523 void __zero() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001524 {
1525 size_type (&__a)[__n_words] = __r_.first().__r.__words;
1526 for (unsigned __i = 0; __i < __n_words; ++__i)
1527 __a[__i] = 0;
1528 }
1529
1530 template <size_type __a> static
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001531 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00001532 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +00001533 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001534 enum {__alignment = 16};
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001535 static _LIBCPP_INLINE_VISIBILITY
1536 size_type __recommend(size_type __s) _NOEXCEPT
Marshall Clow80584522018-02-07 21:30:17 +00001537 {
1538 if (__s < __min_cap) return static_cast<size_type>(__min_cap) - 1;
1539 size_type __guess = __align_it<sizeof(value_type) < __alignment ?
1540 __alignment/sizeof(value_type) : 1 > (__s+1) - 1;
1541 if (__guess == __min_cap) ++__guess;
1542 return __guess;
1543 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001544
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001545 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001546 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001547 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001548 void __init(const value_type* __s, size_type __sz);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001549 inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001550 void __init(size_type __n, value_type __c);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001551
Martijn Vels5e7c9752020-03-04 17:52:46 -05001552 // Slow path for the (inlined) copy constructor for 'long' strings.
1553 // Always externally instantiated and not inlined.
1554 // Requires that __s is zero terminated.
1555 // The main reason for this function to exist is because for unstable, we
1556 // want to allow inlining of the copy constructor. However, we don't want
1557 // to call the __init() functions as those are marked as inline which may
1558 // result in over-aggressive inlining by the compiler, where our aim is
1559 // to only inline the fast path code directly in the ctor.
1560 void __init_copy_ctor_external(const value_type* __s, size_type __sz);
1561
Howard Hinnantc51e1022010-05-11 19:42:16 +00001562 template <class _InputIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001563 inline
Louis Dionne9ce598d2021-09-08 09:14:43 -04001564 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001565 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001566 __is_exactly_cpp17_input_iterator<_InputIterator>::value
1567 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001568 __init(_InputIterator __first, _InputIterator __last);
1569
1570 template <class _ForwardIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001571 inline
Louis Dionne9ce598d2021-09-08 09:14:43 -04001572 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001573 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001574 __is_cpp17_forward_iterator<_ForwardIterator>::value
1575 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001576 __init(_ForwardIterator __first, _ForwardIterator __last);
1577
1578 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001579 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001580 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1581 size_type __n_copy, size_type __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00001582 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001583
Martijn Vels596e3de2020-02-26 15:55:49 -05001584 // __assign_no_alias is invoked for assignment operations where we
1585 // have proof that the input does not alias the current instance.
1586 // For example, operator=(basic_string) performs a 'self' check.
1587 template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04001588 basic_string& __assign_no_alias(const value_type* __s, size_type __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05001589
Howard Hinnantcf823322010-12-17 14:46:43 +00001590 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001591 void __erase_to_end(size_type __pos);
1592
Martijn Velsa81fc792020-02-26 13:25:43 -05001593 // __erase_external_with_move is invoked for erase() invocations where
1594 // `n ~= npos`, likely requiring memory moves on the string data.
1595 void __erase_external_with_move(size_type __pos, size_type __n);
1596
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001597 _LIBCPP_INLINE_VISIBILITY
1598 void __copy_assign_alloc(const basic_string& __str)
1599 {__copy_assign_alloc(__str, integral_constant<bool,
1600 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1601
1602 _LIBCPP_INLINE_VISIBILITY
1603 void __copy_assign_alloc(const basic_string& __str, true_type)
1604 {
Marshall Clowf258c202017-01-31 03:40:52 +00001605 if (__alloc() == __str.__alloc())
1606 __alloc() = __str.__alloc();
1607 else
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001608 {
Marshall Clowf258c202017-01-31 03:40:52 +00001609 if (!__str.__is_long())
1610 {
Vedant Kumar55e007e2018-03-08 21:15:26 +00001611 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001612 __alloc() = __str.__alloc();
1613 }
1614 else
1615 {
1616 allocator_type __a = __str.__alloc();
1617 pointer __p = __alloc_traits::allocate(__a, __str.__get_long_cap());
Vedant Kumar55e007e2018-03-08 21:15:26 +00001618 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001619 __alloc() = _VSTD::move(__a);
1620 __set_long_pointer(__p);
1621 __set_long_cap(__str.__get_long_cap());
1622 __set_long_size(__str.size());
1623 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001624 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001625 }
1626
1627 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001628 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001629 {}
1630
Eric Fiselierfc92be82017-04-19 00:28:44 +00001631#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +00001632 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001633 void __move_assign(basic_string& __str, false_type)
1634 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnantcf823322010-12-17 14:46:43 +00001635 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001636 void __move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001637#if _LIBCPP_STD_VER > 14
1638 _NOEXCEPT;
1639#else
Howard Hinnant3e276872011-06-03 18:40:47 +00001640 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001641#endif
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001642#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001643
1644 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001645 void
Howard Hinnantc2734962011-09-02 20:42:31 +00001646 __move_assign_alloc(basic_string& __str)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001647 _NOEXCEPT_(
1648 !__alloc_traits::propagate_on_container_move_assignment::value ||
1649 is_nothrow_move_assignable<allocator_type>::value)
1650 {__move_assign_alloc(__str, integral_constant<bool,
1651 __alloc_traits::propagate_on_container_move_assignment::value>());}
1652
1653 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00001654 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001655 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1656 {
1657 __alloc() = _VSTD::move(__c.__alloc());
1658 }
1659
1660 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001661 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001662 _NOEXCEPT
1663 {}
1664
Martijn Velsda7d94f2020-06-19 14:24:03 -04001665 basic_string& __assign_external(const value_type* __s);
1666 basic_string& __assign_external(const value_type* __s, size_type __n);
1667
1668 // Assigns the value in __s, guaranteed to be __n < __min_cap in length.
1669 inline basic_string& __assign_short(const value_type* __s, size_type __n) {
1670 pointer __p = __is_long()
1671 ? (__set_long_size(__n), __get_long_pointer())
1672 : (__set_short_size(__n), __get_short_pointer());
1673 traits_type::move(_VSTD::__to_address(__p), __s, __n);
1674 traits_type::assign(__p[__n], value_type());
1675 return *this;
1676 }
1677
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01001678 _LIBCPP_HIDE_FROM_ABI basic_string& __null_terminate_at(value_type* __p, size_type __newsz) {
1679 __set_size(__newsz);
1680 __invalidate_iterators_past(__newsz);
1681 traits_type::assign(__p[__newsz], value_type());
1682 return *this;
1683 }
1684
Howard Hinnantcf823322010-12-17 14:46:43 +00001685 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
1686 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(size_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001687
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001688 template<class _Tp>
1689 _LIBCPP_INLINE_VISIBILITY
1690 bool __addr_in_range(_Tp&& __t) const {
1691 const volatile void *__p = _VSTD::addressof(__t);
1692 return data() <= __p && __p <= data() + size();
1693 }
1694
Louis Dionned24191c2021-08-19 12:39:16 -04001695 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1696 void __throw_length_error() const {
Nikolas Klauserf89bb932022-01-24 19:44:34 +01001697 _VSTD::__throw_length_error("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001698 }
1699
1700 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1701 void __throw_out_of_range() const {
Nikolas Klauserf89bb932022-01-24 19:44:34 +01001702 _VSTD::__throw_out_of_range("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001703 }
1704
Howard Hinnantc51e1022010-05-11 19:42:16 +00001705 friend basic_string operator+<>(const basic_string&, const basic_string&);
1706 friend basic_string operator+<>(const value_type*, const basic_string&);
1707 friend basic_string operator+<>(value_type, const basic_string&);
1708 friend basic_string operator+<>(const basic_string&, const value_type*);
1709 friend basic_string operator+<>(const basic_string&, value_type);
1710};
1711
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001712// These declarations must appear before any functions are implicitly used
1713// so that they have the correct visibility specifier.
1714#ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
Louis Dionne89258142021-08-23 15:32:36 -04001715 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1716# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1717 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1718# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001719#else
Louis Dionne89258142021-08-23 15:32:36 -04001720 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1721# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1722 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1723# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001724#endif
1725
1726
Louis Dionned59f8a52021-08-17 11:59:07 -04001727#if _LIBCPP_STD_VER >= 17
Marshall Clowa0563332018-02-08 06:34:03 +00001728template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -05001729 class _CharT = __iter_value_type<_InputIterator>,
Marshall Clowa0563332018-02-08 06:34:03 +00001730 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001731 class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
1732 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowa0563332018-02-08 06:34:03 +00001733 >
1734basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
1735 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
Marshall Clowe46031a2018-07-02 18:41:15 +00001736
1737template<class _CharT,
1738 class _Traits,
1739 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001740 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowe46031a2018-07-02 18:41:15 +00001741 >
1742explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
1743 -> basic_string<_CharT, _Traits, _Allocator>;
1744
1745template<class _CharT,
1746 class _Traits,
1747 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001748 class = enable_if_t<__is_allocator<_Allocator>::value>,
Marshall Clowe46031a2018-07-02 18:41:15 +00001749 class _Sz = typename allocator_traits<_Allocator>::size_type
1750 >
1751basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
1752 -> basic_string<_CharT, _Traits, _Allocator>;
Marshall Clowa0563332018-02-08 06:34:03 +00001753#endif
1754
Howard Hinnantc51e1022010-05-11 19:42:16 +00001755template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001756inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001757void
1758basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
1759{
Louis Dionneba400782020-10-02 15:02:52 -04001760#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001761 if (!__libcpp_is_constant_evaluated())
1762 __get_db()->__invalidate_all(this);
Louis Dionneba400782020-10-02 15:02:52 -04001763#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001764}
1765
1766template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001767inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001768void
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001769basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type __pos)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001770{
Louis Dionneba400782020-10-02 15:02:52 -04001771#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001772 if (!__libcpp_is_constant_evaluated()) {
1773 __c_node* __c = __get_db()->__find_c_and_lock(this);
1774 if (__c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001775 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001776 const_pointer __new_last = __get_pointer() + __pos;
1777 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001778 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001779 --__p;
1780 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1781 if (__i->base() > __new_last)
1782 {
1783 (*__p)->__c_ = nullptr;
1784 if (--__c->end_ != __p)
1785 _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1786 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001787 }
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001788 __get_db()->unlock();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001789 }
1790 }
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001791#else
1792 (void)__pos;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001793#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001794}
1795
1796template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001797inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001798basic_string<_CharT, _Traits, _Allocator>::basic_string()
Marshall Clowe546cbb2015-06-04 02:05:41 +00001799 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001800 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001801{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001802 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001803 __zero();
1804}
1805
1806template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001807inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001808basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
Eric Fiseliere012bf22015-07-18 20:40:46 +00001809#if _LIBCPP_STD_VER <= 14
1810 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1811#else
1812 _NOEXCEPT
1813#endif
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001814: __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001815{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001816 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001817 __zero();
1818}
1819
1820template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier815ed732016-09-16 00:00:48 +00001821void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1822 size_type __sz,
1823 size_type __reserve)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001824{
1825 if (__reserve > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001826 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001827 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001828 if (__fits_in_sso(__reserve))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001829 {
1830 __set_short_size(__sz);
1831 __p = __get_short_pointer();
1832 }
1833 else
1834 {
1835 size_type __cap = __recommend(__reserve);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001836 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001837 __set_long_pointer(__p);
1838 __set_long_cap(__cap+1);
1839 __set_long_size(__sz);
1840 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001841 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001842 traits_type::assign(__p[__sz], value_type());
1843}
1844
1845template <class _CharT, class _Traits, class _Allocator>
1846void
Howard Hinnantd17880b2013-06-28 16:59:19 +00001847basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001848{
1849 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001850 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001851 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001852 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001853 {
1854 __set_short_size(__sz);
1855 __p = __get_short_pointer();
1856 }
1857 else
1858 {
1859 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001860 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001861 __set_long_pointer(__p);
1862 __set_long_cap(__cap+1);
1863 __set_long_size(__sz);
1864 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001865 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001866 traits_type::assign(__p[__sz], value_type());
1867}
1868
1869template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00001870template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00001871basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001872 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001873{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001874 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001875 __init(__s, traits_type::length(__s));
Nikolas Klauserf2807732022-01-11 00:33:35 +01001876 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001877}
1878
1879template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001880inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001881basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001882 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001883{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001884 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
1885 __init(__s, __n);
1886 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001887}
1888
1889template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001890inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001891basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001892 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001893{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001894 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001895 __init(__s, __n);
Nikolas Klauserf2807732022-01-11 00:33:35 +01001896 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001897}
1898
1899template <class _CharT, class _Traits, class _Allocator>
1900basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001901 : __r_(__default_init_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001902{
1903 if (!__str.__is_long())
1904 __r_.first().__r = __str.__r_.first().__r;
1905 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001906 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1907 __str.__get_long_size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01001908 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001909}
1910
1911template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00001912basic_string<_CharT, _Traits, _Allocator>::basic_string(
1913 const basic_string& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001914 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001915{
1916 if (!__str.__is_long())
1917 __r_.first().__r = __str.__r_.first().__r;
1918 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001919 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1920 __str.__get_long_size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01001921 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001922}
1923
Martijn Vels5e7c9752020-03-04 17:52:46 -05001924template <class _CharT, class _Traits, class _Allocator>
1925void basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(
1926 const value_type* __s, size_type __sz) {
1927 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001928 if (__fits_in_sso(__sz)) {
Martijn Vels5e7c9752020-03-04 17:52:46 -05001929 __p = __get_short_pointer();
1930 __set_short_size(__sz);
1931 } else {
1932 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001933 __throw_length_error();
Martijn Vels5e7c9752020-03-04 17:52:46 -05001934 size_t __cap = __recommend(__sz);
1935 __p = __alloc_traits::allocate(__alloc(), __cap + 1);
1936 __set_long_pointer(__p);
1937 __set_long_cap(__cap + 1);
1938 __set_long_size(__sz);
1939 }
1940 traits_type::copy(_VSTD::__to_address(__p), __s, __sz + 1);
1941}
1942
Eric Fiselierfc92be82017-04-19 00:28:44 +00001943#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001944
1945template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001946inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001947basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +00001948#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +00001949 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowa80abc72015-06-03 19:56:43 +00001950#else
1951 _NOEXCEPT
1952#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001953 : __r_(_VSTD::move(__str.__r_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001954{
1955 __str.__zero();
Nikolas Klauserf2807732022-01-11 00:33:35 +01001956 _VSTD::__debug_db_insert_c(this);
Louis Dionneba400782020-10-02 15:02:52 -04001957#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauserf2807732022-01-11 00:33:35 +01001958 if (!__libcpp_is_constant_evaluated() && __is_long())
1959 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001960#endif
1961}
1962
1963template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001964inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001965basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001966 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001967{
Marshall Clowd2d24692014-07-17 15:32:20 +00001968 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001969 __init(_VSTD::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd2d24692014-07-17 15:32:20 +00001970 else
1971 {
1972 __r_.first().__r = __str.__r_.first().__r;
1973 __str.__zero();
1974 }
Nikolas Klauserf2807732022-01-11 00:33:35 +01001975 _VSTD::__debug_db_insert_c(this);
Louis Dionneba400782020-10-02 15:02:52 -04001976#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauserf2807732022-01-11 00:33:35 +01001977 if (!__libcpp_is_constant_evaluated() && __is_long())
1978 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001979#endif
1980}
1981
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001982#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001983
1984template <class _CharT, class _Traits, class _Allocator>
1985void
1986basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
1987{
1988 if (__n > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001989 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001990 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001991 if (__fits_in_sso(__n))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001992 {
1993 __set_short_size(__n);
1994 __p = __get_short_pointer();
1995 }
1996 else
1997 {
1998 size_type __cap = __recommend(__n);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001999 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002000 __set_long_pointer(__p);
2001 __set_long_cap(__cap+1);
2002 __set_long_size(__n);
2003 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002004 traits_type::assign(_VSTD::__to_address(__p), __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002005 traits_type::assign(__p[__n], value_type());
2006}
2007
2008template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002009inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002010basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002011 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002012{
2013 __init(__n, __c);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002014 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002015}
2016
2017template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002018template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00002019basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002020 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002021{
2022 __init(__n, __c);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002023 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002024}
2025
Howard Hinnantc51e1022010-05-11 19:42:16 +00002026template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00002027basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
2028 size_type __pos, size_type __n,
2029 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002030 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002031{
2032 size_type __str_sz = __str.size();
2033 if (__pos > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002034 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002035 __init(__str.data() + __pos, _VSTD::min(__n, __str_sz - __pos));
Nikolas Klauserf2807732022-01-11 00:33:35 +01002036 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002037}
2038
2039template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002040inline
Marshall Clow83445802016-04-07 18:13:41 +00002041basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +00002042 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002043 : __r_(__default_init_tag(), __a)
Marshall Clow83445802016-04-07 18:13:41 +00002044{
2045 size_type __str_sz = __str.size();
2046 if (__pos > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002047 __throw_out_of_range();
Marshall Clow83445802016-04-07 18:13:41 +00002048 __init(__str.data() + __pos, __str_sz - __pos);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002049 _VSTD::__debug_db_insert_c(this);
Marshall Clow83445802016-04-07 18:13:41 +00002050}
2051
2052template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002053template <class _Tp, class>
Marshall Clow78dbe462016-11-14 18:22:19 +00002054basic_string<_CharT, _Traits, _Allocator>::basic_string(
Marshall Clowe46031a2018-07-02 18:41:15 +00002055 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002056 : __r_(__default_init_tag(), __a)
Marshall Clow78dbe462016-11-14 18:22:19 +00002057{
Marshall Clowe46031a2018-07-02 18:41:15 +00002058 __self_view __sv0 = __t;
2059 __self_view __sv = __sv0.substr(__pos, __n);
Marshall Clow78dbe462016-11-14 18:22:19 +00002060 __init(__sv.data(), __sv.size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002061 _VSTD::__debug_db_insert_c(this);
Marshall Clow78dbe462016-11-14 18:22:19 +00002062}
2063
2064template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002065template <class _Tp, class>
2066basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002067 : __r_(__default_init_tag(), __default_init_tag())
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002068{
Marshall Clowe46031a2018-07-02 18:41:15 +00002069 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002070 __init(__sv.data(), __sv.size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002071 _VSTD::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002072}
2073
2074template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002075template <class _Tp, class>
2076basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002077 : __r_(__default_init_tag(), __a)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002078{
Marshall Clowe46031a2018-07-02 18:41:15 +00002079 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002080 __init(__sv.data(), __sv.size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002081 _VSTD::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002082}
2083
2084template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002085template <class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002086__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002087<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002088 __is_exactly_cpp17_input_iterator<_InputIterator>::value
2089>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002090basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
2091{
2092 __zero();
2093#ifndef _LIBCPP_NO_EXCEPTIONS
2094 try
2095 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002096#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002097 for (; __first != __last; ++__first)
2098 push_back(*__first);
2099#ifndef _LIBCPP_NO_EXCEPTIONS
2100 }
2101 catch (...)
2102 {
2103 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002104 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002105 throw;
2106 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002107#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002108}
2109
2110template <class _CharT, class _Traits, class _Allocator>
2111template <class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002112__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002113<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002114 __is_cpp17_forward_iterator<_ForwardIterator>::value
2115>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002116basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
2117{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002118 size_type __sz = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002119 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002120 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002121 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002122 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002123 {
2124 __set_short_size(__sz);
2125 __p = __get_short_pointer();
2126 }
2127 else
2128 {
2129 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002130 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002131 __set_long_pointer(__p);
2132 __set_long_cap(__cap+1);
2133 __set_long_size(__sz);
2134 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002135
2136#ifndef _LIBCPP_NO_EXCEPTIONS
2137 try
2138 {
2139#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002140 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002141 traits_type::assign(*__p, *__first);
2142 traits_type::assign(*__p, value_type());
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002143#ifndef _LIBCPP_NO_EXCEPTIONS
2144 }
2145 catch (...)
2146 {
2147 if (__is_long())
2148 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
2149 throw;
2150 }
2151#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002152}
2153
2154template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002155template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002156inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002157basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002158 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002159{
2160 __init(__first, __last);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002161 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002162}
2163
2164template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002165template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002166inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002167basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
2168 const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002169 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002170{
2171 __init(__first, __last);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002172 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002173}
2174
Eric Fiselierfc92be82017-04-19 00:28:44 +00002175#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002176
Howard Hinnantc51e1022010-05-11 19:42:16 +00002177template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002178inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002179basic_string<_CharT, _Traits, _Allocator>::basic_string(
2180 initializer_list<_CharT> __il)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002181 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002182{
2183 __init(__il.begin(), __il.end());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002184 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002185}
2186
2187template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002188inline
Eric Fiselier9d355982017-04-12 23:45:53 +00002189
Eric Fiselier812882b2017-02-17 01:17:10 +00002190basic_string<_CharT, _Traits, _Allocator>::basic_string(
2191 initializer_list<_CharT> __il, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002192 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002193{
2194 __init(__il.begin(), __il.end());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002195 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002196}
2197
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002198#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002199
Howard Hinnantc51e1022010-05-11 19:42:16 +00002200template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002201basic_string<_CharT, _Traits, _Allocator>::~basic_string()
2202{
Louis Dionneba400782020-10-02 15:02:52 -04002203#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002204 if (!__libcpp_is_constant_evaluated())
2205 __get_db()->__erase_c(this);
Howard Hinnant8ea98242013-08-23 17:37:05 +00002206#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002207 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002208 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002209}
2210
2211template <class _CharT, class _Traits, class _Allocator>
2212void
2213basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
2214 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002215 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 +00002216{
2217 size_type __ms = max_size();
2218 if (__delta_cap > __ms - __old_cap - 1)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002219 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002220 pointer __old_p = __get_pointer();
2221 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002222 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002223 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002224 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002225 __invalidate_all_iterators();
2226 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002227 traits_type::copy(_VSTD::__to_address(__p),
2228 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002229 if (__n_add != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002230 traits_type::copy(_VSTD::__to_address(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002231 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2232 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002233 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2234 _VSTD::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002235 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002236 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002237 __set_long_pointer(__p);
2238 __set_long_cap(__cap+1);
2239 __old_sz = __n_copy + __n_add + __sec_cp_sz;
2240 __set_long_size(__old_sz);
2241 traits_type::assign(__p[__old_sz], value_type());
2242}
2243
2244template <class _CharT, class _Traits, class _Allocator>
2245void
2246basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2247 size_type __n_copy, size_type __n_del, size_type __n_add)
2248{
2249 size_type __ms = max_size();
Marshall Clow2267c5d2013-11-06 14:24:38 +00002250 if (__delta_cap > __ms - __old_cap)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002251 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002252 pointer __old_p = __get_pointer();
2253 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002254 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002255 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002256 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002257 __invalidate_all_iterators();
2258 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002259 traits_type::copy(_VSTD::__to_address(__p),
2260 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002261 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2262 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002263 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2264 _VSTD::__to_address(__old_p) + __n_copy + __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002265 __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002266 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002267 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002268 __set_long_pointer(__p);
2269 __set_long_cap(__cap+1);
2270}
2271
2272// assign
2273
2274template <class _CharT, class _Traits, class _Allocator>
Martijn Vels596e3de2020-02-26 15:55:49 -05002275template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04002276basic_string<_CharT, _Traits, _Allocator>&
2277basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(
Martijn Vels596e3de2020-02-26 15:55:49 -05002278 const value_type* __s, size_type __n) {
2279 size_type __cap = __is_short ? __min_cap : __get_long_cap();
2280 if (__n < __cap) {
2281 pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
2282 __is_short ? __set_short_size(__n) : __set_long_size(__n);
2283 traits_type::copy(_VSTD::__to_address(__p), __s, __n);
2284 traits_type::assign(__p[__n], value_type());
2285 __invalidate_iterators_past(__n);
2286 } else {
2287 size_type __sz = __is_short ? __get_short_size() : __get_long_size();
2288 __grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
2289 }
Martijn Velsb6a08b62020-04-10 18:36:31 -04002290 return *this;
Martijn Vels596e3de2020-02-26 15:55:49 -05002291}
2292
2293template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002294basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002295basic_string<_CharT, _Traits, _Allocator>::__assign_external(
2296 const value_type* __s, size_type __n) {
2297 size_type __cap = capacity();
2298 if (__cap >= __n) {
2299 value_type* __p = _VSTD::__to_address(__get_pointer());
2300 traits_type::move(__p, __s, __n);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002301 return __null_terminate_at(__p, __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04002302 } else {
2303 size_type __sz = size();
2304 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002305 return *this;
Martijn Velsda7d94f2020-06-19 14:24:03 -04002306 }
Martijn Velsda7d94f2020-06-19 14:24:03 -04002307}
2308
2309template <class _CharT, class _Traits, class _Allocator>
2310basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002311basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002312{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002313 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Nikolas Klauser93826d12022-01-04 17:24:03 +01002314 return (__builtin_constant_p(__n) && __fits_in_sso(__n))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002315 ? __assign_short(__s, __n)
2316 : __assign_external(__s, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002317}
2318
2319template <class _CharT, class _Traits, class _Allocator>
2320basic_string<_CharT, _Traits, _Allocator>&
2321basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2322{
2323 size_type __cap = capacity();
2324 if (__cap < __n)
2325 {
2326 size_type __sz = size();
2327 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2328 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002329 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002330 traits_type::assign(__p, __n, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002331 return __null_terminate_at(__p, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002332}
2333
2334template <class _CharT, class _Traits, class _Allocator>
2335basic_string<_CharT, _Traits, _Allocator>&
2336basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2337{
2338 pointer __p;
2339 if (__is_long())
2340 {
2341 __p = __get_long_pointer();
2342 __set_long_size(1);
2343 }
2344 else
2345 {
2346 __p = __get_short_pointer();
2347 __set_short_size(1);
2348 }
2349 traits_type::assign(*__p, __c);
2350 traits_type::assign(*++__p, value_type());
2351 __invalidate_iterators_past(1);
2352 return *this;
2353}
2354
2355template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002356basic_string<_CharT, _Traits, _Allocator>&
2357basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2358{
Martijn Vels596e3de2020-02-26 15:55:49 -05002359 if (this != &__str) {
2360 __copy_assign_alloc(__str);
2361 if (!__is_long()) {
2362 if (!__str.__is_long()) {
Eric Fiselier03bbc922020-01-15 17:27:10 -05002363 __r_.first().__r = __str.__r_.first().__r;
Martijn Vels596e3de2020-02-26 15:55:49 -05002364 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002365 return __assign_no_alias<true>(__str.data(), __str.size());
Martijn Vels596e3de2020-02-26 15:55:49 -05002366 }
2367 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002368 return __assign_no_alias<false>(__str.data(), __str.size());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002369 }
Martijn Vels596e3de2020-02-26 15:55:49 -05002370 }
2371 return *this;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002372}
2373
Eric Fiselierfc92be82017-04-19 00:28:44 +00002374#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002375
2376template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002377inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002378void
2379basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002380 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002381{
2382 if (__alloc() != __str.__alloc())
2383 assign(__str);
2384 else
2385 __move_assign(__str, true_type());
2386}
2387
2388template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002389inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002390void
2391basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002392#if _LIBCPP_STD_VER > 14
2393 _NOEXCEPT
2394#else
Howard Hinnant3e276872011-06-03 18:40:47 +00002395 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002396#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002397{
marshall2ed41622019-11-27 07:13:00 -08002398 if (__is_long()) {
2399 __alloc_traits::deallocate(__alloc(), __get_long_pointer(),
2400 __get_long_cap());
2401#if _LIBCPP_STD_VER <= 14
2402 if (!is_nothrow_move_assignable<allocator_type>::value) {
2403 __set_short_size(0);
2404 traits_type::assign(__get_short_pointer()[0], value_type());
2405 }
2406#endif
2407 }
2408 __move_assign_alloc(__str);
2409 __r_.first() = __str.__r_.first();
2410 __str.__set_short_size(0);
2411 traits_type::assign(__str.__get_short_pointer()[0], value_type());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002412}
2413
2414template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002415inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002416basic_string<_CharT, _Traits, _Allocator>&
2417basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002418 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002419{
2420 __move_assign(__str, integral_constant<bool,
2421 __alloc_traits::propagate_on_container_move_assignment::value>());
2422 return *this;
2423}
2424
2425#endif
2426
2427template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002428template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002429__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002430<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002431 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002432 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002433>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002434basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2435{
Marshall Clow958362f2016-09-05 01:54:30 +00002436 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002437 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002438 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002439}
2440
2441template <class _CharT, class _Traits, class _Allocator>
2442template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002443__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002444<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002445 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002446 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002447>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002448basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2449{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002450 size_type __cap = capacity();
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002451 size_type __n = __string_is_trivial_iterator<_ForwardIterator>::value ?
2452 static_cast<size_type>(_VSTD::distance(__first, __last)) : 0;
2453
2454 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2455 (__cap >= __n || !__addr_in_range(*__first)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002456 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002457 if (__cap < __n)
2458 {
2459 size_type __sz = size();
2460 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2461 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002462 pointer __p = __get_pointer();
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002463 for (; __first != __last; ++__p, (void) ++__first)
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002464 traits_type::assign(*__p, *__first);
2465 traits_type::assign(*__p, value_type());
2466 __set_size(__n);
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04002467 __invalidate_iterators_past(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002468 }
2469 else
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002470 {
2471 const basic_string __temp(__first, __last, __alloc());
2472 assign(__temp.data(), __temp.size());
2473 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002474 return *this;
2475}
2476
2477template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002478basic_string<_CharT, _Traits, _Allocator>&
2479basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2480{
2481 size_type __sz = __str.size();
2482 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002483 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002484 return assign(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002485}
2486
2487template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002488template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002489__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002490<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002491 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
2492 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00002493 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002494>
Marshall Clow82513342016-09-24 22:45:42 +00002495basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002496{
Marshall Clow82513342016-09-24 22:45:42 +00002497 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002498 size_type __sz = __sv.size();
2499 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002500 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002501 return assign(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2502}
2503
2504
2505template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002506basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002507basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s) {
2508 return __assign_external(__s, traits_type::length(__s));
2509}
2510
2511template <class _CharT, class _Traits, class _Allocator>
2512basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002513basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002514{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002515 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Louis Dionne3e0c4332021-08-31 10:49:06 -04002516 return __builtin_constant_p(*__s)
Nikolas Klauser93826d12022-01-04 17:24:03 +01002517 ? (__fits_in_sso(traits_type::length(__s))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002518 ? __assign_short(__s, traits_type::length(__s))
2519 : __assign_external(__s, traits_type::length(__s)))
2520 : __assign_external(__s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002521}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002522// append
2523
2524template <class _CharT, class _Traits, class _Allocator>
2525basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002526basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002527{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002528 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002529 size_type __cap = capacity();
2530 size_type __sz = size();
2531 if (__cap - __sz >= __n)
2532 {
2533 if (__n)
2534 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002535 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002536 traits_type::copy(__p + __sz, __s, __n);
2537 __sz += __n;
2538 __set_size(__sz);
2539 traits_type::assign(__p[__sz], value_type());
2540 }
2541 }
2542 else
2543 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2544 return *this;
2545}
2546
2547template <class _CharT, class _Traits, class _Allocator>
2548basic_string<_CharT, _Traits, _Allocator>&
2549basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2550{
2551 if (__n)
2552 {
2553 size_type __cap = capacity();
2554 size_type __sz = size();
2555 if (__cap - __sz < __n)
2556 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2557 pointer __p = __get_pointer();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002558 traits_type::assign(_VSTD::__to_address(__p) + __sz, __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002559 __sz += __n;
2560 __set_size(__sz);
2561 traits_type::assign(__p[__sz], value_type());
2562 }
2563 return *this;
2564}
2565
2566template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00002567inline void
2568basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n)
2569{
2570 if (__n)
2571 {
2572 size_type __cap = capacity();
2573 size_type __sz = size();
2574 if (__cap - __sz < __n)
2575 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2576 pointer __p = __get_pointer();
2577 __sz += __n;
2578 __set_size(__sz);
2579 traits_type::assign(__p[__sz], value_type());
2580 }
2581}
2582
2583template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002584void
2585basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2586{
Howard Hinnant68bf1812013-04-30 21:44:48 +00002587 bool __is_short = !__is_long();
2588 size_type __cap;
2589 size_type __sz;
2590 if (__is_short)
2591 {
2592 __cap = __min_cap - 1;
2593 __sz = __get_short_size();
2594 }
2595 else
2596 {
2597 __cap = __get_long_cap() - 1;
2598 __sz = __get_long_size();
2599 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002600 if (__sz == __cap)
Howard Hinnant68bf1812013-04-30 21:44:48 +00002601 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002602 __grow_by(__cap, 1, __sz, __sz, 0);
Louis Dionne82d2b2c2021-11-16 11:26:56 -05002603 __is_short = false; // the string is always long after __grow_by
Howard Hinnant68bf1812013-04-30 21:44:48 +00002604 }
2605 pointer __p;
2606 if (__is_short)
2607 {
2608 __p = __get_short_pointer() + __sz;
2609 __set_short_size(__sz+1);
2610 }
2611 else
2612 {
2613 __p = __get_long_pointer() + __sz;
2614 __set_long_size(__sz+1);
2615 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002616 traits_type::assign(*__p, __c);
2617 traits_type::assign(*++__p, value_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002618}
2619
Howard Hinnantc51e1022010-05-11 19:42:16 +00002620template <class _CharT, class _Traits, class _Allocator>
2621template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002622__enable_if_t
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002623<
2624 __is_cpp17_forward_iterator<_ForwardIterator>::value,
2625 basic_string<_CharT, _Traits, _Allocator>&
2626>
2627basic_string<_CharT, _Traits, _Allocator>::append(
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002628 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002629{
2630 size_type __sz = size();
2631 size_type __cap = capacity();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002632 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002633 if (__n)
2634 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002635 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2636 !__addr_in_range(*__first))
Marshall Clow958362f2016-09-05 01:54:30 +00002637 {
2638 if (__cap - __sz < __n)
2639 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2640 pointer __p = __get_pointer() + __sz;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002641 for (; __first != __last; ++__p, (void) ++__first)
Marshall Clow958362f2016-09-05 01:54:30 +00002642 traits_type::assign(*__p, *__first);
2643 traits_type::assign(*__p, value_type());
2644 __set_size(__sz + __n);
2645 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002646 else
2647 {
2648 const basic_string __temp(__first, __last, __alloc());
2649 append(__temp.data(), __temp.size());
2650 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002651 }
2652 return *this;
2653}
2654
2655template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002656inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002657basic_string<_CharT, _Traits, _Allocator>&
2658basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2659{
2660 return append(__str.data(), __str.size());
2661}
2662
2663template <class _CharT, class _Traits, class _Allocator>
2664basic_string<_CharT, _Traits, _Allocator>&
2665basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2666{
2667 size_type __sz = __str.size();
2668 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002669 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002670 return append(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002671}
2672
2673template <class _CharT, class _Traits, class _Allocator>
Marshall Clow62953962016-10-03 23:40:48 +00002674template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002675 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002676 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002677 __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 +00002678 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002679 >
Marshall Clow82513342016-09-24 22:45:42 +00002680basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002681{
Marshall Clow82513342016-09-24 22:45:42 +00002682 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002683 size_type __sz = __sv.size();
2684 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002685 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002686 return append(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2687}
2688
2689template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002690basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002691basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002692{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002693 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002694 return append(__s, traits_type::length(__s));
2695}
2696
2697// insert
2698
2699template <class _CharT, class _Traits, class _Allocator>
2700basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002701basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002702{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002703 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002704 size_type __sz = size();
2705 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002706 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002707 size_type __cap = capacity();
2708 if (__cap - __sz >= __n)
2709 {
2710 if (__n)
2711 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002712 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002713 size_type __n_move = __sz - __pos;
2714 if (__n_move != 0)
2715 {
2716 if (__p + __pos <= __s && __s < __p + __sz)
2717 __s += __n;
2718 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2719 }
2720 traits_type::move(__p + __pos, __s, __n);
2721 __sz += __n;
2722 __set_size(__sz);
2723 traits_type::assign(__p[__sz], value_type());
2724 }
2725 }
2726 else
2727 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2728 return *this;
2729}
2730
2731template <class _CharT, class _Traits, class _Allocator>
2732basic_string<_CharT, _Traits, _Allocator>&
2733basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2734{
2735 size_type __sz = size();
2736 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002737 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002738 if (__n)
2739 {
2740 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002741 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002742 if (__cap - __sz >= __n)
2743 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002744 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002745 size_type __n_move = __sz - __pos;
2746 if (__n_move != 0)
2747 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2748 }
2749 else
2750 {
2751 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002752 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002753 }
2754 traits_type::assign(__p + __pos, __n, __c);
2755 __sz += __n;
2756 __set_size(__sz);
2757 traits_type::assign(__p[__sz], value_type());
2758 }
2759 return *this;
2760}
2761
2762template <class _CharT, class _Traits, class _Allocator>
2763template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002764__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002765<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002766 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Marshall Clow039b2f02016-01-13 21:54:34 +00002767 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002768>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002769basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2770{
Nikolas Klausereed25832021-12-15 01:32:30 +01002771 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2772 "string::insert(iterator, range) called with an iterator not"
2773 " referring to this string");
2774 const basic_string __temp(__first, __last, __alloc());
2775 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002776}
2777
2778template <class _CharT, class _Traits, class _Allocator>
2779template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002780__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002781<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002782 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002783 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002784>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002785basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2786{
Nikolas Klausereed25832021-12-15 01:32:30 +01002787 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2788 "string::insert(iterator, range) called with an iterator not"
2789 " referring to this string");
2790
Howard Hinnantc51e1022010-05-11 19:42:16 +00002791 size_type __ip = static_cast<size_type>(__pos - begin());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002792 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002793 if (__n)
2794 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002795 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2796 !__addr_in_range(*__first))
2797 {
2798 size_type __sz = size();
2799 size_type __cap = capacity();
2800 value_type* __p;
2801 if (__cap - __sz >= __n)
2802 {
2803 __p = _VSTD::__to_address(__get_pointer());
2804 size_type __n_move = __sz - __ip;
2805 if (__n_move != 0)
2806 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
2807 }
2808 else
2809 {
2810 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
2811 __p = _VSTD::__to_address(__get_long_pointer());
2812 }
2813 __sz += __n;
2814 __set_size(__sz);
2815 traits_type::assign(__p[__sz], value_type());
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002816 for (__p += __ip; __first != __last; ++__p, (void) ++__first)
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002817 traits_type::assign(*__p, *__first);
2818 }
2819 else
Marshall Clow958362f2016-09-05 01:54:30 +00002820 {
2821 const basic_string __temp(__first, __last, __alloc());
2822 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
2823 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002824 }
2825 return begin() + __ip;
2826}
2827
2828template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002829inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002830basic_string<_CharT, _Traits, _Allocator>&
2831basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2832{
2833 return insert(__pos1, __str.data(), __str.size());
2834}
2835
2836template <class _CharT, class _Traits, class _Allocator>
2837basic_string<_CharT, _Traits, _Allocator>&
2838basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2839 size_type __pos2, size_type __n)
2840{
2841 size_type __str_sz = __str.size();
2842 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002843 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002844 return insert(__pos1, __str.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002845}
2846
2847template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002848template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002849__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002850<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002851 __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 +00002852 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002853>
Marshall Clow82513342016-09-24 22:45:42 +00002854basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002855 size_type __pos2, size_type __n)
2856{
Marshall Clow82513342016-09-24 22:45:42 +00002857 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002858 size_type __str_sz = __sv.size();
2859 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002860 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002861 return insert(__pos1, __sv.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
2862}
2863
2864template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002865basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002866basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002867{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002868 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002869 return insert(__pos, __s, traits_type::length(__s));
2870}
2871
2872template <class _CharT, class _Traits, class _Allocator>
2873typename basic_string<_CharT, _Traits, _Allocator>::iterator
2874basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2875{
Louis Dionnef4cc6fb2022-02-15 15:47:45 -05002876 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2877 "string::insert(iterator, character) called with an iterator not"
2878 " referring to this string");
2879
Howard Hinnantc51e1022010-05-11 19:42:16 +00002880 size_type __ip = static_cast<size_type>(__pos - begin());
2881 size_type __sz = size();
2882 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002883 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002884 if (__cap == __sz)
2885 {
2886 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002887 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002888 }
2889 else
2890 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002891 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002892 size_type __n_move = __sz - __ip;
2893 if (__n_move != 0)
2894 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2895 }
2896 traits_type::assign(__p[__ip], __c);
2897 traits_type::assign(__p[++__sz], value_type());
2898 __set_size(__sz);
2899 return begin() + static_cast<difference_type>(__ip);
2900}
2901
2902template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002903inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002904typename basic_string<_CharT, _Traits, _Allocator>::iterator
2905basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2906{
Nikolas Klausereed25832021-12-15 01:32:30 +01002907 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2908 "string::insert(iterator, n, value) called with an iterator not"
2909 " referring to this string");
2910 difference_type __p = __pos - begin();
2911 insert(static_cast<size_type>(__p), __n, __c);
2912 return begin() + __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002913}
2914
2915// replace
2916
2917template <class _CharT, class _Traits, class _Allocator>
2918basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002919basic_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 +00002920 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00002921{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002922 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002923 size_type __sz = size();
2924 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002925 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002926 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002927 size_type __cap = capacity();
2928 if (__cap - __sz + __n1 >= __n2)
2929 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002930 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002931 if (__n1 != __n2)
2932 {
2933 size_type __n_move = __sz - __pos - __n1;
2934 if (__n_move != 0)
2935 {
2936 if (__n1 > __n2)
2937 {
2938 traits_type::move(__p + __pos, __s, __n2);
2939 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002940 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002941 }
2942 if (__p + __pos < __s && __s < __p + __sz)
2943 {
2944 if (__p + __pos + __n1 <= __s)
2945 __s += __n2 - __n1;
2946 else // __p + __pos < __s < __p + __pos + __n1
2947 {
2948 traits_type::move(__p + __pos, __s, __n1);
2949 __pos += __n1;
2950 __s += __n2;
2951 __n2 -= __n1;
2952 __n1 = 0;
2953 }
2954 }
2955 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2956 }
2957 }
2958 traits_type::move(__p + __pos, __s, __n2);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002959 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002960 }
2961 else
2962 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
2963 return *this;
2964}
2965
2966template <class _CharT, class _Traits, class _Allocator>
2967basic_string<_CharT, _Traits, _Allocator>&
2968basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
2969{
2970 size_type __sz = size();
2971 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002972 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002973 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002974 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002975 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002976 if (__cap - __sz + __n1 >= __n2)
2977 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002978 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002979 if (__n1 != __n2)
2980 {
2981 size_type __n_move = __sz - __pos - __n1;
2982 if (__n_move != 0)
2983 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2984 }
2985 }
2986 else
2987 {
2988 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002989 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002990 }
2991 traits_type::assign(__p + __pos, __n2, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002992 return __null_terminate_at(__p, __sz - (__n1 - __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002993}
2994
2995template <class _CharT, class _Traits, class _Allocator>
2996template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002997__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002998<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05002999 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003000 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003001>
Howard Hinnant990d6e82010-11-17 21:11:40 +00003002basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003003 _InputIterator __j1, _InputIterator __j2)
3004{
Marshall Clow958362f2016-09-05 01:54:30 +00003005 const basic_string __temp(__j1, __j2, __alloc());
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003006 return replace(__i1, __i2, __temp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003007}
3008
3009template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003010inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003011basic_string<_CharT, _Traits, _Allocator>&
3012basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
3013{
3014 return replace(__pos1, __n1, __str.data(), __str.size());
3015}
3016
3017template <class _CharT, class _Traits, class _Allocator>
3018basic_string<_CharT, _Traits, _Allocator>&
3019basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
3020 size_type __pos2, size_type __n2)
3021{
3022 size_type __str_sz = __str.size();
3023 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003024 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003025 return replace(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003026}
3027
3028template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003029template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003030__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003031<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003032 __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 +00003033 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003034>
Marshall Clow82513342016-09-24 22:45:42 +00003035basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003036 size_type __pos2, size_type __n2)
3037{
Marshall Clow82513342016-09-24 22:45:42 +00003038 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003039 size_type __str_sz = __sv.size();
3040 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003041 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003042 return replace(__pos1, __n1, __sv.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
3043}
3044
3045template <class _CharT, class _Traits, class _Allocator>
3046basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003047basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003048{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003049 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003050 return replace(__pos, __n1, __s, traits_type::length(__s));
3051}
3052
3053template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003054inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003055basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003056basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003057{
3058 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
3059 __str.data(), __str.size());
3060}
3061
3062template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003063inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003064basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003065basic_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 +00003066{
3067 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
3068}
3069
3070template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003071inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003072basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003073basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003074{
3075 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
3076}
3077
3078template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003079inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003080basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003081basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003082{
3083 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
3084}
3085
3086// erase
3087
Martijn Velsa81fc792020-02-26 13:25:43 -05003088// 'externally instantiated' erase() implementation, called when __n != npos.
3089// Does not check __pos against size()
Howard Hinnantc51e1022010-05-11 19:42:16 +00003090template <class _CharT, class _Traits, class _Allocator>
Martijn Velsa81fc792020-02-26 13:25:43 -05003091void
3092basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(
3093 size_type __pos, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003094{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003095 if (__n)
3096 {
Martijn Velsa81fc792020-02-26 13:25:43 -05003097 size_type __sz = size();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003098 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003099 __n = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003100 size_type __n_move = __sz - __pos - __n;
3101 if (__n_move != 0)
3102 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003103 __null_terminate_at(__p, __sz - __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003104 }
Martijn Velsa81fc792020-02-26 13:25:43 -05003105}
3106
3107template <class _CharT, class _Traits, class _Allocator>
3108basic_string<_CharT, _Traits, _Allocator>&
3109basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos,
3110 size_type __n) {
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003111 if (__pos > size())
3112 __throw_out_of_range();
Martijn Velsa81fc792020-02-26 13:25:43 -05003113 if (__n == npos) {
3114 __erase_to_end(__pos);
3115 } else {
3116 __erase_external_with_move(__pos, __n);
3117 }
3118 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003119}
3120
3121template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003122inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003123typename basic_string<_CharT, _Traits, _Allocator>::iterator
3124basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
3125{
Nikolas Klausereed25832021-12-15 01:32:30 +01003126 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3127 "string::erase(iterator) called with an iterator not"
3128 " referring to this string");
3129
3130 _LIBCPP_ASSERT(__pos != end(), "string::erase(iterator) called with a non-dereferenceable iterator");
3131 iterator __b = begin();
3132 size_type __r = static_cast<size_type>(__pos - __b);
3133 erase(__r, 1);
3134 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003135}
3136
3137template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003138inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003139typename basic_string<_CharT, _Traits, _Allocator>::iterator
3140basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
3141{
Nikolas Klausereed25832021-12-15 01:32:30 +01003142 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
3143 "string::erase(iterator, iterator) called with an iterator not"
3144 " referring to this string");
3145
3146 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
3147 iterator __b = begin();
3148 size_type __r = static_cast<size_type>(__first - __b);
3149 erase(__r, static_cast<size_type>(__last - __first));
3150 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003151}
3152
3153template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003154inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003155void
3156basic_string<_CharT, _Traits, _Allocator>::pop_back()
3157{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003158 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003159 __erase_to_end(size() - 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003160}
3161
3162template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003163inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003164void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003165basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003166{
3167 __invalidate_all_iterators();
3168 if (__is_long())
3169 {
3170 traits_type::assign(*__get_long_pointer(), value_type());
3171 __set_long_size(0);
3172 }
3173 else
3174 {
3175 traits_type::assign(*__get_short_pointer(), value_type());
3176 __set_short_size(0);
3177 }
3178}
3179
3180template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003181inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003182void
3183basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
3184{
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003185 __null_terminate_at(_VSTD::__to_address(__get_pointer()), __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003186}
3187
3188template <class _CharT, class _Traits, class _Allocator>
3189void
3190basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
3191{
3192 size_type __sz = size();
3193 if (__n > __sz)
3194 append(__n - __sz, __c);
3195 else
3196 __erase_to_end(__n);
3197}
3198
3199template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00003200inline void
3201basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n)
3202{
3203 size_type __sz = size();
3204 if (__n > __sz) {
3205 __append_default_init(__n - __sz);
3206 } else
3207 __erase_to_end(__n);
3208}
3209
3210template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003211inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003212typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003213basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003214{
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003215 size_type __m = __alloc_traits::max_size(__alloc());
Eric Fiseliere9cc5922017-10-17 13:16:01 +00003216#ifdef _LIBCPP_BIG_ENDIAN
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003217 return (__m <= ~__long_mask ? __m : __m/2) - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003218#else
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003219 return __m - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003220#endif
3221}
3222
3223template <class _CharT, class _Traits, class _Allocator>
3224void
Marek Kurdejc9848142020-11-26 10:07:16 +01003225basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __requested_capacity)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003226{
Marek Kurdejc9848142020-11-26 10:07:16 +01003227 if (__requested_capacity > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003228 __throw_length_error();
Marek Kurdejc9848142020-11-26 10:07:16 +01003229
Louis Dionne05b6d3e2022-01-14 12:30:22 -05003230 // Make sure reserve(n) never shrinks. This is technically only required in C++20
3231 // and later (since P0966R1), however we provide consistent behavior in all Standard
3232 // modes because this function is instantiated in the shared library.
3233 if (__requested_capacity <= capacity())
3234 return;
Marek Kurdejc9848142020-11-26 10:07:16 +01003235
3236 size_type __target_capacity = _VSTD::max(__requested_capacity, size());
3237 __target_capacity = __recommend(__target_capacity);
3238 if (__target_capacity == capacity()) return;
3239
3240 __shrink_or_extend(__target_capacity);
3241}
3242
3243template <class _CharT, class _Traits, class _Allocator>
Louis Dionnee38b8642021-12-13 14:15:21 -05003244inline
Marek Kurdejc9848142020-11-26 10:07:16 +01003245void
3246basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT
3247{
3248 size_type __target_capacity = __recommend(size());
3249 if (__target_capacity == capacity()) return;
3250
3251 __shrink_or_extend(__target_capacity);
3252}
3253
3254template <class _CharT, class _Traits, class _Allocator>
Louis Dionnee38b8642021-12-13 14:15:21 -05003255inline
Marek Kurdejc9848142020-11-26 10:07:16 +01003256void
3257basic_string<_CharT, _Traits, _Allocator>::__shrink_or_extend(size_type __target_capacity)
3258{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003259 size_type __cap = capacity();
3260 size_type __sz = size();
Marek Kurdejc9848142020-11-26 10:07:16 +01003261
3262 pointer __new_data, __p;
3263 bool __was_long, __now_long;
3264 if (__target_capacity == __min_cap - 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003265 {
Marek Kurdejc9848142020-11-26 10:07:16 +01003266 __was_long = true;
3267 __now_long = false;
3268 __new_data = __get_short_pointer();
3269 __p = __get_long_pointer();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003270 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003271 else
3272 {
3273 if (__target_capacity > __cap)
3274 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3275 else
3276 {
3277 #ifndef _LIBCPP_NO_EXCEPTIONS
3278 try
3279 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003280 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003281 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3282 #ifndef _LIBCPP_NO_EXCEPTIONS
3283 }
3284 catch (...)
3285 {
3286 return;
3287 }
3288 #else // _LIBCPP_NO_EXCEPTIONS
3289 if (__new_data == nullptr)
3290 return;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003291 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003292 }
3293 __now_long = true;
3294 __was_long = __is_long();
3295 __p = __get_pointer();
3296 }
3297 traits_type::copy(_VSTD::__to_address(__new_data),
3298 _VSTD::__to_address(__p), size()+1);
3299 if (__was_long)
3300 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
3301 if (__now_long)
3302 {
3303 __set_long_cap(__target_capacity+1);
3304 __set_long_size(__sz);
3305 __set_long_pointer(__new_data);
3306 }
3307 else
3308 __set_short_size(__sz);
3309 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003310}
3311
3312template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003313inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003314typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003315basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003316{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003317 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003318 return *(data() + __pos);
3319}
3320
3321template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003322inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003323typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003324basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003325{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003326 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003327 return *(__get_pointer() + __pos);
3328}
3329
3330template <class _CharT, class _Traits, class _Allocator>
3331typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3332basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3333{
3334 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003335 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003336 return (*this)[__n];
3337}
3338
3339template <class _CharT, class _Traits, class _Allocator>
3340typename basic_string<_CharT, _Traits, _Allocator>::reference
3341basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
3342{
3343 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003344 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003345 return (*this)[__n];
3346}
3347
3348template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003349inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003350typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003351basic_string<_CharT, _Traits, _Allocator>::front() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003352{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003353 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003354 return *__get_pointer();
3355}
3356
3357template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003358inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003359typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003360basic_string<_CharT, _Traits, _Allocator>::front() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003361{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003362 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003363 return *data();
3364}
3365
3366template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003367inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003368typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003369basic_string<_CharT, _Traits, _Allocator>::back() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003370{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003371 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003372 return *(__get_pointer() + size() - 1);
3373}
3374
3375template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003376inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003377typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003378basic_string<_CharT, _Traits, _Allocator>::back() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003379{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003380 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003381 return *(data() + size() - 1);
3382}
3383
3384template <class _CharT, class _Traits, class _Allocator>
3385typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003386basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003387{
3388 size_type __sz = size();
3389 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003390 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003391 size_type __rlen = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003392 traits_type::copy(__s, data() + __pos, __rlen);
3393 return __rlen;
3394}
3395
3396template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003397inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003398basic_string<_CharT, _Traits, _Allocator>
3399basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3400{
3401 return basic_string(*this, __pos, __n, __alloc());
3402}
3403
3404template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003405inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003406void
3407basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003408#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00003409 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00003410#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00003411 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003412 __is_nothrow_swappable<allocator_type>::value)
3413#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003414{
Louis Dionneba400782020-10-02 15:02:52 -04003415#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01003416 if (!__libcpp_is_constant_evaluated()) {
3417 if (!__is_long())
3418 __get_db()->__invalidate_all(this);
3419 if (!__str.__is_long())
3420 __get_db()->__invalidate_all(&__str);
3421 __get_db()->swap(this, &__str);
3422 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00003423#endif
Eric Fiselier9bf691f2016-12-28 05:53:01 +00003424 _LIBCPP_ASSERT(
3425 __alloc_traits::propagate_on_container_swap::value ||
3426 __alloc_traits::is_always_equal::value ||
3427 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003428 _VSTD::swap(__r_.first(), __str.__r_.first());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05003429 _VSTD::__swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003430}
3431
3432// find
3433
3434template <class _Traits>
3435struct _LIBCPP_HIDDEN __traits_eq
3436{
3437 typedef typename _Traits::char_type char_type;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003438 _LIBCPP_INLINE_VISIBILITY
3439 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3440 {return _Traits::eq(__x, __y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003441};
3442
3443template<class _CharT, class _Traits, class _Allocator>
3444typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003445basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003446 size_type __pos,
3447 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003448{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003449 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003450 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003451 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003452}
3453
3454template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003455inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003456typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003457basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3458 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003459{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003460 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003461 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003462}
3463
3464template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003465template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003466__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003467<
3468 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3469 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003470>
Marshall Clowe46031a2018-07-02 18:41:15 +00003471basic_string<_CharT, _Traits, _Allocator>::find(const _Tp &__t,
zoecarver1997e0a2021-02-05 11:54:47 -08003472 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003473{
Marshall Clowe46031a2018-07-02 18:41:15 +00003474 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003475 return __str_find<value_type, size_type, traits_type, npos>
3476 (data(), size(), __sv.data(), __pos, __sv.size());
3477}
3478
3479template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003480inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003481typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003482basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003483 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003484{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003485 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003486 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003487 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003488}
3489
3490template<class _CharT, class _Traits, class _Allocator>
3491typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003492basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3493 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003494{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003495 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003496 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003497}
3498
3499// rfind
3500
3501template<class _CharT, class _Traits, class _Allocator>
3502typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003503basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003504 size_type __pos,
3505 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003506{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003507 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003508 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003509 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003510}
3511
3512template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003513inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003514typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003515basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3516 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003517{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003518 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003519 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003520}
3521
3522template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003523template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003524__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003525<
3526 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3527 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003528>
Marshall Clowe46031a2018-07-02 18:41:15 +00003529basic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003530 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003531{
Marshall Clowe46031a2018-07-02 18:41:15 +00003532 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003533 return __str_rfind<value_type, size_type, traits_type, npos>
3534 (data(), size(), __sv.data(), __pos, __sv.size());
3535}
3536
3537template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003538inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003539typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003540basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003541 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003542{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003543 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003544 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003545 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003546}
3547
3548template<class _CharT, class _Traits, class _Allocator>
3549typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003550basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3551 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003552{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003553 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003554 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003555}
3556
3557// find_first_of
3558
3559template<class _CharT, class _Traits, class _Allocator>
3560typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003561basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003562 size_type __pos,
3563 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003564{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003565 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003566 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003567 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003568}
3569
3570template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003571inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003572typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003573basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3574 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003575{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003576 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003577 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003578}
3579
3580template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003581template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003582__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003583<
3584 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3585 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003586>
Marshall Clowe46031a2018-07-02 18:41:15 +00003587basic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003588 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003589{
Marshall Clowe46031a2018-07-02 18:41:15 +00003590 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003591 return __str_find_first_of<value_type, size_type, traits_type, npos>
3592 (data(), size(), __sv.data(), __pos, __sv.size());
3593}
3594
3595template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003596inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003597typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003598basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003599 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003600{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003601 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003602 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003603 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003604}
3605
3606template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003607inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003608typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003609basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3610 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003611{
3612 return find(__c, __pos);
3613}
3614
3615// find_last_of
3616
3617template<class _CharT, class _Traits, class _Allocator>
3618typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003619basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003620 size_type __pos,
3621 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003622{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003623 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003624 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003625 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003626}
3627
3628template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003629inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003630typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003631basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3632 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003633{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003634 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003635 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003636}
3637
3638template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003639template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003640__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003641<
3642 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3643 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003644>
Marshall Clowe46031a2018-07-02 18:41:15 +00003645basic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003646 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003647{
Marshall Clowe46031a2018-07-02 18:41:15 +00003648 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003649 return __str_find_last_of<value_type, size_type, traits_type, npos>
3650 (data(), size(), __sv.data(), __pos, __sv.size());
3651}
3652
3653template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003654inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003655typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003656basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003657 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003658{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003659 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003660 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003661 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003662}
3663
3664template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003665inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003666typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003667basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3668 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003669{
3670 return rfind(__c, __pos);
3671}
3672
3673// find_first_not_of
3674
3675template<class _CharT, class _Traits, class _Allocator>
3676typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003677basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003678 size_type __pos,
3679 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003680{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003681 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003682 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003683 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003684}
3685
3686template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003687inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003688typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003689basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3690 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003691{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003692 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003693 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003694}
3695
3696template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003697template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003698__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003699<
3700 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3701 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003702>
Marshall Clowe46031a2018-07-02 18:41:15 +00003703basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003704 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003705{
Marshall Clowe46031a2018-07-02 18:41:15 +00003706 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003707 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3708 (data(), size(), __sv.data(), __pos, __sv.size());
3709}
3710
3711template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003712inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003713typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003714basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003715 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003716{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003717 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003718 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003719 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003720}
3721
3722template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003723inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003724typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003725basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3726 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003727{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003728 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003729 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003730}
3731
3732// find_last_not_of
3733
3734template<class _CharT, class _Traits, class _Allocator>
3735typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003736basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003737 size_type __pos,
3738 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003739{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003740 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003741 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003742 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003743}
3744
3745template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003746inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003747typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003748basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3749 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003750{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003751 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003752 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003753}
3754
3755template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003756template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003757__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003758<
3759 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3760 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003761>
Marshall Clowe46031a2018-07-02 18:41:15 +00003762basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003763 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003764{
Marshall Clowe46031a2018-07-02 18:41:15 +00003765 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003766 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3767 (data(), size(), __sv.data(), __pos, __sv.size());
3768}
3769
3770template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003771inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003772typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003773basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003774 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003775{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003776 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003777 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003778 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003779}
3780
3781template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003782inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003783typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003784basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3785 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003786{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003787 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003788 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003789}
3790
3791// compare
3792
3793template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003794template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003795__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003796<
3797 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3798 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003799>
zoecarver1997e0a2021-02-05 11:54:47 -08003800basic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003801{
Marshall Clowe46031a2018-07-02 18:41:15 +00003802 __self_view __sv = __t;
Howard Hinnantb0485532011-07-24 21:45:06 +00003803 size_t __lhs_sz = size();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003804 size_t __rhs_sz = __sv.size();
3805 int __result = traits_type::compare(data(), __sv.data(),
Howard Hinnantb0485532011-07-24 21:45:06 +00003806 _VSTD::min(__lhs_sz, __rhs_sz));
3807 if (__result != 0)
3808 return __result;
3809 if (__lhs_sz < __rhs_sz)
3810 return -1;
3811 if (__lhs_sz > __rhs_sz)
3812 return 1;
3813 return 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003814}
3815
3816template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003817inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003818int
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003819basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003820{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003821 return compare(__self_view(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003822}
3823
3824template <class _CharT, class _Traits, class _Allocator>
3825int
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003826basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3827 size_type __n1,
Howard Hinnantd17880b2013-06-28 16:59:19 +00003828 const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003829 size_type __n2) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003830{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003831 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003832 size_type __sz = size();
3833 if (__pos1 > __sz || __n2 == npos)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003834 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003835 size_type __rlen = _VSTD::min(__n1, __sz - __pos1);
3836 int __r = traits_type::compare(data() + __pos1, __s, _VSTD::min(__rlen, __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003837 if (__r == 0)
3838 {
3839 if (__rlen < __n2)
3840 __r = -1;
3841 else if (__rlen > __n2)
3842 __r = 1;
3843 }
3844 return __r;
3845}
3846
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003847template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003848template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003849__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003850<
3851 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3852 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003853>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003854basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3855 size_type __n1,
Marshall Clowe46031a2018-07-02 18:41:15 +00003856 const _Tp& __t) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003857{
Marshall Clowe46031a2018-07-02 18:41:15 +00003858 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003859 return compare(__pos1, __n1, __sv.data(), __sv.size());
3860}
3861
3862template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003863inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003864int
3865basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3866 size_type __n1,
3867 const basic_string& __str) const
3868{
3869 return compare(__pos1, __n1, __str.data(), __str.size());
3870}
3871
3872template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003873template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003874__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003875<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003876 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
3877 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00003878 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003879>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003880basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3881 size_type __n1,
Marshall Clow82513342016-09-24 22:45:42 +00003882 const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003883 size_type __pos2,
3884 size_type __n2) const
3885{
Marshall Clow82513342016-09-24 22:45:42 +00003886 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003887 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3888}
3889
3890template <class _CharT, class _Traits, class _Allocator>
3891int
3892basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3893 size_type __n1,
3894 const basic_string& __str,
3895 size_type __pos2,
3896 size_type __n2) const
3897{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003898 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003899}
3900
3901template <class _CharT, class _Traits, class _Allocator>
3902int
3903basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
3904{
3905 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3906 return compare(0, npos, __s, traits_type::length(__s));
3907}
3908
3909template <class _CharT, class _Traits, class _Allocator>
3910int
3911basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3912 size_type __n1,
3913 const value_type* __s) const
3914{
3915 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3916 return compare(__pos1, __n1, __s, traits_type::length(__s));
3917}
3918
Howard Hinnantc51e1022010-05-11 19:42:16 +00003919// __invariants
3920
3921template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003922inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003923bool
3924basic_string<_CharT, _Traits, _Allocator>::__invariants() const
3925{
3926 if (size() > capacity())
3927 return false;
3928 if (capacity() < __min_cap - 1)
3929 return false;
Bruce Mitchener170d8972020-11-24 12:53:53 -05003930 if (data() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003931 return false;
Louis Dionne663415f2020-10-05 16:16:13 -04003932 if (data()[size()] != value_type())
Howard Hinnantc51e1022010-05-11 19:42:16 +00003933 return false;
3934 return true;
3935}
3936
Vedant Kumar55e007e2018-03-08 21:15:26 +00003937// __clear_and_shrink
3938
3939template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003940inline
Louis Dionne173f29e2019-05-29 16:01:36 +00003941void
Marshall Clowe60a7182018-05-29 17:04:37 +00003942basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
Vedant Kumar55e007e2018-03-08 21:15:26 +00003943{
3944 clear();
3945 if(__is_long())
3946 {
3947 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
3948 __set_long_cap(0);
3949 __set_short_size(0);
Louis Dionne663415f2020-10-05 16:16:13 -04003950 traits_type::assign(*__get_short_pointer(), value_type());
Vedant Kumar55e007e2018-03-08 21:15:26 +00003951 }
Louis Dionne173f29e2019-05-29 16:01:36 +00003952}
Vedant Kumar55e007e2018-03-08 21:15:26 +00003953
Howard Hinnantc51e1022010-05-11 19:42:16 +00003954// operator==
3955
3956template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003957inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003958bool
3959operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003960 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003961{
Howard Hinnantaaeb1132013-04-22 23:55:13 +00003962 size_t __lhs_sz = __lhs.size();
3963 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
3964 __rhs.data(),
3965 __lhs_sz) == 0;
3966}
3967
3968template<class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003969inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaaeb1132013-04-22 23:55:13 +00003970bool
3971operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
3972 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
3973{
3974 size_t __lhs_sz = __lhs.size();
3975 if (__lhs_sz != __rhs.size())
3976 return false;
3977 const char* __lp = __lhs.data();
3978 const char* __rp = __rhs.data();
3979 if (__lhs.__is_long())
3980 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
3981 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
3982 if (*__lp != *__rp)
3983 return false;
3984 return true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003985}
3986
3987template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003988inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003989bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003990operator==(const _CharT* __lhs,
3991 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003992{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00003993 typedef basic_string<_CharT, _Traits, _Allocator> _String;
3994 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
3995 size_t __lhs_len = _Traits::length(__lhs);
3996 if (__lhs_len != __rhs.size()) return false;
3997 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003998}
3999
Howard Hinnantc51e1022010-05-11 19:42:16 +00004000template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004001inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004002bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004003operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
4004 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004005{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004006 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4007 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
4008 size_t __rhs_len = _Traits::length(__rhs);
4009 if (__rhs_len != __lhs.size()) return false;
4010 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004011}
4012
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004013template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004014inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004015bool
4016operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004017 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004018{
4019 return !(__lhs == __rhs);
4020}
4021
4022template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004023inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004024bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004025operator!=(const _CharT* __lhs,
4026 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004027{
4028 return !(__lhs == __rhs);
4029}
4030
4031template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004032inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004033bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004034operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4035 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004036{
4037 return !(__lhs == __rhs);
4038}
4039
4040// operator<
4041
4042template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004043inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004044bool
4045operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004046 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004047{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004048 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004049}
4050
4051template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004052inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004053bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004054operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4055 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004056{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004057 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004058}
4059
4060template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004061inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004062bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004063operator< (const _CharT* __lhs,
4064 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004065{
4066 return __rhs.compare(__lhs) > 0;
4067}
4068
Howard Hinnantc51e1022010-05-11 19:42:16 +00004069// operator>
4070
4071template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004072inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004073bool
4074operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004075 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004076{
4077 return __rhs < __lhs;
4078}
4079
4080template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004081inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004082bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004083operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4084 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004085{
4086 return __rhs < __lhs;
4087}
4088
4089template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004090inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004091bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004092operator> (const _CharT* __lhs,
4093 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004094{
4095 return __rhs < __lhs;
4096}
4097
4098// operator<=
4099
4100template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004101inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004102bool
4103operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004104 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004105{
4106 return !(__rhs < __lhs);
4107}
4108
4109template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004110inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004111bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004112operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4113 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004114{
4115 return !(__rhs < __lhs);
4116}
4117
4118template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004119inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004120bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004121operator<=(const _CharT* __lhs,
4122 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004123{
4124 return !(__rhs < __lhs);
4125}
4126
4127// operator>=
4128
4129template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004130inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004131bool
4132operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004133 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004134{
4135 return !(__lhs < __rhs);
4136}
4137
4138template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004139inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004140bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004141operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4142 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004143{
4144 return !(__lhs < __rhs);
4145}
4146
4147template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004148inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004149bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004150operator>=(const _CharT* __lhs,
4151 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004152{
4153 return !(__lhs < __rhs);
4154}
4155
4156// operator +
4157
4158template<class _CharT, class _Traits, class _Allocator>
4159basic_string<_CharT, _Traits, _Allocator>
4160operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4161 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4162{
4163 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4164 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4165 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4166 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4167 __r.append(__rhs.data(), __rhs_sz);
4168 return __r;
4169}
4170
4171template<class _CharT, class _Traits, class _Allocator>
4172basic_string<_CharT, _Traits, _Allocator>
4173operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4174{
4175 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4176 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = _Traits::length(__lhs);
4177 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4178 __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz);
4179 __r.append(__rhs.data(), __rhs_sz);
4180 return __r;
4181}
4182
4183template<class _CharT, class _Traits, class _Allocator>
4184basic_string<_CharT, _Traits, _Allocator>
4185operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4186{
4187 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4188 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4189 __r.__init(&__lhs, 1, 1 + __rhs_sz);
4190 __r.append(__rhs.data(), __rhs_sz);
4191 return __r;
4192}
4193
4194template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004195inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004196basic_string<_CharT, _Traits, _Allocator>
4197operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
4198{
4199 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4200 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4201 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = _Traits::length(__rhs);
4202 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4203 __r.append(__rhs, __rhs_sz);
4204 return __r;
4205}
4206
4207template<class _CharT, class _Traits, class _Allocator>
4208basic_string<_CharT, _Traits, _Allocator>
4209operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
4210{
4211 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4212 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4213 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1);
4214 __r.push_back(__rhs);
4215 return __r;
4216}
4217
Eric Fiselierfc92be82017-04-19 00:28:44 +00004218#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004219
4220template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004221inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004222basic_string<_CharT, _Traits, _Allocator>
4223operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4224{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004225 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004226}
4227
4228template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004229inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004230basic_string<_CharT, _Traits, _Allocator>
4231operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4232{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004233 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004234}
4235
4236template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004237inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004238basic_string<_CharT, _Traits, _Allocator>
4239operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4240{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004241 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004242}
4243
4244template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004245inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004246basic_string<_CharT, _Traits, _Allocator>
4247operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4248{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004249 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004250}
4251
4252template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004253inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004254basic_string<_CharT, _Traits, _Allocator>
4255operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4256{
4257 __rhs.insert(__rhs.begin(), __lhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004258 return _VSTD::move(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004259}
4260
4261template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004262inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004263basic_string<_CharT, _Traits, _Allocator>
4264operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
4265{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004266 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004267}
4268
4269template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004270inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004271basic_string<_CharT, _Traits, _Allocator>
4272operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
4273{
4274 __lhs.push_back(__rhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004275 return _VSTD::move(__lhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004276}
4277
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004278#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004279
4280// swap
4281
4282template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004283inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004284void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004285swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +00004286 basic_string<_CharT, _Traits, _Allocator>& __rhs)
4287 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004288{
4289 __lhs.swap(__rhs);
4290}
4291
Bruce Mitchener170d8972020-11-24 12:53:53 -05004292_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = nullptr, int __base = 10);
4293_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = nullptr, int __base = 10);
4294_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = nullptr, int __base = 10);
4295_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = nullptr, int __base = 10);
4296_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004297
Bruce Mitchener170d8972020-11-24 12:53:53 -05004298_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = nullptr);
4299_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = nullptr);
4300_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004301
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004302_LIBCPP_FUNC_VIS string to_string(int __val);
4303_LIBCPP_FUNC_VIS string to_string(unsigned __val);
4304_LIBCPP_FUNC_VIS string to_string(long __val);
4305_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
4306_LIBCPP_FUNC_VIS string to_string(long long __val);
4307_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
4308_LIBCPP_FUNC_VIS string to_string(float __val);
4309_LIBCPP_FUNC_VIS string to_string(double __val);
4310_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004311
Louis Dionne89258142021-08-23 15:32:36 -04004312#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Bruce Mitchener170d8972020-11-24 12:53:53 -05004313_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4314_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4315_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4316_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4317_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004318
Bruce Mitchener170d8972020-11-24 12:53:53 -05004319_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = nullptr);
4320_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = nullptr);
4321_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004322
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004323_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
4324_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
4325_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
4326_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
4327_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
4328_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
4329_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
4330_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
4331_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Louis Dionne89258142021-08-23 15:32:36 -04004332#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004333
Howard Hinnantc51e1022010-05-11 19:42:16 +00004334template<class _CharT, class _Traits, class _Allocator>
Martin Storsjö88890c22021-02-22 01:13:13 +02004335_LIBCPP_TEMPLATE_DATA_VIS
Eric Fiselierc9fdaf12020-01-15 17:02:17 -05004336const typename basic_string<_CharT, _Traits, _Allocator>::size_type
4337 basic_string<_CharT, _Traits, _Allocator>::npos;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004338
Marshall Clow851b9ec2019-05-20 21:56:51 +00004339template <class _CharT, class _Allocator>
4340struct _LIBCPP_TEMPLATE_VIS
4341 hash<basic_string<_CharT, char_traits<_CharT>, _Allocator> >
4342 : public unary_function<
4343 basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004344{
4345 size_t
Marshall Clow851b9ec2019-05-20 21:56:51 +00004346 operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT
4347 { return __do_string_hash(__val.data(), __val.data() + __val.size()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004348};
4349
Howard Hinnantc51e1022010-05-11 19:42:16 +00004350
Howard Hinnantdc095972011-07-18 15:51:59 +00004351template<class _CharT, class _Traits, class _Allocator>
4352basic_ostream<_CharT, _Traits>&
4353operator<<(basic_ostream<_CharT, _Traits>& __os,
4354 const basic_string<_CharT, _Traits, _Allocator>& __str);
4355
4356template<class _CharT, class _Traits, class _Allocator>
4357basic_istream<_CharT, _Traits>&
4358operator>>(basic_istream<_CharT, _Traits>& __is,
4359 basic_string<_CharT, _Traits, _Allocator>& __str);
4360
4361template<class _CharT, class _Traits, class _Allocator>
4362basic_istream<_CharT, _Traits>&
4363getline(basic_istream<_CharT, _Traits>& __is,
4364 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4365
4366template<class _CharT, class _Traits, class _Allocator>
4367inline _LIBCPP_INLINE_VISIBILITY
4368basic_istream<_CharT, _Traits>&
4369getline(basic_istream<_CharT, _Traits>& __is,
4370 basic_string<_CharT, _Traits, _Allocator>& __str);
4371
Howard Hinnantdc095972011-07-18 15:51:59 +00004372template<class _CharT, class _Traits, class _Allocator>
4373inline _LIBCPP_INLINE_VISIBILITY
4374basic_istream<_CharT, _Traits>&
4375getline(basic_istream<_CharT, _Traits>&& __is,
4376 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4377
4378template<class _CharT, class _Traits, class _Allocator>
4379inline _LIBCPP_INLINE_VISIBILITY
4380basic_istream<_CharT, _Traits>&
4381getline(basic_istream<_CharT, _Traits>&& __is,
4382 basic_string<_CharT, _Traits, _Allocator>& __str);
4383
Marshall Clow29b53f22018-12-14 18:49:35 +00004384#if _LIBCPP_STD_VER > 17
Marek Kurdeja98b1412020-05-02 13:58:03 +02004385template <class _CharT, class _Traits, class _Allocator, class _Up>
Marshall Clow29b53f22018-12-14 18:49:35 +00004386inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004387 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4388 erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
4389 auto __old_size = __str.size();
4390 __str.erase(_VSTD::remove(__str.begin(), __str.end(), __v), __str.end());
4391 return __old_size - __str.size();
4392}
Marshall Clow29b53f22018-12-14 18:49:35 +00004393
Marek Kurdeja98b1412020-05-02 13:58:03 +02004394template <class _CharT, class _Traits, class _Allocator, class _Predicate>
Marshall Clow29b53f22018-12-14 18:49:35 +00004395inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004396 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4397 erase_if(basic_string<_CharT, _Traits, _Allocator>& __str,
4398 _Predicate __pred) {
4399 auto __old_size = __str.size();
4400 __str.erase(_VSTD::remove_if(__str.begin(), __str.end(), __pred),
4401 __str.end());
4402 return __old_size - __str.size();
4403}
Marshall Clow29b53f22018-12-14 18:49:35 +00004404#endif
4405
Louis Dionneba400782020-10-02 15:02:52 -04004406#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004407
4408template<class _CharT, class _Traits, class _Allocator>
4409bool
4410basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4411{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004412 return data() <= _VSTD::__to_address(__i->base()) &&
4413 _VSTD::__to_address(__i->base()) < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004414}
4415
4416template<class _CharT, class _Traits, class _Allocator>
4417bool
4418basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4419{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004420 return data() < _VSTD::__to_address(__i->base()) &&
4421 _VSTD::__to_address(__i->base()) <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004422}
4423
4424template<class _CharT, class _Traits, class _Allocator>
4425bool
4426basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4427{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004428 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004429 return data() <= __p && __p <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004430}
4431
4432template<class _CharT, class _Traits, class _Allocator>
4433bool
4434basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4435{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004436 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004437 return data() <= __p && __p < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004438}
4439
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004440#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004441
Louis Dionne173f29e2019-05-29 16:01:36 +00004442#if _LIBCPP_STD_VER > 11
Marshall Clowcba751f2013-07-23 17:05:24 +00004443// Literal suffixes for basic_string [basic.string.literals]
Marshall Clowac868372013-10-05 21:18:32 +00004444inline namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004445{
4446 inline namespace string_literals
4447 {
Howard Hinnant5c167562013-08-07 19:39:48 +00004448 inline _LIBCPP_INLINE_VISIBILITY
4449 basic_string<char> operator "" s( const char *__str, size_t __len )
4450 {
4451 return basic_string<char> (__str, __len);
4452 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004453
Louis Dionne89258142021-08-23 15:32:36 -04004454#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnant5c167562013-08-07 19:39:48 +00004455 inline _LIBCPP_INLINE_VISIBILITY
4456 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4457 {
4458 return basic_string<wchar_t> (__str, __len);
4459 }
Louis Dionne89258142021-08-23 15:32:36 -04004460#endif
Marshall Clowcba751f2013-07-23 17:05:24 +00004461
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -04004462#ifndef _LIBCPP_HAS_NO_CHAR8_T
Marshall Clow8732fed2018-12-11 04:35:44 +00004463 inline _LIBCPP_INLINE_VISIBILITY
4464 basic_string<char8_t> operator "" s(const char8_t *__str, size_t __len) _NOEXCEPT
4465 {
4466 return basic_string<char8_t> (__str, __len);
4467 }
4468#endif
4469
Howard Hinnant5c167562013-08-07 19:39:48 +00004470 inline _LIBCPP_INLINE_VISIBILITY
4471 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4472 {
4473 return basic_string<char16_t> (__str, __len);
4474 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004475
Howard Hinnant5c167562013-08-07 19:39:48 +00004476 inline _LIBCPP_INLINE_VISIBILITY
4477 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4478 {
4479 return basic_string<char32_t> (__str, __len);
4480 }
Nikolas Klauserd26407a2021-12-02 14:12:51 +01004481 } // namespace string_literals
4482} // namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004483#endif
4484
Howard Hinnantc51e1022010-05-11 19:42:16 +00004485_LIBCPP_END_NAMESPACE_STD
4486
Eric Fiselierf4433a32017-05-31 22:07:49 +00004487_LIBCPP_POP_MACROS
4488
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004489#endif // _LIBCPP_STRING