blob: c4f2b008cafe1b6deffcfdce05f2d19efccdcc1e [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
Louis Dionne9bd93882021-11-17 16:25:01 -05002//===----------------------------------------------------------------------===//
Howard Hinnantc51e1022010-05-11 19:42:16 +00003//
Chandler Carruth7642bb12019-01-19 08:50:56 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_STRING
11#define _LIBCPP_STRING
12
13/*
14 string synopsis
15
16namespace std
17{
18
19template <class stateT>
20class fpos
21{
22private:
23 stateT st;
24public:
25 fpos(streamoff = streamoff());
26
27 operator streamoff() const;
28
29 stateT state() const;
30 void state(stateT);
31
32 fpos& operator+=(streamoff);
33 fpos operator+ (streamoff) const;
34 fpos& operator-=(streamoff);
35 fpos operator- (streamoff) const;
36};
37
38template <class stateT> streamoff operator-(const fpos<stateT>& x, const fpos<stateT>& y);
39
40template <class stateT> bool operator==(const fpos<stateT>& x, const fpos<stateT>& y);
41template <class stateT> bool operator!=(const fpos<stateT>& x, const fpos<stateT>& y);
42
43template <class charT>
44struct char_traits
45{
46 typedef charT char_type;
47 typedef ... int_type;
48 typedef streamoff off_type;
49 typedef streampos pos_type;
50 typedef mbstate_t state_type;
51
Howard Hinnantaeb17d82011-05-29 19:57:12 +000052 static void assign(char_type& c1, const char_type& c2) noexcept;
Howard Hinnant270c00e2012-07-20 19:09:12 +000053 static constexpr bool eq(char_type c1, char_type c2) noexcept;
54 static constexpr bool lt(char_type c1, char_type c2) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000055
56 static int compare(const char_type* s1, const char_type* s2, size_t n);
57 static size_t length(const char_type* s);
58 static const char_type* find(const char_type* s, size_t n, const char_type& a);
59 static char_type* move(char_type* s1, const char_type* s2, size_t n);
60 static char_type* copy(char_type* s1, const char_type* s2, size_t n);
61 static char_type* assign(char_type* s, size_t n, char_type a);
62
Howard Hinnant270c00e2012-07-20 19:09:12 +000063 static constexpr int_type not_eof(int_type c) noexcept;
64 static constexpr char_type to_char_type(int_type c) noexcept;
65 static constexpr int_type to_int_type(char_type c) noexcept;
66 static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept;
67 static constexpr int_type eof() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000068};
69
70template <> struct char_traits<char>;
71template <> struct char_traits<wchar_t>;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +010072template <> struct char_traits<char8_t>; // C++20
73template <> struct char_traits<char16_t>;
74template <> struct char_traits<char32_t>;
Howard Hinnantc51e1022010-05-11 19:42:16 +000075
Howard Hinnant3b6579a2010-08-22 00:02:43 +000076template<class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> >
Howard Hinnantc51e1022010-05-11 19:42:16 +000077class basic_string
78{
Howard Hinnant3b6579a2010-08-22 00:02:43 +000079public:
80// types:
Howard Hinnantc51e1022010-05-11 19:42:16 +000081 typedef traits traits_type;
82 typedef typename traits_type::char_type value_type;
83 typedef Allocator allocator_type;
84 typedef typename allocator_type::size_type size_type;
85 typedef typename allocator_type::difference_type difference_type;
86 typedef typename allocator_type::reference reference;
87 typedef typename allocator_type::const_reference const_reference;
88 typedef typename allocator_type::pointer pointer;
89 typedef typename allocator_type::const_pointer const_pointer;
90 typedef implementation-defined iterator;
91 typedef implementation-defined const_iterator;
92 typedef std::reverse_iterator<iterator> reverse_iterator;
93 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
94
95 static const size_type npos = -1;
96
Howard Hinnant3e276872011-06-03 18:40:47 +000097 basic_string()
98 noexcept(is_nothrow_default_constructible<allocator_type>::value);
99 explicit basic_string(const allocator_type& a);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000100 basic_string(const basic_string& str);
Howard Hinnant3e276872011-06-03 18:40:47 +0000101 basic_string(basic_string&& str)
102 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clow83445802016-04-07 18:13:41 +0000103 basic_string(const basic_string& str, size_type pos,
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000104 const allocator_type& a = allocator_type());
Marshall Clow83445802016-04-07 18:13:41 +0000105 basic_string(const basic_string& str, size_type pos, size_type n,
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000106 const Allocator& a = Allocator());
Marshall Clow78dbe462016-11-14 18:22:19 +0000107 template<class T>
108 basic_string(const T& t, size_type pos, size_type n, const Allocator& a = Allocator()); // C++17
Marshall Clowe46031a2018-07-02 18:41:15 +0000109 template <class T>
110 explicit basic_string(const T& t, const Allocator& a = Allocator()); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000111 basic_string(const value_type* s, const allocator_type& a = allocator_type());
112 basic_string(const value_type* s, size_type n, const allocator_type& a = allocator_type());
Marek Kurdej90d79712021-07-27 16:16:21 +0200113 basic_string(nullptr_t) = delete; // C++2b
Howard Hinnantc51e1022010-05-11 19:42:16 +0000114 basic_string(size_type n, value_type c, const allocator_type& a = allocator_type());
115 template<class InputIterator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000116 basic_string(InputIterator begin, InputIterator end,
117 const allocator_type& a = allocator_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000118 basic_string(initializer_list<value_type>, const Allocator& = Allocator());
119 basic_string(const basic_string&, const Allocator&);
120 basic_string(basic_string&&, const Allocator&);
121
122 ~basic_string();
123
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000124 operator basic_string_view<charT, traits>() const noexcept;
125
Howard Hinnantc51e1022010-05-11 19:42:16 +0000126 basic_string& operator=(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000127 template <class T>
128 basic_string& operator=(const T& t); // C++17
Howard Hinnant3e276872011-06-03 18:40:47 +0000129 basic_string& operator=(basic_string&& str)
130 noexcept(
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000131 allocator_type::propagate_on_container_move_assignment::value ||
132 allocator_type::is_always_equal::value ); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000133 basic_string& operator=(const value_type* s);
Marek Kurdej90d79712021-07-27 16:16:21 +0200134 basic_string& operator=(nullptr_t) = delete; // C++2b
Howard Hinnantc51e1022010-05-11 19:42:16 +0000135 basic_string& operator=(value_type c);
136 basic_string& operator=(initializer_list<value_type>);
137
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000138 iterator begin() noexcept;
139 const_iterator begin() const noexcept;
140 iterator end() noexcept;
141 const_iterator end() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000142
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000143 reverse_iterator rbegin() noexcept;
144 const_reverse_iterator rbegin() const noexcept;
145 reverse_iterator rend() noexcept;
146 const_reverse_iterator rend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000147
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000148 const_iterator cbegin() const noexcept;
149 const_iterator cend() const noexcept;
150 const_reverse_iterator crbegin() const noexcept;
151 const_reverse_iterator crend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000152
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000153 size_type size() const noexcept;
154 size_type length() const noexcept;
155 size_type max_size() const noexcept;
156 size_type capacity() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000157
158 void resize(size_type n, value_type c);
159 void resize(size_type n);
160
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100161 template<class Operation>
162 constexpr void resize_and_overwrite(size_type n, Operation op); // since C++23
163
Marek Kurdejc9848142020-11-26 10:07:16 +0100164 void reserve(size_type res_arg);
165 void reserve(); // deprecated in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000166 void shrink_to_fit();
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000167 void clear() noexcept;
168 bool empty() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000169
170 const_reference operator[](size_type pos) const;
171 reference operator[](size_type pos);
172
173 const_reference at(size_type n) const;
174 reference at(size_type n);
175
176 basic_string& operator+=(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000177 template <class T>
178 basic_string& operator+=(const T& t); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000179 basic_string& operator+=(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000180 basic_string& operator+=(value_type c);
181 basic_string& operator+=(initializer_list<value_type>);
182
183 basic_string& append(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000184 template <class T>
185 basic_string& append(const T& t); // C++17
Marshall Clow8db7fb02014-03-04 19:17:19 +0000186 basic_string& append(const basic_string& str, size_type pos, size_type n=npos); //C++14
Marshall Clow82513342016-09-24 22:45:42 +0000187 template <class T>
188 basic_string& append(const T& t, size_type pos, size_type n=npos); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000189 basic_string& append(const value_type* s, size_type n);
190 basic_string& append(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000191 basic_string& append(size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000192 template<class InputIterator>
193 basic_string& append(InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000194 basic_string& append(initializer_list<value_type>);
195
196 void push_back(value_type c);
197 void pop_back();
198 reference front();
199 const_reference front() const;
200 reference back();
201 const_reference back() const;
202
203 basic_string& assign(const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000204 template <class T>
205 basic_string& assign(const T& t); // C++17
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000206 basic_string& assign(basic_string&& str);
Marshall Clow8db7fb02014-03-04 19:17:19 +0000207 basic_string& assign(const basic_string& str, size_type pos, size_type n=npos); // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000208 template <class T>
209 basic_string& assign(const T& t, size_type pos, size_type n=npos); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000210 basic_string& assign(const value_type* s, size_type n);
211 basic_string& assign(const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000212 basic_string& assign(size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000213 template<class InputIterator>
214 basic_string& assign(InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000215 basic_string& assign(initializer_list<value_type>);
216
217 basic_string& insert(size_type pos1, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000218 template <class T>
219 basic_string& insert(size_type pos1, const T& t);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000220 basic_string& insert(size_type pos1, const basic_string& str,
221 size_type pos2, size_type n);
Marshall Clow82513342016-09-24 22:45:42 +0000222 template <class T>
223 basic_string& insert(size_type pos1, const T& t, size_type pos2, size_type n); // C++17
Marshall Clow8db7fb02014-03-04 19:17:19 +0000224 basic_string& insert(size_type pos, const value_type* s, size_type n=npos); //C++14
Howard Hinnantd17880b2013-06-28 16:59:19 +0000225 basic_string& insert(size_type pos, const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000226 basic_string& insert(size_type pos, size_type n, value_type c);
227 iterator insert(const_iterator p, value_type c);
228 iterator insert(const_iterator p, size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000229 template<class InputIterator>
230 iterator insert(const_iterator p, InputIterator first, InputIterator last);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000231 iterator insert(const_iterator p, initializer_list<value_type>);
232
233 basic_string& erase(size_type pos = 0, size_type n = npos);
234 iterator erase(const_iterator position);
235 iterator erase(const_iterator first, const_iterator last);
236
237 basic_string& replace(size_type pos1, size_type n1, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000238 template <class T>
239 basic_string& replace(size_type pos1, size_type n1, const T& t); // C++17
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000240 basic_string& replace(size_type pos1, size_type n1, const basic_string& str,
Marshall Clow8db7fb02014-03-04 19:17:19 +0000241 size_type pos2, size_type n2=npos); // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000242 template <class T>
243 basic_string& replace(size_type pos1, size_type n1, const T& t,
244 size_type pos2, size_type n); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000245 basic_string& replace(size_type pos, size_type n1, const value_type* s, size_type n2);
246 basic_string& replace(size_type pos, size_type n1, const value_type* s);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000247 basic_string& replace(size_type pos, size_type n1, size_type n2, value_type c);
Howard Hinnant990d6e82010-11-17 21:11:40 +0000248 basic_string& replace(const_iterator i1, const_iterator i2, const basic_string& str);
Marshall Clowe46031a2018-07-02 18:41:15 +0000249 template <class T>
250 basic_string& replace(const_iterator i1, const_iterator i2, const T& t); // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000251 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s, size_type n);
252 basic_string& replace(const_iterator i1, const_iterator i2, const value_type* s);
Howard Hinnant990d6e82010-11-17 21:11:40 +0000253 basic_string& replace(const_iterator i1, const_iterator i2, size_type n, value_type c);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000254 template<class InputIterator>
Howard Hinnant990d6e82010-11-17 21:11:40 +0000255 basic_string& replace(const_iterator i1, const_iterator i2, InputIterator j1, InputIterator j2);
256 basic_string& replace(const_iterator i1, const_iterator i2, initializer_list<value_type>);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000257
Howard Hinnantd17880b2013-06-28 16:59:19 +0000258 size_type copy(value_type* s, size_type n, size_type pos = 0) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000259 basic_string substr(size_type pos = 0, size_type n = npos) const;
260
Howard Hinnant3e276872011-06-03 18:40:47 +0000261 void swap(basic_string& str)
Marshall Clow8982dcd2015-07-13 20:04:56 +0000262 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value ||
263 allocator_traits<allocator_type>::is_always_equal::value); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000264
Howard Hinnantd17880b2013-06-28 16:59:19 +0000265 const value_type* c_str() const noexcept;
266 const value_type* data() const noexcept;
Marshall Clowd3c22392016-03-08 15:44:30 +0000267 value_type* data() noexcept; // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000268
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000269 allocator_type get_allocator() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000270
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000271 size_type find(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000272 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800273 size_type find(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000274 size_type find(const value_type* s, size_type pos, size_type n) const noexcept;
275 size_type find(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000276 size_type find(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000277
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000278 size_type rfind(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000279 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800280 size_type rfind(const T& t, size_type pos = npos) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000281 size_type rfind(const value_type* s, size_type pos, size_type n) const noexcept;
282 size_type rfind(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000283 size_type rfind(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000284
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000285 size_type find_first_of(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000286 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800287 size_type find_first_of(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000288 size_type find_first_of(const value_type* s, size_type pos, size_type n) const noexcept;
289 size_type find_first_of(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000290 size_type find_first_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000291
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000292 size_type find_last_of(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000293 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800294 size_type find_last_of(const T& t, size_type pos = npos) const noexcept noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000295 size_type find_last_of(const value_type* s, size_type pos, size_type n) const noexcept;
296 size_type find_last_of(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000297 size_type find_last_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000298
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000299 size_type find_first_not_of(const basic_string& str, size_type pos = 0) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000300 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800301 size_type find_first_not_of(const T& t, size_type pos = 0) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000302 size_type find_first_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
303 size_type find_first_not_of(const value_type* s, size_type pos = 0) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000304 size_type find_first_not_of(value_type c, size_type pos = 0) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000305
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000306 size_type find_last_not_of(const basic_string& str, size_type pos = npos) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000307 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800308 size_type find_last_not_of(const T& t, size_type pos = npos) const noexcept; // C++17, noexcept as an extension
Howard Hinnantd17880b2013-06-28 16:59:19 +0000309 size_type find_last_not_of(const value_type* s, size_type pos, size_type n) const noexcept;
310 size_type find_last_not_of(const value_type* s, size_type pos = npos) const noexcept;
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000311 size_type find_last_not_of(value_type c, size_type pos = npos) const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000312
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000313 int compare(const basic_string& str) const noexcept;
Marshall Clowe46031a2018-07-02 18:41:15 +0000314 template <class T>
zoecarver1997e0a2021-02-05 11:54:47 -0800315 int compare(const T& t) const noexcept; // C++17, noexcept as an extension
Howard Hinnantc51e1022010-05-11 19:42:16 +0000316 int compare(size_type pos1, size_type n1, const basic_string& str) const;
Marshall Clowe46031a2018-07-02 18:41:15 +0000317 template <class T>
318 int compare(size_type pos1, size_type n1, const T& t) const; // C++17
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000319 int compare(size_type pos1, size_type n1, const basic_string& str,
Marshall Clow8db7fb02014-03-04 19:17:19 +0000320 size_type pos2, size_type n2=npos) const; // C++14
Marshall Clow82513342016-09-24 22:45:42 +0000321 template <class T>
322 int compare(size_type pos1, size_type n1, const T& t,
323 size_type pos2, size_type n2=npos) const; // C++17
Howard Hinnantd17880b2013-06-28 16:59:19 +0000324 int compare(const value_type* s) const noexcept;
325 int compare(size_type pos1, size_type n1, const value_type* s) const;
326 int compare(size_type pos1, size_type n1, const value_type* s, size_type n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000327
Marek Kurdej24b4c512021-01-07 12:29:04 +0100328 bool starts_with(basic_string_view<charT, traits> sv) const noexcept; // C++20
329 bool starts_with(charT c) const noexcept; // C++20
330 bool starts_with(const charT* s) const; // C++20
331 bool ends_with(basic_string_view<charT, traits> sv) const noexcept; // C++20
332 bool ends_with(charT c) const noexcept; // C++20
333 bool ends_with(const charT* s) const; // C++20
Marshall Clow18c293b2017-12-04 20:11:38 +0000334
Wim Leflere023c3542021-01-19 14:33:30 -0500335 constexpr bool contains(basic_string_view<charT, traits> sv) const noexcept; // C++2b
336 constexpr bool contains(charT c) const noexcept; // C++2b
337 constexpr bool contains(const charT* s) const; // C++2b
338
Howard Hinnantc51e1022010-05-11 19:42:16 +0000339 bool __invariants() const;
340};
341
Marshall Clowa0563332018-02-08 06:34:03 +0000342template<class InputIterator,
343 class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
344basic_string(InputIterator, InputIterator, Allocator = Allocator())
345 -> basic_string<typename iterator_traits<InputIterator>::value_type,
346 char_traits<typename iterator_traits<InputIterator>::value_type>,
347 Allocator>; // C++17
348
Howard Hinnantc51e1022010-05-11 19:42:16 +0000349template<class charT, class traits, class Allocator>
350basic_string<charT, traits, Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000351operator+(const basic_string<charT, traits, Allocator>& lhs,
352 const basic_string<charT, traits, Allocator>& rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000353
354template<class charT, class traits, class Allocator>
355basic_string<charT, traits, Allocator>
356operator+(const charT* lhs , const basic_string<charT,traits,Allocator>&rhs);
357
358template<class charT, class traits, class Allocator>
359basic_string<charT, traits, Allocator>
360operator+(charT lhs, const basic_string<charT,traits,Allocator>& rhs);
361
362template<class charT, class traits, class Allocator>
363basic_string<charT, traits, Allocator>
364operator+(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs);
365
366template<class charT, class traits, class Allocator>
367basic_string<charT, traits, Allocator>
368operator+(const basic_string<charT, traits, Allocator>& lhs, charT rhs);
369
370template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000371bool operator==(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000372 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000373
374template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000375bool operator==(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000376
377template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000378bool operator==(const basic_string<charT,traits,Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000379
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000380template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000381bool operator!=(const basic_string<charT,traits,Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000382 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000383
384template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000385bool operator!=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000386
387template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000388bool operator!=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000389
390template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000391bool operator< (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000392 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000393
394template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000395bool operator< (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000396
397template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000398bool operator< (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000399
400template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000401bool operator> (const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000402 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000403
404template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000405bool operator> (const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000406
407template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000408bool operator> (const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000409
410template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000411bool operator<=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000412 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000413
414template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000415bool operator<=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000416
417template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000418bool operator<=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000419
420template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000421bool operator>=(const basic_string<charT, traits, Allocator>& lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000422 const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000423
424template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000425bool operator>=(const basic_string<charT, traits, Allocator>& lhs, const charT* rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000426
427template<class charT, class traits, class Allocator>
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000428bool operator>=(const charT* lhs, const basic_string<charT, traits, Allocator>& rhs) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000429
430template<class charT, class traits, class Allocator>
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000431void swap(basic_string<charT, traits, Allocator>& lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +0000432 basic_string<charT, traits, Allocator>& rhs)
433 noexcept(noexcept(lhs.swap(rhs)));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000434
435template<class charT, class traits, class Allocator>
436basic_istream<charT, traits>&
437operator>>(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
438
439template<class charT, class traits, class Allocator>
440basic_ostream<charT, traits>&
441operator<<(basic_ostream<charT, traits>& os, const basic_string<charT, traits, Allocator>& str);
442
443template<class charT, class traits, class Allocator>
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000444basic_istream<charT, traits>&
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000445getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str,
446 charT delim);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000447
448template<class charT, class traits, class Allocator>
449basic_istream<charT, traits>&
450getline(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
451
Marshall Clow29b53f22018-12-14 18:49:35 +0000452template<class charT, class traits, class Allocator, class U>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200453typename basic_string<charT, traits, Allocator>::size_type
454erase(basic_string<charT, traits, Allocator>& c, const U& value); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000455template<class charT, class traits, class Allocator, class Predicate>
Marek Kurdeja98b1412020-05-02 13:58:03 +0200456typename basic_string<charT, traits, Allocator>::size_type
457erase_if(basic_string<charT, traits, Allocator>& c, Predicate pred); // C++20
Marshall Clow29b53f22018-12-14 18:49:35 +0000458
Howard Hinnantc51e1022010-05-11 19:42:16 +0000459typedef basic_string<char> string;
460typedef basic_string<wchar_t> wstring;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100461typedef basic_string<char8_t> u8string; // C++20
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000462typedef basic_string<char16_t> u16string;
463typedef basic_string<char32_t> u32string;
464
Bruce Mitchener170d8972020-11-24 12:53:53 -0500465int stoi (const string& str, size_t* idx = nullptr, int base = 10);
466long stol (const string& str, size_t* idx = nullptr, int base = 10);
467unsigned long stoul (const string& str, size_t* idx = nullptr, int base = 10);
468long long stoll (const string& str, size_t* idx = nullptr, int base = 10);
469unsigned long long stoull(const string& str, size_t* idx = nullptr, int base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000470
Bruce Mitchener170d8972020-11-24 12:53:53 -0500471float stof (const string& str, size_t* idx = nullptr);
472double stod (const string& str, size_t* idx = nullptr);
473long double stold(const string& str, size_t* idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000474
475string to_string(int val);
476string to_string(unsigned val);
477string to_string(long val);
478string to_string(unsigned long val);
479string to_string(long long val);
480string to_string(unsigned long long val);
481string to_string(float val);
482string to_string(double val);
483string to_string(long double val);
484
Bruce Mitchener170d8972020-11-24 12:53:53 -0500485int stoi (const wstring& str, size_t* idx = nullptr, int base = 10);
486long stol (const wstring& str, size_t* idx = nullptr, int base = 10);
487unsigned long stoul (const wstring& str, size_t* idx = nullptr, int base = 10);
488long long stoll (const wstring& str, size_t* idx = nullptr, int base = 10);
489unsigned long long stoull(const wstring& str, size_t* idx = nullptr, int base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000490
Bruce Mitchener170d8972020-11-24 12:53:53 -0500491float stof (const wstring& str, size_t* idx = nullptr);
492double stod (const wstring& str, size_t* idx = nullptr);
493long double stold(const wstring& str, size_t* idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +0000494
495wstring to_wstring(int val);
496wstring to_wstring(unsigned val);
497wstring to_wstring(long val);
498wstring to_wstring(unsigned long val);
499wstring to_wstring(long long val);
500wstring to_wstring(unsigned long long val);
501wstring to_wstring(float val);
502wstring to_wstring(double val);
503wstring to_wstring(long double val);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000504
505template <> struct hash<string>;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100506template <> struct hash<u8string>; // C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000507template <> struct hash<u16string>;
508template <> struct hash<u32string>;
509template <> struct hash<wstring>;
510
Marshall Clowcba751f2013-07-23 17:05:24 +0000511basic_string<char> operator "" s( const char *str, size_t len ); // C++14
512basic_string<wchar_t> operator "" s( const wchar_t *str, size_t len ); // C++14
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100513basic_string<char8_t> operator "" s( const char8_t *str, size_t len ); // C++20
Marshall Clowcba751f2013-07-23 17:05:24 +0000514basic_string<char16_t> operator "" s( const char16_t *str, size_t len ); // C++14
515basic_string<char32_t> operator "" s( const char32_t *str, size_t len ); // C++14
516
Howard Hinnantc51e1022010-05-11 19:42:16 +0000517} // std
518
519*/
520
521#include <__config>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400522#include <__debug>
523#include <__functional_base>
Louis Dionne77249522021-06-11 09:55:11 -0400524#include <__iterator/wrap_iter.h>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000525#include <algorithm>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400526#include <compare>
527#include <cstdio> // EOF
Louis Dionned24191c2021-08-19 12:39:16 -0400528#include <cstdlib>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400529#include <cstring>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400530#include <initializer_list>
531#include <iosfwd>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000532#include <iterator>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000533#include <memory>
534#include <stdexcept>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400535#include <string_view>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000536#include <type_traits>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400537#include <utility>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000538#include <version>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000539
Louis Dionne89258142021-08-23 15:32:36 -0400540#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
541# include <cwchar>
542#endif
543
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400544#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
545# include <cstdint>
546#endif
Eric Fiselier14b6de92014-08-10 23:53:08 +0000547
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000548#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000549#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000550#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000551
Eric Fiselierf4433a32017-05-31 22:07:49 +0000552_LIBCPP_PUSH_MACROS
553#include <__undef_macros>
554
555
Howard Hinnantc51e1022010-05-11 19:42:16 +0000556_LIBCPP_BEGIN_NAMESPACE_STD
557
558// fpos
559
560template <class _StateT>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000561class _LIBCPP_TEMPLATE_VIS fpos
Howard Hinnantc51e1022010-05-11 19:42:16 +0000562{
563private:
564 _StateT __st_;
565 streamoff __off_;
566public:
567 _LIBCPP_INLINE_VISIBILITY fpos(streamoff __off = streamoff()) : __st_(), __off_(__off) {}
568
569 _LIBCPP_INLINE_VISIBILITY operator streamoff() const {return __off_;}
570
571 _LIBCPP_INLINE_VISIBILITY _StateT state() const {return __st_;}
572 _LIBCPP_INLINE_VISIBILITY void state(_StateT __st) {__st_ = __st;}
573
574 _LIBCPP_INLINE_VISIBILITY fpos& operator+=(streamoff __off) {__off_ += __off; return *this;}
575 _LIBCPP_INLINE_VISIBILITY fpos operator+ (streamoff __off) const {fpos __t(*this); __t += __off; return __t;}
576 _LIBCPP_INLINE_VISIBILITY fpos& operator-=(streamoff __off) {__off_ -= __off; return *this;}
577 _LIBCPP_INLINE_VISIBILITY fpos operator- (streamoff __off) const {fpos __t(*this); __t -= __off; return __t;}
578};
579
580template <class _StateT>
581inline _LIBCPP_INLINE_VISIBILITY
582streamoff operator-(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
583 {return streamoff(__x) - streamoff(__y);}
584
585template <class _StateT>
586inline _LIBCPP_INLINE_VISIBILITY
587bool operator==(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
588 {return streamoff(__x) == streamoff(__y);}
589
590template <class _StateT>
591inline _LIBCPP_INLINE_VISIBILITY
592bool operator!=(const fpos<_StateT>& __x, const fpos<_StateT>& __y)
593 {return streamoff(__x) != streamoff(__y);}
594
Howard Hinnantc51e1022010-05-11 19:42:16 +0000595// basic_string
596
597template<class _CharT, class _Traits, class _Allocator>
598basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000599operator+(const basic_string<_CharT, _Traits, _Allocator>& __x,
600 const basic_string<_CharT, _Traits, _Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000601
602template<class _CharT, class _Traits, class _Allocator>
603basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000604operator+(const _CharT* __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000605
606template<class _CharT, class _Traits, class _Allocator>
607basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000608operator+(_CharT __x, const basic_string<_CharT,_Traits,_Allocator>& __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000609
610template<class _CharT, class _Traits, class _Allocator>
Louis Dionne8f8d95d2018-11-21 17:31:55 +0000611inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000612basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000613operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, const _CharT* __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000614
615template<class _CharT, class _Traits, class _Allocator>
616basic_string<_CharT, _Traits, _Allocator>
Howard Hinnant944510a2011-06-14 19:58:17 +0000617operator+(const basic_string<_CharT, _Traits, _Allocator>& __x, _CharT __y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000618
Shoaib Meenaiea363712017-07-29 02:54:41 +0000619_LIBCPP_EXTERN_TEMPLATE(_LIBCPP_FUNC_VIS string operator+<char, char_traits<char>, allocator<char> >(char const*, string const&))
620
Howard Hinnantc51e1022010-05-11 19:42:16 +0000621template <bool>
Louis Dionneb6aabd92021-08-19 12:21:06 -0400622struct __basic_string_common;
623
624template <>
625struct __basic_string_common<true> {
626 // Both are defined in string.cpp
627 _LIBCPP_NORETURN _LIBCPP_EXPORTED_FROM_ABI void __throw_length_error() const;
628 _LIBCPP_NORETURN _LIBCPP_EXPORTED_FROM_ABI void __throw_out_of_range() const;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000629};
630
Marshall Clow039b2f02016-01-13 21:54:34 +0000631template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400632struct __string_is_trivial_iterator : public false_type {};
633
634template <class _Tp>
635struct __string_is_trivial_iterator<_Tp*>
636 : public is_arithmetic<_Tp> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000637
Louis Dionne173f29e2019-05-29 16:01:36 +0000638template <class _Iter>
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -0400639struct __string_is_trivial_iterator<__wrap_iter<_Iter> >
640 : public __string_is_trivial_iterator<_Iter> {};
Marshall Clow039b2f02016-01-13 21:54:34 +0000641
Marshall Clow82513342016-09-24 22:45:42 +0000642template <class _CharT, class _Traits, class _Tp>
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500643struct __can_be_converted_to_string_view : public _BoolConstant<
644 is_convertible<const _Tp&, basic_string_view<_CharT, _Traits> >::value &&
645 !is_convertible<const _Tp&, const _CharT*>::value
646 > {};
Marshall Clow82513342016-09-24 22:45:42 +0000647
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000648#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000649
650template <class _CharT, size_t = sizeof(_CharT)>
651struct __padding
652{
653 unsigned char __xx[sizeof(_CharT)-1];
654};
655
656template <class _CharT>
657struct __padding<_CharT, 1>
658{
659};
660
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400661#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000662
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400663#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800664typedef basic_string<char8_t> u8string;
665#endif
666
667#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
668typedef basic_string<char16_t> u16string;
669typedef basic_string<char32_t> u32string;
Louis Dionne96fc5f52021-09-09 11:25:10 -0400670#endif
Richard Smith256954d2020-11-11 17:12:18 -0800671
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000672template<class _CharT, class _Traits, class _Allocator>
Richard Smith256954d2020-11-11 17:12:18 -0800673class
674 _LIBCPP_TEMPLATE_VIS
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400675#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800676 _LIBCPP_PREFERRED_NAME(u8string)
677#endif
678#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
679 _LIBCPP_PREFERRED_NAME(u16string)
680 _LIBCPP_PREFERRED_NAME(u32string)
681#endif
682 basic_string
Louis Dionneb6aabd92021-08-19 12:21:06 -0400683 : private __basic_string_common<true> // This base class is historical, but it needs to remain for ABI compatibility
Howard Hinnantc51e1022010-05-11 19:42:16 +0000684{
685public:
686 typedef basic_string __self;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000687 typedef basic_string_view<_CharT, _Traits> __self_view;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000688 typedef _Traits traits_type;
Marshall Clowa3a74e02017-03-15 18:41:11 +0000689 typedef _CharT value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000690 typedef _Allocator allocator_type;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000691 typedef allocator_traits<allocator_type> __alloc_traits;
692 typedef typename __alloc_traits::size_type size_type;
693 typedef typename __alloc_traits::difference_type difference_type;
Howard Hinnant3e276872011-06-03 18:40:47 +0000694 typedef value_type& reference;
695 typedef const value_type& const_reference;
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000696 typedef typename __alloc_traits::pointer pointer;
697 typedef typename __alloc_traits::const_pointer const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000698
Marshall Clow79f33542018-03-21 00:36:05 +0000699 static_assert((!is_array<value_type>::value), "Character type of basic_string must not be an array");
700 static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string must be standard-layout");
701 static_assert(( is_trivial<value_type>::value), "Character type of basic_string must be trivial");
702 static_assert(( is_same<_CharT, typename traits_type::char_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000703 "traits_type::char_type must be the same type as CharT");
Marshall Clow79f33542018-03-21 00:36:05 +0000704 static_assert(( is_same<typename allocator_type::value_type, value_type>::value),
Howard Hinnant8ea98242013-08-23 17:37:05 +0000705 "Allocator::value_type must be same type as value_type");
Marshall Clowe46031a2018-07-02 18:41:15 +0000706
Howard Hinnantc51e1022010-05-11 19:42:16 +0000707 typedef __wrap_iter<pointer> iterator;
708 typedef __wrap_iter<const_pointer> const_iterator;
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000709 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
710 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000711
712private:
Howard Hinnant68bf1812013-04-30 21:44:48 +0000713
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000714#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000715
716 struct __long
717 {
718 pointer __data_;
719 size_type __size_;
720 size_type __cap_;
721 };
722
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000723#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000724 static const size_type __short_mask = 0x01;
725 static const size_type __long_mask = 0x1ul;
Howard Hinnant68bf1812013-04-30 21:44:48 +0000726#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000727 static const size_type __short_mask = 0x80;
728 static const size_type __long_mask = ~(size_type(~0) >> 1);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400729#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +0000730
731 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
732 (sizeof(__long) - 1)/sizeof(value_type) : 2};
733
734 struct __short
735 {
736 value_type __data_[__min_cap];
737 struct
738 : __padding<value_type>
739 {
740 unsigned char __size_;
741 };
742 };
743
744#else
745
Howard Hinnantc51e1022010-05-11 19:42:16 +0000746 struct __long
747 {
748 size_type __cap_;
749 size_type __size_;
750 pointer __data_;
751 };
752
Eric Fiseliere9cc5922017-10-17 13:16:01 +0000753#ifdef _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000754 static const size_type __short_mask = 0x80;
755 static const size_type __long_mask = ~(size_type(~0) >> 1);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000756#else // _LIBCPP_BIG_ENDIAN
Ben Craig4b6f5f12017-07-12 01:45:13 +0000757 static const size_type __short_mask = 0x01;
758 static const size_type __long_mask = 0x1ul;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400759#endif // _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +0000760
Howard Hinnantc51e1022010-05-11 19:42:16 +0000761 enum {__min_cap = (sizeof(__long) - 1)/sizeof(value_type) > 2 ?
762 (sizeof(__long) - 1)/sizeof(value_type) : 2};
763
764 struct __short
765 {
766 union
767 {
768 unsigned char __size_;
Howard Hinnant49e145e2012-10-30 19:06:59 +0000769 value_type __lx;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000770 };
771 value_type __data_[__min_cap];
772 };
773
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400774#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +0000775
Howard Hinnant8ea98242013-08-23 17:37:05 +0000776 union __ulx{__long __lx; __short __lxx;};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000777
Howard Hinnant8ea98242013-08-23 17:37:05 +0000778 enum {__n_words = sizeof(__ulx) / sizeof(size_type)};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000779
780 struct __raw
781 {
782 size_type __words[__n_words];
783 };
784
785 struct __rep
786 {
787 union
788 {
789 __long __l;
790 __short __s;
791 __raw __r;
792 };
793 };
794
795 __compressed_pair<__rep, allocator_type> __r_;
796
Howard Hinnantc51e1022010-05-11 19:42:16 +0000797public:
Martin Storsjö88890c22021-02-22 01:13:13 +0200798 _LIBCPP_TEMPLATE_DATA_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000799 static const size_type npos = -1;
800
Howard Hinnant3e276872011-06-03 18:40:47 +0000801 _LIBCPP_INLINE_VISIBILITY basic_string()
802 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000803
804 _LIBCPP_INLINE_VISIBILITY explicit basic_string(const allocator_type& __a)
805#if _LIBCPP_STD_VER <= 14
806 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
807#else
808 _NOEXCEPT;
809#endif
810
Howard Hinnantc51e1022010-05-11 19:42:16 +0000811 basic_string(const basic_string& __str);
812 basic_string(const basic_string& __str, const allocator_type& __a);
Marshall Clowa80abc72015-06-03 19:56:43 +0000813
Eric Fiselierfc92be82017-04-19 00:28:44 +0000814#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000815 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000816 basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +0000817#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +0000818 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
Marshall Clowa80abc72015-06-03 19:56:43 +0000819#else
820 _NOEXCEPT;
821#endif
822
Howard Hinnantebb0edf2011-01-26 00:06:59 +0000823 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000824 basic_string(basic_string&& __str, const allocator_type& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400825#endif // _LIBCPP_CXX03_LANG
Marshall Clowe46031a2018-07-02 18:41:15 +0000826
Louis Dionne9ce598d2021-09-08 09:14:43 -0400827 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Eric Fiseliera8567862018-07-17 05:48:48 +0000828 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5169d1c2019-12-16 19:03:23 -0500829 basic_string(const _CharT* __s) : __r_(__default_init_tag(), __default_init_tag()) {
Eric Fiseliera8567862018-07-17 05:48:48 +0000830 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
831 __init(__s, traits_type::length(__s));
Nikolas Klauserf2807732022-01-11 00:33:35 +0100832 _VSTD::__debug_db_insert_c(this);
Eric Fiseliera8567862018-07-17 05:48:48 +0000833 }
Marshall Clowe46031a2018-07-02 18:41:15 +0000834
Louis Dionne9ce598d2021-09-08 09:14:43 -0400835 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000836 _LIBCPP_INLINE_VISIBILITY
837 basic_string(const _CharT* __s, const _Allocator& __a);
838
Marek Kurdej90d79712021-07-27 16:16:21 +0200839#if _LIBCPP_STD_VER > 20
840 basic_string(nullptr_t) = delete;
841#endif
842
Howard Hinnantcf823322010-12-17 14:46:43 +0000843 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000844 basic_string(const _CharT* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +0000845 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000846 basic_string(const _CharT* __s, size_type __n, const _Allocator& __a);
Howard Hinnantcf823322010-12-17 14:46:43 +0000847 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000848 basic_string(size_type __n, _CharT __c);
Marshall Clowe46031a2018-07-02 18:41:15 +0000849
Louis Dionne9ce598d2021-09-08 09:14:43 -0400850 template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000851 _LIBCPP_INLINE_VISIBILITY
852 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
853
Marshall Clow83445802016-04-07 18:13:41 +0000854 basic_string(const basic_string& __str, size_type __pos, size_type __n,
Eric Fiselier812882b2017-02-17 01:17:10 +0000855 const _Allocator& __a = _Allocator());
Marshall Clow83445802016-04-07 18:13:41 +0000856 _LIBCPP_INLINE_VISIBILITY
857 basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +0000858 const _Allocator& __a = _Allocator());
Marshall Clowe46031a2018-07-02 18:41:15 +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> >
Shoaib Meenai69c57412017-03-02 03:02:50 +0000861 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Eric Fiselier812882b2017-02-17 01:17:10 +0000862 basic_string(const _Tp& __t, size_type __pos, size_type __n,
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500863 const allocator_type& __a = allocator_type());
Marshall Clowe46031a2018-07-02 18:41:15 +0000864
Louis Dionne9ce598d2021-09-08 09:14:43 -0400865 template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
Eric Fiselierc522ceb2020-01-15 16:57:08 -0500866 !__is_same_uncvref<_Tp, basic_string>::value> >
Marshall Clowe46031a2018-07-02 18:41:15 +0000867 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
868 explicit basic_string(const _Tp& __t);
869
Louis Dionne9ce598d2021-09-08 09:14:43 -0400870 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 +0000871 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
872 explicit basic_string(const _Tp& __t, const allocator_type& __a);
873
Louis Dionne9ce598d2021-09-08 09:14:43 -0400874 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000875 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000876 basic_string(_InputIterator __first, _InputIterator __last);
Louis Dionne9ce598d2021-09-08 09:14:43 -0400877 template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
Howard Hinnantcf823322010-12-17 14:46:43 +0000878 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000879 basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
Eric Fiselierfc92be82017-04-19 00:28:44 +0000880#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000881 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000882 basic_string(initializer_list<_CharT> __il);
Howard Hinnantcf823322010-12-17 14:46:43 +0000883 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier812882b2017-02-17 01:17:10 +0000884 basic_string(initializer_list<_CharT> __il, const _Allocator& __a);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400885#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000886
Eric Fiselierd9a702a2016-10-31 03:42:50 +0000887 inline ~basic_string();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000888
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000889 _LIBCPP_INLINE_VISIBILITY
890 operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
891
Howard Hinnantea8f7e12010-11-17 17:55:08 +0000892 basic_string& operator=(const basic_string& __str);
Eric Fiselier5ec13b12017-01-23 21:24:58 +0000893
Louis Dionne9ce598d2021-09-08 09:14:43 -0400894 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 +0000895 basic_string& operator=(const _Tp& __t)
896 {__self_view __sv = __t; return assign(__sv);}
897
Eric Fiselierfc92be82017-04-19 00:28:44 +0000898#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +0000899 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +0000900 basic_string& operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +0000901 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
Eric Fiselierfc92be82017-04-19 00:28:44 +0000902 _LIBCPP_INLINE_VISIBILITY
903 basic_string& operator=(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000904#endif
Howard Hinnantd17880b2013-06-28 16:59:19 +0000905 _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const value_type* __s) {return assign(__s);}
Marek Kurdej90d79712021-07-27 16:16:21 +0200906#if _LIBCPP_STD_VER > 20
907 basic_string& operator=(nullptr_t) = delete;
908#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000909 basic_string& operator=(value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000910
Louis Dionneba400782020-10-02 15:02:52 -0400911#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +0000912 _LIBCPP_INLINE_VISIBILITY
913 iterator begin() _NOEXCEPT
914 {return iterator(this, __get_pointer());}
915 _LIBCPP_INLINE_VISIBILITY
916 const_iterator begin() const _NOEXCEPT
917 {return const_iterator(this, __get_pointer());}
918 _LIBCPP_INLINE_VISIBILITY
919 iterator end() _NOEXCEPT
920 {return iterator(this, __get_pointer() + size());}
921 _LIBCPP_INLINE_VISIBILITY
922 const_iterator end() const _NOEXCEPT
923 {return const_iterator(this, __get_pointer() + size());}
924#else
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000925 _LIBCPP_INLINE_VISIBILITY
926 iterator begin() _NOEXCEPT
927 {return iterator(__get_pointer());}
928 _LIBCPP_INLINE_VISIBILITY
929 const_iterator begin() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000930 {return const_iterator(__get_pointer());}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000931 _LIBCPP_INLINE_VISIBILITY
932 iterator end() _NOEXCEPT
933 {return iterator(__get_pointer() + size());}
934 _LIBCPP_INLINE_VISIBILITY
935 const_iterator end() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +0000936 {return const_iterator(__get_pointer() + size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400937#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000938 _LIBCPP_INLINE_VISIBILITY
939 reverse_iterator rbegin() _NOEXCEPT
940 {return reverse_iterator(end());}
941 _LIBCPP_INLINE_VISIBILITY
942 const_reverse_iterator rbegin() const _NOEXCEPT
943 {return const_reverse_iterator(end());}
944 _LIBCPP_INLINE_VISIBILITY
945 reverse_iterator rend() _NOEXCEPT
946 {return reverse_iterator(begin());}
947 _LIBCPP_INLINE_VISIBILITY
948 const_reverse_iterator rend() const _NOEXCEPT
949 {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000950
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000951 _LIBCPP_INLINE_VISIBILITY
952 const_iterator cbegin() const _NOEXCEPT
953 {return begin();}
954 _LIBCPP_INLINE_VISIBILITY
955 const_iterator cend() const _NOEXCEPT
956 {return end();}
957 _LIBCPP_INLINE_VISIBILITY
958 const_reverse_iterator crbegin() const _NOEXCEPT
959 {return rbegin();}
960 _LIBCPP_INLINE_VISIBILITY
961 const_reverse_iterator crend() const _NOEXCEPT
962 {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000963
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000964 _LIBCPP_INLINE_VISIBILITY size_type size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000965 {return __is_long() ? __get_long_size() : __get_short_size();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000966 _LIBCPP_INLINE_VISIBILITY size_type length() const _NOEXCEPT {return size();}
967 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT;
968 _LIBCPP_INLINE_VISIBILITY size_type capacity() const _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +0000969 {return (__is_long() ? __get_long_cap()
970 : static_cast<size_type>(__min_cap)) - 1;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000971
972 void resize(size_type __n, value_type __c);
973 _LIBCPP_INLINE_VISIBILITY void resize(size_type __n) {resize(__n, value_type());}
974
Marek Kurdejc9848142020-11-26 10:07:16 +0100975 void reserve(size_type __requested_capacity);
Nikolas Klauser9e6040c2022-01-06 21:43:26 +0100976
977#if _LIBCPP_STD_VER > 20
978 template <class _Op>
979 _LIBCPP_HIDE_FROM_ABI constexpr
980 void resize_and_overwrite(size_type __n, _Op __op) {
981 __resize_default_init(__n);
982 pointer __data = data();
983 __erase_to_end(_VSTD::move(__op)(__data, __n));
984 }
985#endif
986
Eric Fiselier451d5582018-11-26 20:15:38 +0000987 _LIBCPP_INLINE_VISIBILITY void __resize_default_init(size_type __n);
988
Marek Kurdejc9848142020-11-26 10:07:16 +0100989 _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_INLINE_VISIBILITY
990 void reserve() _NOEXCEPT {shrink_to_fit();}
Marshall Clow6ae12a82018-11-28 18:18:34 +0000991 _LIBCPP_INLINE_VISIBILITY
Marek Kurdejc9848142020-11-26 10:07:16 +0100992 void shrink_to_fit() _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +0000993 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +0000994 void clear() _NOEXCEPT;
Marshall Clowb7db4972017-11-15 20:02:27 +0000995 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
996 bool empty() const _NOEXCEPT {return size() == 0;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000997
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000998 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __pos) const _NOEXCEPT;
999 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __pos) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001000
1001 const_reference at(size_type __n) const;
1002 reference at(size_type __n);
1003
1004 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const basic_string& __str) {return append(__str);}
Marshall Clowe46031a2018-07-02 18:41:15 +00001005
1006 template <class _Tp>
1007 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001008 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001009 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001010 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1011 && !__is_same_uncvref<_Tp, basic_string >::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001012 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001013 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001014 operator+=(const _Tp& __t) {__self_view __sv = __t; return append(__sv);}
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001015 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(const value_type* __s) {return append(__s);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001016 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(value_type __c) {push_back(__c); return *this;}
Eric Fiselierfc92be82017-04-19 00:28:44 +00001017#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001018 _LIBCPP_INLINE_VISIBILITY basic_string& operator+=(initializer_list<value_type> __il) {return append(__il);}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001019#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001020
Howard Hinnantcf823322010-12-17 14:46:43 +00001021 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001022 basic_string& append(const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001023
1024 template <class _Tp>
1025 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001026 __enable_if_t<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001027 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1028 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clowe46031a2018-07-02 18:41:15 +00001029 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001030 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001031 append(const _Tp& __t) { __self_view __sv = __t; return append(__sv.data(), __sv.size()); }
Marshall Clow8db7fb02014-03-04 19:17:19 +00001032 basic_string& append(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clowe46031a2018-07-02 18:41:15 +00001033
Marshall Clow62953962016-10-03 23:40:48 +00001034 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001035 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001036 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001037 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001038 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1039 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001040 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001041 >
Marshall Clow82513342016-09-24 22:45:42 +00001042 append(const _Tp& __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001043 basic_string& append(const value_type* __s, size_type __n);
1044 basic_string& append(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001045 basic_string& append(size_type __n, value_type __c);
Eric Fiselier451d5582018-11-26 20:15:38 +00001046
1047 _LIBCPP_INLINE_VISIBILITY
1048 void __append_default_init(size_type __n);
1049
Howard Hinnantc51e1022010-05-11 19:42:16 +00001050 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001051 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001052 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001053 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001054 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001055 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001056 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001057 _LIBCPP_INLINE_VISIBILITY
1058 append(_InputIterator __first, _InputIterator __last) {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001059 const basic_string __temp(__first, __last, __alloc());
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001060 append(__temp.data(), __temp.size());
1061 return *this;
1062 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001063 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001064 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001065 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001066 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001067 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001068 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001069 >
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001070 _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001071 append(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00001072
Eric Fiselierfc92be82017-04-19 00:28:44 +00001073#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001074 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001075 basic_string& append(initializer_list<value_type> __il) {return append(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001076#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001077
1078 void push_back(value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001079 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001080 void pop_back();
Marshall Clow05cf6692019-03-19 03:30:07 +00001081 _LIBCPP_INLINE_VISIBILITY reference front() _NOEXCEPT;
1082 _LIBCPP_INLINE_VISIBILITY const_reference front() const _NOEXCEPT;
1083 _LIBCPP_INLINE_VISIBILITY reference back() _NOEXCEPT;
1084 _LIBCPP_INLINE_VISIBILITY const_reference back() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001085
Marshall Clowe46031a2018-07-02 18:41:15 +00001086 template <class _Tp>
1087 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001088 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001089 <
1090 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1091 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001092 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001093 assign(const _Tp & __t) { __self_view __sv = __t; return assign(__sv.data(), __sv.size()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001094 _LIBCPP_INLINE_VISIBILITY
Marshall Clow95d5e9a2016-03-09 18:08:29 +00001095 basic_string& assign(const basic_string& __str) { return *this = __str; }
Eric Fiselierfc92be82017-04-19 00:28:44 +00001096#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001097 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001098 basic_string& assign(basic_string&& __str)
Marshall Clow5aa9e942015-10-05 16:17:34 +00001099 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001100 {*this = _VSTD::move(__str); return *this;}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001101#endif
Marshall Clow8db7fb02014-03-04 19:17:19 +00001102 basic_string& assign(const basic_string& __str, size_type __pos, size_type __n=npos);
Marshall Clow62953962016-10-03 23:40:48 +00001103 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001104 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001105 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001106 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001107 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
1108 && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001109 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001110 >
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +00001111 assign(const _Tp & __t, size_type __pos, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001112 basic_string& assign(const value_type* __s, size_type __n);
1113 basic_string& assign(const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001114 basic_string& assign(size_type __n, value_type __c);
1115 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001116 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001117 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001118 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001119 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001120 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001121 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001122 assign(_InputIterator __first, _InputIterator __last);
1123 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001124 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001125 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001126 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001127 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001128 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001129 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001130 assign(_ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001131#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001132 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001133 basic_string& assign(initializer_list<value_type> __il) {return assign(__il.begin(), __il.size());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001134#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001135
Howard Hinnantcf823322010-12-17 14:46:43 +00001136 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001137 basic_string& insert(size_type __pos1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001138
1139 template <class _Tp>
1140 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001141 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001142 <
1143 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1144 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001145 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001146 insert(size_type __pos1, const _Tp& __t)
1147 { __self_view __sv = __t; return insert(__pos1, __sv.data(), __sv.size()); }
1148
Marshall Clow82513342016-09-24 22:45:42 +00001149 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001150 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001151 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001152 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001153 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001154 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001155 >
Marshall Clow82513342016-09-24 22:45:42 +00001156 insert(size_type __pos1, const _Tp& __t, size_type __pos2, size_type __n=npos);
Marshall Clow8db7fb02014-03-04 19:17:19 +00001157 basic_string& insert(size_type __pos1, const basic_string& __str, size_type __pos2, size_type __n=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001158 basic_string& insert(size_type __pos, const value_type* __s, size_type __n);
1159 basic_string& insert(size_type __pos, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001160 basic_string& insert(size_type __pos, size_type __n, value_type __c);
1161 iterator insert(const_iterator __pos, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001162 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001163 iterator insert(const_iterator __pos, size_type __n, value_type __c);
1164 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001165 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001166 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001167 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001168 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001169 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001170 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001171 insert(const_iterator __pos, _InputIterator __first, _InputIterator __last);
1172 template<class _ForwardIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001173 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001174 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001175 <
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001176 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001177 iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001178 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001179 insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001180#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001181 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001182 iterator insert(const_iterator __pos, initializer_list<value_type> __il)
1183 {return insert(__pos, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001184#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001185
1186 basic_string& erase(size_type __pos = 0, size_type __n = npos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001187 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001188 iterator erase(const_iterator __pos);
Howard Hinnantcf823322010-12-17 14:46:43 +00001189 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001190 iterator erase(const_iterator __first, const_iterator __last);
1191
Howard Hinnantcf823322010-12-17 14:46:43 +00001192 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001193 basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001194
1195 template <class _Tp>
1196 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001197 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001198 <
1199 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1200 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001201 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001202 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 +00001203 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 +00001204 template <class _Tp>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001205 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001206 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001207 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001208 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001209 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001210 >
Marshall Clow82513342016-09-24 22:45:42 +00001211 replace(size_type __pos1, size_type __n1, const _Tp& __t, size_type __pos2, size_type __n2=npos);
Howard Hinnantd17880b2013-06-28 16:59:19 +00001212 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s, size_type __n2);
1213 basic_string& replace(size_type __pos, size_type __n1, const value_type* __s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001214 basic_string& replace(size_type __pos, size_type __n1, size_type __n2, value_type __c);
Howard Hinnantcf823322010-12-17 14:46:43 +00001215 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001216 basic_string& replace(const_iterator __i1, const_iterator __i2, const basic_string& __str);
Marshall Clowe46031a2018-07-02 18:41:15 +00001217
1218 template <class _Tp>
1219 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001220 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001221 <
1222 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1223 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001224 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001225 replace(const_iterator __i1, const_iterator __i2, const _Tp& __t) { __self_view __sv = __t; return replace(__i1 - begin(), __i2 - __i1, __sv); }
1226
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001227 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001228 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s, size_type __n);
Howard Hinnantcf823322010-12-17 14:46:43 +00001229 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001230 basic_string& replace(const_iterator __i1, const_iterator __i2, const value_type* __s);
Howard Hinnantcf823322010-12-17 14:46:43 +00001231 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001232 basic_string& replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001233 template<class _InputIterator>
Shoaib Meenai69c57412017-03-02 03:02:50 +00001234 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001235 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001236 <
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001237 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001238 basic_string&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001239 >
Howard Hinnant990d6e82010-11-17 21:11:40 +00001240 replace(const_iterator __i1, const_iterator __i2, _InputIterator __j1, _InputIterator __j2);
Eric Fiselierfc92be82017-04-19 00:28:44 +00001241#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant186dca82010-09-23 17:31:07 +00001242 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant990d6e82010-11-17 21:11:40 +00001243 basic_string& replace(const_iterator __i1, const_iterator __i2, initializer_list<value_type> __il)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001244 {return replace(__i1, __i2, __il.begin(), __il.end());}
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001245#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001246
Howard Hinnantd17880b2013-06-28 16:59:19 +00001247 size_type copy(value_type* __s, size_type __n, size_type __pos = 0) const;
Howard Hinnantcf823322010-12-17 14:46:43 +00001248 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001249 basic_string substr(size_type __pos = 0, size_type __n = npos) const;
1250
Howard Hinnantcf823322010-12-17 14:46:43 +00001251 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001252 void swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001253#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00001254 _NOEXCEPT;
Marshall Clow8982dcd2015-07-13 20:04:56 +00001255#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00001256 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00001257 __is_nothrow_swappable<allocator_type>::value);
1258#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001259
Howard Hinnantcf823322010-12-17 14:46:43 +00001260 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001261 const value_type* c_str() const _NOEXCEPT {return data();}
Howard Hinnantcf823322010-12-17 14:46:43 +00001262 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001263 const value_type* data() const _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Eric Fiselier4ee2a892017-11-20 20:23:27 +00001264#if _LIBCPP_STD_VER > 14 || defined(_LIBCPP_BUILDING_LIBRARY)
Marshall Clowd3c22392016-03-08 15:44:30 +00001265 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001266 value_type* data() _NOEXCEPT {return _VSTD::__to_address(__get_pointer());}
Marshall Clowd3c22392016-03-08 15:44:30 +00001267#endif
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 allocator_type get_allocator() const _NOEXCEPT {return __alloc();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001271
Howard Hinnantcf823322010-12-17 14:46:43 +00001272 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001273 size_type find(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001274
1275 template <class _Tp>
1276 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001277 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001278 <
1279 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1280 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001281 >
zoecarver1997e0a2021-02-05 11:54:47 -08001282 find(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001283 size_type find(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001284 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001285 size_type find(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001286 size_type find(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001287
Howard Hinnantcf823322010-12-17 14:46:43 +00001288 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001289 size_type rfind(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001290
1291 template <class _Tp>
1292 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001293 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001294 <
1295 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1296 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001297 >
zoecarver1997e0a2021-02-05 11:54:47 -08001298 rfind(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001299 size_type rfind(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001300 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001301 size_type rfind(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001302 size_type rfind(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001303
Howard Hinnantcf823322010-12-17 14:46:43 +00001304 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001305 size_type find_first_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001306
1307 template <class _Tp>
1308 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001309 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001310 <
1311 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1312 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001313 >
zoecarver1997e0a2021-02-05 11:54:47 -08001314 find_first_of(const _Tp& __t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001315 size_type find_first_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001316 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001317 size_type find_first_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001318 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001319 size_type find_first_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001320
Howard Hinnantcf823322010-12-17 14:46:43 +00001321 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001322 size_type find_last_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001323
1324 template <class _Tp>
1325 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001326 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001327 <
1328 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1329 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001330 >
zoecarver1997e0a2021-02-05 11:54:47 -08001331 find_last_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001332 size_type find_last_of(const value_type* __s, size_type __pos, size_type __n) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001333 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001334 size_type find_last_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantcf823322010-12-17 14:46:43 +00001335 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001336 size_type find_last_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001337
Howard Hinnantcf823322010-12-17 14:46:43 +00001338 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001339 size_type find_first_not_of(const basic_string& __str, size_type __pos = 0) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001340
1341 template <class _Tp>
1342 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001343 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001344 <
1345 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1346 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001347 >
zoecarver1997e0a2021-02-05 11:54:47 -08001348 find_first_not_of(const _Tp &__t, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001349 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 +00001350 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001351 size_type find_first_not_of(const value_type* __s, size_type __pos = 0) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001352 _LIBCPP_INLINE_VISIBILITY
1353 size_type find_first_not_of(value_type __c, size_type __pos = 0) const _NOEXCEPT;
1354
1355 _LIBCPP_INLINE_VISIBILITY
1356 size_type find_last_not_of(const basic_string& __str, size_type __pos = npos) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001357
1358 template <class _Tp>
1359 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001360 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001361 <
1362 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1363 size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001364 >
zoecarver1997e0a2021-02-05 11:54:47 -08001365 find_last_not_of(const _Tp& __t, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantd17880b2013-06-28 16:59:19 +00001366 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 +00001367 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantd17880b2013-06-28 16:59:19 +00001368 size_type find_last_not_of(const value_type* __s, size_type __pos = npos) const _NOEXCEPT;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001369 _LIBCPP_INLINE_VISIBILITY
1370 size_type find_last_not_of(value_type __c, size_type __pos = npos) const _NOEXCEPT;
1371
1372 _LIBCPP_INLINE_VISIBILITY
1373 int compare(const basic_string& __str) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001374
1375 template <class _Tp>
1376 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001377 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001378 <
1379 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1380 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001381 >
zoecarver1997e0a2021-02-05 11:54:47 -08001382 compare(const _Tp &__t) const _NOEXCEPT;
Marshall Clowe46031a2018-07-02 18:41:15 +00001383
1384 template <class _Tp>
1385 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Louis Dionne9ce598d2021-09-08 09:14:43 -04001386 __enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00001387 <
1388 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
1389 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001390 >
Marshall Clowe46031a2018-07-02 18:41:15 +00001391 compare(size_type __pos1, size_type __n1, const _Tp& __t) const;
1392
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001393 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001394 int compare(size_type __pos1, size_type __n1, const basic_string& __str) const;
Marshall Clow8db7fb02014-03-04 19:17:19 +00001395 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 +00001396
Marshall Clow82513342016-09-24 22:45:42 +00001397 template <class _Tp>
Eric Fiselier7d4aa5c2016-10-14 05:29:46 +00001398 inline _LIBCPP_INLINE_VISIBILITY
Louis Dionne9ce598d2021-09-08 09:14:43 -04001399 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00001400 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001401 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value,
Marshall Clow82513342016-09-24 22:45:42 +00001402 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001403 >
Marshall Clow82513342016-09-24 22:45:42 +00001404 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 +00001405 int compare(const value_type* __s) const _NOEXCEPT;
1406 int compare(size_type __pos1, size_type __n1, const value_type* __s) const;
1407 int compare(size_type __pos1, size_type __n1, const value_type* __s, size_type __n2) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001408
Marshall Clow18c293b2017-12-04 20:11:38 +00001409#if _LIBCPP_STD_VER > 17
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001410 constexpr _LIBCPP_INLINE_VISIBILITY
1411 bool starts_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001412 { return __self_view(data(), size()).starts_with(__sv); }
1413
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001414 constexpr _LIBCPP_INLINE_VISIBILITY
1415 bool starts_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001416 { return !empty() && _Traits::eq(front(), __c); }
1417
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001418 constexpr _LIBCPP_INLINE_VISIBILITY
1419 bool starts_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001420 { return starts_with(__self_view(__s)); }
1421
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001422 constexpr _LIBCPP_INLINE_VISIBILITY
1423 bool ends_with(__self_view __sv) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001424 { return __self_view(data(), size()).ends_with( __sv); }
1425
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001426 constexpr _LIBCPP_INLINE_VISIBILITY
1427 bool ends_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001428 { return !empty() && _Traits::eq(back(), __c); }
1429
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -04001430 constexpr _LIBCPP_INLINE_VISIBILITY
1431 bool ends_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +00001432 { return ends_with(__self_view(__s)); }
1433#endif
1434
Wim Leflere023c3542021-01-19 14:33:30 -05001435#if _LIBCPP_STD_VER > 20
1436 constexpr _LIBCPP_INLINE_VISIBILITY
1437 bool contains(__self_view __sv) const noexcept
1438 { return __self_view(data(), size()).contains(__sv); }
1439
1440 constexpr _LIBCPP_INLINE_VISIBILITY
1441 bool contains(value_type __c) const noexcept
1442 { return __self_view(data(), size()).contains(__c); }
1443
1444 constexpr _LIBCPP_INLINE_VISIBILITY
1445 bool contains(const value_type* __s) const
1446 { return __self_view(data(), size()).contains(__s); }
1447#endif
1448
Howard Hinnantcf823322010-12-17 14:46:43 +00001449 _LIBCPP_INLINE_VISIBILITY bool __invariants() const;
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001450
Marshall Clowe60a7182018-05-29 17:04:37 +00001451 _LIBCPP_INLINE_VISIBILITY void __clear_and_shrink() _NOEXCEPT;
Marshall Clow851b9ec2019-05-20 21:56:51 +00001452
Marek Kurdejc9848142020-11-26 10:07:16 +01001453 _LIBCPP_INLINE_VISIBILITY void __shrink_or_extend(size_type __target_capacity);
1454
Howard Hinnantaaeb1132013-04-22 23:55:13 +00001455 _LIBCPP_INLINE_VISIBILITY
1456 bool __is_long() const _NOEXCEPT
1457 {return bool(__r_.first().__s.__size_ & __short_mask);}
1458
Louis Dionneba400782020-10-02 15:02:52 -04001459#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001460
1461 bool __dereferenceable(const const_iterator* __i) const;
1462 bool __decrementable(const const_iterator* __i) const;
1463 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1464 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1465
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001466#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00001467
Howard Hinnantc51e1022010-05-11 19:42:16 +00001468private:
Nikolas Klauser93826d12022-01-04 17:24:03 +01001469 _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI static bool __fits_in_sso(size_type __sz) {
1470 // SSO is disabled during constant evaluation because `__is_long` isn't constexpr friendly
1471 return !__libcpp_is_constant_evaluated() && (__sz < __min_cap);
1472 }
1473
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001474 _LIBCPP_INLINE_VISIBILITY
1475 allocator_type& __alloc() _NOEXCEPT
1476 {return __r_.second();}
1477 _LIBCPP_INLINE_VISIBILITY
1478 const allocator_type& __alloc() const _NOEXCEPT
1479 {return __r_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001480
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001481#ifdef _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001482
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001483 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001484 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001485# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001486 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001487# else
1488 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1489# endif
1490
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001491 _LIBCPP_INLINE_VISIBILITY
1492 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001493# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnantc51e1022010-05-11 19:42:16 +00001494 {return __r_.first().__s.__size_ >> 1;}
Howard Hinnant68bf1812013-04-30 21:44:48 +00001495# else
1496 {return __r_.first().__s.__size_;}
1497# endif
1498
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +00001499#else // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001500
1501 _LIBCPP_INLINE_VISIBILITY
1502 void __set_short_size(size_type __s) _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001503# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001504 {__r_.first().__s.__size_ = (unsigned char)(__s);}
1505# else
1506 {__r_.first().__s.__size_ = (unsigned char)(__s << 1);}
1507# endif
1508
1509 _LIBCPP_INLINE_VISIBILITY
1510 size_type __get_short_size() const _NOEXCEPT
Eric Fiseliere9cc5922017-10-17 13:16:01 +00001511# ifdef _LIBCPP_BIG_ENDIAN
Howard Hinnant68bf1812013-04-30 21:44:48 +00001512 {return __r_.first().__s.__size_;}
1513# else
1514 {return __r_.first().__s.__size_ >> 1;}
1515# endif
1516
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001517#endif // _LIBCPP_ABI_ALTERNATE_STRING_LAYOUT
Howard Hinnant68bf1812013-04-30 21:44:48 +00001518
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001519 _LIBCPP_INLINE_VISIBILITY
1520 void __set_long_size(size_type __s) _NOEXCEPT
1521 {__r_.first().__l.__size_ = __s;}
1522 _LIBCPP_INLINE_VISIBILITY
1523 size_type __get_long_size() const _NOEXCEPT
1524 {return __r_.first().__l.__size_;}
1525 _LIBCPP_INLINE_VISIBILITY
1526 void __set_size(size_type __s) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001527 {if (__is_long()) __set_long_size(__s); else __set_short_size(__s);}
1528
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001529 _LIBCPP_INLINE_VISIBILITY
1530 void __set_long_cap(size_type __s) _NOEXCEPT
1531 {__r_.first().__l.__cap_ = __long_mask | __s;}
1532 _LIBCPP_INLINE_VISIBILITY
1533 size_type __get_long_cap() const _NOEXCEPT
Howard Hinnant28b24882011-12-01 20:21:04 +00001534 {return __r_.first().__l.__cap_ & size_type(~__long_mask);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001535
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001536 _LIBCPP_INLINE_VISIBILITY
1537 void __set_long_pointer(pointer __p) _NOEXCEPT
1538 {__r_.first().__l.__data_ = __p;}
1539 _LIBCPP_INLINE_VISIBILITY
1540 pointer __get_long_pointer() _NOEXCEPT
1541 {return __r_.first().__l.__data_;}
1542 _LIBCPP_INLINE_VISIBILITY
1543 const_pointer __get_long_pointer() const _NOEXCEPT
1544 {return __r_.first().__l.__data_;}
1545 _LIBCPP_INLINE_VISIBILITY
1546 pointer __get_short_pointer() _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001547 {return pointer_traits<pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001548 _LIBCPP_INLINE_VISIBILITY
1549 const_pointer __get_short_pointer() const _NOEXCEPT
Howard Hinnantd17880b2013-06-28 16:59:19 +00001550 {return pointer_traits<const_pointer>::pointer_to(__r_.first().__s.__data_[0]);}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001551 _LIBCPP_INLINE_VISIBILITY
1552 pointer __get_pointer() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001553 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001554 _LIBCPP_INLINE_VISIBILITY
1555 const_pointer __get_pointer() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001556 {return __is_long() ? __get_long_pointer() : __get_short_pointer();}
1557
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001558 _LIBCPP_INLINE_VISIBILITY
1559 void __zero() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001560 {
1561 size_type (&__a)[__n_words] = __r_.first().__r.__words;
1562 for (unsigned __i = 0; __i < __n_words; ++__i)
1563 __a[__i] = 0;
1564 }
1565
1566 template <size_type __a> static
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001567 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantea382952013-08-14 18:00:20 +00001568 size_type __align_it(size_type __s) _NOEXCEPT
Eric Fiselier8599fcc2015-08-28 07:02:42 +00001569 {return (__s + (__a-1)) & ~(__a-1);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001570 enum {__alignment = 16};
Howard Hinnantaeb17d82011-05-29 19:57:12 +00001571 static _LIBCPP_INLINE_VISIBILITY
1572 size_type __recommend(size_type __s) _NOEXCEPT
Marshall Clow80584522018-02-07 21:30:17 +00001573 {
1574 if (__s < __min_cap) return static_cast<size_type>(__min_cap) - 1;
1575 size_type __guess = __align_it<sizeof(value_type) < __alignment ?
1576 __alignment/sizeof(value_type) : 1 > (__s+1) - 1;
1577 if (__guess == __min_cap) ++__guess;
1578 return __guess;
1579 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001580
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001581 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001582 void __init(const value_type* __s, size_type __sz, size_type __reserve);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001583 inline
Howard Hinnantd17880b2013-06-28 16:59:19 +00001584 void __init(const value_type* __s, size_type __sz);
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001585 inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001586 void __init(size_type __n, value_type __c);
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001587
Martijn Vels5e7c9752020-03-04 17:52:46 -05001588 // Slow path for the (inlined) copy constructor for 'long' strings.
1589 // Always externally instantiated and not inlined.
1590 // Requires that __s is zero terminated.
1591 // The main reason for this function to exist is because for unstable, we
1592 // want to allow inlining of the copy constructor. However, we don't want
1593 // to call the __init() functions as those are marked as inline which may
1594 // result in over-aggressive inlining by the compiler, where our aim is
1595 // to only inline the fast path code directly in the ctor.
1596 void __init_copy_ctor_external(const value_type* __s, size_type __sz);
1597
Howard Hinnantc51e1022010-05-11 19:42:16 +00001598 template <class _InputIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001599 inline
Louis Dionne9ce598d2021-09-08 09:14:43 -04001600 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001601 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001602 __is_exactly_cpp17_input_iterator<_InputIterator>::value
1603 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001604 __init(_InputIterator __first, _InputIterator __last);
1605
1606 template <class _ForwardIterator>
Duncan P. N. Exon Smitheb584632017-04-01 03:20:48 +00001607 inline
Louis Dionne9ce598d2021-09-08 09:14:43 -04001608 __enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00001609 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05001610 __is_cpp17_forward_iterator<_ForwardIterator>::value
1611 >
Howard Hinnantc51e1022010-05-11 19:42:16 +00001612 __init(_ForwardIterator __first, _ForwardIterator __last);
1613
1614 void __grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001615 size_type __n_copy, size_type __n_del, size_type __n_add = 0);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001616 void __grow_by_and_replace(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
1617 size_type __n_copy, size_type __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00001618 size_type __n_add, const value_type* __p_new_stuff);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001619
Martijn Vels596e3de2020-02-26 15:55:49 -05001620 // __assign_no_alias is invoked for assignment operations where we
1621 // have proof that the input does not alias the current instance.
1622 // For example, operator=(basic_string) performs a 'self' check.
1623 template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04001624 basic_string& __assign_no_alias(const value_type* __s, size_type __n);
Martijn Vels596e3de2020-02-26 15:55:49 -05001625
Howard Hinnantcf823322010-12-17 14:46:43 +00001626 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001627 void __erase_to_end(size_type __pos);
1628
Martijn Velsa81fc792020-02-26 13:25:43 -05001629 // __erase_external_with_move is invoked for erase() invocations where
1630 // `n ~= npos`, likely requiring memory moves on the string data.
1631 void __erase_external_with_move(size_type __pos, size_type __n);
1632
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001633 _LIBCPP_INLINE_VISIBILITY
1634 void __copy_assign_alloc(const basic_string& __str)
1635 {__copy_assign_alloc(__str, integral_constant<bool,
1636 __alloc_traits::propagate_on_container_copy_assignment::value>());}
1637
1638 _LIBCPP_INLINE_VISIBILITY
1639 void __copy_assign_alloc(const basic_string& __str, true_type)
1640 {
Marshall Clowf258c202017-01-31 03:40:52 +00001641 if (__alloc() == __str.__alloc())
1642 __alloc() = __str.__alloc();
1643 else
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001644 {
Marshall Clowf258c202017-01-31 03:40:52 +00001645 if (!__str.__is_long())
1646 {
Vedant Kumar55e007e2018-03-08 21:15:26 +00001647 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001648 __alloc() = __str.__alloc();
1649 }
1650 else
1651 {
1652 allocator_type __a = __str.__alloc();
1653 pointer __p = __alloc_traits::allocate(__a, __str.__get_long_cap());
Vedant Kumar55e007e2018-03-08 21:15:26 +00001654 __clear_and_shrink();
Marshall Clowf258c202017-01-31 03:40:52 +00001655 __alloc() = _VSTD::move(__a);
1656 __set_long_pointer(__p);
1657 __set_long_cap(__str.__get_long_cap());
1658 __set_long_size(__str.size());
1659 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001660 }
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001661 }
1662
1663 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001664 void __copy_assign_alloc(const basic_string&, false_type) _NOEXCEPT
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001665 {}
1666
Eric Fiselierfc92be82017-04-19 00:28:44 +00001667#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantcf823322010-12-17 14:46:43 +00001668 _LIBCPP_INLINE_VISIBILITY
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001669 void __move_assign(basic_string& __str, false_type)
1670 _NOEXCEPT_(__alloc_traits::is_always_equal::value);
Howard Hinnantcf823322010-12-17 14:46:43 +00001671 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e276872011-06-03 18:40:47 +00001672 void __move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001673#if _LIBCPP_STD_VER > 14
1674 _NOEXCEPT;
1675#else
Howard Hinnant3e276872011-06-03 18:40:47 +00001676 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001677#endif
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00001678#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001679
1680 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001681 void
Howard Hinnantc2734962011-09-02 20:42:31 +00001682 __move_assign_alloc(basic_string& __str)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001683 _NOEXCEPT_(
1684 !__alloc_traits::propagate_on_container_move_assignment::value ||
1685 is_nothrow_move_assignable<allocator_type>::value)
1686 {__move_assign_alloc(__str, integral_constant<bool,
1687 __alloc_traits::propagate_on_container_move_assignment::value>());}
1688
1689 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2734962011-09-02 20:42:31 +00001690 void __move_assign_alloc(basic_string& __c, true_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001691 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
1692 {
1693 __alloc() = _VSTD::move(__c.__alloc());
1694 }
1695
1696 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant28b24882011-12-01 20:21:04 +00001697 void __move_assign_alloc(basic_string&, false_type)
Howard Hinnant58fe91b2011-08-17 20:36:18 +00001698 _NOEXCEPT
1699 {}
1700
Martijn Velsda7d94f2020-06-19 14:24:03 -04001701 basic_string& __assign_external(const value_type* __s);
1702 basic_string& __assign_external(const value_type* __s, size_type __n);
1703
1704 // Assigns the value in __s, guaranteed to be __n < __min_cap in length.
1705 inline basic_string& __assign_short(const value_type* __s, size_type __n) {
1706 pointer __p = __is_long()
1707 ? (__set_long_size(__n), __get_long_pointer())
1708 : (__set_short_size(__n), __get_short_pointer());
1709 traits_type::move(_VSTD::__to_address(__p), __s, __n);
1710 traits_type::assign(__p[__n], value_type());
1711 return *this;
1712 }
1713
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01001714 _LIBCPP_HIDE_FROM_ABI basic_string& __null_terminate_at(value_type* __p, size_type __newsz) {
1715 __set_size(__newsz);
1716 __invalidate_iterators_past(__newsz);
1717 traits_type::assign(__p[__newsz], value_type());
1718 return *this;
1719 }
1720
Howard Hinnantcf823322010-12-17 14:46:43 +00001721 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators();
1722 _LIBCPP_INLINE_VISIBILITY void __invalidate_iterators_past(size_type);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001723
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04001724 template<class _Tp>
1725 _LIBCPP_INLINE_VISIBILITY
1726 bool __addr_in_range(_Tp&& __t) const {
1727 const volatile void *__p = _VSTD::addressof(__t);
1728 return data() <= __p && __p <= data() + size();
1729 }
1730
Louis Dionned24191c2021-08-19 12:39:16 -04001731 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1732 void __throw_length_error() const {
1733#ifndef _LIBCPP_NO_EXCEPTIONS
1734 __basic_string_common<true>::__throw_length_error();
1735#else
1736 _VSTD::abort();
1737#endif
1738 }
1739
1740 _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
1741 void __throw_out_of_range() const {
1742#ifndef _LIBCPP_NO_EXCEPTIONS
1743 __basic_string_common<true>::__throw_out_of_range();
1744#else
1745 _VSTD::abort();
1746#endif
1747 }
1748
Howard Hinnantc51e1022010-05-11 19:42:16 +00001749 friend basic_string operator+<>(const basic_string&, const basic_string&);
1750 friend basic_string operator+<>(const value_type*, const basic_string&);
1751 friend basic_string operator+<>(value_type, const basic_string&);
1752 friend basic_string operator+<>(const basic_string&, const value_type*);
1753 friend basic_string operator+<>(const basic_string&, value_type);
1754};
1755
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001756// These declarations must appear before any functions are implicitly used
1757// so that they have the correct visibility specifier.
1758#ifdef _LIBCPP_ABI_STRING_OPTIMIZED_EXTERNAL_INSTANTIATION
Louis Dionne89258142021-08-23 15:32:36 -04001759 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1760# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1761 _LIBCPP_STRING_UNSTABLE_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1762# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001763#else
Louis Dionne89258142021-08-23 15:32:36 -04001764 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, char)
1765# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1766 _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_EXTERN_TEMPLATE, wchar_t)
1767# endif
Eric Fiselier2ed640b2020-03-04 13:54:04 -05001768#endif
1769
1770
Louis Dionned59f8a52021-08-17 11:59:07 -04001771#if _LIBCPP_STD_VER >= 17
Marshall Clowa0563332018-02-08 06:34:03 +00001772template<class _InputIterator,
Arthur O'Dwyer56226762021-03-03 23:02:20 -05001773 class _CharT = __iter_value_type<_InputIterator>,
Marshall Clowa0563332018-02-08 06:34:03 +00001774 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001775 class = enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value>,
1776 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowa0563332018-02-08 06:34:03 +00001777 >
1778basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
1779 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
Marshall Clowe46031a2018-07-02 18:41:15 +00001780
1781template<class _CharT,
1782 class _Traits,
1783 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001784 class = enable_if_t<__is_allocator<_Allocator>::value>
Marshall Clowe46031a2018-07-02 18:41:15 +00001785 >
1786explicit basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
1787 -> basic_string<_CharT, _Traits, _Allocator>;
1788
1789template<class _CharT,
1790 class _Traits,
1791 class _Allocator = allocator<_CharT>,
Louis Dionne25547162021-08-17 12:26:09 -04001792 class = enable_if_t<__is_allocator<_Allocator>::value>,
Marshall Clowe46031a2018-07-02 18:41:15 +00001793 class _Sz = typename allocator_traits<_Allocator>::size_type
1794 >
1795basic_string(basic_string_view<_CharT, _Traits>, _Sz, _Sz, const _Allocator& = _Allocator())
1796 -> basic_string<_CharT, _Traits, _Allocator>;
Marshall Clowa0563332018-02-08 06:34:03 +00001797#endif
1798
Howard Hinnantc51e1022010-05-11 19:42:16 +00001799template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001800inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001801void
1802basic_string<_CharT, _Traits, _Allocator>::__invalidate_all_iterators()
1803{
Louis Dionneba400782020-10-02 15:02:52 -04001804#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001805 if (!__libcpp_is_constant_evaluated())
1806 __get_db()->__invalidate_all(this);
Louis Dionneba400782020-10-02 15:02:52 -04001807#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001808}
1809
1810template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001811inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001812void
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001813basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type __pos)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001814{
Louis Dionneba400782020-10-02 15:02:52 -04001815#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001816 if (!__libcpp_is_constant_evaluated()) {
1817 __c_node* __c = __get_db()->__find_c_and_lock(this);
1818 if (__c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001819 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001820 const_pointer __new_last = __get_pointer() + __pos;
1821 for (__i_node** __p = __c->end_; __p != __c->beg_; )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001822 {
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001823 --__p;
1824 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_);
1825 if (__i->base() > __new_last)
1826 {
1827 (*__p)->__c_ = nullptr;
1828 if (--__c->end_ != __p)
1829 _VSTD::memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1830 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001831 }
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01001832 __get_db()->unlock();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001833 }
1834 }
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04001835#else
1836 (void)__pos;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001837#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnantc51e1022010-05-11 19:42:16 +00001838}
1839
1840template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001841inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001842basic_string<_CharT, _Traits, _Allocator>::basic_string()
Marshall Clowe546cbb2015-06-04 02:05:41 +00001843 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001844 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001845{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001846 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001847 __zero();
1848}
1849
1850template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001851inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001852basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
Eric Fiseliere012bf22015-07-18 20:40:46 +00001853#if _LIBCPP_STD_VER <= 14
1854 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
1855#else
1856 _NOEXCEPT
1857#endif
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001858: __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001859{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001860 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001861 __zero();
1862}
1863
1864template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier815ed732016-09-16 00:00:48 +00001865void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
1866 size_type __sz,
1867 size_type __reserve)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001868{
1869 if (__reserve > max_size())
1870 this->__throw_length_error();
1871 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001872 if (__fits_in_sso(__reserve))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001873 {
1874 __set_short_size(__sz);
1875 __p = __get_short_pointer();
1876 }
1877 else
1878 {
1879 size_type __cap = __recommend(__reserve);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001880 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001881 __set_long_pointer(__p);
1882 __set_long_cap(__cap+1);
1883 __set_long_size(__sz);
1884 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001885 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001886 traits_type::assign(__p[__sz], value_type());
1887}
1888
1889template <class _CharT, class _Traits, class _Allocator>
1890void
Howard Hinnantd17880b2013-06-28 16:59:19 +00001891basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s, size_type __sz)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001892{
1893 if (__sz > max_size())
1894 this->__throw_length_error();
1895 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001896 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001897 {
1898 __set_short_size(__sz);
1899 __p = __get_short_pointer();
1900 }
1901 else
1902 {
1903 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00001904 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001905 __set_long_pointer(__p);
1906 __set_long_cap(__cap+1);
1907 __set_long_size(__sz);
1908 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05001909 traits_type::copy(_VSTD::__to_address(__p), __s, __sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001910 traits_type::assign(__p[__sz], value_type());
1911}
1912
1913template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00001914template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00001915basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001916 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001917{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001918 _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001919 __init(__s, traits_type::length(__s));
Nikolas Klauserf2807732022-01-11 00:33:35 +01001920 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001921}
1922
1923template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001924inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001925basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001926 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001927{
Nikolas Klauserf2807732022-01-11 00:33:35 +01001928 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
1929 __init(__s, __n);
1930 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001931}
1932
1933template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001934inline
Eric Fiselier812882b2017-02-17 01:17:10 +00001935basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001936 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001937{
Howard Hinnant8ea98242013-08-23 17:37:05 +00001938 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001939 __init(__s, __n);
Nikolas Klauserf2807732022-01-11 00:33:35 +01001940 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001941}
1942
1943template <class _CharT, class _Traits, class _Allocator>
1944basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001945 : __r_(__default_init_tag(), __alloc_traits::select_on_container_copy_construction(__str.__alloc()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001946{
1947 if (!__str.__is_long())
1948 __r_.first().__r = __str.__r_.first().__r;
1949 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001950 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1951 __str.__get_long_size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01001952 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001953}
1954
1955template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00001956basic_string<_CharT, _Traits, _Allocator>::basic_string(
1957 const basic_string& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05001958 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001959{
1960 if (!__str.__is_long())
1961 __r_.first().__r = __str.__r_.first().__r;
1962 else
Martijn Vels5e7c9752020-03-04 17:52:46 -05001963 __init_copy_ctor_external(_VSTD::__to_address(__str.__get_long_pointer()),
1964 __str.__get_long_size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01001965 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001966}
1967
Martijn Vels5e7c9752020-03-04 17:52:46 -05001968template <class _CharT, class _Traits, class _Allocator>
1969void basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(
1970 const value_type* __s, size_type __sz) {
1971 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01001972 if (__fits_in_sso(__sz)) {
Martijn Vels5e7c9752020-03-04 17:52:46 -05001973 __p = __get_short_pointer();
1974 __set_short_size(__sz);
1975 } else {
1976 if (__sz > max_size())
1977 this->__throw_length_error();
1978 size_t __cap = __recommend(__sz);
1979 __p = __alloc_traits::allocate(__alloc(), __cap + 1);
1980 __set_long_pointer(__p);
1981 __set_long_cap(__cap + 1);
1982 __set_long_size(__sz);
1983 }
1984 traits_type::copy(_VSTD::__to_address(__p), __s, __sz + 1);
1985}
1986
Eric Fiselierfc92be82017-04-19 00:28:44 +00001987#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001988
1989template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00001990inline
Howard Hinnant3e276872011-06-03 18:40:47 +00001991basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
Marshall Clowa80abc72015-06-03 19:56:43 +00001992#if _LIBCPP_STD_VER <= 14
Howard Hinnant3e276872011-06-03 18:40:47 +00001993 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Marshall Clowa80abc72015-06-03 19:56:43 +00001994#else
1995 _NOEXCEPT
1996#endif
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001997 : __r_(_VSTD::move(__str.__r_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001998{
1999 __str.__zero();
Nikolas Klauserf2807732022-01-11 00:33:35 +01002000 _VSTD::__debug_db_insert_c(this);
Louis Dionneba400782020-10-02 15:02:52 -04002001#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauserf2807732022-01-11 00:33:35 +01002002 if (!__libcpp_is_constant_evaluated() && __is_long())
2003 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002004#endif
2005}
2006
2007template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002008inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002009basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002010 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002011{
Marshall Clowd2d24692014-07-17 15:32:20 +00002012 if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002013 __init(_VSTD::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
Marshall Clowd2d24692014-07-17 15:32:20 +00002014 else
2015 {
2016 __r_.first().__r = __str.__r_.first().__r;
2017 __str.__zero();
2018 }
Nikolas Klauserf2807732022-01-11 00:33:35 +01002019 _VSTD::__debug_db_insert_c(this);
Louis Dionneba400782020-10-02 15:02:52 -04002020#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauserf2807732022-01-11 00:33:35 +01002021 if (!__libcpp_is_constant_evaluated() && __is_long())
2022 __get_db()->swap(this, &__str);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002023#endif
2024}
2025
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002026#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002027
2028template <class _CharT, class _Traits, class _Allocator>
2029void
2030basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
2031{
2032 if (__n > max_size())
2033 this->__throw_length_error();
2034 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002035 if (__fits_in_sso(__n))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002036 {
2037 __set_short_size(__n);
2038 __p = __get_short_pointer();
2039 }
2040 else
2041 {
2042 size_type __cap = __recommend(__n);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002043 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002044 __set_long_pointer(__p);
2045 __set_long_cap(__cap+1);
2046 __set_long_size(__n);
2047 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002048 traits_type::assign(_VSTD::__to_address(__p), __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002049 traits_type::assign(__p[__n], value_type());
2050}
2051
2052template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002053inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002054basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002055 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002056{
2057 __init(__n, __c);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002058 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002059}
2060
2061template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002062template <class>
Eric Fiselier812882b2017-02-17 01:17:10 +00002063basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002064 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002065{
2066 __init(__n, __c);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002067 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002068}
2069
Howard Hinnantc51e1022010-05-11 19:42:16 +00002070template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier812882b2017-02-17 01:17:10 +00002071basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str,
2072 size_type __pos, size_type __n,
2073 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002074 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002075{
2076 size_type __str_sz = __str.size();
2077 if (__pos > __str_sz)
2078 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002079 __init(__str.data() + __pos, _VSTD::min(__n, __str_sz - __pos));
Nikolas Klauserf2807732022-01-11 00:33:35 +01002080 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002081}
2082
2083template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002084inline
Marshall Clow83445802016-04-07 18:13:41 +00002085basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
Eric Fiselier812882b2017-02-17 01:17:10 +00002086 const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002087 : __r_(__default_init_tag(), __a)
Marshall Clow83445802016-04-07 18:13:41 +00002088{
2089 size_type __str_sz = __str.size();
2090 if (__pos > __str_sz)
2091 this->__throw_out_of_range();
2092 __init(__str.data() + __pos, __str_sz - __pos);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002093 _VSTD::__debug_db_insert_c(this);
Marshall Clow83445802016-04-07 18:13:41 +00002094}
2095
2096template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002097template <class _Tp, class>
Marshall Clow78dbe462016-11-14 18:22:19 +00002098basic_string<_CharT, _Traits, _Allocator>::basic_string(
Marshall Clowe46031a2018-07-02 18:41:15 +00002099 const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002100 : __r_(__default_init_tag(), __a)
Marshall Clow78dbe462016-11-14 18:22:19 +00002101{
Marshall Clowe46031a2018-07-02 18:41:15 +00002102 __self_view __sv0 = __t;
2103 __self_view __sv = __sv0.substr(__pos, __n);
Marshall Clow78dbe462016-11-14 18:22:19 +00002104 __init(__sv.data(), __sv.size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002105 _VSTD::__debug_db_insert_c(this);
Marshall Clow78dbe462016-11-14 18:22:19 +00002106}
2107
2108template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002109template <class _Tp, class>
2110basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002111 : __r_(__default_init_tag(), __default_init_tag())
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002112{
Marshall Clowe46031a2018-07-02 18:41:15 +00002113 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002114 __init(__sv.data(), __sv.size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002115 _VSTD::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002116}
2117
2118template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00002119template <class _Tp, class>
2120basic_string<_CharT, _Traits, _Allocator>::basic_string(const _Tp & __t, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002121 : __r_(__default_init_tag(), __a)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002122{
Marshall Clowe46031a2018-07-02 18:41:15 +00002123 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002124 __init(__sv.data(), __sv.size());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002125 _VSTD::__debug_db_insert_c(this);
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002126}
2127
2128template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002129template <class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002130__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002131<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002132 __is_exactly_cpp17_input_iterator<_InputIterator>::value
2133>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002134basic_string<_CharT, _Traits, _Allocator>::__init(_InputIterator __first, _InputIterator __last)
2135{
2136 __zero();
2137#ifndef _LIBCPP_NO_EXCEPTIONS
2138 try
2139 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002140#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002141 for (; __first != __last; ++__first)
2142 push_back(*__first);
2143#ifndef _LIBCPP_NO_EXCEPTIONS
2144 }
2145 catch (...)
2146 {
2147 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002148 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002149 throw;
2150 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002151#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002152}
2153
2154template <class _CharT, class _Traits, class _Allocator>
2155template <class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002156__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002157<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002158 __is_cpp17_forward_iterator<_ForwardIterator>::value
2159>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002160basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _ForwardIterator __last)
2161{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002162 size_type __sz = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002163 if (__sz > max_size())
2164 this->__throw_length_error();
2165 pointer __p;
Nikolas Klauser93826d12022-01-04 17:24:03 +01002166 if (__fits_in_sso(__sz))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002167 {
2168 __set_short_size(__sz);
2169 __p = __get_short_pointer();
2170 }
2171 else
2172 {
2173 size_type __cap = __recommend(__sz);
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002174 __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002175 __set_long_pointer(__p);
2176 __set_long_cap(__cap+1);
2177 __set_long_size(__sz);
2178 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002179
2180#ifndef _LIBCPP_NO_EXCEPTIONS
2181 try
2182 {
2183#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiseliera09a3b42014-10-27 19:28:20 +00002184 for (; __first != __last; ++__first, (void) ++__p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002185 traits_type::assign(*__p, *__first);
2186 traits_type::assign(*__p, value_type());
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002187#ifndef _LIBCPP_NO_EXCEPTIONS
2188 }
2189 catch (...)
2190 {
2191 if (__is_long())
2192 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
2193 throw;
2194 }
2195#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00002196}
2197
2198template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002199template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002200inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002201basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002202 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002203{
2204 __init(__first, __last);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002205 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002206}
2207
2208template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierc0f566d2019-03-14 12:31:10 +00002209template<class _InputIterator, class>
Eric Fiselierbea70602018-11-26 22:51:35 +00002210inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002211basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
2212 const allocator_type& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002213 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002214{
2215 __init(__first, __last);
Nikolas Klauserf2807732022-01-11 00:33:35 +01002216 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002217}
2218
Eric Fiselierfc92be82017-04-19 00:28:44 +00002219#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002220
Howard Hinnantc51e1022010-05-11 19:42:16 +00002221template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002222inline
Eric Fiselier812882b2017-02-17 01:17:10 +00002223basic_string<_CharT, _Traits, _Allocator>::basic_string(
2224 initializer_list<_CharT> __il)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002225 : __r_(__default_init_tag(), __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00002226{
2227 __init(__il.begin(), __il.end());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002228 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002229}
2230
2231template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002232inline
Eric Fiselier9d355982017-04-12 23:45:53 +00002233
Eric Fiselier812882b2017-02-17 01:17:10 +00002234basic_string<_CharT, _Traits, _Allocator>::basic_string(
2235 initializer_list<_CharT> __il, const _Allocator& __a)
Eric Fiselier5169d1c2019-12-16 19:03:23 -05002236 : __r_(__default_init_tag(), __a)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002237{
2238 __init(__il.begin(), __il.end());
Nikolas Klauserf2807732022-01-11 00:33:35 +01002239 _VSTD::__debug_db_insert_c(this);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002240}
2241
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04002242#endif // _LIBCPP_CXX03_LANG
Howard Hinnant33711792011-08-12 21:56:02 +00002243
Howard Hinnantc51e1022010-05-11 19:42:16 +00002244template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002245basic_string<_CharT, _Traits, _Allocator>::~basic_string()
2246{
Louis Dionneba400782020-10-02 15:02:52 -04002247#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01002248 if (!__libcpp_is_constant_evaluated())
2249 __get_db()->__erase_c(this);
Howard Hinnant8ea98242013-08-23 17:37:05 +00002250#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002251 if (__is_long())
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002252 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), __get_long_cap());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002253}
2254
2255template <class _CharT, class _Traits, class _Allocator>
2256void
2257basic_string<_CharT, _Traits, _Allocator>::__grow_by_and_replace
2258 (size_type __old_cap, size_type __delta_cap, size_type __old_sz,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002259 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 +00002260{
2261 size_type __ms = max_size();
2262 if (__delta_cap > __ms - __old_cap - 1)
2263 this->__throw_length_error();
2264 pointer __old_p = __get_pointer();
2265 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002266 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002267 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002268 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002269 __invalidate_all_iterators();
2270 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002271 traits_type::copy(_VSTD::__to_address(__p),
2272 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002273 if (__n_add != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002274 traits_type::copy(_VSTD::__to_address(__p) + __n_copy, __p_new_stuff, __n_add);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002275 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2276 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002277 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2278 _VSTD::__to_address(__old_p) + __n_copy + __n_del, __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002279 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002280 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002281 __set_long_pointer(__p);
2282 __set_long_cap(__cap+1);
2283 __old_sz = __n_copy + __n_add + __sec_cp_sz;
2284 __set_long_size(__old_sz);
2285 traits_type::assign(__p[__old_sz], value_type());
2286}
2287
2288template <class _CharT, class _Traits, class _Allocator>
2289void
2290basic_string<_CharT, _Traits, _Allocator>::__grow_by(size_type __old_cap, size_type __delta_cap, size_type __old_sz,
2291 size_type __n_copy, size_type __n_del, size_type __n_add)
2292{
2293 size_type __ms = max_size();
Marshall Clow2267c5d2013-11-06 14:24:38 +00002294 if (__delta_cap > __ms - __old_cap)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002295 this->__throw_length_error();
2296 pointer __old_p = __get_pointer();
2297 size_type __cap = __old_cap < __ms / 2 - __alignment ?
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002298 __recommend(_VSTD::max(__old_cap + __delta_cap, 2 * __old_cap)) :
Howard Hinnantc51e1022010-05-11 19:42:16 +00002299 __ms - 1;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002300 pointer __p = __alloc_traits::allocate(__alloc(), __cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002301 __invalidate_all_iterators();
2302 if (__n_copy != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002303 traits_type::copy(_VSTD::__to_address(__p),
2304 _VSTD::__to_address(__old_p), __n_copy);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002305 size_type __sec_cp_sz = __old_sz - __n_del - __n_copy;
2306 if (__sec_cp_sz != 0)
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002307 traits_type::copy(_VSTD::__to_address(__p) + __n_copy + __n_add,
2308 _VSTD::__to_address(__old_p) + __n_copy + __n_del,
Howard Hinnantd17880b2013-06-28 16:59:19 +00002309 __sec_cp_sz);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002310 if (__old_cap+1 != __min_cap)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002311 __alloc_traits::deallocate(__alloc(), __old_p, __old_cap+1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002312 __set_long_pointer(__p);
2313 __set_long_cap(__cap+1);
2314}
2315
2316// assign
2317
2318template <class _CharT, class _Traits, class _Allocator>
Martijn Vels596e3de2020-02-26 15:55:49 -05002319template <bool __is_short>
Martijn Velsb6a08b62020-04-10 18:36:31 -04002320basic_string<_CharT, _Traits, _Allocator>&
2321basic_string<_CharT, _Traits, _Allocator>::__assign_no_alias(
Martijn Vels596e3de2020-02-26 15:55:49 -05002322 const value_type* __s, size_type __n) {
2323 size_type __cap = __is_short ? __min_cap : __get_long_cap();
2324 if (__n < __cap) {
2325 pointer __p = __is_short ? __get_short_pointer() : __get_long_pointer();
2326 __is_short ? __set_short_size(__n) : __set_long_size(__n);
2327 traits_type::copy(_VSTD::__to_address(__p), __s, __n);
2328 traits_type::assign(__p[__n], value_type());
2329 __invalidate_iterators_past(__n);
2330 } else {
2331 size_type __sz = __is_short ? __get_short_size() : __get_long_size();
2332 __grow_by_and_replace(__cap - 1, __n - __cap + 1, __sz, 0, __sz, __n, __s);
2333 }
Martijn Velsb6a08b62020-04-10 18:36:31 -04002334 return *this;
Martijn Vels596e3de2020-02-26 15:55:49 -05002335}
2336
2337template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002338basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002339basic_string<_CharT, _Traits, _Allocator>::__assign_external(
2340 const value_type* __s, size_type __n) {
2341 size_type __cap = capacity();
2342 if (__cap >= __n) {
2343 value_type* __p = _VSTD::__to_address(__get_pointer());
2344 traits_type::move(__p, __s, __n);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002345 return __null_terminate_at(__p, __n);
Martijn Velsda7d94f2020-06-19 14:24:03 -04002346 } else {
2347 size_type __sz = size();
2348 __grow_by_and_replace(__cap, __n - __cap, __sz, 0, __sz, __n, __s);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002349 return *this;
Martijn Velsda7d94f2020-06-19 14:24:03 -04002350 }
Martijn Velsda7d94f2020-06-19 14:24:03 -04002351}
2352
2353template <class _CharT, class _Traits, class _Allocator>
2354basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002355basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002356{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002357 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::assign received nullptr");
Nikolas Klauser93826d12022-01-04 17:24:03 +01002358 return (__builtin_constant_p(__n) && __fits_in_sso(__n))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002359 ? __assign_short(__s, __n)
2360 : __assign_external(__s, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002361}
2362
2363template <class _CharT, class _Traits, class _Allocator>
2364basic_string<_CharT, _Traits, _Allocator>&
2365basic_string<_CharT, _Traits, _Allocator>::assign(size_type __n, value_type __c)
2366{
2367 size_type __cap = capacity();
2368 if (__cap < __n)
2369 {
2370 size_type __sz = size();
2371 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2372 }
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002373 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002374 traits_type::assign(__p, __n, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002375 return __null_terminate_at(__p, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002376}
2377
2378template <class _CharT, class _Traits, class _Allocator>
2379basic_string<_CharT, _Traits, _Allocator>&
2380basic_string<_CharT, _Traits, _Allocator>::operator=(value_type __c)
2381{
2382 pointer __p;
2383 if (__is_long())
2384 {
2385 __p = __get_long_pointer();
2386 __set_long_size(1);
2387 }
2388 else
2389 {
2390 __p = __get_short_pointer();
2391 __set_short_size(1);
2392 }
2393 traits_type::assign(*__p, __c);
2394 traits_type::assign(*++__p, value_type());
2395 __invalidate_iterators_past(1);
2396 return *this;
2397}
2398
2399template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002400basic_string<_CharT, _Traits, _Allocator>&
2401basic_string<_CharT, _Traits, _Allocator>::operator=(const basic_string& __str)
2402{
Martijn Vels596e3de2020-02-26 15:55:49 -05002403 if (this != &__str) {
2404 __copy_assign_alloc(__str);
2405 if (!__is_long()) {
2406 if (!__str.__is_long()) {
Eric Fiselier03bbc922020-01-15 17:27:10 -05002407 __r_.first().__r = __str.__r_.first().__r;
Martijn Vels596e3de2020-02-26 15:55:49 -05002408 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002409 return __assign_no_alias<true>(__str.data(), __str.size());
Martijn Vels596e3de2020-02-26 15:55:49 -05002410 }
2411 } else {
Martijn Velsb6a08b62020-04-10 18:36:31 -04002412 return __assign_no_alias<false>(__str.data(), __str.size());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002413 }
Martijn Vels596e3de2020-02-26 15:55:49 -05002414 }
2415 return *this;
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002416}
2417
Eric Fiselierfc92be82017-04-19 00:28:44 +00002418#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002419
2420template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002421inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002422void
2423basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, false_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002424 _NOEXCEPT_(__alloc_traits::is_always_equal::value)
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002425{
2426 if (__alloc() != __str.__alloc())
2427 assign(__str);
2428 else
2429 __move_assign(__str, true_type());
2430}
2431
2432template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002433inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002434void
2435basic_string<_CharT, _Traits, _Allocator>::__move_assign(basic_string& __str, true_type)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002436#if _LIBCPP_STD_VER > 14
2437 _NOEXCEPT
2438#else
Howard Hinnant3e276872011-06-03 18:40:47 +00002439 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002440#endif
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002441{
marshall2ed41622019-11-27 07:13:00 -08002442 if (__is_long()) {
2443 __alloc_traits::deallocate(__alloc(), __get_long_pointer(),
2444 __get_long_cap());
2445#if _LIBCPP_STD_VER <= 14
2446 if (!is_nothrow_move_assignable<allocator_type>::value) {
2447 __set_short_size(0);
2448 traits_type::assign(__get_short_pointer()[0], value_type());
2449 }
2450#endif
2451 }
2452 __move_assign_alloc(__str);
2453 __r_.first() = __str.__r_.first();
2454 __str.__set_short_size(0);
2455 traits_type::assign(__str.__get_short_pointer()[0], value_type());
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002456}
2457
2458template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002459inline
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002460basic_string<_CharT, _Traits, _Allocator>&
2461basic_string<_CharT, _Traits, _Allocator>::operator=(basic_string&& __str)
Marshall Clow2fe8a8d2015-08-18 18:57:00 +00002462 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value))
Howard Hinnantea8f7e12010-11-17 17:55:08 +00002463{
2464 __move_assign(__str, integral_constant<bool,
2465 __alloc_traits::propagate_on_container_move_assignment::value>());
2466 return *this;
2467}
2468
2469#endif
2470
2471template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002472template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002473__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002474<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002475 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002476 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002477>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002478basic_string<_CharT, _Traits, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
2479{
Marshall Clow958362f2016-09-05 01:54:30 +00002480 const basic_string __temp(__first, __last, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00002481 assign(__temp.data(), __temp.size());
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +00002482 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002483}
2484
2485template <class _CharT, class _Traits, class _Allocator>
2486template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002487__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002488<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002489 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002490 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002491>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002492basic_string<_CharT, _Traits, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last)
2493{
Howard Hinnantc51e1022010-05-11 19:42:16 +00002494 size_type __cap = capacity();
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002495 size_type __n = __string_is_trivial_iterator<_ForwardIterator>::value ?
2496 static_cast<size_type>(_VSTD::distance(__first, __last)) : 0;
2497
2498 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2499 (__cap >= __n || !__addr_in_range(*__first)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002500 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002501 if (__cap < __n)
2502 {
2503 size_type __sz = size();
2504 __grow_by(__cap, __n - __cap, __sz, 0, __sz);
2505 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002506 pointer __p = __get_pointer();
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002507 for (; __first != __last; ++__p, (void) ++__first)
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002508 traits_type::assign(*__p, *__first);
2509 traits_type::assign(*__p, value_type());
2510 __set_size(__n);
Arthur O'Dwyerb3db4542021-04-27 09:10:04 -04002511 __invalidate_iterators_past(__n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002512 }
2513 else
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002514 {
2515 const basic_string __temp(__first, __last, __alloc());
2516 assign(__temp.data(), __temp.size());
2517 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002518 return *this;
2519}
2520
2521template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002522basic_string<_CharT, _Traits, _Allocator>&
2523basic_string<_CharT, _Traits, _Allocator>::assign(const basic_string& __str, size_type __pos, size_type __n)
2524{
2525 size_type __sz = __str.size();
2526 if (__pos > __sz)
2527 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002528 return assign(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002529}
2530
2531template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002532template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002533__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002534<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002535 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
2536 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00002537 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002538>
Marshall Clow82513342016-09-24 22:45:42 +00002539basic_string<_CharT, _Traits, _Allocator>::assign(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002540{
Marshall Clow82513342016-09-24 22:45:42 +00002541 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002542 size_type __sz = __sv.size();
2543 if (__pos > __sz)
2544 this->__throw_out_of_range();
2545 return assign(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2546}
2547
2548
2549template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002550basic_string<_CharT, _Traits, _Allocator>&
Martijn Velsda7d94f2020-06-19 14:24:03 -04002551basic_string<_CharT, _Traits, _Allocator>::__assign_external(const value_type* __s) {
2552 return __assign_external(__s, traits_type::length(__s));
2553}
2554
2555template <class _CharT, class _Traits, class _Allocator>
2556basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002557basic_string<_CharT, _Traits, _Allocator>::assign(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002558{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002559 _LIBCPP_ASSERT(__s != nullptr, "string::assign received nullptr");
Louis Dionne3e0c4332021-08-31 10:49:06 -04002560 return __builtin_constant_p(*__s)
Nikolas Klauser93826d12022-01-04 17:24:03 +01002561 ? (__fits_in_sso(traits_type::length(__s))
Martijn Velsda7d94f2020-06-19 14:24:03 -04002562 ? __assign_short(__s, traits_type::length(__s))
2563 : __assign_external(__s, traits_type::length(__s)))
2564 : __assign_external(__s);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002565}
Howard Hinnantc51e1022010-05-11 19:42:16 +00002566// append
2567
2568template <class _CharT, class _Traits, class _Allocator>
2569basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002570basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002571{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002572 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002573 size_type __cap = capacity();
2574 size_type __sz = size();
2575 if (__cap - __sz >= __n)
2576 {
2577 if (__n)
2578 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002579 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002580 traits_type::copy(__p + __sz, __s, __n);
2581 __sz += __n;
2582 __set_size(__sz);
2583 traits_type::assign(__p[__sz], value_type());
2584 }
2585 }
2586 else
2587 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __sz, 0, __n, __s);
2588 return *this;
2589}
2590
2591template <class _CharT, class _Traits, class _Allocator>
2592basic_string<_CharT, _Traits, _Allocator>&
2593basic_string<_CharT, _Traits, _Allocator>::append(size_type __n, value_type __c)
2594{
2595 if (__n)
2596 {
2597 size_type __cap = capacity();
2598 size_type __sz = size();
2599 if (__cap - __sz < __n)
2600 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2601 pointer __p = __get_pointer();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002602 traits_type::assign(_VSTD::__to_address(__p) + __sz, __n, __c);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002603 __sz += __n;
2604 __set_size(__sz);
2605 traits_type::assign(__p[__sz], value_type());
2606 }
2607 return *this;
2608}
2609
2610template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00002611inline void
2612basic_string<_CharT, _Traits, _Allocator>::__append_default_init(size_type __n)
2613{
2614 if (__n)
2615 {
2616 size_type __cap = capacity();
2617 size_type __sz = size();
2618 if (__cap - __sz < __n)
2619 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2620 pointer __p = __get_pointer();
2621 __sz += __n;
2622 __set_size(__sz);
2623 traits_type::assign(__p[__sz], value_type());
2624 }
2625}
2626
2627template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002628void
2629basic_string<_CharT, _Traits, _Allocator>::push_back(value_type __c)
2630{
Howard Hinnant68bf1812013-04-30 21:44:48 +00002631 bool __is_short = !__is_long();
2632 size_type __cap;
2633 size_type __sz;
2634 if (__is_short)
2635 {
2636 __cap = __min_cap - 1;
2637 __sz = __get_short_size();
2638 }
2639 else
2640 {
2641 __cap = __get_long_cap() - 1;
2642 __sz = __get_long_size();
2643 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002644 if (__sz == __cap)
Howard Hinnant68bf1812013-04-30 21:44:48 +00002645 {
Howard Hinnantc51e1022010-05-11 19:42:16 +00002646 __grow_by(__cap, 1, __sz, __sz, 0);
Louis Dionne82d2b2c2021-11-16 11:26:56 -05002647 __is_short = false; // the string is always long after __grow_by
Howard Hinnant68bf1812013-04-30 21:44:48 +00002648 }
2649 pointer __p;
2650 if (__is_short)
2651 {
2652 __p = __get_short_pointer() + __sz;
2653 __set_short_size(__sz+1);
2654 }
2655 else
2656 {
2657 __p = __get_long_pointer() + __sz;
2658 __set_long_size(__sz+1);
2659 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002660 traits_type::assign(*__p, __c);
2661 traits_type::assign(*++__p, value_type());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002662}
2663
Howard Hinnantc51e1022010-05-11 19:42:16 +00002664template <class _CharT, class _Traits, class _Allocator>
2665template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002666__enable_if_t
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002667<
2668 __is_cpp17_forward_iterator<_ForwardIterator>::value,
2669 basic_string<_CharT, _Traits, _Allocator>&
2670>
2671basic_string<_CharT, _Traits, _Allocator>::append(
Eric Fiselierdb7ee8f2016-10-31 02:46:25 +00002672 _ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002673{
2674 size_type __sz = size();
2675 size_type __cap = capacity();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002676 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002677 if (__n)
2678 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002679 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2680 !__addr_in_range(*__first))
Marshall Clow958362f2016-09-05 01:54:30 +00002681 {
2682 if (__cap - __sz < __n)
2683 __grow_by(__cap, __sz + __n - __cap, __sz, __sz, 0);
2684 pointer __p = __get_pointer() + __sz;
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002685 for (; __first != __last; ++__p, (void) ++__first)
Marshall Clow958362f2016-09-05 01:54:30 +00002686 traits_type::assign(*__p, *__first);
2687 traits_type::assign(*__p, value_type());
2688 __set_size(__sz + __n);
2689 }
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002690 else
2691 {
2692 const basic_string __temp(__first, __last, __alloc());
2693 append(__temp.data(), __temp.size());
2694 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002695 }
2696 return *this;
2697}
2698
2699template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002700inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002701basic_string<_CharT, _Traits, _Allocator>&
2702basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str)
2703{
2704 return append(__str.data(), __str.size());
2705}
2706
2707template <class _CharT, class _Traits, class _Allocator>
2708basic_string<_CharT, _Traits, _Allocator>&
2709basic_string<_CharT, _Traits, _Allocator>::append(const basic_string& __str, size_type __pos, size_type __n)
2710{
2711 size_type __sz = __str.size();
2712 if (__pos > __sz)
2713 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002714 return append(__str.data() + __pos, _VSTD::min(__n, __sz - __pos));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002715}
2716
2717template <class _CharT, class _Traits, class _Allocator>
Marshall Clow62953962016-10-03 23:40:48 +00002718template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002719 __enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002720 <
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002721 __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 +00002722 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002723 >
Marshall Clow82513342016-09-24 22:45:42 +00002724basic_string<_CharT, _Traits, _Allocator>::append(const _Tp & __t, size_type __pos, size_type __n)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002725{
Marshall Clow82513342016-09-24 22:45:42 +00002726 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002727 size_type __sz = __sv.size();
2728 if (__pos > __sz)
2729 this->__throw_out_of_range();
2730 return append(__sv.data() + __pos, _VSTD::min(__n, __sz - __pos));
2731}
2732
2733template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002734basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002735basic_string<_CharT, _Traits, _Allocator>::append(const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002736{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002737 _LIBCPP_ASSERT(__s != nullptr, "string::append received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002738 return append(__s, traits_type::length(__s));
2739}
2740
2741// insert
2742
2743template <class _CharT, class _Traits, class _Allocator>
2744basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002745basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002746{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002747 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002748 size_type __sz = size();
2749 if (__pos > __sz)
2750 this->__throw_out_of_range();
2751 size_type __cap = capacity();
2752 if (__cap - __sz >= __n)
2753 {
2754 if (__n)
2755 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002756 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002757 size_type __n_move = __sz - __pos;
2758 if (__n_move != 0)
2759 {
2760 if (__p + __pos <= __s && __s < __p + __sz)
2761 __s += __n;
2762 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2763 }
2764 traits_type::move(__p + __pos, __s, __n);
2765 __sz += __n;
2766 __set_size(__sz);
2767 traits_type::assign(__p[__sz], value_type());
2768 }
2769 }
2770 else
2771 __grow_by_and_replace(__cap, __sz + __n - __cap, __sz, __pos, 0, __n, __s);
2772 return *this;
2773}
2774
2775template <class _CharT, class _Traits, class _Allocator>
2776basic_string<_CharT, _Traits, _Allocator>&
2777basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, size_type __n, value_type __c)
2778{
2779 size_type __sz = size();
2780 if (__pos > __sz)
2781 this->__throw_out_of_range();
2782 if (__n)
2783 {
2784 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002785 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002786 if (__cap - __sz >= __n)
2787 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002788 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002789 size_type __n_move = __sz - __pos;
2790 if (__n_move != 0)
2791 traits_type::move(__p + __pos + __n, __p + __pos, __n_move);
2792 }
2793 else
2794 {
2795 __grow_by(__cap, __sz + __n - __cap, __sz, __pos, 0, __n);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002796 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002797 }
2798 traits_type::assign(__p + __pos, __n, __c);
2799 __sz += __n;
2800 __set_size(__sz);
2801 traits_type::assign(__p[__sz], value_type());
2802 }
2803 return *this;
2804}
2805
2806template <class _CharT, class _Traits, class _Allocator>
2807template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002808__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002809<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002810 __is_exactly_cpp17_input_iterator<_InputIterator>::value,
Marshall Clow039b2f02016-01-13 21:54:34 +00002811 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002812>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002813basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _InputIterator __first, _InputIterator __last)
2814{
Nikolas Klausereed25832021-12-15 01:32:30 +01002815 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2816 "string::insert(iterator, range) called with an iterator not"
2817 " referring to this string");
2818 const basic_string __temp(__first, __last, __alloc());
2819 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002820}
2821
2822template <class _CharT, class _Traits, class _Allocator>
2823template<class _ForwardIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002824__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00002825<
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002826 __is_cpp17_forward_iterator<_ForwardIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002827 typename basic_string<_CharT, _Traits, _Allocator>::iterator
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002828>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002829basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, _ForwardIterator __first, _ForwardIterator __last)
2830{
Nikolas Klausereed25832021-12-15 01:32:30 +01002831 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2832 "string::insert(iterator, range) called with an iterator not"
2833 " referring to this string");
2834
Howard Hinnantc51e1022010-05-11 19:42:16 +00002835 size_type __ip = static_cast<size_type>(__pos - begin());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002836 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002837 if (__n)
2838 {
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002839 if (__string_is_trivial_iterator<_ForwardIterator>::value &&
2840 !__addr_in_range(*__first))
2841 {
2842 size_type __sz = size();
2843 size_type __cap = capacity();
2844 value_type* __p;
2845 if (__cap - __sz >= __n)
2846 {
2847 __p = _VSTD::__to_address(__get_pointer());
2848 size_type __n_move = __sz - __ip;
2849 if (__n_move != 0)
2850 traits_type::move(__p + __ip + __n, __p + __ip, __n_move);
2851 }
2852 else
2853 {
2854 __grow_by(__cap, __sz + __n - __cap, __sz, __ip, 0, __n);
2855 __p = _VSTD::__to_address(__get_long_pointer());
2856 }
2857 __sz += __n;
2858 __set_size(__sz);
2859 traits_type::assign(__p[__sz], value_type());
Arthur O'Dwyer80dbcbe2021-09-07 21:35:37 -04002860 for (__p += __ip; __first != __last; ++__p, (void) ++__first)
Arthur O'Dwyer98ff31f2021-04-16 17:49:57 -04002861 traits_type::assign(*__p, *__first);
2862 }
2863 else
Marshall Clow958362f2016-09-05 01:54:30 +00002864 {
2865 const basic_string __temp(__first, __last, __alloc());
2866 return insert(__pos, __temp.data(), __temp.data() + __temp.size());
2867 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00002868 }
2869 return begin() + __ip;
2870}
2871
2872template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002873inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002874basic_string<_CharT, _Traits, _Allocator>&
2875basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str)
2876{
2877 return insert(__pos1, __str.data(), __str.size());
2878}
2879
2880template <class _CharT, class _Traits, class _Allocator>
2881basic_string<_CharT, _Traits, _Allocator>&
2882basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const basic_string& __str,
2883 size_type __pos2, size_type __n)
2884{
2885 size_type __str_sz = __str.size();
2886 if (__pos2 > __str_sz)
2887 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002888 return insert(__pos1, __str.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002889}
2890
2891template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00002892template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04002893__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00002894<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002895 __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 +00002896 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05002897>
Marshall Clow82513342016-09-24 22:45:42 +00002898basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002899 size_type __pos2, size_type __n)
2900{
Marshall Clow82513342016-09-24 22:45:42 +00002901 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00002902 size_type __str_sz = __sv.size();
2903 if (__pos2 > __str_sz)
2904 this->__throw_out_of_range();
2905 return insert(__pos1, __sv.data() + __pos2, _VSTD::min(__n, __str_sz - __pos2));
2906}
2907
2908template <class _CharT, class _Traits, class _Allocator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00002909basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002910basic_string<_CharT, _Traits, _Allocator>::insert(size_type __pos, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002911{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002912 _LIBCPP_ASSERT(__s != nullptr, "string::insert received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002913 return insert(__pos, __s, traits_type::length(__s));
2914}
2915
2916template <class _CharT, class _Traits, class _Allocator>
2917typename basic_string<_CharT, _Traits, _Allocator>::iterator
2918basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, value_type __c)
2919{
2920 size_type __ip = static_cast<size_type>(__pos - begin());
2921 size_type __sz = size();
2922 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00002923 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002924 if (__cap == __sz)
2925 {
2926 __grow_by(__cap, 1, __sz, __ip, 0, 1);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002927 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002928 }
2929 else
2930 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002931 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002932 size_type __n_move = __sz - __ip;
2933 if (__n_move != 0)
2934 traits_type::move(__p + __ip + 1, __p + __ip, __n_move);
2935 }
2936 traits_type::assign(__p[__ip], __c);
2937 traits_type::assign(__p[++__sz], value_type());
2938 __set_size(__sz);
2939 return begin() + static_cast<difference_type>(__ip);
2940}
2941
2942template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00002943inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00002944typename basic_string<_CharT, _Traits, _Allocator>::iterator
2945basic_string<_CharT, _Traits, _Allocator>::insert(const_iterator __pos, size_type __n, value_type __c)
2946{
Nikolas Klausereed25832021-12-15 01:32:30 +01002947 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
2948 "string::insert(iterator, n, value) called with an iterator not"
2949 " referring to this string");
2950 difference_type __p = __pos - begin();
2951 insert(static_cast<size_type>(__p), __n, __c);
2952 return begin() + __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002953}
2954
2955// replace
2956
2957template <class _CharT, class _Traits, class _Allocator>
2958basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00002959basic_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 +00002960 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
Howard Hinnantc51e1022010-05-11 19:42:16 +00002961{
Alp Tokerb8a95f52014-05-15 11:27:39 +00002962 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002963 size_type __sz = size();
2964 if (__pos > __sz)
2965 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002966 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002967 size_type __cap = capacity();
2968 if (__cap - __sz + __n1 >= __n2)
2969 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05002970 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002971 if (__n1 != __n2)
2972 {
2973 size_type __n_move = __sz - __pos - __n1;
2974 if (__n_move != 0)
2975 {
2976 if (__n1 > __n2)
2977 {
2978 traits_type::move(__p + __pos, __s, __n2);
2979 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002980 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002981 }
2982 if (__p + __pos < __s && __s < __p + __sz)
2983 {
2984 if (__p + __pos + __n1 <= __s)
2985 __s += __n2 - __n1;
2986 else // __p + __pos < __s < __p + __pos + __n1
2987 {
2988 traits_type::move(__p + __pos, __s, __n1);
2989 __pos += __n1;
2990 __s += __n2;
2991 __n2 -= __n1;
2992 __n1 = 0;
2993 }
2994 }
2995 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
2996 }
2997 }
2998 traits_type::move(__p + __pos, __s, __n2);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01002999 return __null_terminate_at(__p, __sz + (__n2 - __n1));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003000 }
3001 else
3002 __grow_by_and_replace(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2, __s);
3003 return *this;
3004}
3005
3006template <class _CharT, class _Traits, class _Allocator>
3007basic_string<_CharT, _Traits, _Allocator>&
3008basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, size_type __n2, value_type __c)
3009{
3010 size_type __sz = size();
3011 if (__pos > __sz)
3012 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003013 __n1 = _VSTD::min(__n1, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003014 size_type __cap = capacity();
Howard Hinnantd17880b2013-06-28 16:59:19 +00003015 value_type* __p;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003016 if (__cap - __sz + __n1 >= __n2)
3017 {
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003018 __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003019 if (__n1 != __n2)
3020 {
3021 size_type __n_move = __sz - __pos - __n1;
3022 if (__n_move != 0)
3023 traits_type::move(__p + __pos + __n2, __p + __pos + __n1, __n_move);
3024 }
3025 }
3026 else
3027 {
3028 __grow_by(__cap, __sz - __n1 + __n2 - __cap, __sz, __pos, __n1, __n2);
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003029 __p = _VSTD::__to_address(__get_long_pointer());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003030 }
3031 traits_type::assign(__p + __pos, __n2, __c);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003032 return __null_terminate_at(__p, __sz - (__n1 - __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003033}
3034
3035template <class _CharT, class _Traits, class _Allocator>
3036template<class _InputIterator>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003037__enable_if_t
Howard Hinnantc51e1022010-05-11 19:42:16 +00003038<
Eric Fiseliercd5a6772019-11-18 01:46:58 -05003039 __is_cpp17_input_iterator<_InputIterator>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003040 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003041>
Howard Hinnant990d6e82010-11-17 21:11:40 +00003042basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2,
Howard Hinnantc51e1022010-05-11 19:42:16 +00003043 _InputIterator __j1, _InputIterator __j2)
3044{
Marshall Clow958362f2016-09-05 01:54:30 +00003045 const basic_string __temp(__j1, __j2, __alloc());
Marshall Clow039b2f02016-01-13 21:54:34 +00003046 return this->replace(__i1, __i2, __temp);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003047}
3048
3049template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003050inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003051basic_string<_CharT, _Traits, _Allocator>&
3052basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str)
3053{
3054 return replace(__pos1, __n1, __str.data(), __str.size());
3055}
3056
3057template <class _CharT, class _Traits, class _Allocator>
3058basic_string<_CharT, _Traits, _Allocator>&
3059basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const basic_string& __str,
3060 size_type __pos2, size_type __n2)
3061{
3062 size_type __str_sz = __str.size();
3063 if (__pos2 > __str_sz)
3064 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003065 return replace(__pos1, __n1, __str.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003066}
3067
3068template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003069template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003070__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003071<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003072 __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 +00003073 basic_string<_CharT, _Traits, _Allocator>&
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003074>
Marshall Clow82513342016-09-24 22:45:42 +00003075basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos1, size_type __n1, const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003076 size_type __pos2, size_type __n2)
3077{
Marshall Clow82513342016-09-24 22:45:42 +00003078 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003079 size_type __str_sz = __sv.size();
3080 if (__pos2 > __str_sz)
3081 this->__throw_out_of_range();
3082 return replace(__pos1, __n1, __sv.data() + __pos2, _VSTD::min(__n2, __str_sz - __pos2));
3083}
3084
3085template <class _CharT, class _Traits, class _Allocator>
3086basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003087basic_string<_CharT, _Traits, _Allocator>::replace(size_type __pos, size_type __n1, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003088{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003089 _LIBCPP_ASSERT(__s != nullptr, "string::replace received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003090 return replace(__pos, __n1, __s, traits_type::length(__s));
3091}
3092
3093template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003094inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003095basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003096basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const basic_string& __str)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003097{
3098 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1),
3099 __str.data(), __str.size());
3100}
3101
3102template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003103inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003104basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003105basic_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 +00003106{
3107 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s, __n);
3108}
3109
3110template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003111inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003112basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnantd17880b2013-06-28 16:59:19 +00003113basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, const value_type* __s)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003114{
3115 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __s);
3116}
3117
3118template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003119inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003120basic_string<_CharT, _Traits, _Allocator>&
Howard Hinnant990d6e82010-11-17 21:11:40 +00003121basic_string<_CharT, _Traits, _Allocator>::replace(const_iterator __i1, const_iterator __i2, size_type __n, value_type __c)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003122{
3123 return replace(static_cast<size_type>(__i1 - begin()), static_cast<size_type>(__i2 - __i1), __n, __c);
3124}
3125
3126// erase
3127
Martijn Velsa81fc792020-02-26 13:25:43 -05003128// 'externally instantiated' erase() implementation, called when __n != npos.
3129// Does not check __pos against size()
Howard Hinnantc51e1022010-05-11 19:42:16 +00003130template <class _CharT, class _Traits, class _Allocator>
Martijn Velsa81fc792020-02-26 13:25:43 -05003131void
3132basic_string<_CharT, _Traits, _Allocator>::__erase_external_with_move(
3133 size_type __pos, size_type __n)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003134{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003135 if (__n)
3136 {
Martijn Velsa81fc792020-02-26 13:25:43 -05003137 size_type __sz = size();
Eric Fiselierc1b87a72019-11-16 17:13:26 -05003138 value_type* __p = _VSTD::__to_address(__get_pointer());
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003139 __n = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003140 size_type __n_move = __sz - __pos - __n;
3141 if (__n_move != 0)
3142 traits_type::move(__p + __pos, __p + __pos + __n, __n_move);
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003143 __null_terminate_at(__p, __sz - __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003144 }
Martijn Velsa81fc792020-02-26 13:25:43 -05003145}
3146
3147template <class _CharT, class _Traits, class _Allocator>
3148basic_string<_CharT, _Traits, _Allocator>&
3149basic_string<_CharT, _Traits, _Allocator>::erase(size_type __pos,
3150 size_type __n) {
3151 if (__pos > size()) this->__throw_out_of_range();
3152 if (__n == npos) {
3153 __erase_to_end(__pos);
3154 } else {
3155 __erase_external_with_move(__pos, __n);
3156 }
3157 return *this;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003158}
3159
3160template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003161inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003162typename basic_string<_CharT, _Traits, _Allocator>::iterator
3163basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __pos)
3164{
Nikolas Klausereed25832021-12-15 01:32:30 +01003165 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__pos) == this,
3166 "string::erase(iterator) called with an iterator not"
3167 " referring to this string");
3168
3169 _LIBCPP_ASSERT(__pos != end(), "string::erase(iterator) called with a non-dereferenceable iterator");
3170 iterator __b = begin();
3171 size_type __r = static_cast<size_type>(__pos - __b);
3172 erase(__r, 1);
3173 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003174}
3175
3176template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003177inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003178typename basic_string<_CharT, _Traits, _Allocator>::iterator
3179basic_string<_CharT, _Traits, _Allocator>::erase(const_iterator __first, const_iterator __last)
3180{
Nikolas Klausereed25832021-12-15 01:32:30 +01003181 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
3182 "string::erase(iterator, iterator) called with an iterator not"
3183 " referring to this string");
3184
3185 _LIBCPP_ASSERT(__first <= __last, "string::erase(first, last) called with invalid range");
3186 iterator __b = begin();
3187 size_type __r = static_cast<size_type>(__first - __b);
3188 erase(__r, static_cast<size_type>(__last - __first));
3189 return __b + static_cast<difference_type>(__r);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003190}
3191
3192template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003193inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003194void
3195basic_string<_CharT, _Traits, _Allocator>::pop_back()
3196{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003197 _LIBCPP_ASSERT(!empty(), "string::pop_back(): string is already empty");
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003198 __erase_to_end(size() - 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003199}
3200
3201template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003202inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003203void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003204basic_string<_CharT, _Traits, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003205{
3206 __invalidate_all_iterators();
3207 if (__is_long())
3208 {
3209 traits_type::assign(*__get_long_pointer(), value_type());
3210 __set_long_size(0);
3211 }
3212 else
3213 {
3214 traits_type::assign(*__get_short_pointer(), value_type());
3215 __set_short_size(0);
3216 }
3217}
3218
3219template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003220inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003221void
3222basic_string<_CharT, _Traits, _Allocator>::__erase_to_end(size_type __pos)
3223{
Nikolas Klauser3ec7fb22021-12-14 01:20:53 +01003224 __null_terminate_at(_VSTD::__to_address(__get_pointer()), __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003225}
3226
3227template <class _CharT, class _Traits, class _Allocator>
3228void
3229basic_string<_CharT, _Traits, _Allocator>::resize(size_type __n, value_type __c)
3230{
3231 size_type __sz = size();
3232 if (__n > __sz)
3233 append(__n - __sz, __c);
3234 else
3235 __erase_to_end(__n);
3236}
3237
3238template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier451d5582018-11-26 20:15:38 +00003239inline void
3240basic_string<_CharT, _Traits, _Allocator>::__resize_default_init(size_type __n)
3241{
3242 size_type __sz = size();
3243 if (__n > __sz) {
3244 __append_default_init(__n - __sz);
3245 } else
3246 __erase_to_end(__n);
3247}
3248
3249template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003250inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003251typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003252basic_string<_CharT, _Traits, _Allocator>::max_size() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003253{
Howard Hinnantea8f7e12010-11-17 17:55:08 +00003254 size_type __m = __alloc_traits::max_size(__alloc());
Eric Fiseliere9cc5922017-10-17 13:16:01 +00003255#ifdef _LIBCPP_BIG_ENDIAN
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003256 return (__m <= ~__long_mask ? __m : __m/2) - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003257#else
Marshall Clow6dd6cb72013-10-31 17:23:08 +00003258 return __m - __alignment;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003259#endif
3260}
3261
3262template <class _CharT, class _Traits, class _Allocator>
3263void
Marek Kurdejc9848142020-11-26 10:07:16 +01003264basic_string<_CharT, _Traits, _Allocator>::reserve(size_type __requested_capacity)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003265{
Marek Kurdejc9848142020-11-26 10:07:16 +01003266 if (__requested_capacity > max_size())
Howard Hinnantc51e1022010-05-11 19:42:16 +00003267 this->__throw_length_error();
Marek Kurdejc9848142020-11-26 10:07:16 +01003268
3269#if _LIBCPP_STD_VER > 17
3270 // Reserve never shrinks as of C++20.
3271 if (__requested_capacity <= capacity()) return;
3272#endif
3273
3274 size_type __target_capacity = _VSTD::max(__requested_capacity, size());
3275 __target_capacity = __recommend(__target_capacity);
3276 if (__target_capacity == capacity()) return;
3277
3278 __shrink_or_extend(__target_capacity);
3279}
3280
3281template <class _CharT, class _Traits, class _Allocator>
Louis Dionnee38b8642021-12-13 14:15:21 -05003282inline
Marek Kurdejc9848142020-11-26 10:07:16 +01003283void
3284basic_string<_CharT, _Traits, _Allocator>::shrink_to_fit() _NOEXCEPT
3285{
3286 size_type __target_capacity = __recommend(size());
3287 if (__target_capacity == capacity()) return;
3288
3289 __shrink_or_extend(__target_capacity);
3290}
3291
3292template <class _CharT, class _Traits, class _Allocator>
Louis Dionnee38b8642021-12-13 14:15:21 -05003293inline
Marek Kurdejc9848142020-11-26 10:07:16 +01003294void
3295basic_string<_CharT, _Traits, _Allocator>::__shrink_or_extend(size_type __target_capacity)
3296{
Howard Hinnantc51e1022010-05-11 19:42:16 +00003297 size_type __cap = capacity();
3298 size_type __sz = size();
Marek Kurdejc9848142020-11-26 10:07:16 +01003299
3300 pointer __new_data, __p;
3301 bool __was_long, __now_long;
3302 if (__target_capacity == __min_cap - 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003303 {
Marek Kurdejc9848142020-11-26 10:07:16 +01003304 __was_long = true;
3305 __now_long = false;
3306 __new_data = __get_short_pointer();
3307 __p = __get_long_pointer();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003308 }
Marek Kurdejc9848142020-11-26 10:07:16 +01003309 else
3310 {
3311 if (__target_capacity > __cap)
3312 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3313 else
3314 {
3315 #ifndef _LIBCPP_NO_EXCEPTIONS
3316 try
3317 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003318 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003319 __new_data = __alloc_traits::allocate(__alloc(), __target_capacity+1);
3320 #ifndef _LIBCPP_NO_EXCEPTIONS
3321 }
3322 catch (...)
3323 {
3324 return;
3325 }
3326 #else // _LIBCPP_NO_EXCEPTIONS
3327 if (__new_data == nullptr)
3328 return;
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04003329 #endif // _LIBCPP_NO_EXCEPTIONS
Marek Kurdejc9848142020-11-26 10:07:16 +01003330 }
3331 __now_long = true;
3332 __was_long = __is_long();
3333 __p = __get_pointer();
3334 }
3335 traits_type::copy(_VSTD::__to_address(__new_data),
3336 _VSTD::__to_address(__p), size()+1);
3337 if (__was_long)
3338 __alloc_traits::deallocate(__alloc(), __p, __cap+1);
3339 if (__now_long)
3340 {
3341 __set_long_cap(__target_capacity+1);
3342 __set_long_size(__sz);
3343 __set_long_pointer(__new_data);
3344 }
3345 else
3346 __set_short_size(__sz);
3347 __invalidate_all_iterators();
Howard Hinnantc51e1022010-05-11 19:42:16 +00003348}
3349
3350template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003351inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003352typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003353basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003354{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003355 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003356 return *(data() + __pos);
3357}
3358
3359template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003360inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003361typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003362basic_string<_CharT, _Traits, _Allocator>::operator[](size_type __pos) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003363{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003364 _LIBCPP_ASSERT(__pos <= size(), "string index out of bounds");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003365 return *(__get_pointer() + __pos);
3366}
3367
3368template <class _CharT, class _Traits, class _Allocator>
3369typename basic_string<_CharT, _Traits, _Allocator>::const_reference
3370basic_string<_CharT, _Traits, _Allocator>::at(size_type __n) const
3371{
3372 if (__n >= size())
3373 this->__throw_out_of_range();
3374 return (*this)[__n];
3375}
3376
3377template <class _CharT, class _Traits, class _Allocator>
3378typename basic_string<_CharT, _Traits, _Allocator>::reference
3379basic_string<_CharT, _Traits, _Allocator>::at(size_type __n)
3380{
3381 if (__n >= size())
3382 this->__throw_out_of_range();
3383 return (*this)[__n];
3384}
3385
3386template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003387inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003388typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003389basic_string<_CharT, _Traits, _Allocator>::front() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003390{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003391 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003392 return *__get_pointer();
3393}
3394
3395template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003396inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003397typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003398basic_string<_CharT, _Traits, _Allocator>::front() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003399{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003400 _LIBCPP_ASSERT(!empty(), "string::front(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003401 return *data();
3402}
3403
3404template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003405inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003406typename basic_string<_CharT, _Traits, _Allocator>::reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003407basic_string<_CharT, _Traits, _Allocator>::back() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003408{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003409 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003410 return *(__get_pointer() + size() - 1);
3411}
3412
3413template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003414inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003415typename basic_string<_CharT, _Traits, _Allocator>::const_reference
Marshall Clow05cf6692019-03-19 03:30:07 +00003416basic_string<_CharT, _Traits, _Allocator>::back() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003417{
Howard Hinnant8ea98242013-08-23 17:37:05 +00003418 _LIBCPP_ASSERT(!empty(), "string::back(): string is empty");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003419 return *(data() + size() - 1);
3420}
3421
3422template <class _CharT, class _Traits, class _Allocator>
3423typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003424basic_string<_CharT, _Traits, _Allocator>::copy(value_type* __s, size_type __n, size_type __pos) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003425{
3426 size_type __sz = size();
3427 if (__pos > __sz)
3428 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003429 size_type __rlen = _VSTD::min(__n, __sz - __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003430 traits_type::copy(__s, data() + __pos, __rlen);
3431 return __rlen;
3432}
3433
3434template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003435inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003436basic_string<_CharT, _Traits, _Allocator>
3437basic_string<_CharT, _Traits, _Allocator>::substr(size_type __pos, size_type __n) const
3438{
3439 return basic_string(*this, __pos, __n, __alloc());
3440}
3441
3442template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003443inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003444void
3445basic_string<_CharT, _Traits, _Allocator>::swap(basic_string& __str)
Marshall Clow8982dcd2015-07-13 20:04:56 +00003446#if _LIBCPP_STD_VER >= 14
Eric Fiselier873b8d32019-03-18 21:50:12 +00003447 _NOEXCEPT
Marshall Clow8982dcd2015-07-13 20:04:56 +00003448#else
Eric Fiselier873b8d32019-03-18 21:50:12 +00003449 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
Marshall Clow8982dcd2015-07-13 20:04:56 +00003450 __is_nothrow_swappable<allocator_type>::value)
3451#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00003452{
Louis Dionneba400782020-10-02 15:02:52 -04003453#if _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +01003454 if (!__libcpp_is_constant_evaluated()) {
3455 if (!__is_long())
3456 __get_db()->__invalidate_all(this);
3457 if (!__str.__is_long())
3458 __get_db()->__invalidate_all(&__str);
3459 __get_db()->swap(this, &__str);
3460 }
Howard Hinnant8ea98242013-08-23 17:37:05 +00003461#endif
Eric Fiselier9bf691f2016-12-28 05:53:01 +00003462 _LIBCPP_ASSERT(
3463 __alloc_traits::propagate_on_container_swap::value ||
3464 __alloc_traits::is_always_equal::value ||
3465 __alloc() == __str.__alloc(), "swapping non-equal allocators");
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003466 _VSTD::swap(__r_.first(), __str.__r_.first());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05003467 _VSTD::__swap_allocator(__alloc(), __str.__alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003468}
3469
3470// find
3471
3472template <class _Traits>
3473struct _LIBCPP_HIDDEN __traits_eq
3474{
3475 typedef typename _Traits::char_type char_type;
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003476 _LIBCPP_INLINE_VISIBILITY
3477 bool operator()(const char_type& __x, const char_type& __y) _NOEXCEPT
3478 {return _Traits::eq(__x, __y);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00003479};
3480
3481template<class _CharT, class _Traits, class _Allocator>
3482typename 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,
3485 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003486{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003487 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003488 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003489 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003490}
3491
3492template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003493inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003494typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003495basic_string<_CharT, _Traits, _Allocator>::find(const basic_string& __str,
3496 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003497{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003498 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003499 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003500}
3501
3502template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003503template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003504__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003505<
3506 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3507 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003508>
Marshall Clowe46031a2018-07-02 18:41:15 +00003509basic_string<_CharT, _Traits, _Allocator>::find(const _Tp &__t,
zoecarver1997e0a2021-02-05 11:54:47 -08003510 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003511{
Marshall Clowe46031a2018-07-02 18:41:15 +00003512 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003513 return __str_find<value_type, size_type, traits_type, npos>
3514 (data(), size(), __sv.data(), __pos, __sv.size());
3515}
3516
3517template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003518inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003519typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003520basic_string<_CharT, _Traits, _Allocator>::find(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003521 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003522{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003523 _LIBCPP_ASSERT(__s != nullptr, "string::find(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003524 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003525 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003526}
3527
3528template<class _CharT, class _Traits, class _Allocator>
3529typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003530basic_string<_CharT, _Traits, _Allocator>::find(value_type __c,
3531 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003532{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003533 return __str_find<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003534 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003535}
3536
3537// rfind
3538
3539template<class _CharT, class _Traits, class _Allocator>
3540typename 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,
3543 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003544{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003545 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003546 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003547 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003548}
3549
3550template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003551inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003552typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003553basic_string<_CharT, _Traits, _Allocator>::rfind(const basic_string& __str,
3554 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003555{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003556 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003557 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003558}
3559
3560template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003561template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003562__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003563<
3564 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3565 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003566>
Marshall Clowe46031a2018-07-02 18:41:15 +00003567basic_string<_CharT, _Traits, _Allocator>::rfind(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003568 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003569{
Marshall Clowe46031a2018-07-02 18:41:15 +00003570 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003571 return __str_rfind<value_type, size_type, traits_type, npos>
3572 (data(), size(), __sv.data(), __pos, __sv.size());
3573}
3574
3575template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003576inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003577typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003578basic_string<_CharT, _Traits, _Allocator>::rfind(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003579 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003580{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003581 _LIBCPP_ASSERT(__s != nullptr, "string::rfind(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003582 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003583 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003584}
3585
3586template<class _CharT, class _Traits, class _Allocator>
3587typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003588basic_string<_CharT, _Traits, _Allocator>::rfind(value_type __c,
3589 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003590{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003591 return __str_rfind<value_type, size_type, traits_type, npos>
Marshall Clowc527b6e2014-06-02 02:22:49 +00003592 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003593}
3594
3595// find_first_of
3596
3597template<class _CharT, class _Traits, class _Allocator>
3598typename 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,
3601 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003602{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003603 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003604 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003605 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003606}
3607
3608template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003609inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003610typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003611basic_string<_CharT, _Traits, _Allocator>::find_first_of(const basic_string& __str,
3612 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003613{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003614 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003615 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003616}
3617
3618template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003619template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003620__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003621<
3622 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3623 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003624>
Marshall Clowe46031a2018-07-02 18:41:15 +00003625basic_string<_CharT, _Traits, _Allocator>::find_first_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003626 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003627{
Marshall Clowe46031a2018-07-02 18:41:15 +00003628 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003629 return __str_find_first_of<value_type, size_type, traits_type, npos>
3630 (data(), size(), __sv.data(), __pos, __sv.size());
3631}
3632
3633template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003634inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003635typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003636basic_string<_CharT, _Traits, _Allocator>::find_first_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003637 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003638{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003639 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003640 return __str_find_first_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003641 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003642}
3643
3644template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003645inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003646typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003647basic_string<_CharT, _Traits, _Allocator>::find_first_of(value_type __c,
3648 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003649{
3650 return find(__c, __pos);
3651}
3652
3653// find_last_of
3654
3655template<class _CharT, class _Traits, class _Allocator>
3656typename 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,
3659 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003660{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003661 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003662 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003663 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003664}
3665
3666template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003667inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003668typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003669basic_string<_CharT, _Traits, _Allocator>::find_last_of(const basic_string& __str,
3670 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003671{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003672 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003673 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003674}
3675
3676template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003677template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003678__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003679<
3680 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3681 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003682>
Marshall Clowe46031a2018-07-02 18:41:15 +00003683basic_string<_CharT, _Traits, _Allocator>::find_last_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003684 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003685{
Marshall Clowe46031a2018-07-02 18:41:15 +00003686 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003687 return __str_find_last_of<value_type, size_type, traits_type, npos>
3688 (data(), size(), __sv.data(), __pos, __sv.size());
3689}
3690
3691template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003692inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003693typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003694basic_string<_CharT, _Traits, _Allocator>::find_last_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003695 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003696{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003697 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003698 return __str_find_last_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003699 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003700}
3701
3702template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003703inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003704typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003705basic_string<_CharT, _Traits, _Allocator>::find_last_of(value_type __c,
3706 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003707{
3708 return rfind(__c, __pos);
3709}
3710
3711// find_first_not_of
3712
3713template<class _CharT, class _Traits, class _Allocator>
3714typename 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,
3717 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003718{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003719 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003720 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003721 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003722}
3723
3724template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003725inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003726typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003727basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const basic_string& __str,
3728 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003729{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003730 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003731 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003732}
3733
3734template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003735template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003736__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003737<
3738 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3739 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003740>
Marshall Clowe46031a2018-07-02 18:41:15 +00003741basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003742 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003743{
Marshall Clowe46031a2018-07-02 18:41:15 +00003744 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003745 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
3746 (data(), size(), __sv.data(), __pos, __sv.size());
3747}
3748
3749template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003750inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003751typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003752basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003753 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003754{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003755 _LIBCPP_ASSERT(__s != nullptr, "string::find_first_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003756 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003757 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003758}
3759
3760template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003761inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003762typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003763basic_string<_CharT, _Traits, _Allocator>::find_first_not_of(value_type __c,
3764 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003765{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003766 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003767 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003768}
3769
3770// find_last_not_of
3771
3772template<class _CharT, class _Traits, class _Allocator>
3773typename 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,
3776 size_type __n) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003777{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003778 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003779 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003780 (data(), size(), __s, __pos, __n);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003781}
3782
3783template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003784inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003785typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003786basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const basic_string& __str,
3787 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003788{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003789 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003790 (data(), size(), __str.data(), __pos, __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00003791}
3792
3793template<class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003794template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003795__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003796<
3797 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3798 typename basic_string<_CharT, _Traits, _Allocator>::size_type
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003799>
Marshall Clowe46031a2018-07-02 18:41:15 +00003800basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const _Tp& __t,
zoecarver1997e0a2021-02-05 11:54:47 -08003801 size_type __pos) const _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003802{
Marshall Clowe46031a2018-07-02 18:41:15 +00003803 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003804 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
3805 (data(), size(), __sv.data(), __pos, __sv.size());
3806}
3807
3808template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003809inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003810typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantd17880b2013-06-28 16:59:19 +00003811basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003812 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003813{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003814 _LIBCPP_ASSERT(__s != nullptr, "string::find_last_not_of(): received nullptr");
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003815 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003816 (data(), size(), __s, __pos, traits_type::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003817}
3818
3819template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003820inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003821typename basic_string<_CharT, _Traits, _Allocator>::size_type
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003822basic_string<_CharT, _Traits, _Allocator>::find_last_not_of(value_type __c,
3823 size_type __pos) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003824{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003825 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
Marshall Clow4bc2be22014-02-16 01:57:26 +00003826 (data(), size(), __c, __pos);
Howard Hinnantc51e1022010-05-11 19:42:16 +00003827}
3828
3829// compare
3830
3831template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003832template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003833__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003834<
3835 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3836 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003837>
zoecarver1997e0a2021-02-05 11:54:47 -08003838basic_string<_CharT, _Traits, _Allocator>::compare(const _Tp& __t) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003839{
Marshall Clowe46031a2018-07-02 18:41:15 +00003840 __self_view __sv = __t;
Howard Hinnantb0485532011-07-24 21:45:06 +00003841 size_t __lhs_sz = size();
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003842 size_t __rhs_sz = __sv.size();
3843 int __result = traits_type::compare(data(), __sv.data(),
Howard Hinnantb0485532011-07-24 21:45:06 +00003844 _VSTD::min(__lhs_sz, __rhs_sz));
3845 if (__result != 0)
3846 return __result;
3847 if (__lhs_sz < __rhs_sz)
3848 return -1;
3849 if (__lhs_sz > __rhs_sz)
3850 return 1;
3851 return 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00003852}
3853
3854template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003855inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003856int
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003857basic_string<_CharT, _Traits, _Allocator>::compare(const basic_string& __str) const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003858{
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003859 return compare(__self_view(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003860}
3861
3862template <class _CharT, class _Traits, class _Allocator>
3863int
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003864basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3865 size_type __n1,
Howard Hinnantd17880b2013-06-28 16:59:19 +00003866 const value_type* __s,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003867 size_type __n2) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00003868{
Alp Tokerb8a95f52014-05-15 11:27:39 +00003869 _LIBCPP_ASSERT(__n2 == 0 || __s != nullptr, "string::compare(): received nullptr");
Howard Hinnantc51e1022010-05-11 19:42:16 +00003870 size_type __sz = size();
3871 if (__pos1 > __sz || __n2 == npos)
3872 this->__throw_out_of_range();
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00003873 size_type __rlen = _VSTD::min(__n1, __sz - __pos1);
3874 int __r = traits_type::compare(data() + __pos1, __s, _VSTD::min(__rlen, __n2));
Howard Hinnantc51e1022010-05-11 19:42:16 +00003875 if (__r == 0)
3876 {
3877 if (__rlen < __n2)
3878 __r = -1;
3879 else if (__rlen > __n2)
3880 __r = 1;
3881 }
3882 return __r;
3883}
3884
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003885template <class _CharT, class _Traits, class _Allocator>
Marshall Clowe46031a2018-07-02 18:41:15 +00003886template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003887__enable_if_t
Marshall Clowe46031a2018-07-02 18:41:15 +00003888<
3889 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value,
3890 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003891>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003892basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3893 size_type __n1,
Marshall Clowe46031a2018-07-02 18:41:15 +00003894 const _Tp& __t) const
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003895{
Marshall Clowe46031a2018-07-02 18:41:15 +00003896 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003897 return compare(__pos1, __n1, __sv.data(), __sv.size());
3898}
3899
3900template <class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003901inline
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003902int
3903basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3904 size_type __n1,
3905 const basic_string& __str) const
3906{
3907 return compare(__pos1, __n1, __str.data(), __str.size());
3908}
3909
3910template <class _CharT, class _Traits, class _Allocator>
Marshall Clow82513342016-09-24 22:45:42 +00003911template <class _Tp>
Louis Dionne9ce598d2021-09-08 09:14:43 -04003912__enable_if_t
Marshall Clow82513342016-09-24 22:45:42 +00003913<
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003914 __can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value
3915 && !__is_same_uncvref<_Tp, basic_string<_CharT, _Traits, _Allocator> >::value,
Marshall Clowb7db4972017-11-15 20:02:27 +00003916 int
Eric Fiselierc522ceb2020-01-15 16:57:08 -05003917>
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003918basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3919 size_type __n1,
Marshall Clow82513342016-09-24 22:45:42 +00003920 const _Tp& __t,
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003921 size_type __pos2,
3922 size_type __n2) const
3923{
Marshall Clow82513342016-09-24 22:45:42 +00003924 __self_view __sv = __t;
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003925 return __self_view(*this).substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3926}
3927
3928template <class _CharT, class _Traits, class _Allocator>
3929int
3930basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3931 size_type __n1,
3932 const basic_string& __str,
3933 size_type __pos2,
3934 size_type __n2) const
3935{
3936 return compare(__pos1, __n1, __self_view(__str), __pos2, __n2);
3937}
3938
3939template <class _CharT, class _Traits, class _Allocator>
3940int
3941basic_string<_CharT, _Traits, _Allocator>::compare(const value_type* __s) const _NOEXCEPT
3942{
3943 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3944 return compare(0, npos, __s, traits_type::length(__s));
3945}
3946
3947template <class _CharT, class _Traits, class _Allocator>
3948int
3949basic_string<_CharT, _Traits, _Allocator>::compare(size_type __pos1,
3950 size_type __n1,
3951 const value_type* __s) const
3952{
3953 _LIBCPP_ASSERT(__s != nullptr, "string::compare(): received nullptr");
3954 return compare(__pos1, __n1, __s, traits_type::length(__s));
3955}
3956
Howard Hinnantc51e1022010-05-11 19:42:16 +00003957// __invariants
3958
3959template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003960inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00003961bool
3962basic_string<_CharT, _Traits, _Allocator>::__invariants() const
3963{
3964 if (size() > capacity())
3965 return false;
3966 if (capacity() < __min_cap - 1)
3967 return false;
Bruce Mitchener170d8972020-11-24 12:53:53 -05003968 if (data() == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00003969 return false;
Louis Dionne663415f2020-10-05 16:16:13 -04003970 if (data()[size()] != value_type())
Howard Hinnantc51e1022010-05-11 19:42:16 +00003971 return false;
3972 return true;
3973}
3974
Vedant Kumar55e007e2018-03-08 21:15:26 +00003975// __clear_and_shrink
3976
3977template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00003978inline
Louis Dionne173f29e2019-05-29 16:01:36 +00003979void
Marshall Clowe60a7182018-05-29 17:04:37 +00003980basic_string<_CharT, _Traits, _Allocator>::__clear_and_shrink() _NOEXCEPT
Vedant Kumar55e007e2018-03-08 21:15:26 +00003981{
3982 clear();
3983 if(__is_long())
3984 {
3985 __alloc_traits::deallocate(__alloc(), __get_long_pointer(), capacity() + 1);
3986 __set_long_cap(0);
3987 __set_short_size(0);
Louis Dionne663415f2020-10-05 16:16:13 -04003988 traits_type::assign(*__get_short_pointer(), value_type());
Vedant Kumar55e007e2018-03-08 21:15:26 +00003989 }
Louis Dionne173f29e2019-05-29 16:01:36 +00003990}
Vedant Kumar55e007e2018-03-08 21:15:26 +00003991
Howard Hinnantc51e1022010-05-11 19:42:16 +00003992// operator==
3993
3994template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00003995inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00003996bool
3997operator==(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00003998 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00003999{
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004000 size_t __lhs_sz = __lhs.size();
4001 return __lhs_sz == __rhs.size() && _Traits::compare(__lhs.data(),
4002 __rhs.data(),
4003 __lhs_sz) == 0;
4004}
4005
4006template<class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004007inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaaeb1132013-04-22 23:55:13 +00004008bool
4009operator==(const basic_string<char, char_traits<char>, _Allocator>& __lhs,
4010 const basic_string<char, char_traits<char>, _Allocator>& __rhs) _NOEXCEPT
4011{
4012 size_t __lhs_sz = __lhs.size();
4013 if (__lhs_sz != __rhs.size())
4014 return false;
4015 const char* __lp = __lhs.data();
4016 const char* __rp = __rhs.data();
4017 if (__lhs.__is_long())
4018 return char_traits<char>::compare(__lp, __rp, __lhs_sz) == 0;
4019 for (; __lhs_sz != 0; --__lhs_sz, ++__lp, ++__rp)
4020 if (*__lp != *__rp)
4021 return false;
4022 return true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004023}
4024
4025template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004026inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004027bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004028operator==(const _CharT* __lhs,
4029 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004030{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004031 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4032 _LIBCPP_ASSERT(__lhs != nullptr, "operator==(char*, basic_string): received nullptr");
4033 size_t __lhs_len = _Traits::length(__lhs);
4034 if (__lhs_len != __rhs.size()) return false;
4035 return __rhs.compare(0, _String::npos, __lhs, __lhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004036}
4037
Howard Hinnantc51e1022010-05-11 19:42:16 +00004038template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004039inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004040bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004041operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
4042 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004043{
Eric Fiselier0cafa3f2015-08-28 03:02:37 +00004044 typedef basic_string<_CharT, _Traits, _Allocator> _String;
4045 _LIBCPP_ASSERT(__rhs != nullptr, "operator==(basic_string, char*): received nullptr");
4046 size_t __rhs_len = _Traits::length(__rhs);
4047 if (__rhs_len != __lhs.size()) return false;
4048 return __lhs.compare(0, _String::npos, __rhs, __rhs_len) == 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004049}
4050
Howard Hinnant3b6579a2010-08-22 00:02:43 +00004051template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004052inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004053bool
4054operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004055 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004056{
4057 return !(__lhs == __rhs);
4058}
4059
4060template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004061inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004062bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004063operator!=(const _CharT* __lhs,
4064 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004065{
4066 return !(__lhs == __rhs);
4067}
4068
4069template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004070inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004071bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004072operator!=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4073 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004074{
4075 return !(__lhs == __rhs);
4076}
4077
4078// operator<
4079
4080template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004081inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004082bool
4083operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004084 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004085{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004086 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004087}
4088
4089template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004090inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004091bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004092operator< (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4093 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004094{
Howard Hinnanta2660c12011-07-24 15:07:21 +00004095 return __lhs.compare(__rhs) < 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004096}
4097
4098template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004099inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004100bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004101operator< (const _CharT* __lhs,
4102 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004103{
4104 return __rhs.compare(__lhs) > 0;
4105}
4106
Howard Hinnantc51e1022010-05-11 19:42:16 +00004107// operator>
4108
4109template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004110inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004111bool
4112operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004113 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004114{
4115 return __rhs < __lhs;
4116}
4117
4118template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004119inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004120bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004121operator> (const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4122 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004123{
4124 return __rhs < __lhs;
4125}
4126
4127template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004128inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004129bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004130operator> (const _CharT* __lhs,
4131 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004132{
4133 return __rhs < __lhs;
4134}
4135
4136// operator<=
4137
4138template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004139inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004140bool
4141operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004142 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004143{
4144 return !(__rhs < __lhs);
4145}
4146
4147template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004148inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004149bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004150operator<=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4151 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004152{
4153 return !(__rhs < __lhs);
4154}
4155
4156template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004157inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004158bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004159operator<=(const _CharT* __lhs,
4160 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004161{
4162 return !(__rhs < __lhs);
4163}
4164
4165// operator>=
4166
4167template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004168inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004169bool
4170operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004171 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004172{
4173 return !(__lhs < __rhs);
4174}
4175
4176template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004177inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004178bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004179operator>=(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4180 const _CharT* __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004181{
4182 return !(__lhs < __rhs);
4183}
4184
4185template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004186inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004187bool
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004188operator>=(const _CharT* __lhs,
4189 const basic_string<_CharT, _Traits, _Allocator>& __rhs) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00004190{
4191 return !(__lhs < __rhs);
4192}
4193
4194// operator +
4195
4196template<class _CharT, class _Traits, class _Allocator>
4197basic_string<_CharT, _Traits, _Allocator>
4198operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs,
4199 const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4200{
4201 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4202 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4203 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4204 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4205 __r.append(__rhs.data(), __rhs_sz);
4206 return __r;
4207}
4208
4209template<class _CharT, class _Traits, class _Allocator>
4210basic_string<_CharT, _Traits, _Allocator>
4211operator+(const _CharT* __lhs , const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4212{
4213 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4214 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = _Traits::length(__lhs);
4215 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4216 __r.__init(__lhs, __lhs_sz, __lhs_sz + __rhs_sz);
4217 __r.append(__rhs.data(), __rhs_sz);
4218 return __r;
4219}
4220
4221template<class _CharT, class _Traits, class _Allocator>
4222basic_string<_CharT, _Traits, _Allocator>
4223operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Allocator>& __rhs)
4224{
4225 basic_string<_CharT, _Traits, _Allocator> __r(__rhs.get_allocator());
4226 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = __rhs.size();
4227 __r.__init(&__lhs, 1, 1 + __rhs_sz);
4228 __r.append(__rhs.data(), __rhs_sz);
4229 return __r;
4230}
4231
4232template<class _CharT, class _Traits, class _Allocator>
Eric Fiselierbea70602018-11-26 22:51:35 +00004233inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00004234basic_string<_CharT, _Traits, _Allocator>
4235operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, const _CharT* __rhs)
4236{
4237 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4238 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4239 typename basic_string<_CharT, _Traits, _Allocator>::size_type __rhs_sz = _Traits::length(__rhs);
4240 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + __rhs_sz);
4241 __r.append(__rhs, __rhs_sz);
4242 return __r;
4243}
4244
4245template<class _CharT, class _Traits, class _Allocator>
4246basic_string<_CharT, _Traits, _Allocator>
4247operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, _CharT __rhs)
4248{
4249 basic_string<_CharT, _Traits, _Allocator> __r(__lhs.get_allocator());
4250 typename basic_string<_CharT, _Traits, _Allocator>::size_type __lhs_sz = __lhs.size();
4251 __r.__init(__lhs.data(), __lhs_sz, __lhs_sz + 1);
4252 __r.push_back(__rhs);
4253 return __r;
4254}
4255
Eric Fiselierfc92be82017-04-19 00:28:44 +00004256#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004257
4258template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004259inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004260basic_string<_CharT, _Traits, _Allocator>
4261operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const basic_string<_CharT, _Traits, _Allocator>& __rhs)
4262{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004263 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004264}
4265
4266template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004267inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004268basic_string<_CharT, _Traits, _Allocator>
4269operator+(const basic_string<_CharT, _Traits, _Allocator>& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4270{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004271 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004272}
4273
4274template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004275inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004276basic_string<_CharT, _Traits, _Allocator>
4277operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, basic_string<_CharT, _Traits, _Allocator>&& __rhs)
4278{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004279 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004280}
4281
4282template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004283inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004284basic_string<_CharT, _Traits, _Allocator>
4285operator+(const _CharT* __lhs , basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4286{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004287 return _VSTD::move(__rhs.insert(0, __lhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004288}
4289
4290template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004291inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004292basic_string<_CharT, _Traits, _Allocator>
4293operator+(_CharT __lhs, basic_string<_CharT,_Traits,_Allocator>&& __rhs)
4294{
4295 __rhs.insert(__rhs.begin(), __lhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004296 return _VSTD::move(__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004297}
4298
4299template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004300inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004301basic_string<_CharT, _Traits, _Allocator>
4302operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, const _CharT* __rhs)
4303{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004304 return _VSTD::move(__lhs.append(__rhs));
Howard Hinnantc51e1022010-05-11 19:42:16 +00004305}
4306
4307template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004308inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004309basic_string<_CharT, _Traits, _Allocator>
4310operator+(basic_string<_CharT, _Traits, _Allocator>&& __lhs, _CharT __rhs)
4311{
4312 __lhs.push_back(__rhs);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00004313 return _VSTD::move(__lhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00004314}
4315
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004316#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00004317
4318// swap
4319
4320template<class _CharT, class _Traits, class _Allocator>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +00004321inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00004322void
Howard Hinnantaeb17d82011-05-29 19:57:12 +00004323swap(basic_string<_CharT, _Traits, _Allocator>& __lhs,
Howard Hinnant3e276872011-06-03 18:40:47 +00004324 basic_string<_CharT, _Traits, _Allocator>& __rhs)
4325 _NOEXCEPT_(_NOEXCEPT_(__lhs.swap(__rhs)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00004326{
4327 __lhs.swap(__rhs);
4328}
4329
Bruce Mitchener170d8972020-11-24 12:53:53 -05004330_LIBCPP_FUNC_VIS int stoi (const string& __str, size_t* __idx = nullptr, int __base = 10);
4331_LIBCPP_FUNC_VIS long stol (const string& __str, size_t* __idx = nullptr, int __base = 10);
4332_LIBCPP_FUNC_VIS unsigned long stoul (const string& __str, size_t* __idx = nullptr, int __base = 10);
4333_LIBCPP_FUNC_VIS long long stoll (const string& __str, size_t* __idx = nullptr, int __base = 10);
4334_LIBCPP_FUNC_VIS unsigned long long stoull(const string& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004335
Bruce Mitchener170d8972020-11-24 12:53:53 -05004336_LIBCPP_FUNC_VIS float stof (const string& __str, size_t* __idx = nullptr);
4337_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = nullptr);
4338_LIBCPP_FUNC_VIS long double stold(const string& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004339
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004340_LIBCPP_FUNC_VIS string to_string(int __val);
4341_LIBCPP_FUNC_VIS string to_string(unsigned __val);
4342_LIBCPP_FUNC_VIS string to_string(long __val);
4343_LIBCPP_FUNC_VIS string to_string(unsigned long __val);
4344_LIBCPP_FUNC_VIS string to_string(long long __val);
4345_LIBCPP_FUNC_VIS string to_string(unsigned long long __val);
4346_LIBCPP_FUNC_VIS string to_string(float __val);
4347_LIBCPP_FUNC_VIS string to_string(double __val);
4348_LIBCPP_FUNC_VIS string to_string(long double __val);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004349
Louis Dionne89258142021-08-23 15:32:36 -04004350#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Bruce Mitchener170d8972020-11-24 12:53:53 -05004351_LIBCPP_FUNC_VIS int stoi (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4352_LIBCPP_FUNC_VIS long stol (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4353_LIBCPP_FUNC_VIS unsigned long stoul (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4354_LIBCPP_FUNC_VIS long long stoll (const wstring& __str, size_t* __idx = nullptr, int __base = 10);
4355_LIBCPP_FUNC_VIS unsigned long long stoull(const wstring& __str, size_t* __idx = nullptr, int __base = 10);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004356
Bruce Mitchener170d8972020-11-24 12:53:53 -05004357_LIBCPP_FUNC_VIS float stof (const wstring& __str, size_t* __idx = nullptr);
4358_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = nullptr);
4359_LIBCPP_FUNC_VIS long double stold(const wstring& __str, size_t* __idx = nullptr);
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004360
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00004361_LIBCPP_FUNC_VIS wstring to_wstring(int __val);
4362_LIBCPP_FUNC_VIS wstring to_wstring(unsigned __val);
4363_LIBCPP_FUNC_VIS wstring to_wstring(long __val);
4364_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long __val);
4365_LIBCPP_FUNC_VIS wstring to_wstring(long long __val);
4366_LIBCPP_FUNC_VIS wstring to_wstring(unsigned long long __val);
4367_LIBCPP_FUNC_VIS wstring to_wstring(float __val);
4368_LIBCPP_FUNC_VIS wstring to_wstring(double __val);
4369_LIBCPP_FUNC_VIS wstring to_wstring(long double __val);
Louis Dionne89258142021-08-23 15:32:36 -04004370#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnanta5f4f8e2010-06-02 18:20:39 +00004371
Howard Hinnantc51e1022010-05-11 19:42:16 +00004372template<class _CharT, class _Traits, class _Allocator>
Martin Storsjö88890c22021-02-22 01:13:13 +02004373_LIBCPP_TEMPLATE_DATA_VIS
Eric Fiselierc9fdaf12020-01-15 17:02:17 -05004374const typename basic_string<_CharT, _Traits, _Allocator>::size_type
4375 basic_string<_CharT, _Traits, _Allocator>::npos;
Howard Hinnantc51e1022010-05-11 19:42:16 +00004376
Marshall Clow851b9ec2019-05-20 21:56:51 +00004377template <class _CharT, class _Allocator>
4378struct _LIBCPP_TEMPLATE_VIS
4379 hash<basic_string<_CharT, char_traits<_CharT>, _Allocator> >
4380 : public unary_function<
4381 basic_string<_CharT, char_traits<_CharT>, _Allocator>, size_t>
Howard Hinnantc51e1022010-05-11 19:42:16 +00004382{
4383 size_t
Marshall Clow851b9ec2019-05-20 21:56:51 +00004384 operator()(const basic_string<_CharT, char_traits<_CharT>, _Allocator>& __val) const _NOEXCEPT
4385 { return __do_string_hash(__val.data(), __val.data() + __val.size()); }
Howard Hinnantc51e1022010-05-11 19:42:16 +00004386};
4387
Howard Hinnantc51e1022010-05-11 19:42:16 +00004388
Howard Hinnantdc095972011-07-18 15:51:59 +00004389template<class _CharT, class _Traits, class _Allocator>
4390basic_ostream<_CharT, _Traits>&
4391operator<<(basic_ostream<_CharT, _Traits>& __os,
4392 const basic_string<_CharT, _Traits, _Allocator>& __str);
4393
4394template<class _CharT, class _Traits, class _Allocator>
4395basic_istream<_CharT, _Traits>&
4396operator>>(basic_istream<_CharT, _Traits>& __is,
4397 basic_string<_CharT, _Traits, _Allocator>& __str);
4398
4399template<class _CharT, class _Traits, class _Allocator>
4400basic_istream<_CharT, _Traits>&
4401getline(basic_istream<_CharT, _Traits>& __is,
4402 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4403
4404template<class _CharT, class _Traits, class _Allocator>
4405inline _LIBCPP_INLINE_VISIBILITY
4406basic_istream<_CharT, _Traits>&
4407getline(basic_istream<_CharT, _Traits>& __is,
4408 basic_string<_CharT, _Traits, _Allocator>& __str);
4409
Howard Hinnantdc095972011-07-18 15:51:59 +00004410template<class _CharT, class _Traits, class _Allocator>
4411inline _LIBCPP_INLINE_VISIBILITY
4412basic_istream<_CharT, _Traits>&
4413getline(basic_istream<_CharT, _Traits>&& __is,
4414 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm);
4415
4416template<class _CharT, class _Traits, class _Allocator>
4417inline _LIBCPP_INLINE_VISIBILITY
4418basic_istream<_CharT, _Traits>&
4419getline(basic_istream<_CharT, _Traits>&& __is,
4420 basic_string<_CharT, _Traits, _Allocator>& __str);
4421
Marshall Clow29b53f22018-12-14 18:49:35 +00004422#if _LIBCPP_STD_VER > 17
Marek Kurdeja98b1412020-05-02 13:58:03 +02004423template <class _CharT, class _Traits, class _Allocator, class _Up>
Marshall Clow29b53f22018-12-14 18:49:35 +00004424inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004425 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4426 erase(basic_string<_CharT, _Traits, _Allocator>& __str, const _Up& __v) {
4427 auto __old_size = __str.size();
4428 __str.erase(_VSTD::remove(__str.begin(), __str.end(), __v), __str.end());
4429 return __old_size - __str.size();
4430}
Marshall Clow29b53f22018-12-14 18:49:35 +00004431
Marek Kurdeja98b1412020-05-02 13:58:03 +02004432template <class _CharT, class _Traits, class _Allocator, class _Predicate>
Marshall Clow29b53f22018-12-14 18:49:35 +00004433inline _LIBCPP_INLINE_VISIBILITY
Marek Kurdeja98b1412020-05-02 13:58:03 +02004434 typename basic_string<_CharT, _Traits, _Allocator>::size_type
4435 erase_if(basic_string<_CharT, _Traits, _Allocator>& __str,
4436 _Predicate __pred) {
4437 auto __old_size = __str.size();
4438 __str.erase(_VSTD::remove_if(__str.begin(), __str.end(), __pred),
4439 __str.end());
4440 return __old_size - __str.size();
4441}
Marshall Clow29b53f22018-12-14 18:49:35 +00004442#endif
4443
Louis Dionneba400782020-10-02 15:02:52 -04004444#if _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004445
4446template<class _CharT, class _Traits, class _Allocator>
4447bool
4448basic_string<_CharT, _Traits, _Allocator>::__dereferenceable(const const_iterator* __i) const
4449{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004450 return this->data() <= _VSTD::__to_address(__i->base()) &&
4451 _VSTD::__to_address(__i->base()) < this->data() + this->size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004452}
4453
4454template<class _CharT, class _Traits, class _Allocator>
4455bool
4456basic_string<_CharT, _Traits, _Allocator>::__decrementable(const const_iterator* __i) const
4457{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004458 return this->data() < _VSTD::__to_address(__i->base()) &&
4459 _VSTD::__to_address(__i->base()) <= this->data() + this->size();
Howard Hinnant8ea98242013-08-23 17:37:05 +00004460}
4461
4462template<class _CharT, class _Traits, class _Allocator>
4463bool
4464basic_string<_CharT, _Traits, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const
4465{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004466 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Howard Hinnant8ea98242013-08-23 17:37:05 +00004467 return this->data() <= __p && __p <= this->data() + this->size();
4468}
4469
4470template<class _CharT, class _Traits, class _Allocator>
4471bool
4472basic_string<_CharT, _Traits, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const
4473{
Eric Fiselierc1b87a72019-11-16 17:13:26 -05004474 const value_type* __p = _VSTD::__to_address(__i->base()) + __n;
Howard Hinnant8ea98242013-08-23 17:37:05 +00004475 return this->data() <= __p && __p < this->data() + this->size();
4476}
4477
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004478#endif // _LIBCPP_DEBUG_LEVEL == 2
Howard Hinnant8ea98242013-08-23 17:37:05 +00004479
Louis Dionne173f29e2019-05-29 16:01:36 +00004480#if _LIBCPP_STD_VER > 11
Marshall Clowcba751f2013-07-23 17:05:24 +00004481// Literal suffixes for basic_string [basic.string.literals]
Marshall Clowac868372013-10-05 21:18:32 +00004482inline namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004483{
4484 inline namespace string_literals
4485 {
Howard Hinnant5c167562013-08-07 19:39:48 +00004486 inline _LIBCPP_INLINE_VISIBILITY
4487 basic_string<char> operator "" s( const char *__str, size_t __len )
4488 {
4489 return basic_string<char> (__str, __len);
4490 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004491
Louis Dionne89258142021-08-23 15:32:36 -04004492#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Howard Hinnant5c167562013-08-07 19:39:48 +00004493 inline _LIBCPP_INLINE_VISIBILITY
4494 basic_string<wchar_t> operator "" s( const wchar_t *__str, size_t __len )
4495 {
4496 return basic_string<wchar_t> (__str, __len);
4497 }
Louis Dionne89258142021-08-23 15:32:36 -04004498#endif
Marshall Clowcba751f2013-07-23 17:05:24 +00004499
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -04004500#ifndef _LIBCPP_HAS_NO_CHAR8_T
Marshall Clow8732fed2018-12-11 04:35:44 +00004501 inline _LIBCPP_INLINE_VISIBILITY
4502 basic_string<char8_t> operator "" s(const char8_t *__str, size_t __len) _NOEXCEPT
4503 {
4504 return basic_string<char8_t> (__str, __len);
4505 }
4506#endif
4507
Howard Hinnant5c167562013-08-07 19:39:48 +00004508 inline _LIBCPP_INLINE_VISIBILITY
4509 basic_string<char16_t> operator "" s( const char16_t *__str, size_t __len )
4510 {
4511 return basic_string<char16_t> (__str, __len);
4512 }
Marshall Clowcba751f2013-07-23 17:05:24 +00004513
Howard Hinnant5c167562013-08-07 19:39:48 +00004514 inline _LIBCPP_INLINE_VISIBILITY
4515 basic_string<char32_t> operator "" s( const char32_t *__str, size_t __len )
4516 {
4517 return basic_string<char32_t> (__str, __len);
4518 }
Nikolas Klauserd26407a2021-12-02 14:12:51 +01004519 } // namespace string_literals
4520} // namespace literals
Marshall Clowcba751f2013-07-23 17:05:24 +00004521#endif
4522
Howard Hinnantc51e1022010-05-11 19:42:16 +00004523_LIBCPP_END_NAMESPACE_STD
4524
Eric Fiselierf4433a32017-05-31 22:07:49 +00004525_LIBCPP_POP_MACROS
4526
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04004527#endif // _LIBCPP_STRING