blob: 892df770756e264afef0b8fa08e89635b249bf22 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
Louis Dionne9bd93882021-11-17 16:25:01 -05002//===----------------------------------------------------------------------===//
Howard Hinnantc51e1022010-05-11 19:42:16 +00003//
Chandler Carruth7642bb12019-01-19 08:50:56 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_STRING
11#define _LIBCPP_STRING
12
13/*
14 string synopsis
15
16namespace std
17{
18
19template <class stateT>
20class fpos
21{
22private:
23 stateT st;
24public:
25 fpos(streamoff = streamoff());
26
27 operator streamoff() const;
28
29 stateT state() const;
30 void state(stateT);
31
32 fpos& operator+=(streamoff);
33 fpos operator+ (streamoff) const;
34 fpos& operator-=(streamoff);
35 fpos operator- (streamoff) const;
36};
37
38template <class stateT> streamoff operator-(const fpos<stateT>& x, const fpos<stateT>& y);
39
40template <class stateT> bool operator==(const fpos<stateT>& x, const fpos<stateT>& y);
41template <class stateT> bool operator!=(const fpos<stateT>& x, const fpos<stateT>& y);
42
43template <class charT>
44struct char_traits
45{
46 typedef charT char_type;
47 typedef ... int_type;
48 typedef streamoff off_type;
49 typedef streampos pos_type;
50 typedef mbstate_t state_type;
51
Howard Hinnantaeb17d82011-05-29 19:57:12 +000052 static void assign(char_type& c1, const char_type& c2) noexcept;
Howard Hinnant270c00e2012-07-20 19:09:12 +000053 static constexpr bool eq(char_type c1, char_type c2) noexcept;
54 static constexpr bool lt(char_type c1, char_type c2) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000055
56 static int compare(const char_type* s1, const char_type* s2, size_t n);
57 static size_t length(const char_type* s);
58 static const char_type* find(const char_type* s, size_t n, const char_type& a);
59 static char_type* move(char_type* s1, const char_type* s2, size_t n);
60 static char_type* copy(char_type* s1, const char_type* s2, size_t n);
61 static char_type* assign(char_type* s, size_t n, char_type a);
62
Howard Hinnant270c00e2012-07-20 19:09:12 +000063 static constexpr int_type not_eof(int_type c) noexcept;
64 static constexpr char_type to_char_type(int_type c) noexcept;
65 static constexpr int_type to_int_type(char_type c) noexcept;
66 static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept;
67 static constexpr int_type eof() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000068};
69
70template <> struct char_traits<char>;
71template <> struct char_traits<wchar_t>;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +010072template <> struct char_traits<char8_t>; // C++20
73template <> struct char_traits<char16_t>;
74template <> struct char_traits<char32_t>;
Howard Hinnantc51e1022010-05-11 19:42:16 +000075
Howard Hinnant3b6579a2010-08-22 00:02:43 +000076template<class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
Howard Hinnantc51e1022010-05-11 19:42:16 +000077class basic_string
78{
Howard Hinnant3b6579a2010-08-22 00:02:43 +000079public:
80// types:
Howard Hinnantc51e1022010-05-11 19:42:16 +000081 typedef traits traits_type;
82 typedef typename traits_type::char_type value_type;
83 typedef Allocator allocator_type;
84 typedef typename allocator_type::size_type size_type;
85 typedef typename allocator_type::difference_type difference_type;
86 typedef typename allocator_type::reference reference;
87 typedef typename allocator_type::const_reference const_reference;
88 typedef typename allocator_type::pointer pointer;
89 typedef typename allocator_type::const_pointer const_pointer;
90 typedef implementation-defined iterator;
91 typedef implementation-defined const_iterator;
92 typedef std::reverse_iterator<iterator> reverse_iterator;
93 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
94
95 static const size_type npos = -1;
96
Howard Hinnant3e276872011-06-03 18:40:47 +000097 basic_string()
98 noexcept(is_nothrow_default_constructible<allocator_type>::value);
99 explicit basic_string(const allocator_type& a);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000100 basic_string(const basic_string& str);
Howard Hinnant3e276872011-06-03 18:40:47 +0000101 basic_string(basic_string&& str)
102 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clow83445802016-04-07 18:13:41 +0000103 basic_string(const basic_string& str, size_type pos,
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000104 const allocator_type& a = allocator_type());
Marshall Clow83445802016-04-07 18:13:41 +0000105 basic_string(const basic_string& str, size_type pos, size_type n,
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000106 const Allocator& a = Allocator());
Marshall Clow78dbe462016-11-14 18:22:19 +0000107 template<class T>
108 basic_string(const T& t, size_type pos, size_type n, const Allocator& a = Allocator()); // C++17
Marshall Clowe46031a2018-07-02 18:41:15 +0000109 template <class T>
110 explicit basic_string(const T& t, const Allocator& a = Allocator()); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000111 basic_string(const value_type* s, const allocator_type& a = allocator_type());
112 basic_string(const value_type* s, size_type n, const allocator_type& a = allocator_type());
Marek Kurdej90d79712021-07-27 16:16:21 +0200113 basic_string(nullptr_t) = delete; // C++2b
Howard Hinnantc51e1022010-05-11 19:42:16 +0000114 basic_string(size_type n, value_type c, const allocator_type& a = allocator_type());
115 template<class InputIterator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000116 basic_string(InputIterator begin, InputIterator end,
117 const allocator_type& a = allocator_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000118 basic_string(initializer_list<value_type>, const Allocator& = Allocator());
119 basic_string(const basic_string&, const Allocator&);
120 basic_string(basic_string&&, const Allocator&);
121
122 ~basic_string();
123
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000124 operator basic_string_view<charT, traits>() const noexcept;
125
Howard Hinnantc51e1022010-05-11 19:42:16 +0000126 basic_string& operator=(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000127 template <class T>
128 basic_string& operator=(const T& t); // C++17
Howard Hinnant3e276872011-06-03 18:40:47 +0000129 basic_string& operator=(basic_string&& str)
130 noexcept(
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000131 allocator_type::propagate_on_container_move_assignment::value ||
132 allocator_type::is_always_equal::value ); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000133 basic_string& operator=(const value_type* s);
Marek Kurdej90d79712021-07-27 16:16:21 +0200134 basic_string& operator=(nullptr_t) = delete; // C++2b
Howard Hinnantc51e1022010-05-11 19:42:16 +0000135 basic_string& operator=(value_type c);
136 basic_string& operator=(initializer_list<value_type>);
137
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000138 iterator begin() noexcept;
139 const_iterator begin() const noexcept;
140 iterator end() noexcept;
141 const_iterator end() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000142
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000143 reverse_iterator rbegin() noexcept;
144 const_reverse_iterator rbegin() const noexcept;
145 reverse_iterator rend() noexcept;
146 const_reverse_iterator rend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000147
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000148 const_iterator cbegin() const noexcept;
149 const_iterator cend() const noexcept;
150 const_reverse_iterator crbegin() const noexcept;
151 const_reverse_iterator crend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000152
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000153 size_type size() const noexcept;
154 size_type length() const noexcept;
155 size_type max_size() const noexcept;
156 size_type capacity() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000157
158 void resize(size_type n, value_type c);
159 void resize(size_type n);
160
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100161 template<class Operation>
162 constexpr void resize_and_overwrite(size_type n, Operation op); // since C++23
163
Marek Kurdejc9848142020-11-26 10:07:16 +0100164 void reserve(size_type res_arg);
165 void reserve(); // deprecated in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000166 void shrink_to_fit();
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000167 void clear() noexcept;
168 bool empty() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000169
170 const_reference operator[](size_type pos) const;
171 reference operator[](size_type pos);
172
173 const_reference at(size_type n) const;
174 reference at(size_type n);
175
176 basic_string& operator+=(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000177 template <class T>
178 basic_string& operator+=(const T& t); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000179 basic_string& operator+=(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000180 basic_string& operator+=(value_type c);
181 basic_string& operator+=(initializer_list<value_type>);
182
183 basic_string& append(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000184 template <class T>
185 basic_string& append(const T& t); // C++17
Marshall Clow8db7fb02014-03-04 19:17:19 +0000186 basic_string& append(const basic_string& str, size_type pos, size_type n=npos); //C++14
Marshall Clow82513342016-09-24 22:45:42 +0000187 template <class T>
188 basic_string& append(const T& t, size_type pos, size_type n=npos); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000189 basic_string& append(const value_type* s, size_type n);
190 basic_string& append(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000191 basic_string& append(size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000192 template<class InputIterator>
193 basic_string& append(InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000194 basic_string& append(initializer_list<value_type>);
195
196 void push_back(value_type c);
197 void pop_back();
198 reference front();
199 const_reference front() const;
200 reference back();
201 const_reference back() const;
202
203 basic_string& assign(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000204 template <class T>
205 basic_string& assign(const T& t); // C++17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000206 basic_string& assign(basic_string&& str);
Marshall Clow8db7fb02014-03-04 19:17:19 +0000207 basic_string& assign(const basic_string& str, size_type pos, size_type n=npos); // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000208 template <class T>
209 basic_string& assign(const T& t, size_type pos, size_type n=npos); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000210 basic_string& assign(const value_type* s, size_type n);
211 basic_string& assign(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000212 basic_string& assign(size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000213 template<class InputIterator>
214 basic_string& assign(InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000215 basic_string& assign(initializer_list<value_type>);
216
217 basic_string& insert(size_type pos1, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000218 template <class T>
219 basic_string& insert(size_type pos1, const T& t);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000220 basic_string& insert(size_type pos1, const basic_string& str,
221 size_type pos2, size_type n);
Marshall Clow82513342016-09-24 22:45:42 +0000222 template <class T>
223 basic_string& insert(size_type pos1, const T& t, size_type pos2, size_type n); // C++17
Marshall Clow8db7fb02014-03-04 19:17:19 +0000224 basic_string& insert(size_type pos, const value_type* s, size_type n=npos); //C++14
Howard Hinnantd17880b2013-06-28 16:59:19 +0000225 basic_string& insert(size_type pos, const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000226 basic_string& insert(size_type pos, size_type n, value_type c);
227 iterator insert(const_iterator p, value_type c);
228 iterator insert(const_iterator p, size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000229 template<class InputIterator>
230 iterator insert(const_iterator p, InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000231 iterator insert(const_iterator p, initializer_list<value_type>);
232
233 basic_string& erase(size_type pos = 0, size_type n = npos);
234 iterator erase(const_iterator position);
235 iterator erase(const_iterator first, const_iterator last);
236
237 basic_string& replace(size_type pos1, size_type n1, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000238 template <class T>
239 basic_string& replace(size_type pos1, size_type n1, const T& t); // C++17
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000240 basic_string& replace(size_type pos1, size_type n1, const basic_string& str,
Marshall Clow8db7fb02014-03-04 19:17:19 +0000241 size_type pos2, size_type n2=npos); // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000242 template <class T>
243 basic_string& replace(size_type pos1, size_type n1, const T& t,
244 size_type pos2, size_type n); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000245 basic_string& replace(size_type pos, size_type n1, const value_type* s, size_type n2);
246 basic_string& replace(size_type pos, size_type n1, const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000247 basic_string& replace(size_type pos, size_type n1, size_type n2, value_type c);
Howard Hinnant990d6e82010-11-17 21:11:40 +0000248 basic_string& replace(const_iterator i1, const_iterator i2, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000249 template <class T>
250 basic_string& replace(const_iterator i1, const_iterator i2, const T& t); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000251 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s, size_type n);
252 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s);
Howard Hinnant990d6e82010-11-17 21:11:40 +0000253 basic_string& replace(const_iterator i1, const_iterator i2, size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000254 template<class InputIterator>
Howard Hinnant990d6e82010-11-17 21:11:40 +0000255 basic_string& replace(const_iterator i1, const_iterator i2, InputIterator j1, InputIterator j2);
256 basic_string& replace(const_iterator i1, const_iterator i2, initializer_list<value_type>);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000257
Howard Hinnantd17880b2013-06-28 16:59:19 +0000258 size_type copy(value_type* s, size_type n, size_type pos = 0) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000259 basic_string substr(size_type pos = 0, size_type n = npos) const;
260
Howard Hinnant3e276872011-06-03 18:40:47 +0000261 void swap(basic_string& str)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000262 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
263 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000264
Howard Hinnantd17880b2013-06-28 16:59:19 +0000265 const value_type* c_str() const noexcept;
266 const value_type* data() const noexcept;
Marshall Clowd3c22392016-03-08 15:44:30 +0000267 value_type* data() noexcept; // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000268
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000269 allocator_type get_allocator() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000270
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000271 size_type find(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000272 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800273 size_type find(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000274 size_type find(const value_type* s, size_type pos, size_type n) const noexcept;
275 size_type find(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000276 size_type find(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000277
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000278 size_type rfind(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000279 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800280 size_type rfind(const T& t, size_type pos = npos) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000281 size_type rfind(const value_type* s, size_type pos, size_type n) const noexcept;
282 size_type rfind(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000283 size_type rfind(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000284
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000285 size_type find_first_of(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000286 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800287 size_type find_first_of(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000288 size_type find_first_of(const value_type* s, size_type pos, size_type n) const noexcept;
289 size_type find_first_of(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000290 size_type find_first_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000291
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000292 size_type find_last_of(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000293 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800294 size_type find_last_of(const T& t, size_type pos = npos) const noexcept noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000295 size_type find_last_of(const value_type* s, size_type pos, size_type n) const noexcept;
296 size_type find_last_of(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000297 size_type find_last_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000298
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000299 size_type find_first_not_of(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000300 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800301 size_type find_first_not_of(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000302 size_type find_first_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
303 size_type find_first_not_of(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000304 size_type find_first_not_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000305
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000306 size_type find_last_not_of(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000307 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800308 size_type find_last_not_of(const T& t, size_type pos = npos) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000309 size_type find_last_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
310 size_type find_last_not_of(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000311 size_type find_last_not_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000312
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000313 int compare(const basic_string& str) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000314 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800315 int compare(const T& t) const noexcept; // C++17, noexcept as an extension
Howard Hinnantc51e1022010-05-11 19:42:16 +0000316 int compare(size_type pos1, size_type n1, const basic_string& str) const;
Marshall Clowe46031a2018-07-02 18:41:15 +0000317 template <class T>
318 int compare(size_type pos1, size_type n1, const T& t) const; // C++17
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000319 int compare(size_type pos1, size_type n1, const basic_string& str,
Marshall Clow8db7fb02014-03-04 19:17:19 +0000320 size_type pos2, size_type n2=npos) const; // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000321 template <class T>
322 int compare(size_type pos1, size_type n1, const T& t,
323 size_type pos2, size_type n2=npos) const; // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000324 int compare(const value_type* s) const noexcept;
325 int compare(size_type pos1, size_type n1, const value_type* s) const;
326 int compare(size_type pos1, size_type n1, const value_type* s, size_type n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000327
Marek Kurdej24b4c512021-01-07 12:29:04 +0100328 bool starts_with(basic_string_view<charT, traits> sv) const noexcept; // C++20
329 bool starts_with(charT c) const noexcept; // C++20
330 bool starts_with(const charT* s) const; // C++20
331 bool ends_with(basic_string_view<charT, traits> sv) const noexcept; // C++20
332 bool ends_with(charT c) const noexcept; // C++20
333 bool ends_with(const charT* s) const; // C++20
Marshall Clow18c293b2017-12-04 20:11:38 +0000334
Wim Leflere023c3542021-01-19 14:33:30 -0500335 constexpr bool contains(basic_string_view<charT, traits> sv) const noexcept; // C++2b
336 constexpr bool contains(charT c) const noexcept; // C++2b
337 constexpr bool contains(const charT* s) const; // C++2b
338
Howard Hinnantc51e1022010-05-11 19:42:16 +0000339 bool __invariants() const;
340};
341
Marshall Clowa0563332018-02-08 06:34:03 +0000342template<class InputIterator,
343 class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
344basic_string(InputIterator, InputIterator, Allocator = Allocator())
345 -> basic_string<typename iterator_traits<InputIterator>::value_type,
346 char_traits<typename iterator_traits<InputIterator>::value_type>,
347 Allocator>; // C++17
348
Howard Hinnantc51e1022010-05-11 19:42:16 +0000349template<class charT, class traits, class Allocator>
350basic_string<charT, traits, Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000351operator+(const basic_string<charT, traits, Allocator>& lhs,
352 const basic_string<charT, traits, Allocator>& rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000353
354template<class charT, class traits, class Allocator>
355basic_string<charT, traits, Allocator>
356operator+(const charT* lhs , const basic_string<charT,traits,Allocator>&rhs);
357
358template<class charT, class traits, class Allocator>
359basic_string<charT, traits, Allocator>
360operator+(charT lhs, const basic_string<charT,traits,Allocator>& rhs);
361
362template<class charT, class traits, class Allocator>
363basic_string<charT, traits, Allocator>
364operator+(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs);
365
366template<class charT, class traits, class Allocator>
367basic_string<charT, traits, Allocator>
368operator+(const basic_string<charT, traits, Allocator>& lhs, charT rhs);
369
370template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000371bool operator==(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000372 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000373
374template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000375bool operator==(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000376
377template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000378bool operator==(const basic_string<charT,traits,Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000379
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000380template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000381bool operator!=(const basic_string<charT,traits,Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000382 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000383
384template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000385bool operator!=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000386
387template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000388bool operator!=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000389
390template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000391bool operator< (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000392 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000393
394template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000395bool operator< (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000396
397template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000398bool operator< (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000399
400template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000401bool operator> (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000402 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000403
404template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000405bool operator> (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000406
407template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000408bool operator> (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000409
410template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000411bool operator<=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000412 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000413
414template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000415bool operator<=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000416
417template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000418bool operator<=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000419
420template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000421bool operator>=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000422 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000423
424template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000425bool operator>=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000426
427template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000428bool operator>=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000429
430template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000431void swap(basic_string<charT, traits, Allocator>& lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +0000432 basic_string<charT, traits, Allocator>& rhs)
433 noexcept(noexcept(lhs.swap(rhs)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000434
435template<class charT, class traits, class Allocator>
436basic_istream<charT, traits>&
437operator>>(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
438
439template<class charT, class traits, class Allocator>
440basic_ostream<charT, traits>&
441operator<<(basic_ostream<charT, traits>& os, const basic_string<charT, traits, Allocator>& str);
442
443template<class charT, class traits, class Allocator>
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000444basic_istream<charT, traits>&
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000445getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str,
446 charT delim);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000447
448template<class charT, class traits, class Allocator>
449basic_istream<charT, traits>&
450getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
451
Marshall Clow29b53f22018-12-14 18:49:35 +0000452template<class charT, class traits, class Allocator, class U>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200453typename basic_string<charT, traits, Allocator>::size_type
454erase(basic_string<charT, traits, Allocator>& c, const U& value); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000455template<class charT, class traits, class Allocator, class Predicate>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200456typename basic_string<charT, traits, Allocator>::size_type
457erase_if(basic_string<charT, traits, Allocator>& c, Predicate pred); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000458
Howard Hinnantc51e1022010-05-11 19:42:16 +0000459typedef basic_string<char> string;
460typedef basic_string<wchar_t> wstring;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100461typedef basic_string<char8_t> u8string; // C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000462typedef basic_string<char16_t> u16string;
463typedef basic_string<char32_t> u32string;
464
Bruce Mitchener170d8972020-11-24 12:53:53 -0500465int stoi (const string& str, size_t* idx = nullptr, int base = 10);
466long stol (const string& str, size_t* idx = nullptr, int base = 10);
467unsigned long stoul (const string& str, size_t* idx = nullptr, int base = 10);
468long long stoll (const string& str, size_t* idx = nullptr, int base = 10);
469unsigned long long stoull(const string& str, size_t* idx = nullptr, int base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000470
Bruce Mitchener170d8972020-11-24 12:53:53 -0500471float stof (const string& str, size_t* idx = nullptr);
472double stod (const string& str, size_t* idx = nullptr);
473long double stold(const string& str, size_t* idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000474
475string to_string(int val);
476string to_string(unsigned val);
477string to_string(long val);
478string to_string(unsigned long val);
479string to_string(long long val);
480string to_string(unsigned long long val);
481string to_string(float val);
482string to_string(double val);
483string to_string(long double val);
484
Bruce Mitchener170d8972020-11-24 12:53:53 -0500485int stoi (const wstring& str, size_t* idx = nullptr, int base = 10);
486long stol (const wstring& str, size_t* idx = nullptr, int base = 10);
487unsigned long stoul (const wstring& str, size_t* idx = nullptr, int base = 10);
488long long stoll (const wstring& str, size_t* idx = nullptr, int base = 10);
489unsigned long long stoull(const wstring& str, size_t* idx = nullptr, int base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000490
Bruce Mitchener170d8972020-11-24 12:53:53 -0500491float stof (const wstring& str, size_t* idx = nullptr);
492double stod (const wstring& str, size_t* idx = nullptr);
493long double stold(const wstring& str, size_t* idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000494
495wstring to_wstring(int val);
496wstring to_wstring(unsigned val);
497wstring to_wstring(long val);
498wstring to_wstring(unsigned long val);
499wstring to_wstring(long long val);
500wstring to_wstring(unsigned long long val);
501wstring to_wstring(float val);
502wstring to_wstring(double val);
503wstring to_wstring(long double val);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000504
505template <> struct hash<string>;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100506template <> struct hash<u8string>; // C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000507template <> struct hash<u16string>;
508template <> struct hash<u32string>;
509template <> struct hash<wstring>;
510
Marshall Clowcba751f2013-07-23 17:05:24 +0000511basic_string<char> operator "" s( const char *str, size_t len ); // C++14
512basic_string<wchar_t> operator "" s( const wchar_t *str, size_t len ); // C++14
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100513basic_string<char8_t> operator "" s( const char8_t *str, size_t len ); // C++20
Marshall Clowcba751f2013-07-23 17:05:24 +0000514basic_string<char16_t> operator "" s( const char16_t *str, size_t len ); // C++14
515basic_string<char32_t> operator "" s( const char32_t *str, size_t len ); // C++14
516
Howard Hinnantc51e1022010-05-11 19:42:16 +0000517} // std
518
519*/
520
Nikolas Klauserf210d8a2022-02-15 18:18:08 +0100521#include <__algorithm/max.h>
522#include <__algorithm/min.h>
523#include <__algorithm/remove.h>
524#include <__algorithm/remove_if.h>
Louis Dionne9bdbeb32022-02-14 13:41:09 -0500525#include <__assert>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000526#include <__config>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400527#include <__debug>
Nikolas Klauser80840272022-02-04 13:04:33 +0100528#include <__ios/fpos.h>
Louis Dionne77249522021-06-11 09:55:11 -0400529#include <__iterator/wrap_iter.h>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400530#include <compare>
531#include <cstdio> // EOF
Louis Dionned24191c2021-08-19 12:39:16 -0400532#include <cstdlib>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400533#include <cstring>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400534#include <initializer_list>
535#include <iosfwd>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000536#include <iterator>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000537#include <memory>
538#include <stdexcept>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400539#include <string_view>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000540#include <type_traits>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400541#include <utility>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000542#include <version>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000543
Nikolas Klauser4c1bf592022-02-11 19:15:18 +0100544// TODO: remove these headers
545#include <__functional/binary_function.h>
546#include <__functional/invoke.h>
547#include <__functional/operations.h>
548#include <__functional/reference_wrapper.h>
549#include <__functional/unary_function.h>
550#include <__functional/weak_result_type.h>
551#include <new>
552#include <typeinfo>
553
Louis Dionne89258142021-08-23 15:32:36 -0400554#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Nikolas Klauser80840272022-02-04 13:04:33 +0100555# include <cwchar>
Louis Dionne89258142021-08-23 15:32:36 -0400556#endif
557
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400558#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
Nikolas Klauser80840272022-02-04 13:04:33 +0100559# include <cstdint>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400560#endif
Eric Fiselier14b6de92014-08-10 23:53:08 +0000561
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000562#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Arthur O'Dwyer6eeaa002022-02-01 20:16:40 -0500563# pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000564#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000565
Eric Fiselierf4433a32017-05-31 22:07:49 +0000566_LIBCPP_PUSH_MACROS
567#include <__undef_macros>
568
569
Howard Hinnantc51e1022010-05-11 19:42:16 +0000570_LIBCPP_BEGIN_NAMESPACE_STD
571
Howard Hinnantc51e1022010-05-11 19:42:16 +0000572// basic_string
573
574template<class _CharT, class _Traits, class _Allocator>
575basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000576operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
577 const basic_string<_CharT, _Traits, _Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000578
579template<class _CharT, class _Traits, class _Allocator>
580basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000581operator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000582
583template<class _CharT, class _Traits, class _Allocator>
584basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000585operator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000586
587template<class _CharT, class _Traits, class _Allocator>
Louis Dionne8f8d95d2018-11-21 17:31:55 +0000588inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000589basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000590operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000591
592template<class _CharT, class _Traits, class _Allocator>
593basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000594operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000595
Shoaib Meenaiea363712017-07-29 02:54:41 +0000596_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&))
597
Marshall Clow039b2f02016-01-13 21:54:34 +0000598template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400599struct __string_is_trivial_iterator : public false_type {};
600
601template <class _Tp>
602struct __string_is_trivial_iterator<_Tp*>
603 : public is_arithmetic<_Tp> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000604
Louis Dionne173f29e2019-05-29 16:01:36 +0000605template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400606struct __string_is_trivial_iterator<__wrap_iter<_Iter> >
607 : public __string_is_trivial_iterator<_Iter> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000608
Marshall Clow82513342016-09-24 22:45:42 +0000609template <class _CharT, class _Traits, class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500610struct __can_be_converted_to_string_view : public _BoolConstant<
611 is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
612 !is_convertible<const _Tp&, const _CharT*>::value
613 > {};
Marshall Clow82513342016-09-24 22:45:42 +0000614
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000615#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000616
617template <class _CharT, size_t = sizeof(_CharT)>
618struct __padding
619{
620 unsigned char __xx[sizeof(_CharT)-1];
621};
622
623template <class _CharT>
624struct __padding<_CharT, 1>
625{
626};
627
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400628#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000629
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400630#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800631typedef basic_string<char8_t> u8string;
632#endif
633
634#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
635typedef basic_string<char16_t> u16string;
636typedef basic_string<char32_t> u32string;
Louis Dionne96fc5f52021-09-09 11:25:10 -0400637#endif
Richard Smith256954d2020-11-11 17:12:18 -0800638
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000639template<class _CharT, class _Traits, class _Allocator>
Richard Smith256954d2020-11-11 17:12:18 -0800640class
641 _LIBCPP_TEMPLATE_VIS
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400642#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800643 _LIBCPP_PREFERRED_NAME(u8string)
644#endif
645#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
646 _LIBCPP_PREFERRED_NAME(u16string)
647 _LIBCPP_PREFERRED_NAME(u32string)
648#endif
649 basic_string
Howard Hinnantc51e1022010-05-11 19:42:16 +0000650{
651public:
652 typedef basic_string __self;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000653 typedef basic_string_view<_CharT, _Traits> __self_view;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000654 typedef _Traits traits_type;
Marshall Clowa3a74e02017-03-15 18:41:11 +0000655 typedef _CharT value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000656 typedef _Allocator allocator_type;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000657 typedef allocator_traits<allocator_type> __alloc_traits;
658 typedef typename __alloc_traits::size_type size_type;
659 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant3e276872011-06-03 18:40:47 +0000660 typedef value_type& reference;
661 typedef const value_type& const_reference;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000662 typedef typename __alloc_traits::pointer pointer;
663 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000664
Marshall Clow79f33542018-03-21 00:36:05 +0000665 static_assert((!is_array<value_type>::value), "Character type of basic_string must not be an array");
666 static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string must be standard-layout");
667 static_assert(( is_trivial<value_type>::value), "Character type of basic_string must be trivial");
668 static_assert(( is_same<_CharT, typename traits_type::char_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000669 "traits_type::char_type must be the same type as CharT");
Marshall Clow79f33542018-03-21 00:36:05 +0000670 static_assert(( is_same<typename allocator_type::value_type, value_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000671 "Allocator::value_type must be same type as value_type");
Marshall Clowe46031a2018-07-02 18:41:15 +0000672
Howard Hinnantc51e1022010-05-11 19:42:16 +0000673 typedef __wrap_iter<pointer> iterator;
674 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000675 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
676 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000677
678private:
Howard Hinnant68bf1812013-04-30 21:44:48 +0000679
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000680#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000681
682 struct __long
683 {
684 pointer __data_;
685 size_type __size_;
686 size_type __cap_;
687 };
688
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000689#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000690 static const size_type __short_mask = 0x01;
691 static const size_type __long_mask = 0x1ul;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000692#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000693 static const size_type __short_mask = 0x80;
694 static const size_type __long_mask = ~(size_type(~0) >> 1);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400695#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +0000696
697 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
698 (sizeof(__long) - 1)/sizeof(value_type) : 2};
699
700 struct __short
701 {
702 value_type __data_[__min_cap];
703 struct
704 : __padding<value_type>
705 {
706 unsigned char __size_;
707 };
708 };
709
710#else
711
Howard Hinnantc51e1022010-05-11 19:42:16 +0000712 struct __long
713 {
714 size_type __cap_;
715 size_type __size_;
716 pointer __data_;
717 };
718
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000719#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000720 static const size_type __short_mask = 0x80;
721 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000722#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000723 static const size_type __short_mask = 0x01;
724 static const size_type __long_mask = 0x1ul;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400725#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +0000726
Howard Hinnantc51e1022010-05-11 19:42:16 +0000727 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
728 (sizeof(__long) - 1)/sizeof(value_type) : 2};
729
730 struct __short
731 {
732 union
733 {
734 unsigned char __size_;
Howard Hinnant49e145e2012-10-30 19:06:59 +0000735 value_type __lx;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000736 };
737 value_type __data_[__min_cap];
738 };
739
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400740#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000741
Howard Hinnant8ea98242013-08-23 17:37:05 +0000742 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000743
Howard Hinnant8ea98242013-08-23 17:37:05 +0000744 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000745
746 struct __raw
747 {
748 size_type __words[__n_words];
749 };
750
751 struct __rep
752 {
753 union
754 {
755 __long __l;
756 __short __s;
757 __raw __r;
758 };
759 };
760
761 __compressed_pair<__rep, allocator_type> __r_;
762
Howard Hinnantc51e1022010-05-11 19:42:16 +0000763public:
Martin Storsjö88890c22021-02-22 01:13:13 +0200764 _LIBCPP_TEMPLATE_DATA_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000765 static const size_type npos = -1;
766
Howard Hinnant3e276872011-06-03 18:40:47 +0000767 _LIBCPP_INLINE_VISIBILITY basic_string()
768 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000769
770 _LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a)
771#if _LIBCPP_STD_VER <= 14
772 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
773#else
774 _NOEXCEPT;
775#endif
776
Howard Hinnantc51e1022010-05-11 19:42:16 +0000777 basic_string(const basic_string& __str);
778 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clowa80abc72015-06-03 19:56:43 +0000779
Eric Fiselierfc92be82017-04-19 00:28:44 +0000780#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000781 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000782 basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +0000783#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +0000784 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000785#else
786 _NOEXCEPT;
787#endif
788
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000789 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000790 basic_string(basic_string&& __str, const allocator_type& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400791#endif // _LIBCPP_CXX03_LANG
Marshall Clowe46031a2018-07-02 18:41:15 +0000792
Louis Dionne9ce598d2021-09-08 09:14:43 -0400793 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Eric Fiseliera8567862018-07-17 05:48:48 +0000794 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5169d1c2019-12-16 19:03:23 -0500795 basic_string(const _CharT* __s) : __r_(__default_init_tag(), __default_init_tag()) {
Eric Fiseliera8567862018-07-17 05:48:48 +0000796 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
797 __init(__s, traits_type::length(__s));
Nikolas Klauserf2807732022-01-11 00:33:35 +0100798 _VSTD::__debug_db_insert_c(this);
Eric Fiseliera8567862018-07-17 05:48:48 +0000799 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000800
Louis Dionne9ce598d2021-09-08 09:14:43 -0400801 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000802 _LIBCPP_INLINE_VISIBILITY
803 basic_string(const _CharT* __s, const _Allocator& __a);
804
Marek Kurdej90d79712021-07-27 16:16:21 +0200805#if _LIBCPP_STD_VER > 20
806 basic_string(nullptr_t) = delete;
807#endif
808
Howard Hinnantcf823322010-12-17 14:46:43 +0000809 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000810 basic_string(const _CharT* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +0000811 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000812 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000813 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000814 basic_string(size_type __n, _CharT __c);
Marshall Clowe46031a2018-07-02 18:41:15 +0000815
Louis Dionne9ce598d2021-09-08 09:14:43 -0400816 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000817 _LIBCPP_INLINE_VISIBILITY
818 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
819
Marshall Clow83445802016-04-07 18:13:41 +0000820 basic_string(const basic_string& __str, size_type __pos, size_type __n,
Eric Fiselier812882b2017-02-17 01:17:10 +0000821 const _Allocator& __a = _Allocator());
Marshall Clow83445802016-04-07 18:13:41 +0000822 _LIBCPP_INLINE_VISIBILITY
823 basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +0000824 const _Allocator& __a = _Allocator());
Marshall Clowe46031a2018-07-02 18:41:15 +0000825
Louis Dionne9ce598d2021-09-08 09:14:43 -0400826 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 +0000827 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselier812882b2017-02-17 01:17:10 +0000828 basic_string(const _Tp& __t, size_type __pos, size_type __n,
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500829 const allocator_type& __a = allocator_type());
Marshall Clowe46031a2018-07-02 18:41:15 +0000830
Louis Dionne9ce598d2021-09-08 09:14:43 -0400831 template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500832 !__is_same_uncvref<_Tp, basic_string>::value> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000833 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
834 explicit basic_string(const _Tp& __t);
835
Louis Dionne9ce598d2021-09-08 09:14:43 -0400836 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 +0000837 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
838 explicit basic_string(const _Tp& __t, const allocator_type& __a);
839
Louis Dionne9ce598d2021-09-08 09:14:43 -0400840 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000841 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000842 basic_string(_InputIterator __first, _InputIterator __last);
Louis Dionne9ce598d2021-09-08 09:14:43 -0400843 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000844 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000845 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000846#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000847 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000848 basic_string(initializer_list<_CharT> __il);
Howard Hinnantcf823322010-12-17 14:46:43 +0000849 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000850 basic_string(initializer_list<_CharT> __il, const _Allocator& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400851#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000852
Eric Fiselierd9a702a2016-10-31 03:42:50 +0000853 inline ~basic_string();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000854
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000855 _LIBCPP_INLINE_VISIBILITY
856 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
857
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000858 basic_string& operator=(const basic_string& __str);
Eric Fiselier5ec13b12017-01-23 21:24:58 +0000859
Louis Dionne9ce598d2021-09-08 09:14:43 -0400860 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 +0000861 basic_string& operator=(const _Tp& __t)
862 {__self_view __sv = __t; return assign(__sv);}
863
Eric Fiselierfc92be82017-04-19 00:28:44 +0000864#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000865 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000866 basic_string& operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000867 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiselierfc92be82017-04-19 00:28:44 +0000868 _LIBCPP_INLINE_VISIBILITY
869 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000870#endif
Howard Hinnantd17880b2013-06-28 16:59:19 +0000871 _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const value_type* __s) {return assign(__s);}
Marek Kurdej90d79712021-07-27 16:16:21 +0200872#if _LIBCPP_STD_VER > 20
873 basic_string& operator=(nullptr_t) = delete;
874#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000875 basic_string& operator=(value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000876
Louis Dionneba400782020-10-02 15:02:52 -0400877#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +0000878 _LIBCPP_INLINE_VISIBILITY
879 iterator begin() _NOEXCEPT
880 {return iterator(this, __get_pointer());}
881 _LIBCPP_INLINE_VISIBILITY
882 const_iterator begin() const _NOEXCEPT
883 {return const_iterator(this, __get_pointer());}
884 _LIBCPP_INLINE_VISIBILITY
885 iterator end() _NOEXCEPT
886 {return iterator(this, __get_pointer() + size());}
887 _LIBCPP_INLINE_VISIBILITY
888 const_iterator end() const _NOEXCEPT
889 {return const_iterator(this, __get_pointer() + size());}
890#else
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000891 _LIBCPP_INLINE_VISIBILITY
892 iterator begin() _NOEXCEPT
893 {return iterator(__get_pointer());}
894 _LIBCPP_INLINE_VISIBILITY
895 const_iterator begin() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000896 {return const_iterator(__get_pointer());}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000897 _LIBCPP_INLINE_VISIBILITY
898 iterator end() _NOEXCEPT
899 {return iterator(__get_pointer() + size());}
900 _LIBCPP_INLINE_VISIBILITY
901 const_iterator end() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000902 {return const_iterator(__get_pointer() + size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400903#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000904 _LIBCPP_INLINE_VISIBILITY
905 reverse_iterator rbegin() _NOEXCEPT
906 {return reverse_iterator(end());}
907 _LIBCPP_INLINE_VISIBILITY
908 const_reverse_iterator rbegin() const _NOEXCEPT
909 {return const_reverse_iterator(end());}
910 _LIBCPP_INLINE_VISIBILITY
911 reverse_iterator rend() _NOEXCEPT
912 {return reverse_iterator(begin());}
913 _LIBCPP_INLINE_VISIBILITY
914 const_reverse_iterator rend() const _NOEXCEPT
915 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000916
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000917 _LIBCPP_INLINE_VISIBILITY
918 const_iterator cbegin() const _NOEXCEPT
919 {return begin();}
920 _LIBCPP_INLINE_VISIBILITY
921 const_iterator cend() const _NOEXCEPT
922 {return end();}
923 _LIBCPP_INLINE_VISIBILITY
924 const_reverse_iterator crbegin() const _NOEXCEPT
925 {return rbegin();}
926 _LIBCPP_INLINE_VISIBILITY
927 const_reverse_iterator crend() const _NOEXCEPT
928 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000929
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000930 _LIBCPP_INLINE_VISIBILITY size_type size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000931 {return __is_long() ? __get_long_size() : __get_short_size();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000932 _LIBCPP_INLINE_VISIBILITY size_type length() const _NOEXCEPT {return size();}
933 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT;
934 _LIBCPP_INLINE_VISIBILITY size_type capacity() const _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +0000935 {return (__is_long() ? __get_long_cap()
936 : static_cast<size_type>(__min_cap)) - 1;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000937
938 void resize(size_type __n, value_type __c);
939 _LIBCPP_INLINE_VISIBILITY void resize(size_type __n) {resize(__n, value_type());}
940
Marek Kurdejc9848142020-11-26 10:07:16 +0100941 void reserve(size_type __requested_capacity);
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100942
943#if _LIBCPP_STD_VER > 20
944 template <class _Op>
945 _LIBCPP_HIDE_FROM_ABI constexpr
946 void resize_and_overwrite(size_type __n, _Op __op) {
947 __resize_default_init(__n);
Nikolas Klausera4a76a12022-01-20 13:53:59 +0100948 __erase_to_end(_VSTD::move(__op)(data(), _LIBCPP_AUTO_CAST(__n)));
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100949 }
950#endif
951
Eric Fiselier451d5582018-11-26 20:15:38 +0000952 _LIBCPP_INLINE_VISIBILITY void __resize_default_init(size_type __n);
953
Marek Kurdejc9848142020-11-26 10:07:16 +0100954 _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_INLINE_VISIBILITY
955 void reserve() _NOEXCEPT {shrink_to_fit();}
Marshall Clow6ae12a82018-11-28 18:18:34 +0000956 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejc9848142020-11-26 10:07:16 +0100957 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000958 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000959 void clear() _NOEXCEPT;
Marshall Clowb7db4972017-11-15 20:02:27 +0000960 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
961 bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000962
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000963 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __pos) const _NOEXCEPT;
964 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000965
966 const_reference at(size_type __n) const;
967 reference at(size_type __n);
968
969 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const basic_string& __str) {return append(__str);}
Marshall Clowe46031a2018-07-02 18:41:15 +0000970
971 template <class _Tp>
972 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -0400973 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +0000974 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500975 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
976 && !__is_same_uncvref<_Tp, basic_string >::value,
Marshall Clowe46031a2018-07-02 18:41:15 +0000977 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500978 >
Marshall Clowe46031a2018-07-02 18:41:15 +0000979 operator+=(const _Tp& __t) {__self_view __sv = __t; return append(__sv);}
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000980 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const value_type* __s) {return append(__s);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000981 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(value_type __c) {push_back(__c); return *this;}
Eric Fiselierfc92be82017-04-19 00:28:44 +0000982#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000983 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(initializer_list<value_type> __il) {return append(__il);}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400984#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000985
Howard Hinnantcf823322010-12-17 14:46:43 +0000986 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000987 basic_string& append(const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000988
989 template <class _Tp>
990 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -0400991 __enable_if_t<
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500992 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
993 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clowe46031a2018-07-02 18:41:15 +0000994 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500995 >
Marshall Clowe46031a2018-07-02 18:41:15 +0000996 append(const _Tp& __t) { __self_view __sv = __t; return append(__sv.data(), __sv.size()); }
Marshall Clow8db7fb02014-03-04 19:17:19 +0000997 basic_string& append(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clowe46031a2018-07-02 18:41:15 +0000998
Marshall Clow62953962016-10-03 23:40:48 +0000999 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001000 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001001 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001002 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001003 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1004 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001005 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001006 >
Marshall Clow82513342016-09-24 22:45:42 +00001007 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001008 basic_string& append(const value_type* __s, size_type __n);
1009 basic_string& append(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001010 basic_string& append(size_type __n, value_type __c);
Eric Fiselier451d5582018-11-26 20:15:38 +00001011
1012 _LIBCPP_INLINE_VISIBILITY
1013 void __append_default_init(size_type __n);
1014
Howard Hinnantc51e1022010-05-11 19:42:16 +00001015 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001016 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001017 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001018 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001019 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001020 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001021 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001022 _LIBCPP_INLINE_VISIBILITY
1023 append(_InputIterator __first, _InputIterator __last) {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001024 const basic_string __temp(__first, __last, __alloc());
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001025 append(__temp.data(), __temp.size());
1026 return *this;
1027 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001028 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001029 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001030 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001031 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001032 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001033 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001034 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001035 _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001036 append(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001037
Eric Fiselierfc92be82017-04-19 00:28:44 +00001038#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001039 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001040 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001041#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001042
1043 void push_back(value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001044 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001045 void pop_back();
Marshall Clow05cf6692019-03-19 03:30:07 +00001046 _LIBCPP_INLINE_VISIBILITY reference front() _NOEXCEPT;
1047 _LIBCPP_INLINE_VISIBILITY const_reference front() const _NOEXCEPT;
1048 _LIBCPP_INLINE_VISIBILITY reference back() _NOEXCEPT;
1049 _LIBCPP_INLINE_VISIBILITY const_reference back() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001050
Marshall Clowe46031a2018-07-02 18:41:15 +00001051 template <class _Tp>
1052 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001053 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001054 <
1055 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1056 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001057 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001058 assign(const _Tp & __t) { __self_view __sv = __t; return assign(__sv.data(), __sv.size()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001059 _LIBCPP_INLINE_VISIBILITY
Marshall Clow95d5e9a2016-03-09 18:08:29 +00001060 basic_string& assign(const basic_string& __str) { return *this = __str; }
Eric Fiselierfc92be82017-04-19 00:28:44 +00001061#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001062 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001063 basic_string& assign(basic_string&& __str)
Marshall Clow5aa9e942015-10-05 16:17:34 +00001064 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001065 {*this = _VSTD::move(__str); return *this;}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001066#endif
Marshall Clow8db7fb02014-03-04 19:17:19 +00001067 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow62953962016-10-03 23:40:48 +00001068 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001069 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001070 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001071 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001072 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1073 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001074 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001075 >
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001076 assign(const _Tp & __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001077 basic_string& assign(const value_type* __s, size_type __n);
1078 basic_string& assign(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001079 basic_string& assign(size_type __n, value_type __c);
1080 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001081 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001082 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001083 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001084 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001085 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001086 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001087 assign(_InputIterator __first, _InputIterator __last);
1088 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001089 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001090 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001091 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001092 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001093 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001094 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001095 assign(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001096#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001097 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001098 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001099#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001100
Howard Hinnantcf823322010-12-17 14:46:43 +00001101 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001102 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001103
1104 template <class _Tp>
1105 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001106 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001107 <
1108 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1109 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001110 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001111 insert(size_type __pos1, const _Tp& __t)
1112 { __self_view __sv = __t; return insert(__pos1, __sv.data(), __sv.size()); }
1113
Marshall Clow82513342016-09-24 22:45:42 +00001114 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001115 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001116 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001117 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001118 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001119 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001120 >
Marshall Clow82513342016-09-24 22:45:42 +00001121 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Marshall Clow8db7fb02014-03-04 19:17:19 +00001122 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001123 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1124 basic_string& insert(size_type __pos, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001125 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1126 iterator insert(const_iterator __pos, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001127 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001128 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1129 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001130 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001131 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001132 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001133 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001134 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001135 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001136 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1137 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001138 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001139 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001140 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001141 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001142 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001143 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001144 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001145#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001146 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001147 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1148 {return insert(__pos, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001149#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001150
1151 basic_string& erase(size_type __pos = 0, size_type __n = npos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001152 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001153 iterator erase(const_iterator __pos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001154 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001155 iterator erase(const_iterator __first, const_iterator __last);
1156
Howard Hinnantcf823322010-12-17 14:46:43 +00001157 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001158 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001159
1160 template <class _Tp>
1161 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001162 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001163 <
1164 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1165 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001166 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001167 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 +00001168 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 +00001169 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001170 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001171 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001172 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001173 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001174 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001175 >
Marshall Clow82513342016-09-24 22:45:42 +00001176 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001177 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
1178 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001179 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001180 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001181 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001182
1183 template <class _Tp>
1184 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001185 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001186 <
1187 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1188 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001189 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001190 replace(const_iterator __i1, const_iterator __i2, const _Tp& __t) { __self_view __sv = __t; return replace(__i1 - begin(), __i2 - __i1, __sv); }
1191
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001192 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001193 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +00001194 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001195 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Howard Hinnantcf823322010-12-17 14:46:43 +00001196 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001197 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001198 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001199 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001200 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001201 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001202 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001203 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001204 >
Howard Hinnant990d6e82010-11-17 21:11:40 +00001205 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001206#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001207 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001208 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001209 {return replace(__i1, __i2, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001210#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001211
Howard Hinnantd17880b2013-06-28 16:59:19 +00001212 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
Howard Hinnantcf823322010-12-17 14:46:43 +00001213 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001214 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1215
Howard Hinnantcf823322010-12-17 14:46:43 +00001216 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001217 void swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001218#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00001219 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +00001220#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001221 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001222 __is_nothrow_swappable<allocator_type>::value);
1223#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001224
Howard Hinnantcf823322010-12-17 14:46:43 +00001225 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001226 const value_type* c_str() const _NOEXCEPT {return data();}
Howard Hinnantcf823322010-12-17 14:46:43 +00001227 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001228 const value_type* data() const _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Eric Fiselier4ee2a892017-11-20 20:23:27 +00001229#if _LIBCPP_STD_VER > 14 || defined(_LIBCPP_BUILDING_LIBRARY)
Marshall Clowd3c22392016-03-08 15:44:30 +00001230 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001231 value_type* data() _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Marshall Clowd3c22392016-03-08 15:44:30 +00001232#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001233
Howard Hinnantcf823322010-12-17 14:46:43 +00001234 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001235 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001236
Howard Hinnantcf823322010-12-17 14:46:43 +00001237 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001238 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001239
1240 template <class _Tp>
1241 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001242 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001243 <
1244 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1245 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001246 >
zoecarver1997e0a2021-02-05 11:54:47 -08001247 find(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001248 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001249 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001250 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001251 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001252
Howard Hinnantcf823322010-12-17 14:46:43 +00001253 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001254 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001255
1256 template <class _Tp>
1257 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001258 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001259 <
1260 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1261 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001262 >
zoecarver1997e0a2021-02-05 11:54:47 -08001263 rfind(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001264 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001265 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001266 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001267 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001268
Howard Hinnantcf823322010-12-17 14:46:43 +00001269 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001270 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001271
1272 template <class _Tp>
1273 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001274 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001275 <
1276 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1277 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001278 >
zoecarver1997e0a2021-02-05 11:54:47 -08001279 find_first_of(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001280 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001281 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001282 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001283 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001284 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001285
Howard Hinnantcf823322010-12-17 14:46:43 +00001286 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001287 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001288
1289 template <class _Tp>
1290 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001291 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001292 <
1293 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1294 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001295 >
zoecarver1997e0a2021-02-05 11:54:47 -08001296 find_last_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001297 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001298 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001299 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001300 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001301 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001302
Howard Hinnantcf823322010-12-17 14:46:43 +00001303 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001304 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001305
1306 template <class _Tp>
1307 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001308 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001309 <
1310 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1311 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001312 >
zoecarver1997e0a2021-02-05 11:54:47 -08001313 find_first_not_of(const _Tp &__t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001314 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 +00001315 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001316 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001317 _LIBCPP_INLINE_VISIBILITY
1318 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1319
1320 _LIBCPP_INLINE_VISIBILITY
1321 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001322
1323 template <class _Tp>
1324 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001325 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001326 <
1327 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1328 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001329 >
zoecarver1997e0a2021-02-05 11:54:47 -08001330 find_last_not_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001331 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 +00001332 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001333 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001334 _LIBCPP_INLINE_VISIBILITY
1335 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1336
1337 _LIBCPP_INLINE_VISIBILITY
1338 int compare(const basic_string& __str) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001339
1340 template <class _Tp>
1341 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001342 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001343 <
1344 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1345 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001346 >
zoecarver1997e0a2021-02-05 11:54:47 -08001347 compare(const _Tp &__t) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001348
1349 template <class _Tp>
1350 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001351 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001352 <
1353 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1354 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001355 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001356 compare(size_type __pos1, size_type __n1, const _Tp& __t) const;
1357
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001358 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001359 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Marshall Clow8db7fb02014-03-04 19:17:19 +00001360 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 +00001361
Marshall Clow82513342016-09-24 22:45:42 +00001362 template <class _Tp>
Eric Fiselier7d4aa5c2016-10-14 05:29:46 +00001363 inline _LIBCPP_INLINE_VISIBILITY
Louis Dionne9ce598d2021-09-08 09:14:43 -04001364 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001365 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001366 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001367 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001368 >
Marshall Clow82513342016-09-24 22:45:42 +00001369 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 +00001370 int compare(const value_type* __s) const _NOEXCEPT;
1371 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1372 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001373
Marshall Clow18c293b2017-12-04 20:11:38 +00001374#if _LIBCPP_STD_VER > 17
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001375 constexpr _LIBCPP_INLINE_VISIBILITY
1376 bool starts_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001377 { return __self_view(data(), size()).starts_with(__sv); }
1378
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001379 constexpr _LIBCPP_INLINE_VISIBILITY
1380 bool starts_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001381 { return !empty() && _Traits::eq(front(), __c); }
1382
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001383 constexpr _LIBCPP_INLINE_VISIBILITY
1384 bool starts_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001385 { return starts_with(__self_view(__s)); }
1386
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001387 constexpr _LIBCPP_INLINE_VISIBILITY
1388 bool ends_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001389 { return __self_view(data(), size()).ends_with( __sv); }
1390
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001391 constexpr _LIBCPP_INLINE_VISIBILITY
1392 bool ends_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001393 { return !empty() && _Traits::eq(back(), __c); }
1394
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001395 constexpr _LIBCPP_INLINE_VISIBILITY
1396 bool ends_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001397 { return ends_with(__self_view(__s)); }
1398#endif
1399
Wim Leflere023c3542021-01-19 14:33:30 -05001400#if _LIBCPP_STD_VER > 20
1401 constexpr _LIBCPP_INLINE_VISIBILITY
1402 bool contains(__self_view __sv) const noexcept
1403 { return __self_view(data(), size()).contains(__sv); }
1404
1405 constexpr _LIBCPP_INLINE_VISIBILITY
1406 bool contains(value_type __c) const noexcept
1407 { return __self_view(data(), size()).contains(__c); }
1408
1409 constexpr _LIBCPP_INLINE_VISIBILITY
1410 bool contains(const value_type* __s) const
1411 { return __self_view(data(), size()).contains(__s); }
1412#endif
1413
Howard Hinnantcf823322010-12-17 14:46:43 +00001414 _LIBCPP_INLINE_VISIBILITY bool __invariants() const;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001415
Marshall Clowe60a7182018-05-29 17:04:37 +00001416 _LIBCPP_INLINE_VISIBILITY void __clear_and_shrink() _NOEXCEPT;
Marshall Clow851b9ec2019-05-20 21:56:51 +00001417
Marek Kurdejc9848142020-11-26 10:07:16 +01001418 _LIBCPP_INLINE_VISIBILITY void __shrink_or_extend(size_type __target_capacity);
1419
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001420 _LIBCPP_INLINE_VISIBILITY
1421 bool __is_long() const _NOEXCEPT
1422 {return bool(__r_.first().__s.__size_ & __short_mask);}
1423
Louis Dionneba400782020-10-02 15:02:52 -04001424#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001425
1426 bool __dereferenceable(const const_iterator* __i) const;
1427 bool __decrementable(const const_iterator* __i) const;
1428 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1429 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1430
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001431#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001432
Howard Hinnantc51e1022010-05-11 19:42:16 +00001433private:
Nikolas Klauser93826d12022-01-04 17:24:03 +01001434 _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI static bool __fits_in_sso(size_type __sz) {
1435 // SSO is disabled during constant evaluation because `__is_long` isn't constexpr friendly
1436 return !__libcpp_is_constant_evaluated() && (__sz < __min_cap);
1437 }
1438
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001439 _LIBCPP_INLINE_VISIBILITY
1440 allocator_type& __alloc() _NOEXCEPT
1441 {return __r_.second();}
1442 _LIBCPP_INLINE_VISIBILITY
1443 const allocator_type& __alloc() const _NOEXCEPT
1444 {return __r_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001445
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001446#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001447
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001448 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001449 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001450# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001451 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001452# else
1453 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1454# endif
1455
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001456 _LIBCPP_INLINE_VISIBILITY
1457 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001458# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001459 {return __r_.first().__s.__size_ >> 1;}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001460# else
1461 {return __r_.first().__s.__size_;}
1462# endif
1463
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001464#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001465
1466 _LIBCPP_INLINE_VISIBILITY
1467 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001468# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001469 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1470# else
1471 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
1472# endif
1473
1474 _LIBCPP_INLINE_VISIBILITY
1475 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001476# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001477 {return __r_.first().__s.__size_;}
1478# else
1479 {return __r_.first().__s.__size_ >> 1;}
1480# endif
1481
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001482#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001483
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001484 _LIBCPP_INLINE_VISIBILITY
1485 void __set_long_size(size_type __s) _NOEXCEPT
1486 {__r_.first().__l.__size_ = __s;}
1487 _LIBCPP_INLINE_VISIBILITY
1488 size_type __get_long_size() const _NOEXCEPT
1489 {return __r_.first().__l.__size_;}
1490 _LIBCPP_INLINE_VISIBILITY
1491 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001492 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1493
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001494 _LIBCPP_INLINE_VISIBILITY
1495 void __set_long_cap(size_type __s) _NOEXCEPT
1496 {__r_.first().__l.__cap_ = __long_mask | __s;}
1497 _LIBCPP_INLINE_VISIBILITY
1498 size_type __get_long_cap() const _NOEXCEPT
Howard Hinnant28b24882011-12-01 20:21:04 +00001499 {return __r_.first().__l.__cap_ & size_type(~__long_mask);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001500
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001501 _LIBCPP_INLINE_VISIBILITY
1502 void __set_long_pointer(pointer __p) _NOEXCEPT
1503 {__r_.first().__l.__data_ = __p;}
1504 _LIBCPP_INLINE_VISIBILITY
1505 pointer __get_long_pointer() _NOEXCEPT
1506 {return __r_.first().__l.__data_;}
1507 _LIBCPP_INLINE_VISIBILITY
1508 const_pointer __get_long_pointer() const _NOEXCEPT
1509 {return __r_.first().__l.__data_;}
1510 _LIBCPP_INLINE_VISIBILITY
1511 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001512 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001513 _LIBCPP_INLINE_VISIBILITY
1514 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001515 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001516 _LIBCPP_INLINE_VISIBILITY
1517 pointer __get_pointer() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001518 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001519 _LIBCPP_INLINE_VISIBILITY
1520 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001521 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1522
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001523 _LIBCPP_INLINE_VISIBILITY
1524 void __zero() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001525 {
1526 size_type (&__a)[__n_words] = __r_.first().__r.__words;
1527 for (unsigned __i = 0; __i < __n_words; ++__i)
1528 __a[__i] = 0;
1529 }
1530
1531 template <size_type __a> static
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001532 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00001533 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +00001534 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001535 enum {__alignment = 16};
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001536 static _LIBCPP_INLINE_VISIBILITY
1537 size_type __recommend(size_type __s) _NOEXCEPT
Marshall Clow80584522018-02-07 21:30:17 +00001538 {
1539 if (__s < __min_cap) return static_cast<size_type>(__min_cap) - 1;
1540 size_type __guess = __align_it<sizeof(value_type) < __alignment ?
1541 __alignment/sizeof(value_type) : 1 > (__s+1) - 1;
1542 if (__guess == __min_cap) ++__guess;
1543 return __guess;
1544 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001545
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001546 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001547 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001548 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001549 void __init(const value_type* __s, size_type __sz);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001550 inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001551 void __init(size_type __n, value_type __c);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001552
Martijn Vels5e7c9752020-03-04 17:52:46 -05001553 // Slow path for the (inlined) copy constructor for 'long' strings.
1554 // Always externally instantiated and not inlined.
1555 // Requires that __s is zero terminated.
1556 // The main reason for this function to exist is because for unstable, we
1557 // want to allow inlining of the copy constructor. However, we don't want
1558 // to call the __init() functions as those are marked as inline which may
1559 // result in over-aggressive inlining by the compiler, where our aim is
1560 // to only inline the fast path code directly in the ctor.
1561 void __init_copy_ctor_external(const value_type* __s, size_type __sz);
1562
Howard Hinnantc51e1022010-05-11 19:42:16 +00001563 template <class _InputIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001564 inline
Louis Dionne9ce598d2021-09-08 09:14:43 -04001565 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001566 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001567 __is_exactly_cpp17_input_iterator<_InputIterator>::value
1568 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001569 __init(_InputIterator __first, _InputIterator __last);
1570
1571 template <class _ForwardIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001572 inline
Louis Dionne9ce598d2021-09-08 09:14:43 -04001573 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001574 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001575 __is_cpp17_forward_iterator<_ForwardIterator>::value
1576 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001577 __init(_ForwardIterator __first, _ForwardIterator __last);
1578
1579 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001580 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001581 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1582 size_type __n_copy, size_type __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00001583 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001584
Martijn Vels596e3de2020-02-26 15:55:49 -05001585 // __assign_no_alias is invoked for assignment operations where we
1586 // have proof that the input does not alias the current instance.
1587 // For example, operator=(basic_string) performs a 'self' check.
1588 template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04001589 basic_string& __assign_no_alias(const value_type* __s, size_type __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05001590
Howard Hinnantcf823322010-12-17 14:46:43 +00001591 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001592 void __erase_to_end(size_type __pos);
1593
Martijn Velsa81fc792020-02-26 13:25:43 -05001594 // __erase_external_with_move is invoked for erase() invocations where
1595 // `n ~= npos`, likely requiring memory moves on the string data.
1596 void __erase_external_with_move(size_type __pos, size_type __n);
1597
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001598 _LIBCPP_INLINE_VISIBILITY
1599 void __copy_assign_alloc(const basic_string& __str)
1600 {__copy_assign_alloc(__str, integral_constant<bool,
1601 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1602
1603 _LIBCPP_INLINE_VISIBILITY
1604 void __copy_assign_alloc(const basic_string& __str, true_type)
1605 {
Marshall Clowf258c202017-01-31 03:40:52 +00001606 if (__alloc() == __str.__alloc())
1607 __alloc() = __str.__alloc();
1608 else
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001609 {
Marshall Clowf258c202017-01-31 03:40:52 +00001610 if (!__str.__is_long())
1611 {
Vedant Kumar55e007e2018-03-08 21:15:26 +00001612 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001613 __alloc() = __str.__alloc();
1614 }
1615 else
1616 {
1617 allocator_type __a = __str.__alloc();
1618 pointer __p = __alloc_traits::allocate(__a, __str.__get_long_cap());
Vedant Kumar55e007e2018-03-08 21:15:26 +00001619 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001620 __alloc() = _VSTD::move(__a);
1621 __set_long_pointer(__p);
1622 __set_long_cap(__str.__get_long_cap());
1623 __set_long_size(__str.size());
1624 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001625 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001626 }
1627
1628 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001629 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001630 {}
1631
Eric Fiselierfc92be82017-04-19 00:28:44 +00001632#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +00001633 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001634 void __move_assign(basic_string& __str, false_type)
1635 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnantcf823322010-12-17 14:46:43 +00001636 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001637 void __move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001638#if _LIBCPP_STD_VER > 14
1639 _NOEXCEPT;
1640#else
Howard Hinnant3e276872011-06-03 18:40:47 +00001641 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001642#endif
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001643#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001644
1645 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001646 void
Howard Hinnantc2734962011-09-02 20:42:31 +00001647 __move_assign_alloc(basic_string& __str)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001648 _NOEXCEPT_(
1649 !__alloc_traits::propagate_on_container_move_assignment::value ||
1650 is_nothrow_move_assignable<allocator_type>::value)
1651 {__move_assign_alloc(__str, integral_constant<bool,
1652 __alloc_traits::propagate_on_container_move_assignment::value>());}
1653
1654 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00001655 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001656 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1657 {
1658 __alloc() = _VSTD::move(__c.__alloc());
1659 }
1660
1661 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001662 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001663 _NOEXCEPT
1664 {}
1665
Martijn Velsda7d94f2020-06-19 14:24:03 -04001666 basic_string& __assign_external(const value_type* __s);
1667 basic_string& __assign_external(const value_type* __s, size_type __n);
1668
1669 // Assigns the value in __s, guaranteed to be __n < __min_cap in length.
1670 inline basic_string& __assign_short(const value_type* __s, size_type __n) {
1671 pointer __p = __is_long()
1672 ? (__set_long_size(__n), __get_long_pointer())
1673 : (__set_short_size(__n), __get_short_pointer());
1674 traits_type::move(_VSTD::__to_address(__p), __s, __n);
1675 traits_type::assign(__p[__n], value_type());
1676 return *this;
1677 }
1678
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01001679 _LIBCPP_HIDE_FROM_ABI basic_string& __null_terminate_at(value_type* __p, size_type __newsz) {
1680 __set_size(__newsz);
1681 __invalidate_iterators_past(__newsz);
1682 traits_type::assign(__p[__newsz], value_type());
1683 return *this;
1684 }
1685
Howard Hinnantcf823322010-12-17 14:46:43 +00001686 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
1687 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(size_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001688
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001689 template<class _Tp>
1690 _LIBCPP_INLINE_VISIBILITY
1691 bool __addr_in_range(_Tp&& __t) const {
1692 const volatile void *__p = _VSTD::addressof(__t);
1693 return data() <= __p && __p <= data() + size();
1694 }
1695
Louis Dionned24191c2021-08-19 12:39:16 -04001696 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1697 void __throw_length_error() const {
Nikolas Klauserf89bb932022-01-24 19:44:34 +01001698 _VSTD::__throw_length_error("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001699 }
1700
1701 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1702 void __throw_out_of_range() const {
Nikolas Klauserf89bb932022-01-24 19:44:34 +01001703 _VSTD::__throw_out_of_range("basic_string");
Louis Dionned24191c2021-08-19 12:39:16 -04001704 }
1705
Howard Hinnantc51e1022010-05-11 19:42:16 +00001706 friend basic_string operator+<>(const basic_string&, const basic_string&);
1707 friend basic_string operator+<>(const value_type*, const basic_string&);
1708 friend basic_string operator+<>(value_type, const basic_string&);
1709 friend basic_string operator+<>(const basic_string&, const value_type*);
1710 friend basic_string operator+<>(const basic_string&, value_type);
1711};
1712
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001713// These declarations must appear before any functions are implicitly used
1714// so that they have the correct visibility specifier.
1715#ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
Louis Dionne89258142021-08-23 15:32:36 -04001716 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1717# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1718 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1719# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001720#else
Louis Dionne89258142021-08-23 15:32:36 -04001721 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1722# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1723 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1724# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001725#endif
1726
1727
Louis Dionned59f8a52021-08-17 11:59:07 -04001728#if _LIBCPP_STD_VER >= 17
Marshall Clowa0563332018-02-08 06:34:03 +00001729template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -05001730 class _CharT = __iter_value_type<_InputIterator>,
Marshall Clowa0563332018-02-08 06:34:03 +00001731 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001732 class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
1733 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowa0563332018-02-08 06:34:03 +00001734 >
1735basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
1736 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
Marshall Clowe46031a2018-07-02 18:41:15 +00001737
1738template<class _CharT,
1739 class _Traits,
1740 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001741 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowe46031a2018-07-02 18:41:15 +00001742 >
1743explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
1744 -> basic_string<_CharT, _Traits, _Allocator>;
1745
1746template<class _CharT,
1747 class _Traits,
1748 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001749 class = enable_if_t<__is_allocator<_Allocator>::value>,
Marshall Clowe46031a2018-07-02 18:41:15 +00001750 class _Sz = typename allocator_traits<_Allocator>::size_type
1751 >
1752basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
1753 -> basic_string<_CharT, _Traits, _Allocator>;
Marshall Clowa0563332018-02-08 06:34:03 +00001754#endif
1755
Howard Hinnantc51e1022010-05-11 19:42:16 +00001756template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001757inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001758void
1759basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
1760{
Louis Dionneba400782020-10-02 15:02:52 -04001761#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001762 if (!__libcpp_is_constant_evaluated())
1763 __get_db()->__invalidate_all(this);
Louis Dionneba400782020-10-02 15:02:52 -04001764#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001765}
1766
1767template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001768inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001769void
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001770basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type __pos)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001771{
Louis Dionneba400782020-10-02 15:02:52 -04001772#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001773 if (!__libcpp_is_constant_evaluated()) {
1774 __c_node* __c = __get_db()->__find_c_and_lock(this);
1775 if (__c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001776 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001777 const_pointer __new_last = __get_pointer() + __pos;
1778 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001779 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001780 --__p;
1781 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1782 if (__i->base() > __new_last)
1783 {
1784 (*__p)->__c_ = nullptr;
1785 if (--__c->end_ != __p)
1786 _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1787 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001788 }
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001789 __get_db()->unlock();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001790 }
1791 }
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001792#else
1793 (void)__pos;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001794#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001795}
1796
1797template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001798inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001799basic_string<_CharT, _Traits, _Allocator>::basic_string()
Marshall Clowe546cbb2015-06-04 02:05:41 +00001800 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001801 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001802{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001803 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001804 __zero();
1805}
1806
1807template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001808inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001809basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
Eric Fiseliere012bf22015-07-18 20:40:46 +00001810#if _LIBCPP_STD_VER <= 14
1811 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1812#else
1813 _NOEXCEPT
1814#endif
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001815: __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001816{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001817 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001818 __zero();
1819}
1820
1821template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier815ed732016-09-16 00:00:48 +00001822void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1823 size_type __sz,
1824 size_type __reserve)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001825{
1826 if (__reserve > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001827 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001828 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001829 if (__fits_in_sso(__reserve))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001830 {
1831 __set_short_size(__sz);
1832 __p = __get_short_pointer();
1833 }
1834 else
1835 {
1836 size_type __cap = __recommend(__reserve);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001837 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001838 __set_long_pointer(__p);
1839 __set_long_cap(__cap+1);
1840 __set_long_size(__sz);
1841 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001842 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001843 traits_type::assign(__p[__sz], value_type());
1844}
1845
1846template <class _CharT, class _Traits, class _Allocator>
1847void
Howard Hinnantd17880b2013-06-28 16:59:19 +00001848basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001849{
1850 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001851 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001852 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001853 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001854 {
1855 __set_short_size(__sz);
1856 __p = __get_short_pointer();
1857 }
1858 else
1859 {
1860 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001861 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001862 __set_long_pointer(__p);
1863 __set_long_cap(__cap+1);
1864 __set_long_size(__sz);
1865 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001866 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001867 traits_type::assign(__p[__sz], value_type());
1868}
1869
1870template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00001871template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00001872basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001873 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001874{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001875 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001876 __init(__s, traits_type::length(__s));
Nikolas Klauserf2807732022-01-11 00:33:35 +01001877 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001878}
1879
1880template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001881inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001882basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001883 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001884{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001885 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
1886 __init(__s, __n);
1887 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001888}
1889
1890template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001891inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001892basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001893 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001894{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001895 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001896 __init(__s, __n);
Nikolas Klauserf2807732022-01-11 00:33:35 +01001897 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001898}
1899
1900template <class _CharT, class _Traits, class _Allocator>
1901basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001902 : __r_(__default_init_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001903{
1904 if (!__str.__is_long())
1905 __r_.first().__r = __str.__r_.first().__r;
1906 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001907 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1908 __str.__get_long_size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01001909 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001910}
1911
1912template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00001913basic_string<_CharT, _Traits, _Allocator>::basic_string(
1914 const basic_string& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001915 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001916{
1917 if (!__str.__is_long())
1918 __r_.first().__r = __str.__r_.first().__r;
1919 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001920 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1921 __str.__get_long_size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01001922 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001923}
1924
Martijn Vels5e7c9752020-03-04 17:52:46 -05001925template <class _CharT, class _Traits, class _Allocator>
1926void basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(
1927 const value_type* __s, size_type __sz) {
1928 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001929 if (__fits_in_sso(__sz)) {
Martijn Vels5e7c9752020-03-04 17:52:46 -05001930 __p = __get_short_pointer();
1931 __set_short_size(__sz);
1932 } else {
1933 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001934 __throw_length_error();
Martijn Vels5e7c9752020-03-04 17:52:46 -05001935 size_t __cap = __recommend(__sz);
1936 __p = __alloc_traits::allocate(__alloc(), __cap + 1);
1937 __set_long_pointer(__p);
1938 __set_long_cap(__cap + 1);
1939 __set_long_size(__sz);
1940 }
1941 traits_type::copy(_VSTD::__to_address(__p), __s, __sz + 1);
1942}
1943
Eric Fiselierfc92be82017-04-19 00:28:44 +00001944#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001945
1946template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001947inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001948basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +00001949#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +00001950 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowa80abc72015-06-03 19:56:43 +00001951#else
1952 _NOEXCEPT
1953#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001954 : __r_(_VSTD::move(__str.__r_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001955{
1956 __str.__zero();
Nikolas Klauserf2807732022-01-11 00:33:35 +01001957 _VSTD::__debug_db_insert_c(this);
Louis Dionneba400782020-10-02 15:02:52 -04001958#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauserf2807732022-01-11 00:33:35 +01001959 if (!__libcpp_is_constant_evaluated() && __is_long())
1960 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001961#endif
1962}
1963
1964template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001965inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001966basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001967 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001968{
Marshall Clowd2d24692014-07-17 15:32:20 +00001969 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001970 __init(_VSTD::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd2d24692014-07-17 15:32:20 +00001971 else
1972 {
1973 __r_.first().__r = __str.__r_.first().__r;
1974 __str.__zero();
1975 }
Nikolas Klauserf2807732022-01-11 00:33:35 +01001976 _VSTD::__debug_db_insert_c(this);
Louis Dionneba400782020-10-02 15:02:52 -04001977#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauserf2807732022-01-11 00:33:35 +01001978 if (!__libcpp_is_constant_evaluated() && __is_long())
1979 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001980#endif
1981}
1982
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001983#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001984
1985template <class _CharT, class _Traits, class _Allocator>
1986void
1987basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
1988{
1989 if (__n > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01001990 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001991 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001992 if (__fits_in_sso(__n))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001993 {
1994 __set_short_size(__n);
1995 __p = __get_short_pointer();
1996 }
1997 else
1998 {
1999 size_type __cap = __recommend(__n);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002000 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002001 __set_long_pointer(__p);
2002 __set_long_cap(__cap+1);
2003 __set_long_size(__n);
2004 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002005 traits_type::assign(_VSTD::__to_address(__p), __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002006 traits_type::assign(__p[__n], value_type());
2007}
2008
2009template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002010inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002011basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002012 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002013{
2014 __init(__n, __c);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002015 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002016}
2017
2018template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002019template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00002020basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002021 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002022{
2023 __init(__n, __c);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002024 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002025}
2026
Howard Hinnantc51e1022010-05-11 19:42:16 +00002027template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00002028basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
2029 size_type __pos, size_type __n,
2030 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002031 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002032{
2033 size_type __str_sz = __str.size();
2034 if (__pos > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002035 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002036 __init(__str.data() + __pos, _VSTD::min(__n, __str_sz - __pos));
Nikolas Klauserf2807732022-01-11 00:33:35 +01002037 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002038}
2039
2040template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002041inline
Marshall Clow83445802016-04-07 18:13:41 +00002042basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +00002043 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002044 : __r_(__default_init_tag(), __a)
Marshall Clow83445802016-04-07 18:13:41 +00002045{
2046 size_type __str_sz = __str.size();
2047 if (__pos > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002048 __throw_out_of_range();
Marshall Clow83445802016-04-07 18:13:41 +00002049 __init(__str.data() + __pos, __str_sz - __pos);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002050 _VSTD::__debug_db_insert_c(this);
Marshall Clow83445802016-04-07 18:13:41 +00002051}
2052
2053template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002054template <class _Tp, class>
Marshall Clow78dbe462016-11-14 18:22:19 +00002055basic_string<_CharT, _Traits, _Allocator>::basic_string(
Marshall Clowe46031a2018-07-02 18:41:15 +00002056 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002057 : __r_(__default_init_tag(), __a)
Marshall Clow78dbe462016-11-14 18:22:19 +00002058{
Marshall Clowe46031a2018-07-02 18:41:15 +00002059 __self_view __sv0 = __t;
2060 __self_view __sv = __sv0.substr(__pos, __n);
Marshall Clow78dbe462016-11-14 18:22:19 +00002061 __init(__sv.data(), __sv.size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002062 _VSTD::__debug_db_insert_c(this);
Marshall Clow78dbe462016-11-14 18:22:19 +00002063}
2064
2065template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002066template <class _Tp, class>
2067basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002068 : __r_(__default_init_tag(), __default_init_tag())
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002069{
Marshall Clowe46031a2018-07-02 18:41:15 +00002070 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002071 __init(__sv.data(), __sv.size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002072 _VSTD::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002073}
2074
2075template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002076template <class _Tp, class>
2077basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002078 : __r_(__default_init_tag(), __a)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002079{
Marshall Clowe46031a2018-07-02 18:41:15 +00002080 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002081 __init(__sv.data(), __sv.size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002082 _VSTD::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002083}
2084
2085template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002086template <class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002087__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002088<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002089 __is_exactly_cpp17_input_iterator<_InputIterator>::value
2090>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002091basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
2092{
2093 __zero();
2094#ifndef _LIBCPP_NO_EXCEPTIONS
2095 try
2096 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002097#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002098 for (; __first != __last; ++__first)
2099 push_back(*__first);
2100#ifndef _LIBCPP_NO_EXCEPTIONS
2101 }
2102 catch (...)
2103 {
2104 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002105 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002106 throw;
2107 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002108#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002109}
2110
2111template <class _CharT, class _Traits, class _Allocator>
2112template <class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002113__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002114<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002115 __is_cpp17_forward_iterator<_ForwardIterator>::value
2116>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002117basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
2118{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002119 size_type __sz = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002120 if (__sz > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002121 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002122 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002123 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002124 {
2125 __set_short_size(__sz);
2126 __p = __get_short_pointer();
2127 }
2128 else
2129 {
2130 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002131 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002132 __set_long_pointer(__p);
2133 __set_long_cap(__cap+1);
2134 __set_long_size(__sz);
2135 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002136
2137#ifndef _LIBCPP_NO_EXCEPTIONS
2138 try
2139 {
2140#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002141 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002142 traits_type::assign(*__p, *__first);
2143 traits_type::assign(*__p, value_type());
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002144#ifndef _LIBCPP_NO_EXCEPTIONS
2145 }
2146 catch (...)
2147 {
2148 if (__is_long())
2149 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
2150 throw;
2151 }
2152#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002153}
2154
2155template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002156template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002157inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002158basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002159 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002160{
2161 __init(__first, __last);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002162 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002163}
2164
2165template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002166template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002167inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002168basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
2169 const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002170 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002171{
2172 __init(__first, __last);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002173 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002174}
2175
Eric Fiselierfc92be82017-04-19 00:28:44 +00002176#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002177
Howard Hinnantc51e1022010-05-11 19:42:16 +00002178template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002179inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002180basic_string<_CharT, _Traits, _Allocator>::basic_string(
2181 initializer_list<_CharT> __il)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002182 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002183{
2184 __init(__il.begin(), __il.end());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002185 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002186}
2187
2188template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002189inline
Eric Fiselier9d355982017-04-12 23:45:53 +00002190
Eric Fiselier812882b2017-02-17 01:17:10 +00002191basic_string<_CharT, _Traits, _Allocator>::basic_string(
2192 initializer_list<_CharT> __il, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002193 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002194{
2195 __init(__il.begin(), __il.end());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002196 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002197}
2198
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002199#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002200
Howard Hinnantc51e1022010-05-11 19:42:16 +00002201template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002202basic_string<_CharT, _Traits, _Allocator>::~basic_string()
2203{
Louis Dionneba400782020-10-02 15:02:52 -04002204#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002205 if (!__libcpp_is_constant_evaluated())
2206 __get_db()->__erase_c(this);
Howard Hinnant8ea98242013-08-23 17:37:05 +00002207#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002208 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002209 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002210}
2211
2212template <class _CharT, class _Traits, class _Allocator>
2213void
2214basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
2215 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002216 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 +00002217{
2218 size_type __ms = max_size();
2219 if (__delta_cap > __ms - __old_cap - 1)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002220 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002221 pointer __old_p = __get_pointer();
2222 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002223 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002224 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002225 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002226 __invalidate_all_iterators();
2227 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002228 traits_type::copy(_VSTD::__to_address(__p),
2229 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002230 if (__n_add != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002231 traits_type::copy(_VSTD::__to_address(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002232 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2233 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002234 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2235 _VSTD::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002236 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002237 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002238 __set_long_pointer(__p);
2239 __set_long_cap(__cap+1);
2240 __old_sz = __n_copy + __n_add + __sec_cp_sz;
2241 __set_long_size(__old_sz);
2242 traits_type::assign(__p[__old_sz], value_type());
2243}
2244
2245template <class _CharT, class _Traits, class _Allocator>
2246void
2247basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2248 size_type __n_copy, size_type __n_del, size_type __n_add)
2249{
2250 size_type __ms = max_size();
Marshall Clow2267c5d2013-11-06 14:24:38 +00002251 if (__delta_cap > __ms - __old_cap)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002252 __throw_length_error();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002253 pointer __old_p = __get_pointer();
2254 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002255 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002256 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002257 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002258 __invalidate_all_iterators();
2259 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002260 traits_type::copy(_VSTD::__to_address(__p),
2261 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002262 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2263 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002264 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2265 _VSTD::__to_address(__old_p) + __n_copy + __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002266 __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002267 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002268 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002269 __set_long_pointer(__p);
2270 __set_long_cap(__cap+1);
2271}
2272
2273// assign
2274
2275template <class _CharT, class _Traits, class _Allocator>
Martijn Vels596e3de2020-02-26 15:55:49 -05002276template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04002277basic_string<_CharT, _Traits, _Allocator>&
2278basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(
Martijn Vels596e3de2020-02-26 15:55:49 -05002279 const value_type* __s, size_type __n) {
2280 size_type __cap = __is_short ? __min_cap : __get_long_cap();
2281 if (__n < __cap) {
2282 pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
2283 __is_short ? __set_short_size(__n) : __set_long_size(__n);
2284 traits_type::copy(_VSTD::__to_address(__p), __s, __n);
2285 traits_type::assign(__p[__n], value_type());
2286 __invalidate_iterators_past(__n);
2287 } else {
2288 size_type __sz = __is_short ? __get_short_size() : __get_long_size();
2289 __grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
2290 }
Martijn Velsb6a08b62020-04-10 18:36:31 -04002291 return *this;
Martijn Vels596e3de2020-02-26 15:55:49 -05002292}
2293
2294template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002295basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002296basic_string<_CharT, _Traits, _Allocator>::__assign_external(
2297 const value_type* __s, size_type __n) {
2298 size_type __cap = capacity();
2299 if (__cap >= __n) {
2300 value_type* __p = _VSTD::__to_address(__get_pointer());
2301 traits_type::move(__p, __s, __n);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002302 return __null_terminate_at(__p, __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04002303 } else {
2304 size_type __sz = size();
2305 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002306 return *this;
Martijn Velsda7d94f2020-06-19 14:24:03 -04002307 }
Martijn Velsda7d94f2020-06-19 14:24:03 -04002308}
2309
2310template <class _CharT, class _Traits, class _Allocator>
2311basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002312basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002313{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002314 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Nikolas Klauser93826d12022-01-04 17:24:03 +01002315 return (__builtin_constant_p(__n) && __fits_in_sso(__n))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002316 ? __assign_short(__s, __n)
2317 : __assign_external(__s, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002318}
2319
2320template <class _CharT, class _Traits, class _Allocator>
2321basic_string<_CharT, _Traits, _Allocator>&
2322basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2323{
2324 size_type __cap = capacity();
2325 if (__cap < __n)
2326 {
2327 size_type __sz = size();
2328 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2329 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002330 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002331 traits_type::assign(__p, __n, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002332 return __null_terminate_at(__p, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002333}
2334
2335template <class _CharT, class _Traits, class _Allocator>
2336basic_string<_CharT, _Traits, _Allocator>&
2337basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2338{
2339 pointer __p;
2340 if (__is_long())
2341 {
2342 __p = __get_long_pointer();
2343 __set_long_size(1);
2344 }
2345 else
2346 {
2347 __p = __get_short_pointer();
2348 __set_short_size(1);
2349 }
2350 traits_type::assign(*__p, __c);
2351 traits_type::assign(*++__p, value_type());
2352 __invalidate_iterators_past(1);
2353 return *this;
2354}
2355
2356template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002357basic_string<_CharT, _Traits, _Allocator>&
2358basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2359{
Martijn Vels596e3de2020-02-26 15:55:49 -05002360 if (this != &__str) {
2361 __copy_assign_alloc(__str);
2362 if (!__is_long()) {
2363 if (!__str.__is_long()) {
Eric Fiselier03bbc922020-01-15 17:27:10 -05002364 __r_.first().__r = __str.__r_.first().__r;
Martijn Vels596e3de2020-02-26 15:55:49 -05002365 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002366 return __assign_no_alias<true>(__str.data(), __str.size());
Martijn Vels596e3de2020-02-26 15:55:49 -05002367 }
2368 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002369 return __assign_no_alias<false>(__str.data(), __str.size());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002370 }
Martijn Vels596e3de2020-02-26 15:55:49 -05002371 }
2372 return *this;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002373}
2374
Eric Fiselierfc92be82017-04-19 00:28:44 +00002375#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002376
2377template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002378inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002379void
2380basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002381 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002382{
2383 if (__alloc() != __str.__alloc())
2384 assign(__str);
2385 else
2386 __move_assign(__str, true_type());
2387}
2388
2389template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002390inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002391void
2392basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002393#if _LIBCPP_STD_VER > 14
2394 _NOEXCEPT
2395#else
Howard Hinnant3e276872011-06-03 18:40:47 +00002396 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002397#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002398{
marshall2ed41622019-11-27 07:13:00 -08002399 if (__is_long()) {
2400 __alloc_traits::deallocate(__alloc(), __get_long_pointer(),
2401 __get_long_cap());
2402#if _LIBCPP_STD_VER <= 14
2403 if (!is_nothrow_move_assignable<allocator_type>::value) {
2404 __set_short_size(0);
2405 traits_type::assign(__get_short_pointer()[0], value_type());
2406 }
2407#endif
2408 }
2409 __move_assign_alloc(__str);
2410 __r_.first() = __str.__r_.first();
2411 __str.__set_short_size(0);
2412 traits_type::assign(__str.__get_short_pointer()[0], value_type());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002413}
2414
2415template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002416inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002417basic_string<_CharT, _Traits, _Allocator>&
2418basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002419 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002420{
2421 __move_assign(__str, integral_constant<bool,
2422 __alloc_traits::propagate_on_container_move_assignment::value>());
2423 return *this;
2424}
2425
2426#endif
2427
2428template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002429template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002430__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002431<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002432 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002433 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002434>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002435basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2436{
Marshall Clow958362f2016-09-05 01:54:30 +00002437 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002438 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002439 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002440}
2441
2442template <class _CharT, class _Traits, class _Allocator>
2443template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002444__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002445<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002446 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002447 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002448>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002449basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2450{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002451 size_type __cap = capacity();
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002452 size_type __n = __string_is_trivial_iterator<_ForwardIterator>::value ?
2453 static_cast<size_type>(_VSTD::distance(__first, __last)) : 0;
2454
2455 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2456 (__cap >= __n || !__addr_in_range(*__first)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002457 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002458 if (__cap < __n)
2459 {
2460 size_type __sz = size();
2461 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2462 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002463 pointer __p = __get_pointer();
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002464 for (; __first != __last; ++__p, (void) ++__first)
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002465 traits_type::assign(*__p, *__first);
2466 traits_type::assign(*__p, value_type());
2467 __set_size(__n);
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04002468 __invalidate_iterators_past(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002469 }
2470 else
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002471 {
2472 const basic_string __temp(__first, __last, __alloc());
2473 assign(__temp.data(), __temp.size());
2474 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002475 return *this;
2476}
2477
2478template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002479basic_string<_CharT, _Traits, _Allocator>&
2480basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2481{
2482 size_type __sz = __str.size();
2483 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002484 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002485 return assign(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002486}
2487
2488template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002489template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002490__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002491<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002492 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
2493 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00002494 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002495>
Marshall Clow82513342016-09-24 22:45:42 +00002496basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002497{
Marshall Clow82513342016-09-24 22:45:42 +00002498 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002499 size_type __sz = __sv.size();
2500 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002501 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002502 return assign(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2503}
2504
2505
2506template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002507basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002508basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s) {
2509 return __assign_external(__s, traits_type::length(__s));
2510}
2511
2512template <class _CharT, class _Traits, class _Allocator>
2513basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002514basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002515{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002516 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Louis Dionne3e0c4332021-08-31 10:49:06 -04002517 return __builtin_constant_p(*__s)
Nikolas Klauser93826d12022-01-04 17:24:03 +01002518 ? (__fits_in_sso(traits_type::length(__s))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002519 ? __assign_short(__s, traits_type::length(__s))
2520 : __assign_external(__s, traits_type::length(__s)))
2521 : __assign_external(__s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002522}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002523// append
2524
2525template <class _CharT, class _Traits, class _Allocator>
2526basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002527basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002528{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002529 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002530 size_type __cap = capacity();
2531 size_type __sz = size();
2532 if (__cap - __sz >= __n)
2533 {
2534 if (__n)
2535 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002536 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002537 traits_type::copy(__p + __sz, __s, __n);
2538 __sz += __n;
2539 __set_size(__sz);
2540 traits_type::assign(__p[__sz], value_type());
2541 }
2542 }
2543 else
2544 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2545 return *this;
2546}
2547
2548template <class _CharT, class _Traits, class _Allocator>
2549basic_string<_CharT, _Traits, _Allocator>&
2550basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2551{
2552 if (__n)
2553 {
2554 size_type __cap = capacity();
2555 size_type __sz = size();
2556 if (__cap - __sz < __n)
2557 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2558 pointer __p = __get_pointer();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002559 traits_type::assign(_VSTD::__to_address(__p) + __sz, __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002560 __sz += __n;
2561 __set_size(__sz);
2562 traits_type::assign(__p[__sz], value_type());
2563 }
2564 return *this;
2565}
2566
2567template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00002568inline void
2569basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n)
2570{
2571 if (__n)
2572 {
2573 size_type __cap = capacity();
2574 size_type __sz = size();
2575 if (__cap - __sz < __n)
2576 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2577 pointer __p = __get_pointer();
2578 __sz += __n;
2579 __set_size(__sz);
2580 traits_type::assign(__p[__sz], value_type());
2581 }
2582}
2583
2584template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002585void
2586basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2587{
Howard Hinnant68bf1812013-04-30 21:44:48 +00002588 bool __is_short = !__is_long();
2589 size_type __cap;
2590 size_type __sz;
2591 if (__is_short)
2592 {
2593 __cap = __min_cap - 1;
2594 __sz = __get_short_size();
2595 }
2596 else
2597 {
2598 __cap = __get_long_cap() - 1;
2599 __sz = __get_long_size();
2600 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002601 if (__sz == __cap)
Howard Hinnant68bf1812013-04-30 21:44:48 +00002602 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002603 __grow_by(__cap, 1, __sz, __sz, 0);
Louis Dionne82d2b2c2021-11-16 11:26:56 -05002604 __is_short = false; // the string is always long after __grow_by
Howard Hinnant68bf1812013-04-30 21:44:48 +00002605 }
2606 pointer __p;
2607 if (__is_short)
2608 {
2609 __p = __get_short_pointer() + __sz;
2610 __set_short_size(__sz+1);
2611 }
2612 else
2613 {
2614 __p = __get_long_pointer() + __sz;
2615 __set_long_size(__sz+1);
2616 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002617 traits_type::assign(*__p, __c);
2618 traits_type::assign(*++__p, value_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002619}
2620
Howard Hinnantc51e1022010-05-11 19:42:16 +00002621template <class _CharT, class _Traits, class _Allocator>
2622template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002623__enable_if_t
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002624<
2625 __is_cpp17_forward_iterator<_ForwardIterator>::value,
2626 basic_string<_CharT, _Traits, _Allocator>&
2627>
2628basic_string<_CharT, _Traits, _Allocator>::append(
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002629 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002630{
2631 size_type __sz = size();
2632 size_type __cap = capacity();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002633 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002634 if (__n)
2635 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002636 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2637 !__addr_in_range(*__first))
Marshall Clow958362f2016-09-05 01:54:30 +00002638 {
2639 if (__cap - __sz < __n)
2640 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2641 pointer __p = __get_pointer() + __sz;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002642 for (; __first != __last; ++__p, (void) ++__first)
Marshall Clow958362f2016-09-05 01:54:30 +00002643 traits_type::assign(*__p, *__first);
2644 traits_type::assign(*__p, value_type());
2645 __set_size(__sz + __n);
2646 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002647 else
2648 {
2649 const basic_string __temp(__first, __last, __alloc());
2650 append(__temp.data(), __temp.size());
2651 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002652 }
2653 return *this;
2654}
2655
2656template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002657inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002658basic_string<_CharT, _Traits, _Allocator>&
2659basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2660{
2661 return append(__str.data(), __str.size());
2662}
2663
2664template <class _CharT, class _Traits, class _Allocator>
2665basic_string<_CharT, _Traits, _Allocator>&
2666basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2667{
2668 size_type __sz = __str.size();
2669 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002670 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002671 return append(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002672}
2673
2674template <class _CharT, class _Traits, class _Allocator>
Marshall Clow62953962016-10-03 23:40:48 +00002675template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002676 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002677 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002678 __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 +00002679 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002680 >
Marshall Clow82513342016-09-24 22:45:42 +00002681basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002682{
Marshall Clow82513342016-09-24 22:45:42 +00002683 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002684 size_type __sz = __sv.size();
2685 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002686 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002687 return append(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2688}
2689
2690template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002691basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002692basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002693{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002694 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002695 return append(__s, traits_type::length(__s));
2696}
2697
2698// insert
2699
2700template <class _CharT, class _Traits, class _Allocator>
2701basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002702basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002703{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002704 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002705 size_type __sz = size();
2706 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002707 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002708 size_type __cap = capacity();
2709 if (__cap - __sz >= __n)
2710 {
2711 if (__n)
2712 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002713 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002714 size_type __n_move = __sz - __pos;
2715 if (__n_move != 0)
2716 {
2717 if (__p + __pos <= __s && __s < __p + __sz)
2718 __s += __n;
2719 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2720 }
2721 traits_type::move(__p + __pos, __s, __n);
2722 __sz += __n;
2723 __set_size(__sz);
2724 traits_type::assign(__p[__sz], value_type());
2725 }
2726 }
2727 else
2728 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2729 return *this;
2730}
2731
2732template <class _CharT, class _Traits, class _Allocator>
2733basic_string<_CharT, _Traits, _Allocator>&
2734basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2735{
2736 size_type __sz = size();
2737 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002738 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002739 if (__n)
2740 {
2741 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002742 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002743 if (__cap - __sz >= __n)
2744 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002745 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002746 size_type __n_move = __sz - __pos;
2747 if (__n_move != 0)
2748 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2749 }
2750 else
2751 {
2752 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002753 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002754 }
2755 traits_type::assign(__p + __pos, __n, __c);
2756 __sz += __n;
2757 __set_size(__sz);
2758 traits_type::assign(__p[__sz], value_type());
2759 }
2760 return *this;
2761}
2762
2763template <class _CharT, class _Traits, class _Allocator>
2764template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002765__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002766<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002767 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Marshall Clow039b2f02016-01-13 21:54:34 +00002768 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002769>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002770basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2771{
Nikolas Klausereed25832021-12-15 01:32:30 +01002772 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2773 "string::insert(iterator, range) called with an iterator not"
2774 " referring to this string");
2775 const basic_string __temp(__first, __last, __alloc());
2776 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002777}
2778
2779template <class _CharT, class _Traits, class _Allocator>
2780template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002781__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002782<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002783 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002784 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002785>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002786basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2787{
Nikolas Klausereed25832021-12-15 01:32:30 +01002788 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2789 "string::insert(iterator, range) called with an iterator not"
2790 " referring to this string");
2791
Howard Hinnantc51e1022010-05-11 19:42:16 +00002792 size_type __ip = static_cast<size_type>(__pos - begin());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002793 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002794 if (__n)
2795 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002796 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2797 !__addr_in_range(*__first))
2798 {
2799 size_type __sz = size();
2800 size_type __cap = capacity();
2801 value_type* __p;
2802 if (__cap - __sz >= __n)
2803 {
2804 __p = _VSTD::__to_address(__get_pointer());
2805 size_type __n_move = __sz - __ip;
2806 if (__n_move != 0)
2807 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
2808 }
2809 else
2810 {
2811 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
2812 __p = _VSTD::__to_address(__get_long_pointer());
2813 }
2814 __sz += __n;
2815 __set_size(__sz);
2816 traits_type::assign(__p[__sz], value_type());
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002817 for (__p += __ip; __first != __last; ++__p, (void) ++__first)
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002818 traits_type::assign(*__p, *__first);
2819 }
2820 else
Marshall Clow958362f2016-09-05 01:54:30 +00002821 {
2822 const basic_string __temp(__first, __last, __alloc());
2823 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
2824 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002825 }
2826 return begin() + __ip;
2827}
2828
2829template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002830inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002831basic_string<_CharT, _Traits, _Allocator>&
2832basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2833{
2834 return insert(__pos1, __str.data(), __str.size());
2835}
2836
2837template <class _CharT, class _Traits, class _Allocator>
2838basic_string<_CharT, _Traits, _Allocator>&
2839basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2840 size_type __pos2, size_type __n)
2841{
2842 size_type __str_sz = __str.size();
2843 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002844 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002845 return insert(__pos1, __str.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002846}
2847
2848template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002849template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002850__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002851<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002852 __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 +00002853 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002854>
Marshall Clow82513342016-09-24 22:45:42 +00002855basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002856 size_type __pos2, size_type __n)
2857{
Marshall Clow82513342016-09-24 22:45:42 +00002858 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002859 size_type __str_sz = __sv.size();
2860 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002861 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002862 return insert(__pos1, __sv.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
2863}
2864
2865template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002866basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002867basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002868{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002869 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002870 return insert(__pos, __s, traits_type::length(__s));
2871}
2872
2873template <class _CharT, class _Traits, class _Allocator>
2874typename basic_string<_CharT, _Traits, _Allocator>::iterator
2875basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2876{
Louis Dionnef4cc6fb2022-02-15 15:47:45 -05002877 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2878 "string::insert(iterator, character) called with an iterator not"
2879 " referring to this string");
2880
Howard Hinnantc51e1022010-05-11 19:42:16 +00002881 size_type __ip = static_cast<size_type>(__pos - begin());
2882 size_type __sz = size();
2883 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002884 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002885 if (__cap == __sz)
2886 {
2887 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002888 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002889 }
2890 else
2891 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002892 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002893 size_type __n_move = __sz - __ip;
2894 if (__n_move != 0)
2895 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2896 }
2897 traits_type::assign(__p[__ip], __c);
2898 traits_type::assign(__p[++__sz], value_type());
2899 __set_size(__sz);
2900 return begin() + static_cast<difference_type>(__ip);
2901}
2902
2903template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002904inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002905typename basic_string<_CharT, _Traits, _Allocator>::iterator
2906basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2907{
Nikolas Klausereed25832021-12-15 01:32:30 +01002908 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2909 "string::insert(iterator, n, value) called with an iterator not"
2910 " referring to this string");
2911 difference_type __p = __pos - begin();
2912 insert(static_cast<size_type>(__p), __n, __c);
2913 return begin() + __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002914}
2915
2916// replace
2917
2918template <class _CharT, class _Traits, class _Allocator>
2919basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002920basic_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 +00002921 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00002922{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002923 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002924 size_type __sz = size();
2925 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002926 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002927 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002928 size_type __cap = capacity();
2929 if (__cap - __sz + __n1 >= __n2)
2930 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002931 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002932 if (__n1 != __n2)
2933 {
2934 size_type __n_move = __sz - __pos - __n1;
2935 if (__n_move != 0)
2936 {
2937 if (__n1 > __n2)
2938 {
2939 traits_type::move(__p + __pos, __s, __n2);
2940 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002941 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002942 }
2943 if (__p + __pos < __s && __s < __p + __sz)
2944 {
2945 if (__p + __pos + __n1 <= __s)
2946 __s += __n2 - __n1;
2947 else // __p + __pos < __s < __p + __pos + __n1
2948 {
2949 traits_type::move(__p + __pos, __s, __n1);
2950 __pos += __n1;
2951 __s += __n2;
2952 __n2 -= __n1;
2953 __n1 = 0;
2954 }
2955 }
2956 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2957 }
2958 }
2959 traits_type::move(__p + __pos, __s, __n2);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002960 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002961 }
2962 else
2963 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
2964 return *this;
2965}
2966
2967template <class _CharT, class _Traits, class _Allocator>
2968basic_string<_CharT, _Traits, _Allocator>&
2969basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
2970{
2971 size_type __sz = size();
2972 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01002973 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002974 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002975 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002976 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002977 if (__cap - __sz + __n1 >= __n2)
2978 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002979 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002980 if (__n1 != __n2)
2981 {
2982 size_type __n_move = __sz - __pos - __n1;
2983 if (__n_move != 0)
2984 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2985 }
2986 }
2987 else
2988 {
2989 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002990 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002991 }
2992 traits_type::assign(__p + __pos, __n2, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002993 return __null_terminate_at(__p, __sz - (__n1 - __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002994}
2995
2996template <class _CharT, class _Traits, class _Allocator>
2997template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002998__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002999<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003000 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003001 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003002>
Howard Hinnant990d6e82010-11-17 21:11:40 +00003003basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003004 _InputIterator __j1, _InputIterator __j2)
3005{
Marshall Clow958362f2016-09-05 01:54:30 +00003006 const basic_string __temp(__j1, __j2, __alloc());
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003007 return replace(__i1, __i2, __temp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003008}
3009
3010template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003011inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003012basic_string<_CharT, _Traits, _Allocator>&
3013basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
3014{
3015 return replace(__pos1, __n1, __str.data(), __str.size());
3016}
3017
3018template <class _CharT, class _Traits, class _Allocator>
3019basic_string<_CharT, _Traits, _Allocator>&
3020basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
3021 size_type __pos2, size_type __n2)
3022{
3023 size_type __str_sz = __str.size();
3024 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003025 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003026 return replace(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003027}
3028
3029template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003030template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003031__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003032<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003033 __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 +00003034 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003035>
Marshall Clow82513342016-09-24 22:45:42 +00003036basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003037 size_type __pos2, size_type __n2)
3038{
Marshall Clow82513342016-09-24 22:45:42 +00003039 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003040 size_type __str_sz = __sv.size();
3041 if (__pos2 > __str_sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003042 __throw_out_of_range();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003043 return replace(__pos1, __n1, __sv.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
3044}
3045
3046template <class _CharT, class _Traits, class _Allocator>
3047basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003048basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003049{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003050 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003051 return replace(__pos, __n1, __s, traits_type::length(__s));
3052}
3053
3054template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003055inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003056basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003057basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003058{
3059 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
3060 __str.data(), __str.size());
3061}
3062
3063template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003064inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003065basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003066basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003067{
3068 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
3069}
3070
3071template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003072inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003073basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003074basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003075{
3076 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
3077}
3078
3079template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003080inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003081basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003082basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003083{
3084 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
3085}
3086
3087// erase
3088
Martijn Velsa81fc792020-02-26 13:25:43 -05003089// 'externally instantiated' erase() implementation, called when __n != npos.
3090// Does not check __pos against size()
Howard Hinnantc51e1022010-05-11 19:42:16 +00003091template <class _CharT, class _Traits, class _Allocator>
Martijn Velsa81fc792020-02-26 13:25:43 -05003092void
3093basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(
3094 size_type __pos, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003095{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003096 if (__n)
3097 {
Martijn Velsa81fc792020-02-26 13:25:43 -05003098 size_type __sz = size();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003099 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003100 __n = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003101 size_type __n_move = __sz - __pos - __n;
3102 if (__n_move != 0)
3103 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003104 __null_terminate_at(__p, __sz - __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003105 }
Martijn Velsa81fc792020-02-26 13:25:43 -05003106}
3107
3108template <class _CharT, class _Traits, class _Allocator>
3109basic_string<_CharT, _Traits, _Allocator>&
3110basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos,
3111 size_type __n) {
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003112 if (__pos > size())
3113 __throw_out_of_range();
Martijn Velsa81fc792020-02-26 13:25:43 -05003114 if (__n == npos) {
3115 __erase_to_end(__pos);
3116 } else {
3117 __erase_external_with_move(__pos, __n);
3118 }
3119 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003120}
3121
3122template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003123inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003124typename basic_string<_CharT, _Traits, _Allocator>::iterator
3125basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
3126{
Nikolas Klausereed25832021-12-15 01:32:30 +01003127 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3128 "string::erase(iterator) called with an iterator not"
3129 " referring to this string");
3130
3131 _LIBCPP_ASSERT(__pos != end(), "string::erase(iterator) called with a non-dereferenceable iterator");
3132 iterator __b = begin();
3133 size_type __r = static_cast<size_type>(__pos - __b);
3134 erase(__r, 1);
3135 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003136}
3137
3138template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003139inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003140typename basic_string<_CharT, _Traits, _Allocator>::iterator
3141basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
3142{
Nikolas Klausereed25832021-12-15 01:32:30 +01003143 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
3144 "string::erase(iterator, iterator) called with an iterator not"
3145 " referring to this string");
3146
3147 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
3148 iterator __b = begin();
3149 size_type __r = static_cast<size_type>(__first - __b);
3150 erase(__r, static_cast<size_type>(__last - __first));
3151 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003152}
3153
3154template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003155inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003156void
3157basic_string<_CharT, _Traits, _Allocator>::pop_back()
3158{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003159 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003160 __erase_to_end(size() - 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003161}
3162
3163template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003164inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003165void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003166basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003167{
3168 __invalidate_all_iterators();
3169 if (__is_long())
3170 {
3171 traits_type::assign(*__get_long_pointer(), value_type());
3172 __set_long_size(0);
3173 }
3174 else
3175 {
3176 traits_type::assign(*__get_short_pointer(), value_type());
3177 __set_short_size(0);
3178 }
3179}
3180
3181template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003182inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003183void
3184basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
3185{
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003186 __null_terminate_at(_VSTD::__to_address(__get_pointer()), __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003187}
3188
3189template <class _CharT, class _Traits, class _Allocator>
3190void
3191basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
3192{
3193 size_type __sz = size();
3194 if (__n > __sz)
3195 append(__n - __sz, __c);
3196 else
3197 __erase_to_end(__n);
3198}
3199
3200template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00003201inline void
3202basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n)
3203{
3204 size_type __sz = size();
3205 if (__n > __sz) {
3206 __append_default_init(__n - __sz);
3207 } else
3208 __erase_to_end(__n);
3209}
3210
3211template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003212inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003213typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003214basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003215{
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003216 size_type __m = __alloc_traits::max_size(__alloc());
Eric Fiseliere9cc5922017-10-17 13:16:01 +00003217#ifdef _LIBCPP_BIG_ENDIAN
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003218 return (__m <= ~__long_mask ? __m : __m/2) - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003219#else
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003220 return __m - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003221#endif
3222}
3223
3224template <class _CharT, class _Traits, class _Allocator>
3225void
Marek Kurdejc9848142020-11-26 10:07:16 +01003226basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __requested_capacity)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003227{
Marek Kurdejc9848142020-11-26 10:07:16 +01003228 if (__requested_capacity > max_size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003229 __throw_length_error();
Marek Kurdejc9848142020-11-26 10:07:16 +01003230
Louis Dionne05b6d3e2022-01-14 12:30:22 -05003231 // Make sure reserve(n) never shrinks. This is technically only required in C++20
3232 // and later (since P0966R1), however we provide consistent behavior in all Standard
3233 // modes because this function is instantiated in the shared library.
3234 if (__requested_capacity <= capacity())
3235 return;
Marek Kurdejc9848142020-11-26 10:07:16 +01003236
3237 size_type __target_capacity = _VSTD::max(__requested_capacity, size());
3238 __target_capacity = __recommend(__target_capacity);
3239 if (__target_capacity == capacity()) return;
3240
3241 __shrink_or_extend(__target_capacity);
3242}
3243
3244template <class _CharT, class _Traits, class _Allocator>
Louis Dionnee38b8642021-12-13 14:15:21 -05003245inline
Marek Kurdejc9848142020-11-26 10:07:16 +01003246void
3247basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT
3248{
3249 size_type __target_capacity = __recommend(size());
3250 if (__target_capacity == capacity()) return;
3251
3252 __shrink_or_extend(__target_capacity);
3253}
3254
3255template <class _CharT, class _Traits, class _Allocator>
Louis Dionnee38b8642021-12-13 14:15:21 -05003256inline
Marek Kurdejc9848142020-11-26 10:07:16 +01003257void
3258basic_string<_CharT, _Traits, _Allocator>::__shrink_or_extend(size_type __target_capacity)
3259{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003260 size_type __cap = capacity();
3261 size_type __sz = size();
Marek Kurdejc9848142020-11-26 10:07:16 +01003262
3263 pointer __new_data, __p;
3264 bool __was_long, __now_long;
3265 if (__target_capacity == __min_cap - 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003266 {
Marek Kurdejc9848142020-11-26 10:07:16 +01003267 __was_long = true;
3268 __now_long = false;
3269 __new_data = __get_short_pointer();
3270 __p = __get_long_pointer();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003271 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003272 else
3273 {
3274 if (__target_capacity > __cap)
3275 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3276 else
3277 {
3278 #ifndef _LIBCPP_NO_EXCEPTIONS
3279 try
3280 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003281 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003282 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3283 #ifndef _LIBCPP_NO_EXCEPTIONS
3284 }
3285 catch (...)
3286 {
3287 return;
3288 }
3289 #else // _LIBCPP_NO_EXCEPTIONS
3290 if (__new_data == nullptr)
3291 return;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003292 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003293 }
3294 __now_long = true;
3295 __was_long = __is_long();
3296 __p = __get_pointer();
3297 }
3298 traits_type::copy(_VSTD::__to_address(__new_data),
3299 _VSTD::__to_address(__p), size()+1);
3300 if (__was_long)
3301 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
3302 if (__now_long)
3303 {
3304 __set_long_cap(__target_capacity+1);
3305 __set_long_size(__sz);
3306 __set_long_pointer(__new_data);
3307 }
3308 else
3309 __set_short_size(__sz);
3310 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003311}
3312
3313template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003314inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003315typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003316basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003317{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003318 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003319 return *(data() + __pos);
3320}
3321
3322template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003323inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003324typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003325basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003326{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003327 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003328 return *(__get_pointer() + __pos);
3329}
3330
3331template <class _CharT, class _Traits, class _Allocator>
3332typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3333basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3334{
3335 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003336 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003337 return (*this)[__n];
3338}
3339
3340template <class _CharT, class _Traits, class _Allocator>
3341typename basic_string<_CharT, _Traits, _Allocator>::reference
3342basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
3343{
3344 if (__n >= size())
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003345 __throw_out_of_range();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003346 return (*this)[__n];
3347}
3348
3349template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003350inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003351typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003352basic_string<_CharT, _Traits, _Allocator>::front() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003353{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003354 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003355 return *__get_pointer();
3356}
3357
3358template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003359inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003360typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003361basic_string<_CharT, _Traits, _Allocator>::front() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003362{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003363 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003364 return *data();
3365}
3366
3367template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003368inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003369typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003370basic_string<_CharT, _Traits, _Allocator>::back() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003371{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003372 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003373 return *(__get_pointer() + size() - 1);
3374}
3375
3376template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003377inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003378typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003379basic_string<_CharT, _Traits, _Allocator>::back() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003380{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003381 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003382 return *(data() + size() - 1);
3383}
3384
3385template <class _CharT, class _Traits, class _Allocator>
3386typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003387basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003388{
3389 size_type __sz = size();
3390 if (__pos > __sz)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003391 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003392 size_type __rlen = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003393 traits_type::copy(__s, data() + __pos, __rlen);
3394 return __rlen;
3395}
3396
3397template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003398inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003399basic_string<_CharT, _Traits, _Allocator>
3400basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3401{
3402 return basic_string(*this, __pos, __n, __alloc());
3403}
3404
3405template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003406inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003407void
3408basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003409#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00003410 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00003411#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00003412 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003413 __is_nothrow_swappable<allocator_type>::value)
3414#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003415{
Louis Dionneba400782020-10-02 15:02:52 -04003416#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01003417 if (!__libcpp_is_constant_evaluated()) {
3418 if (!__is_long())
3419 __get_db()->__invalidate_all(this);
3420 if (!__str.__is_long())
3421 __get_db()->__invalidate_all(&__str);
3422 __get_db()->swap(this, &__str);
3423 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00003424#endif
Eric Fiselier9bf691f2016-12-28 05:53:01 +00003425 _LIBCPP_ASSERT(
3426 __alloc_traits::propagate_on_container_swap::value ||
3427 __alloc_traits::is_always_equal::value ||
3428 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003429 _VSTD::swap(__r_.first(), __str.__r_.first());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05003430 _VSTD::__swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003431}
3432
3433// find
3434
3435template <class _Traits>
3436struct _LIBCPP_HIDDEN __traits_eq
3437{
3438 typedef typename _Traits::char_type char_type;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003439 _LIBCPP_INLINE_VISIBILITY
3440 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3441 {return _Traits::eq(__x, __y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003442};
3443
3444template<class _CharT, class _Traits, class _Allocator>
3445typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003446basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003447 size_type __pos,
3448 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003449{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003450 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003451 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003452 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003453}
3454
3455template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003456inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003457typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003458basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3459 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003460{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003461 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003462 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003463}
3464
3465template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003466template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003467__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003468<
3469 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3470 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003471>
Marshall Clowe46031a2018-07-02 18:41:15 +00003472basic_string<_CharT, _Traits, _Allocator>::find(const _Tp &__t,
zoecarver1997e0a2021-02-05 11:54:47 -08003473 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003474{
Marshall Clowe46031a2018-07-02 18:41:15 +00003475 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003476 return __str_find<value_type, size_type, traits_type, npos>
3477 (data(), size(), __sv.data(), __pos, __sv.size());
3478}
3479
3480template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003481inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003482typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003483basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003484 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003485{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003486 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003487 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003488 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003489}
3490
3491template<class _CharT, class _Traits, class _Allocator>
3492typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003493basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3494 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003495{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003496 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003497 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003498}
3499
3500// rfind
3501
3502template<class _CharT, class _Traits, class _Allocator>
3503typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003504basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003505 size_type __pos,
3506 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003507{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003508 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003509 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003510 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003511}
3512
3513template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003514inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003515typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003516basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3517 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003518{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003519 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003520 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003521}
3522
3523template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003524template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003525__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003526<
3527 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3528 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003529>
Marshall Clowe46031a2018-07-02 18:41:15 +00003530basic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003531 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003532{
Marshall Clowe46031a2018-07-02 18:41:15 +00003533 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003534 return __str_rfind<value_type, size_type, traits_type, npos>
3535 (data(), size(), __sv.data(), __pos, __sv.size());
3536}
3537
3538template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003539inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003540typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003541basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003542 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003543{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003544 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003545 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003546 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003547}
3548
3549template<class _CharT, class _Traits, class _Allocator>
3550typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003551basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3552 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003553{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003554 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003555 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003556}
3557
3558// find_first_of
3559
3560template<class _CharT, class _Traits, class _Allocator>
3561typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003562basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003563 size_type __pos,
3564 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003565{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003566 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003567 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003568 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003569}
3570
3571template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003572inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003573typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003574basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3575 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003576{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003577 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003578 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003579}
3580
3581template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003582template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003583__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003584<
3585 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3586 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003587>
Marshall Clowe46031a2018-07-02 18:41:15 +00003588basic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003589 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003590{
Marshall Clowe46031a2018-07-02 18:41:15 +00003591 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003592 return __str_find_first_of<value_type, size_type, traits_type, npos>
3593 (data(), size(), __sv.data(), __pos, __sv.size());
3594}
3595
3596template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003597inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003598typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003599basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003600 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003601{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003602 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003603 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003604 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003605}
3606
3607template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003608inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003609typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003610basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3611 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003612{
3613 return find(__c, __pos);
3614}
3615
3616// find_last_of
3617
3618template<class _CharT, class _Traits, class _Allocator>
3619typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003620basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003621 size_type __pos,
3622 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003623{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003624 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003625 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003626 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003627}
3628
3629template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003630inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003631typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003632basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3633 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003634{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003635 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003636 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003637}
3638
3639template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003640template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003641__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003642<
3643 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3644 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003645>
Marshall Clowe46031a2018-07-02 18:41:15 +00003646basic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003647 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003648{
Marshall Clowe46031a2018-07-02 18:41:15 +00003649 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003650 return __str_find_last_of<value_type, size_type, traits_type, npos>
3651 (data(), size(), __sv.data(), __pos, __sv.size());
3652}
3653
3654template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003655inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003656typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003657basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003658 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003659{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003660 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003661 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003662 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003663}
3664
3665template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003666inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003667typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003668basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3669 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003670{
3671 return rfind(__c, __pos);
3672}
3673
3674// find_first_not_of
3675
3676template<class _CharT, class _Traits, class _Allocator>
3677typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003678basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003679 size_type __pos,
3680 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003681{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003682 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003683 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003684 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003685}
3686
3687template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003688inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003689typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003690basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3691 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003692{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003693 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003694 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003695}
3696
3697template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003698template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003699__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003700<
3701 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3702 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003703>
Marshall Clowe46031a2018-07-02 18:41:15 +00003704basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003705 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003706{
Marshall Clowe46031a2018-07-02 18:41:15 +00003707 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003708 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3709 (data(), size(), __sv.data(), __pos, __sv.size());
3710}
3711
3712template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003713inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003714typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003715basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003716 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003717{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003718 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003719 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003720 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003721}
3722
3723template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003724inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003725typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003726basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3727 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003728{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003729 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003730 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003731}
3732
3733// find_last_not_of
3734
3735template<class _CharT, class _Traits, class _Allocator>
3736typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003737basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003738 size_type __pos,
3739 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003740{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003741 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003742 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003743 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003744}
3745
3746template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003747inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003748typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003749basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3750 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003751{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003752 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003753 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003754}
3755
3756template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003757template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003758__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003759<
3760 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3761 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003762>
Marshall Clowe46031a2018-07-02 18:41:15 +00003763basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003764 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003765{
Marshall Clowe46031a2018-07-02 18:41:15 +00003766 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003767 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3768 (data(), size(), __sv.data(), __pos, __sv.size());
3769}
3770
3771template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003772inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003773typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003774basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003775 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003776{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003777 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003778 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003779 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003780}
3781
3782template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003783inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003784typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003785basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3786 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003787{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003788 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003789 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003790}
3791
3792// compare
3793
3794template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003795template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003796__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003797<
3798 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3799 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003800>
zoecarver1997e0a2021-02-05 11:54:47 -08003801basic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003802{
Marshall Clowe46031a2018-07-02 18:41:15 +00003803 __self_view __sv = __t;
Howard Hinnantb0485532011-07-24 21:45:06 +00003804 size_t __lhs_sz = size();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003805 size_t __rhs_sz = __sv.size();
3806 int __result = traits_type::compare(data(), __sv.data(),
Howard Hinnantb0485532011-07-24 21:45:06 +00003807 _VSTD::min(__lhs_sz, __rhs_sz));
3808 if (__result != 0)
3809 return __result;
3810 if (__lhs_sz < __rhs_sz)
3811 return -1;
3812 if (__lhs_sz > __rhs_sz)
3813 return 1;
3814 return 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003815}
3816
3817template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003818inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003819int
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003820basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003821{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003822 return compare(__self_view(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003823}
3824
3825template <class _CharT, class _Traits, class _Allocator>
3826int
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003827basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3828 size_type __n1,
Howard Hinnantd17880b2013-06-28 16:59:19 +00003829 const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003830 size_type __n2) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003831{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003832 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003833 size_type __sz = size();
3834 if (__pos1 > __sz || __n2 == npos)
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003835 __throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003836 size_type __rlen = _VSTD::min(__n1, __sz - __pos1);
3837 int __r = traits_type::compare(data() + __pos1, __s, _VSTD::min(__rlen, __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003838 if (__r == 0)
3839 {
3840 if (__rlen < __n2)
3841 __r = -1;
3842 else if (__rlen > __n2)
3843 __r = 1;
3844 }
3845 return __r;
3846}
3847
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003848template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003849template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003850__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003851<
3852 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3853 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003854>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003855basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3856 size_type __n1,
Marshall Clowe46031a2018-07-02 18:41:15 +00003857 const _Tp& __t) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003858{
Marshall Clowe46031a2018-07-02 18:41:15 +00003859 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003860 return compare(__pos1, __n1, __sv.data(), __sv.size());
3861}
3862
3863template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003864inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003865int
3866basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3867 size_type __n1,
3868 const basic_string& __str) const
3869{
3870 return compare(__pos1, __n1, __str.data(), __str.size());
3871}
3872
3873template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003874template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003875__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003876<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003877 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
3878 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00003879 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003880>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003881basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3882 size_type __n1,
Marshall Clow82513342016-09-24 22:45:42 +00003883 const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003884 size_type __pos2,
3885 size_type __n2) const
3886{
Marshall Clow82513342016-09-24 22:45:42 +00003887 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003888 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3889}
3890
3891template <class _CharT, class _Traits, class _Allocator>
3892int
3893basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3894 size_type __n1,
3895 const basic_string& __str,
3896 size_type __pos2,
3897 size_type __n2) const
3898{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01003899 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003900}
3901
3902template <class _CharT, class _Traits, class _Allocator>
3903int
3904basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
3905{
3906 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3907 return compare(0, npos, __s, traits_type::length(__s));
3908}
3909
3910template <class _CharT, class _Traits, class _Allocator>
3911int
3912basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3913 size_type __n1,
3914 const value_type* __s) const
3915{
3916 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3917 return compare(__pos1, __n1, __s, traits_type::length(__s));
3918}
3919
Howard Hinnantc51e1022010-05-11 19:42:16 +00003920// __invariants
3921
3922template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003923inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003924bool
3925basic_string<_CharT, _Traits, _Allocator>::__invariants() const
3926{
3927 if (size() > capacity())
3928 return false;
3929 if (capacity() < __min_cap - 1)
3930 return false;
Bruce Mitchener170d8972020-11-24 12:53:53 -05003931 if (data() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003932 return false;
Louis Dionne663415f2020-10-05 16:16:13 -04003933 if (data()[size()] != value_type())
Howard Hinnantc51e1022010-05-11 19:42:16 +00003934 return false;
3935 return true;
3936}
3937
Vedant Kumar55e007e2018-03-08 21:15:26 +00003938// __clear_and_shrink
3939
3940template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003941inline
Louis Dionne173f29e2019-05-29 16:01:36 +00003942void
Marshall Clowe60a7182018-05-29 17:04:37 +00003943basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
Vedant Kumar55e007e2018-03-08 21:15:26 +00003944{
3945 clear();
3946 if(__is_long())
3947 {
3948 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
3949 __set_long_cap(0);
3950 __set_short_size(0);
Louis Dionne663415f2020-10-05 16:16:13 -04003951 traits_type::assign(*__get_short_pointer(), value_type());
Vedant Kumar55e007e2018-03-08 21:15:26 +00003952 }
Louis Dionne173f29e2019-05-29 16:01:36 +00003953}
Vedant Kumar55e007e2018-03-08 21:15:26 +00003954
Howard Hinnantc51e1022010-05-11 19:42:16 +00003955// operator==
3956
3957template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003958inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003959bool
3960operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003961 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003962{
Howard Hinnantaaeb1132013-04-22 23:55:13 +00003963 size_t __lhs_sz = __lhs.size();
3964 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
3965 __rhs.data(),
3966 __lhs_sz) == 0;
3967}
3968
3969template<class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003970inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaaeb1132013-04-22 23:55:13 +00003971bool
3972operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
3973 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
3974{
3975 size_t __lhs_sz = __lhs.size();
3976 if (__lhs_sz != __rhs.size())
3977 return false;
3978 const char* __lp = __lhs.data();
3979 const char* __rp = __rhs.data();
3980 if (__lhs.__is_long())
3981 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
3982 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
3983 if (*__lp != *__rp)
3984 return false;
3985 return true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003986}
3987
3988template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003989inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003990bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003991operator==(const _CharT* __lhs,
3992 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003993{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00003994 typedef basic_string<_CharT, _Traits, _Allocator> _String;
3995 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
3996 size_t __lhs_len = _Traits::length(__lhs);
3997 if (__lhs_len != __rhs.size()) return false;
3998 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003999}
4000
Howard Hinnantc51e1022010-05-11 19:42:16 +00004001template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004002inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004003bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004004operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
4005 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004006{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004007 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4008 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
4009 size_t __rhs_len = _Traits::length(__rhs);
4010 if (__rhs_len != __lhs.size()) return false;
4011 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004012}
4013
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004014template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004015inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004016bool
4017operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004018 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004019{
4020 return !(__lhs == __rhs);
4021}
4022
4023template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004024inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004025bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004026operator!=(const _CharT* __lhs,
4027 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004028{
4029 return !(__lhs == __rhs);
4030}
4031
4032template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004033inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004034bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004035operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4036 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004037{
4038 return !(__lhs == __rhs);
4039}
4040
4041// operator<
4042
4043template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004044inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004045bool
4046operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004047 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004048{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004049 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004050}
4051
4052template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004053inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004054bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004055operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4056 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004057{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004058 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004059}
4060
4061template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004062inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004063bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004064operator< (const _CharT* __lhs,
4065 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004066{
4067 return __rhs.compare(__lhs) > 0;
4068}
4069
Howard Hinnantc51e1022010-05-11 19:42:16 +00004070// operator>
4071
4072template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004073inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004074bool
4075operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004076 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004077{
4078 return __rhs < __lhs;
4079}
4080
4081template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004082inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004083bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004084operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4085 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004086{
4087 return __rhs < __lhs;
4088}
4089
4090template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004091inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004092bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004093operator> (const _CharT* __lhs,
4094 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004095{
4096 return __rhs < __lhs;
4097}
4098
4099// operator<=
4100
4101template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004102inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004103bool
4104operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004105 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004106{
4107 return !(__rhs < __lhs);
4108}
4109
4110template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004111inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004112bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004113operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4114 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004115{
4116 return !(__rhs < __lhs);
4117}
4118
4119template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004120inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004121bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004122operator<=(const _CharT* __lhs,
4123 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004124{
4125 return !(__rhs < __lhs);
4126}
4127
4128// operator>=
4129
4130template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004131inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004132bool
4133operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004134 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004135{
4136 return !(__lhs < __rhs);
4137}
4138
4139template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004140inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004141bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004142operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4143 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004144{
4145 return !(__lhs < __rhs);
4146}
4147
4148template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004149inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004150bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004151operator>=(const _CharT* __lhs,
4152 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004153{
4154 return !(__lhs < __rhs);
4155}
4156
4157// operator +
4158
4159template<class _CharT, class _Traits, class _Allocator>
4160basic_string<_CharT, _Traits, _Allocator>
4161operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4162 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4163{
4164 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4165 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4166 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4167 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4168 __r.append(__rhs.data(), __rhs_sz);
4169 return __r;
4170}
4171
4172template<class _CharT, class _Traits, class _Allocator>
4173basic_string<_CharT, _Traits, _Allocator>
4174operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4175{
4176 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4177 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = _Traits::length(__lhs);
4178 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4179 __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz);
4180 __r.append(__rhs.data(), __rhs_sz);
4181 return __r;
4182}
4183
4184template<class _CharT, class _Traits, class _Allocator>
4185basic_string<_CharT, _Traits, _Allocator>
4186operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4187{
4188 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4189 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4190 __r.__init(&__lhs, 1, 1 + __rhs_sz);
4191 __r.append(__rhs.data(), __rhs_sz);
4192 return __r;
4193}
4194
4195template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004196inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004197basic_string<_CharT, _Traits, _Allocator>
4198operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
4199{
4200 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4201 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4202 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = _Traits::length(__rhs);
4203 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4204 __r.append(__rhs, __rhs_sz);
4205 return __r;
4206}
4207
4208template<class _CharT, class _Traits, class _Allocator>
4209basic_string<_CharT, _Traits, _Allocator>
4210operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
4211{
4212 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4213 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4214 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1);
4215 __r.push_back(__rhs);
4216 return __r;
4217}
4218
Eric Fiselierfc92be82017-04-19 00:28:44 +00004219#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004220
4221template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004222inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004223basic_string<_CharT, _Traits, _Allocator>
4224operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4225{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004226 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004227}
4228
4229template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004230inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004231basic_string<_CharT, _Traits, _Allocator>
4232operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4233{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004234 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004235}
4236
4237template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004238inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004239basic_string<_CharT, _Traits, _Allocator>
4240operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4241{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004242 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004243}
4244
4245template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004246inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004247basic_string<_CharT, _Traits, _Allocator>
4248operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4249{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004250 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004251}
4252
4253template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004254inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004255basic_string<_CharT, _Traits, _Allocator>
4256operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4257{
4258 __rhs.insert(__rhs.begin(), __lhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004259 return _VSTD::move(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004260}
4261
4262template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004263inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004264basic_string<_CharT, _Traits, _Allocator>
4265operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
4266{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004267 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004268}
4269
4270template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004271inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004272basic_string<_CharT, _Traits, _Allocator>
4273operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
4274{
4275 __lhs.push_back(__rhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004276 return _VSTD::move(__lhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004277}
4278
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004279#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004280
4281// swap
4282
4283template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004284inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004285void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004286swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +00004287 basic_string<_CharT, _Traits, _Allocator>& __rhs)
4288 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004289{
4290 __lhs.swap(__rhs);
4291}
4292
Bruce Mitchener170d8972020-11-24 12:53:53 -05004293_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = nullptr, int __base = 10);
4294_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = nullptr, int __base = 10);
4295_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = nullptr, int __base = 10);
4296_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = nullptr, int __base = 10);
4297_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004298
Bruce Mitchener170d8972020-11-24 12:53:53 -05004299_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = nullptr);
4300_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = nullptr);
4301_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004302
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004303_LIBCPP_FUNC_VIS string to_string(int __val);
4304_LIBCPP_FUNC_VIS string to_string(unsigned __val);
4305_LIBCPP_FUNC_VIS string to_string(long __val);
4306_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
4307_LIBCPP_FUNC_VIS string to_string(long long __val);
4308_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
4309_LIBCPP_FUNC_VIS string to_string(float __val);
4310_LIBCPP_FUNC_VIS string to_string(double __val);
4311_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004312
Louis Dionne89258142021-08-23 15:32:36 -04004313#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Bruce Mitchener170d8972020-11-24 12:53:53 -05004314_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4315_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4316_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4317_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4318_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004319
Bruce Mitchener170d8972020-11-24 12:53:53 -05004320_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = nullptr);
4321_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = nullptr);
4322_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004323
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004324_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
4325_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
4326_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
4327_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
4328_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
4329_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
4330_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
4331_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
4332_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Louis Dionne89258142021-08-23 15:32:36 -04004333#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004334
Howard Hinnantc51e1022010-05-11 19:42:16 +00004335template<class _CharT, class _Traits, class _Allocator>
Martin Storsjö88890c22021-02-22 01:13:13 +02004336_LIBCPP_TEMPLATE_DATA_VIS
Eric Fiselierc9fdaf12020-01-15 17:02:17 -05004337const typename basic_string<_CharT, _Traits, _Allocator>::size_type
4338 basic_string<_CharT, _Traits, _Allocator>::npos;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004339
Marshall Clow851b9ec2019-05-20 21:56:51 +00004340template <class _CharT, class _Allocator>
4341struct _LIBCPP_TEMPLATE_VIS
4342 hash<basic_string<_CharT, char_traits<_CharT>, _Allocator> >
4343 : public unary_function<
4344 basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004345{
4346 size_t
Marshall Clow851b9ec2019-05-20 21:56:51 +00004347 operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT
4348 { return __do_string_hash(__val.data(), __val.data() + __val.size()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004349};
4350
Howard Hinnantc51e1022010-05-11 19:42:16 +00004351
Howard Hinnantdc095972011-07-18 15:51:59 +00004352template<class _CharT, class _Traits, class _Allocator>
4353basic_ostream<_CharT, _Traits>&
4354operator<<(basic_ostream<_CharT, _Traits>& __os,
4355 const basic_string<_CharT, _Traits, _Allocator>& __str);
4356
4357template<class _CharT, class _Traits, class _Allocator>
4358basic_istream<_CharT, _Traits>&
4359operator>>(basic_istream<_CharT, _Traits>& __is,
4360 basic_string<_CharT, _Traits, _Allocator>& __str);
4361
4362template<class _CharT, class _Traits, class _Allocator>
4363basic_istream<_CharT, _Traits>&
4364getline(basic_istream<_CharT, _Traits>& __is,
4365 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4366
4367template<class _CharT, class _Traits, class _Allocator>
4368inline _LIBCPP_INLINE_VISIBILITY
4369basic_istream<_CharT, _Traits>&
4370getline(basic_istream<_CharT, _Traits>& __is,
4371 basic_string<_CharT, _Traits, _Allocator>& __str);
4372
Howard Hinnantdc095972011-07-18 15:51:59 +00004373template<class _CharT, class _Traits, class _Allocator>
4374inline _LIBCPP_INLINE_VISIBILITY
4375basic_istream<_CharT, _Traits>&
4376getline(basic_istream<_CharT, _Traits>&& __is,
4377 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4378
4379template<class _CharT, class _Traits, class _Allocator>
4380inline _LIBCPP_INLINE_VISIBILITY
4381basic_istream<_CharT, _Traits>&
4382getline(basic_istream<_CharT, _Traits>&& __is,
4383 basic_string<_CharT, _Traits, _Allocator>& __str);
4384
Marshall Clow29b53f22018-12-14 18:49:35 +00004385#if _LIBCPP_STD_VER > 17
Marek Kurdeja98b1412020-05-02 13:58:03 +02004386template <class _CharT, class _Traits, class _Allocator, class _Up>
Marshall Clow29b53f22018-12-14 18:49:35 +00004387inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004388 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4389 erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
4390 auto __old_size = __str.size();
4391 __str.erase(_VSTD::remove(__str.begin(), __str.end(), __v), __str.end());
4392 return __old_size - __str.size();
4393}
Marshall Clow29b53f22018-12-14 18:49:35 +00004394
Marek Kurdeja98b1412020-05-02 13:58:03 +02004395template <class _CharT, class _Traits, class _Allocator, class _Predicate>
Marshall Clow29b53f22018-12-14 18:49:35 +00004396inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004397 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4398 erase_if(basic_string<_CharT, _Traits, _Allocator>& __str,
4399 _Predicate __pred) {
4400 auto __old_size = __str.size();
4401 __str.erase(_VSTD::remove_if(__str.begin(), __str.end(), __pred),
4402 __str.end());
4403 return __old_size - __str.size();
4404}
Marshall Clow29b53f22018-12-14 18:49:35 +00004405#endif
4406
Louis Dionneba400782020-10-02 15:02:52 -04004407#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004408
4409template<class _CharT, class _Traits, class _Allocator>
4410bool
4411basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4412{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004413 return data() <= _VSTD::__to_address(__i->base()) &&
4414 _VSTD::__to_address(__i->base()) < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004415}
4416
4417template<class _CharT, class _Traits, class _Allocator>
4418bool
4419basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4420{
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004421 return data() < _VSTD::__to_address(__i->base()) &&
4422 _VSTD::__to_address(__i->base()) <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004423}
4424
4425template<class _CharT, class _Traits, class _Allocator>
4426bool
4427basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4428{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004429 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004430 return data() <= __p && __p <= data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004431}
4432
4433template<class _CharT, class _Traits, class _Allocator>
4434bool
4435basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4436{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004437 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Nikolas Klauser00ba7f82021-12-28 13:09:40 +01004438 return data() <= __p && __p < data() + size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004439}
4440
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004441#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004442
Louis Dionne173f29e2019-05-29 16:01:36 +00004443#if _LIBCPP_STD_VER > 11
Marshall Clowcba751f2013-07-23 17:05:24 +00004444// Literal suffixes for basic_string [basic.string.literals]
Marshall Clowac868372013-10-05 21:18:32 +00004445inline namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004446{
4447 inline namespace string_literals
4448 {
Howard Hinnant5c167562013-08-07 19:39:48 +00004449 inline _LIBCPP_INLINE_VISIBILITY
4450 basic_string<char> operator "" s( const char *__str, size_t __len )
4451 {
4452 return basic_string<char> (__str, __len);
4453 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004454
Louis Dionne89258142021-08-23 15:32:36 -04004455#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnant5c167562013-08-07 19:39:48 +00004456 inline _LIBCPP_INLINE_VISIBILITY
4457 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4458 {
4459 return basic_string<wchar_t> (__str, __len);
4460 }
Louis Dionne89258142021-08-23 15:32:36 -04004461#endif
Marshall Clowcba751f2013-07-23 17:05:24 +00004462
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -04004463#ifndef _LIBCPP_HAS_NO_CHAR8_T
Marshall Clow8732fed2018-12-11 04:35:44 +00004464 inline _LIBCPP_INLINE_VISIBILITY
4465 basic_string<char8_t> operator "" s(const char8_t *__str, size_t __len) _NOEXCEPT
4466 {
4467 return basic_string<char8_t> (__str, __len);
4468 }
4469#endif
4470
Howard Hinnant5c167562013-08-07 19:39:48 +00004471 inline _LIBCPP_INLINE_VISIBILITY
4472 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4473 {
4474 return basic_string<char16_t> (__str, __len);
4475 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004476
Howard Hinnant5c167562013-08-07 19:39:48 +00004477 inline _LIBCPP_INLINE_VISIBILITY
4478 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4479 {
4480 return basic_string<char32_t> (__str, __len);
4481 }
Nikolas Klauserd26407a2021-12-02 14:12:51 +01004482 } // namespace string_literals
4483} // namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004484#endif
4485
Howard Hinnantc51e1022010-05-11 19:42:16 +00004486_LIBCPP_END_NAMESPACE_STD
4487
Eric Fiselierf4433a32017-05-31 22:07:49 +00004488_LIBCPP_POP_MACROS
4489
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004490#endif // _LIBCPP_STRING